Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
yubico_otp.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <yubihsm.h>
Include dependency graph for yubico_otp.c:

Go to the source code of this file.

Macros

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

Functions

uint16_t yubikey_crc16 (const uint8_t *buf, size_t buf_size)
 
int main (void)
 

Variables

const char * key_label = "label"
 
const uint8_t password [] = "password"
 
const uint8_t otp_key []
 

Macro Definition Documentation

◆ DEFAULT_CONNECTOR_URL

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

Definition at line 32 of file yubico_otp.c.

Function Documentation

◆ main()

int main ( void )

Definition at line 88 of file yubico_otp.c.

88 {
89 yh_connector *connector = NULL;
90 yh_session *session = NULL;
92
93 uint16_t authkey = 1;
94
95 const char *connector_url;
96
97 connector_url = getenv("DEFAULT_CONNECTOR_URL");
98 if (connector_url == NULL) {
99 connector_url = DEFAULT_CONNECTOR_URL;
100 }
101
102 yrc = yh_init();
103 assert(yrc == YHR_SUCCESS);
104
105 yrc = yh_init_connector(connector_url, &connector);
106 assert(yrc == YHR_SUCCESS);
107
108 yrc = yh_connect(connector, 0);
109 assert(yrc == YHR_SUCCESS);
110
111 yrc = yh_create_session_derived(connector, authkey, password,
112 sizeof(password), false, &session);
113 assert(yrc == YHR_SUCCESS);
114
116 assert(yrc == YHR_SUCCESS);
117
118 uint8_t session_id;
119 yrc = yh_get_session_id(session, &session_id);
120 assert(yrc == YHR_SUCCESS);
121
122 printf("Successfully established session %02d\n", session_id);
123
125 yrc =
126 yh_string_to_capabilities("create-otp-aead:decrypt-otp:randomize-otp-aead",
127 &capabilities);
128 assert(yrc == YHR_SUCCESS);
129
130 uint16_t domain_five = 16; // Domain five is 0b0000000000010000
131 uint16_t key_id = 0; // ID 0 lets the device generate an ID
132 uint32_t nonce_id = 0x12345678;
135 nonce_id);
136 assert(yrc == YHR_SUCCESS);
137
138 printf("Generated OTP key with ID %04x\n", key_id);
139
141 assert(yrc == YHR_SUCCESS);
142
144 &capabilities, nonce_id, otp_key,
145 sizeof(otp_key));
146 assert(yrc == YHR_SUCCESS);
147
148 for (size_t i = 0; i < sizeof(test_vectors) / sizeof(test_vectors[0]); i++) {
149 uint8_t aead[512];
150 size_t aead_len = sizeof(aead);
151 yrc = yh_util_create_otp_aead(session, key_id, test_vectors[i].key,
152 test_vectors[i].id, aead, &aead_len);
153 assert(yrc == YHR_SUCCESS);
154
159
160 printf("Checking test vector %zu ... ", i);
161 yrc =
162 yh_util_decrypt_otp(session, key_id, aead, aead_len, test_vectors[i].otp,
165 assert(yrc == YHR_SUCCESS);
166
167 assert(test_vectors[i].use_counter == use_counter);
168 assert(test_vectors[i].session_counter == session_counter);
169 assert(test_vectors[i].timestamp_high == timestamp_high);
170 assert(test_vectors[i].timestamp_low == timestamp_low);
171
172 printf("OK\n");
173 }
174
175 printf("Put OTP key with ID %04x\n", key_id);
176
177 uint8_t otp_data[64];
178 size_t otp_data_len = sizeof(otp_data);
179 size_t tag_len = 8;
180 size_t nonce_len = 13;
181 uint8_t nonce[13] = {0};
182 uint8_t out_buf[32];
183 int out_len;
184 yrc = yh_util_randomize_otp_aead(session, key_id, otp_data, &otp_data_len);
185 assert(yrc == YHR_SUCCESS);
186
187 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
188
189 // Select cipher
190 assert(EVP_DecryptInit_ex(ctx, EVP_aes_128_ccm(), NULL, NULL, NULL) == 1);
191
192 // Set nonce length
193 assert(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, nonce_len, NULL) ==
194 1);
195
196 // Set expected tag value
197 assert(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_len,
198 otp_data + otp_data_len - tag_len) == 1);
199
200 // Specify key and IV
201 memcpy(nonce, &nonce_id, 4);
202 memcpy(nonce + 4, otp_data, 6);
203 assert(EVP_DecryptInit_ex(ctx, NULL, NULL, otp_key, nonce) == 1);
204
205 // Decrypt plaintext, verify tag: can only be called once
206 assert(EVP_DecryptUpdate(ctx, out_buf, &out_len, otp_data + 6,
207 otp_data_len - 6 - tag_len) == 1);
208
209 EVP_CIPHER_CTX_free(ctx);
210
211 struct {
212 union {
213 struct {
214 uint8_t id[6];
219 uint16_t rnd;
221 };
222 uint8_t raw[16];
223 };
224 } token = {.raw = {0}};
225
226 uint8_t otp[16] = {0};
227
228 memcpy(token.id, out_buf + 16, 6);
229 token.use_counter = 0xabcd;
230 token.timestamp_low = 0xdcba;
231 token.timestamp_high = 0xff;
232 token.session_counter = 0x00;
233 token.crc = ~yubikey_crc16(token.raw, 14);
234
235 AES_KEY k;
236 AES_set_encrypt_key(out_buf, 128, &k);
237 AES_ecb_encrypt(token.raw, otp, &k, AES_ENCRYPT);
238
243
244 yrc = yh_util_decrypt_otp(session, key_id, otp_data, otp_data_len, otp,
247 assert(yrc == YHR_SUCCESS);
248
249 assert(use_counter == token.use_counter);
250 assert(timestamp_low == token.timestamp_low);
251 assert(timestamp_high == token.timestamp_high);
252 assert(session_counter == token.session_counter);
253
255 assert(yrc == YHR_SUCCESS);
256
258 assert(yrc == YHR_SUCCESS);
259
260 yh_disconnect(connector);
261 assert(yrc == YHR_SUCCESS);
262
263 yrc = yh_exit();
264 assert(yrc == YHR_SUCCESS);
265
266 return 0;
267}
CK_SESSION_HANDLE session
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
#define DEFAULT_CONNECTOR_URL
Definition yubico_otp.c:32
const uint8_t otp_key[]
Definition yubico_otp.c:37
uint16_t timestamp_low
Definition yubico_otp.c:44
const char * key_label
Definition yubico_otp.c:35
uint8_t timestamp_high
Definition yubico_otp.c:45
uint16_t crc
Definition yubico_otp.c:48
uint8_t otp[32]
Definition yubico_otp.c:49
uint8_t session_counter
Definition yubico_otp.c:46
uint16_t use_counter
Definition yubico_otp.c:43
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_util_decrypt_otp(yh_session *session, uint16_t key_id, const uint8_t *aead, size_t aead_len, const uint8_t *otp, uint16_t *useCtr, uint8_t *sessionCtr, uint8_t *tstph, uint16_t *tstpl)
Definition yubihsm.c:3261
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_randomize_otp_aead(yh_session *session, uint16_t key_id, uint8_t *out, size_t *out_len)
Definition yubihsm.c:3223
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_util_create_otp_aead(yh_session *session, uint16_t key_id, const uint8_t *key, const uint8_t *private_id, uint8_t *out, size_t *out_len)
Definition yubihsm.c:3179
yh_rc yh_init_connector(const char *url, yh_connector **connector)
Definition yubihsm.c:4024
yh_rc yh_util_generate_otp_aead_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, uint32_t nonce_id)
Definition yubihsm.c:3422
yh_rc yh_connect(yh_connector *connector, int timeout)
Definition yubihsm.c:4079
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_util_import_otp_aead_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, uint32_t nonce_id, const uint8_t *in, size_t in_len)
Definition yubihsm.c:3337
yh_rc yh_util_delete_object(yh_session *session, uint16_t id, yh_object_type type)
Definition yubihsm.c:2222
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
@ YH_OTP_AEAD_KEY
OTP AEAD Key is a secret key used to decrypt Yubico OTP values.
Definition yubihsm.h:376
@ YH_ALGO_AES128_YUBICO_OTP
aes128-yubico-otp
Definition yubihsm.h:464
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
size_t out_len
yh_rc yrc
uint16_t key_id
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

