rsa.c 8.3 KB

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