rsa_x931g.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 1995-2016 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. * If not all parameters present only calculate what we can. This allows
  44. * test programs to output selective parameters.
  45. */
  46. if (Xp && rsa->p == NULL) {
  47. rsa->p = BN_new();
  48. if (rsa->p == NULL)
  49. goto err;
  50. if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,
  51. Xp, Xp1, Xp2, e, ctx, cb))
  52. goto err;
  53. }
  54. if (Xq && rsa->q == NULL) {
  55. rsa->q = BN_new();
  56. if (rsa->q == NULL)
  57. goto err;
  58. if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,
  59. Xq, Xq1, Xq2, e, ctx, cb))
  60. goto err;
  61. }
  62. if (rsa->p == NULL || rsa->q == NULL) {
  63. BN_CTX_end(ctx);
  64. BN_CTX_free(ctx);
  65. return 2;
  66. }
  67. /*
  68. * Since both primes are set we can now calculate all remaining
  69. * components.
  70. */
  71. /* calculate n */
  72. rsa->n = BN_new();
  73. if (rsa->n == NULL)
  74. goto err;
  75. if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))
  76. goto err;
  77. /* calculate d */
  78. if (!BN_sub(r1, rsa->p, BN_value_one()))
  79. goto err; /* p-1 */
  80. if (!BN_sub(r2, rsa->q, BN_value_one()))
  81. goto err; /* q-1 */
  82. if (!BN_mul(r0, r1, r2, ctx))
  83. goto err; /* (p-1)(q-1) */
  84. if (!BN_gcd(r3, r1, r2, ctx))
  85. goto err;
  86. if (!BN_div(r0, NULL, r0, r3, ctx))
  87. goto err; /* LCM((p-1)(q-1)) */
  88. ctx2 = BN_CTX_new();
  89. if (ctx2 == NULL)
  90. goto err;
  91. rsa->d = BN_mod_inverse(NULL, rsa->e, r0, ctx2); /* d */
  92. if (rsa->d == NULL)
  93. goto err;
  94. /* calculate d mod (p-1) */
  95. rsa->dmp1 = BN_new();
  96. if (rsa->dmp1 == NULL)
  97. goto err;
  98. if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx))
  99. goto err;
  100. /* calculate d mod (q-1) */
  101. rsa->dmq1 = BN_new();
  102. if (rsa->dmq1 == NULL)
  103. goto err;
  104. if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx))
  105. goto err;
  106. /* calculate inverse of q mod p */
  107. rsa->iqmp = BN_mod_inverse(NULL, rsa->q, rsa->p, ctx2);
  108. ret = 1;
  109. err:
  110. if (ctx)
  111. BN_CTX_end(ctx);
  112. BN_CTX_free(ctx);
  113. BN_CTX_free(ctx2);
  114. return ret;
  115. }
  116. int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
  117. BN_GENCB *cb)
  118. {
  119. int ok = 0;
  120. BIGNUM *Xp = NULL, *Xq = NULL;
  121. BN_CTX *ctx = NULL;
  122. ctx = BN_CTX_new();
  123. if (ctx == NULL)
  124. goto error;
  125. BN_CTX_start(ctx);
  126. Xp = BN_CTX_get(ctx);
  127. Xq = BN_CTX_get(ctx);
  128. if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))
  129. goto error;
  130. rsa->p = BN_new();
  131. rsa->q = BN_new();
  132. if (rsa->p == NULL || rsa->q == NULL)
  133. goto error;
  134. /* Generate two primes from Xp, Xq */
  135. if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,
  136. e, ctx, cb))
  137. goto error;
  138. if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,
  139. e, ctx, cb))
  140. goto error;
  141. /*
  142. * Since rsa->p and rsa->q are valid this call will just derive remaining
  143. * RSA components.
  144. */
  145. if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,
  146. NULL, NULL, NULL, NULL, NULL, NULL, e, cb))
  147. goto error;
  148. ok = 1;
  149. error:
  150. if (ctx)
  151. BN_CTX_end(ctx);
  152. BN_CTX_free(ctx);
  153. if (ok)
  154. return 1;
  155. return 0;
  156. }