From eedb7a5848734a15989b023c5a6ef2a2d98bdf7d Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Wed, 18 May 2016 11:33:33 +0200 Subject: [PATCH] CMAC example Change-Id: I2b17f9375e3cfd14048bb639b84f3627a13bb73f --- examples/sign.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/examples/sign.c b/examples/sign.c index 5612ead..1981b27 100644 --- a/examples/sign.c +++ b/examples/sign.c @@ -150,9 +150,8 @@ finish: void sign_verify_cmac(void) { - // TODO rewrite it -#if 0 - char* signature = NULL; + char *signature1 = NULL; + char *signature2 = NULL; size_t signature_len; yaca_ctx_h ctx = YACA_CTX_NULL; @@ -163,8 +162,7 @@ void sign_verify_cmac(void) return; // SIGN - // TODO: CMAC must extract the key length to select the proper evp (EVP_aes_XXX_cbc()) it should be documented - if (yaca_sign_init(&ctx, YACA_DIGEST_CMAC, key) != 0) + if (yaca_sign_cmac_init(&ctx, YACA_ENC_AES, key) != 0) goto finish; if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE)) @@ -173,35 +171,44 @@ void sign_verify_cmac(void) if (yaca_get_sign_length(ctx, &signature_len) != 0) goto finish; - if ((signature = yaca_malloc(signature_len)) == NULL) + if ((signature1 = yaca_malloc(signature_len)) == NULL) goto finish; - if (yaca_sign_final(ctx, signature, &signature_len)) + if (yaca_sign_final(ctx, signature1, &signature_len)) goto finish; - dump_hex(signature, signature_len, "CMAC Signature of lorem4096:"); + dump_hex(signature1, signature_len, "CMAC Signature of lorem4096:"); // CLEANUP yaca_ctx_free(ctx); ctx = YACA_CTX_NULL; // VERIFY - if (yaca_verify_init(&ctx, YACA_DIGEST_CMAC, key) != 0) + if (yaca_sign_cmac_init(&ctx, YACA_ENC_AES, key) != 0) goto finish; - if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != 0) + if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE)) goto finish; - if (yaca_verify_final(ctx, signature, signature_len) != 0) + if (yaca_get_sign_length(ctx, &signature_len) != 0) + goto finish; + + if ((signature2 = yaca_malloc(signature_len)) == NULL) + goto finish; + + if (yaca_sign_final(ctx, signature2, &signature_len)) + goto finish; + + if (yaca_memcmp(signature1, signature2, signature_len) != 0) printf("CMAC verification failed\n"); else printf("CMAC verification succesful\n"); finish: - yaca_free(signature); + yaca_free(signature1); + yaca_free(signature2); yaca_key_free(key); yaca_ctx_free(ctx); -#endif } int main() -- 2.7.4