A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.8 KiB

  1. /*
  2. * Copyright 1999-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 <openssl/bn.h>
  10. #include <openssl/err.h>
  11. #include "rsa_locl.h"
  12. int RSA_check_key(const RSA *key)
  13. {
  14. return RSA_check_key_ex(key, NULL);
  15. }
  16. int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)
  17. {
  18. BIGNUM *i, *j, *k, *l, *m;
  19. BN_CTX *ctx;
  20. int r;
  21. int ret = 1;
  22. if (!key->p || !key->q || !key->n || !key->e || !key->d) {
  23. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);
  24. return 0;
  25. }
  26. i = BN_new();
  27. j = BN_new();
  28. k = BN_new();
  29. l = BN_new();
  30. m = BN_new();
  31. ctx = BN_CTX_new();
  32. if (i == NULL || j == NULL || k == NULL || l == NULL ||
  33. m == NULL || ctx == NULL) {
  34. ret = -1;
  35. RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);
  36. goto err;
  37. }
  38. /* p prime? */
  39. r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb);
  40. if (r != 1) {
  41. ret = r;
  42. if (r != 0)
  43. goto err;
  44. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);
  45. }
  46. /* q prime? */
  47. r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb);
  48. if (r != 1) {
  49. ret = r;
  50. if (r != 0)
  51. goto err;
  52. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);
  53. }
  54. /* n = p*q? */
  55. r = BN_mul(i, key->p, key->q, ctx);
  56. if (!r) {
  57. ret = -1;
  58. goto err;
  59. }
  60. if (BN_cmp(i, key->n) != 0) {
  61. ret = 0;
  62. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);
  63. }
  64. /* d*e = 1 mod lcm(p-1,q-1)? */
  65. r = BN_sub(i, key->p, BN_value_one());
  66. if (!r) {
  67. ret = -1;
  68. goto err;
  69. }
  70. r = BN_sub(j, key->q, BN_value_one());
  71. if (!r) {
  72. ret = -1;
  73. goto err;
  74. }
  75. /* now compute k = lcm(i,j) */
  76. r = BN_mul(l, i, j, ctx);
  77. if (!r) {
  78. ret = -1;
  79. goto err;
  80. }
  81. r = BN_gcd(m, i, j, ctx);
  82. if (!r) {
  83. ret = -1;
  84. goto err;
  85. }
  86. r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
  87. if (!r) {
  88. ret = -1;
  89. goto err;
  90. }
  91. r = BN_mod_mul(i, key->d, key->e, k, ctx);
  92. if (!r) {
  93. ret = -1;
  94. goto err;
  95. }
  96. if (!BN_is_one(i)) {
  97. ret = 0;
  98. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);
  99. }
  100. if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
  101. /* dmp1 = d mod (p-1)? */
  102. r = BN_sub(i, key->p, BN_value_one());
  103. if (!r) {
  104. ret = -1;
  105. goto err;
  106. }
  107. r = BN_mod(j, key->d, i, ctx);
  108. if (!r) {
  109. ret = -1;
  110. goto err;
  111. }
  112. if (BN_cmp(j, key->dmp1) != 0) {
  113. ret = 0;
  114. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
  115. }
  116. /* dmq1 = d mod (q-1)? */
  117. r = BN_sub(i, key->q, BN_value_one());
  118. if (!r) {
  119. ret = -1;
  120. goto err;
  121. }
  122. r = BN_mod(j, key->d, i, ctx);
  123. if (!r) {
  124. ret = -1;
  125. goto err;
  126. }
  127. if (BN_cmp(j, key->dmq1) != 0) {
  128. ret = 0;
  129. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
  130. }
  131. /* iqmp = q^-1 mod p? */
  132. if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
  133. ret = -1;
  134. goto err;
  135. }
  136. if (BN_cmp(i, key->iqmp) != 0) {
  137. ret = 0;
  138. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);
  139. }
  140. }
  141. err:
  142. BN_free(i);
  143. BN_free(j);
  144. BN_free(k);
  145. BN_free(l);
  146. BN_free(m);
  147. BN_CTX_free(ctx);
  148. return (ret);
  149. }