Added CCM and CCM_8 cipher suites.
[BoarSSL] / SSLTLS / RecordEncryptCCM.cs
1 /*
2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 */
24
25 using System;
26 using System.Text;
27
28 using Crypto;
29
30 namespace SSLTLS {
31
32 internal class RecordEncryptCCM : RecordEncrypt {
33
34 IBlockCipher bc;
35 byte[] iv;
36 ulong seq;
37 byte[] tmp, tag, ctr, cbcmac;
38 bool ccm8;
39
40 internal RecordEncryptCCM(IBlockCipher bc, byte[] iv, bool ccm8)
41 {
42 this.bc = bc;
43 this.iv = new byte[4];
44 Array.Copy(iv, 0, this.iv, 0, 4);
45 seq = 0;
46 tag = new byte[16];
47 tmp = new byte[32];
48 ctr = new byte[16];
49 cbcmac = new byte[16];
50 this.ccm8 = ccm8;
51 }
52
53 internal override void GetMaxPlaintext(ref int start, ref int end)
54 {
55 /*
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).
59 */
60 start += 13;
61 end -= ccm8 ? 8 : 16;
62 int len = Math.Min(end - start, 16384);
63 end = start + len;
64 }
65
66 internal override void Encrypt(int recordType, int version,
67 byte[] data, ref int off, ref int len)
68 {
69 /*
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
76 */
77 tmp[0] = (byte)(0x40 | ((ccm8 ? 6 : 14) << 2) | 2);
78 Array.Copy(iv, 0, tmp, 1, 4);
79 IO.Enc64be(seq, tmp, 5);
80 tmp[13] = 0;
81 IO.Enc16be(len, tmp, 14);
82
83 tmp[16] = 0;
84 tmp[17] = 13;
85 IO.Enc64be(seq, tmp, 18);
86 IO.WriteHeader(recordType, version, len, tmp, 26);
87 tmp[31] = 0;
88
89 for (int i = 0; i < cbcmac.Length; i ++) {
90 cbcmac[i] = 0;
91 }
92 bc.CBCMac(cbcmac, tmp, 0, 32);
93
94 /*
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.
99 */
100 ctr[0] = 2;
101 Array.Copy(tmp, 1, ctr, 1, 12);
102 for (int i = 13; i < 16; i ++) {
103 ctr[i] = 0;
104 }
105 Array.Copy(ctr, 0, tag, 0, 16);
106 bc.BlockEncrypt(tag);
107 ctr[15] = 1;
108
109 /*
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.
113 *
114 * When the last block is partial, then we must pad
115 * the plaintext with zeros, and compute the CBC-MAC
116 * on that plaintext.
117 */
118 int len1 = len & ~15;
119 int len2 = len - len1;
120 bc.CTRCBCRun(ctr, cbcmac, false, data, off, len1);
121 if (len2 > 0) {
122 Array.Copy(data, off + len1, tmp, 0, len2);
123 for (int i = len2; i < 16; i ++) {
124 tmp[i] = 0;
125 }
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];
130 }
131 }
132
133 /*
134 * XOR the CBC-MAC output with the tag mask.
135 */
136 for (int i = 0; i < (ccm8 ? 8 : 16); i ++) {
137 data[off + len + i] = (byte)(tag[i] ^ cbcmac[i]);
138 }
139
140 /*
141 * Encode the header, and adjust offset / length.
142 */
143 off -= 13;
144 len += ccm8 ? 16 : 24;
145 IO.WriteHeader(recordType, version, len, data, off);
146 IO.Enc64be(seq, data, off + 5);
147 len += 5;
148
149 seq ++;
150 }
151 }
152
153 }