◆ yubikey_crc16()

uint16_t yubikey_crc16 ( const uint8_t * buf,
size_t buf_size )

Definition at line 70 of file yubico_otp.c.

70 {
71 uint16_t m_crc = 0xffff;
72
73 while (buf_size--) {
74 int i, j;
75 m_crc ^= (uint8_t) *buf++ & 0xFF;
76 for (i = 0; i < 8; i++) {
77 j = m_crc & 1;
78 m_crc >>= 1;
79 if (j) {
80 m_crc ^= 0x8408;
81 }
82 }
83 }
84
85 return m_crc;
86}
uint16_t j
uint8_t buf[2048]

Variable Documentation

◆ crc

uint16_t crc

Definition at line 48 of file yubico_otp.c.

◆ id

uint8_t id[6]

Definition at line 42 of file yubico_otp.c.

◆ key

Initial value:
=
get_object_desc(session->slot->device_session, session->slot->objects,
yubihsm_pkcs11_object_desc * get_object_desc(yh_session *session, yubihsm_pkcs11_object_desc *objects, CK_OBJECT_HANDLE objHandle)
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_OBJECT_HANDLE hUnwrappingKey

Definition at line 41 of file yubico_otp.c.

◆ key_label

const char* key_label = "label"

Definition at line 35 of file yubico_otp.c.

◆ otp

uint8_t otp[32]

Definition at line 49 of file yubico_otp.c.

◆ otp_key

const uint8_t otp_key[]
Initial value:
= {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f}

Definition at line 37 of file yubico_otp.c.

37 {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
38 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f};

◆ password

const uint8_t password[] = "password"

Definition at line 36 of file yubico_otp.c.

◆ random

uint16_t random

Definition at line 47 of file yubico_otp.c.

◆ session_counter

uint8_t session_counter

Definition at line 46 of file yubico_otp.c.

◆ timestamp_high

uint8_t timestamp_high

Definition at line 45 of file yubico_otp.c.

◆ timestamp_low

uint16_t timestamp_low

Definition at line 44 of file yubico_otp.c.

◆ use_counter

uint16_t use_counter

Definition at line 43 of file yubico_otp.c.