1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
16 ECC Crypto, Tom St Denis
19 static int _ecc_sign_hash(const unsigned char *in, unsigned long inlen,
20 unsigned char *out, unsigned long *outlen,
21 prng_state *prng, int wprng, ecc_key *key, int sigformat)
24 void *r, *s, *e, *p, *b;
25 int err, max_iterations = LTC_PK_MAX_RETRIES;
26 unsigned long pbits, pbytes, i, shift_right;
27 unsigned char ch, buf[MAXBLOCKSIZE];
29 LTC_ARGCHK(in != NULL);
30 LTC_ARGCHK(out != NULL);
31 LTC_ARGCHK(outlen != NULL);
32 LTC_ARGCHK(key != NULL);
34 /* is this a private key? */
35 if (key->type != PK_PRIVATE) {
36 return CRYPT_PK_NOT_PRIVATE;
39 /* is the IDX valid ? */
40 if (ltc_ecc_is_valid_idx(key->idx) != 1) {
41 return CRYPT_PK_INVALID_TYPE;
44 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
48 /* init the bignums */
49 if ((err = mp_init_multi(&r, &s, &p, &e, &b, NULL)) != CRYPT_OK) {
52 if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errnokey; }
54 /* get the hash and load it as a bignum into 'e' */
55 pbits = mp_count_bits(p);
56 pbytes = (pbits+7) >> 3;
57 if (pbits > inlen*8) {
58 if ((err = mp_read_unsigned_bin(e, (unsigned char *)in, inlen)) != CRYPT_OK) { goto errnokey; }
60 else if (pbits % 8 == 0) {
61 if ((err = mp_read_unsigned_bin(e, (unsigned char *)in, pbytes)) != CRYPT_OK) { goto errnokey; }
64 shift_right = 8 - pbits % 8;
65 for (i=0, ch=0; i<pbytes; i++) {
67 ch = (in[i] << (8-shift_right));
68 buf[i] = buf[i] ^ (in[i] >> shift_right);
70 if ((err = mp_read_unsigned_bin(e, (unsigned char *)buf, pbytes)) != CRYPT_OK) { goto errnokey; }
73 /* make up a key and export the public copy */
75 if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
79 /* find r = x1 mod n */
80 if ((err = mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; }
82 if (mp_iszero(r) == LTC_MP_YES) {
85 if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */
86 /* find s = (e + xr)/k */
87 if ((err = mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */
88 if ((err = mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */
89 if ((err = mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */
90 if ((err = mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */
91 if ((err = mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */
92 if ((err = mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */
93 if ((err = mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */
95 if (mp_iszero(s) == LTC_MP_NO) {
99 } while (--max_iterations > 0);
101 if (max_iterations == 0) {
105 if (sigformat == 1) {
107 if (*outlen < 2*pbytes) { err = CRYPT_MEM; goto errnokey; }
108 zeromem(out, 2*pbytes);
109 i = mp_unsigned_bin_size(r);
110 if ((err = mp_to_unsigned_bin(r, out + (pbytes - i))) != CRYPT_OK) { goto errnokey; }
111 i = mp_unsigned_bin_size(s);
112 if ((err = mp_to_unsigned_bin(s, out + (2*pbytes - i))) != CRYPT_OK) { goto errnokey; }
117 /* store as ASN.1 SEQUENCE { r, s -- integer } */
118 err = der_encode_sequence_multi(out, outlen,
119 LTC_ASN1_INTEGER, 1UL, r,
120 LTC_ASN1_INTEGER, 1UL, s,
121 LTC_ASN1_EOL, 0UL, NULL);
127 mp_clear_multi(r, s, p, e, b, NULL);
132 Sign a message digest
133 @param in The message digest to sign
134 @param inlen The length of the digest
135 @param out [out] The destination for the signature
136 @param outlen [in/out] The max size and resulting size of the signature
137 @param prng An active PRNG state
138 @param wprng The index of the PRNG you wish to use
139 @param key A private ECC key
140 @return CRYPT_OK if successful
142 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
143 unsigned char *out, unsigned long *outlen,
144 prng_state *prng, int wprng, ecc_key *key)
146 return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 0);
150 Sign a message digest in RFC7518 format
151 @param in The message digest to sign
152 @param inlen The length of the digest
153 @param out [out] The destination for the signature
154 @param outlen [in/out] The max size and resulting size of the signature
155 @param prng An active PRNG state
156 @param wprng The index of the PRNG you wish to use
157 @param key A private ECC key
158 @return CRYPT_OK if successful
160 int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
161 unsigned char *out, unsigned long *outlen,
162 prng_state *prng, int wprng, ecc_key *key)
164 return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 1);
169 /* ref: $Format:%D$ */
170 /* git commit: $Format:%H$ */
171 /* commit time: $Format:%ai$ */