crypto_internal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * WPA Supplicant / Crypto wrapper for internal crypto implementation
  3. * Copyright (c) 2006-2007, 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 "crypto.h"
  17. #include "md5.h"
  18. #include "sha1.h"
  19. #include "rc4.h"
  20. #include "aes.h"
  21. #include "tls/rsa.h"
  22. #include "tls/bignum.h"
  23. #include "tls/asn1.h"
  24. #ifdef EAP_TLS_FUNCS
  25. #ifdef CONFIG_TLS_INTERNAL
  26. /* from des.c */
  27. struct des3_key_s {
  28. u32 ek[3][32];
  29. u32 dk[3][32];
  30. };
  31. void des3_key_setup(const u8 *key, struct des3_key_s *dkey);
  32. void des3_encrypt(const u8 *plain, const struct des3_key_s *key, u8 *crypt);
  33. void des3_decrypt(const u8 *crypt, const struct des3_key_s *key, u8 *plain);
  34. struct MD5Context {
  35. u32 buf[4];
  36. u32 bits[2];
  37. u8 in[64];
  38. };
  39. struct SHA1Context {
  40. u32 state[5];
  41. u32 count[2];
  42. unsigned char buffer[64];
  43. };
  44. struct crypto_hash {
  45. enum crypto_hash_alg alg;
  46. union {
  47. struct MD5Context md5;
  48. struct SHA1Context sha1;
  49. } u;
  50. u8 key[64];
  51. size_t key_len;
  52. };
  53. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  54. size_t key_len)
  55. {
  56. struct crypto_hash *ctx;
  57. u8 k_pad[64];
  58. u8 tk[20];
  59. size_t i;
  60. ctx = os_zalloc(sizeof(*ctx));
  61. if (ctx == NULL)
  62. return NULL;
  63. ctx->alg = alg;
  64. switch (alg) {
  65. case CRYPTO_HASH_ALG_MD5:
  66. MD5Init(&ctx->u.md5);
  67. break;
  68. case CRYPTO_HASH_ALG_SHA1:
  69. SHA1Init(&ctx->u.sha1);
  70. break;
  71. case CRYPTO_HASH_ALG_HMAC_MD5:
  72. if (key_len > sizeof(k_pad)) {
  73. MD5Init(&ctx->u.md5);
  74. MD5Update(&ctx->u.md5, key, key_len);
  75. MD5Final(tk, &ctx->u.md5);
  76. key = tk;
  77. key_len = 16;
  78. }
  79. os_memcpy(ctx->key, key, key_len);
  80. ctx->key_len = key_len;
  81. os_memcpy(k_pad, key, key_len);
  82. os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
  83. for (i = 0; i < sizeof(k_pad); i++)
  84. k_pad[i] ^= 0x36;
  85. MD5Init(&ctx->u.md5);
  86. MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
  87. break;
  88. case CRYPTO_HASH_ALG_HMAC_SHA1:
  89. if (key_len > sizeof(k_pad)) {
  90. SHA1Init(&ctx->u.sha1);
  91. SHA1Update(&ctx->u.sha1, key, key_len);
  92. SHA1Final(tk, &ctx->u.sha1);
  93. key = tk;
  94. key_len = 20;
  95. }
  96. os_memcpy(ctx->key, key, key_len);
  97. ctx->key_len = key_len;
  98. os_memcpy(k_pad, key, key_len);
  99. os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
  100. for (i = 0; i < sizeof(k_pad); i++)
  101. k_pad[i] ^= 0x36;
  102. SHA1Init(&ctx->u.sha1);
  103. SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
  104. break;
  105. default:
  106. os_free(ctx);
  107. return NULL;
  108. }
  109. return ctx;
  110. }
  111. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  112. {
  113. if (ctx == NULL)
  114. return;
  115. switch (ctx->alg) {
  116. case CRYPTO_HASH_ALG_MD5:
  117. case CRYPTO_HASH_ALG_HMAC_MD5:
  118. MD5Update(&ctx->u.md5, data, len);
  119. break;
  120. case CRYPTO_HASH_ALG_SHA1:
  121. case CRYPTO_HASH_ALG_HMAC_SHA1:
  122. SHA1Update(&ctx->u.sha1, data, len);
  123. break;
  124. }
  125. }
  126. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  127. {
  128. u8 k_pad[64];
  129. size_t i;
  130. if (ctx == NULL)
  131. return -2;
  132. if (mac == NULL || len == NULL) {
  133. os_free(ctx);
  134. return 0;
  135. }
  136. switch (ctx->alg) {
  137. case CRYPTO_HASH_ALG_MD5:
  138. if (*len < 16) {
  139. *len = 16;
  140. os_free(ctx);
  141. return -1;
  142. }
  143. *len = 16;
  144. MD5Final(mac, &ctx->u.md5);
  145. break;
  146. case CRYPTO_HASH_ALG_SHA1:
  147. if (*len < 20) {
  148. *len = 20;
  149. os_free(ctx);
  150. return -1;
  151. }
  152. *len = 20;
  153. SHA1Final(mac, &ctx->u.sha1);
  154. break;
  155. case CRYPTO_HASH_ALG_HMAC_MD5:
  156. if (*len < 16) {
  157. *len = 16;
  158. os_free(ctx);
  159. return -1;
  160. }
  161. *len = 16;
  162. MD5Final(mac, &ctx->u.md5);
  163. os_memcpy(k_pad, ctx->key, ctx->key_len);
  164. os_memset(k_pad + ctx->key_len, 0,
  165. sizeof(k_pad) - ctx->key_len);
  166. for (i = 0; i < sizeof(k_pad); i++)
  167. k_pad[i] ^= 0x5c;
  168. MD5Init(&ctx->u.md5);
  169. MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
  170. MD5Update(&ctx->u.md5, mac, 16);
  171. MD5Final(mac, &ctx->u.md5);
  172. break;
  173. case CRYPTO_HASH_ALG_HMAC_SHA1:
  174. if (*len < 20) {
  175. *len = 20;
  176. os_free(ctx);
  177. return -1;
  178. }
  179. *len = 20;
  180. SHA1Final(mac, &ctx->u.sha1);
  181. os_memcpy(k_pad, ctx->key, ctx->key_len);
  182. os_memset(k_pad + ctx->key_len, 0,
  183. sizeof(k_pad) - ctx->key_len);
  184. for (i = 0; i < sizeof(k_pad); i++)
  185. k_pad[i] ^= 0x5c;
  186. SHA1Init(&ctx->u.sha1);
  187. SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
  188. SHA1Update(&ctx->u.sha1, mac, 20);
  189. SHA1Final(mac, &ctx->u.sha1);
  190. break;
  191. }
  192. os_free(ctx);
  193. return 0;
  194. }
  195. struct crypto_cipher {
  196. enum crypto_cipher_alg alg;
  197. union {
  198. struct {
  199. size_t used_bytes;
  200. u8 key[16];
  201. size_t keylen;
  202. } rc4;
  203. struct {
  204. u8 cbc[32];
  205. size_t block_size;
  206. void *ctx_enc;
  207. void *ctx_dec;
  208. } aes;
  209. struct {
  210. struct des3_key_s key;
  211. u8 cbc[8];
  212. } des3;
  213. } u;
  214. };
  215. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  216. const u8 *iv, const u8 *key,
  217. size_t key_len)
  218. {
  219. struct crypto_cipher *ctx;
  220. ctx = os_zalloc(sizeof(*ctx));
  221. if (ctx == NULL)
  222. return NULL;
  223. ctx->alg = alg;
  224. switch (alg) {
  225. case CRYPTO_CIPHER_ALG_RC4:
  226. if (key_len > sizeof(ctx->u.rc4.key)) {
  227. os_free(ctx);
  228. return NULL;
  229. }
  230. ctx->u.rc4.keylen = key_len;
  231. os_memcpy(ctx->u.rc4.key, key, key_len);
  232. break;
  233. case CRYPTO_CIPHER_ALG_AES:
  234. if (key_len > sizeof(ctx->u.aes.cbc)) {
  235. os_free(ctx);
  236. return NULL;
  237. }
  238. ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
  239. if (ctx->u.aes.ctx_enc == NULL) {
  240. os_free(ctx);
  241. return NULL;
  242. }
  243. ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
  244. if (ctx->u.aes.ctx_dec == NULL) {
  245. aes_encrypt_deinit(ctx->u.aes.ctx_enc);
  246. os_free(ctx);
  247. return NULL;
  248. }
  249. ctx->u.aes.block_size = key_len;
  250. os_memcpy(ctx->u.aes.cbc, iv, ctx->u.aes.block_size);
  251. break;
  252. case CRYPTO_CIPHER_ALG_3DES:
  253. if (key_len != 24) {
  254. os_free(ctx);
  255. return NULL;
  256. }
  257. des3_key_setup(key, &ctx->u.des3.key);
  258. os_memcpy(ctx->u.des3.cbc, iv, 8);
  259. break;
  260. default:
  261. os_free(ctx);
  262. return NULL;
  263. }
  264. return ctx;
  265. }
  266. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  267. u8 *crypt, size_t len)
  268. {
  269. size_t i, j, blocks;
  270. switch (ctx->alg) {
  271. case CRYPTO_CIPHER_ALG_RC4:
  272. if (plain != crypt)
  273. os_memcpy(crypt, plain, len);
  274. rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
  275. ctx->u.rc4.used_bytes, crypt, len);
  276. ctx->u.rc4.used_bytes += len;
  277. break;
  278. case CRYPTO_CIPHER_ALG_AES:
  279. if (len % ctx->u.aes.block_size)
  280. return -1;
  281. blocks = len / ctx->u.aes.block_size;
  282. for (i = 0; i < blocks; i++) {
  283. for (j = 0; j < ctx->u.aes.block_size; j++)
  284. ctx->u.aes.cbc[j] ^= plain[j];
  285. aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
  286. ctx->u.aes.cbc);
  287. os_memcpy(crypt, ctx->u.aes.cbc,
  288. ctx->u.aes.block_size);
  289. plain += ctx->u.aes.block_size;
  290. crypt += ctx->u.aes.block_size;
  291. }
  292. break;
  293. case CRYPTO_CIPHER_ALG_3DES:
  294. if (len % 8)
  295. return -1;
  296. blocks = len / 8;
  297. for (i = 0; i < blocks; i++) {
  298. for (j = 0; j < 8; j++)
  299. ctx->u.des3.cbc[j] ^= plain[j];
  300. des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
  301. ctx->u.des3.cbc);
  302. os_memcpy(crypt, ctx->u.des3.cbc, 8);
  303. plain += 8;
  304. crypt += 8;
  305. }
  306. break;
  307. default:
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  313. u8 *plain, size_t len)
  314. {
  315. size_t i, j, blocks;
  316. u8 tmp[32];
  317. switch (ctx->alg) {
  318. case CRYPTO_CIPHER_ALG_RC4:
  319. if (plain != crypt)
  320. os_memcpy(plain, crypt, len);
  321. rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
  322. ctx->u.rc4.used_bytes, plain, len);
  323. ctx->u.rc4.used_bytes += len;
  324. break;
  325. case CRYPTO_CIPHER_ALG_AES:
  326. if (len % ctx->u.aes.block_size)
  327. return -1;
  328. blocks = len / ctx->u.aes.block_size;
  329. for (i = 0; i < blocks; i++) {
  330. os_memcpy(tmp, crypt, ctx->u.aes.block_size);
  331. aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
  332. for (j = 0; j < ctx->u.aes.block_size; j++)
  333. plain[j] ^= ctx->u.aes.cbc[j];
  334. os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
  335. plain += ctx->u.aes.block_size;
  336. crypt += ctx->u.aes.block_size;
  337. }
  338. break;
  339. case CRYPTO_CIPHER_ALG_3DES:
  340. if (len % 8)
  341. return -1;
  342. blocks = len / 8;
  343. for (i = 0; i < blocks; i++) {
  344. os_memcpy(tmp, crypt, 8);
  345. des3_decrypt(crypt, &ctx->u.des3.key, plain);
  346. for (j = 0; j < 8; j++)
  347. plain[j] ^= ctx->u.des3.cbc[j];
  348. os_memcpy(ctx->u.des3.cbc, tmp, 8);
  349. plain += 8;
  350. crypt += 8;
  351. }
  352. break;
  353. default:
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  359. {
  360. switch (ctx->alg) {
  361. case CRYPTO_CIPHER_ALG_AES:
  362. aes_encrypt_deinit(ctx->u.aes.ctx_enc);
  363. aes_decrypt_deinit(ctx->u.aes.ctx_dec);
  364. break;
  365. case CRYPTO_CIPHER_ALG_3DES:
  366. break;
  367. default:
  368. break;
  369. }
  370. os_free(ctx);
  371. }
  372. /* Dummy structures; these are just typecast to struct crypto_rsa_key */
  373. struct crypto_public_key;
  374. struct crypto_private_key;
  375. struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
  376. {
  377. return (struct crypto_public_key *)
  378. crypto_rsa_import_public_key(key, len);
  379. }
  380. static struct crypto_private_key *
  381. crypto_pkcs8_key_import(const u8 *buf, size_t len)
  382. {
  383. struct asn1_hdr hdr;
  384. const u8 *pos, *end;
  385. struct bignum *zero;
  386. struct asn1_oid oid;
  387. char obuf[80];
  388. /* PKCS #8, Chapter 6 */
  389. /* PrivateKeyInfo ::= SEQUENCE */
  390. if (asn1_get_next(buf, len, &hdr) < 0 ||
  391. hdr.class != ASN1_CLASS_UNIVERSAL ||
  392. hdr.tag != ASN1_TAG_SEQUENCE) {
  393. wpa_printf(MSG_DEBUG, "PKCS #8: Does not start with PKCS #8 "
  394. "header (SEQUENCE); assume PKCS #8 not used");
  395. return NULL;
  396. }
  397. pos = hdr.payload;
  398. end = pos + hdr.length;
  399. /* version Version (Version ::= INTEGER) */
  400. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  401. hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
  402. wpa_printf(MSG_DEBUG, "PKCS #8: Expected INTEGER - found "
  403. "class %d tag 0x%x; assume PKCS #8 not used",
  404. hdr.class, hdr.tag);
  405. return NULL;
  406. }
  407. zero = bignum_init();
  408. if (zero == NULL)
  409. return NULL;
  410. if (bignum_set_unsigned_bin(zero, hdr.payload, hdr.length) < 0) {
  411. wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse INTEGER");
  412. bignum_deinit(zero);
  413. return NULL;
  414. }
  415. pos = hdr.payload + hdr.length;
  416. if (bignum_cmp_d(zero, 0) != 0) {
  417. wpa_printf(MSG_DEBUG, "PKCS #8: Expected zero INTEGER in the "
  418. "beginning of private key; not found; assume "
  419. "PKCS #8 not used");
  420. bignum_deinit(zero);
  421. return NULL;
  422. }
  423. bignum_deinit(zero);
  424. /* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier
  425. * (PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier) */
  426. if (asn1_get_next(pos, len, &hdr) < 0 ||
  427. hdr.class != ASN1_CLASS_UNIVERSAL ||
  428. hdr.tag != ASN1_TAG_SEQUENCE) {
  429. wpa_printf(MSG_DEBUG, "PKCS #8: Expected SEQUENCE "
  430. "(AlgorithmIdentifier) - found class %d tag 0x%x; "
  431. "assume PKCS #8 not used",
  432. hdr.class, hdr.tag);
  433. return NULL;
  434. }
  435. if (asn1_get_oid(hdr.payload, hdr.length, &oid, &pos)) {
  436. wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse OID "
  437. "(algorithm); assume PKCS #8 not used");
  438. return NULL;
  439. }
  440. asn1_oid_to_str(&oid, obuf, sizeof(obuf));
  441. wpa_printf(MSG_DEBUG, "PKCS #8: algorithm=%s", obuf);
  442. if (oid.len != 7 ||
  443. oid.oid[0] != 1 /* iso */ ||
  444. oid.oid[1] != 2 /* member-body */ ||
  445. oid.oid[2] != 840 /* us */ ||
  446. oid.oid[3] != 113549 /* rsadsi */ ||
  447. oid.oid[4] != 1 /* pkcs */ ||
  448. oid.oid[5] != 1 /* pkcs-1 */ ||
  449. oid.oid[6] != 1 /* rsaEncryption */) {
  450. wpa_printf(MSG_DEBUG, "PKCS #8: Unsupported private key "
  451. "algorithm %s", obuf);
  452. return NULL;
  453. }
  454. pos = hdr.payload + hdr.length;
  455. /* privateKey PrivateKey (PrivateKey ::= OCTET STRING) */
  456. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  457. hdr.class != ASN1_CLASS_UNIVERSAL ||
  458. hdr.tag != ASN1_TAG_OCTETSTRING) {
  459. wpa_printf(MSG_DEBUG, "PKCS #8: Expected OCTETSTRING "
  460. "(privateKey) - found class %d tag 0x%x",
  461. hdr.class, hdr.tag);
  462. return NULL;
  463. }
  464. wpa_printf(MSG_DEBUG, "PKCS #8: Try to parse RSAPrivateKey");
  465. return (struct crypto_private_key *)
  466. crypto_rsa_import_private_key(hdr.payload, hdr.length);
  467. }
  468. struct crypto_private_key * crypto_private_key_import(const u8 *key,
  469. size_t len)
  470. {
  471. struct crypto_private_key *res;
  472. /* First, check for possible PKCS #8 encoding */
  473. res = crypto_pkcs8_key_import(key, len);
  474. if (res)
  475. return res;
  476. /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
  477. wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
  478. "key");
  479. return (struct crypto_private_key *)
  480. crypto_rsa_import_private_key(key, len);
  481. }
  482. struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
  483. size_t len)
  484. {
  485. /* No X.509 support in crypto_internal.c */
  486. return NULL;
  487. }
  488. static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
  489. const u8 *in, size_t inlen,
  490. u8 *out, size_t *outlen)
  491. {
  492. size_t ps_len;
  493. u8 *pos;
  494. /*
  495. * PKCS #1 v1.5, 8.1:
  496. *
  497. * EB = 00 || BT || PS || 00 || D
  498. * BT = 00 or 01 for private-key operation; 02 for public-key operation
  499. * PS = k-3-||D||; at least eight octets
  500. * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
  501. * k = length of modulus in octets (modlen)
  502. */
  503. if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
  504. wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
  505. "lengths (modlen=%lu outlen=%lu inlen=%lu)",
  506. __func__, (unsigned long) modlen,
  507. (unsigned long) *outlen,
  508. (unsigned long) inlen);
  509. return -1;
  510. }
  511. pos = out;
  512. *pos++ = 0x00;
  513. *pos++ = block_type; /* BT */
  514. ps_len = modlen - inlen - 3;
  515. switch (block_type) {
  516. case 0:
  517. os_memset(pos, 0x00, ps_len);
  518. pos += ps_len;
  519. break;
  520. case 1:
  521. os_memset(pos, 0xff, ps_len);
  522. pos += ps_len;
  523. break;
  524. case 2:
  525. if (os_get_random(pos, ps_len) < 0) {
  526. wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
  527. "random data for PS", __func__);
  528. return -1;
  529. }
  530. while (ps_len--) {
  531. if (*pos == 0x00)
  532. *pos = 0x01;
  533. pos++;
  534. }
  535. break;
  536. default:
  537. wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
  538. "%d", __func__, block_type);
  539. return -1;
  540. }
  541. *pos++ = 0x00;
  542. os_memcpy(pos, in, inlen); /* D */
  543. return 0;
  544. }
  545. static int crypto_rsa_encrypt_pkcs1(int block_type, struct crypto_rsa_key *key,
  546. int use_private,
  547. const u8 *in, size_t inlen,
  548. u8 *out, size_t *outlen)
  549. {
  550. size_t modlen;
  551. modlen = crypto_rsa_get_modulus_len(key);
  552. if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
  553. out, outlen) < 0)
  554. return -1;
  555. return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
  556. }
  557. int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
  558. const u8 *in, size_t inlen,
  559. u8 *out, size_t *outlen)
  560. {
  561. return crypto_rsa_encrypt_pkcs1(2, (struct crypto_rsa_key *) key,
  562. 0, in, inlen, out, outlen);
  563. }
  564. int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
  565. const u8 *in, size_t inlen,
  566. u8 *out, size_t *outlen)
  567. {
  568. struct crypto_rsa_key *rkey = (struct crypto_rsa_key *) key;
  569. int res;
  570. u8 *pos, *end;
  571. res = crypto_rsa_exptmod(in, inlen, out, outlen, rkey, 1);
  572. if (res)
  573. return res;
  574. if (*outlen < 2 || out[0] != 0 || out[1] != 2)
  575. return -1;
  576. /* Skip PS (pseudorandom non-zero octets) */
  577. pos = out + 2;
  578. end = out + *outlen;
  579. while (*pos && pos < end)
  580. pos++;
  581. if (pos == end)
  582. return -1;
  583. pos++;
  584. *outlen -= pos - out;
  585. /* Strip PKCS #1 header */
  586. os_memmove(out, pos, *outlen);
  587. return 0;
  588. }
  589. int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
  590. const u8 *in, size_t inlen,
  591. u8 *out, size_t *outlen)
  592. {
  593. return crypto_rsa_encrypt_pkcs1(1, (struct crypto_rsa_key *) key,
  594. 1, in, inlen, out, outlen);
  595. }
  596. void crypto_public_key_free(struct crypto_public_key *key)
  597. {
  598. crypto_rsa_free((struct crypto_rsa_key *) key);
  599. }
  600. void crypto_private_key_free(struct crypto_private_key *key)
  601. {
  602. crypto_rsa_free((struct crypto_rsa_key *) key);
  603. }
  604. int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
  605. const u8 *crypt, size_t crypt_len,
  606. u8 *plain, size_t *plain_len)
  607. {
  608. size_t len;
  609. u8 *pos;
  610. len = *plain_len;
  611. if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len,
  612. (struct crypto_rsa_key *) key, 0) < 0)
  613. return -1;
  614. /*
  615. * PKCS #1 v1.5, 8.1:
  616. *
  617. * EB = 00 || BT || PS || 00 || D
  618. * BT = 00 or 01
  619. * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
  620. * k = length of modulus in octets
  621. */
  622. if (len < 3 + 8 + 16 /* min hash len */ ||
  623. plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
  624. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
  625. "structure");
  626. return -1;
  627. }
  628. pos = plain + 3;
  629. if (plain[1] == 0x00) {
  630. /* BT = 00 */
  631. if (plain[2] != 0x00) {
  632. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
  633. "PS (BT=00)");
  634. return -1;
  635. }
  636. while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
  637. pos++;
  638. } else {
  639. /* BT = 01 */
  640. if (plain[2] != 0xff) {
  641. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
  642. "PS (BT=01)");
  643. return -1;
  644. }
  645. while (pos < plain + len && *pos == 0xff)
  646. pos++;
  647. }
  648. if (pos - plain - 2 < 8) {
  649. /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
  650. wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
  651. "padding");
  652. return -1;
  653. }
  654. if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
  655. wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
  656. "structure (2)");
  657. return -1;
  658. }
  659. pos++;
  660. len -= pos - plain;
  661. /* Strip PKCS #1 header */
  662. os_memmove(plain, pos, len);
  663. *plain_len = len;
  664. return 0;
  665. }
  666. int crypto_global_init(void)
  667. {
  668. return 0;
  669. }
  670. void crypto_global_deinit(void)
  671. {
  672. }
  673. #ifdef EAP_FAST
  674. int crypto_mod_exp(const u8 *base, size_t base_len,
  675. const u8 *power, size_t power_len,
  676. const u8 *modulus, size_t modulus_len,
  677. u8 *result, size_t *result_len)
  678. {
  679. struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
  680. int ret = -1;
  681. bn_base = bignum_init();
  682. bn_exp = bignum_init();
  683. bn_modulus = bignum_init();
  684. bn_result = bignum_init();
  685. if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
  686. bn_result == NULL)
  687. goto error;
  688. if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
  689. bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
  690. bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
  691. goto error;
  692. if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
  693. goto error;
  694. ret = bignum_get_unsigned_bin(bn_result, result, result_len);
  695. error:
  696. bignum_deinit(bn_base);
  697. bignum_deinit(bn_exp);
  698. bignum_deinit(bn_modulus);
  699. bignum_deinit(bn_result);
  700. return ret;
  701. }
  702. #endif /* EAP_FAST */
  703. #endif /* CONFIG_TLS_INTERNAL */
  704. #endif /* EAP_TLS_FUNCS */