rsa_meth.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2016-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 <string.h>
  15. #include "rsa_local.h"
  16. #include <openssl/err.h>
  17. RSA_METHOD *RSA_meth_new(const char *name, int flags)
  18. {
  19. RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth));
  20. if (meth != NULL) {
  21. meth->flags = flags;
  22. meth->name = OPENSSL_strdup(name);
  23. if (meth->name != NULL)
  24. return meth;
  25. OPENSSL_free(meth);
  26. }
  27. return NULL;
  28. }
  29. void RSA_meth_free(RSA_METHOD *meth)
  30. {
  31. if (meth != NULL) {
  32. OPENSSL_free(meth->name);
  33. OPENSSL_free(meth);
  34. }
  35. }
  36. RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
  37. {
  38. RSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
  39. if (ret != NULL) {
  40. memcpy(ret, meth, sizeof(*meth));
  41. ret->name = OPENSSL_strdup(meth->name);
  42. if (ret->name != NULL)
  43. return ret;
  44. OPENSSL_free(ret);
  45. }
  46. return NULL;
  47. }
  48. const char *RSA_meth_get0_name(const RSA_METHOD *meth)
  49. {
  50. return meth->name;
  51. }
  52. int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
  53. {
  54. char *tmpname = OPENSSL_strdup(name);
  55. if (tmpname == NULL)
  56. return 0;
  57. OPENSSL_free(meth->name);
  58. meth->name = tmpname;
  59. return 1;
  60. }
  61. int RSA_meth_get_flags(const RSA_METHOD *meth)
  62. {
  63. return meth->flags;
  64. }
  65. int RSA_meth_set_flags(RSA_METHOD *meth, int flags)
  66. {
  67. meth->flags = flags;
  68. return 1;
  69. }
  70. void *RSA_meth_get0_app_data(const RSA_METHOD *meth)
  71. {
  72. return meth->app_data;
  73. }
  74. int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data)
  75. {
  76. meth->app_data = app_data;
  77. return 1;
  78. }
  79. int (*RSA_meth_get_pub_enc(const RSA_METHOD *meth))
  80. (int flen, const unsigned char *from,
  81. unsigned char *to, RSA *rsa, int padding)
  82. {
  83. return meth->rsa_pub_enc;
  84. }
  85. int RSA_meth_set_pub_enc(RSA_METHOD *meth,
  86. int (*pub_enc) (int flen, const unsigned char *from,
  87. unsigned char *to, RSA *rsa,
  88. int padding))
  89. {
  90. meth->rsa_pub_enc = pub_enc;
  91. return 1;
  92. }
  93. int (*RSA_meth_get_pub_dec(const RSA_METHOD *meth))
  94. (int flen, const unsigned char *from,
  95. unsigned char *to, RSA *rsa, int padding)
  96. {
  97. return meth->rsa_pub_dec;
  98. }
  99. int RSA_meth_set_pub_dec(RSA_METHOD *meth,
  100. int (*pub_dec) (int flen, const unsigned char *from,
  101. unsigned char *to, RSA *rsa,
  102. int padding))
  103. {
  104. meth->rsa_pub_dec = pub_dec;
  105. return 1;
  106. }
  107. int (*RSA_meth_get_priv_enc(const RSA_METHOD *meth))
  108. (int flen, const unsigned char *from,
  109. unsigned char *to, RSA *rsa, int padding)
  110. {
  111. return meth->rsa_priv_enc;
  112. }
  113. int RSA_meth_set_priv_enc(RSA_METHOD *meth,
  114. int (*priv_enc) (int flen, const unsigned char *from,
  115. unsigned char *to, RSA *rsa,
  116. int padding))
  117. {
  118. meth->rsa_priv_enc = priv_enc;
  119. return 1;
  120. }
  121. int (*RSA_meth_get_priv_dec(const RSA_METHOD *meth))
  122. (int flen, const unsigned char *from,
  123. unsigned char *to, RSA *rsa, int padding)
  124. {
  125. return meth->rsa_priv_dec;
  126. }
  127. int RSA_meth_set_priv_dec(RSA_METHOD *meth,
  128. int (*priv_dec) (int flen, const unsigned char *from,
  129. unsigned char *to, RSA *rsa,
  130. int padding))
  131. {
  132. meth->rsa_priv_dec = priv_dec;
  133. return 1;
  134. }
  135. /* Can be null */
  136. int (*RSA_meth_get_mod_exp(const RSA_METHOD *meth))
  137. (BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx)
  138. {
  139. return meth->rsa_mod_exp;
  140. }
  141. int RSA_meth_set_mod_exp(RSA_METHOD *meth,
  142. int (*mod_exp) (BIGNUM *r0, const BIGNUM *i, RSA *rsa,
  143. BN_CTX *ctx))
  144. {
  145. meth->rsa_mod_exp = mod_exp;
  146. return 1;
  147. }
  148. /* Can be null */
  149. int (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth))
  150. (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  151. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  152. {
  153. return meth->bn_mod_exp;
  154. }
  155. int RSA_meth_set_bn_mod_exp(RSA_METHOD *meth,
  156. int (*bn_mod_exp) (BIGNUM *r,
  157. const BIGNUM *a,
  158. const BIGNUM *p,
  159. const BIGNUM *m,
  160. BN_CTX *ctx,
  161. BN_MONT_CTX *m_ctx))
  162. {
  163. meth->bn_mod_exp = bn_mod_exp;
  164. return 1;
  165. }
  166. /* called at new */
  167. int (*RSA_meth_get_init(const RSA_METHOD *meth)) (RSA *rsa)
  168. {
  169. return meth->init;
  170. }
  171. int RSA_meth_set_init(RSA_METHOD *meth, int (*init) (RSA *rsa))
  172. {
  173. meth->init = init;
  174. return 1;
  175. }
  176. /* called at free */
  177. int (*RSA_meth_get_finish(const RSA_METHOD *meth)) (RSA *rsa)
  178. {
  179. return meth->finish;
  180. }
  181. int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
  182. {
  183. meth->finish = finish;
  184. return 1;
  185. }
  186. int (*RSA_meth_get_sign(const RSA_METHOD *meth))
  187. (int type,
  188. const unsigned char *m, unsigned int m_length,
  189. unsigned char *sigret, unsigned int *siglen,
  190. const RSA *rsa)
  191. {
  192. return meth->rsa_sign;
  193. }
  194. int RSA_meth_set_sign(RSA_METHOD *meth,
  195. int (*sign) (int type, const unsigned char *m,
  196. unsigned int m_length,
  197. unsigned char *sigret, unsigned int *siglen,
  198. const RSA *rsa))
  199. {
  200. meth->rsa_sign = sign;
  201. return 1;
  202. }
  203. int (*RSA_meth_get_verify(const RSA_METHOD *meth))
  204. (int dtype, const unsigned char *m,
  205. unsigned int m_length, const unsigned char *sigbuf,
  206. unsigned int siglen, const RSA *rsa)
  207. {
  208. return meth->rsa_verify;
  209. }
  210. int RSA_meth_set_verify(RSA_METHOD *meth,
  211. int (*verify) (int dtype, const unsigned char *m,
  212. unsigned int m_length,
  213. const unsigned char *sigbuf,
  214. unsigned int siglen, const RSA *rsa))
  215. {
  216. meth->rsa_verify = verify;
  217. return 1;
  218. }
  219. int (*RSA_meth_get_keygen(const RSA_METHOD *meth))
  220. (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
  221. {
  222. return meth->rsa_keygen;
  223. }
  224. int RSA_meth_set_keygen(RSA_METHOD *meth,
  225. int (*keygen) (RSA *rsa, int bits, BIGNUM *e,
  226. BN_GENCB *cb))
  227. {
  228. meth->rsa_keygen = keygen;
  229. return 1;
  230. }
  231. int (*RSA_meth_get_multi_prime_keygen(const RSA_METHOD *meth))
  232. (RSA *rsa, int bits, int primes, BIGNUM *e, BN_GENCB *cb)
  233. {
  234. return meth->rsa_multi_prime_keygen;
  235. }
  236. int RSA_meth_set_multi_prime_keygen(RSA_METHOD *meth,
  237. int (*keygen) (RSA *rsa, int bits,
  238. int primes, BIGNUM *e,
  239. BN_GENCB *cb))
  240. {
  241. meth->rsa_multi_prime_keygen = keygen;
  242. return 1;
  243. }