comparison libtomcrypt/src/pk/ecc/ecc_sign_hash.c @ 1733:d529a52b2f7c coverity coverity

merge coverity from main
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 Jun 2020 21:07:34 +0800
parents e9dba7abd939
children
comparison
equal deleted inserted replaced
1643:b59623a64678 1733:d529a52b2f7c
12 #if defined(LTC_MECC) && defined(LTC_DER) 12 #if defined(LTC_MECC) && defined(LTC_DER)
13 13
14 /** 14 /**
15 @file ecc_sign_hash.c 15 @file ecc_sign_hash.c
16 ECC Crypto, Tom St Denis 16 ECC Crypto, Tom St Denis
17 */ 17 */
18 18
19 static int _ecc_sign_hash(const unsigned char *in, unsigned long inlen, 19 static int _ecc_sign_hash(const unsigned char *in, unsigned long inlen,
20 unsigned char *out, unsigned long *outlen, 20 unsigned char *out, unsigned long *outlen,
21 prng_state *prng, int wprng, ecc_key *key, int sigformat) 21 prng_state *prng, int wprng, ecc_key *key, int sigformat)
22 { 22 {
23 ecc_key pubkey; 23 ecc_key pubkey;
24 void *r, *s, *e, *p; 24 void *r, *s, *e, *p, *b;
25 int err, max_iterations = LTC_PK_MAX_RETRIES; 25 int err, max_iterations = LTC_PK_MAX_RETRIES;
26 unsigned long pbits, pbytes, i, shift_right; 26 unsigned long pbits, pbytes, i, shift_right;
27 unsigned char ch, buf[MAXBLOCKSIZE]; 27 unsigned char ch, buf[MAXBLOCKSIZE];
28 28
29 LTC_ARGCHK(in != NULL); 29 LTC_ARGCHK(in != NULL);
33 33
34 /* is this a private key? */ 34 /* is this a private key? */
35 if (key->type != PK_PRIVATE) { 35 if (key->type != PK_PRIVATE) {
36 return CRYPT_PK_NOT_PRIVATE; 36 return CRYPT_PK_NOT_PRIVATE;
37 } 37 }
38 38
39 /* is the IDX valid ? */ 39 /* is the IDX valid ? */
40 if (ltc_ecc_is_valid_idx(key->idx) != 1) { 40 if (ltc_ecc_is_valid_idx(key->idx) != 1) {
41 return CRYPT_PK_INVALID_TYPE; 41 return CRYPT_PK_INVALID_TYPE;
42 } 42 }
43 43
44 if ((err = prng_is_valid(wprng)) != CRYPT_OK) { 44 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
45 return err; 45 return err;
46 } 46 }
47 47
48 /* init the bignums */ 48 /* init the bignums */
49 if ((err = mp_init_multi(&r, &s, &p, &e, NULL)) != CRYPT_OK) { 49 if ((err = mp_init_multi(&r, &s, &p, &e, &b, NULL)) != CRYPT_OK) {
50 return err; 50 return err;
51 } 51 }
52 if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errnokey; } 52 if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errnokey; }
53 53
54 /* get the hash and load it as a bignum into 'e' */ 54 /* get the hash and load it as a bignum into 'e' */
55 pbits = mp_count_bits(p); 55 pbits = mp_count_bits(p);
56 pbytes = (pbits+7) >> 3; 56 pbytes = (pbits+7) >> 3;
57 if (pbits > inlen*8) { 57 if (pbits > inlen*8) {
75 if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) { 75 if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
76 goto errnokey; 76 goto errnokey;
77 } 77 }
78 78
79 /* find r = x1 mod n */ 79 /* find r = x1 mod n */
80 if ((err = mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; } 80 if ((err = mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; }
81 81
82 if (mp_iszero(r) == LTC_MP_YES) { 82 if (mp_iszero(r) == LTC_MP_YES) {
83 ecc_free(&pubkey); 83 ecc_free(&pubkey);
84 } else { 84 } else {
85 /* find s = (e + xr)/k */ 85 if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */
86 if ((err = mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/k */ 86 /* find s = (e + xr)/k */
87 if ((err = mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */ 87 if ((err = mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */
88 if ((err = mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e + xr */ 88 if ((err = mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */
89 if ((err = mp_mod(s, p, s)) != CRYPT_OK) { goto error; } /* s = e + xr */ 89 if ((err = mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */
90 if ((err = mp_mulmod(s, pubkey.k, p, s)) != CRYPT_OK) { goto error; } /* s = (e + xr)/k */ 90 if ((err = mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */
91 ecc_free(&pubkey); 91 if ((err = mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */
92 if (mp_iszero(s) == LTC_MP_NO) { 92 if ((err = mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */
93 break; 93 if ((err = mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */
94 } 94 ecc_free(&pubkey);
95 if (mp_iszero(s) == LTC_MP_NO) {
96 break;
97 }
95 } 98 }
96 } while (--max_iterations > 0); 99 } while (--max_iterations > 0);
97 100
98 if (max_iterations == 0) { 101 if (max_iterations == 0) {
99 goto errnokey; 102 goto errnokey;
110 *outlen = 2*pbytes; 113 *outlen = 2*pbytes;
111 err = CRYPT_OK; 114 err = CRYPT_OK;
112 } 115 }
113 else { 116 else {
114 /* store as ASN.1 SEQUENCE { r, s -- integer } */ 117 /* store as ASN.1 SEQUENCE { r, s -- integer } */
115 err = der_encode_sequence_multi(out, outlen, 118 err = der_encode_sequence_multi(out, outlen,
116 LTC_ASN1_INTEGER, 1UL, r, 119 LTC_ASN1_INTEGER, 1UL, r,
117 LTC_ASN1_INTEGER, 1UL, s, 120 LTC_ASN1_INTEGER, 1UL, s,
118 LTC_ASN1_EOL, 0UL, NULL); 121 LTC_ASN1_EOL, 0UL, NULL);
119 } 122 }
120 goto errnokey; 123 goto errnokey;
121 error: 124 error:
122 ecc_free(&pubkey); 125 ecc_free(&pubkey);
123 errnokey: 126 errnokey:
124 mp_clear_multi(r, s, p, e, NULL); 127 mp_clear_multi(r, s, p, e, b, NULL);
125 return err; 128 return err;
126 } 129 }
127 130
128 /** 131 /**
129 Sign a message digest 132 Sign a message digest
130 @param in The message digest to sign 133 @param in The message digest to sign