altos: More work on AES bits
[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 void
44 ao_radio_cmac_key(void) __reentrant
45 {
46         uint8_t i;
47
48         for (i = 0; i < AO_CMAC_KEY_LEN; i++) {
49                 ao_cmd_hex();
50                 if (ao_cmd_status != ao_cmd_success)
51                         return;
52                 cmac_key[i] = ao_cmd_lex_i;
53         }
54 }
55
56 static void
57 ao_radio_cmac_send(void) __reentrant
58 {
59         uint8_t i, b;
60
61         ao_cmd_hex();
62         if (ao_cmd_status != ao_cmd_success)
63                 return;
64         if (ao_cmd_lex_i > AO_CMAC_MAX_LEN || ao_cmd_lex_i % AO_CMAC_KEY_LEN != 0) {
65                 ao_cmd_status = ao_cmd_syntax_error;
66                 return;
67         }
68         for (i = 0; i < ao_cmd_lex_i; i++) {
69                 b = getnibble() << 4;
70                 b |= getnibble();
71                 if (ao_cmd_status != ao_cmd_success)
72                         return;
73                 cmac_data[i] = b;
74         }
75         ao_mutex_get(&ao_aes_mutex);
76         ao_aes_set_mode(ao_aes_mode_cbc_mac);
77         ao_aes_set_key(cmac_key);
78         for (i = 0; i < ao_cmd_lex_i; i += AO_CMAC_KEY_LEN) {
79                 if (i + AO_CMAC_KEY_LEN < ao_cmd_lex_i)
80                         ao_aes_run(&cmac_data[i], NULL);
81                 else
82                         ao_aes_run(&cmac_data[i], &cmac_data[ao_cmd_lex_i]);
83         }
84         ao_mutex_put(&ao_aes_mutex);
85         ao_set_monitor(0);
86         ao_radio_send(cmac_data, ao_cmd_lex_i + AO_CMAC_KEY_LEN);
87 }
88
89 static void
90 ao_radio_cmac_recv(void) __reentrant
91 {
92         ao_cmd_hex();
93         if (ao_cmd_status != ao_cmd_success)
94                 return;
95 }
96
97 __code struct ao_cmds ao_radio_cmac_cmds[] = {
98         { ao_radio_cmac_key,    "k <byte> ...\0Set AES-CMAC key." },
99         { ao_radio_cmac_send,   "s <length>\0Send AES-CMAC packet. Bytes to send follow on next line" },
100         { ao_radio_cmac_recv,   "S <length> <timeout>\0Receive AES-CMAC packet. Timeout in ms" },
101         { 0, NULL },
102 };
103
104 void
105 ao_radio_cmac_init(void)
106 {
107         ao_cmd_register(&ao_radio_cmac_cmds[0]);
108         
109 }