2
0

rsa_meth.c 7.3 KB

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