You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(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)); /* reset to all 0 */
  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(RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
  118. 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. {
  143. i = byte_size; /*ensure size is 0 */
  144. }
  145. }
  146. else /* PKCS1.5 encryption padding is random */
  147. #endif
  148. {
  149. while (block[i++] && i < byte_size);
  150. }
  151. size = byte_size - i;
  152. /* get only the bit we want */
  153. if (size > 0)
  154. {
  155. memcpy(out_data, &block[i], size);
  156. }
  157. free(block);
  158. return size ? size : -1;
  159. }
  160. /**
  161. * Performs m = c^d mod n
  162. */
  163. bigint *RSA_private(RSA_CTX *c, bigint *bi_msg)
  164. {
  165. #ifdef CONFIG_BIGINT_CRT
  166. return bi_crt(c, bi_msg);
  167. #else
  168. BI_CTX *ctx = c->bi_ctx;
  169. ctx->mod_offset = BIGINT_M_OFFSET;
  170. return bi_mod_power(ctx, bi_msg, c->d);
  171. #endif
  172. }
  173. #ifdef CONFIG_BIGINT_CRT
  174. /**
  175. * Use the Chinese Remainder Theorem to quickly perform RSA decrypts.
  176. * This should really be in bigint.c (and was at one stage), but needs
  177. * access to the RSA_CTX context...
  178. */
  179. static bigint *bi_crt(RSA_CTX *rsa, bigint *bi)
  180. {
  181. BI_CTX *ctx = rsa->bi_ctx;
  182. bigint *m1, *m2, *h;
  183. /* Montgomery has a condition the 0 < x, y < m and these products violate
  184. * that condition. So disable Montgomery when using CRT */
  185. #if defined(CONFIG_BIGINT_MONTGOMERY)
  186. ctx->use_classical = 1;
  187. #endif
  188. ctx->mod_offset = BIGINT_P_OFFSET;
  189. m1 = bi_mod_power(ctx, bi_copy(bi), rsa->dP);
  190. ctx->mod_offset = BIGINT_Q_OFFSET;
  191. m2 = bi_mod_power(ctx, bi, rsa->dQ);
  192. h = bi_subtract(ctx, bi_add(ctx, m1, rsa->p), bi_copy(m2), NULL);
  193. h = bi_multiply(ctx, h, rsa->qInv);
  194. ctx->mod_offset = BIGINT_P_OFFSET;
  195. h = bi_residue(ctx, h);
  196. #if defined(CONFIG_BIGINT_MONTGOMERY)
  197. ctx->use_classical = 0; /* reset for any further operation */
  198. #endif
  199. return bi_add(ctx, m2, bi_multiply(ctx, rsa->q, h));
  200. }
  201. #endif
  202. #ifdef CONFIG_SSL_FULL_MODE
  203. /**
  204. * Used for diagnostics.
  205. */
  206. void RSA_print(const RSA_CTX *rsa_ctx)
  207. {
  208. if (rsa_ctx == NULL)
  209. return;
  210. printf("----------------- RSA DEBUG ----------------\n");
  211. printf("Size:\t%d\n", rsa_ctx->num_octets);
  212. bi_print("Modulus", rsa_ctx->m);
  213. bi_print("Public Key", rsa_ctx->e);
  214. bi_print("Private Key", rsa_ctx->d);
  215. }
  216. #endif
  217. #ifdef CONFIG_SSL_CERT_VERIFICATION
  218. /**
  219. * Performs c = m^e mod n
  220. */
  221. bigint *RSA_public(RSA_CTX *c, bigint *bi_msg)
  222. {
  223. c->bi_ctx->mod_offset = BIGINT_M_OFFSET;
  224. return bi_mod_power(c->bi_ctx, bi_msg, c->e);
  225. }
  226. /**
  227. * Use PKCS1.5 for encryption/signing.
  228. * see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
  229. */
  230. int RSA_encrypt(RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  231. uint8_t *out_data, int is_signing)
  232. {
  233. int byte_size = ctx->num_octets;
  234. int num_pads_needed = byte_size-in_len-3;
  235. bigint *dat_bi, *encrypt_bi;
  236. /* note: in_len+11 must be > byte_size */
  237. out_data[0] = 0; /* ensure encryption block is < modulus */
  238. if (is_signing)
  239. {
  240. out_data[1] = 1; /* PKCS1.5 signing pads with "0xff"'s */
  241. memset(&out_data[2], 0xff, num_pads_needed);
  242. }
  243. else /* randomize the encryption padding with non-zero bytes */
  244. {
  245. out_data[1] = 2;
  246. memset(&out_data[2], 0x01, num_pads_needed);
  247. #if 0
  248. get_random_NZ(num_pads_needed, &out_data[2]);
  249. #endif
  250. }
  251. out_data[2+num_pads_needed] = 0;
  252. memcpy(&out_data[3+num_pads_needed], in_data, in_len);
  253. /* now encrypt it */
  254. dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size);
  255. encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) :
  256. RSA_public(ctx, dat_bi);
  257. bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
  258. return byte_size;
  259. }
  260. #if 0
  261. /**
  262. * Take a signature and decrypt it.
  263. */
  264. bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
  265. bigint *modulus, bigint *pub_exp)
  266. {
  267. uint8_t *block = (uint8_t *)malloc(sig_len);
  268. int i, size;
  269. bigint *decrypted_bi, *dat_bi;
  270. bigint *bir = NULL;
  271. /* decrypt */
  272. dat_bi = bi_import(ctx, sig, sig_len);
  273. ctx->mod_offset = BIGINT_M_OFFSET;
  274. /* convert to a normal block */
  275. decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
  276. bi_export(ctx, decrypted_bi, block, sig_len);
  277. ctx->mod_offset = BIGINT_M_OFFSET;
  278. i = 10; /* start at the first possible non-padded byte */
  279. while (block[i++] && i < sig_len);
  280. size = sig_len - i;
  281. /* get only the bit we want */
  282. if (size > 0)
  283. {
  284. int len;
  285. const uint8_t *sig_ptr = x509_get_signature(&block[i], &len);
  286. if (sig_ptr)
  287. {
  288. bir = bi_import(ctx, sig_ptr, len);
  289. }
  290. }
  291. free(block);
  292. return bir;
  293. }
  294. #endif
  295. #endif /* CONFIG_SSL_CERT_VERIFICATION */