int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key);
/**
- * @brief Frees the key created by the library. Passing YACA_KEY_NULL is allowed.
+ * @brief Release the key created by the library. Passing YACA_KEY_NULL is allowed.
*
* @since_tizen 3.0
*
- * @param[in,out] key Key to be freed
+ * @param[in,out] key Key to be released
*
* @see yaca_key_import()
* @see yaca_key_export()
void encrypt_simple(const yaca_encrypt_algorithm_e algo,
const yaca_block_cipher_mode_e bcm,
- const size_t key_bits)
+ const size_t key_bit_len)
{
yaca_key_h key = YACA_KEY_NULL;
yaca_key_h iv = YACA_KEY_NULL;
char *enc = NULL;
char *dec = NULL;
- size_t enc_size;
- size_t dec_size;
- size_t iv_bits;
+ size_t enc_len;
+ size_t dec_len;
+ size_t iv_bit_len;
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
if (yaca_key_derive_pbkdf2("foo bar", "123456789", 10, 1000,
- YACA_DIGEST_SHA256, key_bits, &key) != YACA_ERROR_NONE)
+ YACA_DIGEST_SHA256, key_bit_len, &key) != YACA_ERROR_NONE)
return;
- if (yaca_encrypt_get_iv_bit_length(algo, bcm, key_bits, &iv_bits) != YACA_ERROR_NONE)
+ if (yaca_encrypt_get_iv_bit_length(algo, bcm, key_bit_len, &iv_bit_len) != YACA_ERROR_NONE)
goto exit;
- if (iv_bits > 0 && yaca_key_generate(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
+ if (iv_bit_len > 0 && yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv) != YACA_ERROR_NONE)
goto exit;
- if (yaca_simple_encrypt(algo, bcm, key, iv, lorem4096, LOREM4096_SIZE, &enc, &enc_size) != YACA_ERROR_NONE)
+ if (yaca_simple_encrypt(algo, bcm, key, iv, lorem4096, LOREM4096_SIZE, &enc, &enc_len) != YACA_ERROR_NONE)
goto exit;
- dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
+ dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
- if (yaca_simple_decrypt(algo, bcm, key, iv, enc, enc_size, &dec, &dec_size) != YACA_ERROR_NONE)
+ if (yaca_simple_decrypt(algo, bcm, key, iv, enc, enc_len, &dec, &dec_len) != YACA_ERROR_NONE)
goto exit;
- printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
+ printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
exit:
yaca_free(enc);
void encrypt_advanced(const yaca_encrypt_algorithm_e algo,
const yaca_block_cipher_mode_e bcm,
const yaca_key_type_e key_type,
- const size_t key_bits)
+ const size_t key_bit_len)
{
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h key = YACA_KEY_NULL;
yaca_key_h iv = YACA_KEY_NULL;
- size_t iv_bits;
+ size_t iv_bit_len;
char *enc = NULL;
char *dec = NULL;
- size_t enc_size;
- size_t dec_size;
+ size_t enc_len;
+ size_t dec_len;
size_t block_len;
size_t output_len;
- size_t out_size;
+ size_t written_len;
size_t rem;
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
- if (yaca_key_generate(key_type, key_bits, &key) != YACA_ERROR_NONE)
+ if (yaca_key_generate(key_type, key_bit_len, &key) != YACA_ERROR_NONE)
return;
- if (yaca_encrypt_get_iv_bit_length(algo, bcm, key_bits, &iv_bits) != YACA_ERROR_NONE)
+ if (yaca_encrypt_get_iv_bit_length(algo, bcm, key_bit_len, &iv_bit_len) != YACA_ERROR_NONE)
goto exit;
- if (iv_bits > 0 && yaca_key_generate(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
+ if (iv_bit_len > 0 && yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv) != YACA_ERROR_NONE)
goto exit;
/* Encryption */
goto exit;
/* Calculate max output: size of update + final chunks */
- enc_size = output_len + block_len;
- if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
+ enc_len = output_len + block_len;
+ if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
goto exit;
- out_size = enc_size;
- if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
+ written_len = enc_len;
+ if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = enc_size - out_size;
- if (yaca_encrypt_finalize(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
+ rem = enc_len - written_len;
+ if (yaca_encrypt_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- enc_size = rem + out_size;
+ enc_len = rem + written_len;
- dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
+ dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
yaca_context_destroy(ctx);
ctx = YACA_CONTEXT_NULL;
goto exit;
/* Calculate max output: size of update + final chunks */
- dec_size = output_len + block_len;
- if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
+ dec_len = output_len + block_len;
+ if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
goto exit;
- out_size = dec_size;
- if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
+ written_len = dec_len;
+ if (yaca_decrypt_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = dec_size - out_size;
- if (yaca_decrypt_finalize(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
+ rem = dec_len - written_len;
+ if (yaca_decrypt_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- dec_size = rem + out_size;
+ dec_len = rem + written_len;
- printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
+ printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
}
exit:
yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES;
yaca_block_cipher_mode_e bcm = YACA_BCM_ECB;
yaca_key_type_e key_type = YACA_KEY_TYPE_SYMMETRIC;
- size_t key_bits = YACA_KEY_LENGTH_256BIT;
+ size_t key_bit_len = YACA_KEY_LENGTH_256BIT;
- encrypt_simple(algo, bcm, key_bits);
- encrypt_advanced(algo, bcm, key_type, key_bits);
+ encrypt_simple(algo, bcm, key_bit_len);
+ encrypt_advanced(algo, bcm, key_type, key_bit_len);
algo = YACA_ENCRYPT_3DES_3TDEA;
bcm = YACA_BCM_OFB;
key_type = YACA_KEY_TYPE_DES;
- key_bits = YACA_KEY_LENGTH_192BIT;
+ key_bit_len = YACA_KEY_LENGTH_192BIT;
- encrypt_advanced(algo, bcm, key_type, key_bits);
+ encrypt_advanced(algo, bcm, key_type, key_bit_len);
algo = YACA_ENCRYPT_CAST5;
bcm = YACA_BCM_CFB;
key_type = YACA_KEY_TYPE_SYMMETRIC;
- key_bits = YACA_KEY_LENGTH_UNSAFE_40BIT;
+ key_bit_len = YACA_KEY_LENGTH_UNSAFE_40BIT;
- encrypt_simple(algo, bcm, key_bits);
- encrypt_advanced(algo, bcm, key_type, key_bits);
+ encrypt_simple(algo, bcm, key_bit_len);
+ encrypt_advanced(algo, bcm, key_type, key_bit_len);
algo = YACA_ENCRYPT_UNSAFE_RC2;
bcm = YACA_BCM_CBC;
key_type = YACA_KEY_TYPE_SYMMETRIC;
- key_bits = YACA_KEY_LENGTH_UNSAFE_8BIT;
+ key_bit_len = YACA_KEY_LENGTH_UNSAFE_8BIT;
- encrypt_simple(algo, bcm, key_bits);
- encrypt_advanced(algo, bcm, key_type, key_bits);
+ encrypt_simple(algo, bcm, key_bit_len);
+ encrypt_advanced(algo, bcm, key_type, key_bit_len);
algo = YACA_ENCRYPT_UNSAFE_RC4;
bcm = YACA_BCM_NONE;
key_type = YACA_KEY_TYPE_SYMMETRIC;
- key_bits = YACA_KEY_LENGTH_2048BIT;
+ key_bit_len = YACA_KEY_LENGTH_2048BIT;
- encrypt_simple(algo, bcm, key_bits);
- encrypt_advanced(algo, bcm, key_type, key_bits);
+ encrypt_simple(algo, bcm, key_bit_len);
+ encrypt_advanced(algo, bcm, key_type, key_bit_len);
yaca_cleanup();
yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES;
yaca_block_cipher_mode_e bcm = YACA_BCM_GCM;
yaca_key_type_e key_type = YACA_KEY_TYPE_SYMMETRIC;
- size_t key_bits = YACA_KEY_LENGTH_256BIT;
- size_t iv_bits = YACA_KEY_LENGTH_IV_128BIT;
+ size_t key_bit_len = YACA_KEY_LENGTH_256BIT;
+ size_t iv_bit_len = YACA_KEY_LENGTH_IV_128BIT;
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h key = YACA_KEY_NULL;
char *enc = NULL;
char *dec = NULL;
- size_t enc_size;
- size_t dec_size;
+ size_t enc_len;
+ size_t dec_len;
char *aad = NULL;
char *tag = NULL;
- size_t aad_size = 16;
- size_t tag_size = 16;
+ size_t aad_len = 16;
+ size_t tag_len = 16;
size_t block_len;
size_t output_len;
- size_t out_size;
+ size_t written_len;
size_t rem;
printf("AES GCM 256bit key encryption/decryption\n");
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
- if (yaca_key_generate(key_type, key_bits, &key) != YACA_ERROR_NONE)
+ if (yaca_key_generate(key_type, key_bit_len, &key) != YACA_ERROR_NONE)
return;
/* IV generation */
- if (yaca_key_generate(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
+ if (yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv) != YACA_ERROR_NONE)
goto exit;
- if (yaca_zalloc(aad_size, (void**)&aad) != YACA_ERROR_NONE)
+ if (yaca_zalloc(aad_len, (void**)&aad) != YACA_ERROR_NONE)
goto exit;
- if (yaca_randomize_bytes(aad, aad_size) != YACA_ERROR_NONE)
+ if (yaca_randomize_bytes(aad, aad_len) != YACA_ERROR_NONE)
goto exit;
- if (yaca_zalloc(tag_size, (void**)&tag) != YACA_ERROR_NONE)
+ if (yaca_zalloc(tag_len, (void**)&tag) != YACA_ERROR_NONE)
goto exit;
/* Encryption */
goto exit;
/* Provide any AAD data */
- if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
+ if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, aad, aad_len) != YACA_ERROR_NONE)
goto exit;
/* For the update */
goto exit;
/* Calculate max output: size of update + final chunks */
- enc_size = output_len + block_len;
- if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
+ enc_len = output_len + block_len;
+ if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
goto exit;
- out_size = enc_size;
- if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
+ written_len = enc_len;
+ if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = enc_size - out_size;
- if (yaca_encrypt_finalize(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
+ rem = enc_len - written_len;
+ if (yaca_encrypt_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- enc_size = rem + out_size;
+ enc_len = rem + written_len;
/* Set the tag length and get the tag after final encryption */
if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG_LEN,
- (void*)&tag_size, sizeof(tag_size)) != YACA_ERROR_NONE)
+ (void*)&tag_len, sizeof(tag_len)) != YACA_ERROR_NONE)
goto exit;
- if (yaca_context_get_property(ctx, YACA_PROPERTY_GCM_TAG, (void**)tag, &tag_size) != YACA_ERROR_NONE)
+ if (yaca_context_get_property(ctx, YACA_PROPERTY_GCM_TAG, (void**)tag, &tag_len) != YACA_ERROR_NONE)
goto exit;
- dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
+ dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
yaca_context_destroy(ctx);
ctx = YACA_CONTEXT_NULL;
goto exit;
/* Provide any AAD data */
- if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
+ if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, aad, aad_len) != YACA_ERROR_NONE)
goto exit;
/* For the update */
goto exit;
/* Calculate max output: size of update + final chunks */
- dec_size = output_len + block_len;
- if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
+ dec_len = output_len + block_len;
+ if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
goto exit;
- out_size = dec_size;
- if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
+ written_len = dec_len;
+ if (yaca_decrypt_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = dec_size - out_size;
+ rem = dec_len - written_len;
/* Set expected tag value before final decryption */
- if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, tag, tag_size) != YACA_ERROR_NONE)
+ if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, tag, tag_len) != YACA_ERROR_NONE)
goto exit;
- if (yaca_decrypt_finalize(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
+ if (yaca_decrypt_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- dec_size = rem + out_size;
+ dec_len = rem + written_len;
- printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
+ printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
}
exit:
yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES;
yaca_block_cipher_mode_e bcm = YACA_BCM_CCM;
yaca_key_type_e key_type = YACA_KEY_TYPE_SYMMETRIC;
- size_t key_bits = YACA_KEY_LENGTH_256BIT;
- size_t iv_bits = YACA_KEY_LENGTH_IV_64BIT;
+ size_t key_bit_len = YACA_KEY_LENGTH_256BIT;
+ size_t iv_bit_len = YACA_KEY_LENGTH_IV_64BIT;
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h key = YACA_KEY_NULL;
char *enc = NULL;
char *dec = NULL;
- size_t enc_size;
- size_t dec_size;
+ size_t enc_len;
+ size_t dec_len;
char *aad = NULL;
char *tag = NULL;
- size_t aad_size = 16;
- size_t tag_size = 12;
+ size_t aad_len = 16;
+ size_t tag_len = 12;
size_t block_len;
size_t output_len;
- size_t out_size;
+ size_t written_len;
size_t rem;
size_t len;
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Key generation */
- if (yaca_key_generate(key_type, key_bits, &key) != YACA_ERROR_NONE)
+ if (yaca_key_generate(key_type, key_bit_len, &key) != YACA_ERROR_NONE)
return;
/* IV generation */
- if (yaca_key_generate(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
+ if (yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv) != YACA_ERROR_NONE)
goto exit;
- if (yaca_zalloc(aad_size, (void**)&aad) != YACA_ERROR_NONE)
+ if (yaca_zalloc(aad_len, (void**)&aad) != YACA_ERROR_NONE)
goto exit;
- if (yaca_randomize_bytes(aad, aad_size) != YACA_ERROR_NONE)
+ if (yaca_randomize_bytes(aad, aad_len) != YACA_ERROR_NONE)
goto exit;
- if (yaca_zalloc(tag_size, (void**)&tag) != YACA_ERROR_NONE)
+ if (yaca_zalloc(tag_len, (void**)&tag) != YACA_ERROR_NONE)
goto exit;
/* Encryption */
/* Set tag length (optionally) */
if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG_LEN,
- (void*)&tag_size, sizeof(tag_size)) != YACA_ERROR_NONE)
+ (void*)&tag_len, sizeof(tag_len)) != YACA_ERROR_NONE)
goto exit;
/* The total plain text length must be passed (only needed if AAD is passed) */
if (yaca_encrypt_update(ctx, NULL, LOREM4096_SIZE , NULL, &len) != YACA_ERROR_NONE)
goto exit;
- if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
+ if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, aad, aad_len) != YACA_ERROR_NONE)
goto exit;
/* For the update */
goto exit;
/* Calculate max output: size of update + final chunks */
- enc_size = output_len + block_len;
- if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
+ enc_len = output_len + block_len;
+ if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
goto exit;
- out_size = enc_size;
- if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
+ written_len = enc_len;
+ if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = enc_size - out_size;
- if (yaca_encrypt_finalize(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
+ rem = enc_len - written_len;
+ if (yaca_encrypt_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- enc_size = rem + out_size;
+ enc_len = rem + written_len;
/* Get the tag after final encryption */
- if (yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)tag, &tag_size) != YACA_ERROR_NONE)
+ if (yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)tag, &tag_len) != YACA_ERROR_NONE)
goto exit;
- dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
+ dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
yaca_context_destroy(ctx);
ctx = YACA_CONTEXT_NULL;
goto exit;
/* Set expected tag value */
- if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG, tag, tag_size) != YACA_ERROR_NONE)
+ if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG, tag, tag_len) != YACA_ERROR_NONE)
goto exit;
/* The total encrypted text length must be passed (only needed if AAD is passed) */
- if (yaca_decrypt_update(ctx, NULL, enc_size , NULL, &len) != YACA_ERROR_NONE)
+ if (yaca_decrypt_update(ctx, NULL, enc_len , NULL, &len) != YACA_ERROR_NONE)
goto exit;
- if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
+ if (yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, aad, aad_len) != YACA_ERROR_NONE)
goto exit;
/* For the update */
goto exit;
/* Calculate max output: size of update + final chunks */
- dec_size = output_len + block_len;
- if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
+ dec_len = output_len + block_len;
+ if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
goto exit;
- out_size = dec_size;
+ written_len = dec_len;
/* The tag verify is performed when you call the final yaca_decrypt_update(),
* there is no call to yaca_decrypt_finalize() */
- if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
+ if (yaca_decrypt_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
goto exit;
- dec_size = out_size;
+ dec_len = written_len;
- printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
+ printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
}
exit:
#include "misc.h"
-void dump_hex(const char *buf, size_t dump_size, const char *fmt, ...)
+void dump_hex(const char *buf, size_t dump_len, const char *fmt, ...)
{
va_list ap;
vprintf(fmt, ap);
va_end(ap);
putchar('\n');
- BIO_dump_fp(stdout, buf, dump_size);
+ BIO_dump_fp(stdout, buf, dump_len);
}
void debug_func(const char *buf)
#include <stddef.h>
-void dump_hex(const char *buf, size_t dump_size, const char *fmt, ...);
+void dump_hex(const char *buf, size_t dump_len, const char *fmt, ...);
void debug_func(const char *buf);
void encrypt_seal(const yaca_encrypt_algorithm_e algo,
const yaca_block_cipher_mode_e bcm,
- const size_t key_bits)
+ const size_t key_bit_len)
{
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h key_pub = YACA_KEY_NULL;
size_t block_len;
size_t output_len;
- size_t out_len;
+ size_t written_len;
size_t rem;
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Encrypt a.k.a. seal */
{
- if (yaca_seal_initialize(&ctx, key_pub, algo, bcm, key_bits, &sym_key, &iv) != YACA_ERROR_NONE)
+ if (yaca_seal_initialize(&ctx, key_pub, algo, bcm, key_bit_len, &sym_key, &iv) != YACA_ERROR_NONE)
goto exit;
/* For the update */
goto exit;
/* Seal and finalize */
- out_len = enc_len;
- if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_len) != YACA_ERROR_NONE)
+ written_len = enc_len;
+ if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = enc_len - out_len;
- if (yaca_seal_finalize(ctx, enc + out_len, &rem) != YACA_ERROR_NONE)
+ rem = enc_len - written_len;
+ if (yaca_seal_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- enc_len = rem + out_len;
+ enc_len = rem + written_len;
dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
/* Decrypt a.k.a. open */
{
- if (yaca_open_initialize(&ctx, key_priv, algo, bcm, key_bits, sym_key, iv) != YACA_ERROR_NONE)
+ if (yaca_open_initialize(&ctx, key_priv, algo, bcm, key_bit_len, sym_key, iv) != YACA_ERROR_NONE)
goto exit;
/* For the update */
goto exit;
/* Open and finalize */
- out_len = dec_len;
- if (yaca_open_update(ctx, enc, enc_len, dec, &out_len) != YACA_ERROR_NONE)
+ written_len = dec_len;
+ if (yaca_open_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = dec_len - out_len;
- if (yaca_open_finalize(ctx, dec + out_len, &rem) != YACA_ERROR_NONE)
+ rem = dec_len - written_len;
+ if (yaca_open_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- dec_len = rem + out_len;
+ dec_len = rem + written_len;
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
}
{
yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES;
yaca_block_cipher_mode_e bcm = YACA_BCM_GCM;
- size_t key_bits = YACA_KEY_LENGTH_256BIT;
+ size_t key_bit_len = YACA_KEY_LENGTH_256BIT;
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h key_pub = YACA_KEY_NULL;
size_t block_len;
size_t output_len;
- size_t out_len;
+ size_t written_len;
size_t rem;
printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
/* Encryption */
{
- if (yaca_seal_initialize(&ctx, key_pub, algo, bcm, key_bits, &sym_key, &iv) != YACA_ERROR_NONE)
+ if (yaca_seal_initialize(&ctx, key_pub, algo, bcm, key_bit_len, &sym_key, &iv) != YACA_ERROR_NONE)
goto exit;
/* Provide any AAD data */
if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
goto exit;
- out_len = enc_len;
- if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_len) != YACA_ERROR_NONE)
+ written_len = enc_len;
+ if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = enc_len - out_len;
- if (yaca_seal_finalize(ctx, enc + out_len, &rem) != YACA_ERROR_NONE)
+ rem = enc_len - written_len;
+ if (yaca_seal_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- enc_len = rem + out_len;
+ enc_len = rem + written_len;
/* Set the tag length and get the tag after final encryption */
if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG_LEN,
/* Decryption */
{
- if (yaca_open_initialize(&ctx, key_priv, algo, bcm, key_bits, sym_key, iv) != YACA_ERROR_NONE)
+ if (yaca_open_initialize(&ctx, key_priv, algo, bcm, key_bit_len, sym_key, iv) != YACA_ERROR_NONE)
goto exit;
/* Provide any AAD data */
if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
goto exit;
- out_len = dec_len;
- if (yaca_open_update(ctx, enc, enc_len, dec, &out_len) != YACA_ERROR_NONE)
+ written_len = dec_len;
+ if (yaca_open_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = dec_len - out_len;
+ rem = dec_len - written_len;
/* Set expected tag value before final decryption */
if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, tag, tag_len) != YACA_ERROR_NONE)
goto exit;
- if (yaca_open_finalize(ctx, dec + out_len, &rem) != YACA_ERROR_NONE)
+ if (yaca_open_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- dec_len = rem + out_len;
+ dec_len = rem + written_len;
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
}
{
yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES;
yaca_block_cipher_mode_e bcm = YACA_BCM_CCM;
- size_t key_bits = YACA_KEY_LENGTH_192BIT;
+ size_t key_bit_len = YACA_KEY_LENGTH_192BIT;
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h key_pub = YACA_KEY_NULL;
size_t block_len;
size_t output_len;
- size_t out_len;
+ size_t written_len;
size_t rem;
size_t len;
/* Encryption */
{
- if (yaca_seal_initialize(&ctx, key_pub, algo, bcm, key_bits, &sym_key, &iv) != YACA_ERROR_NONE)
+ if (yaca_seal_initialize(&ctx, key_pub, algo, bcm, key_bit_len, &sym_key, &iv) != YACA_ERROR_NONE)
goto exit;
/* Set tag length (optionally) */
if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
goto exit;
- out_len = enc_len;
- if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_len) != YACA_ERROR_NONE)
+ written_len = enc_len;
+ if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
goto exit;
- rem = enc_len - out_len;
- if (yaca_seal_finalize(ctx, enc + out_len, &rem) != YACA_ERROR_NONE)
+ rem = enc_len - written_len;
+ if (yaca_seal_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
goto exit;
- enc_len = rem + out_len;
+ enc_len = rem + written_len;
/* Get the tag after final encryption */
if (yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)tag, &tag_len) != YACA_ERROR_NONE)
/* Decryption */
{
- if (yaca_open_initialize(&ctx, key_priv, algo, bcm, key_bits, sym_key, iv) != YACA_ERROR_NONE)
+ if (yaca_open_initialize(&ctx, key_priv, algo, bcm, key_bit_len, sym_key, iv) != YACA_ERROR_NONE)
goto exit;
/* Set expected tag value */
if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
goto exit;
- out_len = dec_len;
+ written_len = dec_len;
/* The tag verify is performed when you call the final yaca_open_update(),
* there is no call to yaca_open_finalize() */
- if (yaca_open_update(ctx, enc, enc_len, dec, &out_len) != YACA_ERROR_NONE)
+ if (yaca_open_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
goto exit;
- dec_len = out_len;
+ dec_len = written_len;
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
}
printf("AES CBC 256bit key seal/open\n");
yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES;
yaca_block_cipher_mode_e bcm = YACA_BCM_CBC;
- size_t key_bits = YACA_KEY_LENGTH_256BIT;
- encrypt_seal(algo, bcm, key_bits);
+ size_t key_bit_len = YACA_KEY_LENGTH_256BIT;
+ encrypt_seal(algo, bcm, key_bit_len);
printf("3DES 192bit key seal/open\n");
algo = YACA_ENCRYPT_3DES_3TDEA;
bcm = YACA_BCM_CFB;
- key_bits = YACA_KEY_LENGTH_192BIT;
- encrypt_seal(algo, bcm, key_bits);
-
- printf("RC4 40bit key seal/open\n");
- algo = YACA_ENCRYPT_UNSAFE_RC4;
- bcm = YACA_BCM_NONE;
- key_bits = YACA_KEY_LENGTH_UNSAFE_40BIT;
- encrypt_seal(algo, bcm, key_bits);
+ key_bit_len = YACA_KEY_LENGTH_192BIT;
+ encrypt_seal(algo, bcm, key_bit_len);
+
+ printf("RC2 40bit key seal/open\n");
+ algo = YACA_ENCRYPT_UNSAFE_RC2;
+ bcm = YACA_BCM_OFB;
+ key_bit_len = YACA_KEY_LENGTH_UNSAFE_40BIT;
+ encrypt_seal(algo, bcm, key_bit_len);
printf("AES GCM 256bit key seal/open\n");
encrypt_seal_aes_gcm();
General design:
- All memory allocated by API should be freed with yaca_free()
- - Contexts and keys should be freed with yaca_context_destroy()/yaca_key_destroy()
+ - Contexts and keys should be released with yaca_context_destroy()/yaca_key_destroy()
- Function names: yaca_<operation/object>_<function>; Ex: yaca_verify_initialize()
- Simplified/Simple functions don't have <operation/object> part, but have <simple> prefix
- Enums: YACA_<concept>_<value>; Ex: YACA_KEY_LENGTH_256BIT
{
int ret = YACA_ERROR_NONE;
- /* no calling yaca_initalize() twice on the same thread */
+ /* no calling yaca_initialize() twice on the same thread */
if (current_thread_initialized)
return YACA_ERROR_INTERNAL;
/*
* TODO:
* - We should also decide on Openssl config.
- * - Here's a good tutorial for initalization and cleanup:
+ * - Here's a good tutorial for initialization and cleanup:
* https://wiki.openssl.org/index.php/Library_Initialization
* - We should also initialize the entropy for random number generator:
* https://wiki.openssl.org/index.php/Random_Numbers#Initialization
API int yaca_context_set_property(yaca_context_h ctx, yaca_property_e property,
const void *value, size_t value_len)
{
- if (ctx == YACA_CONTEXT_NULL || ctx->set_param == NULL)
+ if (ctx == YACA_CONTEXT_NULL || ctx->set_property == NULL)
return YACA_ERROR_INVALID_PARAMETER;
- return ctx->set_param(ctx, property, value, value_len);
+ return ctx->set_property(ctx, property, value, value_len);
}
API int yaca_context_get_property(const yaca_context_h ctx, yaca_property_e property,
void **value, size_t *value_len)
{
- if (ctx == YACA_CONTEXT_NULL || ctx->get_param == NULL)
+ if (ctx == YACA_CONTEXT_NULL || ctx->get_property == NULL)
return YACA_ERROR_INVALID_PARAMETER;
- return ctx->get_param(ctx, property, value, value_len);
+ return ctx->get_property(ctx, property, value, value_len);
}
API void yaca_context_destroy(yaca_context_h ctx)
{
if (ctx != YACA_CONTEXT_NULL) {
- assert(ctx->ctx_destroy != NULL);
- ctx->ctx_destroy(ctx);
+ assert(ctx->context_destroy != NULL);
+ ctx->context_destroy(ctx);
yaca_free(ctx);
}
}
#include "internal.h"
-struct yaca_digest_ctx_s {
+struct yaca_digest_context_s {
struct yaca_context_s ctx;
- EVP_MD_CTX *mdctx;
+ EVP_MD_CTX *md_ctx;
};
-static struct yaca_digest_ctx_s *get_digest_ctx(const yaca_context_h ctx)
+static struct yaca_digest_context_s *get_digest_context(const yaca_context_h ctx)
{
if (ctx == YACA_CONTEXT_NULL)
return NULL;
switch (ctx->type) {
- case YACA_CTX_DIGEST:
- return (struct yaca_digest_ctx_s *)ctx;
+ case YACA_CONTEXT_DIGEST:
+ return (struct yaca_digest_context_s *)ctx;
default:
return NULL;
}
{
assert(output_len != NULL);
- struct yaca_digest_ctx_s *c = get_digest_ctx(ctx);
+ struct yaca_digest_context_s *c = get_digest_context(ctx);
if (c == NULL || input_len != 0)
return YACA_ERROR_INVALID_PARAMETER;
- int md_size = EVP_MD_CTX_size(c->mdctx);
+ int md_size = EVP_MD_CTX_size(c->md_ctx);
if (md_size <= 0)
return YACA_ERROR_INTERNAL;
static void destroy_digest_context(yaca_context_h ctx)
{
- struct yaca_digest_ctx_s *c = get_digest_ctx(ctx);
+ struct yaca_digest_context_s *c = get_digest_context(ctx);
if (c == NULL)
return;
- EVP_MD_CTX_destroy(c->mdctx);
- c->mdctx = NULL;
+ EVP_MD_CTX_destroy(c->md_ctx);
+ c->md_ctx = NULL;
}
int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md)
API int yaca_digest_initialize(yaca_context_h *ctx, yaca_digest_algorithm_e algo)
{
int ret;
- struct yaca_digest_ctx_s *nc = NULL;
+ struct yaca_digest_context_s *nc = NULL;
const EVP_MD *md;
if (ctx == NULL)
return YACA_ERROR_INVALID_PARAMETER;
- ret = yaca_zalloc(sizeof(struct yaca_digest_ctx_s), (void**)&nc);
+ ret = yaca_zalloc(sizeof(struct yaca_digest_context_s), (void**)&nc);
if (ret != YACA_ERROR_NONE)
return ret;
- nc->ctx.type = YACA_CTX_DIGEST;
- nc->ctx.ctx_destroy = destroy_digest_context;
+ nc->ctx.type = YACA_CONTEXT_DIGEST;
+ nc->ctx.context_destroy = destroy_digest_context;
nc->ctx.get_output_length = get_digest_output_length;
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
goto exit;
- nc->mdctx = EVP_MD_CTX_create();
- if (nc->mdctx == NULL) {
+ nc->md_ctx = EVP_MD_CTX_create();
+ if (nc->md_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
}
- ret = EVP_DigestInit(nc->mdctx, md);
+ ret = EVP_DigestInit(nc->md_ctx, md);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
API int yaca_digest_update(yaca_context_h ctx, const char *data, size_t data_len)
{
- struct yaca_digest_ctx_s *c = get_digest_ctx(ctx);
+ struct yaca_digest_context_s *c = get_digest_context(ctx);
int ret;
if (c == NULL || data == NULL || data_len == 0)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_DigestUpdate(c->mdctx, data, data_len);
+ ret = EVP_DigestUpdate(c->md_ctx, data, data_len);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
API int yaca_digest_finalize(yaca_context_h ctx, char *digest, size_t *digest_len)
{
- struct yaca_digest_ctx_s *c = get_digest_ctx(ctx);
+ struct yaca_digest_context_s *c = get_digest_context(ctx);
int ret;
unsigned len = 0;
if (*digest_len == 0 || *digest_len > UINT_MAX) /* DigestFinal accepts UINT */
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_DigestFinal_ex(c->mdctx, (unsigned char*)digest, &len);
+ ret = EVP_DigestFinal_ex(c->md_ctx, (unsigned char*)digest, &len);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
return NULL;
switch (ctx->type) {
- case YACA_CTX_ENCRYPT:
+ case YACA_CONTEXT_ENCRYPT:
return (struct yaca_encrypt_context_s *)ctx;
default:
return NULL;
int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
yaca_block_cipher_mode_e bcm,
- size_t key_bits,
+ size_t key_bit_len,
const EVP_CIPHER **cipher)
{
char cipher_name[32];
const EVP_CIPHER *lcipher;
int ret;
- if (algo_name == NULL || bcm_name == NULL || key_bits == 0 ||
- cipher == NULL)
+ if (algo_name == NULL || bcm_name == NULL || key_bit_len == 0 || cipher == NULL)
return YACA_ERROR_INVALID_PARAMETER;
switch (algo) {
case YACA_ENCRYPT_AES:
ret = snprintf(cipher_name, sizeof(cipher_name), "%s-%zu-%s",
- algo_name, key_bits, bcm_name);
+ algo_name, key_bit_len, bcm_name);
break;
case YACA_ENCRYPT_UNSAFE_DES:
case YACA_ENCRYPT_UNSAFE_RC2:
yaca_block_cipher_mode_e bcm,
const yaca_key_h sym_key,
const yaca_key_h iv,
- enum encrypt_op_type op_type)
+ enum encrypt_op_type_e op_type)
{
const struct yaca_key_simple_s *lkey;
const struct yaca_key_simple_s *liv;
struct yaca_encrypt_context_s *nc;
const EVP_CIPHER *cipher;
- size_t key_bits;
+ size_t key_bit_len;
unsigned char *iv_data = NULL;
- size_t iv_bits;
- size_t iv_bits_check;
+ size_t iv_bit_len;
+ size_t iv_bit_len_check;
int ret;
if (ctx == NULL || sym_key == YACA_KEY_NULL)
if (ret != YACA_ERROR_NONE)
return ret;
- nc->ctx.type = YACA_CTX_ENCRYPT;
- nc->ctx.ctx_destroy = destroy_encrypt_context;
+ nc->ctx.type = YACA_CONTEXT_ENCRYPT;
+ nc->ctx.context_destroy = destroy_encrypt_context;
nc->ctx.get_output_length = get_encrypt_output_length;
- nc->ctx.set_param = set_encrypt_property;
- nc->ctx.get_param = get_encrypt_property;
+ nc->ctx.set_property = set_encrypt_property;
+ nc->ctx.get_property = get_encrypt_property;
nc->op_type = op_type;
nc->tag_len = 0;
- ret = yaca_key_get_bit_length(sym_key, &key_bits);
+ ret = yaca_key_get_bit_length(sym_key, &key_bit_len);
if (ret != YACA_ERROR_NONE)
goto exit;
- ret = encrypt_get_algorithm(algo, bcm, key_bits, &cipher);
+ ret = encrypt_get_algorithm(algo, bcm, key_bit_len, &cipher);
if (ret != YACA_ERROR_NONE)
goto exit;
goto exit;
}
- iv_bits = ret * 8;
- if (iv_bits == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
+ iv_bit_len = ret * 8;
+ if (iv_bit_len == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
- if (iv_bits != 0) { /* cipher requires iv*/
+ if (iv_bit_len != 0) { /* cipher requires iv*/
liv = key_get_simple(iv);
if (liv == NULL) { /* iv was not provided */
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
- ret = yaca_key_get_bit_length(iv, &iv_bits_check);
+ ret = yaca_key_get_bit_length(iv, &iv_bit_len_check);
if (ret != YACA_ERROR_NONE) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
/* IV length doesn't match cipher (GCM & CCM supports variable IV length) */
- if (iv_bits != iv_bits_check &&
+ if (iv_bit_len != iv_bit_len_check &&
bcm != YACA_BCM_GCM &&
bcm != YACA_BCM_CCM) {
ret = YACA_ERROR_INVALID_PARAMETER;
}
/* Handling of algorithms with variable key length */
- ret = EVP_CIPHER_CTX_set_key_length(nc->cipher_ctx, key_bits / 8);
+ ret = EVP_CIPHER_CTX_set_key_length(nc->cipher_ctx, key_bit_len / 8);
if (ret != 1) {
ret = YACA_ERROR_INVALID_PARAMETER;
ERROR_DUMP(ret);
}
/* Handling of algorithms with variable IV length */
- if (iv_bits != iv_bits_check) {
+ if (iv_bit_len != iv_bit_len_check) {
if (bcm == YACA_BCM_GCM)
ret = EVP_CIPHER_CTX_ctrl(nc->cipher_ctx, EVP_CTRL_GCM_SET_IVLEN,
- iv_bits_check / 8, NULL);
+ iv_bit_len_check / 8, NULL);
if (bcm == YACA_BCM_CCM)
ret = EVP_CIPHER_CTX_ctrl(nc->cipher_ctx, EVP_CTRL_CCM_SET_IVLEN,
- iv_bits_check / 8, NULL);
+ iv_bit_len_check / 8, NULL);
if (ret != 1) {
ret = YACA_ERROR_INVALID_PARAMETER;
int encrypt_update(yaca_context_h ctx,
const unsigned char *input, size_t input_len,
unsigned char *output, size_t *output_len,
- enum encrypt_op_type op_type)
+ enum encrypt_op_type_e op_type)
{
struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
int ret;
int encrypt_finalize(yaca_context_h ctx,
unsigned char *output, size_t *output_len,
- enum encrypt_op_type op_type)
+ enum encrypt_op_type_e op_type)
{
struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
int ret;
#define API __attribute__ ((visibility("default")))
#define UNUSED __attribute__((unused))
-enum yaca_ctx_type_e {
- YACA_CTX_INVALID = 0,
- YACA_CTX_DIGEST,
- YACA_CTX_SIGN,
- YACA_CTX_ENCRYPT
+enum yaca_context_type_e {
+ YACA_CONTEXT_INVALID = 0,
+ YACA_CONTEXT_DIGEST,
+ YACA_CONTEXT_SIGN,
+ YACA_CONTEXT_ENCRYPT
};
-enum encrypt_op_type {
+enum encrypt_op_type_e {
OP_ENCRYPT = 0,
OP_DECRYPT = 1,
OP_SEAL = 2,
/* Base structure for crypto contexts - to be inherited */
struct yaca_context_s {
- enum yaca_ctx_type_e type;
+ enum yaca_context_type_e type;
- void (*ctx_destroy)(const yaca_context_h ctx);
+ void (*context_destroy)(const yaca_context_h ctx);
int (*get_output_length)(const yaca_context_h ctx, size_t input_len, size_t *output_len);
- int (*set_param)(yaca_context_h ctx, yaca_property_e param,
- const void *value, size_t value_len);
- int (*get_param)(const yaca_context_h ctx, yaca_property_e param,
- void **value, size_t *value_len);
+ int (*set_property)(yaca_context_h ctx, yaca_property_e property,
+ const void *value, size_t value_len);
+ int (*get_property)(const yaca_context_h ctx, yaca_property_e property,
+ void **value, size_t *value_len);
};
struct yaca_encrypt_context_s {
struct yaca_context_s ctx;
EVP_CIPHER_CTX *cipher_ctx;
- enum encrypt_op_type op_type; /* Operation context was created for */
+ enum encrypt_op_type_e op_type; /* Operation context was created for */
size_t tag_len;
};
struct yaca_key_simple_s {
struct yaca_key_s key;
- size_t bits;
+ size_t bit_len;
char d[];
};
int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
yaca_block_cipher_mode_e bcm,
- size_t key_bits,
+ size_t key_bit_len,
const EVP_CIPHER **cipher);
int encrypt_update(yaca_context_h ctx,
const unsigned char *input, size_t input_len,
unsigned char *output, size_t *output_len,
- enum encrypt_op_type op_type);
+ enum encrypt_op_type_e op_type);
int encrypt_finalize(yaca_context_h ctx,
unsigned char *output, size_t *output_len,
- enum encrypt_op_type op_type);
+ enum encrypt_op_type_e op_type);
struct yaca_key_simple_s *key_get_simple(const yaca_key_h key);
struct yaca_key_evp_s *key_get_evp(const yaca_key_h key);
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/des.h>
-#include <openssl/crypto.h>
#include <yaca_crypto.h>
#include <yaca_error.h>
return ret;
}
- /* key_bits has to fit in size_t */
+ /* key_bit_len has to fit in size_t */
if (key_data_len > SIZE_MAX / 8) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
/* DES key length verification */
if (key_type == YACA_KEY_TYPE_DES) {
- size_t key_bits = key_data_len * 8;
- if (key_bits != YACA_KEY_LENGTH_UNSAFE_64BIT &&
- key_bits != YACA_KEY_LENGTH_UNSAFE_128BIT &&
- key_bits != YACA_KEY_LENGTH_192BIT) {
+ size_t key_bit_len = key_data_len * 8;
+ if (key_bit_len != YACA_KEY_LENGTH_UNSAFE_64BIT &&
+ key_bit_len != YACA_KEY_LENGTH_UNSAFE_128BIT &&
+ key_bit_len != YACA_KEY_LENGTH_192BIT) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
goto exit;
memcpy(nk->d, key_data, key_data_len);
- nk->bits = key_data_len * 8;
+ nk->bit_len = key_data_len * 8;
nk->key.type = key_type;
*key = (yaca_key_h)nk;
assert(data != NULL);
assert(data_len != NULL);
- size_t key_len = simple_key->bits / 8;
+ size_t key_len = simple_key->bit_len / 8;
assert(key_len > 0);
assert(data_len != NULL);
int ret;
- size_t key_len = simple_key->bits / 8;
+ size_t key_len = simple_key->bit_len / 8;
BIO *b64;
BIO *mem;
char *bio_data;
return ret;
}
-int gen_simple(struct yaca_key_simple_s **out, size_t key_bits)
+int generate_simple(struct yaca_key_simple_s **out, size_t key_bit_len)
{
assert(out != NULL);
int ret;
struct yaca_key_simple_s *nk;
- size_t key_byte_len = key_bits / 8;
+ size_t key_byte_len = key_bit_len / 8;
ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len, (void**)&nk);
if (ret != YACA_ERROR_NONE)
return ret;
- nk->bits = key_bits;
+ nk->bit_len = key_bit_len;
ret = yaca_randomize_bytes(nk->d, key_byte_len);
if (ret != YACA_ERROR_NONE)
return YACA_ERROR_NONE;
}
-int gen_simple_des(struct yaca_key_simple_s **out, size_t key_bits)
+int generate_simple_des(struct yaca_key_simple_s **out, size_t key_bit_len)
{
assert(out != NULL);
- if (key_bits != YACA_KEY_LENGTH_UNSAFE_64BIT &&
- key_bits != YACA_KEY_LENGTH_UNSAFE_128BIT &&
- key_bits != YACA_KEY_LENGTH_192BIT)
+ if (key_bit_len != YACA_KEY_LENGTH_UNSAFE_64BIT &&
+ key_bit_len != YACA_KEY_LENGTH_UNSAFE_128BIT &&
+ key_bit_len != YACA_KEY_LENGTH_192BIT)
return YACA_ERROR_INVALID_PARAMETER;
int ret;
struct yaca_key_simple_s *nk;
- size_t key_byte_len = key_bits / 8;
+ size_t key_byte_len = key_bit_len / 8;
ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len, (void**)&nk);
if (ret != YACA_ERROR_NONE)
}
}
- nk->bits = key_bits;
+ nk->bit_len = key_bit_len;
*out = nk;
nk = NULL;
ret = YACA_ERROR_NONE;
return ret;
}
-// TODO: consider merging gen_evp_*, they share awful lot of common code
-int gen_evp_rsa(struct yaca_key_evp_s **out, size_t key_bits)
+// TODO: consider merging generate_evp_*, they share awful lot of common code
+int generate_evp_rsa(struct yaca_key_evp_s **out, size_t key_bit_len)
{
assert(out != NULL);
- assert(key_bits > 0);
- assert(key_bits % 8 == 0);
+ assert(key_bit_len > 0);
+ assert(key_bit_len % 8 == 0);
int ret;
struct yaca_key_evp_s *nk;
goto exit;
}
- ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bits);
+ ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bit_len);
if (ret != 1) {
ret = ERROR_HANDLE();
goto exit;
return ret;
}
-int gen_evp_dsa(struct yaca_key_evp_s **out, size_t key_bits)
+int generate_evp_dsa(struct yaca_key_evp_s **out, size_t key_bit_len)
{
assert(out != NULL);
- assert(key_bits > 0);
- assert(key_bits % 8 == 0);
+ assert(key_bit_len > 0);
+ assert(key_bit_len % 8 == 0);
/* Openssl generates 512-bit key for key lengths smaller than 512. It also
* rounds key size to multiplication of 64. */
- if (key_bits < 512 || key_bits % 64 != 0)
+ if (key_bit_len < 512 || key_bit_len % 64 != 0)
return YACA_ERROR_INVALID_PARAMETER;
int ret;
goto exit;
}
- ret = EVP_PKEY_CTX_set_dsa_paramgen_bits(pctx, key_bits);
+ ret = EVP_PKEY_CTX_set_dsa_paramgen_bits(pctx, key_bit_len);
if (ret != 1) {
ret = ERROR_HANDLE();
goto exit;
k = (struct yaca_key_simple_s *)key;
/* sanity check */
- assert(k->bits != 0);
- assert(k->bits % 8 == 0);
+ assert(k->bit_len != 0);
+ assert(k->bit_len % 8 == 0);
assert(k->d != NULL);
return k;
assert(key != NULL);
struct yaca_key_simple_s *copy;
- size_t size = sizeof(struct yaca_key_simple_s) + key->bits / 8;
+ size_t size = sizeof(struct yaca_key_simple_s) + key->bit_len / 8;
ret = yaca_zalloc(size, (void**)©);
if (ret != YACA_ERROR_NONE)
return YACA_ERROR_INVALID_PARAMETER;
if (simple_key != NULL) {
- *key_bit_len = simple_key->bits;
+ *key_bit_len = simple_key->bit_len;
return YACA_ERROR_NONE;
}
switch (key_type) {
case YACA_KEY_TYPE_SYMMETRIC:
case YACA_KEY_TYPE_IV:
- ret = gen_simple(&nk_simple, key_bit_len);
+ ret = generate_simple(&nk_simple, key_bit_len);
break;
case YACA_KEY_TYPE_DES:
- ret = gen_simple_des(&nk_simple, key_bit_len);
+ ret = generate_simple_des(&nk_simple, key_bit_len);
break;
case YACA_KEY_TYPE_RSA_PRIV:
- ret = gen_evp_rsa(&nk_evp, key_bit_len);
+ ret = generate_evp_rsa(&nk_evp, key_bit_len);
break;
case YACA_KEY_TYPE_DSA_PRIV:
- ret = gen_evp_dsa(&nk_evp, key_bit_len);
+ ret = generate_evp_dsa(&nk_evp, key_bit_len);
break;
// case YACA_KEY_TYPE_DH_PRIV:
// case YACA_KEY_TYPE_EC_PRIV:
iterations == 0 || key_bit_len == 0 || key == NULL)
return YACA_ERROR_INVALID_PARAMETER;
- if (key_bit_len % 8) /* Key length must be multiple of 8-bits */
+ if (key_bit_len % 8) /* Key length must be multiple of 8-bit_len */
return YACA_ERROR_INVALID_PARAMETER;
if (iterations > INT_MAX) /* OpenSSL limitation */
if (ret != YACA_ERROR_NONE)
return ret;
- nk->bits = key_bit_len;
+ nk->bit_len = key_bit_len;
nk->key.type = YACA_KEY_TYPE_SYMMETRIC; // TODO: how to handle other keys?
ret = PKCS5_PBKDF2_HMAC(password, -1, (const unsigned char*)salt,
if (ret != YACA_ERROR_NONE)
return ret;
- nc->ctx.type = YACA_CTX_ENCRYPT;
- nc->ctx.ctx_destroy = destroy_encrypt_context;
+ nc->ctx.type = YACA_CONTEXT_ENCRYPT;
+ nc->ctx.context_destroy = destroy_encrypt_context;
nc->ctx.get_output_length = get_encrypt_output_length;
- nc->ctx.set_param = set_encrypt_property;
- nc->ctx.get_param = get_encrypt_property;
+ nc->ctx.set_property = set_encrypt_property;
+ nc->ctx.get_property = get_encrypt_property;
nc->op_type = OP_SEAL;
nc->tag_len = 0;
goto exit;
}
- lkey->bits = key_data_length * 8;
+ lkey->bit_len = key_data_length * 8;
lkey->key.type = YACA_KEY_TYPE_SYMMETRIC;
*sym_key = (yaca_key_h)lkey;
lkey = NULL;
if (iv_length > 0) {
- liv->bits = iv_length * 8;
+ liv->bit_len = iv_length * 8;
liv->key.type = YACA_KEY_TYPE_IV;
*iv = (yaca_key_h)liv;
liv = NULL;
struct yaca_encrypt_context_s *nc;
const EVP_CIPHER *cipher;
unsigned char *iv_data = NULL;
- size_t iv_bits;
- size_t iv_bits_check;
+ size_t iv_bit_len;
+ size_t iv_bit_len_check;
int ret;
if (ctx == NULL || prv_key == YACA_KEY_NULL || sym_key == YACA_KEY_NULL)
if (ret != YACA_ERROR_NONE)
return ret;
- nc->ctx.type = YACA_CTX_ENCRYPT;
- nc->ctx.ctx_destroy = destroy_encrypt_context;
+ nc->ctx.type = YACA_CONTEXT_ENCRYPT;
+ nc->ctx.context_destroy = destroy_encrypt_context;
nc->ctx.get_output_length = get_encrypt_output_length;
- nc->ctx.set_param = set_encrypt_property;
- nc->ctx.get_param = get_encrypt_property;
+ nc->ctx.set_property = set_encrypt_property;
+ nc->ctx.get_property = get_encrypt_property;
nc->op_type = OP_OPEN;
nc->tag_len = 0;
goto exit;
}
- iv_bits = ret * 8;
- if (iv_bits == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
+ iv_bit_len = ret * 8;
+ if (iv_bit_len == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
- if (iv_bits > 0) { /* cipher requires iv*/
+ if (iv_bit_len > 0) { /* cipher requires iv*/
liv = key_get_simple(iv);
if (liv == NULL || liv->key.type != YACA_KEY_TYPE_IV) { /* iv was not provided */
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
- ret = yaca_key_get_bit_length(iv, &iv_bits_check);
+ ret = yaca_key_get_bit_length(iv, &iv_bit_len_check);
if (ret != YACA_ERROR_NONE) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
/* IV length doesn't match cipher */
- if (iv_bits != iv_bits_check) {
+ if (iv_bit_len != iv_bit_len_check) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
OP_VERIFY = 1
};
-struct yaca_sign_ctx_s {
+struct yaca_sign_context_s {
struct yaca_context_s ctx;
- EVP_MD_CTX *mdctx;
+ EVP_MD_CTX *md_ctx;
enum sign_op_type op_type;
};
-static struct yaca_sign_ctx_s *get_sign_ctx(const yaca_context_h ctx)
+static struct yaca_sign_context_s *get_sign_context(const yaca_context_h ctx)
{
if (ctx == YACA_CONTEXT_NULL)
return NULL;
switch (ctx->type) {
- case YACA_CTX_SIGN:
- return (struct yaca_sign_ctx_s *)ctx;
+ case YACA_CONTEXT_SIGN:
+ return (struct yaca_sign_context_s *)ctx;
default:
return NULL;
}
{
assert(output_len != NULL);
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
if (c == NULL || input_len != 0)
return YACA_ERROR_INVALID_PARAMETER;
- assert(c->mdctx != NULL);
+ assert(c->md_ctx != NULL);
- if (c->mdctx->pctx == NULL)
+ if (c->md_ctx->pctx == NULL)
return YACA_ERROR_INTERNAL;
- EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(c->mdctx->pctx);
+ EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(c->md_ctx->pctx);
if (pkey == NULL) {
ERROR_DUMP(YACA_ERROR_INTERNAL);
return YACA_ERROR_INTERNAL;
static void destroy_sign_context(yaca_context_h ctx)
{
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
if (c == NULL)
return;
- EVP_MD_CTX_destroy(c->mdctx);
- c->mdctx = NULL;
+ EVP_MD_CTX_destroy(c->md_ctx);
+ c->md_ctx = NULL;
}
-int set_sign_param(yaca_context_h ctx,
- yaca_property_e param,
- const void *value,
- size_t value_len)
+int set_sign_property(yaca_context_h ctx,
+ yaca_property_e property,
+ const void *value,
+ size_t value_len)
{
int ret;
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
yaca_padding_e padding;
int pad;
EVP_PKEY *pkey;
if (c == NULL || value == NULL)
return YACA_ERROR_INVALID_PARAMETER;
- assert(c->mdctx != NULL);
+ assert(c->md_ctx != NULL);
- if (c->mdctx->pctx == NULL)
+ if (c->md_ctx->pctx == NULL)
return YACA_ERROR_INTERNAL;
/* this function only supports padding */
- if (param != YACA_PROPERTY_PADDING || value_len != sizeof(yaca_padding_e))
+ if (property != YACA_PROPERTY_PADDING || value_len != sizeof(yaca_padding_e))
return YACA_ERROR_INVALID_PARAMETER;
padding = *(yaca_padding_e *)(value);
return YACA_ERROR_INVALID_PARAMETER;
}
- pkey = EVP_PKEY_CTX_get0_pkey(c->mdctx->pctx);
+ pkey = EVP_PKEY_CTX_get0_pkey(c->md_ctx->pctx);
if (pkey == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
if (pkey->type != EVP_PKEY_RSA)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_PKEY_CTX_set_rsa_padding(c->mdctx->pctx, pad);
+ ret = EVP_PKEY_CTX_set_rsa_padding(c->md_ctx->pctx, pad);
if (ret <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
return YACA_ERROR_NONE;
}
-int get_sign_param(const yaca_context_h ctx,
- yaca_property_e param,
- void **value,
- size_t *value_len)
+int get_sign_property(const yaca_context_h ctx,
+ yaca_property_e property,
+ void **value,
+ size_t *value_len)
{
int ret;
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
EVP_PKEY *pkey;
int pad;
yaca_padding_e padding;
if (c == NULL || value == NULL)
return YACA_ERROR_INVALID_PARAMETER;
- assert(c->mdctx != NULL);
+ assert(c->md_ctx != NULL);
- if (c->mdctx->pctx == NULL)
+ if (c->md_ctx->pctx == NULL)
return YACA_ERROR_INTERNAL;
/* this function only supports padding */
- if (param != YACA_PROPERTY_PADDING)
+ if (property != YACA_PROPERTY_PADDING)
return YACA_ERROR_INVALID_PARAMETER;
- pkey = EVP_PKEY_CTX_get0_pkey(c->mdctx->pctx);
+ pkey = EVP_PKEY_CTX_get0_pkey(c->md_ctx->pctx);
if (pkey == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
if (pkey->type != EVP_PKEY_RSA)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_PKEY_CTX_get_rsa_padding(c->mdctx->pctx, &pad);
+ ret = EVP_PKEY_CTX_get_rsa_padding(c->md_ctx->pctx, &pad);
if (ret <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
yaca_digest_algorithm_e algo,
const yaca_key_h key)
{
- struct yaca_sign_ctx_s *nc = NULL;
+ struct yaca_sign_context_s *nc = NULL;
const EVP_MD *md = NULL;
int ret;
const struct yaca_key_evp_s *evp_key = key_get_evp(key);
return YACA_ERROR_INVALID_PARAMETER;
}
- ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+ ret = yaca_zalloc(sizeof(struct yaca_sign_context_s), (void**)&nc);
if (ret != YACA_ERROR_NONE)
return ret;
nc->op_type = OP_SIGN;
- nc->ctx.type = YACA_CTX_SIGN;
- nc->ctx.ctx_destroy = destroy_sign_context;
+ nc->ctx.type = YACA_CONTEXT_SIGN;
+ nc->ctx.context_destroy = destroy_sign_context;
nc->ctx.get_output_length = get_sign_output_length;
- nc->ctx.set_param = set_sign_param;
- nc->ctx.get_param = get_sign_param;
+ nc->ctx.set_property = set_sign_property;
+ nc->ctx.get_property = get_sign_property;
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
goto exit;
- nc->mdctx = EVP_MD_CTX_create();
- if (nc->mdctx == NULL) {
+ nc->md_ctx = EVP_MD_CTX_create();
+ if (nc->md_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
}
- ret = EVP_DigestSignInit(nc->mdctx, NULL, md, NULL, evp_key->evp);
+ ret = EVP_DigestSignInit(nc->md_ctx, NULL, md, NULL, evp_key->evp);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
yaca_digest_algorithm_e algo,
const yaca_key_h key)
{
- struct yaca_sign_ctx_s *nc = NULL;
+ struct yaca_sign_context_s *nc = NULL;
EVP_PKEY *pkey = NULL;
const EVP_MD *md;
int ret;
(key->type != YACA_KEY_TYPE_SYMMETRIC && key->type != YACA_KEY_TYPE_DES))
return YACA_ERROR_INVALID_PARAMETER;
- ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+ ret = yaca_zalloc(sizeof(struct yaca_sign_context_s), (void**)&nc);
if (ret != YACA_ERROR_NONE)
return ret;
nc->op_type = OP_SIGN;
- nc->ctx.type = YACA_CTX_SIGN;
- nc->ctx.ctx_destroy = destroy_sign_context;
+ nc->ctx.type = YACA_CONTEXT_SIGN;
+ nc->ctx.context_destroy = destroy_sign_context;
nc->ctx.get_output_length = get_sign_output_length;
pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC,
NULL,
(unsigned char *)simple_key->d,
- simple_key->bits / 8);
+ simple_key->bit_len / 8);
if (pkey == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
if (ret != YACA_ERROR_NONE)
goto exit;
- nc->mdctx = EVP_MD_CTX_create();
- if (nc->mdctx == NULL) {
+ nc->md_ctx = EVP_MD_CTX_create();
+ if (nc->md_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
}
- ret = EVP_DigestSignInit(nc->mdctx, NULL, md, NULL, pkey);
+ ret = EVP_DigestSignInit(nc->md_ctx, NULL, md, NULL, pkey);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
yaca_encrypt_algorithm_e algo,
const yaca_key_h key)
{
- struct yaca_sign_ctx_s *nc = NULL;
+ struct yaca_sign_context_s *nc = NULL;
CMAC_CTX* cmac_ctx = NULL;
const EVP_CIPHER* cipher = NULL;
EVP_PKEY *pkey = NULL;
(key->type != YACA_KEY_TYPE_SYMMETRIC && key->type != YACA_KEY_TYPE_DES))
return YACA_ERROR_INVALID_PARAMETER;
- ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+ ret = yaca_zalloc(sizeof(struct yaca_sign_context_s), (void**)&nc);
if (ret != YACA_ERROR_NONE)
return ret;
nc->op_type = OP_SIGN;
- nc->ctx.type = YACA_CTX_SIGN;
- nc->ctx.ctx_destroy = destroy_sign_context;
+ nc->ctx.type = YACA_CONTEXT_SIGN;
+ nc->ctx.context_destroy = destroy_sign_context;
nc->ctx.get_output_length = get_sign_output_length;
- ret = encrypt_get_algorithm(algo, YACA_BCM_CBC, simple_key->bits, &cipher);
+ ret = encrypt_get_algorithm(algo, YACA_BCM_CBC, simple_key->bit_len, &cipher);
if (ret != YACA_ERROR_NONE)
goto exit;
goto exit;
}
- if (CMAC_Init(cmac_ctx, simple_key->d, simple_key->bits / 8, cipher, NULL) != 1) {
+ if (CMAC_Init(cmac_ctx, simple_key->d, simple_key->bit_len / 8, cipher, NULL) != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
cmac_ctx = NULL;
- nc->mdctx = EVP_MD_CTX_create();
- if (nc->mdctx == NULL) {
+ nc->md_ctx = EVP_MD_CTX_create();
+ if (nc->md_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
}
- if (EVP_DigestSignInit(nc->mdctx, NULL, NULL, NULL, pkey) != 1) {
+ if (EVP_DigestSignInit(nc->md_ctx, NULL, NULL, NULL, pkey) != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
const char *data,
size_t data_len)
{
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
int ret;
if (c == NULL || c->op_type != OP_SIGN ||
data == NULL || data_len == 0)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_DigestSignUpdate(c->mdctx, data, data_len);
+ ret = EVP_DigestSignUpdate(c->md_ctx, data, data_len);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
char *signature,
size_t *signature_len)
{
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
int ret;
if (c == NULL || c->op_type != OP_SIGN ||
signature == NULL || signature_len == NULL || *signature_len == 0)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_DigestSignFinal(c->mdctx, (unsigned char *)signature, signature_len);
+ ret = EVP_DigestSignFinal(c->md_ctx, (unsigned char *)signature, signature_len);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
yaca_digest_algorithm_e algo,
const yaca_key_h key)
{
- struct yaca_sign_ctx_s *nc = NULL;
+ struct yaca_sign_context_s *nc = NULL;
const EVP_MD *md = NULL;
int ret;
const struct yaca_key_evp_s *evp_key = key_get_evp(key);
return YACA_ERROR_INVALID_PARAMETER;
}
- ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+ ret = yaca_zalloc(sizeof(struct yaca_sign_context_s), (void**)&nc);
if (ret != YACA_ERROR_NONE)
return ret;
nc->op_type = OP_VERIFY;
- nc->ctx.type = YACA_CTX_SIGN;
- nc->ctx.ctx_destroy = destroy_sign_context;
+ nc->ctx.type = YACA_CONTEXT_SIGN;
+ nc->ctx.context_destroy = destroy_sign_context;
nc->ctx.get_output_length = NULL;
- nc->ctx.set_param = set_sign_param;
- nc->ctx.get_param = get_sign_param;
+ nc->ctx.set_property = set_sign_property;
+ nc->ctx.get_property = get_sign_property;
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
goto exit;
- nc->mdctx = EVP_MD_CTX_create();
- if (nc->mdctx == NULL) {
+ nc->md_ctx = EVP_MD_CTX_create();
+ if (nc->md_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
goto exit;
}
- ret = EVP_DigestVerifyInit(nc->mdctx, NULL, md, NULL, evp_key->evp);
+ ret = EVP_DigestVerifyInit(nc->md_ctx, NULL, md, NULL, evp_key->evp);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
const char *data,
size_t data_len)
{
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
int ret;
if (c == NULL || data == NULL || data_len == 0 || c->op_type != OP_VERIFY)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_DigestVerifyUpdate(c->mdctx, data, data_len);
+ ret = EVP_DigestVerifyUpdate(c->md_ctx, data, data_len);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
const char *signature,
size_t signature_len)
{
- struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
+ struct yaca_sign_context_s *c = get_sign_context(ctx);
int ret;
if (c == NULL || signature == NULL || signature_len == 0 || c->op_type != OP_VERIFY)
return YACA_ERROR_INVALID_PARAMETER;
- ret = EVP_DigestVerifyFinal(c->mdctx,
+ ret = EVP_DigestVerifyFinal(c->md_ctx,
(unsigned char *)signature,
signature_len);
{
yaca_context_h ctx;
int ret;
- char *lcipher = NULL;
- size_t out_len, lcipher_len, written;
+ char *lciphertext = NULL;
+ size_t out_len, lciphertext_len, written;
if (plaintext == NULL || plaintext_len == 0 ||
ciphertext == NULL || ciphertext_len == NULL ||
if (ret != YACA_ERROR_NONE)
goto exit;
- ret = yaca_context_get_output_length(ctx, 0, &lcipher_len);
+ ret = yaca_context_get_output_length(ctx, 0, &lciphertext_len);
if (ret != YACA_ERROR_NONE)
goto exit;
- if (out_len > SIZE_MAX - lcipher_len) {
+ if (out_len > SIZE_MAX - lciphertext_len) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
- lcipher_len += out_len;
+ lciphertext_len += out_len;
- assert(lcipher_len > 0);
+ assert(lciphertext_len > 0);
- ret = yaca_malloc(lcipher_len, (void**)&lcipher);
+ ret = yaca_malloc(lciphertext_len, (void**)&lciphertext);
if (ret != YACA_ERROR_NONE)
goto exit;
- out_len = lcipher_len;
- ret = yaca_encrypt_update(ctx, plaintext, plaintext_len, lcipher, &out_len);
+ out_len = lciphertext_len;
+ ret = yaca_encrypt_update(ctx, plaintext, plaintext_len, lciphertext, &out_len);
if (ret != YACA_ERROR_NONE)
goto exit;
- assert(out_len <= lcipher_len);
+ assert(out_len <= lciphertext_len);
written = out_len;
- out_len = lcipher_len - written;
- ret = yaca_encrypt_finalize(ctx, lcipher + written, &out_len);
+ out_len = lciphertext_len - written;
+ ret = yaca_encrypt_finalize(ctx, lciphertext + written, &out_len);
if (ret != YACA_ERROR_NONE)
goto exit;
written += out_len;
- assert(written <= lcipher_len && written > 0);
+ assert(written <= lciphertext_len && written > 0);
- ret = yaca_realloc(written, (void**)&lcipher);
+ ret = yaca_realloc(written, (void**)&lciphertext);
if (ret != YACA_ERROR_NONE)
goto exit;
- *ciphertext = lcipher;
+ *ciphertext = lciphertext;
*ciphertext_len = written;
- lcipher = NULL;
+ lciphertext = NULL;
ret = YACA_ERROR_NONE;
exit:
- yaca_free(lcipher);
+ yaca_free(lciphertext);
yaca_context_destroy(ctx);
return ret;
{
yaca_context_h ctx;
int ret;
- char *lplain = NULL;
- size_t out_len, lplain_len, written;
+ char *lplaintext = NULL;
+ size_t out_len, lplaintext_len, written;
if (ciphertext == NULL || ciphertext_len == 0 ||
plaintext == NULL || plaintext_len == NULL ||
if (ret != YACA_ERROR_NONE)
goto exit;
- ret = yaca_context_get_output_length(ctx, 0, &lplain_len);
+ ret = yaca_context_get_output_length(ctx, 0, &lplaintext_len);
if (ret != YACA_ERROR_NONE)
goto exit;
- if (out_len > SIZE_MAX - lplain_len) {
+ if (out_len > SIZE_MAX - lplaintext_len) {
ret = YACA_ERROR_INVALID_PARAMETER;
goto exit;
}
- lplain_len += out_len;
- assert(lplain_len > 0);
+ lplaintext_len += out_len;
+ assert(lplaintext_len > 0);
- ret = yaca_malloc(lplain_len, (void**)&lplain);
+ ret = yaca_malloc(lplaintext_len, (void**)&lplaintext);
if (ret != YACA_ERROR_NONE)
goto exit;
- out_len = lplain_len;
- ret = yaca_decrypt_update(ctx, ciphertext, ciphertext_len, lplain, &out_len);
+ out_len = lplaintext_len;
+ ret = yaca_decrypt_update(ctx, ciphertext, ciphertext_len, lplaintext, &out_len);
if (ret != YACA_ERROR_NONE)
goto exit;
- assert(out_len <= lplain_len);
+ assert(out_len <= lplaintext_len);
written = out_len;
- out_len = lplain_len - written;
- ret = yaca_decrypt_finalize(ctx, lplain + written, &out_len);
+ out_len = lplaintext_len - written;
+ ret = yaca_decrypt_finalize(ctx, lplaintext + written, &out_len);
if (ret != YACA_ERROR_NONE)
goto exit;
written += out_len;
- assert(written <= lplain_len && written > 0);
+ assert(written <= lplaintext_len && written > 0);
- ret = yaca_realloc(written, (void**)&lplain);
+ ret = yaca_realloc(written, (void**)&lplaintext);
if (ret != YACA_ERROR_NONE)
goto exit;
- *plaintext = lplain;
+ *plaintext = lplaintext;
*plaintext_len = written;
- lplain = NULL;
+ lplaintext = NULL;
ret = YACA_ERROR_NONE;
exit:
- yaca_free(lplain);
+ yaca_free(lplaintext);
yaca_context_destroy(ctx);
return ret;