26#include <openssl/evp.h>
27#include <openssl/rsa.h>
34#ifndef DEFAULT_CONNECTOR_URL
35#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
41const uint8_t data[] =
"sudo make me a sandwich";
50 const char *connector_url;
52 connector_url = getenv(
"DEFAULT_CONNECTOR_URL");
53 if (connector_url == NULL) {
67 sizeof(password),
false, &
session);
77 printf(
"Successfully established session %02d\n", session_id);
84 size_t key_material_len =
sizeof(privkey);
85 if (!
read_file(fp, privkey, &key_material_len)) {
89 &key_material_len,
false);
104 privkey + (key_material_len / 2));
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);
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 BIGNUM *n = BN_bin2bn(public_key, public_key_len, NULL);
155 BIGNUM *e = BN_bin2bn((
const unsigned char *)
"\x01\x00\x01", 3, NULL);
158 RSA *rsa = RSA_new();
161 EVP_PKEY *key = EVP_PKEY_new();
162 assert(EVP_PKEY_assign_RSA(key, rsa) == 1);
163 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
165 assert(EVP_PKEY_verify_init(ctx) == 1);
166 assert(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) == 1);
167 assert(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 1);
168 assert(EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, 32) == 1);
169 assert(EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256()) == 1);
171 if (EVP_PKEY_verify(ctx, signature, signature_len, hashed_data,
172 hashed_data_len) == 1) {
173 printf(
"Signature successfully verified\n");
175 printf(
"Unable to verify signature\n");
178 EVP_PKEY_CTX_free(ctx);
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
const char rsa2048_pvtkey_file[]
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
Capabilities representation.
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_close_session(yh_session *session)
yh_rc yh_authenticate_session(yh_session *session)
yh_rc yh_util_sign_pss(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len, size_t salt_len, yh_algorithm mgf1Algo)
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_util_import_rsa_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const uint8_t *p, const uint8_t *q)
yh_rc yh_disconnect(yh_connector *connector)
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
@ YH_ALGO_RSA_2048
rsa2048
@ YH_ALGO_MGF1_SHA256
mgf1-sha256
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
@ YHR_SUCCESS
Returned value when function was successful.
yh_capabilities capabilities