PKCS #1: Enforce minimum padding for decryption in internal TLS
[mech_eap.git] / src / tls / pkcs1.c
1 /*
2  * PKCS #1 (RSA Encryption)
3  * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "rsa.h"
13 #include "pkcs1.h"
14
15
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)
19 {
20         size_t ps_len;
21         u8 *pos;
22
23         /*
24          * PKCS #1 v1.5, 8.1:
25          *
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)
31          */
32
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);
39                 return -1;
40         }
41
42         pos = out;
43         *pos++ = 0x00;
44         *pos++ = block_type; /* BT */
45         ps_len = modlen - inlen - 3;
46         switch (block_type) {
47         case 0:
48                 os_memset(pos, 0x00, ps_len);
49                 pos += ps_len;
50                 break;
51         case 1:
52                 os_memset(pos, 0xff, ps_len);
53                 pos += ps_len;
54                 break;
55         case 2:
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__);
59                         return -1;
60                 }
61                 while (ps_len--) {
62                         if (*pos == 0x00)
63                                 *pos = 0x01;
64                         pos++;
65                 }
66                 break;
67         default:
68                 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
69                            "%d", __func__, block_type);
70                 return -1;
71         }
72         *pos++ = 0x00;
73         os_memcpy(pos, in, inlen); /* D */
74
75         return 0;
76 }
77
78
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)
82 {
83         size_t modlen;
84
85         modlen = crypto_rsa_get_modulus_len(key);
86
87         if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
88                                             out, outlen) < 0)
89                 return -1;
90
91         return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
92 }
93
94
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)
98 {
99         int res;
100         u8 *pos, *end;
101
102         res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
103         if (res)
104                 return res;
105
106         if (*outlen < 2 || out[0] != 0 || out[1] != 2)
107                 return -1;
108
109         /* Skip PS (pseudorandom non-zero octets) */
110         pos = out + 2;
111         end = out + *outlen;
112         while (*pos && pos < end)
113                 pos++;
114         if (pos == end)
115                 return -1;
116         if (pos - out - 2 < 8) {
117                 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
118                 wpa_printf(MSG_INFO, "LibTomCrypt: Too short padding");
119                 return -1;
120         }
121         pos++;
122
123         *outlen -= pos - out;
124
125         /* Strip PKCS #1 header */
126         os_memmove(out, pos, *outlen);
127
128         return 0;
129 }
130
131
132 int pkcs1_decrypt_public_key(struct crypto_rsa_key *key,
133                              const u8 *crypt, size_t crypt_len,
134                              u8 *plain, size_t *plain_len)
135 {
136         size_t len;
137         u8 *pos;
138
139         len = *plain_len;
140         if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len, key, 0) < 0)
141                 return -1;
142
143         /*
144          * PKCS #1 v1.5, 8.1:
145          *
146          * EB = 00 || BT || PS || 00 || D
147          * BT = 00 or 01
148          * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
149          * k = length of modulus in octets
150          *
151          * Based on 10.1.3, "The block type shall be 01" for a signature.
152          */
153
154         if (len < 3 + 8 + 16 /* min hash len */ ||
155             plain[0] != 0x00 || plain[1] != 0x01) {
156                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
157                            "structure");
158                 return -1;
159         }
160
161         pos = plain + 3;
162         /* BT = 01 */
163         if (plain[2] != 0xff) {
164                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
165                            "PS (BT=01)");
166                 return -1;
167         }
168         while (pos < plain + len && *pos == 0xff)
169                 pos++;
170
171         if (pos - plain - 2 < 8) {
172                 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
173                 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
174                            "padding");
175                 return -1;
176         }
177
178         if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
179                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
180                            "structure (2)");
181                 return -1;
182         }
183         pos++;
184         len -= pos - plain;
185
186         /* Strip PKCS #1 header */
187         os_memmove(plain, pos, len);
188         *plain_len = len;
189
190         return 0;
191 }