rsa_mp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2017 BaishanCloud. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <openssl/bn.h>
  11. #include <openssl/err.h>
  12. #include "rsa_local.h"
  13. void ossl_rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo)
  14. {
  15. /* free pp and pinfo only */
  16. BN_clear_free(pinfo->pp);
  17. OPENSSL_free(pinfo);
  18. }
  19. void ossl_rsa_multip_info_free(RSA_PRIME_INFO *pinfo)
  20. {
  21. /* free an RSA_PRIME_INFO structure */
  22. BN_clear_free(pinfo->r);
  23. BN_clear_free(pinfo->d);
  24. BN_clear_free(pinfo->t);
  25. ossl_rsa_multip_info_free_ex(pinfo);
  26. }
  27. RSA_PRIME_INFO *ossl_rsa_multip_info_new(void)
  28. {
  29. RSA_PRIME_INFO *pinfo;
  30. /* create an RSA_PRIME_INFO structure */
  31. if ((pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO))) == NULL)
  32. return NULL;
  33. if ((pinfo->r = BN_secure_new()) == NULL)
  34. goto err;
  35. if ((pinfo->d = BN_secure_new()) == NULL)
  36. goto err;
  37. if ((pinfo->t = BN_secure_new()) == NULL)
  38. goto err;
  39. if ((pinfo->pp = BN_secure_new()) == NULL)
  40. goto err;
  41. return pinfo;
  42. err:
  43. BN_free(pinfo->r);
  44. BN_free(pinfo->d);
  45. BN_free(pinfo->t);
  46. BN_free(pinfo->pp);
  47. OPENSSL_free(pinfo);
  48. return NULL;
  49. }
  50. /* Refill products of primes */
  51. int ossl_rsa_multip_calc_product(RSA *rsa)
  52. {
  53. RSA_PRIME_INFO *pinfo;
  54. BIGNUM *p1 = NULL, *p2 = NULL;
  55. BN_CTX *ctx = NULL;
  56. int i, rv = 0, ex_primes;
  57. if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) {
  58. /* invalid */
  59. goto err;
  60. }
  61. if ((ctx = BN_CTX_new()) == NULL)
  62. goto err;
  63. /* calculate pinfo->pp = p * q for first 'extra' prime */
  64. p1 = rsa->p;
  65. p2 = rsa->q;
  66. for (i = 0; i < ex_primes; i++) {
  67. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  68. if (pinfo->pp == NULL) {
  69. pinfo->pp = BN_secure_new();
  70. if (pinfo->pp == NULL)
  71. goto err;
  72. }
  73. if (!BN_mul(pinfo->pp, p1, p2, ctx))
  74. goto err;
  75. /* save previous one */
  76. p1 = pinfo->pp;
  77. p2 = pinfo->r;
  78. }
  79. rv = 1;
  80. err:
  81. BN_CTX_free(ctx);
  82. return rv;
  83. }
  84. int ossl_rsa_multip_cap(int bits)
  85. {
  86. int cap = 5;
  87. if (bits < 1024)
  88. cap = 2;
  89. else if (bits < 4096)
  90. cap = 3;
  91. else if (bits < 8192)
  92. cap = 4;
  93. if (cap > RSA_MAX_PRIME_NUM)
  94. cap = RSA_MAX_PRIME_NUM;
  95. return cap;
  96. }