rsa_chk.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 1999-2020 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 <openssl/bn.h>
  10. #include <openssl/err.h>
  11. #include "crypto/rsa.h"
  12. #include "rsa_local.h"
  13. #ifndef FIPS_MODE
  14. static int rsa_validate_keypair_multiprime(const RSA *key, BN_GENCB *cb)
  15. {
  16. BIGNUM *i, *j, *k, *l, *m;
  17. BN_CTX *ctx;
  18. int ret = 1, ex_primes = 0, idx;
  19. RSA_PRIME_INFO *pinfo;
  20. if (key->p == NULL || key->q == NULL || key->n == NULL
  21. || key->e == NULL || key->d == NULL) {
  22. RSAerr(0, RSA_R_VALUE_MISSING);
  23. return 0;
  24. }
  25. /* multi-prime? */
  26. if (key->version == RSA_ASN1_VERSION_MULTI) {
  27. ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);
  28. if (ex_primes <= 0
  29. || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {
  30. RSAerr(0, RSA_R_INVALID_MULTI_PRIME_KEY);
  31. return 0;
  32. }
  33. }
  34. i = BN_new();
  35. j = BN_new();
  36. k = BN_new();
  37. l = BN_new();
  38. m = BN_new();
  39. ctx = BN_CTX_new();
  40. if (i == NULL || j == NULL || k == NULL || l == NULL
  41. || m == NULL || ctx == NULL) {
  42. ret = -1;
  43. RSAerr(0, ERR_R_MALLOC_FAILURE);
  44. goto err;
  45. }
  46. if (BN_is_one(key->e)) {
  47. ret = 0;
  48. RSAerr(0, RSA_R_BAD_E_VALUE);
  49. }
  50. if (!BN_is_odd(key->e)) {
  51. ret = 0;
  52. RSAerr(0, RSA_R_BAD_E_VALUE);
  53. }
  54. /* p prime? */
  55. if (BN_check_prime(key->p, NULL, cb) != 1) {
  56. ret = 0;
  57. RSAerr(0, RSA_R_P_NOT_PRIME);
  58. }
  59. /* q prime? */
  60. if (BN_check_prime(key->q, NULL, cb) != 1) {
  61. ret = 0;
  62. RSAerr(0, RSA_R_Q_NOT_PRIME);
  63. }
  64. /* r_i prime? */
  65. for (idx = 0; idx < ex_primes; idx++) {
  66. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  67. if (BN_check_prime(pinfo->r, NULL, cb) != 1) {
  68. ret = 0;
  69. RSAerr(0, RSA_R_MP_R_NOT_PRIME);
  70. }
  71. }
  72. /* n = p*q * r_3...r_i? */
  73. if (!BN_mul(i, key->p, key->q, ctx)) {
  74. ret = -1;
  75. goto err;
  76. }
  77. for (idx = 0; idx < ex_primes; idx++) {
  78. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  79. if (!BN_mul(i, i, pinfo->r, ctx)) {
  80. ret = -1;
  81. goto err;
  82. }
  83. }
  84. if (BN_cmp(i, key->n) != 0) {
  85. ret = 0;
  86. if (ex_primes)
  87. RSAerr(0, RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);
  88. else
  89. RSAerr(0, RSA_R_N_DOES_NOT_EQUAL_P_Q);
  90. }
  91. /* d*e = 1 mod \lambda(n)? */
  92. if (!BN_sub(i, key->p, BN_value_one())) {
  93. ret = -1;
  94. goto err;
  95. }
  96. if (!BN_sub(j, key->q, BN_value_one())) {
  97. ret = -1;
  98. goto err;
  99. }
  100. /* now compute k = \lambda(n) = LCM(i, j, r_3 - 1...) */
  101. if (!BN_mul(l, i, j, ctx)) {
  102. ret = -1;
  103. goto err;
  104. }
  105. if (!BN_gcd(m, i, j, ctx)) {
  106. ret = -1;
  107. goto err;
  108. }
  109. for (idx = 0; idx < ex_primes; idx++) {
  110. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  111. if (!BN_sub(k, pinfo->r, BN_value_one())) {
  112. ret = -1;
  113. goto err;
  114. }
  115. if (!BN_mul(l, l, k, ctx)) {
  116. ret = -1;
  117. goto err;
  118. }
  119. if (!BN_gcd(m, m, k, ctx)) {
  120. ret = -1;
  121. goto err;
  122. }
  123. }
  124. if (!BN_div(k, NULL, l, m, ctx)) { /* remainder is 0 */
  125. ret = -1;
  126. goto err;
  127. }
  128. if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {
  129. ret = -1;
  130. goto err;
  131. }
  132. if (!BN_is_one(i)) {
  133. ret = 0;
  134. RSAerr(0, RSA_R_D_E_NOT_CONGRUENT_TO_1);
  135. }
  136. if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {
  137. /* dmp1 = d mod (p-1)? */
  138. if (!BN_sub(i, key->p, BN_value_one())) {
  139. ret = -1;
  140. goto err;
  141. }
  142. if (!BN_mod(j, key->d, i, ctx)) {
  143. ret = -1;
  144. goto err;
  145. }
  146. if (BN_cmp(j, key->dmp1) != 0) {
  147. ret = 0;
  148. RSAerr(0, RSA_R_DMP1_NOT_CONGRUENT_TO_D);
  149. }
  150. /* dmq1 = d mod (q-1)? */
  151. if (!BN_sub(i, key->q, BN_value_one())) {
  152. ret = -1;
  153. goto err;
  154. }
  155. if (!BN_mod(j, key->d, i, ctx)) {
  156. ret = -1;
  157. goto err;
  158. }
  159. if (BN_cmp(j, key->dmq1) != 0) {
  160. ret = 0;
  161. RSAerr(0, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
  162. }
  163. /* iqmp = q^-1 mod p? */
  164. if (!BN_mod_inverse(i, key->q, key->p, ctx)) {
  165. ret = -1;
  166. goto err;
  167. }
  168. if (BN_cmp(i, key->iqmp) != 0) {
  169. ret = 0;
  170. RSAerr(0, RSA_R_IQMP_NOT_INVERSE_OF_Q);
  171. }
  172. }
  173. for (idx = 0; idx < ex_primes; idx++) {
  174. pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);
  175. /* d_i = d mod (r_i - 1)? */
  176. if (!BN_sub(i, pinfo->r, BN_value_one())) {
  177. ret = -1;
  178. goto err;
  179. }
  180. if (!BN_mod(j, key->d, i, ctx)) {
  181. ret = -1;
  182. goto err;
  183. }
  184. if (BN_cmp(j, pinfo->d) != 0) {
  185. ret = 0;
  186. RSAerr(0, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);
  187. }
  188. /* t_i = R_i ^ -1 mod r_i ? */
  189. if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {
  190. ret = -1;
  191. goto err;
  192. }
  193. if (BN_cmp(i, pinfo->t) != 0) {
  194. ret = 0;
  195. RSAerr(0, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);
  196. }
  197. }
  198. err:
  199. BN_free(i);
  200. BN_free(j);
  201. BN_free(k);
  202. BN_free(l);
  203. BN_free(m);
  204. BN_CTX_free(ctx);
  205. return ret;
  206. }
  207. #endif /* FIPS_MODE */
  208. int rsa_validate_public(const RSA *key)
  209. {
  210. return rsa_sp800_56b_check_public(key);
  211. }
  212. int rsa_validate_private(const RSA *key)
  213. {
  214. return rsa_sp800_56b_check_private(key);
  215. }
  216. int rsa_validate_pairwise(const RSA *key)
  217. {
  218. #ifdef FIPS_MODE
  219. return rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key));
  220. #else
  221. return rsa_validate_keypair_multiprime(key, NULL);
  222. #endif
  223. }
  224. int RSA_check_key(const RSA *key)
  225. {
  226. return RSA_check_key_ex(key, NULL);
  227. }
  228. int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)
  229. {
  230. #ifdef FIPS_MODE
  231. return rsa_validate_public(key)
  232. && rsa_validate_private(key)
  233. && rsa_validate_pairwise(key);
  234. #else
  235. return rsa_validate_keypair_multiprime(key, cb);
  236. #endif /* FIPS_MODE */
  237. }