rsaref.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* rsaref/rsaref.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #ifndef NO_RSA
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/bn.h>
  62. #include <openssl/rsa.h>
  63. #include <openssl/rsaref.h>
  64. #include <openssl/rand.h>
  65. static int RSAref_bn2bin(BIGNUM * from, unsigned char* to, int max);
  66. #ifdef undef
  67. static BIGNUM* RSAref_bin2bn(unsigned char* from, BIGNUM * to, int max);
  68. #endif
  69. static int RSAref_Public_eay2ref(RSA * from, RSArefPublicKey * to);
  70. static int RSAref_Private_eay2ref(RSA * from, RSArefPrivateKey * to);
  71. int RSA_ref_private_decrypt(int len, unsigned char *from,
  72. unsigned char *to, RSA *rsa, int padding);
  73. int RSA_ref_private_encrypt(int len, unsigned char *from,
  74. unsigned char *to, RSA *rsa, int padding);
  75. int RSA_ref_public_encrypt(int len, unsigned char *from,
  76. unsigned char *to, RSA *rsa, int padding);
  77. int RSA_ref_public_decrypt(int len, unsigned char *from,
  78. unsigned char *to, RSA *rsa, int padding);
  79. static int BN_ref_mod_exp(BIGNUM *r,BIGNUM *a,const BIGNUM *p,const BIGNUM *m,
  80. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  81. static int RSA_ref_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa);
  82. static RSA_METHOD rsa_pkcs1_ref_meth={
  83. "RSAref PKCS#1 RSA",
  84. RSA_ref_public_encrypt,
  85. RSA_ref_public_decrypt,
  86. RSA_ref_private_encrypt,
  87. RSA_ref_private_decrypt,
  88. RSA_ref_mod_exp,
  89. BN_ref_mod_exp,
  90. NULL,
  91. NULL,
  92. 0,
  93. NULL,
  94. };
  95. RSA_METHOD *RSA_PKCS1_RSAref(void)
  96. {
  97. return(&rsa_pkcs1_ref_meth);
  98. }
  99. static int RSA_ref_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
  100. {
  101. RSAREFerr(RSAREF_F_RSA_REF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  102. return(0);
  103. }
  104. static int BN_ref_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
  105. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  106. {
  107. RSAREFerr(RSAREF_F_BN_REF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  108. return(0);
  109. }
  110. /* unsigned char *to: [max] */
  111. static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
  112. {
  113. int i;
  114. i=BN_num_bytes(from);
  115. if (i > max)
  116. {
  117. RSAREFerr(RSAREF_F_RSAREF_BN2BIN,RSAREF_R_LEN);
  118. return(0);
  119. }
  120. memset(to,0,(unsigned int)max);
  121. if (!BN_bn2bin(from,&(to[max-i])))
  122. return(0);
  123. return(1);
  124. }
  125. #ifdef undef
  126. /* unsigned char *from: [max] */
  127. static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
  128. {
  129. int i;
  130. BIGNUM *ret;
  131. for (i=0; i<max; i++)
  132. if (from[i]) break;
  133. ret=BN_bin2bn(&(from[i]),max-i,to);
  134. return(ret);
  135. }
  136. static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
  137. {
  138. to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
  139. to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
  140. if ((to->n == NULL) || (to->e == NULL)) return(0);
  141. return(1);
  142. }
  143. #endif
  144. static int RSAref_Public_eay2ref(RSA *from, RSArefPublicKey *to)
  145. {
  146. to->bits=BN_num_bits(from->n);
  147. if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
  148. if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
  149. return(1);
  150. }
  151. #ifdef undef
  152. static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
  153. {
  154. if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
  155. return(0);
  156. if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
  157. return(0);
  158. if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
  159. return(0);
  160. if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
  161. return(0);
  162. if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
  163. return(0);
  164. if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
  165. == NULL)
  166. return(0);
  167. if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
  168. == NULL)
  169. return(0);
  170. if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
  171. return(0);
  172. return(1);
  173. }
  174. #endif
  175. static int RSAref_Private_eay2ref(RSA *from, RSArefPrivateKey *to)
  176. {
  177. to->bits=BN_num_bits(from->n);
  178. if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
  179. if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
  180. if (!RSAref_bn2bin(from->d,to->d,RSAref_MAX_LEN)) return(0);
  181. if (!RSAref_bn2bin(from->p,to->prime[0],RSAref_MAX_PLEN)) return(0);
  182. if (!RSAref_bn2bin(from->q,to->prime[1],RSAref_MAX_PLEN)) return(0);
  183. if (!RSAref_bn2bin(from->dmp1,to->pexp[0],RSAref_MAX_PLEN)) return(0);
  184. if (!RSAref_bn2bin(from->dmq1,to->pexp[1],RSAref_MAX_PLEN)) return(0);
  185. if (!RSAref_bn2bin(from->iqmp,to->coef,RSAref_MAX_PLEN)) return(0);
  186. return(1);
  187. }
  188. int RSA_ref_private_decrypt(int len, unsigned char *from, unsigned char *to,
  189. RSA *rsa, int padding)
  190. {
  191. int i,outlen= -1;
  192. RSArefPrivateKey RSAkey;
  193. if (!RSAref_Private_eay2ref(rsa,&RSAkey))
  194. goto err;
  195. if ((i=RSAPrivateDecrypt(to,&outlen,from,len,&RSAkey)) != 0)
  196. {
  197. RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_DECRYPT,i);
  198. outlen= -1;
  199. }
  200. err:
  201. memset(&RSAkey,0,sizeof(RSAkey));
  202. return(outlen);
  203. }
  204. int RSA_ref_private_encrypt(int len, unsigned char *from, unsigned char *to,
  205. RSA *rsa, int padding)
  206. {
  207. int i,outlen= -1;
  208. RSArefPrivateKey RSAkey;
  209. if (padding != RSA_PKCS1_PADDING)
  210. {
  211. RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  212. goto err;
  213. }
  214. if (!RSAref_Private_eay2ref(rsa,&RSAkey))
  215. goto err;
  216. if ((i=RSAPrivateEncrypt(to,&outlen,from,len,&RSAkey)) != 0)
  217. {
  218. RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT,i);
  219. outlen= -1;
  220. }
  221. err:
  222. memset(&RSAkey,0,sizeof(RSAkey));
  223. return(outlen);
  224. }
  225. int RSA_ref_public_decrypt(int len, unsigned char *from, unsigned char *to,
  226. RSA *rsa, int padding)
  227. {
  228. int i,outlen= -1;
  229. RSArefPublicKey RSAkey;
  230. if (!RSAref_Public_eay2ref(rsa,&RSAkey))
  231. goto err;
  232. if ((i=RSAPublicDecrypt(to,&outlen,from,len,&RSAkey)) != 0)
  233. {
  234. RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_DECRYPT,i);
  235. outlen= -1;
  236. }
  237. err:
  238. memset(&RSAkey,0,sizeof(RSAkey));
  239. return(outlen);
  240. }
  241. int RSA_ref_public_encrypt(int len, unsigned char *from, unsigned char *to,
  242. RSA *rsa, int padding)
  243. {
  244. int outlen= -1;
  245. int i;
  246. RSArefPublicKey RSAkey;
  247. RSARandomState rnd;
  248. unsigned char buf[16];
  249. if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING)
  250. {
  251. RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  252. goto err;
  253. }
  254. R_RandomInit(&rnd);
  255. R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
  256. while (i > 0)
  257. {
  258. if (RAND_bytes(buf,16) <= 0)
  259. goto err;
  260. R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
  261. i-=16;
  262. }
  263. if (!RSAref_Public_eay2ref(rsa,&RSAkey))
  264. goto err;
  265. if ((i=RSAPublicEncrypt(to,&outlen,from,len,&RSAkey,&rnd)) != 0)
  266. {
  267. RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,i);
  268. outlen= -1;
  269. goto err;
  270. }
  271. err:
  272. memset(&RSAkey,0,sizeof(RSAkey));
  273. R_RandomFinal(&rnd);
  274. memset(&rnd,0,sizeof(rnd));
  275. return(outlen);
  276. }
  277. #else /* !NO_RSA */
  278. # if PEDANTIC
  279. static void *dummy=&dummy;
  280. # endif
  281. #endif