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