1 /* fips/rand/fips_drbg_ec.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
54 #define OPENSSL_FIPSAPI
58 #include <openssl/crypto.h>
59 #include <openssl/fips.h>
60 #include <openssl/fips_rand.h>
61 #include <openssl/bn.h>
62 #include "fips_rand_lcl.h"
64 /*#define EC_DRBG_TRACE*/
67 static void hexprint(FILE *out, const unsigned char *buf, int buflen)
71 for (i = 0; i < buflen; i++)
72 fprintf(out, "%02X", buf[i]);
75 static void bnprint(FILE *out, const char *name, const BIGNUM *b)
79 len = BN_num_bytes(b);
80 tmp = OPENSSL_malloc(len);
82 fprintf(out, "%s\n", name);
83 hexprint(out, tmp, len);
87 static void ecprint(FILE *out, EC_GROUP *grp, EC_POINT *pt)
92 EC_POINT_get_affine_coordinates_GFp(grp, pt, x, y, NULL);
93 bnprint(out, "\tPoint X: ", x);
94 bnprint(out, "\tPoint Y: ", y);
101 /* This is Hash_df from SP 800-90 10.4.1 */
103 static int hash_df(DRBG_CTX *dctx, unsigned char *out,
104 const unsigned char *in1, size_t in1len,
105 const unsigned char *in2, size_t in2len,
106 const unsigned char *in3, size_t in3len)
108 DRBG_EC_CTX *ectx = &dctx->d.ec;
109 EVP_MD_CTX *mctx = &ectx->mctx;
110 unsigned char *vtmp = ectx->vtmp;
111 unsigned char tmp[6];
112 size_t mdlen = M_EVP_MD_size(ectx->md);
113 /* Standard only ever needs seedlen bytes which is always less than
114 * maximum permitted so no need to check length.
116 size_t outlen = dctx->seedlen;
117 size_t nbits = (outlen << 3) - ectx->exbits;
119 tmp[1] = (nbits >> 24) & 0xff;
120 tmp[2] = (nbits >> 16) & 0xff;
121 tmp[3] = (nbits >> 8) & 0xff;
122 tmp[4] = nbits & 0xff;
125 tmp[5] = (unsigned char)in1len;
131 if (!FIPS_digestinit(mctx, ectx->md))
133 if (!FIPS_digestupdate(mctx, tmp, 5))
135 if (in1 && !FIPS_digestupdate(mctx, in1, in1len))
137 if (in2 && !FIPS_digestupdate(mctx, in2, in2len))
139 if (in3 && !FIPS_digestupdate(mctx, in3, in3len))
143 if (!FIPS_digestfinal(mctx, vtmp, NULL))
145 memcpy(out, vtmp, outlen);
146 OPENSSL_cleanse(vtmp, mdlen);
149 else if(!FIPS_digestfinal(mctx, out, NULL))
160 static int bn2binpad(unsigned char *to, size_t tolen, BIGNUM *b)
163 blen = BN_num_bytes(b);
164 /* If BIGNUM length greater than buffer, mask to get rightmost
165 * bytes. NB: modifies b but this doesn't matter for our purposes.
169 BN_mask_bits(b, tolen << 3);
170 /* Update length because mask operation might create leading
173 blen = BN_num_bytes(b);
175 /* If b length smaller than buffer pad with zeroes */
178 memset(to, 0, tolen - blen);
182 /* This call cannot fail */
186 /* Convert buffer to a BIGNUM discarding extra bits if necessary */
187 static int bin2bnbits(DRBG_CTX *dctx, BIGNUM *r, const unsigned char *buf)
189 DRBG_EC_CTX *ectx = &dctx->d.ec;
190 if (!BN_bin2bn(buf, dctx->seedlen, r))
192 /* If we have extra bits right shift off the end of r */
195 if (!BN_rshift(r, r, ectx->exbits))
201 /* Calculate r = phi(s * P) or r= phi(s * Q) */
203 static int drbg_ec_mul(DRBG_EC_CTX *ectx, BIGNUM *r, const BIGNUM *s, int use_q)
207 if (!EC_POINT_mul(ectx->curve, ectx->ptmp,
208 NULL, ectx->Q, s, ectx->bctx))
213 if (!EC_POINT_mul(ectx->curve, ectx->ptmp,
214 s, NULL, NULL, ectx->bctx))
217 /* Get x coordinate of result */
218 if (!EC_POINT_get_affine_coordinates_GFp(ectx->curve, ectx->ptmp, r,
224 static int drbg_ec_instantiate(DRBG_CTX *dctx,
225 const unsigned char *ent, size_t ent_len,
226 const unsigned char *nonce, size_t nonce_len,
227 const unsigned char *pstr, size_t pstr_len)
229 DRBG_EC_CTX *ectx = &dctx->d.ec;
230 if (!hash_df(dctx, ectx->sbuf,
231 ent, ent_len, nonce, nonce_len, pstr, pstr_len))
233 if (!bin2bnbits(dctx, ectx->s, ectx->sbuf))
239 static int drbg_ec_reseed(DRBG_CTX *dctx,
240 const unsigned char *ent, size_t ent_len,
241 const unsigned char *adin, size_t adin_len)
243 DRBG_EC_CTX *ectx = &dctx->d.ec;
244 /* Check if we have a deferred s = s * P */
247 if (drbg_ec_mul(ectx, ectx->s, ectx->s, 0))
251 /* Convert s value to a binary buffer. Save it to tbuf as we are
252 * about to overwrite it.
255 BN_lshift(ectx->s, ectx->s, ectx->exbits);
256 bn2binpad(ectx->tbuf, dctx->seedlen, ectx->s);
257 if (!hash_df(dctx, ectx->sbuf, ectx->tbuf, dctx->seedlen,
258 ent, ent_len, adin, adin_len))
260 if (!bin2bnbits(dctx, ectx->s, ectx->sbuf))
262 dctx->reseed_counter = 0;
266 static int drbg_ec_generate(DRBG_CTX *dctx,
267 unsigned char *out, size_t outlen,
268 const unsigned char *adin, size_t adin_len)
270 DRBG_EC_CTX *ectx = &dctx->d.ec;
273 /* special case: check reseed interval */
276 size_t nb = (outlen + dctx->blocklength - 1)/dctx->blocklength;
277 if (dctx->reseed_counter + nb > dctx->reseed_interval)
278 dctx->status = DRBG_STATUS_RESEED;
281 /* Check if we have a deferred s = s * P */
284 if (drbg_ec_mul(ectx, s, s, 0))
289 BN_CTX_start(ectx->bctx);
290 r = BN_CTX_get(ectx->bctx);
293 if (adin && adin_len)
296 t = BN_CTX_get(ectx->bctx);
299 /* Convert s to buffer */
301 BN_lshift(s, s, ectx->exbits);
302 bn2binpad(ectx->sbuf, dctx->seedlen, s);
304 if (!hash_df(dctx, ectx->tbuf, adin, adin_len,
308 for (i = 0; i < dctx->seedlen; i++)
309 ectx->tbuf[i] ^= ectx->sbuf[i];
310 if (!bin2bnbits(dctx, t, ectx->tbuf))
314 /* Note if no additional input the algorithm never
315 * needs separate values for t and s.
320 bnprint(stderr, "s at start of generate: ", s);
325 /* Step #6, calculate s = t * P */
326 if (drbg_ec_mul(ectx, s, t, 0))
329 bnprint(stderr, "s in generate: ", ectx->s);
331 /* Step #7, calculate r = s * Q */
332 if (drbg_ec_mul(ectx, r, s, 1))
335 bnprint(stderr, "r in generate is: ", r);
337 dctx->reseed_counter++;
338 /* Get rightmost bits of r to output buffer */
340 if (!(dctx->xflags & DRBG_FLAG_TEST) && !dctx->lb_valid)
342 if (!bn2binpad(dctx->lb, dctx->blocklength, r))
347 if (outlen < dctx->blocklength)
349 if (!bn2binpad(ectx->vtmp, dctx->blocklength, r))
351 if (!fips_drbg_cprng_test(dctx, ectx->vtmp))
353 memcpy(out, ectx->vtmp, outlen);
358 if (!bn2binpad(out, dctx->blocklength, r))
360 if (!fips_drbg_cprng_test(dctx, out))
363 outlen -= dctx->blocklength;
366 out += dctx->blocklength;
367 /* Step #5 after first pass */
370 fprintf(stderr, "Random bits written:\n");
371 hexprint(stderr, out, dctx->blocklength);
374 /* Defer s = s * P until we need it */
377 bnprint(stderr, "s after generate is: ", s);
379 BN_CTX_end(ectx->bctx);
382 BN_CTX_end(ectx->bctx);
386 static int drbg_ec_uninstantiate(DRBG_CTX *dctx)
388 DRBG_EC_CTX *ectx = &dctx->d.ec;
389 EVP_MD_CTX_cleanup(&ectx->mctx);
390 EC_GROUP_free(ectx->curve);
391 EC_POINT_free(ectx->Q);
392 EC_POINT_free(ectx->ptmp);
393 BN_clear_free(ectx->s);
394 BN_CTX_free(ectx->bctx);
395 OPENSSL_cleanse(&dctx->d.ec, sizeof(DRBG_EC_CTX));
399 /* Q points from SP 800-90 A.1, P is generator */
402 static const unsigned char p_256_qx[] = {
403 0xc9,0x74,0x45,0xf4,0x5c,0xde,0xf9,0xf0,0xd3,0xe0,0x5e,0x1e,
404 0x58,0x5f,0xc2,0x97,0x23,0x5b,0x82,0xb5,0xbe,0x8f,0xf3,0xef,
405 0xca,0x67,0xc5,0x98,0x52,0x01,0x81,0x92
408 static const unsigned char p_256_qy[] = {
409 0xb2,0x8e,0xf5,0x57,0xba,0x31,0xdf,0xcb,0xdd,0x21,0xac,0x46,
410 0xe2,0xa9,0x1e,0x3c,0x30,0x4f,0x44,0xcb,0x87,0x05,0x8a,0xda,
411 0x2c,0xb8,0x15,0x15,0x1e,0x61,0x00,0x46
415 static const unsigned char p_384_qx[] = {
416 0x8e,0x72,0x2d,0xe3,0x12,0x5b,0xdd,0xb0,0x55,0x80,0x16,0x4b,
417 0xfe,0x20,0xb8,0xb4,0x32,0x21,0x6a,0x62,0x92,0x6c,0x57,0x50,
418 0x2c,0xee,0xde,0x31,0xc4,0x78,0x16,0xed,0xd1,0xe8,0x97,0x69,
419 0x12,0x41,0x79,0xd0,0xb6,0x95,0x10,0x64,0x28,0x81,0x50,0x65
422 static const unsigned char p_384_qy[] = {
423 0x02,0x3b,0x16,0x60,0xdd,0x70,0x1d,0x08,0x39,0xfd,0x45,0xee,
424 0xc3,0x6f,0x9e,0xe7,0xb3,0x2e,0x13,0xb3,0x15,0xdc,0x02,0x61,
425 0x0a,0xa1,0xb6,0x36,0xe3,0x46,0xdf,0x67,0x1f,0x79,0x0f,0x84,
426 0xc5,0xe0,0x9b,0x05,0x67,0x4d,0xbb,0x7e,0x45,0xc8,0x03,0xdd
430 static const unsigned char p_521_qx[] = {
431 0x01,0xb9,0xfa,0x3e,0x51,0x8d,0x68,0x3c,0x6b,0x65,0x76,0x36,
432 0x94,0xac,0x8e,0xfb,0xae,0xc6,0xfa,0xb4,0x4f,0x22,0x76,0x17,
433 0x1a,0x42,0x72,0x65,0x07,0xdd,0x08,0xad,0xd4,0xc3,0xb3,0xf4,
434 0xc1,0xeb,0xc5,0xb1,0x22,0x2d,0xdb,0xa0,0x77,0xf7,0x22,0x94,
435 0x3b,0x24,0xc3,0xed,0xfa,0x0f,0x85,0xfe,0x24,0xd0,0xc8,0xc0,
436 0x15,0x91,0xf0,0xbe,0x6f,0x63
439 static const unsigned char p_521_qy[] = {
440 0x01,0xf3,0xbd,0xba,0x58,0x52,0x95,0xd9,0xa1,0x11,0x0d,0x1d,
441 0xf1,0xf9,0x43,0x0e,0xf8,0x44,0x2c,0x50,0x18,0x97,0x6f,0xf3,
442 0x43,0x7e,0xf9,0x1b,0x81,0xdc,0x0b,0x81,0x32,0xc8,0xd5,0xc3,
443 0x9c,0x32,0xd0,0xe0,0x04,0xa3,0x09,0x2b,0x7d,0x32,0x7c,0x0e,
444 0x7a,0x4d,0x26,0xd2,0xc7,0xb6,0x9b,0x58,0xf9,0x06,0x66,0x52,
445 0x91,0x1e,0x45,0x77,0x79,0xde
448 int fips_drbg_ec_init(DRBG_CTX *dctx)
451 const unsigned char *Q_x, *Q_y;
454 int md_nid = dctx->type & 0xffff;
455 int curve_nid = dctx->type >> 16;
456 DRBG_EC_CTX *ectx = &dctx->d.ec;
457 md = FIPS_get_digestbynid(md_nid);
461 /* These are taken from SP 800-90 10.3.1 table 4 */
464 case NID_X9_62_prime256v1:
465 dctx->strength = 128;
467 dctx->blocklength = 30;
471 ptlen = sizeof(p_256_qx);
475 if (md_nid == NID_sha1)
477 dctx->strength = 192;
479 dctx->blocklength = 46;
483 ptlen = sizeof(p_384_qx);
487 if (md_nid == NID_sha1 || md_nid == NID_sha224)
489 dctx->strength = 256;
491 dctx->blocklength = 63;
495 ptlen = sizeof(p_521_qx);
502 dctx->iflags |= DRBG_CUSTOM_RESEED;
503 dctx->reseed_counter = 0;
504 dctx->instantiate = drbg_ec_instantiate;
505 dctx->reseed = drbg_ec_reseed;
506 dctx->generate = drbg_ec_generate;
507 dctx->uninstantiate = drbg_ec_uninstantiate;
510 EVP_MD_CTX_init(&ectx->mctx);
512 dctx->min_entropy = dctx->strength / 8;
513 dctx->max_entropy = 2 << 10;
515 dctx->min_nonce = dctx->min_entropy / 2;
516 dctx->max_nonce = 2 << 10;
518 dctx->max_pers = 2 << 10;
519 dctx->max_adin = 2 << 10;
521 dctx->reseed_interval = 1<<24;
522 dctx->max_request = dctx->reseed_interval * dctx->blocklength;
524 /* Setup internal structures */
525 ectx->bctx = BN_CTX_new();
528 BN_CTX_start(ectx->bctx);
532 ectx->curve = EC_GROUP_new_by_curve_name(curve_nid);
534 ectx->Q = EC_POINT_new(ectx->curve);
535 ectx->ptmp = EC_POINT_new(ectx->curve);
539 x = BN_CTX_get(ectx->bctx);
540 y = BN_CTX_get(ectx->bctx);
542 if (!ectx->s || !ectx->curve || !ectx->Q || !y)
545 if (!BN_bin2bn(Q_x, ptlen, x) || !BN_bin2bn(Q_y, ptlen, y))
547 if (!EC_POINT_set_affine_coordinates_GFp(ectx->curve, ectx->Q,
551 BN_CTX_end(ectx->bctx);
555 BN_CTX_end(ectx->bctx);
556 drbg_ec_uninstantiate(dctx);