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
32 internal class RecordEncryptCCM : RecordEncrypt {
37 byte[] tmp, tag, ctr, cbcmac;
40 internal RecordEncryptCCM(IBlockCipher bc, byte[] iv, bool ccm8)
43 this.iv = new byte[4];
44 Array.Copy(iv, 0, this.iv, 0, 4);
49 cbcmac = new byte[16];
53 internal override void GetMaxPlaintext(ref int start, ref int end)
56 * We need room at the start for the record header (5 bytes)
57 * and the explicit nonce (8 bytes). We need room at the end
58 * for the MAC (16 bytes).
62 int len = Math.Min(end - start, 16384);
66 internal override void Encrypt(int recordType, int version,
67 byte[] data, ref int off, ref int len)
70 * CBC-MAC starts with block B0, that encodes the
71 * nonce, tag length, and data length.
72 * It is then followed by the AAD:
73 * - AAD header (length, over 2 bytes in our case)
74 * - TLS sequence number (8 bytes)
75 * - plain record header
77 tmp[0] = (byte)(0x40 | ((ccm8 ? 6 : 14) << 2) | 2);
78 Array.Copy(iv, 0, tmp, 1, 4);
79 IO.Enc64be(seq, tmp, 5);
81 IO.Enc16be(len, tmp, 14);
85 IO.Enc64be(seq, tmp, 18);
86 IO.WriteHeader(recordType, version, len, tmp, 26);
89 for (int i = 0; i < cbcmac.Length; i ++) {
92 bc.CBCMac(cbcmac, tmp, 0, 32);
95 * Make initial counter value, and compute tag mask.
96 * Since the counter least significant byte has value 0,
97 * getting it to the next value is simple and requires
98 * no carry propagation.
101 Array.Copy(tmp, 1, ctr, 1, 12);
102 for (int i = 13; i < 16; i ++) {
105 Array.Copy(ctr, 0, tag, 0, 16);
106 bc.BlockEncrypt(tag);
110 * Perform CTR encryption and CBC-MAC. CCM is defined
111 * to use CBC-MAC on the plaintext, not the ciphertext,
112 * thus we need to set the 'encrypt' flag to false.
114 * When the last block is partial, then we must pad
115 * the plaintext with zeros, and compute the CBC-MAC
118 int len1 = len & ~15;
119 int len2 = len - len1;
120 bc.CTRCBCRun(ctr, cbcmac, false, data, off, len1);
122 Array.Copy(data, off + len1, tmp, 0, len2);
123 for (int i = len2; i < 16; i ++) {
126 bc.CBCMac(cbcmac, tmp, 0, 16);
127 bc.BlockEncrypt(ctr);
128 for (int i = 0; i < len2; i ++) {
129 data[off + len1 + i] ^= ctr[i];
134 * XOR the CBC-MAC output with the tag mask.
136 for (int i = 0; i < (ccm8 ? 8 : 16); i ++) {
137 data[off + len + i] = (byte)(tag[i] ^ cbcmac[i]);
141 * Encode the header, and adjust offset / length.
144 len += ccm8 ? 16 : 24;
145 IO.WriteHeader(recordType, version, len, data, off);
146 IO.Enc64be(seq, data, off + 5);