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];
25 static __pdata uint8_t ao_radio_cmac_len;
33 if ('0' <= c && c <= '9')
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;
53 ao_radio_cmac_key(void) __reentrant
57 for (i = 0; i < AO_CMAC_KEY_LEN; i++) {
58 cmac_key[i] = getbyte();
59 if (ao_cmd_status != ao_cmd_success)
65 ao_radio_cmac_send(void) __reentrant
71 if (ao_cmd_status != ao_cmd_success)
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)
77 ao_cmd_status = ao_cmd_syntax_error;
82 for (i = 0; i < len; i++) {
83 cmac_data[i] = getbyte();
84 if (ao_cmd_status != ao_cmd_success)
87 ao_mutex_get(&ao_aes_mutex);
88 ao_aes_set_mode(ao_aes_mode_cbc_mac);
89 ao_aes_set_key(cmac_key);
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);
95 ao_aes_run(&cmac_data[i], &cmac_data[len]);
97 ao_mutex_put(&ao_aes_mutex);
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);
109 ao_radio_cmac_recv(void) __reentrant
115 if (ao_cmd_status != ao_cmd_success)
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)
121 ao_cmd_status = ao_cmd_syntax_error;
126 if (ao_cmd_status != ao_cmd_success)
128 timeout = ao_cmd_lex_i;
134 if (!ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2)) {
138 ao_mutex_get(&ao_aes_mutex);
139 ao_aes_set_mode(ao_aes_mode_cbc_mac);
140 ao_aes_set_key(cmac_key);
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);
146 ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
149 for (i = 0; i < len + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN; i++)
150 printf("%02x", cmac_data[i]);
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" },
162 ao_radio_cmac_init(void)
164 ao_cmd_register(&ao_radio_cmac_cmds[0]);