Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
wrap_data.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 <stdio.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <yubihsm.h>
27
28#ifndef DEFAULT_CONNECTOR_URL
29#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
30#endif
31
32const char *key_label = "label";
33const uint8_t password[] = "password";
34
35const uint8_t clear[] = "test data";
36
37int main(void) {
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\n", session_id);
72
74 yrc = yh_string_to_capabilities("wrap-data:unwrap-data", &capabilities);
75 assert(yrc == YHR_SUCCESS);
76
78
79 uint16_t domain_five = 0;
80 uint16_t wrapping_key_id = 0; // ID 0 lets the device generate an ID
81
82 yrc = yh_string_to_domains("5", &domain_five);
83 assert(yrc == YHR_SUCCESS);
84
85 yrc =
86 yh_util_generate_wrap_key(session, &wrapping_key_id, key_label, domain_five,
89 assert(yrc == YHR_SUCCESS);
90
91 printf("Generated wrapping key with ID %04x\n", wrapping_key_id);
92
93 uint8_t data[1024];
94 size_t data_len = sizeof(data);
95
96 yrc = yh_util_wrap_data(session, wrapping_key_id, clear, sizeof(clear), data,
97 &data_len);
98 assert(yrc == YHR_SUCCESS);
99
100 printf("Data wrapped to length %zu\n", data_len);
101
102 assert(data_len == sizeof(clear) + YH_CCM_WRAP_OVERHEAD);
103 assert(memcmp(data, clear, sizeof(clear)) != 0);
104
105 yrc = yh_util_unwrap_data(session, wrapping_key_id, data, data_len, data,
106 &data_len);
107 assert(yrc == YHR_SUCCESS);
108
109 assert(data_len == sizeof(clear));
110 assert(memcmp(data, clear, sizeof(clear)) == 0);
111
112 printf("Data unwrapped successfully\n");
113
115 assert(yrc == YHR_SUCCESS);
116
118 assert(yrc == YHR_SUCCESS);
119
120 yh_disconnect(connector);
121 assert(yrc == YHR_SUCCESS);
122
123 yrc = yh_exit();
124 assert(yrc == YHR_SUCCESS);
125
126 return 0;
127}
CK_SESSION_HANDLE session
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
#define DEFAULT_CONNECTOR_URL
Definition wrap_data.c:29
int main(void)
Definition wrap_data.c:37
const char * key_label
Definition wrap_data.c:32
const uint8_t clear[]
Definition wrap_data.c:35
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_util_generate_wrap_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const yh_capabilities *delegated_capabilities)
Definition yubihsm.c:2458
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_unwrap_data(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:3716
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_wrap_data(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:3667
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_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
@ YH_ALGO_AES256_CCM_WRAP
aes256-ccm-wrap
Definition yubihsm.h:474
#define YH_CCM_WRAP_OVERHEAD
Definition yubihsm.h:149
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
yh_capabilities delegated_capabilities