Added CCM and CCM_8 cipher suites.
[BoarSSL] / SSLTLS / RecordDecryptCCM.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 RecordDecryptCCM : RecordDecrypt {
33
34 IBlockCipher bc;
35 byte[] iv;
36 ulong seq;
37 byte[] tmp, tag, ctr, cbcmac;
38 bool ccm8;
39
40 internal RecordDecryptCCM(IBlockCipher bc, byte[] iv, bool ccm8)
41 {
42 this.bc = bc;
43 this.iv = new byte[12];
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 bool CheckLength(int len)
54 {
55 int tagLen = ccm8 ? 8 : 16;
56 return len >= (8 + tagLen) && len <= (16384 + 8 + tagLen);
57 }
58
59 internal override bool Decrypt(int recordType, int version,
60 byte[] data, ref int off, ref int len)
61 {
62 Array.Copy(data, off, iv, 4, 8);
63 off += 8;
64 len -= ccm8 ? 16 : 24;
65
66 /*
67 * Assemble block B0 and AAD.
68 */
69 tmp[0] = (byte)(0x40 | ((ccm8 ? 6 : 14) << 2) | 2);
70 Array.Copy(iv, 0, tmp, 1, 12);
71 tmp[13] = 0;
72 IO.Enc16be(len, tmp, 14);
73
74 tmp[16] = 0;
75 tmp[17] = 13;
76 IO.Enc64be(seq, tmp, 18);
77 IO.WriteHeader(recordType, version, len, tmp, 26);
78 tmp[31] = 0;
79 seq ++;
80
81 for (int i = 0; i < cbcmac.Length; i ++) {
82 cbcmac[i] = 0;
83 }
84 bc.CBCMac(cbcmac, tmp, 0, 32);
85
86 /*
87 * Make initial counter value, and compute tag mask.
88 * Since the counter least significant byte has value 0,
89 * getting it to the next value is simple and requires
90 * no carry propagation.
91 */
92 ctr[0] = 2;
93 Array.Copy(iv, 0, ctr, 1, 12);
94 for (int i = 13; i < 16; i ++) {
95 ctr[i] = 0;
96 }
97 Array.Copy(ctr, 0, tag, 0, 16);
98 bc.BlockEncrypt(tag);
99 ctr[15] = 1;
100
101 /*
102 * Perform CTR decryption, and compute CBC-MAC. Since
103 * CBC-MAC requires full blocks, we have to do the
104 * processing of the last partial block in a temporary
105 * buffer. The CBC-MAC is computed on the plaintext,
106 * padded with zeros in the last block.
107 */
108 int len1 = len & ~15;
109 int len2 = len - len1;
110 bc.CTRCBCRun(ctr, cbcmac, true, data, off, len1);
111 if (len2 > 0) {
112 bc.BlockEncrypt(ctr);
113 for (int i = 0; i < len2; i ++) {
114 data[off + len1 + i] ^= ctr[i];
115 }
116 Array.Copy(data, off + len1, tmp, 0, len2);
117 for (int i = len2; i < 16; i ++) {
118 tmp[i] = 0;
119 }
120 bc.CBCMac(cbcmac, tmp, 0, 16);
121 }
122
123 /*
124 * Check that the record MAC matches the expected value
125 * (taking into account the tag mask).
126 */
127 int z = 0;
128 for (int i = 0; i < (ccm8 ? 8 : 16); i ++) {
129 z |= cbcmac[i] ^ tag[i] ^ data[off + len + i];
130 }
131 return z == 0;
132 }
133 }
134
135 }