xref: /freebsd/contrib/wpa/src/crypto/crypto_internal-rsa.c (revision f05cddf940dbfc5b657f5e9beb9de2c31e509e5b)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * Crypto wrapper for internal crypto implementation - RSA parts
3e28a4053SRui Paulo  * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  *
5*f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6*f05cddf9SRui Paulo  * See README for more details.
7e28a4053SRui Paulo  */
8e28a4053SRui Paulo 
9e28a4053SRui Paulo #include "includes.h"
10e28a4053SRui Paulo 
11e28a4053SRui Paulo #include "common.h"
12e28a4053SRui Paulo #include "crypto.h"
13e28a4053SRui Paulo #include "tls/rsa.h"
14e28a4053SRui Paulo #include "tls/pkcs1.h"
15e28a4053SRui Paulo #include "tls/pkcs8.h"
16e28a4053SRui Paulo 
17e28a4053SRui Paulo /* Dummy structures; these are just typecast to struct crypto_rsa_key */
18e28a4053SRui Paulo struct crypto_public_key;
19e28a4053SRui Paulo struct crypto_private_key;
20e28a4053SRui Paulo 
21e28a4053SRui Paulo 
22e28a4053SRui Paulo struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
23e28a4053SRui Paulo {
24e28a4053SRui Paulo 	return (struct crypto_public_key *)
25e28a4053SRui Paulo 		crypto_rsa_import_public_key(key, len);
26e28a4053SRui Paulo }
27e28a4053SRui Paulo 
28e28a4053SRui Paulo 
29e28a4053SRui Paulo struct crypto_private_key * crypto_private_key_import(const u8 *key,
30e28a4053SRui Paulo 						      size_t len,
31e28a4053SRui Paulo 						      const char *passwd)
32e28a4053SRui Paulo {
33e28a4053SRui Paulo 	struct crypto_private_key *res;
34e28a4053SRui Paulo 
35e28a4053SRui Paulo 	/* First, check for possible PKCS #8 encoding */
36e28a4053SRui Paulo 	res = pkcs8_key_import(key, len);
37e28a4053SRui Paulo 	if (res)
38e28a4053SRui Paulo 		return res;
39e28a4053SRui Paulo 
40e28a4053SRui Paulo 	if (passwd) {
41e28a4053SRui Paulo 		/* Try to parse as encrypted PKCS #8 */
42e28a4053SRui Paulo 		res = pkcs8_enc_key_import(key, len, passwd);
43e28a4053SRui Paulo 		if (res)
44e28a4053SRui Paulo 			return res;
45e28a4053SRui Paulo 	}
46e28a4053SRui Paulo 
47e28a4053SRui Paulo 	/* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
48e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
49e28a4053SRui Paulo 		   "key");
50e28a4053SRui Paulo 	return (struct crypto_private_key *)
51e28a4053SRui Paulo 		crypto_rsa_import_private_key(key, len);
52e28a4053SRui Paulo }
53e28a4053SRui Paulo 
54e28a4053SRui Paulo 
55e28a4053SRui Paulo struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
56e28a4053SRui Paulo 						       size_t len)
57e28a4053SRui Paulo {
58e28a4053SRui Paulo 	/* No X.509 support in crypto_internal.c */
59e28a4053SRui Paulo 	return NULL;
60e28a4053SRui Paulo }
61e28a4053SRui Paulo 
62e28a4053SRui Paulo 
63e28a4053SRui Paulo int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
64e28a4053SRui Paulo 					const u8 *in, size_t inlen,
65e28a4053SRui Paulo 					u8 *out, size_t *outlen)
66e28a4053SRui Paulo {
67e28a4053SRui Paulo 	return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
68e28a4053SRui Paulo 			     0, in, inlen, out, outlen);
69e28a4053SRui Paulo }
70e28a4053SRui Paulo 
71e28a4053SRui Paulo 
72e28a4053SRui Paulo int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
73e28a4053SRui Paulo 					 const u8 *in, size_t inlen,
74e28a4053SRui Paulo 					 u8 *out, size_t *outlen)
75e28a4053SRui Paulo {
76e28a4053SRui Paulo 	return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
77e28a4053SRui Paulo 					     in, inlen, out, outlen);
78e28a4053SRui Paulo }
79e28a4053SRui Paulo 
80e28a4053SRui Paulo 
81e28a4053SRui Paulo int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
82e28a4053SRui Paulo 				  const u8 *in, size_t inlen,
83e28a4053SRui Paulo 				  u8 *out, size_t *outlen)
84e28a4053SRui Paulo {
85e28a4053SRui Paulo 	return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
86e28a4053SRui Paulo 			     1, in, inlen, out, outlen);
87e28a4053SRui Paulo }
88e28a4053SRui Paulo 
89e28a4053SRui Paulo 
90e28a4053SRui Paulo void crypto_public_key_free(struct crypto_public_key *key)
91e28a4053SRui Paulo {
92e28a4053SRui Paulo 	crypto_rsa_free((struct crypto_rsa_key *) key);
93e28a4053SRui Paulo }
94e28a4053SRui Paulo 
95e28a4053SRui Paulo 
96e28a4053SRui Paulo void crypto_private_key_free(struct crypto_private_key *key)
97e28a4053SRui Paulo {
98e28a4053SRui Paulo 	crypto_rsa_free((struct crypto_rsa_key *) key);
99e28a4053SRui Paulo }
100e28a4053SRui Paulo 
101e28a4053SRui Paulo 
102e28a4053SRui Paulo int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
103e28a4053SRui Paulo 				    const u8 *crypt, size_t crypt_len,
104e28a4053SRui Paulo 				    u8 *plain, size_t *plain_len)
105e28a4053SRui Paulo {
106e28a4053SRui Paulo 	return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
107e28a4053SRui Paulo 					crypt, crypt_len, plain, plain_len);
108e28a4053SRui Paulo }
109