CMAC example 50/70150/10
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 18 May 2016 09:33:33 +0000 (11:33 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 23 May 2016 08:03:11 +0000 (10:03 +0200)
Change-Id: I2b17f9375e3cfd14048bb639b84f3627a13bb73f

examples/sign.c

index 5612ead..1981b27 100644 (file)
@@ -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()