2 * Crypto wrapper for internal crypto implementation - RSA parts
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "tls/bignum.h"
21 #include "tls/pkcs1.h"
22 #include "tls/pkcs8.h"
24 /* Dummy structures; these are just typecast to struct crypto_rsa_key */
25 struct crypto_public_key
;
26 struct crypto_private_key
;
29 struct crypto_public_key
* crypto_public_key_import(const u8
*key
, size_t len
)
31 return (struct crypto_public_key
*)
32 crypto_rsa_import_public_key(key
, len
);
36 struct crypto_private_key
* crypto_private_key_import(const u8
*key
,
40 struct crypto_private_key
*res
;
42 /* First, check for possible PKCS #8 encoding */
43 res
= pkcs8_key_import(key
, len
);
48 /* Try to parse as encrypted PKCS #8 */
49 res
= pkcs8_enc_key_import(key
, len
, passwd
);
54 /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
55 wpa_printf(MSG_DEBUG
, "Trying to parse PKCS #1 encoded RSA private "
57 return (struct crypto_private_key
*)
58 crypto_rsa_import_private_key(key
, len
);
62 struct crypto_public_key
* crypto_public_key_from_cert(const u8
*buf
,
65 /* No X.509 support in crypto_internal.c */
70 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key
*key
,
71 const u8
*in
, size_t inlen
,
72 u8
*out
, size_t *outlen
)
74 return pkcs1_encrypt(2, (struct crypto_rsa_key
*) key
,
75 0, in
, inlen
, out
, outlen
);
79 int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key
*key
,
80 const u8
*in
, size_t inlen
,
81 u8
*out
, size_t *outlen
)
83 return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key
*) key
,
84 in
, inlen
, out
, outlen
);
88 int crypto_private_key_sign_pkcs1(struct crypto_private_key
*key
,
89 const u8
*in
, size_t inlen
,
90 u8
*out
, size_t *outlen
)
92 return pkcs1_encrypt(1, (struct crypto_rsa_key
*) key
,
93 1, in
, inlen
, out
, outlen
);
97 void crypto_public_key_free(struct crypto_public_key
*key
)
99 crypto_rsa_free((struct crypto_rsa_key
*) key
);
103 void crypto_private_key_free(struct crypto_private_key
*key
)
105 crypto_rsa_free((struct crypto_rsa_key
*) key
);
109 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key
*key
,
110 const u8
*crypt
, size_t crypt_len
,
111 u8
*plain
, size_t *plain_len
)
113 return pkcs1_decrypt_public_key((struct crypto_rsa_key
*) key
,
114 crypt
, crypt_len
, plain
, plain_len
);