2 * PKCS #1 (RSA Encryption)
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.
22 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
23 const u8 *in, size_t inlen,
24 u8 *out, size_t *outlen)
32 * EB = 00 || BT || PS || 00 || D
33 * BT = 00 or 01 for private-key operation; 02 for public-key operation
34 * PS = k-3-||D||; at least eight octets
35 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
36 * k = length of modulus in octets (modlen)
39 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
40 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
41 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
42 __func__, (unsigned long) modlen,
43 (unsigned long) *outlen,
44 (unsigned long) inlen);
50 *pos++ = block_type; /* BT */
51 ps_len = modlen - inlen - 3;
54 os_memset(pos, 0x00, ps_len);
58 os_memset(pos, 0xff, ps_len);
62 if (os_get_random(pos, ps_len) < 0) {
63 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
64 "random data for PS", __func__);
74 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
75 "%d", __func__, block_type);
79 os_memcpy(pos, in, inlen); /* D */
85 int pkcs1_encrypt(int block_type, struct crypto_rsa_key *key,
86 int use_private, const u8 *in, size_t inlen,
87 u8 *out, size_t *outlen)
91 modlen = crypto_rsa_get_modulus_len(key);
93 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
97 return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
101 int pkcs1_v15_private_key_decrypt(struct crypto_rsa_key *key,
102 const u8 *in, size_t inlen,
103 u8 *out, size_t *outlen)
108 res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
112 if (*outlen < 2 || out[0] != 0 || out[1] != 2)
115 /* Skip PS (pseudorandom non-zero octets) */
118 while (*pos && pos < end)
124 *outlen -= pos - out;
126 /* Strip PKCS #1 header */
127 os_memmove(out, pos, *outlen);
133 int pkcs1_decrypt_public_key(struct crypto_rsa_key *key,
134 const u8 *crypt, size_t crypt_len,
135 u8 *plain, size_t *plain_len)
141 if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len, key, 0) < 0)
147 * EB = 00 || BT || PS || 00 || D
149 * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
150 * k = length of modulus in octets
153 if (len < 3 + 8 + 16 /* min hash len */ ||
154 plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
155 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
161 if (plain[1] == 0x00) {
163 if (plain[2] != 0x00) {
164 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
168 while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
172 if (plain[2] != 0xff) {
173 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
177 while (pos < plain + len && *pos == 0xff)
181 if (pos - plain - 2 < 8) {
182 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
183 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
188 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
189 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
196 /* Strip PKCS #1 header */
197 os_memmove(plain, pos, len);