rsa_chk.c 6.5 KB

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