1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty *
4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty * the following conditions:
11*0957b409SSimon J. Gerraty *
12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty *
15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty * SOFTWARE.
23*0957b409SSimon J. Gerraty */
24*0957b409SSimon J. Gerraty
25*0957b409SSimon J. Gerraty #include "inner.h"
26*0957b409SSimon J. Gerraty
27*0957b409SSimon J. Gerraty /*
28*0957b409SSimon J. Gerraty * CCM initialisation. This does everything except setting the vtable,
29*0957b409SSimon J. Gerraty * which depends on whether this is a context for encrypting or for
30*0957b409SSimon J. Gerraty * decrypting.
31*0957b409SSimon J. Gerraty */
32*0957b409SSimon J. Gerraty static void
gen_ccm_init(br_sslrec_ccm_context * cc,const br_block_ctrcbc_class * bc_impl,const void * key,size_t key_len,const void * iv,size_t tag_len)33*0957b409SSimon J. Gerraty gen_ccm_init(br_sslrec_ccm_context *cc,
34*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *bc_impl,
35*0957b409SSimon J. Gerraty const void *key, size_t key_len,
36*0957b409SSimon J. Gerraty const void *iv, size_t tag_len)
37*0957b409SSimon J. Gerraty {
38*0957b409SSimon J. Gerraty cc->seq = 0;
39*0957b409SSimon J. Gerraty bc_impl->init(&cc->bc.vtable, key, key_len);
40*0957b409SSimon J. Gerraty memcpy(cc->iv, iv, sizeof cc->iv);
41*0957b409SSimon J. Gerraty cc->tag_len = tag_len;
42*0957b409SSimon J. Gerraty }
43*0957b409SSimon J. Gerraty
44*0957b409SSimon J. Gerraty static void
in_ccm_init(br_sslrec_ccm_context * cc,const br_block_ctrcbc_class * bc_impl,const void * key,size_t key_len,const void * iv,size_t tag_len)45*0957b409SSimon J. Gerraty in_ccm_init(br_sslrec_ccm_context *cc,
46*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *bc_impl,
47*0957b409SSimon J. Gerraty const void *key, size_t key_len,
48*0957b409SSimon J. Gerraty const void *iv, size_t tag_len)
49*0957b409SSimon J. Gerraty {
50*0957b409SSimon J. Gerraty cc->vtable.in = &br_sslrec_in_ccm_vtable;
51*0957b409SSimon J. Gerraty gen_ccm_init(cc, bc_impl, key, key_len, iv, tag_len);
52*0957b409SSimon J. Gerraty }
53*0957b409SSimon J. Gerraty
54*0957b409SSimon J. Gerraty static int
ccm_check_length(const br_sslrec_ccm_context * cc,size_t rlen)55*0957b409SSimon J. Gerraty ccm_check_length(const br_sslrec_ccm_context *cc, size_t rlen)
56*0957b409SSimon J. Gerraty {
57*0957b409SSimon J. Gerraty /*
58*0957b409SSimon J. Gerraty * CCM overhead is 8 bytes for nonce_explicit, and the tag
59*0957b409SSimon J. Gerraty * (normally 8 or 16 bytes, depending on cipher suite).
60*0957b409SSimon J. Gerraty */
61*0957b409SSimon J. Gerraty size_t over;
62*0957b409SSimon J. Gerraty
63*0957b409SSimon J. Gerraty over = 8 + cc->tag_len;
64*0957b409SSimon J. Gerraty return rlen >= over && rlen <= (16384 + over);
65*0957b409SSimon J. Gerraty }
66*0957b409SSimon J. Gerraty
67*0957b409SSimon J. Gerraty static unsigned char *
ccm_decrypt(br_sslrec_ccm_context * cc,int record_type,unsigned version,void * data,size_t * data_len)68*0957b409SSimon J. Gerraty ccm_decrypt(br_sslrec_ccm_context *cc,
69*0957b409SSimon J. Gerraty int record_type, unsigned version, void *data, size_t *data_len)
70*0957b409SSimon J. Gerraty {
71*0957b409SSimon J. Gerraty br_ccm_context zc;
72*0957b409SSimon J. Gerraty unsigned char *buf;
73*0957b409SSimon J. Gerraty unsigned char nonce[12], header[13];
74*0957b409SSimon J. Gerraty size_t len;
75*0957b409SSimon J. Gerraty
76*0957b409SSimon J. Gerraty buf = (unsigned char *)data + 8;
77*0957b409SSimon J. Gerraty len = *data_len - (8 + cc->tag_len);
78*0957b409SSimon J. Gerraty
79*0957b409SSimon J. Gerraty /*
80*0957b409SSimon J. Gerraty * Make nonce (implicit + explicit parts).
81*0957b409SSimon J. Gerraty */
82*0957b409SSimon J. Gerraty memcpy(nonce, cc->iv, sizeof cc->iv);
83*0957b409SSimon J. Gerraty memcpy(nonce + 4, data, 8);
84*0957b409SSimon J. Gerraty
85*0957b409SSimon J. Gerraty /*
86*0957b409SSimon J. Gerraty * Assemble synthetic header for the AAD.
87*0957b409SSimon J. Gerraty */
88*0957b409SSimon J. Gerraty br_enc64be(header, cc->seq ++);
89*0957b409SSimon J. Gerraty header[8] = (unsigned char)record_type;
90*0957b409SSimon J. Gerraty br_enc16be(header + 9, version);
91*0957b409SSimon J. Gerraty br_enc16be(header + 11, len);
92*0957b409SSimon J. Gerraty
93*0957b409SSimon J. Gerraty /*
94*0957b409SSimon J. Gerraty * Perform CCM decryption.
95*0957b409SSimon J. Gerraty */
96*0957b409SSimon J. Gerraty br_ccm_init(&zc, &cc->bc.vtable);
97*0957b409SSimon J. Gerraty br_ccm_reset(&zc, nonce, sizeof nonce, sizeof header, len, cc->tag_len);
98*0957b409SSimon J. Gerraty br_ccm_aad_inject(&zc, header, sizeof header);
99*0957b409SSimon J. Gerraty br_ccm_flip(&zc);
100*0957b409SSimon J. Gerraty br_ccm_run(&zc, 0, buf, len);
101*0957b409SSimon J. Gerraty if (!br_ccm_check_tag(&zc, buf + len)) {
102*0957b409SSimon J. Gerraty return NULL;
103*0957b409SSimon J. Gerraty }
104*0957b409SSimon J. Gerraty *data_len = len;
105*0957b409SSimon J. Gerraty return buf;
106*0957b409SSimon J. Gerraty }
107*0957b409SSimon J. Gerraty
108*0957b409SSimon J. Gerraty /* see bearssl_ssl.h */
109*0957b409SSimon J. Gerraty const br_sslrec_in_ccm_class br_sslrec_in_ccm_vtable = {
110*0957b409SSimon J. Gerraty {
111*0957b409SSimon J. Gerraty sizeof(br_sslrec_ccm_context),
112*0957b409SSimon J. Gerraty (int (*)(const br_sslrec_in_class *const *, size_t))
113*0957b409SSimon J. Gerraty &ccm_check_length,
114*0957b409SSimon J. Gerraty (unsigned char *(*)(const br_sslrec_in_class **,
115*0957b409SSimon J. Gerraty int, unsigned, void *, size_t *))
116*0957b409SSimon J. Gerraty &ccm_decrypt
117*0957b409SSimon J. Gerraty },
118*0957b409SSimon J. Gerraty (void (*)(const br_sslrec_in_ccm_class **,
119*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *, const void *, size_t,
120*0957b409SSimon J. Gerraty const void *, size_t))
121*0957b409SSimon J. Gerraty &in_ccm_init
122*0957b409SSimon J. Gerraty };
123*0957b409SSimon J. Gerraty
124*0957b409SSimon J. Gerraty static void
out_ccm_init(br_sslrec_ccm_context * cc,const br_block_ctrcbc_class * bc_impl,const void * key,size_t key_len,const void * iv,size_t tag_len)125*0957b409SSimon J. Gerraty out_ccm_init(br_sslrec_ccm_context *cc,
126*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *bc_impl,
127*0957b409SSimon J. Gerraty const void *key, size_t key_len,
128*0957b409SSimon J. Gerraty const void *iv, size_t tag_len)
129*0957b409SSimon J. Gerraty {
130*0957b409SSimon J. Gerraty cc->vtable.out = &br_sslrec_out_ccm_vtable;
131*0957b409SSimon J. Gerraty gen_ccm_init(cc, bc_impl, key, key_len, iv, tag_len);
132*0957b409SSimon J. Gerraty }
133*0957b409SSimon J. Gerraty
134*0957b409SSimon J. Gerraty static void
ccm_max_plaintext(const br_sslrec_ccm_context * cc,size_t * start,size_t * end)135*0957b409SSimon J. Gerraty ccm_max_plaintext(const br_sslrec_ccm_context *cc,
136*0957b409SSimon J. Gerraty size_t *start, size_t *end)
137*0957b409SSimon J. Gerraty {
138*0957b409SSimon J. Gerraty size_t len;
139*0957b409SSimon J. Gerraty
140*0957b409SSimon J. Gerraty *start += 8;
141*0957b409SSimon J. Gerraty len = *end - *start - cc->tag_len;
142*0957b409SSimon J. Gerraty if (len > 16384) {
143*0957b409SSimon J. Gerraty len = 16384;
144*0957b409SSimon J. Gerraty }
145*0957b409SSimon J. Gerraty *end = *start + len;
146*0957b409SSimon J. Gerraty }
147*0957b409SSimon J. Gerraty
148*0957b409SSimon J. Gerraty static unsigned char *
ccm_encrypt(br_sslrec_ccm_context * cc,int record_type,unsigned version,void * data,size_t * data_len)149*0957b409SSimon J. Gerraty ccm_encrypt(br_sslrec_ccm_context *cc,
150*0957b409SSimon J. Gerraty int record_type, unsigned version, void *data, size_t *data_len)
151*0957b409SSimon J. Gerraty {
152*0957b409SSimon J. Gerraty br_ccm_context zc;
153*0957b409SSimon J. Gerraty unsigned char *buf;
154*0957b409SSimon J. Gerraty unsigned char nonce[12], header[13];
155*0957b409SSimon J. Gerraty size_t len;
156*0957b409SSimon J. Gerraty
157*0957b409SSimon J. Gerraty buf = (unsigned char *)data;
158*0957b409SSimon J. Gerraty len = *data_len;
159*0957b409SSimon J. Gerraty
160*0957b409SSimon J. Gerraty /*
161*0957b409SSimon J. Gerraty * Make nonce; the explicit part is an encoding of the sequence
162*0957b409SSimon J. Gerraty * number.
163*0957b409SSimon J. Gerraty */
164*0957b409SSimon J. Gerraty memcpy(nonce, cc->iv, sizeof cc->iv);
165*0957b409SSimon J. Gerraty br_enc64be(nonce + 4, cc->seq);
166*0957b409SSimon J. Gerraty
167*0957b409SSimon J. Gerraty /*
168*0957b409SSimon J. Gerraty * Assemble synthetic header for the AAD.
169*0957b409SSimon J. Gerraty */
170*0957b409SSimon J. Gerraty br_enc64be(header, cc->seq ++);
171*0957b409SSimon J. Gerraty header[8] = (unsigned char)record_type;
172*0957b409SSimon J. Gerraty br_enc16be(header + 9, version);
173*0957b409SSimon J. Gerraty br_enc16be(header + 11, len);
174*0957b409SSimon J. Gerraty
175*0957b409SSimon J. Gerraty /*
176*0957b409SSimon J. Gerraty * Perform CCM encryption.
177*0957b409SSimon J. Gerraty */
178*0957b409SSimon J. Gerraty br_ccm_init(&zc, &cc->bc.vtable);
179*0957b409SSimon J. Gerraty br_ccm_reset(&zc, nonce, sizeof nonce, sizeof header, len, cc->tag_len);
180*0957b409SSimon J. Gerraty br_ccm_aad_inject(&zc, header, sizeof header);
181*0957b409SSimon J. Gerraty br_ccm_flip(&zc);
182*0957b409SSimon J. Gerraty br_ccm_run(&zc, 1, buf, len);
183*0957b409SSimon J. Gerraty br_ccm_get_tag(&zc, buf + len);
184*0957b409SSimon J. Gerraty
185*0957b409SSimon J. Gerraty /*
186*0957b409SSimon J. Gerraty * Assemble header and adjust pointer/length.
187*0957b409SSimon J. Gerraty */
188*0957b409SSimon J. Gerraty len += 8 + cc->tag_len;
189*0957b409SSimon J. Gerraty buf -= 13;
190*0957b409SSimon J. Gerraty memcpy(buf + 5, nonce + 4, 8);
191*0957b409SSimon J. Gerraty buf[0] = (unsigned char)record_type;
192*0957b409SSimon J. Gerraty br_enc16be(buf + 1, version);
193*0957b409SSimon J. Gerraty br_enc16be(buf + 3, len);
194*0957b409SSimon J. Gerraty *data_len = len + 5;
195*0957b409SSimon J. Gerraty return buf;
196*0957b409SSimon J. Gerraty }
197*0957b409SSimon J. Gerraty
198*0957b409SSimon J. Gerraty /* see bearssl_ssl.h */
199*0957b409SSimon J. Gerraty const br_sslrec_out_ccm_class br_sslrec_out_ccm_vtable = {
200*0957b409SSimon J. Gerraty {
201*0957b409SSimon J. Gerraty sizeof(br_sslrec_ccm_context),
202*0957b409SSimon J. Gerraty (void (*)(const br_sslrec_out_class *const *,
203*0957b409SSimon J. Gerraty size_t *, size_t *))
204*0957b409SSimon J. Gerraty &ccm_max_plaintext,
205*0957b409SSimon J. Gerraty (unsigned char *(*)(const br_sslrec_out_class **,
206*0957b409SSimon J. Gerraty int, unsigned, void *, size_t *))
207*0957b409SSimon J. Gerraty &ccm_encrypt
208*0957b409SSimon J. Gerraty },
209*0957b409SSimon J. Gerraty (void (*)(const br_sslrec_out_ccm_class **,
210*0957b409SSimon J. Gerraty const br_block_ctrcbc_class *, const void *, size_t,
211*0957b409SSimon J. Gerraty const void *, size_t))
212*0957b409SSimon J. Gerraty &out_ccm_init
213*0957b409SSimon J. Gerraty };
214