Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
generate_hmac.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <yubihsm.h>
Include dependency graph for generate_hmac.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 30 of file generate_hmac.c.

Function Documentation

◆ main()

int main ( void )

Definition at line 37 of file generate_hmac.c.

37 {
38 yh_connector *connector = NULL;
39 yh_session *session = NULL;
41
42 uint16_t authkey = 1;
43
44 const char *connector_url;
45
46 connector_url = getenv("DEFAULT_CONNECTOR_URL");
47 if (connector_url == NULL) {
48 connector_url = DEFAULT_CONNECTOR_URL;
49 }
50
51 yrc = yh_init();
52 assert(yrc == YHR_SUCCESS);
53
54 yrc = yh_init_connector(connector_url, &connector);
55 assert(yrc == YHR_SUCCESS);
56
57 yrc = yh_connect(connector, 0);
58 assert(yrc == YHR_SUCCESS);
59
60 yrc = yh_create_session_derived(connector, authkey, password,
61 sizeof(password), false, &session);
62 assert(yrc == YHR_SUCCESS);
63
65 assert(yrc == YHR_SUCCESS);
66
67 uint8_t session_id;
68 yrc = yh_get_session_id(session, &session_id);
69 assert(yrc == YHR_SUCCESS);
70
71 printf("Successfully established session %02d using Authentication Key "
72 "%04x\n",
73 session_id, authkey);
74
76 yrc = yh_string_to_capabilities("sign-hmac:verify-hmac", &capabilities);
77 assert(yrc == YHR_SUCCESS);
78
79 uint16_t domain_five = 0;
80 yrc = yh_string_to_domains("5", &domain_five);
81 assert(yrc == YHR_SUCCESS);
82
83 uint16_t key_id = 0; // ID 0 lets the device generate an ID
86 assert(yrc == YHR_SUCCESS);
87
88 printf("Generated HMAC-SHA256 key with ID %04x\n", key_id);
89
90 uint8_t hmac_data[64];
91 size_t hmac_data_len = sizeof(hmac_data);
92 yrc = yh_util_sign_hmac(session, key_id, data, sizeof(data) - 1, hmac_data,
93 &hmac_data_len);
94 assert(yrc == YHR_SUCCESS);
95
96 printf("HMAC of data (%zu bytes) is:", hmac_data_len);
97 for (uint16_t i = 0; i < hmac_data_len; i++) {
98 printf(" %02x", hmac_data[i]);
99 }
100 printf("\n");
101
102 bool verified;
103 yrc = yh_util_verify_hmac(session, key_id, hmac_data, hmac_data_len, data,
104 sizeof(data) - 1, &verified);
105 assert(yrc == YHR_SUCCESS);
106
107 if (verified == true) {
108 printf("Successfully verified HMAC\n");
109 } else {
110 printf("Unable to verify HMAC\n");
111 }
112
113 hmac_data[0] += 1;
114 yrc = yh_util_verify_hmac(session, key_id, hmac_data, hmac_data_len, data,
115 sizeof(data) - 1, &verified);
116 assert(yrc == YHR_SUCCESS);
117
118 if (verified == true) {
119 printf("Successfully verified HMAC\n");
120 } else {
121 printf("Unable to verify HMAC\n");
122 }
123
125 assert(yrc == YHR_SUCCESS);
126
128 assert(yrc == YHR_SUCCESS);
129
130 yh_disconnect(connector);
131 assert(yrc == YHR_SUCCESS);
132
133 yrc = yh_exit();
134 assert(yrc == YHR_SUCCESS);
135
136 return 0;
137}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
const char * key_label
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_util_generate_hmac_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm)
Definition yubihsm.c:1992
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_authenticate_session(yh_session *session)
Definition yubihsm.c:2927
yh_rc yh_util_sign_hmac(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:1520
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_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_verify_hmac(yh_session *session, uint16_t key_id, const uint8_t *signature, size_t signature_len, const uint8_t *data, size_t data_len, bool *verified)
Definition yubihsm.c:1939
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
@ YH_ALGO_HMAC_SHA256
hmac-sha256
Definition yubihsm.h:430
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
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 35 of file generate_hmac.c.

◆ key_label

const char* key_label = "label"

Definition at line 33 of file generate_hmac.c.

◆ password

const uint8_t password[] = "password"

Definition at line 34 of file generate_hmac.c.