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/array.hpp"
32*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
33*cfb92d14SAndroid Build Coastguard Worker #include "common/message.hpp"
34*cfb92d14SAndroid Build Coastguard Worker #include "crypto/hmac_sha256.hpp"
35*cfb92d14SAndroid Build Coastguard Worker #include "crypto/sha256.hpp"
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
38*cfb92d14SAndroid Build Coastguard Worker #include "test_util.h"
39*cfb92d14SAndroid Build Coastguard Worker
40*cfb92d14SAndroid Build Coastguard Worker namespace ot {
41*cfb92d14SAndroid Build Coastguard Worker
TestSha256(void)42*cfb92d14SAndroid Build Coastguard Worker void TestSha256(void)
43*cfb92d14SAndroid Build Coastguard Worker {
44*cfb92d14SAndroid Build Coastguard Worker const char kData1[] = "abc";
45*cfb92d14SAndroid Build Coastguard Worker
46*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash1 = {{
47*cfb92d14SAndroid Build Coastguard Worker 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
48*cfb92d14SAndroid Build Coastguard Worker 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
49*cfb92d14SAndroid Build Coastguard Worker }};
50*cfb92d14SAndroid Build Coastguard Worker
51*cfb92d14SAndroid Build Coastguard Worker const char kData2[] = "";
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash2 = {{
54*cfb92d14SAndroid Build Coastguard Worker 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
55*cfb92d14SAndroid Build Coastguard Worker 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
56*cfb92d14SAndroid Build Coastguard Worker }};
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker const char kData3[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash3 = {{
61*cfb92d14SAndroid Build Coastguard Worker 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
62*cfb92d14SAndroid Build Coastguard Worker 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1,
63*cfb92d14SAndroid Build Coastguard Worker }};
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker const char kData4[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmno"
66*cfb92d14SAndroid Build Coastguard Worker "pqrsmnopqrstnopqrstu";
67*cfb92d14SAndroid Build Coastguard Worker
68*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash4 = {{
69*cfb92d14SAndroid Build Coastguard Worker 0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37,
70*cfb92d14SAndroid Build Coastguard Worker 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1,
71*cfb92d14SAndroid Build Coastguard Worker }};
72*cfb92d14SAndroid Build Coastguard Worker
73*cfb92d14SAndroid Build Coastguard Worker struct TestCase
74*cfb92d14SAndroid Build Coastguard Worker {
75*cfb92d14SAndroid Build Coastguard Worker const char *mData; // (null-terminated string).
76*cfb92d14SAndroid Build Coastguard Worker otCryptoSha256Hash mHash;
77*cfb92d14SAndroid Build Coastguard Worker };
78*cfb92d14SAndroid Build Coastguard Worker
79*cfb92d14SAndroid Build Coastguard Worker static const TestCase kTestCases[] = {
80*cfb92d14SAndroid Build Coastguard Worker {kData1, kHash1},
81*cfb92d14SAndroid Build Coastguard Worker {kData2, kHash2},
82*cfb92d14SAndroid Build Coastguard Worker {kData3, kHash3},
83*cfb92d14SAndroid Build Coastguard Worker {kData4, kHash4},
84*cfb92d14SAndroid Build Coastguard Worker };
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Worker printf("TestSha256\n");
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
89*cfb92d14SAndroid Build Coastguard Worker MessagePool *messagePool;
90*cfb92d14SAndroid Build Coastguard Worker Message *message;
91*cfb92d14SAndroid Build Coastguard Worker uint16_t offsets[GetArrayLength(kTestCases)];
92*cfb92d14SAndroid Build Coastguard Worker uint8_t index;
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr);
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Worker messagePool = &instance->Get<MessagePool>();
97*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit((message = messagePool->Allocate(Message::kTypeIp6)) != nullptr);
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Worker for (const TestCase &testCase : kTestCases)
100*cfb92d14SAndroid Build Coastguard Worker {
101*cfb92d14SAndroid Build Coastguard Worker Crypto::Sha256 sha256;
102*cfb92d14SAndroid Build Coastguard Worker Crypto::Sha256::Hash hash;
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Worker sha256.Start();
105*cfb92d14SAndroid Build Coastguard Worker sha256.Update(testCase.mData, static_cast<uint16_t>(strlen(testCase.mData)));
106*cfb92d14SAndroid Build Coastguard Worker sha256.Finish(hash);
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash));
109*cfb92d14SAndroid Build Coastguard Worker }
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Worker // Append all test case `mData` in the message.
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Worker index = 0;
114*cfb92d14SAndroid Build Coastguard Worker
115*cfb92d14SAndroid Build Coastguard Worker for (const TestCase &testCase : kTestCases)
116*cfb92d14SAndroid Build Coastguard Worker {
117*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append("Hello"));
118*cfb92d14SAndroid Build Coastguard Worker offsets[index++] = message->GetLength();
119*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->AppendBytes(testCase.mData, static_cast<uint16_t>(strlen(testCase.mData))));
120*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append("There!"));
121*cfb92d14SAndroid Build Coastguard Worker }
122*cfb92d14SAndroid Build Coastguard Worker
123*cfb92d14SAndroid Build Coastguard Worker index = 0;
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker for (const TestCase &testCase : kTestCases)
126*cfb92d14SAndroid Build Coastguard Worker {
127*cfb92d14SAndroid Build Coastguard Worker Crypto::Sha256 sha256;
128*cfb92d14SAndroid Build Coastguard Worker Crypto::Sha256::Hash hash;
129*cfb92d14SAndroid Build Coastguard Worker
130*cfb92d14SAndroid Build Coastguard Worker sha256.Start();
131*cfb92d14SAndroid Build Coastguard Worker sha256.Update(*message, offsets[index++], static_cast<uint16_t>(strlen(testCase.mData)));
132*cfb92d14SAndroid Build Coastguard Worker sha256.Finish(hash);
133*cfb92d14SAndroid Build Coastguard Worker
134*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash));
135*cfb92d14SAndroid Build Coastguard Worker }
136*cfb92d14SAndroid Build Coastguard Worker
137*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
138*cfb92d14SAndroid Build Coastguard Worker }
139*cfb92d14SAndroid Build Coastguard Worker
TestHmacSha256(void)140*cfb92d14SAndroid Build Coastguard Worker void TestHmacSha256(void)
141*cfb92d14SAndroid Build Coastguard Worker {
142*cfb92d14SAndroid Build Coastguard Worker struct TestCase
143*cfb92d14SAndroid Build Coastguard Worker {
144*cfb92d14SAndroid Build Coastguard Worker otCryptoKey mKey;
145*cfb92d14SAndroid Build Coastguard Worker const void *mData;
146*cfb92d14SAndroid Build Coastguard Worker uint16_t mDataLength;
147*cfb92d14SAndroid Build Coastguard Worker otCryptoSha256Hash mHash;
148*cfb92d14SAndroid Build Coastguard Worker };
149*cfb92d14SAndroid Build Coastguard Worker
150*cfb92d14SAndroid Build Coastguard Worker // Test-cases from RFC 4231.
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Worker const uint8_t kKey1[] = {
153*cfb92d14SAndroid Build Coastguard Worker 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
154*cfb92d14SAndroid Build Coastguard Worker 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
155*cfb92d14SAndroid Build Coastguard Worker };
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker const char kData1[] = "Hi There";
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash1 = {{
160*cfb92d14SAndroid Build Coastguard Worker 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
161*cfb92d14SAndroid Build Coastguard Worker 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7,
162*cfb92d14SAndroid Build Coastguard Worker }};
163*cfb92d14SAndroid Build Coastguard Worker
164*cfb92d14SAndroid Build Coastguard Worker const char kKey2[] = "Jefe";
165*cfb92d14SAndroid Build Coastguard Worker const char kData2[] = "what do ya want for nothing?";
166*cfb92d14SAndroid Build Coastguard Worker
167*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash2 = {{
168*cfb92d14SAndroid Build Coastguard Worker 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7,
169*cfb92d14SAndroid Build Coastguard Worker 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43,
170*cfb92d14SAndroid Build Coastguard Worker }};
171*cfb92d14SAndroid Build Coastguard Worker
172*cfb92d14SAndroid Build Coastguard Worker const uint8_t kKey3[] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
173*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
174*cfb92d14SAndroid Build Coastguard Worker
175*cfb92d14SAndroid Build Coastguard Worker const uint8_t kData3[] = {
176*cfb92d14SAndroid Build Coastguard Worker 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
177*cfb92d14SAndroid Build Coastguard Worker 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
178*cfb92d14SAndroid Build Coastguard Worker 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
179*cfb92d14SAndroid Build Coastguard Worker };
180*cfb92d14SAndroid Build Coastguard Worker
181*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash3 = {{
182*cfb92d14SAndroid Build Coastguard Worker 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7,
183*cfb92d14SAndroid Build Coastguard Worker 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe,
184*cfb92d14SAndroid Build Coastguard Worker }};
185*cfb92d14SAndroid Build Coastguard Worker
186*cfb92d14SAndroid Build Coastguard Worker const uint8_t kKey4[] = {
187*cfb92d14SAndroid Build Coastguard Worker 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
188*cfb92d14SAndroid Build Coastguard Worker 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
189*cfb92d14SAndroid Build Coastguard Worker };
190*cfb92d14SAndroid Build Coastguard Worker
191*cfb92d14SAndroid Build Coastguard Worker const uint8_t kData4[] = {
192*cfb92d14SAndroid Build Coastguard Worker 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
193*cfb92d14SAndroid Build Coastguard Worker 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
194*cfb92d14SAndroid Build Coastguard Worker 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
195*cfb92d14SAndroid Build Coastguard Worker };
196*cfb92d14SAndroid Build Coastguard Worker
197*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash4 = {{
198*cfb92d14SAndroid Build Coastguard Worker 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a,
199*cfb92d14SAndroid Build Coastguard Worker 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b,
200*cfb92d14SAndroid Build Coastguard Worker }};
201*cfb92d14SAndroid Build Coastguard Worker
202*cfb92d14SAndroid Build Coastguard Worker const uint8_t kKey5[] = {
203*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
204*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
205*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
206*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
207*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
208*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
209*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
210*cfb92d14SAndroid Build Coastguard Worker 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
211*cfb92d14SAndroid Build Coastguard Worker };
212*cfb92d14SAndroid Build Coastguard Worker
213*cfb92d14SAndroid Build Coastguard Worker const char kData5[] = "This is a test using a larger than block-size key and a larger than block-size data. The "
214*cfb92d14SAndroid Build Coastguard Worker "key needs to be hashed before being used by the HMAC algorithm.";
215*cfb92d14SAndroid Build Coastguard Worker
216*cfb92d14SAndroid Build Coastguard Worker const otCryptoSha256Hash kHash5 = {{
217*cfb92d14SAndroid Build Coastguard Worker 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44,
218*cfb92d14SAndroid Build Coastguard Worker 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2,
219*cfb92d14SAndroid Build Coastguard Worker }};
220*cfb92d14SAndroid Build Coastguard Worker
221*cfb92d14SAndroid Build Coastguard Worker static const TestCase kTestCases[] = {
222*cfb92d14SAndroid Build Coastguard Worker {{&kKey1[0], sizeof(kKey1), 0}, kData1, sizeof(kData1) - 1, kHash1},
223*cfb92d14SAndroid Build Coastguard Worker {{reinterpret_cast<const uint8_t *>(&kKey2[0]), sizeof(kKey2) - 1, 0}, kData2, sizeof(kData2) - 1, kHash2},
224*cfb92d14SAndroid Build Coastguard Worker {{&kKey3[0], sizeof(kKey3), 0}, kData3, sizeof(kData3), kHash3},
225*cfb92d14SAndroid Build Coastguard Worker {{&kKey4[0], sizeof(kKey4), 0}, kData4, sizeof(kData4), kHash4},
226*cfb92d14SAndroid Build Coastguard Worker {{&kKey5[0], sizeof(kKey5), 0}, kData5, sizeof(kData5) - 1, kHash5},
227*cfb92d14SAndroid Build Coastguard Worker };
228*cfb92d14SAndroid Build Coastguard Worker
229*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
230*cfb92d14SAndroid Build Coastguard Worker MessagePool *messagePool;
231*cfb92d14SAndroid Build Coastguard Worker Message *message;
232*cfb92d14SAndroid Build Coastguard Worker uint16_t offsets[GetArrayLength(kTestCases)];
233*cfb92d14SAndroid Build Coastguard Worker uint8_t index;
234*cfb92d14SAndroid Build Coastguard Worker
235*cfb92d14SAndroid Build Coastguard Worker printf("TestHmacSha256\n");
236*cfb92d14SAndroid Build Coastguard Worker
237*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr);
238*cfb92d14SAndroid Build Coastguard Worker
239*cfb92d14SAndroid Build Coastguard Worker messagePool = &instance->Get<MessagePool>();
240*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit((message = messagePool->Allocate(Message::kTypeIp6)) != nullptr);
241*cfb92d14SAndroid Build Coastguard Worker
242*cfb92d14SAndroid Build Coastguard Worker for (const TestCase &testCase : kTestCases)
243*cfb92d14SAndroid Build Coastguard Worker {
244*cfb92d14SAndroid Build Coastguard Worker Crypto::HmacSha256 hmac;
245*cfb92d14SAndroid Build Coastguard Worker Crypto::HmacSha256::Hash hash;
246*cfb92d14SAndroid Build Coastguard Worker
247*cfb92d14SAndroid Build Coastguard Worker hmac.Start(static_cast<const Crypto::Key &>(testCase.mKey));
248*cfb92d14SAndroid Build Coastguard Worker hmac.Update(testCase.mData, testCase.mDataLength);
249*cfb92d14SAndroid Build Coastguard Worker hmac.Finish(hash);
250*cfb92d14SAndroid Build Coastguard Worker
251*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash));
252*cfb92d14SAndroid Build Coastguard Worker }
253*cfb92d14SAndroid Build Coastguard Worker
254*cfb92d14SAndroid Build Coastguard Worker // Append all test case `mData` in the message.
255*cfb92d14SAndroid Build Coastguard Worker
256*cfb92d14SAndroid Build Coastguard Worker index = 0;
257*cfb92d14SAndroid Build Coastguard Worker
258*cfb92d14SAndroid Build Coastguard Worker for (const TestCase &testCase : kTestCases)
259*cfb92d14SAndroid Build Coastguard Worker {
260*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append("Hello"));
261*cfb92d14SAndroid Build Coastguard Worker offsets[index++] = message->GetLength();
262*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->AppendBytes(testCase.mData, testCase.mDataLength));
263*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(message->Append("There"));
264*cfb92d14SAndroid Build Coastguard Worker }
265*cfb92d14SAndroid Build Coastguard Worker
266*cfb92d14SAndroid Build Coastguard Worker index = 0;
267*cfb92d14SAndroid Build Coastguard Worker
268*cfb92d14SAndroid Build Coastguard Worker for (const TestCase &testCase : kTestCases)
269*cfb92d14SAndroid Build Coastguard Worker {
270*cfb92d14SAndroid Build Coastguard Worker Crypto::HmacSha256 hmac;
271*cfb92d14SAndroid Build Coastguard Worker Crypto::HmacSha256::Hash hash;
272*cfb92d14SAndroid Build Coastguard Worker
273*cfb92d14SAndroid Build Coastguard Worker hmac.Start(static_cast<const Crypto::Key &>(testCase.mKey));
274*cfb92d14SAndroid Build Coastguard Worker hmac.Update(*message, offsets[index++], testCase.mDataLength);
275*cfb92d14SAndroid Build Coastguard Worker hmac.Finish(hash);
276*cfb92d14SAndroid Build Coastguard Worker
277*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(hash == static_cast<const Crypto::HmacSha256::Hash &>(testCase.mHash));
278*cfb92d14SAndroid Build Coastguard Worker }
279*cfb92d14SAndroid Build Coastguard Worker
280*cfb92d14SAndroid Build Coastguard Worker message->Free();
281*cfb92d14SAndroid Build Coastguard Worker
282*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
283*cfb92d14SAndroid Build Coastguard Worker }
284*cfb92d14SAndroid Build Coastguard Worker
285*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
286*cfb92d14SAndroid Build Coastguard Worker
main(void)287*cfb92d14SAndroid Build Coastguard Worker int main(void)
288*cfb92d14SAndroid Build Coastguard Worker {
289*cfb92d14SAndroid Build Coastguard Worker ot::TestSha256();
290*cfb92d14SAndroid Build Coastguard Worker ot::TestHmacSha256();
291*cfb92d14SAndroid Build Coastguard Worker printf("All tests passed\n");
292*cfb92d14SAndroid Build Coastguard Worker return 0;
293*cfb92d14SAndroid Build Coastguard Worker }
294