7648a2f599b46cc08f67022b95a3bafb214ea2bf
[fw/altos] / src / ao_radio_cmac.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 #define AO_CMAC_KEY_LEN         16
21 #define AO_CMAC_MAX_LEN         (128 - AO_CMAC_KEY_LEN)
22
23 static __xdata uint8_t cmac_key[AO_CMAC_KEY_LEN];
24 static __xdata uint8_t cmac_data[AO_CMAC_MAX_LEN + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN];
25 static __pdata uint8_t ao_radio_cmac_len;
26
27 static uint8_t
28 getnibble(void)
29 {
30         __pdata char    c;
31
32         c = getchar();
33         if ('0' <= c && c <= '9')
34                 return c - '0';
35         if ('a' <= c && c <= 'f')
36                 return c - ('a' - 10);
37         if ('A' <= c && c <= 'F')
38                 return c - ('A' - 10);
39         ao_cmd_status = ao_cmd_lex_error;
40         return 0;
41 }
42
43 static uint8_t
44 getbyte(void)
45 {
46         uint8_t b;
47         b = getnibble() << 4;
48         b |= getnibble();
49         return b;
50 }
51         
52 static void
53 ao_radio_cmac_key(void) __reentrant
54 {
55         uint8_t i;
56
57         for (i = 0; i < AO_CMAC_KEY_LEN; i++) {
58                 cmac_key[i] = getbyte();
59                 if (ao_cmd_status != ao_cmd_success)
60                         return;
61         }
62 }
63
64 static void
65 ao_radio_cmac_send(void) __reentrant
66 {
67         uint8_t i;
68         uint8_t len;
69
70         ao_cmd_decimal();
71         if (ao_cmd_status != ao_cmd_success)
72                 return;
73         if (ao_cmd_lex_i < AO_CMAC_KEY_LEN ||
74             ao_cmd_lex_i > AO_CMAC_MAX_LEN ||
75             ao_cmd_lex_i % AO_CMAC_KEY_LEN != 0)
76         {
77                 ao_cmd_status = ao_cmd_syntax_error;
78                 return;
79         }
80         flush();
81         len = ao_cmd_lex_i;
82         for (i = 0; i < len; i++) {
83                 cmac_data[i] = getbyte();
84                 if (ao_cmd_status != ao_cmd_success)
85                         return;
86         }
87         ao_mutex_get(&ao_aes_mutex);
88         ao_aes_set_mode(ao_aes_mode_cbc_mac);
89         ao_aes_set_key(cmac_key);
90         ao_aes_zero_iv();
91         for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
92                 if (i + AO_CMAC_KEY_LEN < len)
93                         ao_aes_run(&cmac_data[i], NULL);
94                 else
95                         ao_aes_run(&cmac_data[i], &cmac_data[len]);
96         }
97         ao_mutex_put(&ao_aes_mutex);
98 #if HAS_MONITOR
99         ao_set_monitor(0);
100 #endif
101         printf("send:");
102         for (i = 0; i < len + AO_CMAC_KEY_LEN; i++)
103                 printf(" %02x", cmac_data[i]);
104         printf("\n"); flush();
105         ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN);
106 }
107
108 static void
109 ao_radio_cmac_recv(void) __reentrant
110 {
111         uint8_t         len, i;
112         uint16_t        timeout;
113
114         ao_cmd_decimal();
115         if (ao_cmd_status != ao_cmd_success)
116                 return;
117         if (ao_cmd_lex_i < AO_CMAC_KEY_LEN ||
118             ao_cmd_lex_i > AO_CMAC_MAX_LEN ||
119             ao_cmd_lex_i % AO_CMAC_KEY_LEN != 0)
120         {
121                 ao_cmd_status = ao_cmd_syntax_error;
122                 return;
123         }
124         len = ao_cmd_lex_i;
125         ao_cmd_decimal();
126         if (ao_cmd_status != ao_cmd_success)
127                 return;
128         timeout = ao_cmd_lex_i;
129 #if HAS_MONITOR
130         ao_set_monitor(0);
131 #endif
132         if (timeout)
133                 ao_alarm(timeout);
134         if (!ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2)) {
135                 printf("timeout\n");
136                 return;
137         }
138         ao_mutex_get(&ao_aes_mutex);
139         ao_aes_set_mode(ao_aes_mode_cbc_mac);
140         ao_aes_set_key(cmac_key);
141         ao_aes_zero_iv();
142         for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
143                 if (i + AO_CMAC_KEY_LEN < len)
144                         ao_aes_run(&cmac_data[i], NULL);
145                 else
146                         ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
147         }
148         printf ("PACKET ");
149         for (i = 0; i < len + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN; i++)
150                 printf("%02x", cmac_data[i]);
151         printf ("\n");
152 }
153
154 static __code struct ao_cmds ao_radio_cmac_cmds[] = {
155         { ao_radio_cmac_key,    "k\0Set AES-CMAC key. 16 key bytes follow on next line" },
156         { ao_radio_cmac_send,   "s <length>\0Send AES-CMAC packet. Bytes to send follow on next line" },
157         { ao_radio_cmac_recv,   "S <length> <timeout>\0Receive AES-CMAC packet. Timeout in ms" },
158         { 0, NULL },
159 };
160
161 void
162 ao_radio_cmac_init(void)
163 {
164         ao_cmd_register(&ao_radio_cmac_cmds[0]);
165 }