2
0

rsa_chk.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 1999-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 <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 ret = 1, ex_primes = 0, idx;
  21. RSA_PRIME_INFO *pinfo;
  22. if (key->p == NULL || key->q == NULL || key->n == NULL
  23. || key->e == NULL || key->d == NULL) {
  24. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);
  25. return 0;
  26. }
  27. /* multi-prime? */
  28. if (key->version == RSA_ASN1_VERSION_MULTI) {
  29. ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);
  30. if (ex_primes <= 0
  31. || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {
  32. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);
  33. return 0;
  34. }
  35. }
  36. i = BN_new();
  37. j = BN_new();
  38. k = BN_new();
  39. l = BN_new();
  40. m = BN_new();
  41. ctx = BN_CTX_new();
  42. if (i == NULL || j == NULL || k == NULL || l == NULL
  43. || m == NULL || ctx == NULL) {
  44. ret = -1;
  45. RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);
  46. goto err;
  47. }
  48. if (BN_is_one(key->e)) {
  49. ret = 0;
  50. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);
  51. }
  52. if (!BN_is_odd(key->e)) {
  53. ret = 0;
  54. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);
  55. }
  56. /* p prime? */
  57. if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {
  58. ret = 0;
  59. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);
  60. }
  61. /* q prime? */
  62. if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {
  63. ret = 0;
  64. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);
  65. }
  66. /* r_i prime? */
  67. for (idx = 0; idx < ex_primes; idx++) {
  68. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  69. if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {
  70. ret = 0;
  71. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);
  72. }
  73. }
  74. /* n = p*q * r_3...r_i? */
  75. if (!BN_mul(i, key->p, key->q, ctx)) {
  76. ret = -1;
  77. goto err;
  78. }
  79. for (idx = 0; idx < ex_primes; idx++) {
  80. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  81. if (!BN_mul(i, i, pinfo->r, ctx)) {
  82. ret = -1;
  83. goto err;
  84. }
  85. }
  86. if (BN_cmp(i, key->n) != 0) {
  87. ret = 0;
  88. if (ex_primes)
  89. RSAerr(RSA_F_RSA_CHECK_KEY_EX,
  90. RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);
  91. else
  92. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);
  93. }
  94. /* d*e = 1 mod \lambda(n)? */
  95. if (!BN_sub(i, key->p, BN_value_one())) {
  96. ret = -1;
  97. goto err;
  98. }
  99. if (!BN_sub(j, key->q, BN_value_one())) {
  100. ret = -1;
  101. goto err;
  102. }
  103. /* now compute k = \lambda(n) = LCM(i, j, r_3 - 1...) */
  104. if (!BN_mul(l, i, j, ctx)) {
  105. ret = -1;
  106. goto err;
  107. }
  108. if (!BN_gcd(m, i, j, ctx)) {
  109. ret = -1;
  110. goto err;
  111. }
  112. for (idx = 0; idx < ex_primes; idx++) {
  113. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  114. if (!BN_sub(k, pinfo->r, BN_value_one())) {
  115. ret = -1;
  116. goto err;
  117. }
  118. if (!BN_mul(l, l, k, ctx)) {
  119. ret = -1;
  120. goto err;
  121. }
  122. if (!BN_gcd(m, m, k, ctx)) {
  123. ret = -1;
  124. goto err;
  125. }
  126. }
  127. if (!BN_div(k, NULL, l, m, ctx)) { /* remainder is 0 */
  128. ret = -1;
  129. goto err;
  130. }
  131. if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {
  132. ret = -1;
  133. goto err;
  134. }
  135. if (!BN_is_one(i)) {
  136. ret = 0;
  137. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);
  138. }
  139. if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
  140. /* dmp1 = d mod (p-1)? */
  141. if (!BN_sub(i, key->p, BN_value_one())) {
  142. ret = -1;
  143. goto err;
  144. }
  145. if (!BN_mod(j, key->d, i, ctx)) {
  146. ret = -1;
  147. goto err;
  148. }
  149. if (BN_cmp(j, key->dmp1) != 0) {
  150. ret = 0;
  151. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
  152. }
  153. /* dmq1 = d mod (q-1)? */
  154. if (!BN_sub(i, key->q, BN_value_one())) {
  155. ret = -1;
  156. goto err;
  157. }
  158. if (!BN_mod(j, key->d, i, ctx)) {
  159. ret = -1;
  160. goto err;
  161. }
  162. if (BN_cmp(j, key->dmq1) != 0) {
  163. ret = 0;
  164. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
  165. }
  166. /* iqmp = q^-1 mod p? */
  167. if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
  168. ret = -1;
  169. goto err;
  170. }
  171. if (BN_cmp(i, key->iqmp) != 0) {
  172. ret = 0;
  173. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);
  174. }
  175. }
  176. for (idx = 0; idx < ex_primes; idx++) {
  177. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  178. /* d_i = d mod (r_i - 1)? */
  179. if (!BN_sub(i, pinfo->r, BN_value_one())) {
  180. ret = -1;
  181. goto err;
  182. }
  183. if (!BN_mod(j, key->d, i, ctx)) {
  184. ret = -1;
  185. goto err;
  186. }
  187. if (BN_cmp(j, pinfo->d) != 0) {
  188. ret = 0;
  189. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);
  190. }
  191. /* t_i = R_i ^ -1 mod r_i ? */
  192. if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {
  193. ret = -1;
  194. goto err;
  195. }
  196. if (BN_cmp(i, pinfo->t) != 0) {
  197. ret = 0;
  198. RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);
  199. }
  200. }
  201. err:
  202. BN_free(i);
  203. BN_free(j);
  204. BN_free(k);
  205. BN_free(l);
  206. BN_free(m);
  207. BN_CTX_free(ctx);
  208. return ret;
  209. }