xref: /aosp_15_r20/external/openthread/tests/unit/test_hkdf_sha256.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2020, 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 "test_platform.h"
32*cfb92d14SAndroid Build Coastguard Worker #include "test_util.h"
33*cfb92d14SAndroid Build Coastguard Worker #include "test_util.hpp"
34*cfb92d14SAndroid Build Coastguard Worker 
35*cfb92d14SAndroid Build Coastguard Worker #include "common/array.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
37*cfb92d14SAndroid Build Coastguard Worker #include "crypto/hkdf_sha256.hpp"
38*cfb92d14SAndroid Build Coastguard Worker 
39*cfb92d14SAndroid Build Coastguard Worker namespace ot {
40*cfb92d14SAndroid Build Coastguard Worker 
41*cfb92d14SAndroid Build Coastguard Worker struct TestVector
42*cfb92d14SAndroid Build Coastguard Worker {
43*cfb92d14SAndroid Build Coastguard Worker     const uint8_t *mInKey;
44*cfb92d14SAndroid Build Coastguard Worker     uint16_t       mInKeyLength;
45*cfb92d14SAndroid Build Coastguard Worker     const uint8_t *mSalt;
46*cfb92d14SAndroid Build Coastguard Worker     uint16_t       mSaltLength;
47*cfb92d14SAndroid Build Coastguard Worker     const uint8_t *mInfo;
48*cfb92d14SAndroid Build Coastguard Worker     uint16_t       mInfoLength;
49*cfb92d14SAndroid Build Coastguard Worker     const uint8_t *mOutKey;
50*cfb92d14SAndroid Build Coastguard Worker     uint16_t       mOutKeyLength;
51*cfb92d14SAndroid Build Coastguard Worker };
52*cfb92d14SAndroid Build Coastguard Worker 
TestHkdfSha256(void)53*cfb92d14SAndroid Build Coastguard Worker void TestHkdfSha256(void)
54*cfb92d14SAndroid Build Coastguard Worker {
55*cfb92d14SAndroid Build Coastguard Worker     enum
56*cfb92d14SAndroid Build Coastguard Worker     {
57*cfb92d14SAndroid Build Coastguard Worker         kMaxOuttKey = 128,
58*cfb92d14SAndroid Build Coastguard Worker         kFillByte   = 0x77,
59*cfb92d14SAndroid Build Coastguard Worker     };
60*cfb92d14SAndroid Build Coastguard Worker 
61*cfb92d14SAndroid Build Coastguard Worker     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62*cfb92d14SAndroid Build Coastguard Worker     // Test Case #1: RFC-5869 Appendix A.1
63*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kInKey1[]  = {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
64*cfb92d14SAndroid Build Coastguard Worker                                 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
65*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kSalt1[]   = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c};
66*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kInfo1[]   = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9};
67*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kOutKey1[] = {0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36,
68*cfb92d14SAndroid Build Coastguard Worker                                 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56,
69*cfb92d14SAndroid Build Coastguard Worker                                 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65};
70*cfb92d14SAndroid Build Coastguard Worker 
71*cfb92d14SAndroid Build Coastguard Worker     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72*cfb92d14SAndroid Build Coastguard Worker     // Test Case #2: RFC-5869 Appendix A.2
73*cfb92d14SAndroid Build Coastguard Worker 
74*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kInKey2[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
75*cfb92d14SAndroid Build Coastguard Worker                                0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
76*cfb92d14SAndroid Build Coastguard Worker                                0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
77*cfb92d14SAndroid Build Coastguard Worker                                0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
78*cfb92d14SAndroid Build Coastguard Worker                                0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
79*cfb92d14SAndroid Build Coastguard Worker                                0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f};
80*cfb92d14SAndroid Build Coastguard Worker 
81*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kSalt2[] = {0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
82*cfb92d14SAndroid Build Coastguard Worker                               0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b,
83*cfb92d14SAndroid Build Coastguard Worker                               0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
84*cfb92d14SAndroid Build Coastguard Worker                               0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
85*cfb92d14SAndroid Build Coastguard Worker                               0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
86*cfb92d14SAndroid Build Coastguard Worker                               0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf};
87*cfb92d14SAndroid Build Coastguard Worker 
88*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kInfo2[] = {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd,
89*cfb92d14SAndroid Build Coastguard Worker                               0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
90*cfb92d14SAndroid Build Coastguard Worker                               0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
91*cfb92d14SAndroid Build Coastguard Worker                               0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
92*cfb92d14SAndroid Build Coastguard Worker                               0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5,
93*cfb92d14SAndroid Build Coastguard Worker                               0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff};
94*cfb92d14SAndroid Build Coastguard Worker 
95*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kOutKey2[] = {0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1, 0xc8, 0xe7, 0xf7, 0x8c, 0x59, 0x6a,
96*cfb92d14SAndroid Build Coastguard Worker                                 0x49, 0x34, 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 0xfa, 0xd8, 0xa0, 0x50, 0xcc, 0x4c,
97*cfb92d14SAndroid Build Coastguard Worker                                 0x19, 0xaf, 0xa9, 0x7c, 0x59, 0x04, 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72, 0x71, 0xcb,
98*cfb92d14SAndroid Build Coastguard Worker                                 0x41, 0xc6, 0x5e, 0x59, 0x0e, 0x09, 0xda, 0x32, 0x75, 0x60, 0x0c, 0x2f, 0x09, 0xb8,
99*cfb92d14SAndroid Build Coastguard Worker                                 0x36, 0x77, 0x93, 0xa9, 0xac, 0xa3, 0xdb, 0x71, 0xcc, 0x30, 0xc5, 0x81, 0x79, 0xec,
100*cfb92d14SAndroid Build Coastguard Worker                                 0x3e, 0x87, 0xc1, 0x4c, 0x01, 0xd5, 0xc1, 0xf3, 0x43, 0x4f, 0x1d, 0x87};
101*cfb92d14SAndroid Build Coastguard Worker 
102*cfb92d14SAndroid Build Coastguard Worker     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103*cfb92d14SAndroid Build Coastguard Worker     // Test Case #3: RFC-5869 Appendix A.3
104*cfb92d14SAndroid Build Coastguard Worker 
105*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kInKey3[] = {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
106*cfb92d14SAndroid Build Coastguard Worker                                0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
107*cfb92d14SAndroid Build Coastguard Worker 
108*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kOutKey3[] = {0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f, 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c,
109*cfb92d14SAndroid Build Coastguard Worker                                 0x5a, 0x31, 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e, 0xc3, 0x45, 0x4e, 0x5f,
110*cfb92d14SAndroid Build Coastguard Worker                                 0x3c, 0x73, 0x8d, 0x2d, 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a, 0x96, 0xc8};
111*cfb92d14SAndroid Build Coastguard Worker 
112*cfb92d14SAndroid Build Coastguard Worker     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113*cfb92d14SAndroid Build Coastguard Worker     // Test Case #4:
114*cfb92d14SAndroid Build Coastguard Worker 
115*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kInKey4[]  = {'h', 'e', 'l', 'l', 'o'};
116*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kSalt4[]   = {0x8e, 0x94, 0xef, 0x80, 0x5b, 0x93, 0xe6, 0x83, 0xff, 0x18};
117*cfb92d14SAndroid Build Coastguard Worker     const uint8_t kOutKey4[] = {0x13, 0x48, 0x50, 0x67, 0xe2, 0x1a, 0xf1, 0x7c,
118*cfb92d14SAndroid Build Coastguard Worker                                 0x09, 0x00, 0xf7, 0x0d, 0x88, 0x5f, 0x02, 0x59};
119*cfb92d14SAndroid Build Coastguard Worker 
120*cfb92d14SAndroid Build Coastguard Worker     const TestVector kTestVectors[] = {
121*cfb92d14SAndroid Build Coastguard Worker         {kInKey1, sizeof(kInKey1), kSalt1, sizeof(kSalt1), kInfo1, sizeof(kInfo1), kOutKey1, sizeof(kOutKey1)},
122*cfb92d14SAndroid Build Coastguard Worker         {kInKey2, sizeof(kInKey2), kSalt2, sizeof(kSalt2), kInfo2, sizeof(kInfo2), kOutKey2, sizeof(kOutKey2)},
123*cfb92d14SAndroid Build Coastguard Worker         {kInKey3, sizeof(kInKey3), nullptr, 0, nullptr, 0, kOutKey3, sizeof(kOutKey3)},
124*cfb92d14SAndroid Build Coastguard Worker         {kInKey4, sizeof(kInKey4), kSalt4, sizeof(kSalt4), nullptr, 0, kOutKey4, sizeof(kOutKey4)},
125*cfb92d14SAndroid Build Coastguard Worker     };
126*cfb92d14SAndroid Build Coastguard Worker 
127*cfb92d14SAndroid Build Coastguard Worker     otInstance *instance = testInitInstance();
128*cfb92d14SAndroid Build Coastguard Worker 
129*cfb92d14SAndroid Build Coastguard Worker     VerifyOrQuit(instance != nullptr);
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker     for (const TestVector *test = &kTestVectors[0]; test < GetArrayEnd(kTestVectors); test++)
132*cfb92d14SAndroid Build Coastguard Worker     {
133*cfb92d14SAndroid Build Coastguard Worker         Crypto::HkdfSha256 hkdf;
134*cfb92d14SAndroid Build Coastguard Worker         uint8_t            outKey[kMaxOuttKey];
135*cfb92d14SAndroid Build Coastguard Worker         Crypto::Key        testInputKey;
136*cfb92d14SAndroid Build Coastguard Worker 
137*cfb92d14SAndroid Build Coastguard Worker         printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
138*cfb92d14SAndroid Build Coastguard Worker         DumpBuffer("\nInput Key", test->mInKey, test->mInKeyLength);
139*cfb92d14SAndroid Build Coastguard Worker         DumpBuffer("\nSalt", test->mSalt, test->mSaltLength);
140*cfb92d14SAndroid Build Coastguard Worker         DumpBuffer("\nInfo", test->mInfo, test->mInfoLength);
141*cfb92d14SAndroid Build Coastguard Worker         DumpBuffer("\nExpected Output Key", test->mOutKey, test->mOutKeyLength);
142*cfb92d14SAndroid Build Coastguard Worker 
143*cfb92d14SAndroid Build Coastguard Worker         memset(outKey, kFillByte, sizeof(outKey));
144*cfb92d14SAndroid Build Coastguard Worker         memset(&testInputKey, 0x00, sizeof(testInputKey));
145*cfb92d14SAndroid Build Coastguard Worker         testInputKey.mKey       = test->mInKey;
146*cfb92d14SAndroid Build Coastguard Worker         testInputKey.mKeyLength = test->mInKeyLength;
147*cfb92d14SAndroid Build Coastguard Worker 
148*cfb92d14SAndroid Build Coastguard Worker         hkdf.Extract(test->mSalt, test->mSaltLength, testInputKey);
149*cfb92d14SAndroid Build Coastguard Worker         hkdf.Expand(test->mInfo, test->mInfoLength, outKey, test->mOutKeyLength);
150*cfb92d14SAndroid Build Coastguard Worker 
151*cfb92d14SAndroid Build Coastguard Worker         DumpBuffer("\nCalculated Output Key", outKey, test->mOutKeyLength);
152*cfb92d14SAndroid Build Coastguard Worker 
153*cfb92d14SAndroid Build Coastguard Worker         VerifyOrQuit(memcmp(outKey, test->mOutKey, test->mOutKeyLength) == 0, "HKDF-SHA-256 failed");
154*cfb92d14SAndroid Build Coastguard Worker 
155*cfb92d14SAndroid Build Coastguard Worker         for (size_t i = test->mOutKeyLength + 1; i < sizeof(outKey); i++)
156*cfb92d14SAndroid Build Coastguard Worker         {
157*cfb92d14SAndroid Build Coastguard Worker             VerifyOrQuit(outKey[i] == kFillByte, "HKDF-SHA-256 wrote beyond output key length");
158*cfb92d14SAndroid Build Coastguard Worker         }
159*cfb92d14SAndroid Build Coastguard Worker     }
160*cfb92d14SAndroid Build Coastguard Worker 
161*cfb92d14SAndroid Build Coastguard Worker     testFreeInstance(instance);
162*cfb92d14SAndroid Build Coastguard Worker }
163*cfb92d14SAndroid Build Coastguard Worker 
164*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
165*cfb92d14SAndroid Build Coastguard Worker 
main(void)166*cfb92d14SAndroid Build Coastguard Worker int main(void)
167*cfb92d14SAndroid Build Coastguard Worker {
168*cfb92d14SAndroid Build Coastguard Worker     ot::TestHkdfSha256();
169*cfb92d14SAndroid Build Coastguard Worker     printf("All tests passed\n");
170*cfb92d14SAndroid Build Coastguard Worker     return 0;
171*cfb92d14SAndroid Build Coastguard Worker }
172