You've already forked linux-packaging-mono
Imported Upstream version 4.8.0.309
Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
This commit is contained in:
parent
ee1447783b
commit
94b2861243
24
external/boringssl/crypto/ecdsa/CMakeLists.txt
vendored
Normal file
24
external/boringssl/crypto/ecdsa/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
include_directories(../../include)
|
||||
|
||||
add_library(
|
||||
ecdsa
|
||||
|
||||
OBJECT
|
||||
|
||||
ecdsa.c
|
||||
ecdsa_asn1.c
|
||||
)
|
||||
|
||||
|
||||
if(ENABLE_TESTS)
|
||||
add_executable(
|
||||
ecdsa_test
|
||||
|
||||
ecdsa_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
target_link_libraries(ecdsa_test crypto)
|
||||
add_dependencies(all_tests ecdsa_test)
|
||||
endif()
|
||||
487
external/boringssl/crypto/ecdsa/ecdsa.c
vendored
Normal file
487
external/boringssl/crypto/ecdsa/ecdsa.c
vendored
Normal file
@@ -0,0 +1,487 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
|
||||
*
|
||||
* 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 above 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 acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please contact
|
||||
* openssl-core@OpenSSL.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "OpenSSL"
|
||||
* nor may "OpenSSL" appear in their names without prior written
|
||||
* permission of the OpenSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED 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 OpenSSL PROJECT OR
|
||||
* ITS 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This product includes cryptographic software written by Eric Young
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
#include <openssl/ecdsa.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/bytestring.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/mem.h>
|
||||
|
||||
#include "../ec/internal.h"
|
||||
|
||||
|
||||
int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
|
||||
unsigned int *sig_len, EC_KEY *eckey) {
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len, eckey);
|
||||
}
|
||||
|
||||
return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
|
||||
eckey);
|
||||
}
|
||||
|
||||
int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
|
||||
const uint8_t *sig, size_t sig_len, EC_KEY *eckey) {
|
||||
ECDSA_SIG *s;
|
||||
int ret = 0;
|
||||
uint8_t *der = NULL;
|
||||
|
||||
/* Decode the ECDSA signature. */
|
||||
s = ECDSA_SIG_from_bytes(sig, sig_len);
|
||||
if (s == NULL) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Defend against potential laxness in the DER parser. */
|
||||
size_t der_len;
|
||||
if (!ECDSA_SIG_to_bytes(&der, &der_len, s) ||
|
||||
der_len != sig_len || memcmp(sig, der, sig_len) != 0) {
|
||||
/* This should never happen. crypto/bytestring is strictly DER. */
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = ECDSA_do_verify(digest, digest_len, s, eckey);
|
||||
|
||||
err:
|
||||
OPENSSL_free(der);
|
||||
ECDSA_SIG_free(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian
|
||||
* number and sets |out| to that value. It then truncates |out| so that it's,
|
||||
* at most, as long as |order|. It returns one on success and zero otherwise. */
|
||||
static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len,
|
||||
const BIGNUM *order) {
|
||||
size_t num_bits;
|
||||
|
||||
num_bits = BN_num_bits(order);
|
||||
/* Need to truncate digest if it is too long: first truncate whole
|
||||
* bytes. */
|
||||
if (8 * digest_len > num_bits) {
|
||||
digest_len = (num_bits + 7) / 8;
|
||||
}
|
||||
if (!BN_bin2bn(digest, digest_len, out)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If still too long truncate remaining bits with a shift */
|
||||
if ((8 * digest_len > num_bits) &&
|
||||
!BN_rshift(out, out, 8 - (num_bits & 0x7))) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
|
||||
EC_KEY *key) {
|
||||
return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
|
||||
}
|
||||
|
||||
int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
|
||||
const ECDSA_SIG *sig, EC_KEY *eckey) {
|
||||
int ret = 0;
|
||||
BN_CTX *ctx;
|
||||
BIGNUM *u1, *u2, *m, *X;
|
||||
EC_POINT *point = NULL;
|
||||
const EC_GROUP *group;
|
||||
const EC_POINT *pub_key;
|
||||
|
||||
/* check input values */
|
||||
if ((group = EC_KEY_get0_group(eckey)) == NULL ||
|
||||
(pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
|
||||
sig == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx = BN_CTX_new();
|
||||
if (!ctx) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
BN_CTX_start(ctx);
|
||||
u1 = BN_CTX_get(ctx);
|
||||
u2 = BN_CTX_get(ctx);
|
||||
m = BN_CTX_get(ctx);
|
||||
X = BN_CTX_get(ctx);
|
||||
if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
|
||||
BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
|
||||
BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
|
||||
ret = 0; /* signature is invalid */
|
||||
goto err;
|
||||
}
|
||||
/* calculate tmp1 = inv(S) mod order */
|
||||
if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!digest_to_bn(m, digest, digest_len, order)) {
|
||||
goto err;
|
||||
}
|
||||
/* u1 = m * tmp mod order */
|
||||
if (!BN_mod_mul(u1, m, u2, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
/* u2 = r * w mod q */
|
||||
if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
point = EC_POINT_new(group);
|
||||
if (point == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!BN_nnmod(u1, X, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
/* if the signature is correct u1 is equal to sig->r */
|
||||
ret = (BN_ucmp(u1, sig->r) == 0);
|
||||
|
||||
err:
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
EC_POINT_free(point);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
|
||||
BIGNUM **rp, const uint8_t *digest,
|
||||
size_t digest_len) {
|
||||
BN_CTX *ctx = NULL;
|
||||
BIGNUM *k = NULL, *r = NULL, *X = NULL;
|
||||
EC_POINT *tmp_point = NULL;
|
||||
const EC_GROUP *group;
|
||||
int ret = 0;
|
||||
|
||||
if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx_in == NULL) {
|
||||
if ((ctx = BN_CTX_new()) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
ctx = ctx_in;
|
||||
}
|
||||
|
||||
k = BN_new(); /* this value is later returned in *kinvp */
|
||||
r = BN_new(); /* this value is later returned in *rp */
|
||||
X = BN_new();
|
||||
if (k == NULL || r == NULL || X == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
tmp_point = EC_POINT_new(group);
|
||||
if (tmp_point == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
|
||||
do {
|
||||
/* If possible, we'll include the private key and message digest in the k
|
||||
* generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
|
||||
* being used. */
|
||||
do {
|
||||
int ok;
|
||||
|
||||
if (digest_len > 0) {
|
||||
ok = BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
|
||||
digest, digest_len, ctx);
|
||||
} else {
|
||||
ok = BN_rand_range(k, order);
|
||||
}
|
||||
if (!ok) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
} while (BN_is_zero(k));
|
||||
|
||||
/* We do not want timing information to leak the length of k,
|
||||
* so we compute G*k using an equivalent scalar of fixed
|
||||
* bit-length. */
|
||||
|
||||
if (!BN_add(k, k, order)) {
|
||||
goto err;
|
||||
}
|
||||
if (BN_num_bits(k) <= BN_num_bits(order)) {
|
||||
if (!BN_add(k, k, order)) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
/* compute r the x-coordinate of generator * k */
|
||||
if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X, NULL, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!BN_nnmod(r, X, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
} while (BN_is_zero(r));
|
||||
|
||||
/* compute the inverse of k */
|
||||
if (ec_group_get_mont_data(group) != NULL) {
|
||||
/* We want inverse in constant time, therefore we use that the order must
|
||||
* be prime and thus we can use Fermat's Little Theorem. */
|
||||
if (!BN_set_word(X, 2) ||
|
||||
!BN_sub(X, order, X)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
BN_set_flags(X, BN_FLG_CONSTTIME);
|
||||
if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx,
|
||||
ec_group_get_mont_data(group))) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
} else if (!BN_mod_inverse(k, k, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
/* clear old values if necessary */
|
||||
BN_clear_free(*rp);
|
||||
BN_clear_free(*kinvp);
|
||||
|
||||
/* save the pre-computed values */
|
||||
*rp = r;
|
||||
*kinvp = k;
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
if (!ret) {
|
||||
BN_clear_free(k);
|
||||
BN_clear_free(r);
|
||||
}
|
||||
if (ctx_in == NULL) {
|
||||
BN_CTX_free(ctx);
|
||||
}
|
||||
EC_POINT_free(tmp_point);
|
||||
BN_clear_free(X);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp) {
|
||||
return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
|
||||
const BIGNUM *in_kinv, const BIGNUM *in_r,
|
||||
EC_KEY *eckey) {
|
||||
int ok = 0;
|
||||
BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
|
||||
const BIGNUM *ckinv;
|
||||
BN_CTX *ctx = NULL;
|
||||
const EC_GROUP *group;
|
||||
ECDSA_SIG *ret;
|
||||
const BIGNUM *priv_key;
|
||||
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
group = EC_KEY_get0_group(eckey);
|
||||
priv_key = EC_KEY_get0_private_key(eckey);
|
||||
|
||||
if (group == NULL || priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = ECDSA_SIG_new();
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
s = ret->s;
|
||||
|
||||
if ((ctx = BN_CTX_new()) == NULL ||
|
||||
(tmp = BN_new()) == NULL ||
|
||||
(m = BN_new()) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
|
||||
if (!digest_to_bn(m, digest, digest_len, order)) {
|
||||
goto err;
|
||||
}
|
||||
for (;;) {
|
||||
if (in_kinv == NULL || in_r == NULL) {
|
||||
if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB);
|
||||
goto err;
|
||||
}
|
||||
ckinv = kinv;
|
||||
} else {
|
||||
ckinv = in_kinv;
|
||||
if (BN_copy(ret->r, in_r) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!BN_mod_add_quick(s, tmp, m, order)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (BN_is_zero(s)) {
|
||||
/* if kinv and r have been supplied by the caller
|
||||
* don't to generate new kinv and r values */
|
||||
if (in_kinv != NULL && in_r != NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
/* s != 0 => we have a valid signature */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
if (!ok) {
|
||||
ECDSA_SIG_free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
BN_CTX_free(ctx);
|
||||
BN_clear_free(m);
|
||||
BN_clear_free(tmp);
|
||||
BN_clear_free(kinv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ECDSA_sign_ex(int type, const uint8_t *digest, size_t digest_len,
|
||||
uint8_t *sig, unsigned int *sig_len, const BIGNUM *kinv,
|
||||
const BIGNUM *r, EC_KEY *eckey) {
|
||||
int ret = 0;
|
||||
ECDSA_SIG *s = NULL;
|
||||
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
|
||||
*sig_len = 0;
|
||||
goto err;
|
||||
}
|
||||
|
||||
s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
|
||||
if (s == NULL) {
|
||||
*sig_len = 0;
|
||||
goto err;
|
||||
}
|
||||
|
||||
CBB cbb;
|
||||
CBB_zero(&cbb);
|
||||
size_t len;
|
||||
if (!CBB_init_fixed(&cbb, sig, ECDSA_size(eckey)) ||
|
||||
!ECDSA_SIG_marshal(&cbb, s) ||
|
||||
!CBB_finish(&cbb, NULL, &len)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
|
||||
CBB_cleanup(&cbb);
|
||||
*sig_len = 0;
|
||||
goto err;
|
||||
}
|
||||
*sig_len = (unsigned)len;
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
ECDSA_SIG_free(s);
|
||||
return ret;
|
||||
}
|
||||
227
external/boringssl/crypto/ecdsa/ecdsa_asn1.c
vendored
Normal file
227
external/boringssl/crypto/ecdsa/ecdsa_asn1.c
vendored
Normal file
@@ -0,0 +1,227 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
|
||||
*
|
||||
* 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 above 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 acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please contact
|
||||
* openssl-core@OpenSSL.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "OpenSSL"
|
||||
* nor may "OpenSSL" appear in their names without prior written
|
||||
* permission of the OpenSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED 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 OpenSSL PROJECT OR
|
||||
* ITS 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This product includes cryptographic software written by Eric Young
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
#include <openssl/ecdsa.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/bytestring.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ec_key.h>
|
||||
#include <openssl/mem.h>
|
||||
|
||||
#include "../bytestring/internal.h"
|
||||
#include "../ec/internal.h"
|
||||
|
||||
|
||||
size_t ECDSA_size(const EC_KEY *key) {
|
||||
if (key == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t group_order_size;
|
||||
if (key->ecdsa_meth && key->ecdsa_meth->group_order_size) {
|
||||
group_order_size = key->ecdsa_meth->group_order_size(key);
|
||||
} else {
|
||||
const EC_GROUP *group = EC_KEY_get0_group(key);
|
||||
if (group == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
group_order_size = BN_num_bytes(EC_GROUP_get0_order(group));
|
||||
}
|
||||
|
||||
return ECDSA_SIG_max_len(group_order_size);
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_SIG_new(void) {
|
||||
ECDSA_SIG *sig = OPENSSL_malloc(sizeof(ECDSA_SIG));
|
||||
if (sig == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
sig->r = BN_new();
|
||||
sig->s = BN_new();
|
||||
if (sig->r == NULL || sig->s == NULL) {
|
||||
ECDSA_SIG_free(sig);
|
||||
return NULL;
|
||||
}
|
||||
return sig;
|
||||
}
|
||||
|
||||
void ECDSA_SIG_free(ECDSA_SIG *sig) {
|
||||
if (sig == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
BN_free(sig->r);
|
||||
BN_free(sig->s);
|
||||
OPENSSL_free(sig);
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_SIG_parse(CBS *cbs) {
|
||||
ECDSA_SIG *ret = ECDSA_SIG_new();
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
CBS child;
|
||||
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!BN_parse_asn1_unsigned(&child, ret->r) ||
|
||||
!BN_parse_asn1_unsigned(&child, ret->s) ||
|
||||
CBS_len(&child) != 0) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
|
||||
ECDSA_SIG_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_SIG_from_bytes(const uint8_t *in, size_t in_len) {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, in_len);
|
||||
ECDSA_SIG *ret = ECDSA_SIG_parse(&cbs);
|
||||
if (ret == NULL || CBS_len(&cbs) != 0) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
|
||||
ECDSA_SIG_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_marshal(CBB *cbb, const ECDSA_SIG *sig) {
|
||||
CBB child;
|
||||
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!BN_marshal_asn1(&child, sig->r) ||
|
||||
!BN_marshal_asn1(&child, sig->s) ||
|
||||
!CBB_flush(cbb)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len,
|
||||
const ECDSA_SIG *sig) {
|
||||
CBB cbb;
|
||||
CBB_zero(&cbb);
|
||||
if (!CBB_init(&cbb, 0) ||
|
||||
!ECDSA_SIG_marshal(&cbb, sig) ||
|
||||
!CBB_finish(&cbb, out_bytes, out_len)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
|
||||
CBB_cleanup(&cbb);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* der_len_len returns the number of bytes needed to represent a length of |len|
|
||||
* in DER. */
|
||||
static size_t der_len_len(size_t len) {
|
||||
if (len < 0x80) {
|
||||
return 1;
|
||||
}
|
||||
size_t ret = 1;
|
||||
while (len > 0) {
|
||||
ret++;
|
||||
len >>= 8;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t ECDSA_SIG_max_len(size_t order_len) {
|
||||
/* Compute the maximum length of an |order_len| byte integer. Defensively
|
||||
* assume that the leading 0x00 is included. */
|
||||
size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
|
||||
if (integer_len < order_len) {
|
||||
return 0;
|
||||
}
|
||||
/* An ECDSA signature is two INTEGERs. */
|
||||
size_t value_len = 2 * integer_len;
|
||||
if (value_len < integer_len) {
|
||||
return 0;
|
||||
}
|
||||
/* Add the header. */
|
||||
size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
|
||||
if (ret < value_len) {
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **out, const uint8_t **inp, long len) {
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
ECDSA_SIG *ret = ECDSA_SIG_parse(&cbs);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (out != NULL) {
|
||||
ECDSA_SIG_free(*out);
|
||||
*out = ret;
|
||||
}
|
||||
*inp = CBS_data(&cbs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int i2d_ECDSA_SIG(const ECDSA_SIG *sig, uint8_t **outp) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 0) ||
|
||||
!ECDSA_SIG_marshal(&cbb, sig)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
363
external/boringssl/crypto/ecdsa/ecdsa_test.cc
vendored
Normal file
363
external/boringssl/crypto/ecdsa/ecdsa_test.cc
vendored
Normal file
@@ -0,0 +1,363 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
|
||||
*
|
||||
* 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 above 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 acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please contact
|
||||
* openssl-core@OpenSSL.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "OpenSSL"
|
||||
* nor may "OpenSSL" appear in their names without prior written
|
||||
* permission of the OpenSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED 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 OpenSSL PROJECT OR
|
||||
* ITS 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This product includes cryptographic software written by Eric Young
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
#include <openssl/ecdsa.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/mem.h>
|
||||
#include <openssl/nid.h>
|
||||
#include <openssl/rand.h>
|
||||
|
||||
#include "../test/scoped_types.h"
|
||||
|
||||
enum Api {
|
||||
kEncodedApi,
|
||||
kRawApi,
|
||||
};
|
||||
|
||||
// VerifyECDSASig returns true on success, false on failure.
|
||||
static bool VerifyECDSASig(Api api, const uint8_t *digest,
|
||||
size_t digest_len, const ECDSA_SIG *ecdsa_sig,
|
||||
EC_KEY *eckey, int expected_result) {
|
||||
int actual_result;
|
||||
|
||||
switch (api) {
|
||||
case kEncodedApi: {
|
||||
uint8_t *der;
|
||||
size_t der_len;
|
||||
if (!ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig)) {
|
||||
return false;
|
||||
}
|
||||
ScopedOpenSSLBytes delete_der(der);
|
||||
actual_result = ECDSA_verify(0, digest, digest_len, der, der_len, eckey);
|
||||
break;
|
||||
}
|
||||
|
||||
case kRawApi:
|
||||
actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey);
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return expected_result == actual_result;
|
||||
}
|
||||
|
||||
// TestTamperedSig verifies that signature verification fails when a valid
|
||||
// signature is tampered with. |ecdsa_sig| must be a valid signature, which will
|
||||
// be modified. TestTamperedSig returns true on success, false on failure.
|
||||
static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest,
|
||||
size_t digest_len, ECDSA_SIG *ecdsa_sig,
|
||||
EC_KEY *eckey, const BIGNUM *order) {
|
||||
// Modify a single byte of the signature: to ensure we don't
|
||||
// garble the ASN1 structure, we read the raw signature and
|
||||
// modify a byte in one of the bignums directly.
|
||||
|
||||
// Store the two BIGNUMs in raw_buf.
|
||||
size_t r_len = BN_num_bytes(ecdsa_sig->r);
|
||||
size_t s_len = BN_num_bytes(ecdsa_sig->s);
|
||||
size_t bn_len = BN_num_bytes(order);
|
||||
if (r_len > bn_len || s_len > bn_len) {
|
||||
return false;
|
||||
}
|
||||
size_t buf_len = 2 * bn_len;
|
||||
std::vector<uint8_t> raw_buf(buf_len);
|
||||
// Pad the bignums with leading zeroes.
|
||||
if (!BN_bn2bin_padded(raw_buf.data(), bn_len, ecdsa_sig->r) ||
|
||||
!BN_bn2bin_padded(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Modify a single byte in the buffer.
|
||||
size_t offset = raw_buf[10] % buf_len;
|
||||
uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1;
|
||||
raw_buf[offset] ^= dirt;
|
||||
// Now read the BIGNUMs back in from raw_buf.
|
||||
if (BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r) == NULL ||
|
||||
BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s) == NULL ||
|
||||
!VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sanity check: Undo the modification and verify signature.
|
||||
raw_buf[offset] ^= dirt;
|
||||
if (BN_bin2bn(raw_buf.data(), bn_len, ecdsa_sig->r) == NULL ||
|
||||
BN_bin2bn(raw_buf.data() + bn_len, bn_len, ecdsa_sig->s) == NULL ||
|
||||
!VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool TestBuiltin(FILE *out) {
|
||||
// Fill digest values with some random data.
|
||||
uint8_t digest[20], wrong_digest[20];
|
||||
if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) {
|
||||
fprintf(out, "ERROR: unable to get random data\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
static const struct {
|
||||
int nid;
|
||||
const char *name;
|
||||
} kCurves[] = {
|
||||
{ NID_secp224r1, "secp224r1" },
|
||||
{ NID_X9_62_prime256v1, "secp256r1" },
|
||||
{ NID_secp384r1, "secp384r1" },
|
||||
{ NID_secp521r1, "secp521r1" },
|
||||
{ NID_undef, NULL }
|
||||
};
|
||||
|
||||
// Create and verify ECDSA signatures with every available curve.
|
||||
fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and "
|
||||
"ECDSA_do_verify() with some internal curves:\n", out);
|
||||
|
||||
for (size_t n = 0; kCurves[n].nid != NID_undef; n++) {
|
||||
fprintf(out, "%s: ", kCurves[n].name);
|
||||
|
||||
int nid = kCurves[n].nid;
|
||||
ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(nid));
|
||||
if (!group) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group.get());
|
||||
if (BN_num_bits(order) < 160) {
|
||||
// Too small to test.
|
||||
fprintf(out, " skipped\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create a new ECDSA key.
|
||||
ScopedEC_KEY eckey(EC_KEY_new());
|
||||
if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) ||
|
||||
!EC_KEY_generate_key(eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
// Create a second key.
|
||||
ScopedEC_KEY wrong_eckey(EC_KEY_new());
|
||||
if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) ||
|
||||
!EC_KEY_generate_key(wrong_eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
|
||||
// Check the key.
|
||||
if (!EC_KEY_check_key(eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
|
||||
// Test ASN.1-encoded signatures.
|
||||
// Create a signature.
|
||||
unsigned sig_len = ECDSA_size(eckey.get());
|
||||
std::vector<uint8_t> signature(sig_len);
|
||||
if (!ECDSA_sign(0, digest, 20, signature.data(), &sig_len, eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
signature.resize(sig_len);
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify the signature.
|
||||
if (!ECDSA_verify(0, digest, 20, signature.data(), signature.size(),
|
||||
eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify the signature with the wrong key.
|
||||
if (ECDSA_verify(0, digest, 20, signature.data(), signature.size(),
|
||||
wrong_eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify the signature using the wrong digest.
|
||||
if (ECDSA_verify(0, wrong_digest, 20, signature.data(), signature.size(),
|
||||
eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify a truncated signature.
|
||||
if (ECDSA_verify(0, digest, 20, signature.data(), signature.size() - 1,
|
||||
eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify a tampered signature.
|
||||
ScopedECDSA_SIG ecdsa_sig(ECDSA_SIG_from_bytes(
|
||||
signature.data(), signature.size()));
|
||||
if (!ecdsa_sig ||
|
||||
!TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(),
|
||||
eckey.get(), order)) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
|
||||
// Test ECDSA_SIG signing and verification.
|
||||
// Create a signature.
|
||||
ecdsa_sig.reset(ECDSA_do_sign(digest, 20, eckey.get()));
|
||||
if (!ecdsa_sig) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify the signature using the correct key.
|
||||
if (!ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify the signature with the wrong key.
|
||||
if (ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify the signature using the wrong digest.
|
||||
if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get())) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
// Verify a tampered signature.
|
||||
if (!TestTamperedSig(out, kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(),
|
||||
order)) {
|
||||
fprintf(out, " failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(out, ".");
|
||||
fflush(out);
|
||||
|
||||
fprintf(out, " ok\n");
|
||||
// Clear bogus errors.
|
||||
ERR_clear_error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool TestECDSA_SIG_max_len(size_t order_len) {
|
||||
/* Create the largest possible |ECDSA_SIG| of the given constraints. */
|
||||
ScopedECDSA_SIG sig(ECDSA_SIG_new());
|
||||
if (!sig) {
|
||||
return false;
|
||||
}
|
||||
std::vector<uint8_t> bytes(order_len, 0xff);
|
||||
if (!BN_bin2bn(bytes.data(), bytes.size(), sig->r) ||
|
||||
!BN_bin2bn(bytes.data(), bytes.size(), sig->s)) {
|
||||
return false;
|
||||
}
|
||||
/* Serialize it. */
|
||||
uint8_t *der;
|
||||
size_t der_len;
|
||||
if (!ECDSA_SIG_to_bytes(&der, &der_len, sig.get())) {
|
||||
return false;
|
||||
}
|
||||
ScopedOpenSSLBytes delete_der(der);
|
||||
|
||||
size_t max_len = ECDSA_SIG_max_len(order_len);
|
||||
if (max_len != der_len) {
|
||||
fprintf(stderr, "ECDSA_SIG_max_len(%u) returned %u, wanted %u\n",
|
||||
static_cast<unsigned>(order_len), static_cast<unsigned>(max_len),
|
||||
static_cast<unsigned>(der_len));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t BitsToBytes(size_t bits) {
|
||||
return (bits / 8) + (7 + (bits % 8)) / 8;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
CRYPTO_library_init();
|
||||
|
||||
if (!TestBuiltin(stdout) ||
|
||||
!TestECDSA_SIG_max_len(BitsToBytes(224)) ||
|
||||
!TestECDSA_SIG_max_len(BitsToBytes(256)) ||
|
||||
!TestECDSA_SIG_max_len(BitsToBytes(384)) ||
|
||||
!TestECDSA_SIG_max_len(BitsToBytes(521)) ||
|
||||
!TestECDSA_SIG_max_len(BitsToBytes(10000))) {
|
||||
printf("\nECDSA test failed\n");
|
||||
ERR_print_errors_fp(stdout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("\nPASS\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user