2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * CCM initialisation. This does everything except setting the vtable,
29 * which depends on whether this is a context for encrypting or for
33 gen_ccm_init(br_sslrec_ccm_context
*cc
,
34 const br_block_ctrcbc_class
*bc_impl
,
35 const void *key
, size_t key_len
,
36 const void *iv
, size_t tag_len
)
39 bc_impl
->init(&cc
->bc
.vtable
, key
, key_len
);
40 memcpy(cc
->iv
, iv
, sizeof cc
->iv
);
41 cc
->tag_len
= tag_len
;
45 in_ccm_init(br_sslrec_ccm_context
*cc
,
46 const br_block_ctrcbc_class
*bc_impl
,
47 const void *key
, size_t key_len
,
48 const void *iv
, size_t tag_len
)
50 cc
->vtable
.in
= &br_sslrec_in_ccm_vtable
;
51 gen_ccm_init(cc
, bc_impl
, key
, key_len
, iv
, tag_len
);
55 ccm_check_length(const br_sslrec_ccm_context
*cc
, size_t rlen
)
58 * CCM overhead is 8 bytes for nonce_explicit, and the tag
59 * (normally 8 or 16 bytes, depending on cipher suite).
63 over
= 8 + cc
->tag_len
;
64 return rlen
>= over
&& rlen
<= (16384 + over
);
67 static unsigned char *
68 ccm_decrypt(br_sslrec_ccm_context
*cc
,
69 int record_type
, unsigned version
, void *data
, size_t *data_len
)
73 unsigned char nonce
[12], header
[13];
76 buf
= (unsigned char *)data
+ 8;
77 len
= *data_len
- (8 + cc
->tag_len
);
80 * Make nonce (implicit + explicit parts).
82 memcpy(nonce
, cc
->iv
, sizeof cc
->iv
);
83 memcpy(nonce
+ 4, data
, 8);
86 * Assemble synthetic header for the AAD.
88 br_enc64be(header
, cc
->seq
++);
89 header
[8] = (unsigned char)record_type
;
90 br_enc16be(header
+ 9, version
);
91 br_enc16be(header
+ 11, len
);
94 * Perform CCM decryption.
96 br_ccm_init(&zc
, &cc
->bc
.vtable
);
97 br_ccm_reset(&zc
, nonce
, sizeof nonce
, sizeof header
, len
, cc
->tag_len
);
98 br_ccm_aad_inject(&zc
, header
, sizeof header
);
100 br_ccm_run(&zc
, 0, buf
, len
);
101 if (!br_ccm_check_tag(&zc
, buf
+ len
)) {
108 /* see bearssl_ssl.h */
109 const br_sslrec_in_ccm_class br_sslrec_in_ccm_vtable
= {
111 sizeof(br_sslrec_ccm_context
),
112 (int (*)(const br_sslrec_in_class
*const *, size_t))
114 (unsigned char *(*)(const br_sslrec_in_class
**,
115 int, unsigned, void *, size_t *))
118 (void (*)(const br_sslrec_in_ccm_class
**,
119 const br_block_ctrcbc_class
*, const void *, size_t,
120 const void *, size_t))
125 out_ccm_init(br_sslrec_ccm_context
*cc
,
126 const br_block_ctrcbc_class
*bc_impl
,
127 const void *key
, size_t key_len
,
128 const void *iv
, size_t tag_len
)
130 cc
->vtable
.out
= &br_sslrec_out_ccm_vtable
;
131 gen_ccm_init(cc
, bc_impl
, key
, key_len
, iv
, tag_len
);
135 ccm_max_plaintext(const br_sslrec_ccm_context
*cc
,
136 size_t *start
, size_t *end
)
141 len
= *end
- *start
- cc
->tag_len
;
148 static unsigned char *
149 ccm_encrypt(br_sslrec_ccm_context
*cc
,
150 int record_type
, unsigned version
, void *data
, size_t *data_len
)
154 unsigned char nonce
[12], header
[13];
157 buf
= (unsigned char *)data
;
161 * Make nonce; the explicit part is an encoding of the sequence
164 memcpy(nonce
, cc
->iv
, sizeof cc
->iv
);
165 br_enc64be(nonce
+ 4, cc
->seq
);
168 * Assemble synthetic header for the AAD.
170 br_enc64be(header
, cc
->seq
++);
171 header
[8] = (unsigned char)record_type
;
172 br_enc16be(header
+ 9, version
);
173 br_enc16be(header
+ 11, len
);
176 * Perform CCM encryption.
178 br_ccm_init(&zc
, &cc
->bc
.vtable
);
179 br_ccm_reset(&zc
, nonce
, sizeof nonce
, sizeof header
, len
, cc
->tag_len
);
180 br_ccm_aad_inject(&zc
, header
, sizeof header
);
182 br_ccm_run(&zc
, 1, buf
, len
);
183 br_ccm_get_tag(&zc
, buf
+ len
);
186 * Assemble header and adjust pointer/length.
188 len
+= 8 + cc
->tag_len
;
190 memcpy(buf
+ 5, nonce
+ 4, 8);
191 buf
[0] = (unsigned char)record_type
;
192 br_enc16be(buf
+ 1, version
);
193 br_enc16be(buf
+ 3, len
);
198 /* see bearssl_ssl.h */
199 const br_sslrec_out_ccm_class br_sslrec_out_ccm_vtable
= {
201 sizeof(br_sslrec_ccm_context
),
202 (void (*)(const br_sslrec_out_class
*const *,
205 (unsigned char *(*)(const br_sslrec_out_class
**,
206 int, unsigned, void *, size_t *))
209 (void (*)(const br_sslrec_out_ccm_class
**,
210 const br_block_ctrcbc_class
*, const void *, size_t,
211 const void *, size_t))