2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
|
|
|
*
|
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.] */
|
|
|
|
|
|
|
|
#include <openssl/dh.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
#include <vector>
|
|
|
|
|
Do a cursory conversion of a few tests to GTest.
For now, this is the laziest conversion possible. The intent is to just
get the build setup ready so that we can get everything working in our
consumers. The intended end state is:
- The standalone build produces three test targets, one per library:
{crypto,ssl,decrepit}_tests.
- Each FOO_test is made up of:
FOO/**/*_test.cc
crypto/test/gtest_main.cc
test_support
- generate_build_files.py emits variables crypto_test_sources and
ssl_test_sources. These variables are populated with FindCFiles,
looking for *_test.cc.
- The consuming file assembles those variables into the two test targets
(plus decrepit) from there. This avoids having generate_build_files.py
emit actual build rules.
- Our standalone builders, Chromium, and Android just run the top-level
test targets using whatever GTest-based reporting story they have.
In transition, we start by converting one of two tests in each library
to populate the three test targets. Those are added to all_tests.json
and all_tests.go hacked to handle them transparently. This keeps our
standalone builder working.
generate_build_files.py, to start with, populates the new source lists
manually and subtracts them out of the old machinery. We emit both for
the time being. When this change rolls in, we'll write all the build
glue needed to build the GTest-based tests and add it to consumers'
continuous builders.
Next, we'll subsume a file-based test and get the consumers working with
that. (I.e. make sure the GTest targets can depend on a data file.)
Once that's all done, we'll be sure all this will work. At that point,
we start subsuming the remaining tests into the GTest targets and,
asynchronously, rewriting tests to use GTest properly rather than
cursory conversion here.
When all non-GTest tests are gone, the old generate_build_files.py hooks
will be removed, consumers updated to not depend on them, and standalone
builders converted to not rely on all_tests.go, which can then be
removed. (Unless bits end up being needed as a malloc test driver. I'm
thinking we'll want to do something with --gtest_filter.)
As part of this CL, I've bumped the CMake requirements (for
target_include_directories) and added a few suppressions for warnings
that GTest doesn't pass.
BUG=129
Change-Id: I881b26b07a8739cc0b52dbb51a30956908e1b71a
Reviewed-on: https://boringssl-review.googlesource.com/13232
Reviewed-by: Adam Langley <agl@google.com>
2017-01-20 00:05:47 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/bn.h>
|
2016-09-06 23:10:19 +01:00
|
|
|
#include <openssl/bytestring.h>
|
2014-09-12 00:11:15 +01:00
|
|
|
#include <openssl/crypto.h>
|
2016-08-18 04:10:28 +01:00
|
|
|
#include <openssl/dh.h>
|
2015-04-10 03:21:10 +01:00
|
|
|
#include <openssl/err.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/mem.h>
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../internal.h"
|
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
|
2016-07-12 16:09:33 +01:00
|
|
|
static bool RunBasicTests();
|
|
|
|
static bool TestBadY();
|
|
|
|
static bool TestASN1();
|
2016-08-10 18:38:51 +01:00
|
|
|
static bool TestRFC3526();
|
2016-07-12 16:09:33 +01:00
|
|
|
|
Do a cursory conversion of a few tests to GTest.
For now, this is the laziest conversion possible. The intent is to just
get the build setup ready so that we can get everything working in our
consumers. The intended end state is:
- The standalone build produces three test targets, one per library:
{crypto,ssl,decrepit}_tests.
- Each FOO_test is made up of:
FOO/**/*_test.cc
crypto/test/gtest_main.cc
test_support
- generate_build_files.py emits variables crypto_test_sources and
ssl_test_sources. These variables are populated with FindCFiles,
looking for *_test.cc.
- The consuming file assembles those variables into the two test targets
(plus decrepit) from there. This avoids having generate_build_files.py
emit actual build rules.
- Our standalone builders, Chromium, and Android just run the top-level
test targets using whatever GTest-based reporting story they have.
In transition, we start by converting one of two tests in each library
to populate the three test targets. Those are added to all_tests.json
and all_tests.go hacked to handle them transparently. This keeps our
standalone builder working.
generate_build_files.py, to start with, populates the new source lists
manually and subtracts them out of the old machinery. We emit both for
the time being. When this change rolls in, we'll write all the build
glue needed to build the GTest-based tests and add it to consumers'
continuous builders.
Next, we'll subsume a file-based test and get the consumers working with
that. (I.e. make sure the GTest targets can depend on a data file.)
Once that's all done, we'll be sure all this will work. At that point,
we start subsuming the remaining tests into the GTest targets and,
asynchronously, rewriting tests to use GTest properly rather than
cursory conversion here.
When all non-GTest tests are gone, the old generate_build_files.py hooks
will be removed, consumers updated to not depend on them, and standalone
builders converted to not rely on all_tests.go, which can then be
removed. (Unless bits end up being needed as a malloc test driver. I'm
thinking we'll want to do something with --gtest_filter.)
As part of this CL, I've bumped the CMake requirements (for
target_include_directories) and added a few suppressions for warnings
that GTest doesn't pass.
BUG=129
Change-Id: I881b26b07a8739cc0b52dbb51a30956908e1b71a
Reviewed-on: https://boringssl-review.googlesource.com/13232
Reviewed-by: Adam Langley <agl@google.com>
2017-01-20 00:05:47 +00:00
|
|
|
// TODO(davidben): Convert this file to GTest properly.
|
|
|
|
TEST(DHTest, AllTests) {
|
2016-07-12 16:09:33 +01:00
|
|
|
if (!RunBasicTests() ||
|
|
|
|
!TestBadY() ||
|
2016-08-10 18:38:51 +01:00
|
|
|
!TestASN1() ||
|
|
|
|
!TestRFC3526()) {
|
Do a cursory conversion of a few tests to GTest.
For now, this is the laziest conversion possible. The intent is to just
get the build setup ready so that we can get everything working in our
consumers. The intended end state is:
- The standalone build produces three test targets, one per library:
{crypto,ssl,decrepit}_tests.
- Each FOO_test is made up of:
FOO/**/*_test.cc
crypto/test/gtest_main.cc
test_support
- generate_build_files.py emits variables crypto_test_sources and
ssl_test_sources. These variables are populated with FindCFiles,
looking for *_test.cc.
- The consuming file assembles those variables into the two test targets
(plus decrepit) from there. This avoids having generate_build_files.py
emit actual build rules.
- Our standalone builders, Chromium, and Android just run the top-level
test targets using whatever GTest-based reporting story they have.
In transition, we start by converting one of two tests in each library
to populate the three test targets. Those are added to all_tests.json
and all_tests.go hacked to handle them transparently. This keeps our
standalone builder working.
generate_build_files.py, to start with, populates the new source lists
manually and subtracts them out of the old machinery. We emit both for
the time being. When this change rolls in, we'll write all the build
glue needed to build the GTest-based tests and add it to consumers'
continuous builders.
Next, we'll subsume a file-based test and get the consumers working with
that. (I.e. make sure the GTest targets can depend on a data file.)
Once that's all done, we'll be sure all this will work. At that point,
we start subsuming the remaining tests into the GTest targets and,
asynchronously, rewriting tests to use GTest properly rather than
cursory conversion here.
When all non-GTest tests are gone, the old generate_build_files.py hooks
will be removed, consumers updated to not depend on them, and standalone
builders converted to not rely on all_tests.go, which can then be
removed. (Unless bits end up being needed as a malloc test driver. I'm
thinking we'll want to do something with --gtest_filter.)
As part of this CL, I've bumped the CMake requirements (for
target_include_directories) and added a few suppressions for warnings
that GTest doesn't pass.
BUG=129
Change-Id: I881b26b07a8739cc0b52dbb51a30956908e1b71a
Reviewed-on: https://boringssl-review.googlesource.com/13232
Reviewed-by: Adam Langley <agl@google.com>
2017-01-20 00:05:47 +00:00
|
|
|
ADD_FAILURE() << "Tests failed.";
|
2016-07-12 16:09:33 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
|
|
|
|
static int GenerateCallback(int p, int n, BN_GENCB *arg) {
|
2014-06-20 20:00:00 +01:00
|
|
|
char c = '*';
|
|
|
|
|
2015-02-11 06:16:26 +00:00
|
|
|
if (p == 0) {
|
2014-06-20 20:00:00 +01:00
|
|
|
c = '.';
|
2015-02-11 06:16:26 +00:00
|
|
|
} else if (p == 1) {
|
2014-06-20 20:00:00 +01:00
|
|
|
c = '+';
|
2015-02-11 06:16:26 +00:00
|
|
|
} else if (p == 2) {
|
2014-06-20 20:00:00 +01:00
|
|
|
c = '*';
|
2015-02-11 06:16:26 +00:00
|
|
|
} else if (p == 3) {
|
2014-06-20 20:00:00 +01:00
|
|
|
c = '\n';
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
FILE *out = reinterpret_cast<FILE*>(arg->arg);
|
|
|
|
fputc(c, out);
|
|
|
|
fflush(out);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
static bool RunBasicTests() {
|
|
|
|
BN_GENCB cb;
|
|
|
|
BN_GENCB_set(&cb, &GenerateCallback, stdout);
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<DH> a(DH_new());
|
2015-05-09 07:27:39 +01:00
|
|
|
if (!a || !DH_generate_parameters_ex(a.get(), 64, DH_GENERATOR_5, &cb)) {
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
int check_result;
|
|
|
|
if (!DH_check(a.get(), &check_result)) {
|
|
|
|
return false;
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
if (check_result & DH_CHECK_P_NOT_PRIME) {
|
|
|
|
printf("p value is not prime\n");
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
if (check_result & DH_CHECK_P_NOT_SAFE_PRIME) {
|
|
|
|
printf("p value is not a safe prime\n");
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
if (check_result & DH_CHECK_UNABLE_TO_CHECK_GENERATOR) {
|
|
|
|
printf("unable to check the generator value\n");
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
if (check_result & DH_CHECK_NOT_SUITABLE_GENERATOR) {
|
|
|
|
printf("the g value is not a generator\n");
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\np = ");
|
2015-03-12 03:36:41 +00:00
|
|
|
BN_print_fp(stdout, a->p);
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\ng = ");
|
2015-03-12 03:36:41 +00:00
|
|
|
BN_print_fp(stdout, a->g);
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\n");
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<DH> b(DH_new());
|
2015-05-09 07:27:39 +01:00
|
|
|
if (!b) {
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
b->p = BN_dup(a->p);
|
|
|
|
b->g = BN_dup(a->g);
|
2015-05-09 07:27:39 +01:00
|
|
|
if (b->p == nullptr || b->g == nullptr) {
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
if (!DH_generate_key(a.get())) {
|
|
|
|
return false;
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("pri1 = ");
|
2015-03-12 03:36:41 +00:00
|
|
|
BN_print_fp(stdout, a->priv_key);
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\npub1 = ");
|
2015-03-12 03:36:41 +00:00
|
|
|
BN_print_fp(stdout, a->pub_key);
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\n");
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
if (!DH_generate_key(b.get())) {
|
|
|
|
return false;
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("pri2 = ");
|
2015-03-12 03:36:41 +00:00
|
|
|
BN_print_fp(stdout, b->priv_key);
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\npub2 = ");
|
2015-03-12 03:36:41 +00:00
|
|
|
BN_print_fp(stdout, b->pub_key);
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\n");
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
std::vector<uint8_t> key1(DH_size(a.get()));
|
2015-11-11 22:01:27 +00:00
|
|
|
int ret = DH_compute_key(key1.data(), b->pub_key, a.get());
|
2015-05-09 07:27:39 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
key1.resize(ret);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("key1 = ");
|
|
|
|
for (size_t i = 0; i < key1.size(); i++) {
|
|
|
|
printf("%02x", key1[i]);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
std::vector<uint8_t> key2(DH_size(b.get()));
|
2015-11-11 22:01:27 +00:00
|
|
|
ret = DH_compute_key(key2.data(), a->pub_key, b.get());
|
2015-05-09 07:27:39 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return false;
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
key2.resize(ret);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("key2 = ");
|
|
|
|
for (size_t i = 0; i < key2.size(); i++) {
|
|
|
|
printf("%02x", key2[i]);
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2015-05-09 07:27:39 +01:00
|
|
|
printf("\n");
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
if (key1.size() < 4 || key1 != key2) {
|
|
|
|
fprintf(stderr, "Error in DH routines\n");
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-09 07:27:39 +01:00
|
|
|
return true;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-16 18:36:57 +01:00
|
|
|
// The following parameters are taken from RFC 5114, section 2.2. This is not a
|
|
|
|
// safe prime. Do not use these parameters.
|
|
|
|
static const uint8_t kRFC5114_2048_224P[] = {
|
|
|
|
0xad, 0x10, 0x7e, 0x1e, 0x91, 0x23, 0xa9, 0xd0, 0xd6, 0x60, 0xfa, 0xa7,
|
|
|
|
0x95, 0x59, 0xc5, 0x1f, 0xa2, 0x0d, 0x64, 0xe5, 0x68, 0x3b, 0x9f, 0xd1,
|
|
|
|
0xb5, 0x4b, 0x15, 0x97, 0xb6, 0x1d, 0x0a, 0x75, 0xe6, 0xfa, 0x14, 0x1d,
|
|
|
|
0xf9, 0x5a, 0x56, 0xdb, 0xaf, 0x9a, 0x3c, 0x40, 0x7b, 0xa1, 0xdf, 0x15,
|
|
|
|
0xeb, 0x3d, 0x68, 0x8a, 0x30, 0x9c, 0x18, 0x0e, 0x1d, 0xe6, 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x9a, 0x99, 0xb3, 0x63, 0x71, 0xeb,
|
2015-05-09 07:27:39 +01:00
|
|
|
};
|
2016-01-28 19:57:35 +00:00
|
|
|
|
|
|
|
// kRFC5114_2048_224BadY is a bad y-coordinate for RFC 5114's 2048-bit MODP
|
|
|
|
// Group with 224-bit Prime Order Subgroup (section 2.2).
|
|
|
|
static const uint8_t kRFC5114_2048_224BadY[] = {
|
|
|
|
0x45, 0x32, 0x5f, 0x51, 0x07, 0xe5, 0xdf, 0x1c, 0xd6, 0x02, 0x82, 0xb3,
|
|
|
|
0x32, 0x8f, 0xa4, 0x0f, 0x87, 0xb8, 0x41, 0xfe, 0xb9, 0x35, 0xde, 0xad,
|
|
|
|
0xc6, 0x26, 0x85, 0xb4, 0xff, 0x94, 0x8c, 0x12, 0x4c, 0xbf, 0x5b, 0x20,
|
|
|
|
0xc4, 0x46, 0xa3, 0x26, 0xeb, 0xa4, 0x25, 0xb7, 0x68, 0x8e, 0xcc, 0x67,
|
|
|
|
0xba, 0xea, 0x58, 0xd0, 0xf2, 0xe9, 0xd2, 0x24, 0x72, 0x60, 0xda, 0x88,
|
|
|
|
0x18, 0x9c, 0xe0, 0x31, 0x6a, 0xad, 0x50, 0x6d, 0x94, 0x35, 0x8b, 0x83,
|
|
|
|
0x4a, 0x6e, 0xfa, 0x48, 0x73, 0x0f, 0x83, 0x87, 0xff, 0x6b, 0x66, 0x1f,
|
|
|
|
0xa8, 0x82, 0xc6, 0x01, 0xe5, 0x80, 0xb5, 0xb0, 0x52, 0xd0, 0xe9, 0xd8,
|
|
|
|
0x72, 0xf9, 0x7d, 0x5b, 0x8b, 0xa5, 0x4c, 0xa5, 0x25, 0x95, 0x74, 0xe2,
|
|
|
|
0x7a, 0x61, 0x4e, 0xa7, 0x8f, 0x12, 0xe2, 0xd2, 0x9d, 0x8c, 0x02, 0x70,
|
|
|
|
0x34, 0x44, 0x32, 0xc7, 0xb2, 0xf3, 0xb9, 0xfe, 0x17, 0x2b, 0xd6, 0x1f,
|
|
|
|
0x8b, 0x7e, 0x4a, 0xfa, 0xa3, 0xb5, 0x3e, 0x7a, 0x81, 0x9a, 0x33, 0x66,
|
|
|
|
0x62, 0xa4, 0x50, 0x18, 0x3e, 0xa2, 0x5f, 0x00, 0x07, 0xd8, 0x9b, 0x22,
|
|
|
|
0xe4, 0xec, 0x84, 0xd5, 0xeb, 0x5a, 0xf3, 0x2a, 0x31, 0x23, 0xd8, 0x44,
|
|
|
|
0x22, 0x2a, 0x8b, 0x37, 0x44, 0xcc, 0xc6, 0x87, 0x4b, 0xbe, 0x50, 0x9d,
|
|
|
|
0x4a, 0xc4, 0x8e, 0x45, 0xcf, 0x72, 0x4d, 0xc0, 0x89, 0xb3, 0x72, 0xed,
|
|
|
|
0x33, 0x2c, 0xbc, 0x7f, 0x16, 0x39, 0x3b, 0xeb, 0xd2, 0xdd, 0xa8, 0x01,
|
|
|
|
0x73, 0x84, 0x62, 0xb9, 0x29, 0xd2, 0xc9, 0x51, 0x32, 0x9e, 0x7a, 0x6a,
|
|
|
|
0xcf, 0xc1, 0x0a, 0xdb, 0x0e, 0xe0, 0x62, 0x77, 0x6f, 0x59, 0x62, 0x72,
|
|
|
|
0x5a, 0x69, 0xa6, 0x5b, 0x70, 0xca, 0x65, 0xc4, 0x95, 0x6f, 0x9a, 0xc2,
|
|
|
|
0xdf, 0x72, 0x6d, 0xb1, 0x1e, 0x54, 0x7b, 0x51, 0xb4, 0xef, 0x7f, 0x89,
|
|
|
|
0x93, 0x74, 0x89, 0x59,
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool TestBadY() {
|
2017-08-16 18:36:57 +01:00
|
|
|
bssl::UniquePtr<DH> dh(DH_new());
|
|
|
|
dh->p = BN_bin2bn(kRFC5114_2048_224P, sizeof(kRFC5114_2048_224P), nullptr);
|
|
|
|
dh->g = BN_bin2bn(kRFC5114_2048_224G, sizeof(kRFC5114_2048_224G), nullptr);
|
|
|
|
dh->q = BN_bin2bn(kRFC5114_2048_224Q, sizeof(kRFC5114_2048_224Q), nullptr);
|
|
|
|
if (!dh->p || !dh->g || !dh->q) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BIGNUM> pub_key(
|
2016-01-28 19:57:35 +00:00
|
|
|
BN_bin2bn(kRFC5114_2048_224BadY, sizeof(kRFC5114_2048_224BadY), nullptr));
|
|
|
|
if (!dh || !pub_key || !DH_generate_key(dh.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
if (!DH_check_pub_key(dh.get(), pub_key.get(), &flags)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(flags & DH_CHECK_PUBKEY_INVALID)) {
|
|
|
|
fprintf(stderr, "DH_check_pub_key did not reject the key.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> result(DH_size(dh.get()));
|
|
|
|
if (DH_compute_key(result.data(), pub_key.get(), dh.get()) >= 0) {
|
|
|
|
fprintf(stderr, "DH_compute_key unexpectedly succeeded.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-07 22:40:02 +01:00
|
|
|
|
|
|
|
static bool BIGNUMEqualsHex(const BIGNUM *bn, const char *hex) {
|
|
|
|
BIGNUM *hex_bn = NULL;
|
|
|
|
if (!BN_hex2bn(&hex_bn, hex)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BIGNUM> free_hex_bn(hex_bn);
|
2016-05-07 22:40:02 +01:00
|
|
|
return BN_cmp(bn, hex_bn) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool TestASN1() {
|
|
|
|
// kParams are a set of Diffie-Hellman parameters generated with
|
|
|
|
// openssl dhparam 256
|
|
|
|
static const uint8_t kParams[] = {
|
|
|
|
0x30, 0x26, 0x02, 0x21, 0x00, 0xd7, 0x20, 0x34, 0xa3, 0x27,
|
|
|
|
0x4f, 0xdf, 0xbf, 0x04, 0xfd, 0x24, 0x68, 0x25, 0xb6, 0x56,
|
|
|
|
0xd8, 0xab, 0x2a, 0x41, 0x2d, 0x74, 0x0a, 0x52, 0x08, 0x7c,
|
|
|
|
0x40, 0x71, 0x4e, 0xd2, 0x57, 0x93, 0x13, 0x02, 0x01, 0x02,
|
|
|
|
};
|
|
|
|
|
|
|
|
CBS cbs;
|
|
|
|
CBS_init(&cbs, kParams, sizeof(kParams));
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<DH> dh(DH_parse_parameters(&cbs));
|
2016-05-07 22:40:02 +01:00
|
|
|
if (!dh || CBS_len(&cbs) != 0 ||
|
|
|
|
!BIGNUMEqualsHex(
|
|
|
|
dh->p,
|
|
|
|
"d72034a3274fdfbf04fd246825b656d8ab2a412d740a52087c40714ed2579313") ||
|
|
|
|
!BIGNUMEqualsHex(dh->g, "2") || dh->priv_length != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-07 03:35:41 +00:00
|
|
|
bssl::ScopedCBB cbb;
|
2016-05-07 22:40:02 +01:00
|
|
|
uint8_t *der;
|
|
|
|
size_t der_len;
|
|
|
|
if (!CBB_init(cbb.get(), 0) ||
|
|
|
|
!DH_marshal_parameters(cbb.get(), dh.get()) ||
|
|
|
|
!CBB_finish(cbb.get(), &der, &der_len)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<uint8_t> free_der(der);
|
2016-12-13 06:07:13 +00:00
|
|
|
if (der_len != sizeof(kParams) ||
|
|
|
|
OPENSSL_memcmp(der, kParams, der_len) != 0) {
|
2016-05-07 22:40:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// kParamsDSA are a set of Diffie-Hellman parameters generated with
|
|
|
|
// openssl dhparam 256 -dsaparam
|
|
|
|
static const uint8_t kParamsDSA[] = {
|
|
|
|
0x30, 0x81, 0x89, 0x02, 0x41, 0x00, 0x93, 0xf3, 0xc1, 0x18, 0x01, 0xe6,
|
|
|
|
0x62, 0xb6, 0xd1, 0x46, 0x9a, 0x2c, 0x72, 0xea, 0x31, 0xd9, 0x18, 0x10,
|
|
|
|
0x30, 0x28, 0x63, 0xe2, 0x34, 0x7d, 0x80, 0xca, 0xee, 0x82, 0x2b, 0x19,
|
|
|
|
0x3c, 0x19, 0xbb, 0x42, 0x83, 0x02, 0x70, 0xdd, 0xdb, 0x8c, 0x03, 0xab,
|
|
|
|
0xe9, 0x9c, 0xc4, 0x00, 0x4d, 0x70, 0x5f, 0x52, 0x03, 0x31, 0x2c, 0xa4,
|
|
|
|
0x67, 0x34, 0x51, 0x95, 0x2a, 0xac, 0x11, 0xe2, 0x6a, 0x55, 0x02, 0x40,
|
|
|
|
0x44, 0xc8, 0x10, 0x53, 0x44, 0x32, 0x31, 0x63, 0xd8, 0xd1, 0x8c, 0x75,
|
|
|
|
0xc8, 0x98, 0x53, 0x3b, 0x5b, 0x4a, 0x2a, 0x0a, 0x09, 0xe7, 0xd0, 0x3c,
|
|
|
|
0x53, 0x72, 0xa8, 0x6b, 0x70, 0x41, 0x9c, 0x26, 0x71, 0x44, 0xfc, 0x7f,
|
|
|
|
0x08, 0x75, 0xe1, 0x02, 0xab, 0x74, 0x41, 0xe8, 0x2a, 0x3d, 0x3c, 0x26,
|
|
|
|
0x33, 0x09, 0xe4, 0x8b, 0xb4, 0x41, 0xec, 0xa6, 0xa8, 0xba, 0x1a, 0x07,
|
|
|
|
0x8a, 0x77, 0xf5, 0x5f, 0x02, 0x02, 0x00, 0xa0,
|
|
|
|
};
|
|
|
|
|
|
|
|
CBS_init(&cbs, kParamsDSA, sizeof(kParamsDSA));
|
|
|
|
dh.reset(DH_parse_parameters(&cbs));
|
|
|
|
if (!dh || CBS_len(&cbs) != 0 ||
|
|
|
|
!BIGNUMEqualsHex(dh->p,
|
|
|
|
"93f3c11801e662b6d1469a2c72ea31d91810302863e2347d80caee8"
|
|
|
|
"22b193c19bb42830270dddb8c03abe99cc4004d705f5203312ca467"
|
|
|
|
"3451952aac11e26a55") ||
|
|
|
|
!BIGNUMEqualsHex(dh->g,
|
|
|
|
"44c8105344323163d8d18c75c898533b5b4a2a0a09e7d03c5372a86"
|
|
|
|
"b70419c267144fc7f0875e102ab7441e82a3d3c263309e48bb441ec"
|
|
|
|
"a6a8ba1a078a77f55f") ||
|
|
|
|
dh->priv_length != 160) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CBB_init(cbb.get(), 0) ||
|
|
|
|
!DH_marshal_parameters(cbb.get(), dh.get()) ||
|
|
|
|
!CBB_finish(cbb.get(), &der, &der_len)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<uint8_t> free_der2(der);
|
2016-12-13 06:07:13 +00:00
|
|
|
if (der_len != sizeof(kParamsDSA) ||
|
|
|
|
OPENSSL_memcmp(der, kParamsDSA, der_len) != 0) {
|
2016-05-07 22:40:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-10 18:38:51 +01:00
|
|
|
|
|
|
|
static bool TestRFC3526() {
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BIGNUM> bn(BN_get_rfc3526_prime_1536(nullptr));
|
2016-08-10 18:38:51 +01:00
|
|
|
if (!bn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t kPrime1536[] = {
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2,
|
|
|
|
0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1,
|
|
|
|
0x29, 0x02, 0x4e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
|
|
|
|
|
|
uint8_t buffer[sizeof(kPrime1536)];
|
|
|
|
if (BN_num_bytes(bn.get()) != sizeof(kPrime1536) ||
|
|
|
|
BN_bn2bin(bn.get(), buffer) != sizeof(kPrime1536) ||
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcmp(buffer, kPrime1536, sizeof(kPrime1536)) != 0) {
|
2016-08-10 18:38:51 +01:00
|
|
|
fprintf(stderr, "1536-bit MODP prime did not match.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|