rsa.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2007-2014, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * Implements the RSA public encryption algorithm. Uses the bigint library to
  32. * perform its calculations.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include <stdlib.h>
  38. #include "os_port.h"
  39. #include "crypto.h"
  40. void RSA_priv_key_new(RSA_CTX **ctx,
  41. const uint8_t *modulus, int mod_len,
  42. const uint8_t *pub_exp, int pub_len,
  43. const uint8_t *priv_exp, int priv_len
  44. #if CONFIG_BIGINT_CRT
  45. , const uint8_t *p, int p_len,
  46. const uint8_t *q, int q_len,
  47. const uint8_t *dP, int dP_len,
  48. const uint8_t *dQ, int dQ_len,
  49. const uint8_t *qInv, int qInv_len
  50. #endif
  51. )
  52. {
  53. RSA_CTX *rsa_ctx;
  54. BI_CTX *bi_ctx;
  55. RSA_pub_key_new(ctx, modulus, mod_len, pub_exp, pub_len);
  56. rsa_ctx = *ctx;
  57. bi_ctx = rsa_ctx->bi_ctx;
  58. rsa_ctx->d = bi_import(bi_ctx, priv_exp, priv_len);
  59. bi_permanent(rsa_ctx->d);
  60. #ifdef CONFIG_BIGINT_CRT
  61. rsa_ctx->p = bi_import(bi_ctx, p, p_len);
  62. rsa_ctx->q = bi_import(bi_ctx, q, q_len);
  63. rsa_ctx->dP = bi_import(bi_ctx, dP, dP_len);
  64. rsa_ctx->dQ = bi_import(bi_ctx, dQ, dQ_len);
  65. rsa_ctx->qInv = bi_import(bi_ctx, qInv, qInv_len);
  66. bi_permanent(rsa_ctx->dP);
  67. bi_permanent(rsa_ctx->dQ);
  68. bi_permanent(rsa_ctx->qInv);
  69. bi_set_mod(bi_ctx, rsa_ctx->p, BIGINT_P_OFFSET);
  70. bi_set_mod(bi_ctx, rsa_ctx->q, BIGINT_Q_OFFSET);
  71. #endif
  72. }
  73. void RSA_pub_key_new(RSA_CTX **ctx,
  74. const uint8_t *modulus, int mod_len,
  75. const uint8_t *pub_exp, int pub_len)
  76. {
  77. RSA_CTX *rsa_ctx;
  78. BI_CTX *bi_ctx;
  79. if (*ctx) /* if we load multiple certs, dump the old one */
  80. RSA_free(*ctx);
  81. bi_ctx = bi_initialize();
  82. *ctx = (RSA_CTX *)calloc(1, sizeof(RSA_CTX));
  83. rsa_ctx = *ctx;
  84. rsa_ctx->bi_ctx = bi_ctx;
  85. rsa_ctx->num_octets = mod_len;
  86. rsa_ctx->m = bi_import(bi_ctx, modulus, mod_len);
  87. bi_set_mod(bi_ctx, rsa_ctx->m, BIGINT_M_OFFSET);
  88. rsa_ctx->e = bi_import(bi_ctx, pub_exp, pub_len);
  89. bi_permanent(rsa_ctx->e);
  90. }
  91. /**
  92. * Free up any RSA context resources.
  93. */
  94. void RSA_free(RSA_CTX *rsa_ctx)
  95. {
  96. BI_CTX *bi_ctx;
  97. if (rsa_ctx == NULL) /* deal with ptrs that are null */
  98. return;
  99. bi_ctx = rsa_ctx->bi_ctx;
  100. bi_depermanent(rsa_ctx->e);
  101. bi_free(bi_ctx, rsa_ctx->e);
  102. bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);
  103. if (rsa_ctx->d)
  104. {
  105. bi_depermanent(rsa_ctx->d);
  106. bi_free(bi_ctx, rsa_ctx->d);
  107. #ifdef CONFIG_BIGINT_CRT
  108. bi_depermanent(rsa_ctx->dP);
  109. bi_depermanent(rsa_ctx->dQ);
  110. bi_depermanent(rsa_ctx->qInv);
  111. bi_free(bi_ctx, rsa_ctx->dP);
  112. bi_free(bi_ctx, rsa_ctx->dQ);
  113. bi_free(bi_ctx, rsa_ctx->qInv);
  114. bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
  115. bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
  116. #endif
  117. }
  118. bi_terminate(bi_ctx);
  119. free(rsa_ctx);
  120. }
  121. /**
  122. * @brief Use PKCS1.5 for decryption/verification.
  123. * @param ctx [in] The context
  124. * @param in_data [in] The data to decrypt (must be < modulus size-11)
  125. * @param out_data [out] The decrypted data.
  126. * @param out_len [int] The size of the decrypted buffer in bytes
  127. * @param is_decryption [in] Decryption or verify operation.
  128. * @return The number of bytes that were originally encrypted. -1 on error.
  129. * @see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
  130. */
  131. int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
  132. uint8_t *out_data, int out_len, int is_decryption)
  133. {
  134. const int byte_size = ctx->num_octets;
  135. int i = 0, size;
  136. bigint *decrypted_bi, *dat_bi;
  137. uint8_t *block = (uint8_t *)alloca(byte_size);
  138. int pad_count = 0;
  139. if (out_len < byte_size) /* check output has enough size */
  140. return -1;
  141. memset(out_data, 0, out_len); /* initialise */
  142. /* decrypt */
  143. dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
  144. #ifdef CONFIG_SSL_CERT_VERIFICATION
  145. decrypted_bi = is_decryption ? /* decrypt or verify? */
  146. RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi);
  147. #else /* always a decryption */
  148. decrypted_bi = RSA_private(ctx, dat_bi);
  149. #endif
  150. /* convert to a normal block */
  151. bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
  152. if (block[i++] != 0) /* leading 0? */
  153. return -1;
  154. #ifdef CONFIG_SSL_CERT_VERIFICATION
  155. if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
  156. {
  157. if (block[i++] != 0x01) /* BT correct? */
  158. return -1;
  159. while (block[i++] == 0xff && i < byte_size)
  160. pad_count++;
  161. }
  162. else /* PKCS1.5 encryption padding is random */
  163. #endif
  164. {
  165. if (block[i++] != 0x02) /* BT correct? */
  166. return -1;
  167. while (block[i++] && i < byte_size)
  168. pad_count++;
  169. }
  170. /* check separator byte 0x00 - and padding must be 8 or more bytes */
  171. if (i == byte_size || pad_count < 8)
  172. return -1;
  173. size = byte_size - i;
  174. /* get only the bit we want */
  175. memcpy(out_data, &block[i], size);
  176. return size;
  177. }
  178. /**
  179. * Performs m = c^d mod n
  180. */
  181. bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg)
  182. {
  183. #ifdef CONFIG_BIGINT_CRT
  184. return bi_crt(c->bi_ctx, bi_msg, c->dP, c->dQ, c->p, c->q, c->qInv);
  185. #else
  186. BI_CTX *ctx = c->bi_ctx;
  187. ctx->mod_offset = BIGINT_M_OFFSET;
  188. return bi_mod_power(ctx, bi_msg, c->d);
  189. #endif
  190. }
  191. #ifdef CONFIG_SSL_FULL_MODE
  192. /**
  193. * Used for diagnostics.
  194. */
  195. void RSA_print(const RSA_CTX *rsa_ctx)
  196. {
  197. if (rsa_ctx == NULL)
  198. return;
  199. printf("----------------- RSA DEBUG ----------------\n");
  200. printf("Size:\t%d\n", rsa_ctx->num_octets);
  201. bi_print("Modulus", rsa_ctx->m);
  202. bi_print("Public Key", rsa_ctx->e);
  203. bi_print("Private Key", rsa_ctx->d);
  204. }
  205. #endif
  206. #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
  207. /**
  208. * Performs c = m^e mod n
  209. */
  210. bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg)
  211. {
  212. c->bi_ctx->mod_offset = BIGINT_M_OFFSET;
  213. return bi_mod_power(c->bi_ctx, bi_msg, c->e);
  214. }
  215. /**
  216. * Use PKCS1.5 for encryption/signing.
  217. * see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
  218. */
  219. int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
  220. uint8_t *out_data, int is_signing)
  221. {
  222. int byte_size = ctx->num_octets;
  223. int num_pads_needed = byte_size-in_len-3;
  224. bigint *dat_bi, *encrypt_bi;
  225. /* note: in_len+11 must be > byte_size */
  226. out_data[0] = 0; /* ensure encryption block is < modulus */
  227. if (is_signing)
  228. {
  229. out_data[1] = 1; /* PKCS1.5 signing pads with "0xff"'s */
  230. memset(&out_data[2], 0xff, num_pads_needed);
  231. }
  232. else /* randomize the encryption padding with non-zero bytes */
  233. {
  234. out_data[1] = 2;
  235. if (get_random_NZ(num_pads_needed, &out_data[2]) < 0)
  236. return -1;
  237. }
  238. out_data[2+num_pads_needed] = 0;
  239. memcpy(&out_data[3+num_pads_needed], in_data, in_len);
  240. /* now encrypt it */
  241. dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size);
  242. encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) :
  243. RSA_public(ctx, dat_bi);
  244. bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
  245. /* save a few bytes of memory */
  246. bi_clear_cache(ctx->bi_ctx);
  247. return byte_size;
  248. }
  249. #endif /* CONFIG_SSL_CERT_VERIFICATION */