Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / crypto / crypto_internal-rsa.c
blob205042cf36311a8fd93d83d8e92c5e728ed6c8d4
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "crypto.h"
19 #include "tls/rsa.h"
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,
37 size_t len,
38 const char *passwd)
40 struct crypto_private_key *res;
42 /* First, check for possible PKCS #8 encoding */
43 res = pkcs8_key_import(key, len);
44 if (res)
45 return res;
47 if (passwd) {
48 /* Try to parse as encrypted PKCS #8 */
49 res = pkcs8_enc_key_import(key, len, passwd);
50 if (res)
51 return res;
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 "
56 "key");
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,
63 size_t len)
65 /* No X.509 support in crypto_internal.c */
66 return NULL;
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);