blob: 2cf656616764bae7af928ed2f2df2abc16b77648 [file] [log] [blame]
Jarno Lamsa7c5dc6b2019-08-26 13:12:35 +03001/* BEGIN_HEADER */
2
3#include "tinycrypt/ecc.h"
4#include "tinycrypt/ecc_dh.h"
5#include "tinycrypt/ecc_dsa.h"
6
Jarno Lamsa34fcbfe2019-08-26 14:37:33 +03007static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
8{
9 int ret;
10 ret = rnd_std_rand( NULL, dest, size );
11 if( ret == 0 )
12 return( (int) size );
13
14 return( 0 );
15}
16
Jarno Lamsa7c5dc6b2019-08-26 13:12:35 +030017/* END_HEADER */
18
19/* BEGIN_DEPENDENCIES
20 * depends_on:MBEDTLS_USE_TINYCRYPT
21 * END_DEPENDENCIES
22 */
23
24/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
25void test_ecdh()
26{
27 uint8_t private1[NUM_ECC_BYTES] = {0};
28 uint8_t private2[NUM_ECC_BYTES] = {0};
29 uint8_t public1[2*NUM_ECC_BYTES] = {0};
30 uint8_t public2[2*NUM_ECC_BYTES] = {0};
31 uint8_t secret1[NUM_ECC_BYTES] = {0};
32 uint8_t secret2[NUM_ECC_BYTES] = {0};
33
34 const struct uECC_Curve_t * curve = uECC_secp256r1();
35
Jarno Lamsa34fcbfe2019-08-26 14:37:33 +030036 uECC_set_rng( &uecc_rng_wrapper );
37
Jarno Lamsa7c5dc6b2019-08-26 13:12:35 +030038 TEST_ASSERT( uECC_make_key( public1, private1, curve ) != 0 );
Jarno Lamsa34fcbfe2019-08-26 14:37:33 +030039
Jarno Lamsa7c5dc6b2019-08-26 13:12:35 +030040 TEST_ASSERT( uECC_make_key( public2, private2, curve ) != 0 );
41
42 TEST_ASSERT( uECC_shared_secret( public2, private1, secret1, curve ) != 0 );
43
44 TEST_ASSERT( uECC_shared_secret( public1, private2, secret2, curve ) != 0 );
45
46 TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 );
47}
48/* END_CASE */
Jarno Lamsa6c2f76e2019-08-26 13:34:45 +030049
50/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
51void test_ecdsa()
52{
53 uint8_t private[NUM_ECC_BYTES] = {0};
54 uint8_t public[2*NUM_ECC_BYTES] = {0};
55 uint8_t hash[NUM_ECC_BYTES] = {0};
56 uint8_t sig[2*NUM_ECC_BYTES] = {0};
Jarno Lamsa6c2f76e2019-08-26 13:34:45 +030057
58 const struct uECC_Curve_t * curve = uECC_secp256r1();
59
Jarno Lamsa34fcbfe2019-08-26 14:37:33 +030060 uECC_set_rng( &uecc_rng_wrapper );
61
Jarno Lamsaf35f35b2019-09-02 15:36:49 +030062 TEST_ASSERT( rnd_std_rand( NULL, hash, NUM_ECC_BYTES ) == 0 );
Jarno Lamsa6c2f76e2019-08-26 13:34:45 +030063
64 TEST_ASSERT( uECC_make_key( public, private, curve ) != 0 );
65
66 TEST_ASSERT( uECC_sign( private, hash, sizeof( hash ), sig, curve ) != 0 );
67
68 TEST_ASSERT( uECC_verify( public, hash, sizeof( hash ), sig, curve ) != 0 );
69}
Jarno Lamsaa7e0f632019-09-02 09:47:37 +030070/* END_CASE */
71
72/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
73void ecdh_primitive_testvec( data_t * private1, data_t * xA_str,
74 data_t * yA_str, data_t * private2,
75 data_t * xB_str, data_t * yB_str, data_t * z_str )
76{
77 const struct uECC_Curve_t * curve = uECC_secp256r1();
78 uint8_t public1[2*NUM_ECC_BYTES] = {0};
79 uint8_t public2[2*NUM_ECC_BYTES] = {0};
80 uint8_t secret1[NUM_ECC_BYTES] = {0};
81 uint8_t secret2[NUM_ECC_BYTES] = {0};
82
83 memcpy( public1, xA_str->x, xA_str->len );
84 memcpy( public1 + NUM_ECC_BYTES, yA_str->x, yA_str->len );
85 memcpy( public2, xB_str->x, xB_str->len );
86 memcpy( public2 + NUM_ECC_BYTES, yB_str->x, yB_str->len );
87
88 // Compute shared secrets and compare to test vector secret
89 TEST_ASSERT( uECC_shared_secret( public2, private1->x, secret1, curve ) != 0 );
90
91 TEST_ASSERT( uECC_shared_secret( public1, private2->x, secret2, curve ) != 0 );
92
93 TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 );
94 TEST_ASSERT( memcmp( secret1, z_str->x, sizeof( secret1 ) ) == 0 );
95 TEST_ASSERT( memcmp( secret2, z_str->x, sizeof( secret2 ) ) == 0 );
96}
97/* END_CASE */
98
99/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
100void ecdsa_primitive_testvec( data_t * xQ_str, data_t * yQ_str,
101 data_t * hash, data_t * r_str, data_t * s_str,
102 int result )
103{
104 const struct uECC_Curve_t * curve = uECC_secp256r1();
105 uint8_t pub_bytes[2*NUM_ECC_BYTES] = {0};
106 uint8_t sig_bytes[2*NUM_ECC_BYTES] = {0};
107
108 memcpy( pub_bytes, xQ_str->x, xQ_str->len );
109 memcpy( pub_bytes + NUM_ECC_BYTES, yQ_str->x, yQ_str->len );
110 memcpy( sig_bytes, r_str->x, r_str->len );
111 memcpy( sig_bytes + NUM_ECC_BYTES, s_str->x, r_str->len );
112
113 TEST_ASSERT( uECC_verify( pub_bytes, hash->x, hash->len,
114 sig_bytes, curve ) == result );
115
116 // Alter the signature and check the verification fails
117 for( int i = 0; i < 2*NUM_ECC_BYTES; i++ )
118 {
119 uint8_t temp = sig_bytes[i];
120 sig_bytes[i] = ( sig_bytes[i] + 1 ) % 256;
121 TEST_ASSERT( uECC_verify( pub_bytes, hash->x, hash->len,
122 sig_bytes, curve ) == 0 );
123 sig_bytes[i] = temp;
124 }
125
126}
127/* END_CASE */