* used. If it's not known if the key is encrypted one should pass NULL as
* password and check for the #YACA_ERROR_PASSWORD_INVALID return code.
*
- * @param[out] key Returned key (must be freed with yaca_key_free())
* @param[in] key_type Type of the key
* @param[in] password null terminated password for the key (can be NULL)
* @param[in] data Blob containing the key
* @param[in] data_len Size of the blob
+ * @param[out] key Returned key (must be freed with yaca_key_free())
*
* @return #YACA_ERROR_NONE on success, negative on error
* @retval #YACA_ERROR_NONE Succesful
* @see yaca_key_export()
* @see yaca_key_free()
*/
-int yaca_key_import(yaca_key_h *key,
- yaca_key_type_e key_type,
+int yaca_key_import(yaca_key_type_e key_type,
const char *password,
const char *data,
- size_t data_len);
+ size_t data_len,
+ yaca_key_h *key);
/**
* @brief Exports a key to arbitrary format. Export may fail if key is HW-based.
*
* @remarks This function is used to generate symmetric and private asymmetric keys.
*
- * @param[out] key Newly generated key (must be freed with yaca_key_free())
* @param[in] key_type Type of the key to be generated
* @param[in] key_bits Length of the key (in bits) to be generated
+ * @param[out] key Newly generated key (must be freed with yaca_key_free())
*
* @return #YACA_ERROR_NONE on success, negative on error
* @retval #YACA_ERROR_NONE Succesful
* @see #yaca_key_bits_e
* @see yaca_key_free()
*/
-int yaca_key_gen(yaca_key_h *key,
- yaca_key_type_e key_type,
- size_t key_bits);
+int yaca_key_gen(yaca_key_type_e key_type,
+ size_t key_bits,
+ yaca_key_h *key);
/**
* @brief Extracts public key from a private one.
if (yaca_get_iv_bits(algo, bcm, key_bits, &iv_bits) != YACA_ERROR_NONE)
goto exit;
- if (iv_bits > 0 && yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits) != YACA_ERROR_NONE)
+ if (iv_bits > 0 && yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
goto exit;
if (yaca_encrypt(algo, bcm, key, iv, lorem4096, LOREM4096_SIZE, &enc, &enc_size) != YACA_ERROR_NONE)
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
- if (yaca_key_gen(&key, key_type, key_bits) != YACA_ERROR_NONE)
+ if (yaca_key_gen(key_type, key_bits, &key) != YACA_ERROR_NONE)
return;
if (yaca_get_iv_bits(algo, bcm, key_bits, &iv_bits) != YACA_ERROR_NONE)
goto ex_key;
- if (iv_bits > 0 && yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits) != YACA_ERROR_NONE)
+ if (iv_bits > 0 && yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
goto ex_key;
/* Encryption */
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
- if (yaca_key_gen(&key, key_type, key_bits) != YACA_ERROR_NONE)
+ if (yaca_key_gen(key_type, key_bits, &key) != YACA_ERROR_NONE)
return;
/* IV generation */
- if (yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits) != YACA_ERROR_NONE)
+ if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
goto clean;
if ((aad = yaca_zalloc(aad_size)) == NULL)
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
- if (yaca_key_gen(&key, key_type, key_bits) != YACA_ERROR_NONE)
+ if (yaca_key_gen(key_type, key_bits, &key) != YACA_ERROR_NONE)
return;
/* IV generation */
- if (yaca_key_gen(&iv, YACA_KEY_TYPE_IV, iv_bits) != YACA_ERROR_NONE)
+ if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
goto clean;
if ((aad = yaca_zalloc(aad_size)) == NULL)
long size;
// generate private, public key
- ret = yaca_key_gen(&private_key, YACA_KEY_TYPE_DH_PRIV, YACA_KEY_2048BIT);
+ ret = yaca_key_gen(YACA_KEY_TYPE_DH_PRIV, YACA_KEY_2048BIT, &private_key);
if (ret != YACA_ERROR_NONE)
goto clean;
if (1 != fread(buffer, size, 1, fp))
goto clean;
- ret = yaca_key_import(&peer_key, YACA_KEY_TYPE_DH_PUB, NULL,
- buffer, size);
+ ret = yaca_key_import(YACA_KEY_TYPE_DH_PUB, NULL,
+ buffer, size, &peer_key);
if (ret != YACA_ERROR_NONE)
goto clean;
long size;
// generate private, public key
- ret = yaca_key_gen(&private_key, YACA_KEY_TYPE_EC_PRIV, YACA_KEY_CURVE_P256);
+ ret = yaca_key_gen(YACA_KEY_TYPE_EC_PRIV, YACA_KEY_CURVE_P256, &private_key);
if (ret != YACA_ERROR_NONE)
goto clean;
if (1 != fread(buffer, size, 1, fp))
goto clean;
- ret = yaca_key_import(&peer_key, YACA_KEY_TYPE_EC_PUB, NULL, buffer, size);
+ ret = yaca_key_import(YACA_KEY_TYPE_EC_PUB, NULL, buffer, size, &peer_key);
if (ret != YACA_ERROR_NONE)
goto clean;
ret = yaca_key_export(sym, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_BASE64, NULL, &b64, &b64_len);
if (ret != YACA_ERROR_NONE)
return ret;
- ret = yaca_key_import(&b64_imported, YACA_KEY_TYPE_SYMMETRIC, NULL, b64, b64_len);
+ ret = yaca_key_import(YACA_KEY_TYPE_SYMMETRIC, NULL, b64, b64_len, &b64_imported);
if (ret != YACA_ERROR_NONE)
goto free;
ret = yaca_key_export(sym, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_RAW, NULL, &raw, &raw_len);
if (ret != YACA_ERROR_NONE)
goto free;
- ret = yaca_key_import(&raw_imported, YACA_KEY_TYPE_SYMMETRIC, NULL, raw, raw_len);
+ ret = yaca_key_import(YACA_KEY_TYPE_SYMMETRIC, NULL, raw, raw_len, &raw_imported);
if (ret != YACA_ERROR_NONE)
goto free;
ret = yaca_key_export(priv, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_prv, &pem_prv_len);
if (ret != YACA_ERROR_NONE)
return ret;
- ret = yaca_key_import(&pem_prv_imported, priv_type, NULL, pem_prv, pem_prv_len);
+ ret = yaca_key_import(priv_type, NULL, pem_prv, pem_prv_len, &pem_prv_imported);
if (ret != YACA_ERROR_NONE)
goto free;
ret = yaca_key_export(priv, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_prv, &der_prv_len);
if (ret != YACA_ERROR_NONE)
goto free;
- ret = yaca_key_import(&der_prv_imported, priv_type, NULL, der_prv, der_prv_len);
+ ret = yaca_key_import(priv_type, NULL, der_prv, der_prv_len, &der_prv_imported);
if (ret != YACA_ERROR_NONE)
goto free;
ret = yaca_key_export(pub, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_pub, &pem_pub_len);
if (ret != YACA_ERROR_NONE)
goto free;
- ret = yaca_key_import(&pem_pub_imported, pub_type, NULL, pem_pub, pem_pub_len);
+ ret = yaca_key_import(pub_type, NULL, pem_pub, pem_pub_len, &pem_pub_imported);
if (ret != YACA_ERROR_NONE)
goto free;
ret = yaca_key_export(pub, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_pub, &der_pub_len);
if (ret != YACA_ERROR_NONE)
goto free;
- ret = yaca_key_import(&der_pub_imported, pub_type, NULL, der_pub, der_pub_len);
+ ret = yaca_key_import(pub_type, NULL, der_pub, der_pub_len, &der_pub_imported);
if (ret != YACA_ERROR_NONE)
goto free;
return ret;
}
- ret = yaca_key_import(&rsa_pub_from_cert, YACA_KEY_TYPE_RSA_PUB, NULL, pub, pub_len);
+ ret = yaca_key_import(YACA_KEY_TYPE_RSA_PUB, NULL, pub, pub_len, &rsa_pub_from_cert);
if (ret != YACA_ERROR_NONE)
goto free;
yaca_debug_set_error_cb(debug_func);
- ret = yaca_key_gen(&sym, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_1024BIT);
+ ret = yaca_key_gen(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_1024BIT, &sym);
if (ret != YACA_ERROR_NONE)
goto exit;
- ret = yaca_key_gen(&rsa_priv, YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT);
+ ret = yaca_key_gen(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT, &rsa_priv);
if (ret != YACA_ERROR_NONE)
goto free;
if (ret != YACA_ERROR_NONE)
goto free;
- ret = yaca_key_gen(&dsa_priv, YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_1024BIT);
+ ret = yaca_key_gen(YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_1024BIT, &dsa_priv);
if (ret != YACA_ERROR_NONE)
goto free;
if (ret != YACA_ERROR_NONE)
goto exit;
- ret = yaca_key_gen(&key, YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT);
+ ret = yaca_key_gen(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT, &key);
if (ret != YACA_ERROR_NONE)
goto exit;
password = NULL;
key = YACA_KEY_NULL;
- ret = yaca_key_import(&key, YACA_KEY_TYPE_RSA_PRIV, NULL, k, kl);
+ ret = yaca_key_import(YACA_KEY_TYPE_RSA_PRIV, NULL, k, kl, &key);
if (ret == YACA_ERROR_PASSWORD_INVALID) {
ret = read_stdin_line("decryption pass: ", &password);
if (ret != YACA_ERROR_NONE)
goto exit;
- ret = yaca_key_import(&key, YACA_KEY_TYPE_RSA_PRIV, password, k, kl);
+ ret = yaca_key_import(YACA_KEY_TYPE_RSA_PRIV, password, k, kl, &key);
if (ret == YACA_ERROR_PASSWORD_INVALID)
printf("invalid password\n");
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Generate key pair */
- if (yaca_key_gen(&key_priv, YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_4096BIT) != YACA_ERROR_NONE)
+ if (yaca_key_gen(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_4096BIT, &key_priv) != YACA_ERROR_NONE)
return;
if (yaca_key_extract_public(key_priv, &key_pub) != YACA_ERROR_NONE)
yaca_key_h pub = YACA_KEY_NULL;
// GENERATE
- if (yaca_key_gen(&prv, type, YACA_KEY_1024BIT) != YACA_ERROR_NONE)
+ if (yaca_key_gen(type, YACA_KEY_1024BIT, &prv) != YACA_ERROR_NONE)
return;
if (yaca_key_extract_public(prv, &pub) != YACA_ERROR_NONE)
yaca_key_h key = YACA_KEY_NULL;
// GENERATE
- if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT) != YACA_ERROR_NONE)
+ if (yaca_key_gen(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT, &key) != YACA_ERROR_NONE)
return;
// SIGN
yaca_key_h key = YACA_KEY_NULL;
// GENERATE
- if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT))
+ if (yaca_key_gen(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT, &key))
return;
// SIGN
yaca_padding_e padding = YACA_PADDING_PKCS1_PSS;
// GENERATE
- if (yaca_key_gen(&prv, type, YACA_KEY_1024BIT) != YACA_ERROR_NONE)
+ if (yaca_key_gen(type, YACA_KEY_1024BIT, &prv) != YACA_ERROR_NONE)
return;
if (yaca_key_extract_public(prv, &pub) != YACA_ERROR_NONE)
yaca_key_h key = YACA_KEY_NULL;
// GENERATE
- if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT) != YACA_ERROR_NONE)
+ if (yaca_key_gen(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT, &key) != YACA_ERROR_NONE)
return;
// SIGN
yaca_key_h key = YACA_KEY_NULL;
// GENERATE
- if (yaca_key_gen(&key, YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT))
+ if (yaca_key_gen(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_256BIT, &key))
return;
// SIGN
return YACA_ERROR_INVALID_ARGUMENT;
}
-API int yaca_key_import(yaca_key_h *key,
- yaca_key_type_e key_type,
+API int yaca_key_import(yaca_key_type_e key_type,
const char *password,
const char *data,
- size_t data_len)
+ size_t data_len,
+ yaca_key_h *key)
{
if (key == NULL || data == NULL || data_len == 0)
return YACA_ERROR_INVALID_ARGUMENT;
// TODO: this NEEDS random number generator initialized
// there is some other TODO elsewhere about it
-API int yaca_key_gen(yaca_key_h *key,
- yaca_key_type_e key_type,
- size_t key_bits)
+API int yaca_key_gen(yaca_key_type_e key_type,
+ size_t key_bits,
+ yaca_key_h *key)
{
int ret;
struct yaca_key_simple_s *nk_simple = NULL;