Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / crypto / tests / PBKDF2_SHA256_test_vectors.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  * @file - This file contains PBKDF2 SHA256 test vectors.
20  */
21
22 #pragma once
23
24 #include <stddef.h>
25
26 #include <core/CHIPError.h>
27 #include <core/CHIPSafeCasts.h>
28
29 struct pbkdf2_test_vector
30 {
31     const uint8_t * password;
32     size_t plen;
33     const uint8_t * salt;
34     size_t slen;
35     unsigned int iter;
36     uint32_t key_len;
37     const uint8_t * key;
38     unsigned tcId;
39     int result;
40 };
41
42 static const uint8_t chiptest_key1[]                          = { 0x9a, 0x87, 0xd2, 0x5e, 0x37, 0x40, 0x8f, 0xd7, 0x2f, 0x42,
43                                          0x3d, 0x22, 0x85, 0xc7, 0x08, 0x6d, 0x3a, 0x64, 0x2a, 0x26 };
44 static const struct pbkdf2_test_vector chiptest_test_vector_1 = { .password = chip::Uint8::from_const_char("password"),
45                                                                   .plen     = 8,
46                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
47                                                                   .slen     = 8,
48                                                                   .iter     = 1,
49                                                                   .key_len  = 20,
50                                                                   .key      = chiptest_key1,
51                                                                   .tcId     = 1,
52                                                                   .result   = CHIP_NO_ERROR };
53
54 static const uint8_t chiptest_key2[]                          = { 0xa0, 0xf3, 0x0e, 0x25, 0xe8, 0x65, 0x9c, 0x36, 0xa0, 0x95,
55                                          0x89, 0x49, 0x0d, 0x64, 0xa6, 0xb1, 0x2e, 0x0c, 0x38, 0x3c };
56 static const struct pbkdf2_test_vector chiptest_test_vector_2 = { .password = chip::Uint8::from_const_char("password"),
57                                                                   .plen     = 8,
58                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
59                                                                   .slen     = 8,
60                                                                   .iter     = 2,
61                                                                   .key_len  = 20,
62                                                                   .key      = chiptest_key2,
63                                                                   .tcId     = 2,
64                                                                   .result   = CHIP_NO_ERROR };
65
66 static const uint8_t chiptest_key3[]                          = { 0x86, 0x16, 0x14, 0x28, 0x77, 0x14, 0xd4, 0x29, 0x75, 0x16,
67                                          0x5e, 0xb6, 0x3e, 0xa7, 0x8c, 0xce, 0x85, 0x50, 0x71, 0x3c };
68 static const struct pbkdf2_test_vector chiptest_test_vector_3 = { .password = chip::Uint8::from_const_char("password"),
69                                                                   .plen     = 8,
70                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
71                                                                   .slen     = 8,
72                                                                   .iter     = 5,
73                                                                   .key_len  = 20,
74                                                                   .key      = chiptest_key3,
75                                                                   .tcId     = 3,
76                                                                   .result   = CHIP_NO_ERROR };
77
78 static const uint8_t chiptest_key4[]                          = { 0x3f, 0x12, 0x78, 0x37, 0x88, 0x9c, 0x4a, 0x62, 0x3d, 0x6e,
79                                          0x8d, 0xc8, 0x8c, 0xa3, 0x3c, 0x01, 0x23, 0xa2, 0x9f, 0x07 };
80 static const struct pbkdf2_test_vector chiptest_test_vector_4 = { .password = chip::Uint8::from_const_char("password"),
81                                                                   .plen     = 8,
82                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
83                                                                   .slen     = 8,
84                                                                   .iter     = 3,
85                                                                   .key_len  = 20,
86                                                                   .key      = chiptest_key4,
87                                                                   .tcId     = 4,
88                                                                   .result   = CHIP_NO_ERROR };
89
90 static const uint8_t chiptest_key5[] = { 0x28, 0x1a, 0xef, 0x26, 0xfa, 0x2d, 0xdf, 0x5b, 0x76, 0x82, 0x01, 0xff, 0x3a,
91                                          0xdd, 0xfc, 0x68, 0x0a, 0xda, 0x07, 0x87, 0xc9, 0xb4, 0x76, 0x55, 0x42 };
92 static const struct pbkdf2_test_vector chiptest_test_vector_5 = { .password =
93                                                                       chip::Uint8::from_const_char("passwordPASSWORDpassword"),
94                                                                   .plen    = 24,
95                                                                   .salt    = chip::Uint8::from_const_char("saltSALTsaltSALT"),
96                                                                   .slen    = 16,
97                                                                   .iter    = 10,
98                                                                   .key_len = 25,
99                                                                   .key     = chiptest_key5,
100                                                                   .tcId    = 5,
101                                                                   .result  = CHIP_NO_ERROR };
102
103 static const struct pbkdf2_test_vector chiptest_test_vector_6 = { .password = nullptr,
104                                                                   .plen     = 8,
105                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
106                                                                   .slen     = 8,
107                                                                   .iter     = 1,
108                                                                   .key_len  = 20,
109                                                                   .key      = chiptest_key4,
110                                                                   .tcId     = 6,
111                                                                   .result   = CHIP_ERROR_INVALID_ARGUMENT };
112
113 static const struct pbkdf2_test_vector chiptest_test_vector_7 = { .password = chip::Uint8::from_const_char("password"),
114                                                                   .plen     = 0,
115                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
116                                                                   .slen     = 8,
117                                                                   .iter     = 1,
118                                                                   .key_len  = 20,
119                                                                   .key      = chiptest_key4,
120                                                                   .tcId     = 7,
121                                                                   .result   = CHIP_ERROR_INVALID_ARGUMENT };
122
123 static const struct pbkdf2_test_vector chiptest_test_vector_8 = { .password = chip::Uint8::from_const_char("password"),
124                                                                   .plen     = 8,
125                                                                   .salt     = nullptr,
126                                                                   .slen     = 8,
127                                                                   .iter     = 1,
128                                                                   .key_len  = 20,
129                                                                   .key      = chiptest_key4,
130                                                                   .tcId     = 8,
131                                                                   .result   = CHIP_ERROR_INVALID_ARGUMENT };
132
133 static const struct pbkdf2_test_vector chiptest_test_vector_9 = { .password = chip::Uint8::from_const_char("password"),
134                                                                   .plen     = 0,
135                                                                   .salt     = chip::Uint8::from_const_char("saltSALT"),
136                                                                   .slen     = 8,
137                                                                   .iter     = 1,
138                                                                   .key_len  = 0,
139                                                                   .key      = chiptest_key4,
140                                                                   .tcId     = 9,
141                                                                   .result   = CHIP_ERROR_INVALID_ARGUMENT };
142
143 static const struct pbkdf2_test_vector chiptest_test_vector_10 = { .password = chip::Uint8::from_const_char("password"),
144                                                                    .plen     = 0,
145                                                                    .salt     = chip::Uint8::from_const_char("saltSALT"),
146                                                                    .slen     = 8,
147                                                                    .iter     = 1,
148                                                                    .key_len  = 20,
149                                                                    .key      = nullptr,
150                                                                    .tcId     = 10,
151                                                                    .result   = CHIP_ERROR_INVALID_ARGUMENT };
152
153 static const struct pbkdf2_test_vector chiptest_test_vector_11 = { .password = chip::Uint8::from_const_char("password"),
154                                                                    .plen     = 8,
155                                                                    .salt     = nullptr,
156                                                                    .slen     = 0,
157                                                                    .iter     = 1,
158                                                                    .key_len  = 20,
159                                                                    .key      = chiptest_key4,
160                                                                    .tcId     = 11,
161                                                                    .result   = CHIP_ERROR_INVALID_ARGUMENT };
162
163 static const struct pbkdf2_test_vector chiptest_test_vector_12 = { .password = chip::Uint8::from_const_char("password"),
164                                                                    .plen     = 8,
165                                                                    .salt     = chip::Uint8::from_const_char("saltSAL"),
166                                                                    .slen     = 7,
167                                                                    .iter     = 1,
168                                                                    .key_len  = 20,
169                                                                    .key      = chiptest_key4,
170                                                                    .tcId     = 12,
171                                                                    .result   = CHIP_ERROR_INVALID_ARGUMENT };
172
173 static const struct pbkdf2_test_vector chiptest_test_vector_13 = { .password = chip::Uint8::from_const_char("password"),
174                                                                    .plen     = 8,
175                                                                    .salt     = chip::Uint8::from_const_char("saltSALTsaltSALTs"),
176                                                                    .slen     = 17,
177                                                                    .iter     = 1,
178                                                                    .key_len  = 20,
179                                                                    .key      = chiptest_key4,
180                                                                    .tcId     = 13,
181                                                                    .result   = CHIP_ERROR_INVALID_ARGUMENT };
182
183 static const struct pbkdf2_test_vector * pbkdf2_sha256_test_vectors[] = {
184     &chiptest_test_vector_1, &chiptest_test_vector_2, &chiptest_test_vector_3,
185 #if !CHIP_TARGET_STYLE_EMBEDDED
186     // The following test vector takes excessive time to run on an embedded target
187     &chiptest_test_vector_4,
188 #endif
189     &chiptest_test_vector_5, &chiptest_test_vector_6, &chiptest_test_vector_7, &chiptest_test_vector_8, &chiptest_test_vector_9,
190     &chiptest_test_vector_10, &chiptest_test_vector_11, &chiptest_test_vector_12, &chiptest_test_vector_13
191 };