選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

rsa.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright(C) 2006 Cameron Rich
  3. *
  4. * This library is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2.1 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * Implements the RSA public encryption algorithm. Uses the bigint library to
  20. * perform its calculations.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <stdlib.h>
  26. #include "crypto.h"
  27. #ifdef CONFIG_BIGINT_CRT
  28. static bigint *bi_crt(const RSA_CTX *rsa, bigint *bi);
  29. #endif
  30. void RSA_priv_key_new(RSA_CTX **ctx,
  31. const uint8_t *modulus, int mod_len,
  32. const uint8_t *pub_exp, int pub_len,
  33. const uint8_t *priv_exp, int priv_len
  34. #if CONFIG_BIGINT_CRT
  35. , const uint8_t *p, int p_len,
  36. const uint8_t *q, int q_len,
  37. const uint8_t *dP, int dP_len,
  38. const uint8_t *dQ, int dQ_len,
  39. const uint8_t *qInv, int qInv_len
  40. #endif
  41. )
  42. {
  43. RSA_CTX *rsa_ctx;
  44. BI_CTX *bi_ctx;
  45. RSA_pub_key_new(ctx, modulus, mod_len, pub_exp, pub_len);
  46. rsa_ctx = *ctx;
  47. bi_ctx = rsa_ctx->bi_ctx;
  48. rsa_ctx->d = bi_import(bi_ctx, priv_exp, priv_len);
  49. bi_permanent(rsa_ctx->d);
  50. #ifdef CONFIG_BIGINT_CRT
  51. rsa_ctx->p = bi_import(bi_ctx, p, p_len);
  52. rsa_ctx->q = bi_import(bi_ctx, q, q_len);
  53. rsa_ctx->dP = bi_import(bi_ctx, dP, dP_len);
  54. rsa_ctx->dQ = bi_import(bi_ctx, dQ, dQ_len);
  55. rsa_ctx->qInv = bi_import(bi_ctx, qInv, qInv_len);
  56. bi_permanent(rsa_ctx->dP);
  57. bi_permanent(rsa_ctx->dQ);
  58. bi_permanent(rsa_ctx->qInv);
  59. bi_set_mod(bi_ctx, rsa_ctx->p, BIGINT_P_OFFSET);
  60. bi_set_mod(bi_ctx, rsa_ctx->q, BIGINT_Q_OFFSET);
  61. #endif
  62. }
  63. void RSA_pub_key_new(RSA_CTX **ctx,
  64. const uint8_t *modulus, int mod_len,
  65. const uint8_t *pub_exp, int pub_len)
  66. {
  67. RSA_CTX *rsa_ctx;
  68. BI_CTX *bi_ctx = bi_initialize();
  69. *ctx = (RSA_CTX *)calloc(1, sizeof(RSA_CTX));
  70. rsa_ctx = *ctx;
  71. rsa_ctx->bi_ctx = bi_ctx;
  72. rsa_ctx->num_octets = (mod_len & 0xFFF0);
  73. rsa_ctx->m = bi_import(bi_ctx, modulus, mod_len);
  74. bi_set_mod(bi_ctx, rsa_ctx->m, BIGINT_M_OFFSET);
  75. rsa_ctx->e = bi_import(bi_ctx, pub_exp, pub_len);
  76. bi_permanent(rsa_ctx->e);
  77. }
  78. /**
  79. * Free up any RSA context resources.
  80. */
  81. void RSA_free(RSA_CTX *rsa_ctx)
  82. {
  83. BI_CTX *bi_ctx;
  84. if (rsa_ctx == NULL) /* deal with ptrs that are null */
  85. return;
  86. bi_ctx = rsa_ctx->bi_ctx;
  87. bi_depermanent(rsa_ctx->e);
  88. bi_free(bi_ctx, rsa_ctx->e);
  89. bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);
  90. if (rsa_ctx->d)
  91. {
  92. bi_depermanent(rsa_ctx->d);
  93. bi_free(bi_ctx, rsa_ctx->d);
  94. #ifdef CONFIG_BIGINT_CRT
  95. bi_depermanent(rsa_ctx->dP);
  96. bi_depermanent(rsa_ctx->dQ);
  97. bi_depermanent(rsa_ctx->qInv);
  98. bi_free(bi_ctx, rsa_ctx->dP);
  99. bi_free(bi_ctx, rsa_ctx->dQ);
  100. bi_free(bi_ctx, rsa_ctx->qInv);
  101. bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
  102. bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
  103. #endif
  104. }
  105. bi_terminate(bi_ctx);
  106. free(rsa_ctx);
  107. }
  108. /**
  109. * @brief Use PKCS1.5 for decryption/verification.
  110. * @param ctx [in] The context
  111. * @param in_data [in] The data to encrypt (must be < modulus size-11)
  112. * @param out_data [out] The encrypted data.
  113. * @param is_decryption [in] Decryption or verify operation.
  114. * @return The number of bytes that were originally encrypted. -1 on error.
  115. * @see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
  116. */
  117. int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
  118. uint8_t *out_data, int is_decryption)
  119. {
  120. int byte_size = ctx->num_octets;
  121. uint8_t *block;
  122. int i, size;
  123. bigint *decrypted_bi, *dat_bi;
  124. memset(out_data, 0, byte_size); /* initialise */
  125. /* decrypt */
  126. dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
  127. #ifdef CONFIG_SSL_CERT_VERIFICATION
  128. decrypted_bi = is_decryption ? /* decrypt or verify? */
  129. RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi);
  130. #else /* always a decryption */
  131. decrypted_bi = RSA_private(ctx, dat_bi);
  132. #endif
  133. /* convert to a normal block */
  134. block = (uint8_t *)malloc(byte_size);
  135. bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
  136. i = 10; /* start at the first possible non-padded byte */
  137. #ifdef CONFIG_SSL_CERT_VERIFICATION
  138. if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
  139. {
  140. while (block[i++] == 0xff && i < byte_size);
  141. if (block[i-2] != 0xff)
  142. i = byte_size; /*ensure size is 0 */
  143. }
  144. else /* PKCS1.5 encryption padding is random */
  145. #endif
  146. {
  147. while (block[i++] && i < byte_size);
  148. }
  149. size = byte_size - i;
  150. /* get only the bit we want */
  151. if (size > 0)
  152. memcpy(out_data, &block[i], size);
  153. free(block);
  154. return size ? size : -1;
  155. }
  156. /**
  157. * Performs m = c^d mod n
  158. */
  159. bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg)
  160. {
  161. #ifdef CONFIG_BIGINT_CRT
  162. return bi_crt(c, bi_msg);
  163. #else
  164. BI_CTX *ctx = c->bi_ctx;
  165. ctx->mod_offset = BIGINT_M_OFFSET;
  166. return bi_mod_power(ctx, bi_msg, c->d);
  167. #endif
  168. }
  169. #ifdef CONFIG_BIGINT_CRT
  170. /**
  171. * Use the Chinese Remainder Theorem to quickly perform RSA decrypts.
  172. * This should really be in bigint.c (and was at one stage), but needs
  173. * access to the RSA_CTX context...
  174. */
  175. static bigint *bi_crt(const RSA_CTX *rsa, bigint *bi)
  176. {
  177. BI_CTX *ctx = rsa->bi_ctx;
  178. bigint *m1, *m2, *h;
  179. /* Montgomery has a condition the 0 < x, y < m and these products violate
  180. * that condition. So disable Montgomery when using CRT */
  181. #if defined(CONFIG_BIGINT_MONTGOMERY)
  182. ctx->use_classical = 1;
  183. #endif
  184. ctx->mod_offset = BIGINT_P_OFFSET;
  185. m1 = bi_mod_power(ctx, bi_copy(bi), rsa->dP);
  186. ctx->mod_offset = BIGINT_Q_OFFSET;
  187. m2 = bi_mod_power(ctx, bi, rsa->dQ);
  188. h = bi_subtract(ctx, bi_add(ctx, m1, rsa->p), bi_copy(m2), NULL);
  189. h = bi_multiply(ctx, h, rsa->qInv);
  190. ctx->mod_offset = BIGINT_P_OFFSET;
  191. h = bi_residue(ctx, h);
  192. #if defined(CONFIG_BIGINT_MONTGOMERY)
  193. ctx->use_classical = 0; /* reset for any further operation */
  194. #endif
  195. return bi_add(ctx, m2, bi_multiply(ctx, rsa->q, h));
  196. }
  197. #endif
  198. #ifdef CONFIG_SSL_FULL_MODE
  199. /**
  200. * Used for diagnostics.
  201. */
  202. void RSA_print(const RSA_CTX *rsa_ctx)
  203. {
  204. if (rsa_ctx == NULL)
  205. return;
  206. printf("----------------- RSA DEBUG ----------------\n");
  207. printf("Size:\t%d\n", rsa_ctx->num_octets);
  208. bi_print("Modulus", rsa_ctx->m);
  209. bi_print("Public Key", rsa_ctx->e);
  210. bi_print("Private Key", rsa_ctx->d);
  211. }
  212. #endif
  213. #ifdef CONFIG_SSL_CERT_VERIFICATION
  214. /**
  215. * Performs c = m^e mod n
  216. */
  217. bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg)
  218. {
  219. c->bi_ctx->mod_offset = BIGINT_M_OFFSET;
  220. return bi_mod_power(c->bi_ctx, bi_msg, c->e);
  221. }
  222. /**
  223. * Use PKCS1.5 for encryption/signing.
  224. * see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
  225. */
  226. int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  227. uint8_t *out_data, int is_signing)
  228. {
  229. int byte_size = ctx->num_octets;
  230. int num_pads_needed = byte_size-in_len-3;
  231. bigint *dat_bi, *encrypt_bi;
  232. /* note: in_len+11 must be > byte_size */
  233. out_data[0] = 0; /* ensure encryption block is < modulus */
  234. if (is_signing)
  235. {
  236. out_data[1] = 1; /* PKCS1.5 signing pads with "0xff"'s */
  237. memset(&out_data[2], 0xff, num_pads_needed);
  238. }
  239. else /* randomize the encryption padding with non-zero bytes */
  240. {
  241. out_data[1] = 2;
  242. get_random_NZ(num_pads_needed, &out_data[2]);
  243. }
  244. out_data[2+num_pads_needed] = 0;
  245. memcpy(&out_data[3+num_pads_needed], in_data, in_len);
  246. /* now encrypt it */
  247. dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size);
  248. encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) :
  249. RSA_public(ctx, dat_bi);
  250. bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
  251. return byte_size;
  252. }
  253. #if 0
  254. /**
  255. * Take a signature and decrypt it.
  256. */
  257. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  258. bigint *modulus, bigint *pub_exp)
  259. {
  260. uint8_t *block;
  261. int i, size;
  262. bigint *decrypted_bi, *dat_bi;
  263. bigint *bir = NULL;
  264. block = (uint8_t *)malloc(sig_len);
  265. /* decrypt */
  266. dat_bi = bi_import(ctx, sig, sig_len);
  267. ctx->mod_offset = BIGINT_M_OFFSET;
  268. /* convert to a normal block */
  269. decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
  270. bi_export(ctx, decrypted_bi, block, sig_len);
  271. ctx->mod_offset = BIGINT_M_OFFSET;
  272. i = 10; /* start at the first possible non-padded byte */
  273. while (block[i++] && i < sig_len);
  274. size = sig_len - i;
  275. /* get only the bit we want */
  276. if (size > 0)
  277. {
  278. int len;
  279. const uint8_t *sig_ptr = x509_get_signature(&block[i], &len);
  280. if (sig_ptr)
  281. {
  282. bir = bi_import(ctx, sig_ptr, len);
  283. }
  284. }
  285. free(block);
  286. return bir;
  287. }
  288. #endif
  289. #endif /* CONFIG_SSL_CERT_VERIFICATION */