2
0

rsa_mp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 a 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 a RSA_PRIME_INFO structure */
  31. if ((pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO))) == NULL) {
  32. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  33. return NULL;
  34. }
  35. if ((pinfo->r = BN_secure_new()) == NULL)
  36. goto err;
  37. if ((pinfo->d = BN_secure_new()) == NULL)
  38. goto err;
  39. if ((pinfo->t = BN_secure_new()) == NULL)
  40. goto err;
  41. if ((pinfo->pp = BN_secure_new()) == NULL)
  42. goto err;
  43. return pinfo;
  44. err:
  45. BN_free(pinfo->r);
  46. BN_free(pinfo->d);
  47. BN_free(pinfo->t);
  48. BN_free(pinfo->pp);
  49. OPENSSL_free(pinfo);
  50. return NULL;
  51. }
  52. /* Refill products of primes */
  53. int ossl_rsa_multip_calc_product(RSA *rsa)
  54. {
  55. RSA_PRIME_INFO *pinfo;
  56. BIGNUM *p1 = NULL, *p2 = NULL;
  57. BN_CTX *ctx = NULL;
  58. int i, rv = 0, ex_primes;
  59. if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) {
  60. /* invalid */
  61. goto err;
  62. }
  63. if ((ctx = BN_CTX_new()) == NULL)
  64. goto err;
  65. /* calculate pinfo->pp = p * q for first 'extra' prime */
  66. p1 = rsa->p;
  67. p2 = rsa->q;
  68. for (i = 0; i < ex_primes; i++) {
  69. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  70. if (pinfo->pp == NULL) {
  71. pinfo->pp = BN_secure_new();
  72. if (pinfo->pp == NULL)
  73. goto err;
  74. }
  75. if (!BN_mul(pinfo->pp, p1, p2, ctx))
  76. goto err;
  77. /* save previous one */
  78. p1 = pinfo->pp;
  79. p2 = pinfo->r;
  80. }
  81. rv = 1;
  82. err:
  83. BN_CTX_free(ctx);
  84. return rv;
  85. }
  86. int ossl_rsa_multip_cap(int bits)
  87. {
  88. int cap = 5;
  89. if (bits < 1024)
  90. cap = 2;
  91. else if (bits < 4096)
  92. cap = 3;
  93. else if (bits < 8192)
  94. cap = 4;
  95. if (cap > RSA_MAX_PRIME_NUM)
  96. cap = RSA_MAX_PRIME_NUM;
  97. return cap;
  98. }