2 * PKCS #1 (RSA Encryption)
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.
16 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
17 const u8 *in, size_t inlen,
18 u8 *out, size_t *outlen)
26 * EB = 00 || BT || PS || 00 || D
27 * BT = 00 or 01 for private-key operation; 02 for public-key operation
28 * PS = k-3-||D||; at least eight octets
29 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
30 * k = length of modulus in octets (modlen)
33 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
34 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
35 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
36 __func__, (unsigned long) modlen,
37 (unsigned long) *outlen,
38 (unsigned long) inlen);
44 *pos++ = block_type; /* BT */
45 ps_len = modlen - inlen - 3;
48 os_memset(pos, 0x00, ps_len);
52 os_memset(pos, 0xff, ps_len);
56 if (os_get_random(pos, ps_len) < 0) {
57 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
58 "random data for PS", __func__);
68 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
69 "%d", __func__, block_type);
73 os_memcpy(pos, in, inlen); /* D */
79 int pkcs1_encrypt(int block_type, struct crypto_rsa_key *key,
80 int use_private, const u8 *in, size_t inlen,
81 u8 *out, size_t *outlen)
85 modlen = crypto_rsa_get_modulus_len(key);
87 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
91 return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
95 int pkcs1_v15_private_key_decrypt(struct crypto_rsa_key *key,
96 const u8 *in, size_t inlen,
97 u8 *out, size_t *outlen)
102 res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
106 if (*outlen < 2 || out[0] != 0 || out[1] != 2)
109 /* Skip PS (pseudorandom non-zero octets) */
112 while (*pos && pos < end)
118 *outlen -= pos - out;
120 /* Strip PKCS #1 header */
121 os_memmove(out, pos, *outlen);
127 int pkcs1_decrypt_public_key(struct crypto_rsa_key *key,
128 const u8 *crypt, size_t crypt_len,
129 u8 *plain, size_t *plain_len)
135 if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len, key, 0) < 0)
141 * EB = 00 || BT || PS || 00 || D
143 * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
144 * k = length of modulus in octets
147 if (len < 3 + 8 + 16 /* min hash len */ ||
148 plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
149 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
155 if (plain[1] == 0x00) {
157 if (plain[2] != 0x00) {
158 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
162 while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
166 if (plain[2] != 0xff) {
167 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
171 while (pos < plain + len && *pos == 0xff)
175 if (pos - plain - 2 < 8) {
176 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
177 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
182 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
183 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
190 /* Strip PKCS #1 header */
191 os_memmove(plain, pos, len);