1 /*-
2 * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <sys/types.h>
18 #include <crypto/aes.h>
19 #include <crypto/cmac.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 static void
print_hex(char * str,unsigned char * buf,int len)24 print_hex(char *str, unsigned char *buf, int len)
25 {
26 int i;
27
28 for ( i=0; i<len; i++ ) {
29 if ( (i % 16) == 0 && i != 0 ) printf("%s", str);
30 printf("%02x", buf[i]);
31 if ( (i % 4) == 3 ) printf(" ");
32 if ( (i % 16) == 15 ) printf("\n");
33 }
34 if ( (i % 16) != 0 ) printf("\n");
35 }
36
37 static void
print128(unsigned char * bytes)38 print128(unsigned char *bytes)
39 {
40 int j;
41 for (j=0; j<16;j++) {
42 printf("%02x",bytes[j]);
43 if ( (j%4) == 3 ) printf(" ");
44 }
45 }
46
47 int
main(void)48 main(void)
49 {
50 unsigned char T[16];
51 unsigned char M[64] = {
52 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
53 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
54 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
55 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
56 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
57 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
58 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
59 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
60 };
61 unsigned char key[16] = {
62 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
63 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
64 };
65 AES_CMAC_CTX ctx;
66
67 printf("--------------------------------------------------\n");
68 printf("K "); print128(key); printf("\n");
69
70 printf("\nExample 1: len = 0\n");
71 printf("M "); printf("<empty string>\n");
72
73 AES_CMAC_SetKey(&ctx, key);
74 AES_CMAC_Init(&ctx);
75 AES_CMAC_Update(&ctx, M, 0);
76 AES_CMAC_Final(T, &ctx);
77 printf("AES_CMAC "); print128(T); printf("\n");
78
79 printf("\nExample 2: len = 16\n");
80 printf("M "); print_hex(" ",M,16);
81
82 AES_CMAC_SetKey(&ctx, key);
83 AES_CMAC_Init(&ctx);
84 AES_CMAC_Update(&ctx, M, 16);
85 AES_CMAC_Final(T, &ctx);
86 printf("AES_CMAC "); print128(T); printf("\n");
87 printf("\nExample 3: len = 40\n");
88 printf("M "); print_hex(" ",M,40);
89
90 AES_CMAC_SetKey(&ctx, key);
91 AES_CMAC_Init(&ctx);
92 AES_CMAC_Update(&ctx, M, 40);
93 AES_CMAC_Final(T, &ctx);
94 printf("AES_CMAC "); print128(T); printf("\n");
95
96 printf("\nExample 4: len = 64\n");
97 printf("M "); print_hex(" ",M,64);
98 AES_CMAC_SetKey(&ctx, key);
99 AES_CMAC_Init(&ctx);
100 AES_CMAC_Update(&ctx, M, 64);
101 AES_CMAC_Final(T, &ctx);
102 printf("AES_CMAC "); print128(T); printf("\n");
103
104 printf("\nExample 4bis: len = 64\n");
105 printf("M "); print_hex(" ",M,64);
106 AES_CMAC_SetKey(&ctx, key);
107 AES_CMAC_Init(&ctx);
108 AES_CMAC_Update(&ctx, M, 40);
109 AES_CMAC_Update(&ctx, M + 40, 24);
110 AES_CMAC_Final(T, &ctx);
111 printf("AES_CMAC "); print128(T); printf("\n");
112
113 printf("\nExample 4ter: len = 64\n");
114 printf("M "); print_hex(" ",M,64);
115 AES_CMAC_SetKey(&ctx, key);
116 AES_CMAC_Init(&ctx);
117 AES_CMAC_Update(&ctx, M, 16);
118 AES_CMAC_Update(&ctx, M + 16, 16);
119 AES_CMAC_Update(&ctx, M + 32, 10);
120 AES_CMAC_Update(&ctx, M + 42, 0);
121 AES_CMAC_Update(&ctx, M + 42, 14);
122 AES_CMAC_Update(&ctx, M + 56, 8);
123 AES_CMAC_Final(T, &ctx);
124 printf("AES_CMAC "); print128(T); printf("\n");
125
126 printf("--------------------------------------------------\n");
127
128 return 0;
129 }
130
131 void
explicit_bzero(void * b,size_t len)132 explicit_bzero(void *b, size_t len)
133 {
134 bzero(b, len);
135 }
136