rsa_crpt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 1995-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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <openssl/crypto.h>
  16. #include "internal/cryptlib.h"
  17. #include "crypto/bn.h"
  18. #include <openssl/rand.h>
  19. #include "rsa_local.h"
  20. int RSA_bits(const RSA *r)
  21. {
  22. return BN_num_bits(r->n);
  23. }
  24. int RSA_size(const RSA *r)
  25. {
  26. return BN_num_bytes(r->n);
  27. }
  28. int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
  29. RSA *rsa, int padding)
  30. {
  31. return rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding);
  32. }
  33. int RSA_private_encrypt(int flen, const unsigned char *from,
  34. unsigned char *to, RSA *rsa, int padding)
  35. {
  36. return rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding);
  37. }
  38. int RSA_private_decrypt(int flen, const unsigned char *from,
  39. unsigned char *to, RSA *rsa, int padding)
  40. {
  41. return rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding);
  42. }
  43. int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
  44. RSA *rsa, int padding)
  45. {
  46. return rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding);
  47. }
  48. int RSA_flags(const RSA *r)
  49. {
  50. return r == NULL ? 0 : r->meth->flags;
  51. }
  52. void RSA_blinding_off(RSA *rsa)
  53. {
  54. BN_BLINDING_free(rsa->blinding);
  55. rsa->blinding = NULL;
  56. rsa->flags &= ~RSA_FLAG_BLINDING;
  57. rsa->flags |= RSA_FLAG_NO_BLINDING;
  58. }
  59. int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
  60. {
  61. int ret = 0;
  62. if (rsa->blinding != NULL)
  63. RSA_blinding_off(rsa);
  64. rsa->blinding = RSA_setup_blinding(rsa, ctx);
  65. if (rsa->blinding == NULL)
  66. goto err;
  67. rsa->flags |= RSA_FLAG_BLINDING;
  68. rsa->flags &= ~RSA_FLAG_NO_BLINDING;
  69. ret = 1;
  70. err:
  71. return ret;
  72. }
  73. static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
  74. const BIGNUM *q, BN_CTX *ctx)
  75. {
  76. BIGNUM *ret = NULL, *r0, *r1, *r2;
  77. if (d == NULL || p == NULL || q == NULL)
  78. return NULL;
  79. BN_CTX_start(ctx);
  80. r0 = BN_CTX_get(ctx);
  81. r1 = BN_CTX_get(ctx);
  82. r2 = BN_CTX_get(ctx);
  83. if (r2 == NULL)
  84. goto err;
  85. if (!BN_sub(r1, p, BN_value_one()))
  86. goto err;
  87. if (!BN_sub(r2, q, BN_value_one()))
  88. goto err;
  89. if (!BN_mul(r0, r1, r2, ctx))
  90. goto err;
  91. ret = BN_mod_inverse(NULL, d, r0, ctx);
  92. err:
  93. BN_CTX_end(ctx);
  94. return ret;
  95. }
  96. BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
  97. {
  98. BIGNUM *e;
  99. BN_CTX *ctx;
  100. BN_BLINDING *ret = NULL;
  101. if (in_ctx == NULL) {
  102. if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
  103. return 0;
  104. } else {
  105. ctx = in_ctx;
  106. }
  107. BN_CTX_start(ctx);
  108. e = BN_CTX_get(ctx);
  109. if (e == NULL) {
  110. ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
  111. goto err;
  112. }
  113. if (rsa->e == NULL) {
  114. e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
  115. if (e == NULL) {
  116. ERR_raise(ERR_LIB_RSA, RSA_R_NO_PUBLIC_EXPONENT);
  117. goto err;
  118. }
  119. } else {
  120. e = rsa->e;
  121. }
  122. {
  123. BIGNUM *n = BN_new();
  124. if (n == NULL) {
  125. ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
  126. goto err;
  127. }
  128. BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
  129. ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
  130. rsa->_method_mod_n);
  131. /* We MUST free n before any further use of rsa->n */
  132. BN_free(n);
  133. }
  134. if (ret == NULL) {
  135. ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
  136. goto err;
  137. }
  138. BN_BLINDING_set_current_thread(ret);
  139. err:
  140. BN_CTX_end(ctx);
  141. if (ctx != in_ctx)
  142. BN_CTX_free(ctx);
  143. if (e != rsa->e)
  144. BN_free(e);
  145. return ret;
  146. }