1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2016, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker *
5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker *
16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker */
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
30*cfb92d14SAndroid Build Coastguard Worker
31*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
32*cfb92d14SAndroid Build Coastguard Worker #include "crypto/aes_ccm.hpp"
33*cfb92d14SAndroid Build Coastguard Worker
34*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
35*cfb92d14SAndroid Build Coastguard Worker #include "test_util.hpp"
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Worker namespace ot {
38*cfb92d14SAndroid Build Coastguard Worker
39*cfb92d14SAndroid Build Coastguard Worker /**
40*cfb92d14SAndroid Build Coastguard Worker * Verifies test vectors from IEEE 802.15.4-2006 Annex C Section C.2.1
41*cfb92d14SAndroid Build Coastguard Worker */
TestMacBeaconFrame(void)42*cfb92d14SAndroid Build Coastguard Worker void TestMacBeaconFrame(void)
43*cfb92d14SAndroid Build Coastguard Worker {
44*cfb92d14SAndroid Build Coastguard Worker uint8_t key[] = {
45*cfb92d14SAndroid Build Coastguard Worker 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
46*cfb92d14SAndroid Build Coastguard Worker };
47*cfb92d14SAndroid Build Coastguard Worker
48*cfb92d14SAndroid Build Coastguard Worker uint8_t test[] = {0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE,
49*cfb92d14SAndroid Build Coastguard Worker 0xAC, 0x02, 0x05, 0x00, 0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52,
50*cfb92d14SAndroid Build Coastguard Worker 0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A, 0xB5, 0x53};
51*cfb92d14SAndroid Build Coastguard Worker
52*cfb92d14SAndroid Build Coastguard Worker uint8_t encrypted[] = {0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE,
53*cfb92d14SAndroid Build Coastguard Worker 0xAC, 0x02, 0x05, 0x00, 0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52,
54*cfb92d14SAndroid Build Coastguard Worker 0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A, 0xB5, 0x53};
55*cfb92d14SAndroid Build Coastguard Worker
56*cfb92d14SAndroid Build Coastguard Worker uint8_t decrypted[] = {0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE,
57*cfb92d14SAndroid Build Coastguard Worker 0xAC, 0x02, 0x05, 0x00, 0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52,
58*cfb92d14SAndroid Build Coastguard Worker 0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A, 0xB5, 0x53};
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Worker otInstance *instance = testInitInstance();
61*cfb92d14SAndroid Build Coastguard Worker Crypto::AesCcm aesCcm;
62*cfb92d14SAndroid Build Coastguard Worker uint32_t headerLength = sizeof(test) - 8;
63*cfb92d14SAndroid Build Coastguard Worker uint32_t payloadLength = 0;
64*cfb92d14SAndroid Build Coastguard Worker uint8_t tagLength = 8;
65*cfb92d14SAndroid Build Coastguard Worker
66*cfb92d14SAndroid Build Coastguard Worker uint8_t nonce[] = {
67*cfb92d14SAndroid Build Coastguard Worker 0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x02,
68*cfb92d14SAndroid Build Coastguard Worker };
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr);
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Worker aesCcm.SetKey(key, sizeof(key));
73*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
74*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(test, headerLength);
75*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aesCcm.GetTagLength() == tagLength);
76*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(test + headerLength);
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(test, encrypted, sizeof(encrypted)) == 0);
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
81*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(test, headerLength);
82*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aesCcm.GetTagLength() == tagLength);
83*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(test + headerLength);
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(test, decrypted, sizeof(decrypted)) == 0);
86*cfb92d14SAndroid Build Coastguard Worker
87*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
88*cfb92d14SAndroid Build Coastguard Worker }
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Worker /**
91*cfb92d14SAndroid Build Coastguard Worker * Verifies test vectors from IEEE 802.15.4-2006 Annex C Section C.2.3
92*cfb92d14SAndroid Build Coastguard Worker */
TestMacCommandFrame(void)93*cfb92d14SAndroid Build Coastguard Worker void TestMacCommandFrame(void)
94*cfb92d14SAndroid Build Coastguard Worker {
95*cfb92d14SAndroid Build Coastguard Worker uint8_t key[] = {
96*cfb92d14SAndroid Build Coastguard Worker 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
97*cfb92d14SAndroid Build Coastguard Worker };
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Worker uint8_t test[] = {
100*cfb92d14SAndroid Build Coastguard Worker 0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC,
101*cfb92d14SAndroid Build Coastguard Worker 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06, 0x05, 0x00,
102*cfb92d14SAndroid Build Coastguard Worker 0x00, 0x00, 0x01, 0xCE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103*cfb92d14SAndroid Build Coastguard Worker };
104*cfb92d14SAndroid Build Coastguard Worker
105*cfb92d14SAndroid Build Coastguard Worker static constexpr uint32_t kHeaderLength = 29;
106*cfb92d14SAndroid Build Coastguard Worker static constexpr uint32_t kPayloadLength = 1;
107*cfb92d14SAndroid Build Coastguard Worker static constexpr uint8_t kTagLength = 8;
108*cfb92d14SAndroid Build Coastguard Worker
109*cfb92d14SAndroid Build Coastguard Worker uint8_t encrypted[] = {
110*cfb92d14SAndroid Build Coastguard Worker 0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC,
111*cfb92d14SAndroid Build Coastguard Worker 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06, 0x05, 0x00,
112*cfb92d14SAndroid Build Coastguard Worker 0x00, 0x00, 0x01, 0xD8, 0x4F, 0xDE, 0x52, 0x90, 0x61, 0xF9, 0xC6, 0xF1,
113*cfb92d14SAndroid Build Coastguard Worker };
114*cfb92d14SAndroid Build Coastguard Worker
115*cfb92d14SAndroid Build Coastguard Worker uint8_t decrypted[] = {
116*cfb92d14SAndroid Build Coastguard Worker 0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC,
117*cfb92d14SAndroid Build Coastguard Worker 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06, 0x05, 0x00,
118*cfb92d14SAndroid Build Coastguard Worker 0x00, 0x00, 0x01, 0xCE, 0x4F, 0xDE, 0x52, 0x90, 0x61, 0xF9, 0xC6, 0xF1,
119*cfb92d14SAndroid Build Coastguard Worker };
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Worker uint8_t nonce[] = {
122*cfb92d14SAndroid Build Coastguard Worker 0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x06,
123*cfb92d14SAndroid Build Coastguard Worker };
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker uint8_t tag[kTagLength];
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
128*cfb92d14SAndroid Build Coastguard Worker Message *message;
129*cfb92d14SAndroid Build Coastguard Worker Crypto::AesCcm aesCcm;
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr);
132*cfb92d14SAndroid Build Coastguard Worker
133*cfb92d14SAndroid Build Coastguard Worker aesCcm.SetKey(key, sizeof(key));
134*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(kHeaderLength, kPayloadLength, kTagLength, nonce, sizeof(nonce));
135*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(test, kHeaderLength);
136*cfb92d14SAndroid Build Coastguard Worker aesCcm.Payload(test + kHeaderLength, test + kHeaderLength, kPayloadLength, Crypto::AesCcm::kEncrypt);
137*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aesCcm.GetTagLength() == kTagLength);
138*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(test + kHeaderLength + kPayloadLength);
139*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(test, encrypted, sizeof(encrypted)) == 0);
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(kHeaderLength, kPayloadLength, kTagLength, nonce, sizeof(nonce));
142*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(test, kHeaderLength);
143*cfb92d14SAndroid Build Coastguard Worker aesCcm.Payload(test + kHeaderLength, test + kHeaderLength, kPayloadLength, Crypto::AesCcm::kDecrypt);
144*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aesCcm.GetTagLength() == kTagLength);
145*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(test + kHeaderLength + kPayloadLength);
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(test, decrypted, sizeof(decrypted)) == 0);
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Worker // Verify encryption/decryption in place within a message.
150*cfb92d14SAndroid Build Coastguard Worker
151*cfb92d14SAndroid Build Coastguard Worker message = instance->Get<MessagePool>().Allocate(Message::kTypeIp6);
152*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message != nullptr);
153*cfb92d14SAndroid Build Coastguard Worker
154*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->AppendBytes(test, kHeaderLength + kPayloadLength));
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(kHeaderLength, kPayloadLength, kTagLength, nonce, sizeof(nonce));
157*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(test, kHeaderLength);
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker aesCcm.Payload(*message, kHeaderLength, kPayloadLength, Crypto::AesCcm::kEncrypt);
160*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(aesCcm.GetTagLength() == kTagLength);
161*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(tag);
162*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append(tag));
163*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->GetLength() == sizeof(encrypted));
164*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->Compare(0, encrypted));
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(kHeaderLength, kPayloadLength, kTagLength, nonce, sizeof(nonce));
167*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(test, kHeaderLength);
168*cfb92d14SAndroid Build Coastguard Worker aesCcm.Payload(*message, kHeaderLength, kPayloadLength, Crypto::AesCcm::kDecrypt);
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->GetLength() == sizeof(encrypted));
171*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->Compare(0, decrypted));
172*cfb92d14SAndroid Build Coastguard Worker
173*cfb92d14SAndroid Build Coastguard Worker message->Free();
174*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
175*cfb92d14SAndroid Build Coastguard Worker }
176*cfb92d14SAndroid Build Coastguard Worker
177*cfb92d14SAndroid Build Coastguard Worker /**
178*cfb92d14SAndroid Build Coastguard Worker * Verifies in-place encryption/decryption.
179*cfb92d14SAndroid Build Coastguard Worker *
180*cfb92d14SAndroid Build Coastguard Worker */
TestInPlaceAesCcmProcessing(void)181*cfb92d14SAndroid Build Coastguard Worker void TestInPlaceAesCcmProcessing(void)
182*cfb92d14SAndroid Build Coastguard Worker {
183*cfb92d14SAndroid Build Coastguard Worker static constexpr uint16_t kTagLength = 4;
184*cfb92d14SAndroid Build Coastguard Worker static constexpr uint32_t kHeaderLength = 19;
185*cfb92d14SAndroid Build Coastguard Worker
186*cfb92d14SAndroid Build Coastguard Worker static const uint8_t kKey[] = {
187*cfb92d14SAndroid Build Coastguard Worker 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
188*cfb92d14SAndroid Build Coastguard Worker };
189*cfb92d14SAndroid Build Coastguard Worker
190*cfb92d14SAndroid Build Coastguard Worker static const uint8_t kNonce[] = {
191*cfb92d14SAndroid Build Coastguard Worker 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x06,
192*cfb92d14SAndroid Build Coastguard Worker };
193*cfb92d14SAndroid Build Coastguard Worker
194*cfb92d14SAndroid Build Coastguard Worker static uint16_t kMessageLengths[] = {30, 400, 800};
195*cfb92d14SAndroid Build Coastguard Worker
196*cfb92d14SAndroid Build Coastguard Worker uint8_t tag[kTagLength];
197*cfb92d14SAndroid Build Coastguard Worker uint8_t header[kHeaderLength];
198*cfb92d14SAndroid Build Coastguard Worker
199*cfb92d14SAndroid Build Coastguard Worker Crypto::AesCcm aesCcm;
200*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
201*cfb92d14SAndroid Build Coastguard Worker Message *message;
202*cfb92d14SAndroid Build Coastguard Worker Message *messageClone;
203*cfb92d14SAndroid Build Coastguard Worker
204*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr);
205*cfb92d14SAndroid Build Coastguard Worker
206*cfb92d14SAndroid Build Coastguard Worker message = instance->Get<MessagePool>().Allocate(Message::kTypeIp6);
207*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message != nullptr);
208*cfb92d14SAndroid Build Coastguard Worker
209*cfb92d14SAndroid Build Coastguard Worker aesCcm.SetKey(kKey, sizeof(kKey));
210*cfb92d14SAndroid Build Coastguard Worker
211*cfb92d14SAndroid Build Coastguard Worker for (uint16_t msgLength : kMessageLengths)
212*cfb92d14SAndroid Build Coastguard Worker {
213*cfb92d14SAndroid Build Coastguard Worker printf("msgLength %d\n", msgLength);
214*cfb92d14SAndroid Build Coastguard Worker
215*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->SetLength(0));
216*cfb92d14SAndroid Build Coastguard Worker
217*cfb92d14SAndroid Build Coastguard Worker for (uint16_t i = msgLength; i != 0; i--)
218*cfb92d14SAndroid Build Coastguard Worker {
219*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append<uint8_t>(i & 0xff));
220*cfb92d14SAndroid Build Coastguard Worker }
221*cfb92d14SAndroid Build Coastguard Worker
222*cfb92d14SAndroid Build Coastguard Worker messageClone = message->Clone();
223*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(messageClone != nullptr);
224*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(messageClone->GetLength() == msgLength);
225*cfb92d14SAndroid Build Coastguard Worker
226*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Read(0, header));
227*cfb92d14SAndroid Build Coastguard Worker
228*cfb92d14SAndroid Build Coastguard Worker // Encrypt in place
229*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(kHeaderLength, msgLength - kHeaderLength, kTagLength, kNonce, sizeof(kNonce));
230*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(header);
231*cfb92d14SAndroid Build Coastguard Worker aesCcm.Payload(*message, kHeaderLength, msgLength - kHeaderLength, Crypto::AesCcm::kEncrypt);
232*cfb92d14SAndroid Build Coastguard Worker
233*cfb92d14SAndroid Build Coastguard Worker // Append the tag
234*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(tag);
235*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append(tag));
236*cfb92d14SAndroid Build Coastguard Worker
237*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->GetLength() == msgLength + kTagLength);
238*cfb92d14SAndroid Build Coastguard Worker
239*cfb92d14SAndroid Build Coastguard Worker // Decrypt in place
240*cfb92d14SAndroid Build Coastguard Worker aesCcm.Init(kHeaderLength, msgLength - kHeaderLength, kTagLength, kNonce, sizeof(kNonce));
241*cfb92d14SAndroid Build Coastguard Worker aesCcm.Header(header);
242*cfb92d14SAndroid Build Coastguard Worker aesCcm.Payload(*message, kHeaderLength, msgLength - kHeaderLength, Crypto::AesCcm::kDecrypt);
243*cfb92d14SAndroid Build Coastguard Worker
244*cfb92d14SAndroid Build Coastguard Worker // Check the tag against what is the message
245*cfb92d14SAndroid Build Coastguard Worker aesCcm.Finalize(tag);
246*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->Compare(msgLength, tag));
247*cfb92d14SAndroid Build Coastguard Worker
248*cfb92d14SAndroid Build Coastguard Worker // Check that decrypted message is the same as original (cloned) message
249*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(message->CompareBytes(0, *messageClone, 0, msgLength));
250*cfb92d14SAndroid Build Coastguard Worker
251*cfb92d14SAndroid Build Coastguard Worker messageClone->Free();
252*cfb92d14SAndroid Build Coastguard Worker }
253*cfb92d14SAndroid Build Coastguard Worker
254*cfb92d14SAndroid Build Coastguard Worker message->Free();
255*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
256*cfb92d14SAndroid Build Coastguard Worker }
257*cfb92d14SAndroid Build Coastguard Worker
258*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
259*cfb92d14SAndroid Build Coastguard Worker
main(void)260*cfb92d14SAndroid Build Coastguard Worker int main(void)
261*cfb92d14SAndroid Build Coastguard Worker {
262*cfb92d14SAndroid Build Coastguard Worker ot::TestMacBeaconFrame();
263*cfb92d14SAndroid Build Coastguard Worker ot::TestMacCommandFrame();
264*cfb92d14SAndroid Build Coastguard Worker ot::TestInPlaceAesCcmProcessing();
265*cfb92d14SAndroid Build Coastguard Worker printf("All tests passed\n");
266*cfb92d14SAndroid Build Coastguard Worker return 0;
267*cfb92d14SAndroid Build Coastguard Worker }
268