2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #define AO_CMAC_KEY_LEN 16
21 #define AO_CMAC_MAX_LEN (128 - AO_CMAC_KEY_LEN)
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];
32 if ('0' <= c && c <= '9')
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;
43 ao_radio_cmac_key(void) __reentrant
47 for (i = 0; i < AO_CMAC_KEY_LEN; i++) {
49 if (ao_cmd_status != ao_cmd_success)
51 cmac_key[i] = ao_cmd_lex_i;
56 ao_radio_cmac_send(void) __reentrant
61 if (ao_cmd_status != ao_cmd_success)
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;
67 for (i = 0; i < ao_cmd_lex_i; i++) {
70 if (ao_cmd_status != ao_cmd_success)
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);
81 ao_aes_run(&cmac_data[i], &cmac_data[ao_cmd_lex_i]);
83 ao_mutex_put(&ao_aes_mutex);
85 ao_radio_send(cmac_data, ao_cmd_lex_i + AO_CMAC_KEY_LEN);
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" },
96 ao_radio_cmac_init(void)
98 ao_cmd_register(&ao_radio_cmac_cmds[0]);