blob: 698058f01a4560c8a727a52c2a7d0cb991eb7f29 [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
7/* END_HEADER */
8
9/* BEGIN_DEPENDENCIES
10 * depends_on:MBEDTLS_USE_TINYCRYPT
11 * END_DEPENDENCIES
12 */
13
14/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
15void test_ecdh()
16{
17 uint8_t private1[NUM_ECC_BYTES] = {0};
18 uint8_t private2[NUM_ECC_BYTES] = {0};
19 uint8_t public1[2*NUM_ECC_BYTES] = {0};
20 uint8_t public2[2*NUM_ECC_BYTES] = {0};
21 uint8_t secret1[NUM_ECC_BYTES] = {0};
22 uint8_t secret2[NUM_ECC_BYTES] = {0};
23
24 const struct uECC_Curve_t * curve = uECC_secp256r1();
25
26 TEST_ASSERT( uECC_make_key( public1, private1, curve ) != 0 );
27 TEST_ASSERT( uECC_make_key( public2, private2, curve ) != 0 );
28
29 TEST_ASSERT( uECC_shared_secret( public2, private1, secret1, curve ) != 0 );
30
31 TEST_ASSERT( uECC_shared_secret( public1, private2, secret2, curve ) != 0 );
32
33 TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 );
34}
35/* END_CASE */
Jarno Lamsa6c2f76e2019-08-26 13:34:45 +030036
37/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */
38void test_ecdsa()
39{
40 uint8_t private[NUM_ECC_BYTES] = {0};
41 uint8_t public[2*NUM_ECC_BYTES] = {0};
42 uint8_t hash[NUM_ECC_BYTES] = {0};
43 uint8_t sig[2*NUM_ECC_BYTES] = {0};
44 unsigned int hash_words[NUM_ECC_WORDS] = {0};
45
46 const struct uECC_Curve_t * curve = uECC_secp256r1();
47
48 uECC_generate_random_int( hash_words, curve->n,
49 BITS_TO_WORDS( curve->num_n_bits ) );
50
51 uECC_vli_nativeToBytes( hash, NUM_ECC_BYTES, hash_words );
52
53 TEST_ASSERT( uECC_make_key( public, private, curve ) != 0 );
54
55 TEST_ASSERT( uECC_sign( private, hash, sizeof( hash ), sig, curve ) != 0 );
56
57 TEST_ASSERT( uECC_verify( public, hash, sizeof( hash ), sig, curve ) != 0 );
58}
59/* END_CASE */