blob: 36e4c66cdb76175138d07df5561dad6d497c89d3 [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};
57 unsigned int hash_words[NUM_ECC_WORDS] = {0};
58
59 const struct uECC_Curve_t * curve = uECC_secp256r1();
60
Jarno Lamsa34fcbfe2019-08-26 14:37:33 +030061 uECC_set_rng( &uecc_rng_wrapper );
62
Jarno Lamsa6c2f76e2019-08-26 13:34:45 +030063 uECC_generate_random_int( hash_words, curve->n,
64 BITS_TO_WORDS( curve->num_n_bits ) );
65
66 uECC_vli_nativeToBytes( hash, NUM_ECC_BYTES, hash_words );
67
68 TEST_ASSERT( uECC_make_key( public, private, curve ) != 0 );
69
70 TEST_ASSERT( uECC_sign( private, hash, sizeof( hash ), sig, curve ) != 0 );
71
72 TEST_ASSERT( uECC_verify( public, hash, sizeof( hash ), sig, curve ) != 0 );
73}
Jarno Lamsaa7e0f632019-09-02 09:47:37 +030074/* END_CASE */
75
76/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
77void ecdh_primitive_testvec( data_t * private1, data_t * xA_str,
78 data_t * yA_str, data_t * private2,
79 data_t * xB_str, data_t * yB_str, data_t * z_str )
80{
81 const struct uECC_Curve_t * curve = uECC_secp256r1();
82 uint8_t public1[2*NUM_ECC_BYTES] = {0};
83 uint8_t public2[2*NUM_ECC_BYTES] = {0};
84 uint8_t secret1[NUM_ECC_BYTES] = {0};
85 uint8_t secret2[NUM_ECC_BYTES] = {0};
86
87 memcpy( public1, xA_str->x, xA_str->len );
88 memcpy( public1 + NUM_ECC_BYTES, yA_str->x, yA_str->len );
89 memcpy( public2, xB_str->x, xB_str->len );
90 memcpy( public2 + NUM_ECC_BYTES, yB_str->x, yB_str->len );
91
92 // Compute shared secrets and compare to test vector secret
93 TEST_ASSERT( uECC_shared_secret( public2, private1->x, secret1, curve ) != 0 );
94
95 TEST_ASSERT( uECC_shared_secret( public1, private2->x, secret2, curve ) != 0 );
96
97 TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 );
98 TEST_ASSERT( memcmp( secret1, z_str->x, sizeof( secret1 ) ) == 0 );
99 TEST_ASSERT( memcmp( secret2, z_str->x, sizeof( secret2 ) ) == 0 );
100}
101/* END_CASE */
102
103/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
104void ecdsa_primitive_testvec( data_t * xQ_str, data_t * yQ_str,
105 data_t * hash, data_t * r_str, data_t * s_str,
106 int result )
107{
108 const struct uECC_Curve_t * curve = uECC_secp256r1();
109 uint8_t pub_bytes[2*NUM_ECC_BYTES] = {0};
110 uint8_t sig_bytes[2*NUM_ECC_BYTES] = {0};
111
112 memcpy( pub_bytes, xQ_str->x, xQ_str->len );
113 memcpy( pub_bytes + NUM_ECC_BYTES, yQ_str->x, yQ_str->len );
114 memcpy( sig_bytes, r_str->x, r_str->len );
115 memcpy( sig_bytes + NUM_ECC_BYTES, s_str->x, r_str->len );
116
117 TEST_ASSERT( uECC_verify( pub_bytes, hash->x, hash->len,
118 sig_bytes, curve ) == result );
119
120 // Alter the signature and check the verification fails
121 for( int i = 0; i < 2*NUM_ECC_BYTES; i++ )
122 {
123 uint8_t temp = sig_bytes[i];
124 sig_bytes[i] = ( sig_bytes[i] + 1 ) % 256;
125 TEST_ASSERT( uECC_verify( pub_bytes, hash->x, hash->len,
126 sig_bytes, curve ) == 0 );
127 sig_bytes[i] = temp;
128 }
129
130}
131/* END_CASE */