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