rsa_crpt.c 3.9 KB

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