rsa_x931g.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 1995-2017 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 <stdio.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <openssl/err.h>
  13. #include <openssl/bn.h>
  14. #include "rsa_local.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. if (rsa->iqmp == NULL)
  110. goto err;
  111. ret = 1;
  112. err:
  113. BN_CTX_end(ctx);
  114. BN_CTX_free(ctx);
  115. BN_CTX_free(ctx2);
  116. return ret;
  117. }
  118. int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
  119. BN_GENCB *cb)
  120. {
  121. int ok = 0;
  122. BIGNUM *Xp = NULL, *Xq = NULL;
  123. BN_CTX *ctx = NULL;
  124. ctx = BN_CTX_new();
  125. if (ctx == NULL)
  126. goto error;
  127. BN_CTX_start(ctx);
  128. Xp = BN_CTX_get(ctx);
  129. Xq = BN_CTX_get(ctx);
  130. if (Xq == NULL)
  131. goto error;
  132. if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))
  133. goto error;
  134. rsa->p = BN_new();
  135. rsa->q = BN_new();
  136. if (rsa->p == NULL || rsa->q == NULL)
  137. goto error;
  138. /* Generate two primes from Xp, Xq */
  139. if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,
  140. e, ctx, cb))
  141. goto error;
  142. if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,
  143. e, ctx, cb))
  144. goto error;
  145. /*
  146. * Since rsa->p and rsa->q are valid this call will just derive remaining
  147. * RSA components.
  148. */
  149. if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,
  150. NULL, NULL, NULL, NULL, NULL, NULL, e, cb))
  151. goto error;
  152. ok = 1;
  153. error:
  154. BN_CTX_end(ctx);
  155. BN_CTX_free(ctx);
  156. if (ok)
  157. return 1;
  158. return 0;
  159. }