27#include <openssl/ec.h>
28#include <openssl/ecdsa.h>
29#include <openssl/evp.h>
35#ifndef DEFAULT_CONNECTOR_URL
36#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
42const uint8_t data[] =
"sudo make me a sandwich";
51 const char *connector_url;
53 connector_url = getenv(
"DEFAULT_CONNECTOR_URL");
54 if (connector_url == NULL) {
68 sizeof(password),
false, &
session);
78 printf(
"Successfully established session %02d\n", session_id);
85 size_t key_material_len =
sizeof(
key);
86 if (!
read_file(fp, key, &key_material_len)) {
90 &key_material_len,
false);
107 printf(
"Key imported with ID %04x\n",
key_id);
109 printf(
"Data to sign (%zu bytes) is: %s\n",
sizeof(data) - 1, data);
111 EVP_MD_CTX *
mdctx = NULL;
113 unsigned int hashed_data_len;
115 mdctx = EVP_MD_CTX_create();
116 assert(
mdctx != NULL);
117 EVP_DigestInit_ex(
mdctx, EVP_sha256(), NULL);
118 EVP_DigestUpdate(
mdctx, data,
sizeof(data) - 1);
119 EVP_DigestFinal_ex(
mdctx, hashed_data, &hashed_data_len);
120 EVP_MD_CTX_destroy(
mdctx);
122 printf(
"Hash of data (%d bytes) is:", EVP_MD_size(EVP_sha256()));
123 for (
unsigned int i = 0; i < hashed_data_len; i++) {
124 printf(
" %02x", hashed_data[i]);
129 size_t signature_len =
sizeof(signature);
131 signature, &signature_len);
134 printf(
"Signature (%zu bytes) is:", signature_len);
135 for (
unsigned int i = 0; i < signature_len; i++) {
136 printf(
" %02x", signature[i]);
141 size_t public_key_len =
sizeof(public_key);
146 printf(
"Public key (%zu bytes) is:", public_key_len);
147 for (
unsigned int i = 0; i < public_key_len; i++) {
148 printf(
" %02x", public_key[i]);
152 EC_KEY *eckey = EC_KEY_new();
155 EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
157 EC_GROUP_set_asn1_flag(group, nid);
158 EC_KEY_set_group(eckey, group);
159 point = EC_POINT_new(group);
161 memmove(public_key + 1, public_key, public_key_len);
162 public_key[0] = 0x04;
165 EC_POINT_oct2point(group,
point, public_key, public_key_len, NULL);
167 EC_KEY_set_public_key(eckey,
point);
169 if (ECDSA_verify(0, hashed_data, hashed_data_len, signature, signature_len,
171 printf(
"Signature successfully verified\n");
173 printf(
"Unable to verify signature\n");
176 EC_POINT_free(
point);
178 EC_GROUP_free(group);
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
const char p256_pvtkey_file[]
Capabilities representation.
int algo2nid(yh_algorithm algo)
bool read_file(FILE *fp, uint8_t *buf, size_t *buf_len)
bool read_private_key(uint8_t *buf, size_t len, yh_algorithm *algo, uint8_t *bytes, size_t *bytes_len, bool internal_repr)
yh_rc yh_destroy_session(yh_session **session)
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)
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)
yh_rc yh_util_close_session(yh_session *session)
yh_rc yh_authenticate_session(yh_session *session)
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
yh_rc yh_init_connector(const char *url, yh_connector **connector)
yh_rc yh_connect(yh_connector *connector, int timeout)
yh_rc yh_util_get_public_key(yh_session *session, uint16_t id, uint8_t *data, size_t *data_len, yh_algorithm *algorithm)
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_disconnect(yh_connector *connector)
yh_rc yh_util_import_ec_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const uint8_t *s)
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
@ YHR_SUCCESS
Returned value when function was successful.
yh_capabilities capabilities