139beb93cSSam Leffler /*
239beb93cSSam Leffler * RSA
35b9c547cSRui Paulo * Copyright (c) 2006-2014, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
739beb93cSSam Leffler */
839beb93cSSam Leffler
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler
1139beb93cSSam Leffler #include "common.h"
1239beb93cSSam Leffler #include "asn1.h"
1339beb93cSSam Leffler #include "bignum.h"
1439beb93cSSam Leffler #include "rsa.h"
1539beb93cSSam Leffler
1639beb93cSSam Leffler
1739beb93cSSam Leffler struct crypto_rsa_key {
1839beb93cSSam Leffler int private_key; /* whether private key is set */
1939beb93cSSam Leffler struct bignum *n; /* modulus (p * q) */
2039beb93cSSam Leffler struct bignum *e; /* public exponent */
2139beb93cSSam Leffler /* The following parameters are available only if private_key is set */
2239beb93cSSam Leffler struct bignum *d; /* private exponent */
2339beb93cSSam Leffler struct bignum *p; /* prime p (factor of n) */
2439beb93cSSam Leffler struct bignum *q; /* prime q (factor of n) */
2539beb93cSSam Leffler struct bignum *dmp1; /* d mod (p - 1); CRT exponent */
2639beb93cSSam Leffler struct bignum *dmq1; /* d mod (q - 1); CRT exponent */
2739beb93cSSam Leffler struct bignum *iqmp; /* 1 / q mod p; CRT coefficient */
2839beb93cSSam Leffler };
2939beb93cSSam Leffler
3039beb93cSSam Leffler
crypto_rsa_parse_integer(const u8 * pos,const u8 * end,struct bignum * num)3139beb93cSSam Leffler static const u8 * crypto_rsa_parse_integer(const u8 *pos, const u8 *end,
3239beb93cSSam Leffler struct bignum *num)
3339beb93cSSam Leffler {
3439beb93cSSam Leffler struct asn1_hdr hdr;
3539beb93cSSam Leffler
3639beb93cSSam Leffler if (pos == NULL)
3739beb93cSSam Leffler return NULL;
3839beb93cSSam Leffler
3939beb93cSSam Leffler if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
40*c1d255d3SCy Schubert !asn1_is_integer(&hdr)) {
41*c1d255d3SCy Schubert asn1_unexpected(&hdr, "RSA: Expected INTEGER");
4239beb93cSSam Leffler return NULL;
4339beb93cSSam Leffler }
4439beb93cSSam Leffler
4539beb93cSSam Leffler if (bignum_set_unsigned_bin(num, hdr.payload, hdr.length) < 0) {
4639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "RSA: Failed to parse INTEGER");
4739beb93cSSam Leffler return NULL;
4839beb93cSSam Leffler }
4939beb93cSSam Leffler
5039beb93cSSam Leffler return hdr.payload + hdr.length;
5139beb93cSSam Leffler }
5239beb93cSSam Leffler
5339beb93cSSam Leffler
5439beb93cSSam Leffler /**
5539beb93cSSam Leffler * crypto_rsa_import_public_key - Import an RSA public key
5639beb93cSSam Leffler * @buf: Key buffer (DER encoded RSA public key)
5739beb93cSSam Leffler * @len: Key buffer length in bytes
5839beb93cSSam Leffler * Returns: Pointer to the public key or %NULL on failure
5939beb93cSSam Leffler */
6039beb93cSSam Leffler struct crypto_rsa_key *
crypto_rsa_import_public_key(const u8 * buf,size_t len)6139beb93cSSam Leffler crypto_rsa_import_public_key(const u8 *buf, size_t len)
6239beb93cSSam Leffler {
6339beb93cSSam Leffler struct crypto_rsa_key *key;
6439beb93cSSam Leffler struct asn1_hdr hdr;
6539beb93cSSam Leffler const u8 *pos, *end;
6639beb93cSSam Leffler
6739beb93cSSam Leffler key = os_zalloc(sizeof(*key));
6839beb93cSSam Leffler if (key == NULL)
6939beb93cSSam Leffler return NULL;
7039beb93cSSam Leffler
7139beb93cSSam Leffler key->n = bignum_init();
7239beb93cSSam Leffler key->e = bignum_init();
7339beb93cSSam Leffler if (key->n == NULL || key->e == NULL) {
7439beb93cSSam Leffler crypto_rsa_free(key);
7539beb93cSSam Leffler return NULL;
7639beb93cSSam Leffler }
7739beb93cSSam Leffler
7839beb93cSSam Leffler /*
7939beb93cSSam Leffler * PKCS #1, 7.1:
8039beb93cSSam Leffler * RSAPublicKey ::= SEQUENCE {
8139beb93cSSam Leffler * modulus INTEGER, -- n
8239beb93cSSam Leffler * publicExponent INTEGER -- e
8339beb93cSSam Leffler * }
8439beb93cSSam Leffler */
8539beb93cSSam Leffler
86*c1d255d3SCy Schubert if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
87*c1d255d3SCy Schubert asn1_unexpected(&hdr, "RSA: Expected SEQUENCE (public key)");
8839beb93cSSam Leffler goto error;
8939beb93cSSam Leffler }
9039beb93cSSam Leffler pos = hdr.payload;
9139beb93cSSam Leffler end = pos + hdr.length;
9239beb93cSSam Leffler
9339beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->n);
9439beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->e);
9539beb93cSSam Leffler
9639beb93cSSam Leffler if (pos == NULL)
9739beb93cSSam Leffler goto error;
9839beb93cSSam Leffler
9939beb93cSSam Leffler if (pos != end) {
10039beb93cSSam Leffler wpa_hexdump(MSG_DEBUG,
10139beb93cSSam Leffler "RSA: Extra data in public key SEQUENCE",
10239beb93cSSam Leffler pos, end - pos);
10339beb93cSSam Leffler goto error;
10439beb93cSSam Leffler }
10539beb93cSSam Leffler
10639beb93cSSam Leffler return key;
10739beb93cSSam Leffler
10839beb93cSSam Leffler error:
10939beb93cSSam Leffler crypto_rsa_free(key);
11039beb93cSSam Leffler return NULL;
11139beb93cSSam Leffler }
11239beb93cSSam Leffler
11339beb93cSSam Leffler
1145b9c547cSRui Paulo struct crypto_rsa_key *
crypto_rsa_import_public_key_parts(const u8 * n,size_t n_len,const u8 * e,size_t e_len)1155b9c547cSRui Paulo crypto_rsa_import_public_key_parts(const u8 *n, size_t n_len,
1165b9c547cSRui Paulo const u8 *e, size_t e_len)
1175b9c547cSRui Paulo {
1185b9c547cSRui Paulo struct crypto_rsa_key *key;
1195b9c547cSRui Paulo
1205b9c547cSRui Paulo key = os_zalloc(sizeof(*key));
1215b9c547cSRui Paulo if (key == NULL)
1225b9c547cSRui Paulo return NULL;
1235b9c547cSRui Paulo
1245b9c547cSRui Paulo key->n = bignum_init();
1255b9c547cSRui Paulo key->e = bignum_init();
1265b9c547cSRui Paulo if (key->n == NULL || key->e == NULL ||
1275b9c547cSRui Paulo bignum_set_unsigned_bin(key->n, n, n_len) < 0 ||
1285b9c547cSRui Paulo bignum_set_unsigned_bin(key->e, e, e_len) < 0) {
1295b9c547cSRui Paulo crypto_rsa_free(key);
1305b9c547cSRui Paulo return NULL;
1315b9c547cSRui Paulo }
1325b9c547cSRui Paulo
1335b9c547cSRui Paulo return key;
1345b9c547cSRui Paulo }
1355b9c547cSRui Paulo
1365b9c547cSRui Paulo
13739beb93cSSam Leffler /**
13839beb93cSSam Leffler * crypto_rsa_import_private_key - Import an RSA private key
13939beb93cSSam Leffler * @buf: Key buffer (DER encoded RSA private key)
14039beb93cSSam Leffler * @len: Key buffer length in bytes
14139beb93cSSam Leffler * Returns: Pointer to the private key or %NULL on failure
14239beb93cSSam Leffler */
14339beb93cSSam Leffler struct crypto_rsa_key *
crypto_rsa_import_private_key(const u8 * buf,size_t len)14439beb93cSSam Leffler crypto_rsa_import_private_key(const u8 *buf, size_t len)
14539beb93cSSam Leffler {
14639beb93cSSam Leffler struct crypto_rsa_key *key;
14739beb93cSSam Leffler struct bignum *zero;
14839beb93cSSam Leffler struct asn1_hdr hdr;
14939beb93cSSam Leffler const u8 *pos, *end;
15039beb93cSSam Leffler
15139beb93cSSam Leffler key = os_zalloc(sizeof(*key));
15239beb93cSSam Leffler if (key == NULL)
15339beb93cSSam Leffler return NULL;
15439beb93cSSam Leffler
15539beb93cSSam Leffler key->private_key = 1;
15639beb93cSSam Leffler
15739beb93cSSam Leffler key->n = bignum_init();
15839beb93cSSam Leffler key->e = bignum_init();
15939beb93cSSam Leffler key->d = bignum_init();
16039beb93cSSam Leffler key->p = bignum_init();
16139beb93cSSam Leffler key->q = bignum_init();
16239beb93cSSam Leffler key->dmp1 = bignum_init();
16339beb93cSSam Leffler key->dmq1 = bignum_init();
16439beb93cSSam Leffler key->iqmp = bignum_init();
16539beb93cSSam Leffler
16639beb93cSSam Leffler if (key->n == NULL || key->e == NULL || key->d == NULL ||
16739beb93cSSam Leffler key->p == NULL || key->q == NULL || key->dmp1 == NULL ||
16839beb93cSSam Leffler key->dmq1 == NULL || key->iqmp == NULL) {
16939beb93cSSam Leffler crypto_rsa_free(key);
17039beb93cSSam Leffler return NULL;
17139beb93cSSam Leffler }
17239beb93cSSam Leffler
17339beb93cSSam Leffler /*
17439beb93cSSam Leffler * PKCS #1, 7.2:
17539beb93cSSam Leffler * RSAPrivateKey ::= SEQUENCE {
17639beb93cSSam Leffler * version Version,
17739beb93cSSam Leffler * modulus INTEGER, -- n
17839beb93cSSam Leffler * publicExponent INTEGER, -- e
17939beb93cSSam Leffler * privateExponent INTEGER, -- d
18039beb93cSSam Leffler * prime1 INTEGER, -- p
18139beb93cSSam Leffler * prime2 INTEGER, -- q
18239beb93cSSam Leffler * exponent1 INTEGER, -- d mod (p-1)
18339beb93cSSam Leffler * exponent2 INTEGER, -- d mod (q-1)
18439beb93cSSam Leffler * coefficient INTEGER -- (inverse of q) mod p
18539beb93cSSam Leffler * }
18639beb93cSSam Leffler *
18739beb93cSSam Leffler * Version ::= INTEGER -- shall be 0 for this version of the standard
18839beb93cSSam Leffler */
189*c1d255d3SCy Schubert if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
190*c1d255d3SCy Schubert asn1_unexpected(&hdr, "RSA: Expected SEQUENCE (public key)");
19139beb93cSSam Leffler goto error;
19239beb93cSSam Leffler }
19339beb93cSSam Leffler pos = hdr.payload;
19439beb93cSSam Leffler end = pos + hdr.length;
19539beb93cSSam Leffler
19639beb93cSSam Leffler zero = bignum_init();
19739beb93cSSam Leffler if (zero == NULL)
19839beb93cSSam Leffler goto error;
19939beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, zero);
20039beb93cSSam Leffler if (pos == NULL || bignum_cmp_d(zero, 0) != 0) {
20139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "RSA: Expected zero INTEGER in the "
20239beb93cSSam Leffler "beginning of private key; not found");
20339beb93cSSam Leffler bignum_deinit(zero);
20439beb93cSSam Leffler goto error;
20539beb93cSSam Leffler }
20639beb93cSSam Leffler bignum_deinit(zero);
20739beb93cSSam Leffler
20839beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->n);
20939beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->e);
21039beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->d);
21139beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->p);
21239beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->q);
21339beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->dmp1);
21439beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->dmq1);
21539beb93cSSam Leffler pos = crypto_rsa_parse_integer(pos, end, key->iqmp);
21639beb93cSSam Leffler
21739beb93cSSam Leffler if (pos == NULL)
21839beb93cSSam Leffler goto error;
21939beb93cSSam Leffler
22039beb93cSSam Leffler if (pos != end) {
22139beb93cSSam Leffler wpa_hexdump(MSG_DEBUG,
22239beb93cSSam Leffler "RSA: Extra data in public key SEQUENCE",
22339beb93cSSam Leffler pos, end - pos);
22439beb93cSSam Leffler goto error;
22539beb93cSSam Leffler }
22639beb93cSSam Leffler
22739beb93cSSam Leffler return key;
22839beb93cSSam Leffler
22939beb93cSSam Leffler error:
23039beb93cSSam Leffler crypto_rsa_free(key);
23139beb93cSSam Leffler return NULL;
23239beb93cSSam Leffler }
23339beb93cSSam Leffler
23439beb93cSSam Leffler
23539beb93cSSam Leffler /**
23639beb93cSSam Leffler * crypto_rsa_get_modulus_len - Get the modulus length of the RSA key
23739beb93cSSam Leffler * @key: RSA key
23839beb93cSSam Leffler * Returns: Modulus length of the key
23939beb93cSSam Leffler */
crypto_rsa_get_modulus_len(struct crypto_rsa_key * key)24039beb93cSSam Leffler size_t crypto_rsa_get_modulus_len(struct crypto_rsa_key *key)
24139beb93cSSam Leffler {
24239beb93cSSam Leffler return bignum_get_unsigned_bin_len(key->n);
24339beb93cSSam Leffler }
24439beb93cSSam Leffler
24539beb93cSSam Leffler
24639beb93cSSam Leffler /**
24739beb93cSSam Leffler * crypto_rsa_exptmod - RSA modular exponentiation
24839beb93cSSam Leffler * @in: Input data
24939beb93cSSam Leffler * @inlen: Input data length
25039beb93cSSam Leffler * @out: Buffer for output data
25139beb93cSSam Leffler * @outlen: Maximum size of the output buffer and used size on success
25239beb93cSSam Leffler * @key: RSA key
25339beb93cSSam Leffler * @use_private: 1 = Use RSA private key, 0 = Use RSA public key
25439beb93cSSam Leffler * Returns: 0 on success, -1 on failure
25539beb93cSSam Leffler */
crypto_rsa_exptmod(const u8 * in,size_t inlen,u8 * out,size_t * outlen,struct crypto_rsa_key * key,int use_private)25639beb93cSSam Leffler int crypto_rsa_exptmod(const u8 *in, size_t inlen, u8 *out, size_t *outlen,
25739beb93cSSam Leffler struct crypto_rsa_key *key, int use_private)
25839beb93cSSam Leffler {
25939beb93cSSam Leffler struct bignum *tmp, *a = NULL, *b = NULL;
26039beb93cSSam Leffler int ret = -1;
26139beb93cSSam Leffler size_t modlen;
26239beb93cSSam Leffler
26339beb93cSSam Leffler if (use_private && !key->private_key)
26439beb93cSSam Leffler return -1;
26539beb93cSSam Leffler
26639beb93cSSam Leffler tmp = bignum_init();
26739beb93cSSam Leffler if (tmp == NULL)
26839beb93cSSam Leffler return -1;
26939beb93cSSam Leffler
27039beb93cSSam Leffler if (bignum_set_unsigned_bin(tmp, in, inlen) < 0)
27139beb93cSSam Leffler goto error;
27239beb93cSSam Leffler if (bignum_cmp(key->n, tmp) < 0) {
27339beb93cSSam Leffler /* Too large input value for the RSA key modulus */
27439beb93cSSam Leffler goto error;
27539beb93cSSam Leffler }
27639beb93cSSam Leffler
27739beb93cSSam Leffler if (use_private) {
27839beb93cSSam Leffler /*
279*c1d255d3SCy Schubert * Decrypt (or sign) using Chinese remainder theorem to speed
28039beb93cSSam Leffler * up calculation. This is equivalent to tmp = tmp^d mod n
28139beb93cSSam Leffler * (which would require more CPU to calculate directly).
28239beb93cSSam Leffler *
28339beb93cSSam Leffler * dmp1 = (1/e) mod (p-1)
28439beb93cSSam Leffler * dmq1 = (1/e) mod (q-1)
28539beb93cSSam Leffler * iqmp = (1/q) mod p, where p > q
28639beb93cSSam Leffler * m1 = c^dmp1 mod p
28739beb93cSSam Leffler * m2 = c^dmq1 mod q
28839beb93cSSam Leffler * h = q^-1 (m1 - m2) mod p
28939beb93cSSam Leffler * m = m2 + hq
29039beb93cSSam Leffler */
29139beb93cSSam Leffler a = bignum_init();
29239beb93cSSam Leffler b = bignum_init();
29339beb93cSSam Leffler if (a == NULL || b == NULL)
29439beb93cSSam Leffler goto error;
29539beb93cSSam Leffler
29639beb93cSSam Leffler /* a = tmp^dmp1 mod p */
29739beb93cSSam Leffler if (bignum_exptmod(tmp, key->dmp1, key->p, a) < 0)
29839beb93cSSam Leffler goto error;
29939beb93cSSam Leffler
30039beb93cSSam Leffler /* b = tmp^dmq1 mod q */
30139beb93cSSam Leffler if (bignum_exptmod(tmp, key->dmq1, key->q, b) < 0)
30239beb93cSSam Leffler goto error;
30339beb93cSSam Leffler
30439beb93cSSam Leffler /* tmp = (a - b) * (1/q mod p) (mod p) */
30539beb93cSSam Leffler if (bignum_sub(a, b, tmp) < 0 ||
30639beb93cSSam Leffler bignum_mulmod(tmp, key->iqmp, key->p, tmp) < 0)
30739beb93cSSam Leffler goto error;
30839beb93cSSam Leffler
30939beb93cSSam Leffler /* tmp = b + q * tmp */
31039beb93cSSam Leffler if (bignum_mul(tmp, key->q, tmp) < 0 ||
31139beb93cSSam Leffler bignum_add(tmp, b, tmp) < 0)
31239beb93cSSam Leffler goto error;
31339beb93cSSam Leffler } else {
31439beb93cSSam Leffler /* Encrypt (or verify signature) */
31539beb93cSSam Leffler /* tmp = tmp^e mod N */
31639beb93cSSam Leffler if (bignum_exptmod(tmp, key->e, key->n, tmp) < 0)
31739beb93cSSam Leffler goto error;
31839beb93cSSam Leffler }
31939beb93cSSam Leffler
32039beb93cSSam Leffler modlen = crypto_rsa_get_modulus_len(key);
32139beb93cSSam Leffler if (modlen > *outlen) {
32239beb93cSSam Leffler *outlen = modlen;
32339beb93cSSam Leffler goto error;
32439beb93cSSam Leffler }
32539beb93cSSam Leffler
32639beb93cSSam Leffler if (bignum_get_unsigned_bin_len(tmp) > modlen)
32739beb93cSSam Leffler goto error; /* should never happen */
32839beb93cSSam Leffler
32939beb93cSSam Leffler *outlen = modlen;
33039beb93cSSam Leffler os_memset(out, 0, modlen);
33139beb93cSSam Leffler if (bignum_get_unsigned_bin(
33239beb93cSSam Leffler tmp, out +
33339beb93cSSam Leffler (modlen - bignum_get_unsigned_bin_len(tmp)), NULL) < 0)
33439beb93cSSam Leffler goto error;
33539beb93cSSam Leffler
33639beb93cSSam Leffler ret = 0;
33739beb93cSSam Leffler
33839beb93cSSam Leffler error:
33939beb93cSSam Leffler bignum_deinit(tmp);
34039beb93cSSam Leffler bignum_deinit(a);
34139beb93cSSam Leffler bignum_deinit(b);
34239beb93cSSam Leffler return ret;
34339beb93cSSam Leffler }
34439beb93cSSam Leffler
34539beb93cSSam Leffler
34639beb93cSSam Leffler /**
34739beb93cSSam Leffler * crypto_rsa_free - Free RSA key
34839beb93cSSam Leffler * @key: RSA key to be freed
34939beb93cSSam Leffler *
35039beb93cSSam Leffler * This function frees an RSA key imported with either
35139beb93cSSam Leffler * crypto_rsa_import_public_key() or crypto_rsa_import_private_key().
35239beb93cSSam Leffler */
crypto_rsa_free(struct crypto_rsa_key * key)35339beb93cSSam Leffler void crypto_rsa_free(struct crypto_rsa_key *key)
35439beb93cSSam Leffler {
35539beb93cSSam Leffler if (key) {
35639beb93cSSam Leffler bignum_deinit(key->n);
35739beb93cSSam Leffler bignum_deinit(key->e);
35839beb93cSSam Leffler bignum_deinit(key->d);
35939beb93cSSam Leffler bignum_deinit(key->p);
36039beb93cSSam Leffler bignum_deinit(key->q);
36139beb93cSSam Leffler bignum_deinit(key->dmp1);
36239beb93cSSam Leffler bignum_deinit(key->dmq1);
36339beb93cSSam Leffler bignum_deinit(key->iqmp);
36439beb93cSSam Leffler os_free(key);
36539beb93cSSam Leffler }
36639beb93cSSam Leffler }
367