Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
yubihsm_shell_tests.c File Reference
#include <stdlib.h>
#include <yubihsm.h>
#include <sys/time.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
Include dependency graph for yubihsm_shell_tests.c:

Go to the source code of this file.

Classes

struct  repetition_stats
 

Macros

#define EXIT_SKIPPED   77;
 
#define DEFAULT_KEY    "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
 
#define N_REPS    8
 
#define N_ITERATIONS   1000
 
#define ECHO_BYTE   0x0f
 

Functions

int main ()
 

Variables

yh_device ** devices
 
uint16_t n_devices
 
yh_sessionses
 

Macro Definition Documentation

◆ DEFAULT_KEY

#define DEFAULT_KEY    "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"

Definition at line 37 of file yubihsm_shell_tests.c.

37#define DEFAULT_KEY \
38 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"

◆ ECHO_BYTE

#define ECHO_BYTE   0x0f

Definition at line 43 of file yubihsm_shell_tests.c.

◆ EXIT_SKIPPED

#define EXIT_SKIPPED   77;

Definition at line 24 of file yubihsm_shell_tests.c.

◆ N_ITERATIONS

#define N_ITERATIONS   1000

Definition at line 42 of file yubihsm_shell_tests.c.

◆ N_REPS

#define N_REPS    8

Definition at line 40 of file yubihsm_shell_tests.c.

40#define N_REPS \
41 8 // Data length for repetition i is 2^(1 + i) for i in [0, N_REPS-1]

Function Documentation

◆ main()

int main ( void )

Definition at line 45 of file yubihsm_shell_tests.c.

