1 /* test_ecc_dh.c - TinyCrypt implementation of some EC-DH tests */
2 
3 /* Copyright (c) 2014, Kenneth MacKay
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *  * Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  *  Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
29  *
30  *  Redistribution and use in source and binary forms, with or without
31  *  modification, are permitted provided that the following conditions are met:
32  *
33  *    - Redistributions of source code must retain the above copyright notice,
34  *     this list of conditions and the following disclaimer.
35  *
36  *    - Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  *
40  *    - Neither the name of Intel Corporation nor the names of its contributors
41  *    may be used to endorse or promote products derived from this software
42  *    without specific prior written permission.
43  *
44  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  *  POSSIBILITY OF SUCH DAMAGE.
55  *
56  *  test_ecc_ecdh.c -- Implementation of some EC-DH tests
57  *
58  */
59 #include <tinycrypt/ecc.h>
60 #include <tinycrypt/ecc_dh.h>
61 #include <tinycrypt/ecc_platform_specific.h>
62 #include "test_ecc_utils.h"
63 #include <zephyr/test_utils.h>
64 #include <tinycrypt/constants.h>
65 
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <zephyr/ztest.h>
70 
ecdh_vectors(char ** qx_vec,char ** qy_vec,char ** d_vec,char ** z_vec,int tests,int verbose)71 int ecdh_vectors(char **qx_vec, char **qy_vec, char **d_vec, char **z_vec,
72 		 int tests, int verbose)
73 {
74 
75 	unsigned int pub[2 * NUM_ECC_WORDS];
76 	unsigned int prv[NUM_ECC_WORDS];
77 	unsigned int z[NUM_ECC_WORDS];
78 	unsigned int result = TC_PASS;
79 
80 	int rc;
81 	unsigned int exp_z[NUM_ECC_WORDS];
82 
83 	const struct uECC_Curve_t *curve = uECC_secp256r1();
84 
85 	for (int i = 0; i < tests; i++) {
86 
87 		string2scalar(pub + NUM_ECC_WORDS, NUM_ECC_WORDS, qx_vec[i]);
88 		string2scalar(pub, NUM_ECC_WORDS, qy_vec[i]);
89 		string2scalar(exp_z, NUM_ECC_WORDS, z_vec[i]);
90 		string2scalar(prv, NUM_ECC_WORDS, d_vec[i]);
91 
92 		uint8_t pub_bytes[2 * NUM_ECC_BYTES];
93 		uECC_vli_nativeToBytes(pub_bytes, 2 * NUM_ECC_BYTES, pub);
94 		uint8_t private_bytes[NUM_ECC_BYTES];
95 		uECC_vli_nativeToBytes(private_bytes, NUM_ECC_BYTES, prv);
96 		uint8_t z_bytes[NUM_ECC_BYTES];
97 		uECC_vli_nativeToBytes(z_bytes, NUM_ECC_BYTES, exp_z);
98 
99 		rc = uECC_shared_secret(pub_bytes, private_bytes, z_bytes, curve);
100 
101 		/**TESTPOINT: Check for ECDH failure*/
102 		zassert_equal(rc, TC_CRYPTO_SUCCESS, "ECDH failure, exit");
103 
104 		uECC_vli_bytesToNative(z, z_bytes, NUM_ECC_BYTES);
105 
106 		result = check_ecc_result(i, "Z", exp_z, z, NUM_ECC_WORDS, verbose);
107 
108 		/**TESTPOINT: Check result*/
109 		zassert_false(result, "ECDH test failed");
110 	}
111 	return result;
112 }
113 
cavp_ecdh(bool verbose)114 int cavp_ecdh(bool verbose)
115 {
116 	unsigned int result = TC_PASS;
117 	/*
118 	 * P-256
119 	 */
120 	char *d[] = {
121 		"7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534",
122 		"38f65d6dce47676044d58ce5139582d568f64bb16098d179dbab07741dd5caf5",
123 		"1accfaf1b97712b85a6f54b148985a1bdc4c9bec0bd258cad4b3d603f49f32c8",
124 		"207c43a79bfee03db6f4b944f53d2fb76cc49ef1c9c4d34d51b6c65c4db6932d",
125 		"59137e38152350b195c9718d39673d519838055ad908dd4757152fd8255c09bf",
126 		"f5f8e0174610a661277979b58ce5c90fee6c9b3bb346a90a7196255e40b132ef",
127 		"3b589af7db03459c23068b64f63f28d3c3c6bc25b5bf76ac05f35482888b5190",
128 		"d8bf929a20ea7436b2461b541a11c80e61d826c0a4c9d322b31dd54e7f58b9c8",
129 		"0f9883ba0ef32ee75ded0d8bda39a5146a29f1f2507b3bd458dbea0b2bb05b4d",
130 		"2beedb04b05c6988f6a67500bb813faf2cae0d580c9253b6339e4a3337bb6c08",
131 		"77c15dcf44610e41696bab758943eff1409333e4d5a11bbe72c8f6c395e9f848",
132 		"42a83b985011d12303db1a800f2610f74aa71cdf19c67d54ce6c9ed951e9093e",
133 		"ceed35507b5c93ead5989119b9ba342cfe38e6e638ba6eea343a55475de2800b",
134 		"43e0e9d95af4dc36483cdd1968d2b7eeb8611fcce77f3a4e7d059ae43e509604",
135 		"b2f3600df3368ef8a0bb85ab22f41fc0e5f4fdd54be8167a5c3cd4b08db04903",
136 		"4002534307f8b62a9bf67ff641ddc60fef593b17c3341239e95bdb3e579bfdc8",
137 		"4dfa12defc60319021b681b3ff84a10a511958c850939ed45635934ba4979147",
138 		"1331f6d874a4ed3bc4a2c6e9c74331d3039796314beee3b7152fcdba5556304e",
139 		"dd5e9f70ae740073ca0204df60763fb6036c45709bf4a7bb4e671412fad65da3",
140 		"5ae026cfc060d55600717e55b8a12e116d1d0df34af831979057607c2d9c2f76",
141 		"b601ac425d5dbf9e1735c5e2d5bdb79ca98b3d5be4a2cfd6f2273f150e064d9d",
142 		"fefb1dda1845312b5fce6b81b2be205af2f3a274f5a212f66c0d9fc33d7ae535",
143 		"334ae0c4693d23935a7e8e043ebbde21e168a7cba3fa507c9be41d7681e049ce",
144 		"2c4bde40214fcc3bfc47d4cf434b629acbe9157f8fd0282540331de7942cf09d",
145 		"85a268f9d7772f990c36b42b0a331adc92b5941de0b862d5d89a347cbf8faab0",
146 	};
147 
148 	char *x[] = {
149 		"700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287",
150 		"809f04289c64348c01515eb03d5ce7ac1a8cb9498f5caa50197e58d43a86a7ae",
151 		"a2339c12d4a03c33546de533268b4ad667debf458b464d77443636440ee7fec3",
152 		"df3989b9fa55495719b3cf46dccd28b5153f7808191dd518eff0c3cff2b705ed",
153 		"41192d2813e79561e6a1d6f53c8bc1a433a199c835e141b05a74a97b0faeb922",
154 		"33e82092a0f1fb38f5649d5867fba28b503172b7035574bf8e5b7100a3052792",
155 		"6a9e0c3f916e4e315c91147be571686d90464e8bf981d34a90b6353bca6eeba7",
156 		"a9c0acade55c2a73ead1a86fb0a9713223c82475791cd0e210b046412ce224bb",
157 		"94e94f16a98255fff2b9ac0c9598aac35487b3232d3231bd93b7db7df36f9eb9",
158 		"e099bf2a4d557460b5544430bbf6da11004d127cb5d67f64ab07c94fcdf5274f",
159 		"f75a5fe56bda34f3c1396296626ef012dc07e4825838778a645c8248cff01658",
160 		"2db4540d50230756158abf61d9835712b6486c74312183ccefcaef2797b7674d",
161 		"cd94fc9497e8990750309e9a8534fd114b0a6e54da89c4796101897041d14ecb",
162 		"15b9e467af4d290c417402e040426fe4cf236bae72baa392ed89780dfccdb471",
163 		"49c503ba6c4fa605182e186b5e81113f075bc11dcfd51c932fb21e951eee2fa1",
164 		"19b38de39fdd2f70f7091631a4f75d1993740ba9429162c2a45312401636b29c",
165 		"2c91c61f33adfe9311c942fdbff6ba47020feff416b7bb63cec13faf9b099954",
166 		"a28a2edf58025668f724aaf83a50956b7ac1cfbbff79b08c3bf87dfd2828d767",
167 		"a2ef857a081f9d6eb206a81c4cf78a802bdf598ae380c8886ecd85fdc1ed7644",
168 		"ccd8a2d86bc92f2e01bce4d6922cf7fe1626aed044685e95e2eebd464505f01f",
169 		"c188ffc8947f7301fb7b53e36746097c2134bf9cc981ba74b4e9c4361f595e4e",
170 		"317e1020ff53fccef18bf47bb7f2dd7707fb7b7a7578e04f35b3beed222a0eb6",
171 		"45fb02b2ceb9d7c79d9c2fa93e9c7967c2fa4df5789f9640b24264b1e524fcb1",
172 		"a19ef7bff98ada781842fbfc51a47aff39b5935a1c7d9625c8d323d511c92de6",
173 		"356c5a444c049a52fee0adeb7e5d82ae5aa83030bfff31bbf8ce2096cf161c4b",
174 	};
175 
176 	char *y[] = {
177 		"db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac",
178 		"b29d84e811197f25eba8f5194092cb6ff440e26d4421011372461f579271cda3",
179 		"ef48a3ab26e20220bcda2c1851076839dae88eae962869a497bf73cb66faf536",
180 		"422294ff46003429d739a33206c8752552c8ba54a270defc06e221e0feaf6ac4",
181 		"1af98cc45e98a7e041b01cf35f462b7562281351c8ebf3ffa02e33a0722a1328",
182 		"f2cf6b601e0a05945e335550bf648d782f46186c772c0f20d3cd0d6b8ca14b2f",
183 		"40f9bead39c2f2bcc2602f75b8a73ec7bdffcbcead159d0174c6c4d3c5357f05",
184 		"f6de0afa20e93e078467c053d241903edad734c6b403ba758c2b5ff04c9d4229",
185 		"d8049a43579cfa90b8093a94416cbefbf93386f15b3f6e190b6e3455fedfe69a",
186 		"d9c50dbe70d714edb5e221f4e020610eeb6270517e688ca64fb0e98c7ef8c1c5",
187 		"33bbdf1b1772d8059df568b061f3f1122f28a8d819167c97be448e3dc3fb0c3c",
188 		"62f57f314e3f3495dc4e099012f5e0ba71770f9660a1eada54104cdfde77243e",
189 		"c3def4b5fe04faee0a11932229fff563637bfdee0e79c6deeaf449f85401c5c4",
190 		"cdf4e9170fb904302b8fd93a820ba8cc7ed4efd3a6f2d6b05b80b2ff2aee4e77",
191 		"8af706ff0922d87b3f0c5e4e31d8b259aeb260a9269643ed520a13bb25da5924",
192 		"09aed7232b28e060941741b6828bcdfa2bc49cc844f3773611504f82a390a5ae",
193 		"6cab31b06419e5221fca014fb84ec870622a1b12bab5ae43682aa7ea73ea08d0",
194 		"dfa7bfffd4c766b86abeaf5c99b6e50cb9ccc9d9d00b7ffc7804b0491b67bc03",
195 		"563c4c20419f07bc17d0539fade1855e34839515b892c0f5d26561f97fa04d1a",
196 		"e9ddd583a9635a667777d5b8a8f31b0f79eba12c75023410b54b8567dddc0f38",
197 		"bf7d2f2056e72421ef393f0c0f2b0e00130e3cac4abbcc00286168e85ec55051",
198 		"09420ce5a19d77c6fe1ee587e6a49fbaf8f280e8df033d75403302e5a27db2ae",
199 		"5c6e8ecf1f7d3023893b7b1ca1e4d178972ee2a230757ddc564ffe37f5c5a321",
200 		"e9c184df75c955e02e02e400ffe45f78f339e1afe6d056fb3245f4700ce606ef",
201 		"57d128de8b2a57a094d1a001e572173f96e8866ae352bf29cddaf92fc85b2f92",
202 	};
203 
204 	char *Z[] = {
205 		"46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b",
206 		"057d636096cb80b67a8c038c890e887d1adfa4195e9b3ce241c8a778c59cda67",
207 		"2d457b78b4614132477618a5b077965ec90730a8c81a1c75d6d4ec68005d67ec",
208 		"96441259534b80f6aee3d287a6bb17b5094dd4277d9e294f8fe73e48bf2a0024",
209 		"19d44c8d63e8e8dd12c22a87b8cd4ece27acdde04dbf47f7f27537a6999a8e62",
210 		"664e45d5bba4ac931cd65d52017e4be9b19a515f669bea4703542a2c525cd3d3",
211 		"ca342daa50dc09d61be7c196c85e60a80c5cb04931746820be548cdde055679d",
212 		"35aa9b52536a461bfde4e85fc756be928c7de97923f0416c7a3ac8f88b3d4489",
213 		"605c16178a9bc875dcbff54d63fe00df699c03e8a888e9e94dfbab90b25f39b4",
214 		"f96e40a1b72840854bb62bc13c40cc2795e373d4e715980b261476835a092e0b",
215 		"8388fa79c4babdca02a8e8a34f9e43554976e420a4ad273c81b26e4228e9d3a3",
216 		"72877cea33ccc4715038d4bcbdfe0e43f42a9e2c0c3b017fc2370f4b9acbda4a",
217 		"e4e7408d85ff0e0e9c838003f28cdbd5247cdce31f32f62494b70e5f1bc36307",
218 		"ed56bcf695b734142c24ecb1fc1bb64d08f175eb243a31f37b3d9bb4407f3b96",
219 		"bc5c7055089fc9d6c89f83c1ea1ada879d9934b2ea28fcf4e4a7e984b28ad2cf",
220 		"9a4e8e657f6b0e097f47954a63c75d74fcba71a30d83651e3e5a91aa7ccd8343",
221 		"3ca1fc7ad858fb1a6aba232542f3e2a749ffc7203a2374a3f3d3267f1fc97b78",
222 		"1aaabe7ee6e4a6fa732291202433a237df1b49bc53866bfbe00db96a0f58224f",
223 		"430e6a4fba4449d700d2733e557f66a3bf3d50517c1271b1ddae1161b7ac798c",
224 		"1ce9e6740529499f98d1f1d71329147a33df1d05e4765b539b11cf615d6974d3",
225 		"4690e3743c07d643f1bc183636ab2a9cb936a60a802113c49bb1b3f2d0661660",
226 		"30c2261bd0004e61feda2c16aa5e21ffa8d7e7f7dbf6ec379a43b48e4b36aeb0",
227 		"2adae4a138a239dcd93c243a3803c3e4cf96e37fe14e6a9b717be9599959b11c",
228 		"2e277ec30f5ea07d6ce513149b9479b96e07f4b6913b1b5c11305c1444a1bc0b",
229 		"1e51373bd2c6044c129c436e742a55be2a668a85ae08441b6756445df5493857",
230 	};
231 
232 	TC_PRINT("Test #1: ECDH");
233 	TC_PRINT("NIST-p256\n");
234 
235 	result = ecdh_vectors(x, y, d, Z, 25, verbose);
236 
237 	/**TESTPOINT: Check result*/
238 	zassert_false(result, "ECDH test failed");
239 	return result;
240 }
241 
cavp_keygen(bool verbose)242 int cavp_keygen(bool verbose)
243 {
244 	unsigned int result = TC_PASS;
245 	/*
246 	 * [P-256, B.4.2 Key Pair Generation by Testing Candidates]
247 	 */
248 	char *d[] = {
249 		"c9806898a0334916c860748880a541f093b579a9b1f32934d86c363c39800357",
250 		"710735c8388f48c684a97bd66751cc5f5a122d6b9a96a2dbe73662f78217446d",
251 		"78d5d8b7b3e2c16b3e37e7e63becd8ceff61e2ce618757f514620ada8a11f6e4",
252 		"2a61a0703860585fe17420c244e1de5a6ac8c25146b208ef88ad51ae34c8cb8c",
253 		"01b965b45ff386f28c121c077f1d7b2710acc6b0cb58d8662d549391dcf5a883",
254 		"fac92c13d374c53a085376fe4101618e1e181b5a63816a84a0648f3bdc24e519",
255 		"f257a192dde44227b3568008ff73bcf599a5c45b32ab523b5b21ca582fef5a0a",
256 		"add67e57c42a3d28708f0235eb86885a4ea68e0d8cfd76eb46134c596522abfd",
257 		"4494860fd2c805c5c0d277e58f802cff6d731f76314eb1554142a637a9bc5538",
258 		"d40b07b1ea7b86d4709ef9dc634c61229feb71abd63dc7fc85ef46711a87b210",
259 	};
260 
261 	char *x[] = {
262 		"d0720dc691aa80096ba32fed1cb97c2b620690d06de0317b8618d5ce65eb728f",
263 		"f6836a8add91cb182d8d258dda6680690eb724a66dc3bb60d2322565c39e4ab9",
264 		"76711126cbb2af4f6a5fe5665dad4c88d27b6cb018879e03e54f779f203a854e",
265 		"e1aa7196ceeac088aaddeeba037abb18f67e1b55c0a5c4e71ec70ad666fcddc8",
266 		"1f038c5422e88eec9e88b815e8f6b3e50852333fc423134348fc7d79ef8e8a10",
267 		"7258f2ab96fc84ef6ccb33e308cd392d8b568ea635730ceb4ebd72fa870583b9",
268 		"d2e01411817b5512b79bbbe14d606040a4c90deb09e827d25b9f2fc068997872",
269 		"55bed2d9c029b7f230bde934c7124ed52b1330856f13cbac65a746f9175f85d7",
270 		"5190277a0c14d8a3d289292f8a544ce6ea9183200e51aec08440e0c1a463a4e4",
271 		"fbcea7c2827e0e8085d7707b23a3728823ea6f4878b24747fb4fd2842d406c73",
272 	};
273 
274 	char *y[] = {
275 		"9681b517b1cda17d0d83d335d9c4a8a9a9b0b1b3c7106d8f3c72bc5093dc275f",
276 		"1f837aa32864870cb8e8d0ac2ff31f824e7beddc4bb7ad72c173ad974b289dc2",
277 		"a26df39960ab5248fd3620fd018398e788bd89a3cea509b352452b69811e6856",
278 		"d7d35bdce6dedc5de98a7ecb27a9cd066a08f586a733b59f5a2cdb54f971d5c8",
279 		"43a047cb20e94b4ffb361ef68952b004c0700b2962e0c0635a70269bc789b849",
280 		"489807ca55bdc29ca5c8fe69b94f227b0345cccdbe89975e75d385cc2f6bb1e2",
281 		"503f138f8bab1df2c4507ff663a1fdf7f710e7adb8e7841eaa902703e314e793",
282 		"32805e311d583b4e007c40668185e85323948e21912b6b0d2cda8557389ae7b0",
283 		"ecd98514821bd5aaf3419ab79b71780569470e4fed3da3c1353b28fe137f36eb",
284 		"2393c85f1f710c5afc115a39ba7e18abe03f19c9d4bb3d47d19468b818efa535",
285 	};
286 
287 	TC_PRINT("Test #2: ECC KeyGen ");
288 	TC_PRINT("NIST-p256\n");
289 
290 	result = keygen_vectors(d, x, y, 10, verbose);
291 
292 	/**TESTPOINT: Check result*/
293 	zassert_false(result, "ECC KeyGen test failed");
294 	return result;
295 }
296 
297 /* Test ecc_make_keys, and also as keygen part of other tests */
pkv_vectors(char ** qx_vec,char ** qy_vec,int res_vec[],int tests,bool verbose)298 int pkv_vectors(char **qx_vec, char **qy_vec, int res_vec[], int tests,
299 		bool verbose)
300 {
301 
302 	unsigned int pub[2 * NUM_ECC_WORDS];
303 	uint8_t _public[2 * NUM_ECC_BYTES];
304 	int rc;
305 	int exp_rc;
306 	unsigned int result = TC_PASS;
307 	const struct uECC_Curve_t *curve = uECC_secp256r1();
308 
309 	for (int i = 0; i < tests; i++) {
310 		exp_rc = res_vec[i];
311 
312 		if (strlen(qx_vec[i]) > 2 * NUM_ECC_BYTES ||
313 		    strlen(qy_vec[i]) > 2 * NUM_ECC_BYTES) {
314 			/* invalid input to ECC digit conversion (string2native()) */
315 			rc = -2;
316 		} else {
317 			string2scalar(pub, NUM_ECC_WORDS, qx_vec[i]);
318 			string2scalar(pub + NUM_ECC_WORDS, NUM_ECC_WORDS, qy_vec[i]);
319 
320 			uECC_vli_nativeToBytes(_public, NUM_ECC_BYTES, pub);
321 			uECC_vli_nativeToBytes(_public + NUM_ECC_BYTES,
322 					       NUM_ECC_BYTES, pub + NUM_ECC_WORDS);
323 
324 			rc = uECC_valid_public_key(_public, curve);
325 		}
326 
327 		/*
328 		 * map to CAVP error codes
329 		 *  0 => 0 - success
330 		 * -1 => ? - (x,y) = (0,0) (not covered)
331 		 * -2 => 1 - out of bounds (pubverify or ecc import)
332 		 * -3 => 2 - not on curve
333 		 * -4 => ? - public key is the group generator
334 		 */
335 
336 		if (rc == -3) {
337 			rc = 2;
338 		}
339 		if (rc == -2) {
340 			rc = 1;
341 		}
342 
343 		result = check_code(i, exp_rc, rc, verbose);
344 
345 		/**TESTPOINT: Check result*/
346 		zassert_false(result, "PubKey verification failed");
347 	}
348 		return result;
349 }
350 
cavp_pkv(bool verbose)351 int cavp_pkv(bool verbose)
352 {
353 	/*
354 	 * [P-256]
355 	 */
356 	char *x[] = {
357 		"e0f7449c5588f24492c338f2bc8f7865f755b958d48edb0f2d0056e50c3fd5b7",
358 		"d17c446237d9df87266ba3a91ff27f45abfdcb77bfd83536e92903efb861a9a9",
359 		"17875397ae87369365656d490e8ce956911bd97607f2aff41b56f6f3a61989826",
360 		"f2d1c0dc0852c3d8a2a2500a23a44813ccce1ac4e58444175b440469ffc12273",
361 		"10b0ca230fff7c04768f4b3d5c75fa9f6c539bea644dffbec5dc796a213061b58",
362 		"2c1052f25360a15062d204a056274e93cbe8fc4c4e9b9561134ad5c15ce525da",
363 		"a40d077a87dae157d93dcccf3fe3aca9c6479a75aa2669509d2ef05c7de6782f",
364 		"2633d398a3807b1895548adbb0ea2495ef4b930f91054891030817df87d4ac0a",
365 		"14bf57f76c260b51ec6bbc72dbd49f02a56eaed070b774dc4bad75a54653c3d56",
366 		"2fa74931ae816b426f484180e517f5050c92decfc8daf756cd91f54d51b302f1",
367 		"f8c6dd3181a76aa0e36c2790bba47041acbe7b1e473ff71eee39a824dc595ff0",
368 		"7a81a7e0b015252928d8b36e4ca37e92fdc328eb25c774b4f872693028c4be38",
369 	};
370 
371 	char *y[] = {
372 		"86d7e9255d0f4b6f44fa2cd6f8ba3c0aa828321d6d8cc430ca6284ce1d5b43a0",
373 		"1eabb6a349ce2cd447d777b6739c5fc066add2002d2029052c408d0701066231c",
374 		"980a3c4f61b9692633fbba5ef04c9cb546dd05cdec9fa8428b8849670e2fba92",
375 		"32bfe992831b305d8c37b9672df5d29fcb5c29b4a40534683e3ace23d24647dd",
376 		"f5edf37c11052b75f771b7f9fa050e353e464221fec916684ed45b6fead38205",
377 		"ced9783713a8a2a09eff366987639c625753295d9a85d0f5325e32dedbcada0b",
378 		"503d86b87d743ba20804fd7e7884aa017414a7b5b5963e0d46e3a9611419ddf3",
379 		"d6b2f738e3873cc8364a2d364038ce7d0798bb092e3dd77cbdae7c263ba618d2",
380 		"7a231a23bf8b3aa31d9600d888a0678677a30e573decd3dc56b33f365cc11236",
381 		"5b994346137988c58c14ae2152ac2f6ad96d97decb33099bd8a0210114cd1141",
382 		"9c965f227f281b3072b95b8daf29e88b35284f3574462e268e529bbdc50e9e52",
383 		"08862f7335147261e7b1c3d055f9a316e4cab7daf99cc09d1c647f5dd6e7d5bb",
384 	};
385 
386 	int results[] = { 0, 1, 1, 2, 1, 0, 2, 0, 1, 0, 2, 2 };
387 
388 	TC_PRINT("Test #3: PubKeyVerify ");
389 	TC_PRINT("NIST-p256-SHA2-256\n");
390 
391 	return pkv_vectors(x, y, results, 12, verbose);
392 }
393 
montecarlo_ecdh(int num_tests,bool verbose)394 int montecarlo_ecdh(int num_tests, bool verbose)
395 {
396 	int i;
397 	uint8_t private1[NUM_ECC_BYTES] = { 0 };
398 	uint8_t private2[NUM_ECC_BYTES] = { 0 };
399 	uint8_t public1[2 * NUM_ECC_BYTES] = { 0 };
400 	uint8_t public2[2 * NUM_ECC_BYTES] = { 0 };
401 	uint8_t secret1[NUM_ECC_BYTES] = { 0 };
402 	uint8_t secret2[NUM_ECC_BYTES] = { 0 };
403 
404 	const struct uECC_Curve_t *curve = uECC_secp256r1();
405 
406 	TC_PRINT("Test #4: Monte Carlo (%d Randomized EC-DH key-exchange) ", num_tests);
407 	TC_PRINT("NIST-p256\n  ");
408 
409 	for (i = 0; i < num_tests; ++i) {
410 		if (verbose) {
411 			TC_PRINT(".");
412 		}
413 
414 		if (!uECC_make_key(public1, private1, curve) ||
415 		    !uECC_make_key(public2, private2, curve)) {
416 			zassert_true(0, "uECC_make_key() failed");
417 		}
418 
419 		if (!uECC_shared_secret(public2, private1, secret1, curve)) {
420 			zassert_true(0, "shared_secret() failed (1)");
421 		}
422 
423 		if (!uECC_shared_secret(public1, private2, secret2, curve)) {
424 			zassert_true(0, "shared_secret() failed (2)");
425 		}
426 
427 		if (memcmp(secret1, secret2, sizeof(secret1)) != 0) {
428 			TC_PRINT("Shared secrets are not identical!\n");
429 			TC_PRINT("Private key 1 = ");
430 			vli_print_bytes(private1, 32);
431 			TC_PRINT("\nPrivate key 2 = ");
432 			vli_print_bytes(private2, 32);
433 			TC_PRINT("\nPublic key 1 = ");
434 			vli_print_bytes(public1, 64);
435 			TC_PRINT("\nPublic key 2 = ");
436 			vli_print_bytes(public2, 64);
437 			TC_PRINT("\nShared secret 1 = ");
438 			vli_print_bytes(secret1, 32);
439 			TC_PRINT("\nShared secret 2 = ");
440 			vli_print_bytes(secret2, 32);
441 			TC_PRINT("\n");
442 		}
443 	}
444 
445 	TC_PRINT("\n");
446 	return TC_PASS;
447 }
448 
ZTEST(tinycrypt,test_ecc_dh)449 ZTEST(tinycrypt, test_ecc_dh)
450 {
451 	unsigned int result = TC_PASS;
452 
453 
454 	/* Setup of the Cryptographically Secure PRNG. */
455 	uECC_set_rng(&default_CSPRNG);
456 
457 	bool verbose = true;
458 
459 	TC_PRINT("Performing cavp_ecdh test:\n");
460 	result = cavp_ecdh(verbose);
461 
462 	/**TESTPOINT: Check cavp_ecdh*/
463 	zassert_false(result, "cavp_ecdh test failed");
464 
465 	TC_PRINT("Performing cavp_keygen test:\n");
466 	result = cavp_keygen(verbose);
467 
468 	/**TESTPOINT: Check cavp_keygen*/
469 	zassert_false(result, "cavp_keygen test failed");
470 
471 	TC_PRINT("Performing cavp_pkv test:\n");
472 	result = cavp_pkv(verbose);
473 
474 	/**TESTPOINT: Check cavp_pkv*/
475 	zassert_false(result, "cavp_pkv test failed");
476 
477 	TC_PRINT("Performing montecarlo_ecdh test:\n");
478 	result = montecarlo_ecdh(10, verbose);
479 
480 	/**TESTPOINT: Check cavp_ecdh*/
481 	zassert_false(result, "montecarlo_ecdh test failed");
482 
483 	TC_PRINT("All EC-DH tests succeeded!\n");
484 }
485