deterministic_nonce.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <openssl/bn.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/kdf.h>
  13. #include "internal/deterministic_nonce.h"
  14. /*
  15. * Convert a Bit String to an Integer (See RFC 6979 Section 2.3.2)
  16. *
  17. * Params:
  18. * out The returned Integer as a BIGNUM
  19. * qlen_bits The maximum size of the returned integer in bits. The returned
  20. * Integer is shifted right if inlen is larger than qlen_bits..
  21. * in, inlen The input Bit String (in bytes).
  22. * Returns: 1 if successful, or 0 otherwise.
  23. */
  24. static int bits2int(BIGNUM *out, int qlen_bits,
  25. const unsigned char *in, size_t inlen)
  26. {
  27. int blen_bits = inlen * 8;
  28. int shift;
  29. if (BN_bin2bn(in, (int)inlen, out) == NULL)
  30. return 0;
  31. shift = blen_bits - qlen_bits;
  32. if (shift > 0)
  33. return BN_rshift(out, out, shift);
  34. return 1;
  35. }
  36. /*
  37. * Convert an Integer to an Octet String (See RFC 6979 2.3.3).
  38. * The value is zero padded if required.
  39. *
  40. * Params:
  41. * out The returned Octet String
  42. * num The input Integer
  43. * rlen The required size of the returned Octet String in bytes
  44. * Returns: 1 if successful, or 0 otherwise.
  45. */
  46. static int int2octets(unsigned char *out, const BIGNUM *num, int rlen)
  47. {
  48. return BN_bn2binpad(num, out, rlen) >= 0;
  49. }
  50. /*
  51. * Convert a Bit String to an Octet String (See RFC 6979 Section 2.3.4)
  52. *
  53. * Params:
  54. * out The returned octet string.
  55. * q The modulus
  56. * qlen_bits The length of q in bits
  57. * rlen The value of qlen_bits rounded up to the nearest 8 bits.
  58. * in, inlen The input bit string (in bytes)
  59. * Returns: 1 if successful, or 0 otherwise.
  60. */
  61. static int bits2octets(unsigned char *out, const BIGNUM *q, int qlen_bits,
  62. int rlen, const unsigned char *in, size_t inlen)
  63. {
  64. int ret = 0;
  65. BIGNUM *z = BN_new();
  66. if (z == NULL
  67. || !bits2int(z, qlen_bits, in, inlen))
  68. goto err;
  69. /* z2 = z1 mod q (Do a simple subtract, since z1 < 2^qlen_bits) */
  70. if (BN_cmp(z, q) >= 0
  71. && !BN_usub(z, z, q))
  72. goto err;
  73. ret = int2octets(out, z, rlen);
  74. err:
  75. BN_free(z);
  76. return ret;
  77. }
  78. /*
  79. * Setup a KDF HMAC_DRBG object using fixed entropy and nonce data.
  80. *
  81. * Params:
  82. * digestname The digest name for the HMAC
  83. * entropy, entropylen A fixed input entropy buffer
  84. * nonce, noncelen A fixed input nonce buffer
  85. * libctx, propq Are used for fetching algorithms
  86. *
  87. * Returns: The created KDF HMAC_DRBG object if successful, or NULL otherwise.
  88. */
  89. static EVP_KDF_CTX *kdf_setup(const char *digestname,
  90. const unsigned char *entropy, size_t entropylen,
  91. const unsigned char *nonce, size_t noncelen,
  92. OSSL_LIB_CTX *libctx, const char *propq)
  93. {
  94. EVP_KDF_CTX *ctx = NULL;
  95. EVP_KDF *kdf = NULL;
  96. OSSL_PARAM params[5], *p;
  97. kdf = EVP_KDF_fetch(libctx, "HMAC-DRBG-KDF", propq);
  98. ctx = EVP_KDF_CTX_new(kdf);
  99. EVP_KDF_free(kdf);
  100. if (ctx == NULL)
  101. goto err;
  102. p = params;
  103. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  104. (char *)digestname, 0);
  105. if (propq != NULL)
  106. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
  107. (char *)propq, 0);
  108. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY,
  109. (void *)entropy, entropylen);
  110. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE,
  111. (void *)nonce, noncelen);
  112. *p = OSSL_PARAM_construct_end();
  113. if (EVP_KDF_CTX_set_params(ctx, params) <= 0)
  114. goto err;
  115. return ctx;
  116. err:
  117. EVP_KDF_CTX_free(ctx);
  118. return NULL;
  119. }
  120. /*
  121. * Generate a Deterministic nonce 'k' for DSA/ECDSA as defined in
  122. * RFC 6979 Section 3.3. "Alternate Description of the Generation of k"
  123. *
  124. * Params:
  125. * out Returns the generated deterministic nonce 'k'
  126. * q A large prime number used for modulus operations for DSA and ECDSA.
  127. * priv The private key in the range [1, q-1]
  128. * hm, hmlen The digested message buffer in bytes
  129. * digestname The digest name used for signing. It is used as the HMAC digest.
  130. * libctx, propq Used for fetching algorithms
  131. *
  132. * Returns: 1 if successful, or 0 otherwise.
  133. */
  134. int ossl_gen_deterministic_nonce_rfc6979(BIGNUM *out, const BIGNUM *q,
  135. const BIGNUM *priv,
  136. const unsigned char *hm, size_t hmlen,
  137. const char *digestname,
  138. OSSL_LIB_CTX *libctx,
  139. const char *propq)
  140. {
  141. EVP_KDF_CTX *kdfctx = NULL;
  142. int ret = 0, rlen = 0, qlen_bits = 0;
  143. unsigned char *entropyx = NULL, *nonceh = NULL, *T = NULL;
  144. size_t allocsz = 0;
  145. if (out == NULL)
  146. return 0;
  147. qlen_bits = BN_num_bits(q);
  148. if (qlen_bits == 0)
  149. return 0;
  150. /* Note rlen used here is in bytes since the input values are byte arrays */
  151. rlen = (qlen_bits + 7) / 8;
  152. allocsz = 3 * rlen;
  153. /* Use a single alloc for the buffers T, nonceh and entropyx */
  154. T = (unsigned char *)OPENSSL_zalloc(allocsz);
  155. if (T == NULL)
  156. return 0;
  157. nonceh = T + rlen;
  158. entropyx = nonceh + rlen;
  159. if (!int2octets(entropyx, priv, rlen)
  160. || !bits2octets(nonceh, q, qlen_bits, rlen, hm, hmlen))
  161. goto end;
  162. kdfctx = kdf_setup(digestname, entropyx, rlen, nonceh, rlen, libctx, propq);
  163. if (kdfctx == NULL)
  164. goto end;
  165. do {
  166. if (!EVP_KDF_derive(kdfctx, T, rlen, NULL)
  167. || !bits2int(out, qlen_bits, T, rlen))
  168. goto end;
  169. } while (BN_is_zero(out) || BN_is_one(out) || BN_cmp(out, q) >= 0);
  170. ret = 1;
  171. end:
  172. EVP_KDF_CTX_free(kdfctx);
  173. OPENSSL_clear_free(T, allocsz);
  174. return ret;
  175. }