45 {
46
47 yh_rc yrc;
48
49 uint8_t key_enc[YH_KEY_LEN];
50 uint8_t key_mac[YH_KEY_LEN];
51 uint8_t key_dek[YH_KEY_LEN];
52
53 uint8_t host_challenge[YH_HOST_CHAL_LEN] = {0};
54
55 uint8_t yh_context[YH_CONTEXT_LEN];
56
57 struct timeval before, after;
58 double this;
60
62 uint8_t response[YH_MSG_BUF_SIZE];
63 uint16_t response_len;
65
66 uint8_t verbosity = 0; // YH_VERB_USB | YH_VERB_CRYPTO | YH_VERB_INTERMEDIATE
67 // | YH_VERB_INFO | YH_VERB_ERR;
68
70 if (yrc != YH_SUCCESS) {
71 fprintf(stderr, "Unable to initialize\n");
72 fprintf(stderr, "(Typically this happens if you don't have permissions to "
73 "open the device\n");
74 fprintf(stderr, "try re-running as root/Administrator)\n");
75 return EXIT_SKIPPED;
76 }
77
78 if (n_devices == 0) {
79 fprintf(stderr, "No suitable device found. Skipping this test\n");
80 return EXIT_SKIPPED;
81 }
82
83 yh_set_verbosity(verbosity);
84
85 memset(data, ECHO_BYTE, 1024);
86
87 // Plain commands
88 len = 4;
89 for (uint16_t rep = 0; rep < N_REPS; rep++) {
90 len *= 2;
91 rs[rep].len = len;
92 fprintf(stderr, "now doing plain with len %d\n", len);
93 for (uint16_t i = 0; i < N_ITERATIONS; i++) {
94 response_len = sizeof(response);
95
96 gettimeofday(&before, NULL);
97 yrc = yh_send_plain_msg(devices[0], YHC_ECHO, data, rs[rep].len, response,
98 &response_len);
99 gettimeofday(&after, NULL);
100
101 this = (after.tv_sec - before.tv_sec) * 1000;
102 this += ((double) (after.tv_usec - before.tv_usec)) / 1000;
103 if (i == 0) {
104 rs[rep].max = rs[rep].min = this;
105 rs[rep].tot = 0;
106 } else {
107 if (this > rs[rep].max) {
108 rs[rep].max = this;
109 }
110 if (this < rs[rep].min) {
111 rs[rep].min = this;
112 }
113 }
114
115 rs[rep].tot += this;
116
117 if (rs[rep].len != response_len ||
118 memcmp(data, response, rs[rep].len) != 0) {
119 fprintf(stderr, "Data mismatch\n");
120 return EXIT_FAILURE;
121 }
122 }
123 }
124
125 // Auth commands
126 memcpy(key_enc, DEFAULT_KEY, YH_KEY_LEN); // TODO: fix
127 memcpy(key_mac, DEFAULT_KEY, YH_KEY_LEN); // TODO: fix
128 memcpy(key_dek, DEFAULT_KEY, YH_KEY_LEN); // TODO: fix
129
130 yrc = yh_open_device(devices[0]);
131 if (yrc != YH_SUCCESS) {
132 fprintf(stderr, "Unable to open device: %s\n", yh_strerror(yrc));
133 return EXIT_FAILURE;
134 }
135
136 yrc = yh_create_session(devices[0], 0, host_challenge, key_enc, 16, key_mac,
137 16, yh_context, &ses);
138 if (yrc != YH_SUCCESS) {
139 fprintf(stderr, "Failed to create session: %s\n", yh_strerror(yrc));
140 return EXIT_FAILURE;
141 }
142
143 yrc = yh_authenticate_session(ses, yh_context);
144 if (yrc != YH_SUCCESS) {
145 fprintf(stderr, "Failed to create session: %d, %s\n", yrc,
147 return EXIT_FAILURE;
148 }
149
150 len = 4;
151 for (uint16_t rep = 0; rep < N_REPS; rep++) {
152 len *= 2;
153 rs2[rep].len = len;
154 fprintf(stderr, "now doing auth with len %d\n", len);
155 for (uint16_t i = 0; i < N_ITERATIONS; i++) {
156 response_len = sizeof(response);
157
158 gettimeofday(&before, NULL);
159 yrc = yh_send_secure_msg(ses, YHC_ECHO, data, rs2[rep].len, response,
160 &response_len);
161 gettimeofday(&after, NULL);
162 if (yrc != YH_SUCCESS) {
163 fprintf(stderr, "Failed to send message: %d, %s\n", yrc,
165 return EXIT_FAILURE;
166 }
167
168 this = (after.tv_sec - before.tv_sec) * 1000;
169 this += ((double) (after.tv_usec - before.tv_usec)) / 1000;
170 if (i == 0) {
171 rs2[rep].max = rs2[rep].min = this;
172 rs2[rep].tot = 0;
173 } else {
174 if (this > rs2[rep].max) {
175 rs2[rep].max = this;
176 }
177 if (this < rs2[rep].min) {
178 rs2[rep].min = this;
179 }
180 }
181
182 rs2[rep].tot += this;
183
184 if (rs2[rep].len != response_len ||
185 memcmp(data, response, rs2[rep].len) != 0) {
186 fprintf(stderr, "Data mismatch\n");
187 return EXIT_FAILURE;
188 }
189 }
190 }
191
193
194 fprintf(stdout, "Iterations %d\n", N_ITERATIONS);
195 fprintf(stdout, "| Len | min [ms] | max [ms] | avg [ms] | type |\n");
196 fprintf(stdout, "---------------------------------------------------\n");
197 for (uint16_t i = 0; i < N_REPS; i++) {
198 fprintf(stdout, "| %6d | %8.02f | %8.02f | %8.02f | plain |\n", rs[i].len,
199 rs[i].min, rs[i].max, rs[i].tot / N_ITERATIONS);
200 }
201 for (uint16_t i = 0; i < N_REPS; i++) {
202 fprintf(stdout, "| %6d | %8.02f | %8.02f | %8.02f | auth |\n", rs2[i].len,
203 rs2[i].min, rs2[i].max, rs2[i].tot / N_ITERATIONS);
204 }
205
206 return EXIT_SUCCESS;
207}
const char * yh_strerror(yh_rc err)
Definition error.c:65
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
void rep()
yh_rc yh_create_session(yh_connector *connector, uint16_t authkey_id, const uint8_t *key_enc, size_t key_enc_len, const uint8_t *key_mac, size_t key_mac_len, bool recreate, yh_session **session)
Definition yubihsm.c:616
yh_rc yh_set_verbosity(yh_connector *connector, uint8_t verbosity)
Definition yubihsm.c:3825
yh_rc yh_exit(void)
Definition yubihsm.c:3910
yh_rc yh_send_secure_msg(yh_session *session, yh_cmd cmd, const uint8_t *data, size_t data_len, yh_cmd *response_cmd, uint8_t *response, size_t *response_len)
Definition yubihsm.c:416
yh_rc yh_init(void)
Definition yubihsm.c:3857
yh_rc yh_authenticate_session(yh_session *session)
Definition yubihsm.c:2927
yh_rc yh_send_plain_msg(yh_connector *connector, yh_cmd cmd, const uint8_t *data, size_t data_len, yh_cmd *response_cmd, uint8_t *response, size_t *response_len)
Definition yubihsm.c:126
#define YH_HOST_CHAL_LEN
Length of host challenge for authentication.
Definition yubihsm.h:91
#define YH_MSG_BUF_SIZE
Maximum length of message buffer.
Definition yubihsm.h:93
#define YH_KEY_LEN
Length of authentication keys.
Definition yubihsm.h:95
yh_rc
Definition yubihsm.h:170
#define YH_CONTEXT_LEN
Length of context array for authentication.
Definition yubihsm.h:89
size_t len
yh_rc yrc
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)
yh_session * ses
uint16_t n_devices
#define EXIT_SKIPPED
#define N_ITERATIONS
yh_device ** devices
#define DEFAULT_KEY
#define N_REPS
#define ECHO_BYTE
Here is the call graph for this function:

Variable Documentation

◆ devices

yh_device** devices

Definition at line 26 of file yubihsm_shell_tests.c.

◆ n_devices

uint16_t n_devices

Definition at line 27 of file yubihsm_shell_tests.c.

◆ ses

yh_session* ses

Definition at line 28 of file yubihsm_shell_tests.c.