2 * Crypto wrapper for internal crypto implementation - RSA parts
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "tls/pkcs1.h"
15 #include "tls/pkcs8.h"
17 /* Dummy structures; these are just typecast to struct crypto_rsa_key */
18 struct crypto_public_key;
19 struct crypto_private_key;
22 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
24 return (struct crypto_public_key *)
25 crypto_rsa_import_public_key(key, len);
29 struct crypto_private_key * crypto_private_key_import(const u8 *key,
33 struct crypto_private_key *res;
35 /* First, check for possible PKCS #8 encoding */
36 res = pkcs8_key_import(key, len);
41 /* Try to parse as encrypted PKCS #8 */
42 res = pkcs8_enc_key_import(key, len, passwd);
47 /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
48 wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
50 return (struct crypto_private_key *)
51 crypto_rsa_import_private_key(key, len);
55 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
58 /* No X.509 support in crypto_internal.c */
63 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
64 const u8 *in, size_t inlen,
65 u8 *out, size_t *outlen)
67 return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
68 0, in, inlen, out, outlen);
72 int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
73 const u8 *in, size_t inlen,
74 u8 *out, size_t *outlen)
76 return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
77 in, inlen, out, outlen);
81 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
82 const u8 *in, size_t inlen,
83 u8 *out, size_t *outlen)
85 return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
86 1, in, inlen, out, outlen);
90 void crypto_public_key_free(struct crypto_public_key *key)
92 crypto_rsa_free((struct crypto_rsa_key *) key);
96 void crypto_private_key_free(struct crypto_private_key *key)
98 crypto_rsa_free((struct crypto_rsa_key *) key);
102 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
103 const u8 *crypt, size_t crypt_len,
104 u8 *plain, size_t *plain_len)
106 return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
107 crypt, crypt_len, plain, plain_len);