48 const char *connector_url;
50 connector_url = getenv(
"DEFAULT_CONNECTOR_URL");
51 if (connector_url == NULL) {
65 sizeof(password),
false, &
session);
75 printf(
"Successfully established session %02d\n", session_id);
90 printf(
"Generated key with ID %04x\n",
key_id);
93 size_t public_key_len =
sizeof(public_key);
98 printf(
"Public key (%zd bytes) is:", public_key_len);
99 for (
size_t i = 0; i < public_key_len; i++) {
100 printf(
" %02x", public_key[i]);
104 EC_KEY *eckey = EC_KEY_new();
107 EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
109 EC_GROUP_set_asn1_flag(group, nid);
110 EC_KEY_set_group(eckey, group);
111 point = EC_POINT_new(group);
113 memmove(public_key + 1, public_key, public_key_len);
114 public_key[0] = 0x04;
117 EC_POINT_oct2point(group,
point, public_key, public_key_len, NULL);
119 EC_KEY_set_public_key(eckey,
point);
122 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
123 assert(pctx != NULL);
126 assert(EVP_PKEY_paramgen_init(pctx) == 1);
129 assert(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1) ==
134 assert(EVP_PKEY_paramgen(pctx, &
params) == 1);
137 EVP_PKEY_CTX *kctx = EVP_PKEY_CTX_new(
params, NULL);
138 assert(kctx != NULL);
141 EVP_PKEY *pkey = NULL;
142 assert(EVP_PKEY_keygen_init(kctx) == 1);
143 assert(EVP_PKEY_keygen(kctx, &pkey) == 1);
146 EVP_PKEY *peerkey = EVP_PKEY_new();
147 assert(peerkey != NULL);
148 assert(EVP_PKEY_set1_EC_KEY(peerkey, eckey) == 1);
151 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL);
155 assert(EVP_PKEY_derive_init(ctx) == 1);
158 assert(EVP_PKEY_derive_set_peer(ctx, peerkey) == 1);
161 size_t secret_len =
sizeof(secret_len);
164 assert(EVP_PKEY_derive(ctx, NULL, &secret_len));
167 assert(EVP_PKEY_derive(ctx, secret, &secret_len) == 1);
169 EC_KEY *eckey2 = EVP_PKEY_get1_EC_KEY(pkey);
170 const EC_POINT *
pub = EC_KEY_get0_public_key(eckey2);
173 size_t pubkey_len =
sizeof(
pubkey);
175 pubkey_len = EC_POINT_point2oct(group,
pub, POINT_CONVERSION_UNCOMPRESSED,
176 pubkey, pubkey_len, NULL);
177 assert(pubkey_len == 65);
180 size_t computed_secret_len =
sizeof(computed_secret);
182 computed_secret, &computed_secret_len);
185 assert(computed_secret_len == secret_len);
186 assert(memcmp(secret, computed_secret, computed_secret_len) == 0);
188 printf(
"Secrets match\n");
190 EVP_PKEY_CTX_free(ctx);
191 EVP_PKEY_free(peerkey);
193 EVP_PKEY_CTX_free(kctx);
195 EVP_PKEY_CTX_free(pctx);
196 EC_POINT_free(
point);
199 EC_GROUP_free(group);
yh_rc yh_util_derive_ecdh(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_generate_ec_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm)
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)