3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 struct crypto_rsa_key {
18 int private_key; /* whether private key is set */
19 struct bignum *n; /* modulus (p * q) */
20 struct bignum *e; /* public exponent */
21 /* The following parameters are available only if private_key is set */
22 struct bignum *d; /* private exponent */
23 struct bignum *p; /* prime p (factor of n) */
24 struct bignum *q; /* prime q (factor of n) */
25 struct bignum *dmp1; /* d mod (p - 1); CRT exponent */
26 struct bignum *dmq1; /* d mod (q - 1); CRT exponent */
27 struct bignum *iqmp; /* 1 / q mod p; CRT coefficient */
31 static const u8 * crypto_rsa_parse_integer(const u8 *pos, const u8 *end,
39 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
40 hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
41 wpa_printf(MSG_DEBUG, "RSA: Expected INTEGER - found class %d "
42 "tag 0x%x", hdr.class, hdr.tag);
46 if (bignum_set_unsigned_bin(num, hdr.payload, hdr.length) < 0) {
47 wpa_printf(MSG_DEBUG, "RSA: Failed to parse INTEGER");
51 return hdr.payload + hdr.length;
56 * crypto_rsa_import_public_key - Import an RSA public key
57 * @buf: Key buffer (DER encoded RSA public key)
58 * @len: Key buffer length in bytes
59 * Returns: Pointer to the public key or %NULL on failure
61 struct crypto_rsa_key *
62 crypto_rsa_import_public_key(const u8 *buf, size_t len)
64 struct crypto_rsa_key *key;
68 key = os_zalloc(sizeof(*key));
72 key->n = bignum_init();
73 key->e = bignum_init();
74 if (key->n == NULL || key->e == NULL) {
81 * RSAPublicKey ::= SEQUENCE {
82 * modulus INTEGER, -- n
83 * publicExponent INTEGER -- e
87 if (asn1_get_next(buf, len, &hdr) < 0 ||
88 hdr.class != ASN1_CLASS_UNIVERSAL ||
89 hdr.tag != ASN1_TAG_SEQUENCE) {
90 wpa_printf(MSG_DEBUG, "RSA: Expected SEQUENCE "
91 "(public key) - found class %d tag 0x%x",
96 end = pos + hdr.length;
98 pos = crypto_rsa_parse_integer(pos, end, key->n);
99 pos = crypto_rsa_parse_integer(pos, end, key->e);
105 wpa_hexdump(MSG_DEBUG,
106 "RSA: Extra data in public key SEQUENCE",
114 crypto_rsa_free(key);
120 * crypto_rsa_import_private_key - Import an RSA private key
121 * @buf: Key buffer (DER encoded RSA private key)
122 * @len: Key buffer length in bytes
123 * Returns: Pointer to the private key or %NULL on failure
125 struct crypto_rsa_key *
126 crypto_rsa_import_private_key(const u8 *buf, size_t len)
128 struct crypto_rsa_key *key;
133 key = os_zalloc(sizeof(*key));
137 key->private_key = 1;
139 key->n = bignum_init();
140 key->e = bignum_init();
141 key->d = bignum_init();
142 key->p = bignum_init();
143 key->q = bignum_init();
144 key->dmp1 = bignum_init();
145 key->dmq1 = bignum_init();
146 key->iqmp = bignum_init();
148 if (key->n == NULL || key->e == NULL || key->d == NULL ||
149 key->p == NULL || key->q == NULL || key->dmp1 == NULL ||
150 key->dmq1 == NULL || key->iqmp == NULL) {
151 crypto_rsa_free(key);
157 * RSAPrivateKey ::= SEQUENCE {
159 * modulus INTEGER, -- n
160 * publicExponent INTEGER, -- e
161 * privateExponent INTEGER, -- d
162 * prime1 INTEGER, -- p
163 * prime2 INTEGER, -- q
164 * exponent1 INTEGER, -- d mod (p-1)
165 * exponent2 INTEGER, -- d mod (q-1)
166 * coefficient INTEGER -- (inverse of q) mod p
169 * Version ::= INTEGER -- shall be 0 for this version of the standard
171 if (asn1_get_next(buf, len, &hdr) < 0 ||
172 hdr.class != ASN1_CLASS_UNIVERSAL ||
173 hdr.tag != ASN1_TAG_SEQUENCE) {
174 wpa_printf(MSG_DEBUG, "RSA: Expected SEQUENCE "
175 "(public key) - found class %d tag 0x%x",
180 end = pos + hdr.length;
182 zero = bignum_init();
185 pos = crypto_rsa_parse_integer(pos, end, zero);
186 if (pos == NULL || bignum_cmp_d(zero, 0) != 0) {
187 wpa_printf(MSG_DEBUG, "RSA: Expected zero INTEGER in the "
188 "beginning of private key; not found");
194 pos = crypto_rsa_parse_integer(pos, end, key->n);
195 pos = crypto_rsa_parse_integer(pos, end, key->e);
196 pos = crypto_rsa_parse_integer(pos, end, key->d);
197 pos = crypto_rsa_parse_integer(pos, end, key->p);
198 pos = crypto_rsa_parse_integer(pos, end, key->q);
199 pos = crypto_rsa_parse_integer(pos, end, key->dmp1);
200 pos = crypto_rsa_parse_integer(pos, end, key->dmq1);
201 pos = crypto_rsa_parse_integer(pos, end, key->iqmp);
207 wpa_hexdump(MSG_DEBUG,
208 "RSA: Extra data in public key SEQUENCE",
216 crypto_rsa_free(key);
222 * crypto_rsa_get_modulus_len - Get the modulus length of the RSA key
224 * Returns: Modulus length of the key
226 size_t crypto_rsa_get_modulus_len(struct crypto_rsa_key *key)
228 return bignum_get_unsigned_bin_len(key->n);
233 * crypto_rsa_exptmod - RSA modular exponentiation
235 * @inlen: Input data length
236 * @out: Buffer for output data
237 * @outlen: Maximum size of the output buffer and used size on success
239 * @use_private: 1 = Use RSA private key, 0 = Use RSA public key
240 * Returns: 0 on success, -1 on failure
242 int crypto_rsa_exptmod(const u8 *in, size_t inlen, u8 *out, size_t *outlen,
243 struct crypto_rsa_key *key, int use_private)
245 struct bignum *tmp, *a = NULL, *b = NULL;
249 if (use_private && !key->private_key)
256 if (bignum_set_unsigned_bin(tmp, in, inlen) < 0)
258 if (bignum_cmp(key->n, tmp) < 0) {
259 /* Too large input value for the RSA key modulus */
265 * Decrypt (or sign) using Chinese remainer theorem to speed
266 * up calculation. This is equivalent to tmp = tmp^d mod n
267 * (which would require more CPU to calculate directly).
269 * dmp1 = (1/e) mod (p-1)
270 * dmq1 = (1/e) mod (q-1)
271 * iqmp = (1/q) mod p, where p > q
274 * h = q^-1 (m1 - m2) mod p
279 if (a == NULL || b == NULL)
282 /* a = tmp^dmp1 mod p */
283 if (bignum_exptmod(tmp, key->dmp1, key->p, a) < 0)
286 /* b = tmp^dmq1 mod q */
287 if (bignum_exptmod(tmp, key->dmq1, key->q, b) < 0)
290 /* tmp = (a - b) * (1/q mod p) (mod p) */
291 if (bignum_sub(a, b, tmp) < 0 ||
292 bignum_mulmod(tmp, key->iqmp, key->p, tmp) < 0)
295 /* tmp = b + q * tmp */
296 if (bignum_mul(tmp, key->q, tmp) < 0 ||
297 bignum_add(tmp, b, tmp) < 0)
300 /* Encrypt (or verify signature) */
301 /* tmp = tmp^e mod N */
302 if (bignum_exptmod(tmp, key->e, key->n, tmp) < 0)
306 modlen = crypto_rsa_get_modulus_len(key);
307 if (modlen > *outlen) {
312 if (bignum_get_unsigned_bin_len(tmp) > modlen)
313 goto error; /* should never happen */
316 os_memset(out, 0, modlen);
317 if (bignum_get_unsigned_bin(
319 (modlen - bignum_get_unsigned_bin_len(tmp)), NULL) < 0)
333 * crypto_rsa_free - Free RSA key
334 * @key: RSA key to be freed
336 * This function frees an RSA key imported with either
337 * crypto_rsa_import_public_key() or crypto_rsa_import_private_key().
339 void crypto_rsa_free(struct crypto_rsa_key *key)
342 bignum_deinit(key->n);
343 bignum_deinit(key->e);
344 bignum_deinit(key->d);
345 bignum_deinit(key->p);
346 bignum_deinit(key->q);
347 bignum_deinit(key->dmp1);
348 bignum_deinit(key->dmq1);
349 bignum_deinit(key->iqmp);