diff options
| author | tb <> | 2019-01-19 01:07:00 +0000 |
|---|---|---|
| committer | tb <> | 2019-01-19 01:07:00 +0000 |
| commit | aa769d92fad41004606a446424dde716784d7854 (patch) | |
| tree | f28042e7a3c924e4bf846d6cded984e02699177d /src/lib/libcrypto/ecdsa | |
| parent | 640fa09d8cf557a05ae0593b939b6f5c57397e06 (diff) | |
| download | openbsd-aa769d92fad41004606a446424dde716784d7854.tar.gz openbsd-aa769d92fad41004606a446424dde716784d7854.tar.bz2 openbsd-aa769d92fad41004606a446424dde716784d7854.zip | |
Partial port of EC_KEY_METHOD from OpenSSL 1.1.
This commit adds init/free, support for signing, setting and
getting the method, engine support as well as extra data.
from markus
Diffstat (limited to 'src/lib/libcrypto/ecdsa')
| -rw-r--r-- | src/lib/libcrypto/ecdsa/ecdsa.h | 22 | ||||
| -rw-r--r-- | src/lib/libcrypto/ecdsa/ecs_locl.h | 10 | ||||
| -rw-r--r-- | src/lib/libcrypto/ecdsa/ecs_ossl.c | 39 | ||||
| -rw-r--r-- | src/lib/libcrypto/ecdsa/ecs_sign.c | 38 |
4 files changed, 84 insertions, 25 deletions
diff --git a/src/lib/libcrypto/ecdsa/ecdsa.h b/src/lib/libcrypto/ecdsa/ecdsa.h index 9c53230a88..12d6677ce3 100644 --- a/src/lib/libcrypto/ecdsa/ecdsa.h +++ b/src/lib/libcrypto/ecdsa/ecdsa.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ecdsa.h,v 1.5 2018/03/17 15:24:44 tb Exp $ */ | 1 | /* $OpenBSD: ecdsa.h,v 1.6 2019/01/19 01:07:00 tb Exp $ */ |
| 2 | /** | 2 | /** |
| 3 | * \file crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions | 3 | * \file crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions |
| 4 | * \author Written by Nils Larsch for the OpenSSL project | 4 | * \author Written by Nils Larsch for the OpenSSL project |
| @@ -269,6 +269,26 @@ int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg); | |||
| 269 | void *ECDSA_get_ex_data(EC_KEY *d, int idx); | 269 | void *ECDSA_get_ex_data(EC_KEY *d, int idx); |
| 270 | 270 | ||
| 271 | 271 | ||
| 272 | /* XXX should be in ec.h, but needs ECDSA_SIG */ | ||
| 273 | void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth, | ||
| 274 | int (*sign)(int type, const unsigned char *dgst, | ||
| 275 | int dlen, unsigned char *sig, unsigned int *siglen, | ||
| 276 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey), | ||
| 277 | int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, | ||
| 278 | BIGNUM **kinvp, BIGNUM **rp), | ||
| 279 | ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, | ||
| 280 | int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r, | ||
| 281 | EC_KEY *eckey)); | ||
| 282 | void EC_KEY_METHOD_get_sign(EC_KEY_METHOD *meth, | ||
| 283 | int (**psign)(int type, const unsigned char *dgst, | ||
| 284 | int dlen, unsigned char *sig, unsigned int *siglen, | ||
| 285 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey), | ||
| 286 | int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, | ||
| 287 | BIGNUM **kinvp, BIGNUM **rp), | ||
| 288 | ECDSA_SIG *(**psign_sig)(const unsigned char *dgst, | ||
| 289 | int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r, | ||
| 290 | EC_KEY *eckey)); | ||
| 291 | |||
| 272 | /* BEGIN ERROR CODES */ | 292 | /* BEGIN ERROR CODES */ |
| 273 | /* The following lines are auto generated by the script mkerr.pl. Any changes | 293 | /* The following lines are auto generated by the script mkerr.pl. Any changes |
| 274 | * made after this point may be overwritten when the script is next run. | 294 | * made after this point may be overwritten when the script is next run. |
diff --git a/src/lib/libcrypto/ecdsa/ecs_locl.h b/src/lib/libcrypto/ecdsa/ecs_locl.h index 94e8874332..0a9f17908b 100644 --- a/src/lib/libcrypto/ecdsa/ecs_locl.h +++ b/src/lib/libcrypto/ecdsa/ecs_locl.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ecs_locl.h,v 1.5 2016/12/21 15:49:29 jsing Exp $ */ | 1 | /* $OpenBSD: ecs_locl.h,v 1.6 2019/01/19 01:07:00 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Written by Nils Larsch for the OpenSSL project | 3 | * Written by Nils Larsch for the OpenSSL project |
| 4 | */ | 4 | */ |
| @@ -81,6 +81,14 @@ typedef struct ecdsa_data_st { | |||
| 81 | */ | 81 | */ |
| 82 | ECDSA_DATA *ecdsa_check(EC_KEY *eckey); | 82 | ECDSA_DATA *ecdsa_check(EC_KEY *eckey); |
| 83 | 83 | ||
| 84 | int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, | ||
| 85 | BIGNUM **rp); | ||
| 86 | int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, | ||
| 87 | unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv, | ||
| 88 | const BIGNUM *r, EC_KEY *eckey); | ||
| 89 | ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len, | ||
| 90 | const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey); | ||
| 91 | |||
| 84 | __END_HIDDEN_DECLS | 92 | __END_HIDDEN_DECLS |
| 85 | 93 | ||
| 86 | #endif /* HEADER_ECS_LOCL_H */ | 94 | #endif /* HEADER_ECS_LOCL_H */ |
diff --git a/src/lib/libcrypto/ecdsa/ecs_ossl.c b/src/lib/libcrypto/ecdsa/ecs_ossl.c index 87d80642df..4e05cb9aac 100644 --- a/src/lib/libcrypto/ecdsa/ecs_ossl.c +++ b/src/lib/libcrypto/ecdsa/ecs_ossl.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ecs_ossl.c,v 1.16 2018/07/10 21:36:02 tb Exp $ */ | 1 | /* $OpenBSD: ecs_ossl.c,v 1.17 2019/01/19 01:07:00 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Written by Nils Larsch for the OpenSSL project | 3 | * Written by Nils Larsch for the OpenSSL project |
| 4 | */ | 4 | */ |
| @@ -111,6 +111,21 @@ ecdsa_prepare_digest(const unsigned char *dgst, int dgst_len, BIGNUM *order, | |||
| 111 | return 1; | 111 | return 1; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | int | ||
| 115 | ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig, | ||
| 116 | unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) | ||
| 117 | { | ||
| 118 | ECDSA_SIG *s; | ||
| 119 | |||
| 120 | if ((s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey)) == NULL) { | ||
| 121 | *siglen = 0; | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | *siglen = i2d_ECDSA_SIG(s, &sig); | ||
| 125 | ECDSA_SIG_free(s); | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | |||
| 114 | static int | 129 | static int |
| 115 | ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | 130 | ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) |
| 116 | { | 131 | { |
| @@ -234,6 +249,16 @@ ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | |||
| 234 | return (ret); | 249 | return (ret); |
| 235 | } | 250 | } |
| 236 | 251 | ||
| 252 | /* replace w/ ecdsa_sign_setup() when ECDSA_METHOD gets removed */ | ||
| 253 | int | ||
| 254 | ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | ||
| 255 | { | ||
| 256 | ECDSA_DATA *ecdsa; | ||
| 257 | |||
| 258 | if ((ecdsa = ecdsa_check(eckey)) == NULL) | ||
| 259 | return 0; | ||
| 260 | return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp); | ||
| 261 | } | ||
| 237 | 262 | ||
| 238 | static ECDSA_SIG * | 263 | static ECDSA_SIG * |
| 239 | ecdsa_do_sign(const unsigned char *dgst, int dgst_len, | 264 | ecdsa_do_sign(const unsigned char *dgst, int dgst_len, |
| @@ -384,6 +409,18 @@ ecdsa_do_sign(const unsigned char *dgst, int dgst_len, | |||
| 384 | return ret; | 409 | return ret; |
| 385 | } | 410 | } |
| 386 | 411 | ||
| 412 | /* replace w/ ecdsa_do_sign() when ECDSA_METHOD gets removed */ | ||
| 413 | ECDSA_SIG * | ||
| 414 | ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len, | ||
| 415 | const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey) | ||
| 416 | { | ||
| 417 | ECDSA_DATA *ecdsa; | ||
| 418 | |||
| 419 | if ((ecdsa = ecdsa_check(eckey)) == NULL) | ||
| 420 | return NULL; | ||
| 421 | return ecdsa->meth->ecdsa_do_sign(dgst, dgst_len, in_kinv, in_r, eckey); | ||
| 422 | } | ||
| 423 | |||
| 387 | static int | 424 | static int |
| 388 | ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig, | 425 | ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig, |
| 389 | EC_KEY *eckey) | 426 | EC_KEY *eckey) |
diff --git a/src/lib/libcrypto/ecdsa/ecs_sign.c b/src/lib/libcrypto/ecdsa/ecs_sign.c index 029a0cb562..5beb853b94 100644 --- a/src/lib/libcrypto/ecdsa/ecs_sign.c +++ b/src/lib/libcrypto/ecdsa/ecs_sign.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ecs_sign.c,v 1.6 2015/02/08 13:35:07 jsing Exp $ */ | 1 | /* $OpenBSD: ecs_sign.c,v 1.7 2019/01/19 01:07:00 tb Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 4 | * | 4 | * |
| @@ -55,11 +55,13 @@ | |||
| 55 | 55 | ||
| 56 | #include <openssl/opensslconf.h> | 56 | #include <openssl/opensslconf.h> |
| 57 | 57 | ||
| 58 | #include "ecs_locl.h" | ||
| 59 | #ifndef OPENSSL_NO_ENGINE | 58 | #ifndef OPENSSL_NO_ENGINE |
| 60 | #include <openssl/engine.h> | 59 | #include <openssl/engine.h> |
| 61 | #endif | 60 | #endif |
| 62 | 61 | ||
| 62 | #include "ecs_locl.h" | ||
| 63 | #include "ec_lcl.h" | ||
| 64 | |||
| 63 | ECDSA_SIG * | 65 | ECDSA_SIG * |
| 64 | ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) | 66 | ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) |
| 65 | { | 67 | { |
| @@ -70,11 +72,10 @@ ECDSA_SIG * | |||
| 70 | ECDSA_do_sign_ex(const unsigned char *dgst, int dlen, const BIGNUM *kinv, | 72 | ECDSA_do_sign_ex(const unsigned char *dgst, int dlen, const BIGNUM *kinv, |
| 71 | const BIGNUM *rp, EC_KEY *eckey) | 73 | const BIGNUM *rp, EC_KEY *eckey) |
| 72 | { | 74 | { |
| 73 | ECDSA_DATA *ecdsa = ecdsa_check(eckey); | 75 | if (eckey->meth->sign_sig != NULL) |
| 74 | 76 | return eckey->meth->sign_sig(dgst, dlen, kinv, rp, eckey); | |
| 75 | if (ecdsa == NULL) | 77 | ECDSAerror(EVP_R_METHOD_NOT_SUPPORTED); |
| 76 | return NULL; | 78 | return 0; |
| 77 | return ecdsa->meth->ecdsa_do_sign(dgst, dlen, kinv, rp, eckey); | ||
| 78 | } | 79 | } |
| 79 | 80 | ||
| 80 | int | 81 | int |
| @@ -88,24 +89,17 @@ int | |||
| 88 | ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char *sig, | 89 | ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char *sig, |
| 89 | unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) | 90 | unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) |
| 90 | { | 91 | { |
| 91 | ECDSA_SIG *s; | 92 | if (eckey->meth->sign != NULL) |
| 92 | 93 | return eckey->meth->sign(type, dgst, dlen, sig, siglen, kinv, r, eckey); | |
| 93 | s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey); | 94 | ECDSAerror(EVP_R_METHOD_NOT_SUPPORTED); |
| 94 | if (s == NULL) { | 95 | return 0; |
| 95 | *siglen = 0; | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | *siglen = i2d_ECDSA_SIG(s, &sig); | ||
| 99 | ECDSA_SIG_free(s); | ||
| 100 | return 1; | ||
| 101 | } | 96 | } |
| 102 | 97 | ||
| 103 | int | 98 | int |
| 104 | ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | 99 | ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) |
| 105 | { | 100 | { |
| 106 | ECDSA_DATA *ecdsa = ecdsa_check(eckey); | 101 | if (eckey->meth->sign_setup != NULL) |
| 107 | 102 | return eckey->meth->sign_setup(eckey, ctx_in, kinvp, rp); | |
| 108 | if (ecdsa == NULL) | 103 | ECDSAerror(EVP_R_METHOD_NOT_SUPPORTED); |
| 109 | return 0; | 104 | return 0; |
| 110 | return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp); | ||
| 111 | } | 105 | } |
