-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecdsa_optiga.c
More file actions
189 lines (135 loc) · 4.08 KB
/
ecdsa_optiga.c
File metadata and controls
189 lines (135 loc) · 4.08 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
187
188
189
#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 callback_handler_func(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, callback_handler_func, NULL);
optiga_crypt_t* optcreate = optiga_crypt_create(0, callback_handler_func, NULL);
opstatus = OPTIGA_UTIL_BUSY;
optiga_lib_status_t utilstatus = optiga_util_open_application(utilcreate, 0);
wait();
if (utilstatus != OPTIGA_UTIL_SUCCESS){
printf("Open application unsuccessfull\n\r");
return 0;
}
printf("App openedd\n\r");
opstatus = OPTIGA_UTIL_BUSY;
uint8_t publickey[100] = {0};
uint16_t publen = sizeof(publickey);
optiga_ecc_curve_t curveid = OPTIGA_ECC_CURVE_NIST_P_256;
optiga_key_id_t key = OPTIGA_KEY_ID_E0F2;
uint8_t keyusage = OPTIGA_KEY_USAGE_SIGN;
optiga_crypt_ecc_generate_keypair(optcreate, curveid, keyusage, 0, &key, publickey, &publen);
wait();
if (opstatus != 0){
printf("Key generation failed\n\r");
return 0;
}
printf("Key pair generated\n\r");
opstatus = OPTIGA_UTIL_BUSY;
const char * msg = "Hello world ecdsa";
uint32_t msglen= strlen(msg);
uint8_t hash[32];
uint8_t hashlen = sizeof(hash);
optiga_hash_type_t hosttype = OPTIGA_HASH_TYPE_SHA_256;
uint8_t hostdata = OPTIGA_CRYPT_HOST_DATA;
hash_data_from_host_t data;
data.buffer = (const uint8_t *)msg;
data.length = msglen;
optiga_crypt_hash(optcreate, hosttype, hostdata, &data, hash);
wait();
if(opstatus != 0){
printf("Hash unsuccessfull\n\r");
return 0;
}
printf("Hash data: \n\r");
print_uint8_data(hash, 32);
opstatus = OPTIGA_UTIL_BUSY;
uint8_t signature[100] = {0};
uint16_t signlen = sizeof(signature);
optiga_crypt_ecdsa_sign(optcreate, hash, hashlen, key, signature, &signlen);
wait();
if(opstatus != 0){
printf("Sign unsuccessfull\n\r");
return 0;
}
opstatus = OPTIGA_UTIL_BUSY;
printf("Data signed\n\r");
// uint16_t hostdata = OPTIGA_CRYPT_HOST_DATA;
public_key_from_host_t hostpub;
hostpub.public_key = (uint8_t *)publickey;
hostpub.length = publen;
hostpub.key_type = OPTIGA_ECC_CURVE_NIST_P_256;
optiga_crypt_ecdsa_verify(optcreate, hash, hashlen, signature, signlen, hostdata, &hostpub);
wait();
if(opstatus != 0){
printf("Verification unsuccessfull\n\r");
return 0;
}
printf("Verification success\n\r");
opstatus = OPTIGA_UTIL_BUSY;
}
/* [] END OF FILE */