Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
generate_ec.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/evp.h>
#include "util.h"
#include <yubihsm.h>
Include dependency graph for generate_ec.c:

Go to the source code of this file.

Macros

#define DEFAULT_CONNECTOR_URL   "http://127.0.0.1:12345"
 

Functions

int main (void)
 

Variables

const char * key_label = "label"
 
const uint8_t password [] = "password"
 
const uint8_t data [] = "sudo make me a sandwich"
 

Macro Definition Documentation

◆ DEFAULT_CONNECTOR_URL

#define DEFAULT_CONNECTOR_URL   "http://127.0.0.1:12345"

Definition at line 36 of file generate_ec.c.

Function Documentation

◆ main()

int main ( void )

Definition at line 43 of file generate_ec.c.

43 {
44 yh_connector *connector = NULL;
45 yh_session *session = NULL;
47
48 uint16_t authkey = 1;
49
50 const char *connector_url;
51
52 connector_url = getenv("DEFAULT_CONNECTOR_URL");
53 if (connector_url == NULL) {
54 connector_url = DEFAULT_CONNECTOR_URL;
55 }
56
57 yrc = yh_init();
58 assert(yrc == YHR_SUCCESS);
59
60 yrc = yh_init_connector(connector_url, &connector);
61 assert(yrc == YHR_SUCCESS);
62
63 yrc = yh_connect(connector, 0);
64 assert(yrc == YHR_SUCCESS);
65
66 yrc = yh_create_session_derived(connector, authkey, password,
67 sizeof(password), false, &session);
68 assert(yrc == YHR_SUCCESS);
69
71 assert(yrc == YHR_SUCCESS);
72
73 uint8_t session_id;
74 yrc = yh_get_session_id(session, &session_id);
75 assert(yrc == YHR_SUCCESS);
76
77 printf("Successfully established session %02d\n", session_id);
78
81 assert(yrc == YHR_SUCCESS);
82
83 uint16_t domain_five = 0;
84 yrc = yh_string_to_domains("5", &domain_five);
85 assert(yrc == YHR_SUCCESS);
86
87 uint16_t key_id = 0; // ID 0 lets the device generate an ID
90 assert(yrc == YHR_SUCCESS);
91
92 printf("Generated key with ID %04x\n", key_id);
93
94 printf("Data to sign (%zu bytes) is: %s\n", sizeof(data) - 1, data);
95
96 EVP_MD_CTX *mdctx = NULL;
97 uint8_t hashed_data[32];
98 unsigned int hashed_data_len;
99
100 mdctx = EVP_MD_CTX_create();
101 assert(mdctx != NULL);
102 EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
103 EVP_DigestUpdate(mdctx, data, sizeof(data) - 1);
104 EVP_DigestFinal_ex(mdctx, hashed_data, &hashed_data_len);
105 EVP_MD_CTX_destroy(mdctx);
106
107 printf("Hash of data (%d bytes) is:", EVP_MD_size(EVP_sha256()));
108 for (unsigned int i = 0; i < hashed_data_len; i++) {
109 printf(" %02x", hashed_data[i]);
110 }
111 printf("\n");
112
113 uint8_t signature[128];
114 size_t signature_len = sizeof(signature);
115 yrc = yh_util_sign_ecdsa(session, key_id, hashed_data, hashed_data_len,
116 signature, &signature_len);
117 assert(yrc == YHR_SUCCESS);
118
119 printf("Signature (%zu bytes) is:", signature_len);
120 for (unsigned int i = 0; i < signature_len; i++) {
121 printf(" %02x", signature[i]);
122 }
123 printf("\n");
124
125 uint8_t public_key[512];
126 size_t public_key_len = sizeof(public_key);
127 yrc =
128 yh_util_get_public_key(session, key_id, public_key, &public_key_len, NULL);
129 assert(yrc == YHR_SUCCESS);
130
131 printf("Public key (%zu bytes) is:", public_key_len);
132 for (unsigned int i = 0; i < public_key_len; i++) {
133 printf(" %02x", public_key[i]);
134 }
135 printf("\n");
136
137 EC_KEY *eckey = EC_KEY_new();
138 int nid = algo2nid(YH_ALGO_EC_P256);
139 EC_POINT *point;
140 EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
141
142 EC_GROUP_set_asn1_flag(group, nid);
143 EC_KEY_set_group(eckey, group);
144 point = EC_POINT_new(group);
145
146 memmove(public_key + 1, public_key, public_key_len);
147 public_key[0] = 0x04; // hack to make it a valid ec pubkey..
148 public_key_len++;
149
150 EC_POINT_oct2point(group, point, public_key, public_key_len, NULL);
151
152 EC_KEY_set_public_key(eckey, point);
153
154 if (ECDSA_verify(0, hashed_data, hashed_data_len, signature, signature_len,
155 eckey) == 1) {
156 printf("Signature successfully verified\n");
157 } else {
158 printf("Unable to verify signature\n");
159 }
160
161 EC_POINT_free(point);
162 EC_KEY_free(eckey);
163 EC_GROUP_free(group);
164
166 assert(yrc == YHR_SUCCESS);
167
169 assert(yrc == YHR_SUCCESS);
170
171 yh_disconnect(connector);
172 assert(yrc == YHR_SUCCESS);
173
174 yrc = yh_exit();
175 assert(yrc == YHR_SUCCESS);
176
177 return 0;
178}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
Definition generate_ec.c:36
const char * key_label
Definition generate_ec.c:39
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
fc::array< char, 72 > signature
Definition elliptic.hpp:25
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
int algo2nid(yh_algorithm algo)
Definition util.c:335
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_util_generate_ec_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm)
Definition yubihsm.c:1913
yh_rc yh_exit(void)
Definition yubihsm.c:3910
yh_rc yh_create_session_derived(yh_connector *connector, uint16_t authkey_id, const uint8_t *password, size_t password_len, bool recreate, yh_session **session)
Definition yubihsm.c:593
yh_rc yh_util_sign_ecdsa(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:1411
yh_rc yh_init(void)
Definition yubihsm.c:3857
yh_rc yh_util_close_session(yh_session *session)
Definition yubihsm.c:1257
yh_rc yh_authenticate_session(yh_session *session)
Definition yubihsm.c:2927
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
Definition yubihsm.c:4535
yh_rc yh_init_connector(const char *url, yh_connector **connector)
Definition yubihsm.c:4024
yh_rc yh_connect(yh_connector *connector, int timeout)
Definition yubihsm.c:4079
yh_rc yh_util_get_public_key(yh_session *session, uint16_t id, uint8_t *data, size_t *data_len, yh_algorithm *algorithm)
Definition yubihsm.c:1216
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
Definition yubihsm.c:4115
yh_rc yh_disconnect(yh_connector *connector)
Definition yubihsm.c:4097
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
@ YH_ALGO_EC_P256
ecp256
Definition yubihsm.h:414
yh_rc
Definition yubihsm.h:170
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
Definition yubihsm.h:228
@ YHR_SUCCESS
Returned value when function was successful.
Definition yubihsm.h:172
yh_capabilities capabilities
EVP_MD_CTX * mdctx
yh_rc yrc
uint16_t key_id
Here is the call graph for this function:

Variable Documentation

◆ data

const uint8_t data[] = "sudo make me a sandwich"

Definition at line 41 of file generate_ec.c.

◆ key_label

const char* key_label = "label"

Definition at line 39 of file generate_ec.c.

◆ password

const uint8_t password[] = "password"

Definition at line 40 of file generate_ec.c.