Multiple goto labels replaced with single 'free' label.
Change-Id: I4936f2ef178c9b6fbf58a38beda7d21a700232a9
ret = yaca_digest_update(ctx, lorem1024, 1024);
if (ret != YACA_ERROR_NONE)
- goto exit_ctx;
+ goto exit;
size_t digest_len;
ret = yaca_get_digest_length(ctx, &digest_len);
if (ret != YACA_ERROR_NONE)
- goto exit_ctx;
+ goto exit;
{
char digest[digest_len];
ret = yaca_digest_final(ctx, digest, &digest_len);
if (ret != YACA_ERROR_NONE)
- goto exit_ctx;
+ goto exit;
dump_hex(digest, digest_len, "Message digest: ");
}
-exit_ctx:
+exit:
yaca_ctx_free(ctx);
}
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
exit:
-
yaca_free(enc);
yaca_free(dec);
yaca_key_free(iv);
return;
if (yaca_get_iv_bits(algo, bcm, key_bits, &iv_bits) != YACA_ERROR_NONE)
- goto ex_key;
+ goto exit;
if (iv_bits > 0 && yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
- goto ex_key;
+ goto exit;
/* Encryption */
{
if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
- goto ex_iv;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto ex_ctx;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto ex_ctx;
+ goto exit;
/* Calculate max output: size of update + final chunks */
enc_size = output_len + block_len;
if ((enc = yaca_malloc(enc_size)) == NULL)
- goto ex_ctx;
+ goto exit;
out_size = enc_size;
if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
rem = enc_size - out_size;
if (yaca_encrypt_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
enc_size = rem + out_size;
/* Decryption */
{
if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
/* Calculate max output: size of update + final chunks */
dec_size = output_len + block_len;
if ((dec = yaca_malloc(dec_size)) == NULL)
- goto ex_of;
+ goto exit;
out_size = dec_size;
if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
- goto ex_in;
+ goto exit;
rem = dec_size - out_size;
if (yaca_decrypt_final(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
- goto ex_in;
+ goto exit;
dec_size = rem + out_size;
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
}
-ex_in:
+exit:
yaca_free(dec);
-ex_of:
yaca_free(enc);
-ex_ctx:
yaca_ctx_free(ctx);
-ex_iv:
yaca_key_free(iv);
-ex_key:
yaca_key_free(key);
}
/* IV generation */
if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if ((aad = yaca_zalloc(aad_size)) == NULL)
- goto clean;
+ goto exit;
if (yaca_rand_bytes(aad, aad_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if ((tag = yaca_zalloc(tag_size)) == NULL)
- goto clean;
+ goto exit;
/* Encryption */
{
if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Provide any AAD data */
if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Calculate max output: size of update + final chunks */
enc_size = output_len + block_len;
if ((enc = yaca_malloc(enc_size)) == NULL)
- goto clean;
+ goto exit;
out_size = enc_size;
if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
rem = enc_size - out_size;
if (yaca_encrypt_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
enc_size = rem + out_size;
/* Set the tag length and get the tag after final encryption */
if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_TAG_LEN,
(void*)&tag_size, sizeof(tag_size)) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_ctx_get_param(ctx, YACA_PARAM_GCM_TAG, (void**)tag, &tag_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
/* Decryption */
{
if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Provide any AAD data */
if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Calculate max output: size of update + final chunks */
dec_size = output_len + block_len;
if ((dec = yaca_malloc(dec_size)) == NULL)
- goto clean;
+ goto exit;
out_size = dec_size;
if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
rem = dec_size - out_size;
/* Set expected tag value before final decryption */
if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_TAG, tag, tag_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_decrypt_final(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
dec_size = rem + out_size;
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
}
-clean:
+exit:
yaca_free(enc);
yaca_free(dec);
yaca_free(tag);
/* IV generation */
if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if ((aad = yaca_zalloc(aad_size)) == NULL)
- goto clean;
+ goto exit;
if (yaca_rand_bytes(aad, aad_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if ((tag = yaca_zalloc(tag_size)) == NULL)
- goto clean;
+ goto exit;
/* Encryption */
{
if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Set tag length (optionally) */
if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_TAG_LEN,
(void*)&tag_size, sizeof(tag_size)) != YACA_ERROR_NONE)
- goto clean;
+ 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 clean;
+ goto exit;
if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Calculate max output: size of update + final chunks */
enc_size = output_len + block_len;
if ((enc = yaca_malloc(enc_size)) == NULL)
- goto clean;
+ goto exit;
out_size = enc_size;
if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
rem = enc_size - out_size;
if (yaca_encrypt_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
enc_size = rem + out_size;
/* Get the tag after final encryption */
if (yaca_ctx_get_param(ctx, YACA_PARAM_CCM_TAG, (void**)tag, &tag_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
/* Decryption */
{
if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Set expected tag value */
if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_TAG, tag, tag_size) != YACA_ERROR_NONE)
- goto clean;
+ 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)
- goto clean;
+ goto exit;
if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
/* Calculate max output: size of update + final chunks */
dec_size = output_len + block_len;
if ((dec = yaca_malloc(dec_size)) == NULL)
- goto clean;
+ goto exit;
out_size = dec_size;
/* The tag verify is performed when you call the final yaca_decrypt_update(),
* there is no call to yaca_decrypt_final() */
if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
dec_size = out_size;
printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
}
-clean:
+exit:
yaca_free(enc);
yaca_free(dec);
yaca_free(tag);
// generate private, public key
ret = yaca_key_gen(YACA_KEY_TYPE_DH_PRIV, YACA_KEY_2048BIT, &private_key);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
ret = yaca_key_extract_public(private_key, &public_key);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
// get peer public key from file
// add helper to read key from file to buffer?
fp = fopen("key.pub", "r");
- if (!fp) goto clean;
+ if (!fp) goto exit;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
/* allocate memory for entire content */
buffer = yaca_malloc(size+1);
if (buffer == NULL)
- goto clean;
+ goto exit;
/* copy the file into the buffer */
if (1 != fread(buffer, size, 1, fp))
- goto clean;
+ goto exit;
ret = yaca_key_import(YACA_KEY_TYPE_DH_PUB, NULL,
buffer, size, &peer_key);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
// derive secret
ret = yaca_key_derive_dh(private_key, peer_key, &secret);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
-clean:
+exit:
yaca_key_free(private_key);
yaca_key_free(public_key);
yaca_key_free(peer_key);
// generate private, public key
ret = yaca_key_gen(YACA_KEY_TYPE_EC_PRIV, YACA_KEY_CURVE_P256, &private_key);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
ret = yaca_key_extract_public(private_key, &public_key);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
// get peer public key from file
fp = fopen("key.pub", "r");
if (fp == NULL)
- goto clean;
+ goto exit;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
/* allocate memory for entire content */
buffer = yaca_malloc(size+1);
if (buffer == NULL)
- goto clean;
+ goto exit;
/* copy the file into the buffer */
if (1 != fread(buffer, size, 1, fp))
- goto clean;
+ goto exit;
ret = yaca_key_import(YACA_KEY_TYPE_EC_PUB, NULL, buffer, size, &peer_key);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
// derive secret
ret = yaca_key_derive_dh(private_key, peer_key, &secret);
if (ret != YACA_ERROR_NONE)
- goto clean;
+ goto exit;
-clean:
+exit:
yaca_key_free(private_key);
yaca_key_free(public_key);
yaca_key_free(peer_key);
return ret;
ret = yaca_key_import(YACA_KEY_TYPE_SYMMETRIC, NULL, b64, b64_len, &b64_imported);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\n\t***** BASE64 exported key: *****\n%.*s\n", (int)b64_len, b64);
yaca_free(b64);
ret = yaca_key_export(b64_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_BASE64, NULL, &b64, &b64_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\t***** BASE64 imported key: *****\n%.*s\n", (int)b64_len, b64);
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;
+ goto exit;
ret = yaca_key_import(YACA_KEY_TYPE_SYMMETRIC, NULL, raw, raw_len, &raw_imported);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
dump_hex(raw, raw_len, "\n\t***** RAW exported key: *****");
yaca_free(raw);
ret = yaca_key_export(raw_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_RAW, NULL, &raw, &raw_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
dump_hex(raw, raw_len, "\t***** RAW imported key: *****");
- ret = YACA_ERROR_NONE;
-
-free:
+exit:
yaca_key_free(raw_imported);
yaca_key_free(b64_imported);
yaca_free(raw);
return ret;
ret = yaca_key_import(priv_type, NULL, pem_prv, pem_prv_len, &pem_prv_imported);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\n\t***** %s PEM exported private key: *****\n%.*s", algo, (int)pem_prv_len, pem_prv);
yaca_free(pem_prv);
ret = yaca_key_export(pem_prv_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_prv, &pem_prv_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\t***** %s PEM imported private key: *****\n%.*s", algo, (int)pem_prv_len, pem_prv);
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;
+ goto exit;
ret = yaca_key_import(priv_type, NULL, der_prv, der_prv_len, &der_prv_imported);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
dump_hex(der_prv, der_prv_len, "\n\t***** %s DER exported private key: *****", algo);
yaca_free(der_prv);
ret = yaca_key_export(der_prv_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_prv, &der_prv_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
dump_hex(der_prv, der_prv_len, "\t***** %s DER imported private key: *****", algo);
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;
+ goto exit;
ret = yaca_key_import(pub_type, NULL, pem_pub, pem_pub_len, &pem_pub_imported);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\n\t***** %s PEM exported public key: *****\n%.*s", algo, (int)pem_pub_len, pem_pub);
yaca_free(pem_pub);
ret = yaca_key_export(pem_pub_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_pub, &pem_pub_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\t***** %s PEM imported public key: *****\n%.*s", algo, (int)pem_pub_len, pem_pub);
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;
+ goto exit;
ret = yaca_key_import(pub_type, NULL, der_pub, der_pub_len, &der_pub_imported);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
dump_hex(der_pub, der_pub_len, "\n\t***** %s DER exported public key: *****", algo);
yaca_free(der_pub);
ret = yaca_key_export(der_pub_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_pub, &der_pub_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
dump_hex(der_pub, der_pub_len, "\t***** %s DER imported public key: *****", algo);
- ret = YACA_ERROR_NONE;
-
-free:
+exit:
yaca_key_free(der_pub_imported);
yaca_key_free(pem_pub_imported);
yaca_key_free(der_prv_imported);
ret = yaca_key_import(YACA_KEY_TYPE_RSA_PUB, NULL, pub, pub_len, &rsa_pub_from_cert);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
yaca_free(pub);
pub = NULL;
ret = yaca_key_export(rsa_pub_from_cert, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pub, &pub_len);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\n\t***** RSA X509 imported public key: *****\n%.*s", (int)pub_len, pub);
- ret = YACA_ERROR_NONE;
-
-free:
+exit:
yaca_key_free(rsa_pub_from_cert);
yaca_free(pub);
+
return ret;
}
ret = yaca_key_gen(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT, &rsa_priv);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
ret = yaca_key_extract_public(rsa_priv, &rsa_pub);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
ret = yaca_key_gen(YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_1024BIT, &dsa_priv);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
ret = yaca_key_extract_public(dsa_priv, &dsa_pub);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
printf("\t***************************************\n");
printf("\t************** SYMMETRIC **************\n");
else
printf("\n\t*********** X509 - failure ************\n\n");
-free:
+exit:
yaca_key_free(dsa_pub);
yaca_key_free(dsa_priv);
yaca_key_free(rsa_pub);
yaca_key_free(rsa_priv);
yaca_key_free(sym);
-exit:
+
yaca_exit();
return ret;
return;
if (yaca_key_extract_public(key_priv, &key_pub) != YACA_ERROR_NONE)
- goto ex_prvk;
+ goto exit;
/* Encrypt a.k.a. seal */
{
if (yaca_seal_init(&ctx, key_pub, algo, bcm, key_bits, &aes_key, &iv) != YACA_ERROR_NONE)
- goto ex_pubk;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto ex_ak;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto ex_ak;
+ goto exit;
/* Calculate max output: size of update + final chunks */
enc_size = output_len + block_len;
if ((enc = yaca_malloc(enc_size)) == NULL)
- goto ex_ak;
+ goto exit;
/* Seal and finalize */
out_size = enc_size;
if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
rem = enc_size - out_size;
if (yaca_seal_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
enc_size = rem + out_size;
/* Decrypt a.k.a. open */
{
if (yaca_open_init(&ctx, key_priv, algo, bcm, key_bits, aes_key, iv) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
- goto ex_of;
+ goto exit;
/* Calculate max output: size of update + final chunks */
dec_size = output_len + block_len;
if ((dec = yaca_malloc(dec_size)) == NULL)
- goto ex_of;
+ goto exit;
/* Open and finalize */
out_size = dec_size;
if (yaca_open_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
- goto ex_in;
+ goto exit;
rem = dec_size - out_size;
if (yaca_open_final(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
- goto ex_in;
+ goto exit;
dec_size = rem + out_size;
printf("Decrypted data (16 of %zu bytes): %.16s\n", dec_size, dec);
}
-ex_in:
+exit:
yaca_free(dec);
-ex_of:
yaca_free(enc);
-ex_ak:
yaca_ctx_free(ctx);
yaca_key_free(aes_key);
yaca_key_free(iv);
-ex_pubk:
yaca_key_free(key_pub);
-ex_prvk:
yaca_key_free(key_priv);
}
return;
if (yaca_key_extract_public(prv, &pub) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
// SIGN
if (yaca_sign(YACA_DIGEST_SHA512,
LOREM4096_SIZE,
&signature,
&signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
dump_hex(signature, signature_len, "[Simple API] %s Signature of lorem4096:", algo);
else
printf("[Simple API] %s verification successful\n", algo);
-finish:
+exit:
yaca_free(signature);
yaca_key_free(prv);
yaca_key_free(pub);
LOREM4096_SIZE,
&signature1,
&signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
dump_hex(signature1, signature_len, "[Simple API] HMAC Signature of lorem4096:");
LOREM4096_SIZE,
&signature2,
&signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
printf("[Simple API] HMAC verification failed\n");
else
printf("[Simple API] HMAC verification successful\n");
-finish:
+exit:
yaca_free(signature1);
yaca_free(signature2);
yaca_key_free(key);
LOREM4096_SIZE,
&signature1,
&signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
dump_hex(signature1, signature_len, "[Simple API] CMAC Signature of lorem4096:");
LOREM4096_SIZE,
&signature2,
&signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
printf("[Simple API] CMAC verification failed\n");
else
printf("[Simple API] CMAC verification successful\n");
-finish:
+exit:
yaca_free(signature1);
yaca_free(signature2);
yaca_key_free(key);
return;
if (yaca_key_extract_public(prv, &pub) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
// SIGN
if (yaca_sign_init(&ctx, YACA_DIGEST_SHA512, prv) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if ((signature = yaca_malloc(signature_len)) == NULL)
- goto finish;
+ goto exit;
if (yaca_sign_final(ctx, signature, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
dump_hex(signature, signature_len, "[Advanced API] %s Signature of lorem4096:", algo);
// VERIFY
if (yaca_verify_init(&ctx, YACA_DIGEST_SHA512, pub) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_verify_final(ctx, signature, signature_len) != YACA_ERROR_NONE)
printf("[Advanced API] %s verification failed\n", algo);
else
printf("[Advanced API] %s verification successful\n", algo);
-finish:
+exit:
yaca_free(signature);
yaca_key_free(prv);
yaca_key_free(pub);
// SIGN
if (yaca_sign_hmac_init(&ctx, YACA_DIGEST_SHA512, key) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if ((signature1 = yaca_malloc(signature_len)) == NULL)
- goto finish;
+ goto exit;
if (yaca_sign_final(ctx, signature1, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
dump_hex(signature1, signature_len, "[Advanced API] HMAC Signature of lorem4096:");
// VERIFY
if (yaca_sign_hmac_init(&ctx, YACA_DIGEST_SHA512, key) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if ((signature2 = yaca_malloc(signature_len)) == NULL)
- goto finish;
+ goto exit;
if (yaca_sign_final(ctx, signature2, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
printf("[Advanced API] HMAC verification failed\n");
else
printf("[Advanced API] HMAC verification successful\n");
-finish:
+exit:
yaca_free(signature1);
yaca_free(signature2);
yaca_key_free(key);
// SIGN
if (yaca_sign_cmac_init(&ctx, YACA_ENC_AES, key) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
- goto finish;
+ goto exit;
if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if ((signature1 = yaca_malloc(signature_len)) == NULL)
- goto finish;
+ goto exit;
if (yaca_sign_final(ctx, signature1, &signature_len))
- goto finish;
+ goto exit;
dump_hex(signature1, signature_len, "[Advanced API] CMAC Signature of lorem4096:");
// VERIFY
if (yaca_sign_cmac_init(&ctx, YACA_ENC_AES, key) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
- goto finish;
+ goto exit;
if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
- goto finish;
+ goto exit;
if ((signature2 = yaca_malloc(signature_len)) == NULL)
- goto finish;
+ goto exit;
if (yaca_sign_final(ctx, signature2, &signature_len))
- goto finish;
+ goto exit;
if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
printf("[Advanced API] CMAC verification failed\n");
else
printf("[Advanced API] CMAC verification successful\n");
-finish:
+exit:
yaca_free(signature1);
yaca_free(signature2);
yaca_key_free(key);
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
- goto free;
+ goto exit;
nc->mdctx = EVP_MD_CTX_create();
if (nc->mdctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free;
+ goto exit;
}
ret = EVP_DigestInit(nc->mdctx, md);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto ctx;
+ goto exit;
}
*ctx = (yaca_ctx_h)nc;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
- return YACA_ERROR_NONE;
+exit:
+ yaca_ctx_free((yaca_ctx_h)nc);
-ctx:
- EVP_MD_CTX_destroy(nc->mdctx);
-free:
- yaca_free(nc);
return ret;
}
ret = yaca_key_get_bits(sym_key, &key_bits);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
ret = encrypt_get_algorithm(algo, bcm, key_bits, &cipher);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
ret = EVP_CIPHER_iv_length(cipher);
if (ret < 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_free;
+ goto exit;
}
iv_bits = ret * 8;
if (iv_bits == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
if (iv_bits != 0) { /* cipher requires iv*/
liv = key_get_simple(iv);
if (liv == NULL) { /* iv was not provided */
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
ret = yaca_key_get_bits(iv, &iv_bits_check);
if (ret != YACA_ERROR_NONE) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
/* IV length doesn't match cipher (GCM & CCM supports variable IV length) */
if (iv_bits != iv_bits_check &&
bcm != YACA_BCM_GCM &&
bcm != YACA_BCM_CCM) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
iv_data = (unsigned char*)liv->d;
}
if (nc->cipher_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_free;
+ goto exit;
}
switch (op_type) {
break;
default:
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_ctx;
+ goto exit;
}
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_ctx;
+ goto exit;
}
/* Handling of algorithms with variable key length */
if (ret != 1) {
ret = YACA_ERROR_INVALID_ARGUMENT;
ERROR_DUMP(ret);
- goto err_ctx;
+ goto exit;
}
/* Handling of algorithms with variable IV length */
if (ret != 1) {
ret = YACA_ERROR_INVALID_ARGUMENT;
ERROR_DUMP(ret);
- goto err_ctx;
+ goto exit;
}
}
break;
default:
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_ctx;
+ goto exit;
}
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_ctx;
+ goto exit;
}
*ctx = (yaca_ctx_h)nc;
- return YACA_ERROR_NONE;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
+
+exit:
+ yaca_ctx_free((yaca_ctx_h)nc);
-err_ctx:
- EVP_CIPHER_CTX_free(nc->cipher_ctx);
-err_free:
- yaca_free(nc);
return ret;
}
if (src == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
BIO_push(b64, src);
if (dst == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
if (ret < 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
if (ret == YACA_ERROR_NONE)
if (BIO_write(dst, tmpbuf, ret) != ret) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
}
if (out_len < 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
if ((size_t)out_len != b64_len) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto free_bio;
+ goto exit;
}
*output = dst;
dst = NULL;
ret = YACA_ERROR_NONE;
-free_bio:
+exit:
BIO_free_all(b64);
BIO_free_all(dst);
/* key_bits has to fit in size_t */
if (key_data_len > SIZE_MAX / 8) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto out;
+ goto exit;
}
/* DES key length verification */
key_bits != YACA_KEY_UNSAFE_128BIT &&
key_bits != YACA_KEY_192BIT) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto out;
+ goto exit;
}
}
nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_data_len);
if (nk == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
- goto out;
+ goto exit;
}
memcpy(nk->d, key_data, key_data_len);
nk->key.type = key_type;
*key = (yaca_key_h)nk;
+ nk = NULL;
ret = YACA_ERROR_NONE;
-out:
+exit:
BIO_free_all(decoded);
+
return ret;
}
default:
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto free;
+ goto exit;
}
if (type != key_type) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto free;
+ goto exit;
}
nk = yaca_zalloc(sizeof(struct yaca_key_evp_s));
if (nk == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
- goto free;
+ goto exit;
}
nk->evp = pkey;
pkey = NULL;
ret = YACA_ERROR_NONE;
-free:
+exit:
EVP_PKEY_free(pkey);
+
return ret;
}
if (mem == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
BIO_push(b64, mem);
if (ret <= 0 || (unsigned)ret != key_len) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
ret = BIO_flush(b64);
if (ret <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
bio_data_len = BIO_get_mem_data(mem, &bio_data);
if (bio_data_len <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
*data = yaca_malloc(bio_data_len);
if (*data == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
memcpy(*data, bio_data, bio_data_len);
*data_len = bio_data_len;
ret = YACA_ERROR_NONE;
-free_bio:
+exit:
BIO_free_all(b64);
return ret;
}
if (ret != YACA_ERROR_NONE)
- goto free_bio;
+ goto exit;
ret = BIO_flush(mem);
if (ret <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
bio_data_len = BIO_get_mem_data(mem, &bio_data);
if (bio_data_len <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
*data = yaca_malloc(bio_data_len);
if (*data == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
memcpy(*data, bio_data, bio_data_len);
*data_len = bio_data_len;
ret = YACA_ERROR_NONE;
-free_bio:
+exit:
BIO_free_all(mem);
+
return ret;
}
DES_cblock *des_key = (DES_cblock*)nk->d;
if (key_byte_len >= 8) {
ret = DES_random_key(des_key);
- if (ret != 1)
- goto free_nk;
+ if (ret != 1) {
+ ret = YACA_ERROR_INTERNAL;
+ ERROR_DUMP(ret);
+ goto exit;
+ }
}
if (key_byte_len >= 16) {
ret = DES_random_key(des_key + 1);
- if (ret != 1)
- goto free_nk;
+ if (ret != 1) {
+ ret = YACA_ERROR_INTERNAL;
+ ERROR_DUMP(ret);
+ goto exit;
+ }
}
if (key_byte_len >= 24) {
ret = DES_random_key(des_key + 2);
- if (ret != 1)
- goto free_nk;
+ if (ret != 1) {
+ ret = YACA_ERROR_INTERNAL;
+ ERROR_DUMP(ret);
+ goto exit;
+ }
}
nk->bits = key_bits;
*out = nk;
- return YACA_ERROR_NONE;
+ nk = NULL;
+ ret = YACA_ERROR_NONE;
-free_nk:
+exit:
yaca_free(nk);
- ret = YACA_ERROR_INTERNAL;
- ERROR_DUMP(ret);
+
return ret;
}
if (ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_nk;
+ goto exit;
}
ret = EVP_PKEY_keygen_init(ctx);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bits);
if (ret != 1) {
ret = ERROR_HANDLE();
- goto free_ctx;
+ goto exit;
}
ret = EVP_PKEY_keygen(ctx, &pkey);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
nk->evp = pkey;
-
+ pkey = NULL;
*out = nk;
nk = NULL;
+
ret = YACA_ERROR_NONE;
-free_ctx:
+exit:
EVP_PKEY_CTX_free(ctx);
-free_nk:
yaca_free(nk);
return ret;
int ret;
struct yaca_key_evp_s *nk;
- EVP_PKEY_CTX *pctx;
- EVP_PKEY_CTX *kctx;
+ EVP_PKEY_CTX *pctx = NULL;
+ EVP_PKEY_CTX *kctx = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY *params = NULL;
if (pctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_nk;
+ goto exit;
}
ret = EVP_PKEY_paramgen_init(pctx);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pctx;
+ goto exit;
}
ret = EVP_PKEY_CTX_set_dsa_paramgen_bits(pctx, key_bits);
if (ret != 1) {
ret = ERROR_HANDLE();
- goto free_pctx;
+ goto exit;
}
ret = EVP_PKEY_paramgen(pctx, ¶ms);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pctx;
+ goto exit;
}
kctx = EVP_PKEY_CTX_new(params, NULL);
if (kctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_params;
+ goto exit;
}
ret = EVP_PKEY_keygen_init(kctx);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_kctx;
+ goto exit;
}
ret = EVP_PKEY_keygen(kctx, &pkey);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_kctx;
+ goto exit;
}
nk->evp = pkey;
-
+ pkey = NULL;
*out = nk;
nk = NULL;
+
ret = YACA_ERROR_NONE;
-free_kctx:
+exit:
EVP_PKEY_CTX_free(kctx);
-free_params:
EVP_PKEY_free(params);
-free_pctx:
EVP_PKEY_CTX_free(pctx);
-free_nk:
yaca_free(nk);
return ret;
int ret;
struct yaca_key_evp_s *evp_key = key_get_evp(prv_key);
struct yaca_key_evp_s *nk;
- BIO *mem;
- EVP_PKEY *pkey;
+ BIO *mem = NULL;
+ EVP_PKEY *pkey = NULL;
if (prv_key == YACA_KEY_NULL || evp_key == NULL || pub_key == NULL)
return YACA_ERROR_INVALID_ARGUMENT;
if (mem == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_nk;
+ goto exit;
}
ret = i2d_PUBKEY_bio(mem, evp_key->evp);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
pkey = d2i_PUBKEY_bio(mem, NULL);
if (pkey == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_bio;
+ goto exit;
}
BIO_free(mem);
mem = NULL;
- nk->evp = pkey;
- *pub_key = (yaca_key_h)nk;
-
switch (prv_key->type) {
case YACA_KEY_TYPE_RSA_PRIV:
- (*pub_key)->type = YACA_KEY_TYPE_RSA_PUB;
+ nk->key.type = YACA_KEY_TYPE_RSA_PUB;
break;
case YACA_KEY_TYPE_DSA_PRIV:
- (*pub_key)->type = YACA_KEY_TYPE_DSA_PUB;
+ nk->key.type = YACA_KEY_TYPE_DSA_PUB;
break;
// case YACA_KEY_TYPE_EC_PRIV:
-// (*pub_key)->type = YACA_KEY_TYPE_EC_PUB;
+// nk->key.type = YACA_KEY_TYPE_EC_PUB;
// break;
default:
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto free_pkey;
+ goto exit;
}
- return YACA_ERROR_NONE;
+ nk->evp = pkey;
+ pkey = NULL;
+ *pub_key = (yaca_key_h)nk;
+ nk = NULL;
+ ret = YACA_ERROR_NONE;
-free_pkey:
+exit:
EVP_PKEY_free(pkey);
-free_bio:
BIO_free(mem);
-free_nk:
yaca_free(nk);
return ret;
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err;
+ goto exit;
}
*key = (yaca_key_h)nk;
- return YACA_ERROR_NONE;
-err:
+ nk = NULL;
+ ret = YACA_ERROR_NONE;
+exit:
yaca_free(nk);
+
return ret;
}
yaca_key_h *iv)
{
struct yaca_key_evp_s *lpub;
- struct yaca_key_simple_s *lkey;
- struct yaca_key_simple_s *liv;
+ struct yaca_key_simple_s *lkey = NULL;
+ struct yaca_key_simple_s *liv = NULL;
struct yaca_seal_ctx_s *nc;
const EVP_CIPHER *cipher;
int pub_key_length;
if (nc->cipher_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_free;
+ goto exit;
}
ret = EVP_PKEY_size(lpub->evp);
if (ret <= 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_ctx;
+ goto exit;
}
pub_key_length = ret;
lkey = yaca_zalloc(sizeof(struct yaca_key_simple_s) + pub_key_length);
if (lkey == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
- goto err_ctx;
+ goto exit;
}
ret = encrypt_get_algorithm(algo, bcm, sym_key_bits, &cipher);
if (ret != YACA_ERROR_NONE)
- goto err_key;
+ goto exit;
ret = EVP_CIPHER_iv_length(cipher);
if (ret < 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_key;
+ goto exit;
}
iv_length = ret;
liv = yaca_zalloc(sizeof(struct yaca_key_simple_s) + iv_length);
if (liv == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
- goto err_key;
+ goto exit;
}
unsigned char *key_data = (unsigned char*)lkey->d;
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_iv;
+ goto exit;
}
lkey->bits = key_data_length * 8;
lkey->key.type = YACA_KEY_TYPE_SYMMETRIC;
*sym_key = (yaca_key_h)lkey;
+ lkey = NULL;
liv->bits = iv_length * 8;
liv->key.type = YACA_KEY_TYPE_IV;
*iv = (yaca_key_h)liv;
+ liv = NULL;
*ctx = (yaca_ctx_h)nc;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
- return YACA_ERROR_NONE;
-
-err_iv:
+exit:
yaca_free(liv);
-err_key:
yaca_free(lkey);
-err_ctx:
- EVP_CIPHER_CTX_free(nc->cipher_ctx);
-err_free:
- yaca_free(nc);
+ yaca_ctx_free((yaca_ctx_h)nc);
+
return ret;
}
ret = encrypt_get_algorithm(algo, bcm, sym_key_bits, &cipher);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
ret = EVP_CIPHER_iv_length(cipher);
if (ret < 0) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_free;
+ goto exit;
}
iv_bits = ret * 8;
if (iv_bits == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
liv = key_get_simple(iv);
/* cipher requires iv, but none was provided, or provided wrong iv */
if (iv_bits != 0 && (liv == NULL || liv->key.type != YACA_KEY_TYPE_IV)) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
// TODO: handling of algorithms with variable IV length
ret = yaca_key_get_bits(iv, &iv_bits_check);
if (ret != YACA_ERROR_NONE) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
if (iv_bits != iv_bits_check) { /* IV length doesn't match cipher */
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err_free;
+ goto exit;
}
nc->cipher_ctx = EVP_CIPHER_CTX_new();
if (nc->cipher_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_free;
+ goto exit;
}
ret = EVP_OpenInit(nc->cipher_ctx, cipher,
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto err_ctx;
+ goto exit;
}
*ctx = (yaca_ctx_h)nc;
- return YACA_ERROR_NONE;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
+
+exit:
+ yaca_ctx_free((yaca_ctx_h)nc);
-err_ctx:
- EVP_CIPHER_CTX_free(nc->cipher_ctx);
-err_free:
- yaca_free(nc);
return ret;
}
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
- goto free_ctx;
+ goto exit;
nc->mdctx = EVP_MD_CTX_create();
if (nc->mdctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
ret = EVP_DigestSignInit(nc->mdctx, NULL, md, NULL, evp_key->evp);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
*ctx = (yaca_ctx_h)nc;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
- return YACA_ERROR_NONE;
-
-free_ctx:
+exit:
yaca_ctx_free((yaca_ctx_h)nc);
return ret;
if (pkey == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
- goto free_pkey;
+ goto exit;
nc->mdctx = EVP_MD_CTX_create();
if (nc->mdctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pkey;
+ goto exit;
}
ret = EVP_DigestSignInit(nc->mdctx, NULL, md, NULL, pkey);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pkey;
+ goto exit;
}
+ pkey = NULL;
+
*ctx = (yaca_ctx_h)nc;
- return YACA_ERROR_NONE;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
-free_pkey:
+exit:
EVP_PKEY_free(pkey);
-free_ctx:
yaca_ctx_free((yaca_ctx_h)nc);
return ret;
ret = encrypt_get_algorithm(algo, YACA_BCM_CBC, simple_key->bits, &cipher);
if (ret != YACA_ERROR_NONE)
- goto free_ctx;
+ goto exit;
// create and initialize low level CMAC context
cmac_ctx = CMAC_CTX_new();
if (cmac_ctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ 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->bits / 8, cipher, NULL) != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- // TODO refactor error handling: use single cleanup label
- goto free_cmac_ctx;
+ goto exit;
}
// create key and assign CMAC context to it
pkey = EVP_PKEY_new();
- if (!pkey) {
+ if (pkey == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_cmac_ctx;
+ goto exit;
}
if (EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmac_ctx) != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pkey;
+ goto exit;
}
- // TODO refactor error handling: set cmac_ctx to NULL
+
+ cmac_ctx = NULL;
nc->mdctx = EVP_MD_CTX_create();
if (nc->mdctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pkey;
+ goto exit;
}
if (EVP_DigestSignInit(nc->mdctx, NULL, NULL, NULL, pkey) != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_pkey;
+ goto exit;
}
- // TODO refactor error handling: set mdctx to NULL, set pkey to NULL
+
+ pkey = NULL;
*ctx = (yaca_ctx_h)nc;
- return YACA_ERROR_NONE;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
-free_pkey:
+exit:
EVP_PKEY_free(pkey);
-free_cmac_ctx:
CMAC_CTX_free(cmac_ctx);
-free_ctx:
yaca_ctx_free((yaca_ctx_h)nc);
return ret;
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
- goto free_ctx;
+ goto exit;
nc->mdctx = EVP_MD_CTX_create();
if (nc->mdctx == NULL) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
ret = EVP_DigestVerifyInit(nc->mdctx, NULL, md, NULL, evp_key->evp);
if (ret != 1) {
ret = YACA_ERROR_INTERNAL;
ERROR_DUMP(ret);
- goto free_ctx;
+ goto exit;
}
*ctx = (yaca_ctx_h)nc;
+ nc = NULL;
+ ret = YACA_ERROR_NONE;
- return YACA_ERROR_NONE;
-
-free_ctx:
+exit:
yaca_ctx_free((yaca_ctx_h)nc);
return ret;
{
yaca_ctx_h ctx;
int ret;
- char *ldigest;
+ char *ldigest = NULL;
size_t ldigest_len;
if (data == NULL || data_len == 0 || digest == NULL || digest_len == NULL)
ret = yaca_digest_update(ctx, data, data_len);
if (ret != YACA_ERROR_NONE)
- goto err;
+ goto exit;
ret = yaca_get_digest_length(ctx, &ldigest_len);
if (ret != YACA_ERROR_NONE)
- goto err;
+ goto exit;
ldigest = yaca_malloc(ldigest_len);
- if (!ldigest)
- goto err;
+ if (ldigest == NULL)
+ goto exit;
ret = yaca_digest_final(ctx, ldigest, &ldigest_len);
if (ret != YACA_ERROR_NONE)
- goto err_free;
-
- yaca_ctx_free(ctx);
+ goto exit;
*digest_len = ldigest_len;
*digest = ldigest;
- return YACA_ERROR_NONE;
+ ldigest = NULL;
-err_free:
+exit:
yaca_free(ldigest);
-err:
yaca_ctx_free(ctx);
+
return ret;
}
{
yaca_ctx_h ctx;
int ret;
- char *lcipher;
- char *rcipher;
+ char *lcipher = NULL;
+ char *rcipher = NULL;
size_t out_len, lcipher_len, written;
if (plain == NULL || plain_len == 0 || cipher == NULL || cipher_len == NULL ||
ret = yaca_get_block_length(ctx, &lcipher_len);
if (ret != YACA_ERROR_NONE)
- goto err;
+ goto exit;
ret = yaca_get_output_length(ctx, plain_len, &out_len);
if (ret != YACA_ERROR_NONE)
- goto err;
+ goto exit;
if (out_len > SIZE_MAX - lcipher_len) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err;
+ goto exit;
}
lcipher_len += out_len;
lcipher = yaca_malloc(lcipher_len);
if (lcipher == NULL)
- goto err;
+ goto exit;
out_len = lcipher_len;
ret = yaca_encrypt_update(ctx, plain, plain_len, lcipher, &out_len);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
assert(out_len <= lcipher_len);
out_len = lcipher_len - written;
ret = yaca_encrypt_final(ctx, lcipher + written, &out_len);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
written += out_len;
assert(written <= lcipher_len);
rcipher = yaca_realloc(lcipher, written);
if (rcipher == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
- goto err_free;
+ goto exit;
}
- yaca_ctx_free(ctx);
-
*cipher = rcipher;
*cipher_len = written;
- return YACA_ERROR_NONE;
+ lcipher = NULL;
+ ret = YACA_ERROR_NONE;
-err_free:
+exit:
yaca_free(lcipher);
-err:
yaca_ctx_free(ctx);
+
return ret;
}
{
yaca_ctx_h ctx;
int ret;
- char *lplain;
- char *rplain;
+ char *lplain = NULL;
+ char *rplain = NULL;
size_t out_len, lplain_len, written;
if (cipher == NULL || cipher_len == 0 || plain == NULL || plain_len == NULL ||
ret = yaca_get_block_length(ctx, &lplain_len);
if (ret != YACA_ERROR_NONE)
- goto err;
+ goto exit;
ret = yaca_get_output_length(ctx, cipher_len, &out_len);
if (ret != YACA_ERROR_NONE)
- goto err;
+ goto exit;
if (out_len > SIZE_MAX - lplain_len) {
ret = YACA_ERROR_INVALID_ARGUMENT;
- goto err;
+ goto exit;
}
lplain_len += out_len;
lplain = yaca_malloc(lplain_len);
- if (!lplain)
- goto err;
+ if (lplain == NULL)
+ goto exit;
out_len = lplain_len;
ret = yaca_decrypt_update(ctx, cipher, cipher_len, lplain, &out_len);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
assert(out_len <= lplain_len);
out_len = lplain_len - written;
ret = yaca_decrypt_final(ctx, lplain + written, &out_len);
if (ret != YACA_ERROR_NONE)
- goto err_free;
+ goto exit;
written += out_len;
assert(written <= lplain_len);
rplain = yaca_realloc(lplain, written);
if (rplain == NULL) {
ret = YACA_ERROR_OUT_OF_MEMORY;
- goto err_free;
+ goto exit;
}
- yaca_ctx_free(ctx);
-
*plain = rplain;
*plain_len = written;
- return YACA_ERROR_NONE;
+ lplain = NULL;
+ ret = YACA_ERROR_NONE;
-err_free:
+exit:
yaca_free(lplain);
-err:
yaca_ctx_free(ctx);
+
return ret;
}
ret = yaca_verify_update(ctx, data, data_len);
if (ret != YACA_ERROR_NONE)
- goto free_ctx;
+ goto exit;
ret = yaca_verify_final(ctx, signature, signature_len);
-free_ctx:
+exit:
yaca_ctx_free(ctx);
return ret;