-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecdh_optiga.c
More file actions
186 lines (136 loc) · 4.23 KB
/
ecdh_optiga.c
File metadata and controls
186 lines (136 loc) · 4.23 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#if defined (CY_USING_HAL)
#include "cyhal.h"
#endif
#include "cybsp.h"
#include "cy_retarget_io.h"
#include "ecp.h"
#include "optiga_util.h"
#include "optiga_crypt.h"
#include "optiga_lib_common.h"
void print_uint8_data(uint8_t* data, size_t len)
{
char print[10];
for (uint8_t i=0; i < len; i++)
{
if ((i % 16) == 0)
{
printf("\r\n");
}
sprintf(print,"0x%02X ", *(data+i));
printf("%s", print);
}
printf("\r\n");
}
void print_ecp_point_data(mbedtls_ecp_point* data, mbedtls_ecp_group *grp)
{
unsigned char buffer[100] = {0};
size_t buflen = 0; //ECP_KEY_LENGTH
mbedtls_ecp_point_write_binary(grp, data, MBEDTLS_ECP_PF_UNCOMPRESSED,
&buflen, buffer, sizeof(buffer));
print_uint8_data(buffer, buflen);
}
void print_mpi_data(mbedtls_mpi* data)
{
size_t len = mbedtls_mpi_size(data);
unsigned char buffer[100] = {0};
mbedtls_mpi_write_binary(data, buffer, len);
print_uint8_data(buffer, len);
}
optiga_lib_status_t opstatus = OPTIGA_UTIL_BUSY;
void handlerFunc(void *callback_ctx, optiga_lib_status_t event){
opstatus = event;
}
void wait(){
while (opstatus == OPTIGA_UTIL_BUSY);
}
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io to use the debug UART port */
result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE);
/* UART port init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("PSOC_OPTIGA_IOT_KIT template is ready to start.\r\n");
optiga_util_t* utilcreate = optiga_util_create(0, handlerFunc, NULL);
opstatus = OPTIGA_UTIL_BUSY;
optiga_util_open_application(utilcreate, 0);
wait();
if (opstatus != 0){
printf("Utill open unsuccessfull\n\r");
return 0;
}
optiga_crypt_t* opcreate = optiga_crypt_create(0, handlerFunc, NULL);
opstatus = OPTIGA_UTIL_BUSY;
uint8_t alicepubkey[100] = {0};
uint16_t alicepublen = sizeof(alicepubkey);
optiga_key_id_t aliceprivate = OPTIGA_KEY_ID_E0F2;
optiga_crypt_ecc_generate_keypair(opcreate, OPTIGA_ECC_CURVE_NIST_P_256, OPTIGA_KEY_USAGE_KEY_AGREEMENT, 0, &aliceprivate, &alicepubkey, &alicepublen);
wait();
if (opstatus != 0){
printf("Alice key generation unsuccessucful\n\r");
return 0;
}
printf("Alice successfull key generation\n\r");
opstatus = OPTIGA_UTIL_BUSY;
uint8_t bobpubkey[100] = {0};
uint16_t bobpublen = sizeof(bobpubkey);
optiga_key_id_t bobprivate = OPTIGA_KEY_ID_E0F3;
optiga_crypt_ecc_generate_keypair(opcreate, OPTIGA_ECC_CURVE_NIST_P_256, OPTIGA_KEY_USAGE_KEY_AGREEMENT, 0, &bobprivate, &bobpubkey, &bobpublen);
wait();
if (opstatus != 0){
printf("Bob key generation unsuccessucful\n\r");
return 0;
}
printf("Bob successfull key generation\n\r");
opstatus = OPTIGA_UTIL_BUSY;
public_key_from_host_t bob;
bob.public_key = bobpubkey;
bob.length = bobpublen;
bob.key_type = OPTIGA_ECC_CURVE_NIST_P_256;
uint8_t alicesharekey[32] = {0};
optiga_crypt_ecdh(opcreate, aliceprivate, &bob, 1, &alicesharekey);
wait();
if (opstatus != 0){
printf("Alice shared key generation unsuccessucful\n\r");
return 0;
}
printf("Alice successfull shared key generation\n\r");
opstatus = OPTIGA_UTIL_BUSY;
public_key_from_host_t alice;
alice.public_key = alicepubkey;
alice.length = alicepublen;
alice.key_type = OPTIGA_ECC_CURVE_NIST_P_256;
uint8_t bobsharekey[32] = {0};
optiga_crypt_ecdh(opcreate, bobprivate, &alice, 1, &bobsharekey);
wait();
if (opstatus != 0){
printf("Bob shared key generation unsuccessucful\n\r");
return 0;
}
printf("Bob successfull shared key generation\n\r");
opstatus = OPTIGA_UTIL_BUSY;
if (memcmp(alicesharekey, bobsharekey, 32) == 0) {
printf("Shared keys match!\r\n");
} else {
printf("Shared keys DO NOT match!\r\n");
}
printf("Shared Key: \n\r");
print_uint8_data(alicesharekey, 32);
print_uint8_data(bobsharekey, 32);
}
/* [] END OF FILE */