rsa_x931g.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 1995-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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #define OPENSSL_SUPPRESS_DEPRECATED
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <openssl/err.h>
  18. #include <openssl/bn.h>
  19. #include "rsa_local.h"
  20. /* X9.31 RSA key derivation and generation */
  21. int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,
  22. BIGNUM *q2, const BIGNUM *Xp1, const BIGNUM *Xp2,
  23. const BIGNUM *Xp, const BIGNUM *Xq1, const BIGNUM *Xq2,
  24. const BIGNUM *Xq, const BIGNUM *e, BN_GENCB *cb)
  25. {
  26. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL;
  27. BN_CTX *ctx = NULL, *ctx2 = NULL;
  28. int ret = 0;
  29. if (rsa == NULL)
  30. goto err;
  31. ctx = BN_CTX_new_ex(rsa->libctx);
  32. if (ctx == NULL)
  33. goto err;
  34. BN_CTX_start(ctx);
  35. r0 = BN_CTX_get(ctx);
  36. r1 = BN_CTX_get(ctx);
  37. r2 = BN_CTX_get(ctx);
  38. r3 = BN_CTX_get(ctx);
  39. if (r3 == NULL)
  40. goto err;
  41. if (!rsa->e) {
  42. rsa->e = BN_dup(e);
  43. if (!rsa->e)
  44. goto err;
  45. } else {
  46. e = rsa->e;
  47. }
  48. /*
  49. * If not all parameters present only calculate what we can. This allows
  50. * test programs to output selective parameters.
  51. */
  52. if (Xp && rsa->p == NULL) {
  53. rsa->p = BN_new();
  54. if (rsa->p == NULL)
  55. goto err;
  56. if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,
  57. Xp, Xp1, Xp2, e, ctx, cb))
  58. goto err;
  59. }
  60. if (Xq && rsa->q == NULL) {
  61. rsa->q = BN_new();
  62. if (rsa->q == NULL)
  63. goto err;
  64. if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,
  65. Xq, Xq1, Xq2, e, ctx, cb))
  66. goto err;
  67. }
  68. if (rsa->p == NULL || rsa->q == NULL) {
  69. BN_CTX_end(ctx);
  70. BN_CTX_free(ctx);
  71. return 2;
  72. }
  73. /*
  74. * Since both primes are set we can now calculate all remaining
  75. * components.
  76. */
  77. /* calculate n */
  78. rsa->n = BN_new();
  79. if (rsa->n == NULL)
  80. goto err;
  81. if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))
  82. goto err;
  83. /* calculate d */
  84. if (!BN_sub(r1, rsa->p, BN_value_one()))
  85. goto err; /* p-1 */
  86. if (!BN_sub(r2, rsa->q, BN_value_one()))
  87. goto err; /* q-1 */
  88. if (!BN_mul(r0, r1, r2, ctx))
  89. goto err; /* (p-1)(q-1) */
  90. if (!BN_gcd(r3, r1, r2, ctx))
  91. goto err;
  92. if (!BN_div(r0, NULL, r0, r3, ctx))
  93. goto err; /* LCM((p-1)(q-1)) */
  94. ctx2 = BN_CTX_new();
  95. if (ctx2 == NULL)
  96. goto err;
  97. rsa->d = BN_mod_inverse(NULL, rsa->e, r0, ctx2); /* d */
  98. if (rsa->d == NULL)
  99. goto err;
  100. /* calculate d mod (p-1) */
  101. rsa->dmp1 = BN_new();
  102. if (rsa->dmp1 == NULL)
  103. goto err;
  104. if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx))
  105. goto err;
  106. /* calculate d mod (q-1) */
  107. rsa->dmq1 = BN_new();
  108. if (rsa->dmq1 == NULL)
  109. goto err;
  110. if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx))
  111. goto err;
  112. /* calculate inverse of q mod p */
  113. rsa->iqmp = BN_mod_inverse(NULL, rsa->q, rsa->p, ctx2);
  114. if (rsa->iqmp == NULL)
  115. goto err;
  116. rsa->dirty_cnt++;
  117. ret = 1;
  118. err:
  119. BN_CTX_end(ctx);
  120. BN_CTX_free(ctx);
  121. BN_CTX_free(ctx2);
  122. return ret;
  123. }
  124. int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
  125. BN_GENCB *cb)
  126. {
  127. int ok = 0;
  128. BIGNUM *Xp = NULL, *Xq = NULL;
  129. BN_CTX *ctx = NULL;
  130. ctx = BN_CTX_new_ex(rsa->libctx);
  131. if (ctx == NULL)
  132. goto error;
  133. BN_CTX_start(ctx);
  134. Xp = BN_CTX_get(ctx);
  135. Xq = BN_CTX_get(ctx);
  136. if (Xq == NULL)
  137. goto error;
  138. if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))
  139. goto error;
  140. rsa->p = BN_new();
  141. rsa->q = BN_new();
  142. if (rsa->p == NULL || rsa->q == NULL)
  143. goto error;
  144. /* Generate two primes from Xp, Xq */
  145. if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,
  146. e, ctx, cb))
  147. goto error;
  148. if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,
  149. e, ctx, cb))
  150. goto error;
  151. /*
  152. * Since rsa->p and rsa->q are valid this call will just derive remaining
  153. * RSA components.
  154. */
  155. if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,
  156. NULL, NULL, NULL, NULL, NULL, NULL, e, cb))
  157. goto error;
  158. rsa->dirty_cnt++;
  159. ok = 1;
  160. error:
  161. BN_CTX_end(ctx);
  162. BN_CTX_free(ctx);
  163. if (ok)
  164. return 1;
  165. return 0;
  166. }