a752c7ab
elopes
add first test an...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* Copyright (C) 2014 Tobias Markmann <tm@ayena.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#define TEST_RELIC_SHOW_OUTPUT (0) /**< set if encoded/decoded string is displayed */
#if (TEST_RELIC_SHOW_OUTPUT == 1)
#include <stdio.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include "relic.h"
#include "embUnit.h"
void print_mem(void *mem, int len) {
int i;
unsigned char *p = (unsigned char *)mem;
for (i=0;i<len;i++) {
printf("0x%02x ", p[i]);
}
printf("\n");
}
static void setUp(void)
{
/* Initialize RELIC */
TEST_ASSERT_EQUAL_INT(STS_OK, core_init());
}
static void tearDown(void)
{
/* Finalize RELIC */
core_clean();
}
static void tests_relic_ecdh(void)
{
/* The following is an example for doing an elliptic-curve Diffie-Hellman
key exchange.
*/
/* Select an elliptic curve configuration */
if (ec_param_set_any() == STS_OK) {
#if (TEST_RELIC_SHOW_OUTPUT == 1)
ec_param_print();
#endif
bn_t privateA;
ec_t publicA;
uint8_t sharedKeyA[MD_LEN];
bn_t privateB;
ec_t publicB;
uint8_t sharedKeyB[MD_LEN];
bn_null(privateA);
ec_null(publicA);
bn_new(privateA);
ec_new(publicA);
bn_null(privateB);
ec_null(publicB);
bn_new(privateB);
ec_new(publicB);
/* User A generates private/public key pair */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_gen(privateA, publicA));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("User A\n");
printf("======\n");
printf("private key: ");
bn_print(privateA);
printf("\npublic key: ");
ec_print(publicA);
printf("\n");
#endif
/* User B generates private/public key pair */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_gen(privateB, publicB));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("User B\n");
printf("======\n");
printf("private key: ");
bn_print(privateB);
printf("\npublic key: ");
ec_print(publicB);
printf("\n");
#endif
/* In a protocol you would exchange the public keys now */
/* User A calculates shared secret */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_key(sharedKeyA, MD_LEN, privateA, publicB));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("\nshared key computed by user A: ");
print_mem(sharedKeyA, MD_LEN);
#endif
/* User B calculates shared secret */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_key(sharedKeyB, MD_LEN, privateB, publicA));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("\nshared key computed by user B: ");
print_mem(sharedKeyB, MD_LEN);
#endif
/* The secrets should be the same now */
TEST_ASSERT_EQUAL_INT(CMP_EQ, util_cmp_const(sharedKeyA, sharedKeyB, MD_LEN));
bn_free(privateA);
ec_free(publicA);
bn_free(privateB);
ec_free(publicB);
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("\nRELIC EC-DH test successful\n");
#endif
}
}
TestRef tests_relic_all(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(tests_relic_ecdh)
};
EMB_UNIT_TESTCALLER(RELICTest, setUp, tearDown, fixtures);
return (TestRef)&RELICTest;
}
void tests_relic(void)
{
TESTS_RUN(tests_relic_all());
}
|