rsa.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * RSA
  3. * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "asn1.h"
  17. #include "bignum.h"
  18. #include "rsa.h"
  19. struct crypto_rsa_key {
  20. int private_key; /* whether private key is set */
  21. struct bignum *n; /* modulus (p * q) */
  22. struct bignum *e; /* public exponent */
  23. /* The following parameters are available only if private_key is set */
  24. struct bignum *d; /* private exponent */
  25. struct bignum *p; /* prime p (factor of n) */
  26. struct bignum *q; /* prime q (factor of n) */
  27. struct bignum *dmp1; /* d mod (p - 1); CRT exponent */
  28. struct bignum *dmq1; /* d mod (q - 1); CRT exponent */
  29. struct bignum *iqmp; /* 1 / q mod p; CRT coefficient */
  30. };
  31. static const u8 * crypto_rsa_parse_integer(const u8 *pos, const u8 *end,
  32. struct bignum *num)
  33. {
  34. struct asn1_hdr hdr;
  35. if (pos == NULL)
  36. return NULL;
  37. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  38. hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
  39. wpa_printf(MSG_DEBUG, "RSA: Expected INTEGER - found class %d "
  40. "tag 0x%x", hdr.class, hdr.tag);
  41. return NULL;
  42. }
  43. if (bignum_set_unsigned_bin(num, hdr.payload, hdr.length) < 0) {
  44. wpa_printf(MSG_DEBUG, "RSA: Failed to parse INTEGER");
  45. return NULL;
  46. }
  47. return hdr.payload + hdr.length;
  48. }
  49. /**
  50. * crypto_rsa_import_public_key - Import an RSA public key
  51. * @buf: Key buffer (DER encoded RSA public key)
  52. * @len: Key buffer length in bytes
  53. * Returns: Pointer to the public key or %NULL on failure
  54. */
  55. struct crypto_rsa_key *
  56. crypto_rsa_import_public_key(const u8 *buf, size_t len)
  57. {
  58. struct crypto_rsa_key *key;
  59. struct asn1_hdr hdr;
  60. const u8 *pos, *end;
  61. key = os_zalloc(sizeof(*key));
  62. if (key == NULL)
  63. return NULL;
  64. key->n = bignum_init();
  65. key->e = bignum_init();
  66. if (key->n == NULL || key->e == NULL) {
  67. crypto_rsa_free(key);
  68. return NULL;
  69. }
  70. /*
  71. * PKCS #1, 7.1:
  72. * RSAPublicKey ::= SEQUENCE {
  73. * modulus INTEGER, -- n
  74. * publicExponent INTEGER -- e
  75. * }
  76. */
  77. if (asn1_get_next(buf, len, &hdr) < 0 ||
  78. hdr.class != ASN1_CLASS_UNIVERSAL ||
  79. hdr.tag != ASN1_TAG_SEQUENCE) {
  80. wpa_printf(MSG_DEBUG, "RSA: Expected SEQUENCE "
  81. "(public key) - found class %d tag 0x%x",
  82. hdr.class, hdr.tag);
  83. goto error;
  84. }
  85. pos = hdr.payload;
  86. end = pos + hdr.length;
  87. pos = crypto_rsa_parse_integer(pos, end, key->n);
  88. pos = crypto_rsa_parse_integer(pos, end, key->e);
  89. if (pos == NULL)
  90. goto error;
  91. if (pos != end) {
  92. wpa_hexdump(MSG_DEBUG,
  93. "RSA: Extra data in public key SEQUENCE",
  94. pos, end - pos);
  95. goto error;
  96. }
  97. return key;
  98. error:
  99. crypto_rsa_free(key);
  100. return NULL;
  101. }
  102. /**
  103. * crypto_rsa_import_private_key - Import an RSA private key
  104. * @buf: Key buffer (DER encoded RSA private key)
  105. * @len: Key buffer length in bytes
  106. * Returns: Pointer to the private key or %NULL on failure
  107. */
  108. struct crypto_rsa_key *
  109. crypto_rsa_import_private_key(const u8 *buf, size_t len)
  110. {
  111. struct crypto_rsa_key *key;
  112. struct bignum *zero;
  113. struct asn1_hdr hdr;
  114. const u8 *pos, *end;
  115. key = os_zalloc(sizeof(*key));
  116. if (key == NULL)
  117. return NULL;
  118. key->private_key = 1;
  119. key->n = bignum_init();
  120. key->e = bignum_init();
  121. key->d = bignum_init();
  122. key->p = bignum_init();
  123. key->q = bignum_init();
  124. key->dmp1 = bignum_init();
  125. key->dmq1 = bignum_init();
  126. key->iqmp = bignum_init();
  127. if (key->n == NULL || key->e == NULL || key->d == NULL ||
  128. key->p == NULL || key->q == NULL || key->dmp1 == NULL ||
  129. key->dmq1 == NULL || key->iqmp == NULL) {
  130. crypto_rsa_free(key);
  131. return NULL;
  132. }
  133. /*
  134. * PKCS #1, 7.2:
  135. * RSAPrivateKey ::= SEQUENCE {
  136. * version Version,
  137. * modulus INTEGER, -- n
  138. * publicExponent INTEGER, -- e
  139. * privateExponent INTEGER, -- d
  140. * prime1 INTEGER, -- p
  141. * prime2 INTEGER, -- q
  142. * exponent1 INTEGER, -- d mod (p-1)
  143. * exponent2 INTEGER, -- d mod (q-1)
  144. * coefficient INTEGER -- (inverse of q) mod p
  145. * }
  146. *
  147. * Version ::= INTEGER -- shall be 0 for this version of the standard
  148. */
  149. if (asn1_get_next(buf, len, &hdr) < 0 ||
  150. hdr.class != ASN1_CLASS_UNIVERSAL ||
  151. hdr.tag != ASN1_TAG_SEQUENCE) {
  152. wpa_printf(MSG_DEBUG, "RSA: Expected SEQUENCE "
  153. "(public key) - found class %d tag 0x%x",
  154. hdr.class, hdr.tag);
  155. goto error;
  156. }
  157. pos = hdr.payload;
  158. end = pos + hdr.length;
  159. zero = bignum_init();
  160. if (zero == NULL)
  161. goto error;
  162. pos = crypto_rsa_parse_integer(pos, end, zero);
  163. if (pos == NULL || bignum_cmp_d(zero, 0) != 0) {
  164. wpa_printf(MSG_DEBUG, "RSA: Expected zero INTEGER in the "
  165. "beginning of private key; not found");
  166. bignum_deinit(zero);
  167. goto error;
  168. }
  169. bignum_deinit(zero);
  170. pos = crypto_rsa_parse_integer(pos, end, key->n);
  171. pos = crypto_rsa_parse_integer(pos, end, key->e);
  172. pos = crypto_rsa_parse_integer(pos, end, key->d);
  173. pos = crypto_rsa_parse_integer(pos, end, key->p);
  174. pos = crypto_rsa_parse_integer(pos, end, key->q);
  175. pos = crypto_rsa_parse_integer(pos, end, key->dmp1);
  176. pos = crypto_rsa_parse_integer(pos, end, key->dmq1);
  177. pos = crypto_rsa_parse_integer(pos, end, key->iqmp);
  178. if (pos == NULL)
  179. goto error;
  180. if (pos != end) {
  181. wpa_hexdump(MSG_DEBUG,
  182. "RSA: Extra data in public key SEQUENCE",
  183. pos, end - pos);
  184. goto error;
  185. }
  186. return key;
  187. error:
  188. crypto_rsa_free(key);
  189. return NULL;
  190. }
  191. /**
  192. * crypto_rsa_get_modulus_len - Get the modulus length of the RSA key
  193. * @key: RSA key
  194. * Returns: Modulus length of the key
  195. */
  196. size_t crypto_rsa_get_modulus_len(struct crypto_rsa_key *key)
  197. {
  198. return bignum_get_unsigned_bin_len(key->n);
  199. }
  200. /**
  201. * crypto_rsa_exptmod - RSA modular exponentiation
  202. * @in: Input data
  203. * @inlen: Input data length
  204. * @out: Buffer for output data
  205. * @outlen: Maximum size of the output buffer and used size on success
  206. * @key: RSA key
  207. * @use_private: 1 = Use RSA private key, 0 = Use RSA public key
  208. * Returns: 0 on success, -1 on failure
  209. */
  210. int crypto_rsa_exptmod(const u8 *in, size_t inlen, u8 *out, size_t *outlen,
  211. struct crypto_rsa_key *key, int use_private)
  212. {
  213. struct bignum *tmp, *a = NULL, *b = NULL;
  214. int ret = -1;
  215. size_t modlen;
  216. if (use_private && !key->private_key)
  217. return -1;
  218. tmp = bignum_init();
  219. if (tmp == NULL)
  220. return -1;
  221. if (bignum_set_unsigned_bin(tmp, in, inlen) < 0)
  222. goto error;
  223. if (bignum_cmp(key->n, tmp) < 0) {
  224. /* Too large input value for the RSA key modulus */
  225. goto error;
  226. }
  227. if (use_private) {
  228. /*
  229. * Decrypt (or sign) using Chinese remainer theorem to speed
  230. * up calculation. This is equivalent to tmp = tmp^d mod n
  231. * (which would require more CPU to calculate directly).
  232. *
  233. * dmp1 = (1/e) mod (p-1)
  234. * dmq1 = (1/e) mod (q-1)
  235. * iqmp = (1/q) mod p, where p > q
  236. * m1 = c^dmp1 mod p
  237. * m2 = c^dmq1 mod q
  238. * h = q^-1 (m1 - m2) mod p
  239. * m = m2 + hq
  240. */
  241. a = bignum_init();
  242. b = bignum_init();
  243. if (a == NULL || b == NULL)
  244. goto error;
  245. /* a = tmp^dmp1 mod p */
  246. if (bignum_exptmod(tmp, key->dmp1, key->p, a) < 0)
  247. goto error;
  248. /* b = tmp^dmq1 mod q */
  249. if (bignum_exptmod(tmp, key->dmq1, key->q, b) < 0)
  250. goto error;
  251. /* tmp = (a - b) * (1/q mod p) (mod p) */
  252. if (bignum_sub(a, b, tmp) < 0 ||
  253. bignum_mulmod(tmp, key->iqmp, key->p, tmp) < 0)
  254. goto error;
  255. /* tmp = b + q * tmp */
  256. if (bignum_mul(tmp, key->q, tmp) < 0 ||
  257. bignum_add(tmp, b, tmp) < 0)
  258. goto error;
  259. } else {
  260. /* Encrypt (or verify signature) */
  261. /* tmp = tmp^e mod N */
  262. if (bignum_exptmod(tmp, key->e, key->n, tmp) < 0)
  263. goto error;
  264. }
  265. modlen = crypto_rsa_get_modulus_len(key);
  266. if (modlen > *outlen) {
  267. *outlen = modlen;
  268. goto error;
  269. }
  270. if (bignum_get_unsigned_bin_len(tmp) > modlen)
  271. goto error; /* should never happen */
  272. *outlen = modlen;
  273. os_memset(out, 0, modlen);
  274. if (bignum_get_unsigned_bin(
  275. tmp, out +
  276. (modlen - bignum_get_unsigned_bin_len(tmp)), NULL) < 0)
  277. goto error;
  278. ret = 0;
  279. error:
  280. bignum_deinit(tmp);
  281. bignum_deinit(a);
  282. bignum_deinit(b);
  283. return ret;
  284. }
  285. /**
  286. * crypto_rsa_free - Free RSA key
  287. * @key: RSA key to be freed
  288. *
  289. * This function frees an RSA key imported with either
  290. * crypto_rsa_import_public_key() or crypto_rsa_import_private_key().
  291. */
  292. void crypto_rsa_free(struct crypto_rsa_key *key)
  293. {
  294. if (key) {
  295. bignum_deinit(key->n);
  296. bignum_deinit(key->e);
  297. bignum_deinit(key->d);
  298. bignum_deinit(key->p);
  299. bignum_deinit(key->q);
  300. bignum_deinit(key->dmp1);
  301. bignum_deinit(key->dmq1);
  302. bignum_deinit(key->iqmp);
  303. os_free(key);
  304. }
  305. }