]>
git.ipfire.org Git - thirdparty/hostap.git/blob - src/crypto/crypto_internal-rsa.c
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_public_key
*
30 crypto_public_key_import_parts(const u8
*n
, size_t n_len
,
31 const u8
*e
, size_t e_len
)
33 return (struct crypto_public_key
*)
34 crypto_rsa_import_public_key_parts(n
, n_len
, e
, e_len
);
38 struct crypto_private_key
* crypto_private_key_import(const u8
*key
,
42 struct crypto_private_key
*res
;
44 /* First, check for possible PKCS #8 encoding */
45 res
= pkcs8_key_import(key
, len
);
50 /* Try to parse as encrypted PKCS #8 */
51 res
= pkcs8_enc_key_import(key
, len
, passwd
);
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 "
59 return (struct crypto_private_key
*)
60 crypto_rsa_import_private_key(key
, len
);
64 struct crypto_public_key
* crypto_public_key_from_cert(const u8
*buf
,
67 /* No X.509 support in crypto_internal.c */
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
)
76 return pkcs1_encrypt(2, (struct crypto_rsa_key
*) key
,
77 0, in
, inlen
, out
, outlen
);
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
)
85 return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key
*) key
,
86 in
, inlen
, out
, outlen
);
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
)
94 return pkcs1_encrypt(1, (struct crypto_rsa_key
*) key
,
95 1, in
, inlen
, out
, outlen
);
99 void crypto_public_key_free(struct crypto_public_key
*key
)
101 crypto_rsa_free((struct crypto_rsa_key
*) key
);
105 void crypto_private_key_free(struct crypto_private_key
*key
)
107 crypto_rsa_free((struct crypto_rsa_key
*) key
);
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
)
115 return pkcs1_decrypt_public_key((struct crypto_rsa_key
*) key
,
116 crypt
, crypt_len
, plain
, plain_len
);