1 /*  test_ecc_utils.h - TinyCrypt interface to common functions for ECC 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  *  Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
28  *
29  *  Redistribution and use in source and binary forms, with or without
30  *  modification, are permitted provided that the following conditions are met:
31  *
32  *    - Redistributions of source code must retain the above copyright notice,
33  *     this list of conditions and the following disclaimer.
34  *
35  *    - Redistributions in binary form must reproduce the above copyright
36  *    notice, this list of conditions and the following disclaimer in the
37  *    documentation and/or other materials provided with the distribution.
38  *
39  *    - Neither the name of Intel Corporation nor the names of its contributors
40  *    may be used to endorse or promote products derived from this software
41  *    without specific prior written permission.
42  *
43  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53  *  POSSIBILITY OF SUCH DAMAGE.
54  *
55  *  test_ecc_utils.h -- Interface to common functions for ECC tests.
56  */
57 
58 #ifndef __TEST_ECC_UTILS_H__
59 #define __TEST_ECC_UTILS_H__
60 
61 #include <tinycrypt/ecc_dh.h>
62 #include <tinycrypt/ecc.h>
63 #include <test_utils.h>
64 
65 int hex2int (char hex);
66 
67 
68 /*
69  * Convert hex string to byte string
70  * Return number of bytes written to buf, or 0 on error
71  */
72 int hex2bin(uint8_t *buf, const size_t buflen, const char *hex,
73 	    const size_t hexlen);
74 
75 /*
76  * Convert hex string to zero-padded nanoECC scalar
77  */
78 void string2scalar(unsigned int * scalar, unsigned int num_word32, char *str);
79 
80 
81 void print_ecc_scalar(const char *label, const unsigned int * p_vli,
82 		      unsigned int num_word32);
83 
84 int check_ecc_result(const int num, const char *name,
85 		      const unsigned int *expected,
86 		      const unsigned int *computed,
87 		      const unsigned int num_word32, const bool verbose);
88 
89 /* Test ecc_make_keys, and also as keygen part of other tests */
90 int keygen_vectors(char **d_vec, char **qx_vec, char **qy_vec, int tests, bool verbose);
91 
92 void vli_print_bytes(uint8_t *vli, unsigned int size);
93 
94 
95 int check_code(const int num, const char *name, const int expected,
96 		const int computed, const int verbose);
97 
98 
99 #endif /* __TEST_ECC_UTILS_H__ */
100 
101