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