rsa_x931g.c 4.7 KB

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