deterministic_nonce.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/bn.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/kdf.h>
  14. #include "internal/deterministic_nonce.h"
  15. #include "crypto/bn.h"
  16. /*
  17. * Convert a Bit String to an Integer (See RFC 6979 Section 2.3.2)
  18. *
  19. * Params:
  20. * out The returned Integer as a BIGNUM
  21. * qlen_bits The maximum size of the returned integer in bits. The returned
  22. * Integer is shifted right if inlen is larger than qlen_bits..
  23. * in, inlen The input Bit String (in bytes).
  24. * Returns: 1 if successful, or 0 otherwise.
  25. */
  26. static int bits2int(BIGNUM *out, int qlen_bits,
  27. const unsigned char *in, size_t inlen)
  28. {
  29. int blen_bits = inlen * 8;
  30. int shift;
  31. if (BN_bin2bn(in, (int)inlen, out) == NULL)
  32. return 0;
  33. shift = blen_bits - qlen_bits;
  34. if (shift > 0)
  35. return BN_rshift(out, out, shift);
  36. return 1;
  37. }
  38. /*
  39. * Convert as above a Bit String in const time to an Integer w fixed top
  40. *
  41. * Params:
  42. * out The returned Integer as a BIGNUM
  43. * qlen_bits The maximum size of the returned integer in bits. The returned
  44. * Integer is shifted right if inlen is larger than qlen_bits..
  45. * in, inlen The input Bit String (in bytes). It has sizeof(BN_ULONG) bytes
  46. * prefix with all bits set that needs to be cleared out after
  47. * the conversion.
  48. * Returns: 1 if successful, or 0 otherwise.
  49. */
  50. static int bits2int_consttime(BIGNUM *out, int qlen_bits,
  51. const unsigned char *in, size_t inlen)
  52. {
  53. int blen_bits = (inlen - sizeof(BN_ULONG)) * 8;
  54. int shift;
  55. if (BN_bin2bn(in, (int)inlen, out) == NULL)
  56. return 0;
  57. BN_set_flags(out, BN_FLG_CONSTTIME);
  58. ossl_bn_mask_bits_fixed_top(out, blen_bits);
  59. shift = blen_bits - qlen_bits;
  60. if (shift > 0)
  61. return bn_rshift_fixed_top(out, out, shift);
  62. return 1;
  63. }
  64. /*
  65. * Convert an Integer to an Octet String (See RFC 6979 2.3.3).
  66. * The value is zero padded if required.
  67. *
  68. * Params:
  69. * out The returned Octet String
  70. * num The input Integer
  71. * rlen The required size of the returned Octet String in bytes
  72. * Returns: 1 if successful, or 0 otherwise.
  73. */
  74. static int int2octets(unsigned char *out, const BIGNUM *num, int rlen)
  75. {
  76. return BN_bn2binpad(num, out, rlen) >= 0;
  77. }
  78. /*
  79. * Convert a Bit String to an Octet String (See RFC 6979 Section 2.3.4)
  80. *
  81. * Params:
  82. * out The returned octet string.
  83. * q The modulus
  84. * qlen_bits The length of q in bits
  85. * rlen The value of qlen_bits rounded up to the nearest 8 bits.
  86. * in, inlen The input bit string (in bytes)
  87. * Returns: 1 if successful, or 0 otherwise.
  88. */
  89. static int bits2octets(unsigned char *out, const BIGNUM *q, int qlen_bits,
  90. int rlen, const unsigned char *in, size_t inlen)
  91. {
  92. int ret = 0;
  93. BIGNUM *z = BN_new();
  94. if (z == NULL
  95. || !bits2int(z, qlen_bits, in, inlen))
  96. goto err;
  97. /* z2 = z1 mod q (Do a simple subtract, since z1 < 2^qlen_bits) */
  98. if (BN_cmp(z, q) >= 0
  99. && !BN_usub(z, z, q))
  100. goto err;
  101. ret = int2octets(out, z, rlen);
  102. err:
  103. BN_free(z);
  104. return ret;
  105. }
  106. /*
  107. * Setup a KDF HMAC_DRBG object using fixed entropy and nonce data.
  108. *
  109. * Params:
  110. * digestname The digest name for the HMAC
  111. * entropy, entropylen A fixed input entropy buffer
  112. * nonce, noncelen A fixed input nonce buffer
  113. * libctx, propq Are used for fetching algorithms
  114. *
  115. * Returns: The created KDF HMAC_DRBG object if successful, or NULL otherwise.
  116. */
  117. static EVP_KDF_CTX *kdf_setup(const char *digestname,
  118. const unsigned char *entropy, size_t entropylen,
  119. const unsigned char *nonce, size_t noncelen,
  120. OSSL_LIB_CTX *libctx, const char *propq)
  121. {
  122. EVP_KDF_CTX *ctx = NULL;
  123. EVP_KDF *kdf = NULL;
  124. OSSL_PARAM params[5], *p;
  125. kdf = EVP_KDF_fetch(libctx, "HMAC-DRBG-KDF", propq);
  126. ctx = EVP_KDF_CTX_new(kdf);
  127. EVP_KDF_free(kdf);
  128. if (ctx == NULL)
  129. goto err;
  130. p = params;
  131. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  132. (char *)digestname, 0);
  133. if (propq != NULL)
  134. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
  135. (char *)propq, 0);
  136. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY,
  137. (void *)entropy, entropylen);
  138. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE,
  139. (void *)nonce, noncelen);
  140. *p = OSSL_PARAM_construct_end();
  141. if (EVP_KDF_CTX_set_params(ctx, params) <= 0)
  142. goto err;
  143. return ctx;
  144. err:
  145. EVP_KDF_CTX_free(ctx);
  146. return NULL;
  147. }
  148. /*
  149. * Generate a Deterministic nonce 'k' for DSA/ECDSA as defined in
  150. * RFC 6979 Section 3.3. "Alternate Description of the Generation of k"
  151. *
  152. * Params:
  153. * out Returns the generated deterministic nonce 'k'
  154. * q A large prime number used for modulus operations for DSA and ECDSA.
  155. * priv The private key in the range [1, q-1]
  156. * hm, hmlen The digested message buffer in bytes
  157. * digestname The digest name used for signing. It is used as the HMAC digest.
  158. * libctx, propq Used for fetching algorithms
  159. *
  160. * Returns: 1 if successful, or 0 otherwise.
  161. */
  162. int ossl_gen_deterministic_nonce_rfc6979(BIGNUM *out, const BIGNUM *q,
  163. const BIGNUM *priv,
  164. const unsigned char *hm, size_t hmlen,
  165. const char *digestname,
  166. OSSL_LIB_CTX *libctx,
  167. const char *propq)
  168. {
  169. EVP_KDF_CTX *kdfctx = NULL;
  170. int ret = 0, rlen = 0, qlen_bits = 0;
  171. unsigned char *entropyx = NULL, *nonceh = NULL, *rbits = NULL, *T = NULL;
  172. size_t allocsz = 0;
  173. const size_t prefsz = sizeof(BN_ULONG);
  174. if (out == NULL)
  175. return 0;
  176. qlen_bits = BN_num_bits(q);
  177. if (qlen_bits == 0)
  178. return 0;
  179. /* Note rlen used here is in bytes since the input values are byte arrays */
  180. rlen = (qlen_bits + 7) / 8;
  181. allocsz = prefsz + 3 * rlen;
  182. /* Use a single alloc for the buffers T, nonceh and entropyx */
  183. T = (unsigned char *)OPENSSL_zalloc(allocsz);
  184. if (T == NULL)
  185. return 0;
  186. rbits = T + prefsz;
  187. nonceh = rbits + rlen;
  188. entropyx = nonceh + rlen;
  189. memset(T, 0xff, prefsz);
  190. if (!int2octets(entropyx, priv, rlen)
  191. || !bits2octets(nonceh, q, qlen_bits, rlen, hm, hmlen))
  192. goto end;
  193. kdfctx = kdf_setup(digestname, entropyx, rlen, nonceh, rlen, libctx, propq);
  194. if (kdfctx == NULL)
  195. goto end;
  196. do {
  197. if (!EVP_KDF_derive(kdfctx, rbits, rlen, NULL)
  198. || !bits2int_consttime(out, qlen_bits, T, rlen + prefsz))
  199. goto end;
  200. } while (ossl_bn_is_word_fixed_top(out, 0)
  201. || ossl_bn_is_word_fixed_top(out, 1)
  202. || BN_ucmp(out, q) >= 0);
  203. #ifdef BN_DEBUG
  204. /* With BN_DEBUG on a fixed top number cannot be returned */
  205. bn_correct_top(out);
  206. #endif
  207. ret = 1;
  208. end:
  209. EVP_KDF_CTX_free(kdfctx);
  210. OPENSSL_clear_free(T, allocsz);
  211. return ret;
  212. }