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 <string.h>
18 #include <stdio.h>
19 #include <crypto/aes.h>
20 #include <crypto/key_wrap.h>
21
22 static void
print_hex(const char * str,unsigned char * buf,int len)23 print_hex(const char *str, unsigned char *buf, int len)
24 {
25 int i;
26
27 printf("%s", str);
28 for (i = 0; i < len; i++) {
29 if ((i % 8) == 0)
30 printf(" ");
31 printf("%02X", buf[i]);
32 }
33 printf("\n");
34 }
35
36 static void
do_test(u_int kek_len,u_int data_len)37 do_test(u_int kek_len, u_int data_len)
38 {
39 aes_key_wrap_ctx ctx;
40 u_int8_t kek[32], data[32];
41 u_int8_t output[64];
42 u_int i;
43
44 for (i = 0; i < kek_len; i++)
45 kek[i] = i;
46 printf("Input:\n");
47 print_hex("KEK:\n ", kek, kek_len);
48 for (i = 0; i < 16; i++)
49 data[i] = i * 16 + i;
50 for (; i < data_len; i++)
51 data[i] = i - 16;
52 print_hex("Key Data:\n ", data, data_len);
53 aes_key_wrap_set_key(&ctx, kek, kek_len);
54 aes_key_wrap(&ctx, data, data_len / 8, output);
55 print_hex("Ciphertext:\n ", output, data_len + 8);
56 aes_key_unwrap(&ctx, output, output, data_len / 8);
57 printf("Output:\n");
58 print_hex("Key Data:\n ", output, data_len);
59 printf("====\n");
60 }
61
62 int
main(void)63 main(void)
64 {
65 do_test(16, 16);
66 do_test(24, 16);
67 do_test(32, 16);
68 do_test(24, 24);
69 do_test(32, 24);
70 do_test(32, 32);
71
72 return 0;
73 }
74
75 void
explicit_bzero(void * b,size_t len)76 explicit_bzero(void *b, size_t len)
77 {
78 bzero(b, len);
79 }
80