1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #ifdef HAVE_STRINGS_H
5 #include <strings.h>
6 #endif
7
8 #ifdef WITH_CONTIKI
9 #include "contiki.h"
10 #include "contiki-lib.h"
11 #include "contiki-net.h"
12 #endif /* WITH_CONTIKI */
13
14 //#include "debug.h"
15 #include "numeric.h"
16 #include "ccm.h"
17
18 #include "ccm-testdata.c"
19
20 #ifndef HAVE_FLS
fls(unsigned int i)21 int fls(unsigned int i) {
22 int n;
23 for (n = 0; i; n++)
24 i >>= 1;
25 return n;
26 }
27 #endif
28
29 void
dump(unsigned char * buf,size_t len)30 dump(unsigned char *buf, size_t len) {
31 size_t i = 0;
32 while (i < len) {
33 printf("%02x ", buf[i++]);
34 if (i % 4 == 0)
35 printf(" ");
36 if (i % 16 == 0)
37 printf("\n\t");
38 }
39 printf("\n");
40 }
41
42 #ifdef WITH_CONTIKI
43 PROCESS(ccm_test_process, "CCM test process");
44 AUTOSTART_PROCESSES(&ccm_test_process);
PROCESS_THREAD(ccm_test_process,ev,d)45 PROCESS_THREAD(ccm_test_process, ev, d)
46 {
47 #else /* WITH_CONTIKI */
48 int main(int argc, char **argv) {
49 #endif /* WITH_CONTIKI */
50 long int len;
51 int n;
52
53 rijndael_ctx ctx;
54
55 #ifdef WITH_CONTIKI
56 PROCESS_BEGIN();
57 #endif /* WITH_CONTIKI */
58
59 for (n = 0; n < sizeof(data)/sizeof(struct test_vector); ++n) {
60
61 if (rijndael_set_key_enc_only(&ctx, data[n].key, 8*sizeof(data[n].key)) < 0) {
62 fprintf(stderr, "cannot set key\n");
63 return -1;
64 }
65
66 len = dtls_ccm_encrypt_message(&ctx, data[n].M, data[n].L, data[n].nonce,
67 data[n].msg + data[n].la,
68 data[n].lm - data[n].la,
69 data[n].msg, data[n].la);
70
71 len += + data[n].la;
72 printf("Packet Vector #%d ", n+1);
73 if (len != data[n].r_lm || memcmp(data[n].msg, data[n].result, len))
74 printf("FAILED, ");
75 else
76 printf("OK, ");
77
78 printf("result is (total length = %lu):\n\t", len);
79 dump(data[n].msg, len);
80
81 len = dtls_ccm_decrypt_message(&ctx, data[n].M, data[n].L, data[n].nonce,
82 data[n].msg + data[n].la, len - data[n].la,
83 data[n].msg, data[n].la);
84
85 if (len < 0)
86 printf("Packet Vector #%d: cannot decrypt message\n", n+1);
87 else
88 printf("\t*** MAC verified (total length = %lu) ***\n", len + data[n].la);
89 }
90
91 #ifdef WITH_CONTIKI
92 PROCESS_END();
93 #else /* WITH_CONTIKI */
94 return 0;
95 #endif /* WITH_CONTIKI */
96 }
97