altos: Start work on AES and raw radio interfaces.
[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
26 static uint8_t
27 getnibble(void)
28 {
29         __pdata char    c;
30
31         c = getchar();
32         if ('0' <= c && c <= '9')
33                 return c - '0';
34         if ('a' <= c && c <= 'f')
35                 return c - ('a' - 10);
36         if ('A' <= c && c <= 'F')
37                 return c - ('A' - 10);
38         ao_cmd_status = ao_cmd_lex_error;
39         return 0;
40 }
41
42 static void
43 ao_radio_cmac_key(void) __reentrant
44 {
45         uint8_t i;
46
47         for (i = 0; i < AO_CMAC_KEY_LEN; i++) {
48                 ao_cmd_hex();
49                 if (ao_cmd_status != ao_cmd_success)
50                         return;
51                 cmac_key[i] = ao_cmd_lex_i;
52         }
53 }
54
55 static void
56 ao_radio_cmac_send(void) __reentrant
57 {
58         uint8_t i, b;
59
60         ao_cmd_hex();
61         if (ao_cmd_status != ao_cmd_success)
62                 return;
63         if (ao_cmd_lex_i > AO_CMAC_MAX_LEN || ao_cmd_lex_i % AO_CMAC_KEY_LEN != 0) {
64                 ao_cmd_status = ao_cmd_syntax_error;
65                 return;
66         }
67         for (i = 0; i < ao_cmd_lex_i; i++) {
68                 b = getnibble() << 4;
69                 b |= getnibble();
70                 if (ao_cmd_status != ao_cmd_success)
71                         return;
72                 cmac_data[i] = b;
73         }
74         ao_mutex_get(&ao_aes_mutex);
75         ao_aes_set_mode(ao_aes_mode_cbc_mac);
76         ao_aes_set_key(cmac_key);
77         for (i = 0; i < ao_cmd_lex_i; i += AO_CMAC_KEY_LEN) {
78                 if (i + AO_CMAC_KEY_LEN < ao_cmd_lex_i)
79                         ao_aes_run(&cmac_data[i], NULL);
80                 else
81                         ao_aes_run(&cmac_data[i], &cmac_data[ao_cmd_lex_i]);
82         }
83         ao_mutex_put(&ao_aes_mutex);
84         ao_set_monitor(0);
85         ao_radio_send(cmac_data, ao_cmd_lex_i + AO_CMAC_KEY_LEN);
86 }
87
88 __code struct ao_cmds ao_radio_cmac_cmds[] = {
89         { ao_radio_cmac_key,    "k <byte> ...\0Set AES-CMAC key." },
90         { ao_radio_cmac_send,   "s <length>\0Send AES-CMAC packet. Bytes to send follow on next line" },
91         { ao_radio_cmac_recv,   "S <length> <timeout>\0Receive AES-CMAC packet. Timeout in ms" },
92         { 0, NULL },
93 };
94
95 void
96 ao_radio_cmac_init(void)
97 {
98         ao_cmd_register(&ao_radio_cmac_cmds[0]);
99         
100 }