Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
import_ed.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifdef NDEBUG
18#undef NDEBUG
19#endif
20#include <assert.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "util.h"
28
29#include <yubihsm.h>
30
31#ifndef DEFAULT_CONNECTOR_URL
32#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
33#endif
34
35const char ed25519_pvtkey_file[] = "ed25519_pvtkey.pem";
36const char *key_label = "label";
37const uint8_t password[] = "password";
38const uint8_t data[] = {0x72};
40 {0x92, 0xa0, 0x09, 0xa9, 0xf0, 0xd4, 0xca, 0xb8, 0x72, 0x0e, 0x82, 0x0b, 0x5f,
41 0x64, 0x25, 0x40, 0xa2, 0xb2, 0x7b, 0x54, 0x16, 0x50, 0x3f, 0x8f, 0xb3, 0x76,
42 0x22, 0x23, 0xeb, 0xdb, 0x69, 0xda, 0x08, 0x5a, 0xc1, 0xe4, 0x3e, 0x15, 0x99,
43 0x6e, 0x45, 0x8f, 0x36, 0x13, 0xd0, 0xf1, 0x1d, 0x8c, 0x38, 0x7b, 0x2e, 0xae,
44 0xb4, 0x30, 0x2a, 0xee, 0xb0, 0x0d, 0x29, 0x16, 0x12, 0xbb, 0x0c, 0x00};
45const uint8_t expected_public_key[] = {0x3d, 0x40, 0x17, 0xc3, 0xe8, 0x43, 0x89,
46 0x5a, 0x92, 0xb7, 0x0a, 0xa7, 0x4d, 0x1b,
47 0x7e, 0xbc, 0x9c, 0x98, 0x2c, 0xcf, 0x2e,
48 0xc4, 0x96, 0x8c, 0xc0, 0xcd, 0x55, 0xf1,
49 0x2a, 0xf4, 0x66, 0x0c};
50
51int main(void) {
52 yh_connector *connector = NULL;
53 yh_session *session = NULL;
55
56 uint16_t authkey = 1;
57
58 const char *connector_url;
59
60 connector_url = getenv("DEFAULT_CONNECTOR_URL");
61 if (connector_url == NULL) {
62 connector_url = DEFAULT_CONNECTOR_URL;
63 }
64
65 yrc = yh_init();
66 assert(yrc == YHR_SUCCESS);
67
68 yrc = yh_init_connector(connector_url, &connector);
69 assert(yrc == YHR_SUCCESS);
70
71 yrc = yh_connect(connector, 0);
72 assert(yrc == YHR_SUCCESS);
73
74 yrc = yh_create_session_derived(connector, authkey, password,
75 sizeof(password), false, &session);
76 assert(yrc == YHR_SUCCESS);
77
79 assert(yrc == YHR_SUCCESS);
80
81 uint8_t session_id;
82 yrc = yh_get_session_id(session, &session_id);
83 assert(yrc == YHR_SUCCESS);
84
85 printf("Successfully established session %02d\n", session_id);
86
87 FILE *fp = fopen(ed25519_pvtkey_file, "rb");
88 assert(fp != NULL);
89
91 uint8_t key[2048];
92 size_t key_material_len = sizeof(key);
93 if (!read_file(fp, key, &key_material_len)) {
94 assert(false);
95 }
96 bool ret = read_private_key(key, key_material_len, &algorithm, key,
97 &key_material_len, false);
98 assert(ret == true);
100
103 assert(yrc == YHR_SUCCESS);
104
105 uint16_t domain_five = 0;
106 yrc = yh_string_to_domains("5", &domain_five);
107 assert(yrc == YHR_SUCCESS);
108
109 uint16_t key_id = 0; // ID 0 lets the device generate an ID
111 &capabilities, algorithm, key);
112 assert(yrc == YHR_SUCCESS);
113
114 printf("Key imported with ID %04x\n", key_id);
115
116 printf("Signing %zu bytes of data\n", sizeof(data));
117
118 uint8_t signature[128];
119 size_t signature_len = sizeof(signature);
120 yrc = yh_util_sign_eddsa(session, key_id, data, sizeof(data), signature,
121 &signature_len);
122 assert(yrc == YHR_SUCCESS);
123
124 printf("Signature (%zu bytes) is:", signature_len);
125 for (unsigned int i = 0; i < signature_len; i++) {
126 printf(" %02x", signature[i]);
127 }
128 printf("\n");
129
130 assert(signature_len == 64);
131 assert(memcmp(signature, expected_signature, 64) == 0);
132
133 uint8_t public_key[512];
134 size_t public_key_len = sizeof(public_key);
135 yrc =
136 yh_util_get_public_key(session, key_id, public_key, &public_key_len, NULL);
137 assert(yrc == YHR_SUCCESS);
138
139 assert(public_key_len == 32);
140 assert(memcmp(public_key, expected_public_key, 32) == 0);
141
142 printf("Public key (%zu bytes) is:", public_key_len);
143 for (unsigned int i = 0; i < public_key_len; i++) {
144 printf(" %02x", public_key[i]);
145 }
146 printf("\n");
147
149 assert(yrc == YHR_SUCCESS);
150
152 assert(yrc == YHR_SUCCESS);
153
154 yh_disconnect(connector);
155 assert(yrc == YHR_SUCCESS);
156
157 yrc = yh_exit();
158 assert(yrc == YHR_SUCCESS);
159
160 return 0;
161}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
Definition import_ed.c:32
const char ed25519_pvtkey_file[]
Definition import_ed.c:35
const uint8_t expected_public_key[]
Definition import_ed.c:45
int main(void)
Definition import_ed.c:51
const char * key_label
Definition import_ed.c:36
const uint8_t expected_signature[]
Definition import_ed.c:39
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
bool read_file(FILE *fp, uint8_t *buf, size_t *buf_len)
Definition util.c:476
bool read_private_key(uint8_t *buf, size_t len, yh_algorithm *algo, uint8_t *bytes, size_t *bytes_len, bool internal_repr)
Definition util.c:116
uint8_t key[16]
Definition yubico_otp.c:41
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
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_init(void)
Definition yubihsm.c:3857
yh_rc yh_util_close_session(yh_session *session)
Definition yubihsm.c:1257
yh_rc yh_util_sign_eddsa(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:1470
yh_rc yh_authenticate_session(yh_session *session)
Definition yubihsm.c:2927
yh_rc yh_util_import_ed_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const uint8_t *k)
Definition yubihsm.c:1727
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_algorithm
Definition yubihsm.h:390
@ YH_ALGO_EC_ED25519
ed25519
Definition yubihsm.h:482
yh_algorithm algorithm
Definition yubihsm.h:619
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
CK_RV ret
yh_rc yrc
uint16_t key_id