Provides APIs for creating/verifying a signature and digesting a message.
Required Header
#include <yaca/yaca_sign.h>
#include <yaca/yaca_digest.h>
Overview
It provides advanced APIs for creating a signature using asymmetric private key, verifying a signature using asymmetric public key, calculating a HMAC/CMAC of given message using symmetric key and calculating message digests of given message without key.
Examples
Message Digest API example
#include <stdio.h>
#include <yaca_crypto.h>
#include <yaca_digest.h>
#include <yaca_error.h>
#include "misc.h"
int main()
{
int ret;
yaca_context_h ctx = YACA_CONTEXT_NULL;
ret = yaca_initialize();
if (ret != YACA_ERROR_NONE)
goto exit;
printf("Plain data (16 of %zu bytes): %.16s\n", INPUT_DATA_SIZE, INPUT_DATA);
ret = yaca_digest_initialize(&ctx, YACA_DIGEST_SHA256);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_digest_update(ctx, INPUT_DATA, INPUT_DATA_SIZE);
if (ret != YACA_ERROR_NONE)
goto exit;
size_t digest_len;
ret = yaca_context_get_output_length(ctx, 0, &digest_len);
if (ret != YACA_ERROR_NONE)
goto exit;
{
char digest[digest_len];
ret = yaca_digest_finalize(ctx, digest, &digest_len);
if (ret != YACA_ERROR_NONE)
goto exit;
dump_hex(digest, digest_len, "Message digest: ");
}
exit:
yaca_context_destroy(ctx);
yaca_cleanup();
return ret;
}
Signature API example
#include <stdio.h>
#include <yaca_crypto.h>
#include <yaca_sign.h>
#include <yaca_key.h>
#include <yaca_error.h>
#include "misc.h"
int main()
{
int ret;
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h priv_key = YACA_KEY_NULL;
yaca_key_h pub_key = YACA_KEY_NULL;
yaca_padding_e padding = YACA_PADDING_PKCS1_PSS;
char *signature = NULL;
size_t signature_len;
ret = yaca_initialize();
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_2048BIT, &priv_key);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_key_extract_public(priv_key, &pub_key);
if (ret != YACA_ERROR_NONE)
goto exit;
{
ret = yaca_sign_initialize(&ctx, YACA_DIGEST_SHA256, priv_key);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, &padding, sizeof(padding));
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_sign_update(ctx, INPUT_DATA, INPUT_DATA_SIZE);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_context_get_output_length(ctx, 0, &signature_len);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_malloc(signature_len, (void**)&signature);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_sign_finalize(ctx, signature, &signature_len);
if (ret != YACA_ERROR_NONE)
goto exit;
dump_hex(signature, signature_len, "Signature of INPUT_DATA:");
yaca_context_destroy(ctx);
ctx = YACA_CONTEXT_NULL;
}
{
ret = yaca_verify_initialize(&ctx, YACA_DIGEST_SHA256, pub_key);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, &padding, sizeof(padding));
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_verify_update(ctx, INPUT_DATA, INPUT_DATA_SIZE);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_verify_finalize(ctx, signature, signature_len);
if (ret != YACA_ERROR_NONE) {
printf("Verification failed\n");
goto exit;
} else {
printf("Verification successful\n");
}
}
exit:
yaca_free(signature);
yaca_key_destroy(priv_key);
yaca_key_destroy(pub_key);
yaca_context_destroy(ctx);
yaca_cleanup();
return ret;
}
HMAC Signature API example
#include <stdio.h>
#include <yaca_crypto.h>
#include <yaca_sign.h>
#include <yaca_key.h>
#include <yaca_error.h>
#include "misc.h"
int main()
{
int ret;
yaca_context_h ctx = YACA_CONTEXT_NULL;
yaca_key_h sym_key = YACA_KEY_NULL;
char *signature1 = NULL;
char *signature2 = NULL;
size_t signature_len;
ret = yaca_initialize();
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_256BIT, &sym_key);
if (ret != YACA_ERROR_NONE)
goto exit;
{
ret = yaca_sign_initialize_hmac(&ctx, YACA_DIGEST_SHA512, sym_key);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_sign_update(ctx, INPUT_DATA, INPUT_DATA_SIZE);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_context_get_output_length(ctx, 0, &signature_len);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_malloc(signature_len, (void**)&signature1);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_sign_finalize(ctx, signature1, &signature_len);
if (ret != YACA_ERROR_NONE)
goto exit;
dump_hex(signature1, signature_len, "HMAC Signature of INPUT_DATA:");
yaca_context_destroy(ctx);
ctx = YACA_CONTEXT_NULL;
}
{
ret = yaca_sign_initialize_hmac(&ctx, YACA_DIGEST_SHA512, sym_key);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_sign_update(ctx, INPUT_DATA, INPUT_DATA_SIZE);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_context_get_output_length(ctx, 0, &signature_len);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_malloc(signature_len, (void**)&signature2);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_sign_finalize(ctx, signature2, &signature_len);
if (ret != YACA_ERROR_NONE)
goto exit;
ret = yaca_memcmp(signature1, signature2, signature_len);
if (ret != YACA_ERROR_NONE) {
printf("Verification failed\n");
goto exit;
} else {
printf("Verification successful\n");
}
}
exit:
yaca_free(signature1);
yaca_free(signature2);
yaca_key_destroy(sym_key);
yaca_context_destroy(ctx);
yaca_cleanup();
return ret;
}
Functions |
int | yaca_digest_initialize (yaca_context_h *ctx, yaca_digest_algorithm_e algo) |
| Initializes a digest context.
|
int | yaca_digest_update (yaca_context_h ctx, const char *message, size_t message_len) |
| Feeds the message into the message digest algorithm.
|
int | yaca_digest_finalize (yaca_context_h ctx, char *digest, size_t *digest_len) |
| Calculates the final digest.
|
int | yaca_sign_initialize (yaca_context_h *ctx, yaca_digest_algorithm_e algo, const yaca_key_h prv_key) |
| Initializes a signature context for asymmetric signatures.
|
int | yaca_sign_initialize_hmac (yaca_context_h *ctx, yaca_digest_algorithm_e algo, const yaca_key_h sym_key) |
| Initializes a signature context for HMAC.
|
int | yaca_sign_initialize_cmac (yaca_context_h *ctx, yaca_encrypt_algorithm_e algo, const yaca_key_h sym_key) |
| Initializes a signature context for CMAC.
|
int | yaca_sign_update (yaca_context_h ctx, const char *message, size_t message_len) |
| Feeds the message into the digital signature or MAC algorithm.
|
int | yaca_sign_finalize (yaca_context_h ctx, char *signature, size_t *signature_len) |
| Calculates the final signature or MAC.
|
int | yaca_verify_initialize (yaca_context_h *ctx, yaca_digest_algorithm_e algo, const yaca_key_h pub_key) |
| Initializes a signature verification context for asymmetric signatures.
|
int | yaca_verify_update (yaca_context_h ctx, const char *message, size_t message_len) |
| Feeds the message into the digital signature verification algorithm.
|
int | yaca_verify_finalize (yaca_context_h ctx, const char *signature, size_t signature_len) |
| Performs the verification.
|
Function Documentation