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 RecordDecryptCCM : RecordDecrypt {
37 byte[] tmp, tag, ctr, cbcmac;
40 internal RecordDecryptCCM(IBlockCipher bc, byte[] iv, bool ccm8)
43 this.iv = new byte[12];
44 Array.Copy(iv, 0, this.iv, 0, 4);
49 cbcmac = new byte[16];
53 internal override bool CheckLength(int len)
55 int tagLen = ccm8 ? 8 : 16;
56 return len >= (8 + tagLen) && len <= (16384 + 8 + tagLen);
59 internal override bool Decrypt(int recordType, int version,
60 byte[] data, ref int off, ref int len)
62 Array.Copy(data, off, iv, 4, 8);
64 len -= ccm8 ? 16 : 24;
67 * Assemble block B0 and AAD.
69 tmp[0] = (byte)(0x40 | ((ccm8 ? 6 : 14) << 2) | 2);
70 Array.Copy(iv, 0, tmp, 1, 12);
72 IO.Enc16be(len, tmp, 14);
76 IO.Enc64be(seq, tmp, 18);
77 IO.WriteHeader(recordType, version, len, tmp, 26);
81 for (int i = 0; i < cbcmac.Length; i ++) {
84 bc.CBCMac(cbcmac, tmp, 0, 32);
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.
93 Array.Copy(iv, 0, ctr, 1, 12);
94 for (int i = 13; i < 16; i ++) {
97 Array.Copy(ctr, 0, tag, 0, 16);
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.
108 int len1 = len & ~15;
109 int len2 = len - len1;
110 bc.CTRCBCRun(ctr, cbcmac, true, data, off, len1);
112 bc.BlockEncrypt(ctr);
113 for (int i = 0; i < len2; i ++) {
114 data[off + len1 + i] ^= ctr[i];
116 Array.Copy(data, off + len1, tmp, 0, len2);
117 for (int i = len2; i < 16; i ++) {
120 bc.CBCMac(cbcmac, tmp, 0, 16);
124 * Check that the record MAC matches the expected value
125 * (taking into account the tag mask).
128 for (int i = 0; i < (ccm8 ? 8 : 16); i ++) {
129 z |= cbcmac[i] ^ tag[i] ^ data[off + len + i];