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.
19 #include <ao_radio_cmac.h>
21 static __xdata uint8_t ao_radio_cmac_mutex;
22 __pdata int8_t ao_radio_cmac_rssi;
23 static __xdata uint8_t cmac_data[AO_CMAC_MAX_LEN + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN];
26 round_len(uint8_t len)
30 /* Make sure we transfer at least one packet, and
31 * then make sure every packet is full. Note that
32 * there is no length encoded, and that the receiver
33 * must deal with any extra bytes in the packet
35 if (len < AO_CMAC_KEY_LEN)
36 len = AO_CMAC_KEY_LEN;
37 rem = len % AO_CMAC_KEY_LEN;
39 len += (AO_CMAC_KEY_LEN - rem);
44 * Sign and deliver the data sitting in the cmac buffer
47 radio_cmac_send(uint8_t len) __reentrant
52 /* Make sure the AES key is loaded */
59 ao_mutex_get(&ao_aes_mutex);
60 ao_aes_set_mode(ao_aes_mode_cbc_mac);
61 ao_aes_set_key(ao_config.aes_key);
63 for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
64 if (i + AO_CMAC_KEY_LEN < len)
65 ao_aes_run(&cmac_data[i], NULL);
67 ao_aes_run(&cmac_data[i], &cmac_data[len]);
69 ao_mutex_put(&ao_aes_mutex);
71 ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN);
75 * Receive and validate an incoming packet
79 radio_cmac_recv(uint8_t len, uint16_t timeout) __reentrant
87 i = ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2, timeout);
90 ao_radio_cmac_rssi = 0;
91 return AO_RADIO_CMAC_TIMEOUT;
94 if (!(cmac_data[len + AO_CMAC_KEY_LEN +1] & AO_RADIO_STATUS_CRC_OK))
95 return AO_RADIO_CMAC_CRC_ERROR;
99 /* Compute the packet signature
101 ao_mutex_get(&ao_aes_mutex);
102 ao_aes_set_mode(ao_aes_mode_cbc_mac);
103 ao_aes_set_key(ao_config.aes_key);
105 for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
106 if (i + AO_CMAC_KEY_LEN < len)
107 ao_aes_run(&cmac_data[i], NULL);
109 ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
111 ao_mutex_put(&ao_aes_mutex);
113 /* Check the packet signature against the signature provided
117 if (memcmp(&cmac_data[len],
118 &cmac_data[len + AO_CMAC_KEY_LEN + 2],
119 AO_CMAC_KEY_LEN) != 0) {
120 return AO_RADIO_CMAC_MAC_ERROR;
123 ao_radio_cmac_rssi = ao_radio_rssi;
125 return AO_RADIO_CMAC_OK;
129 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant
131 if (len > AO_CMAC_MAX_LEN)
132 return AO_RADIO_CMAC_LEN_ERROR;
133 ao_mutex_get(&ao_radio_cmac_mutex);
134 ao_xmemcpy(cmac_data, packet, len);
136 ao_led_on(AO_LED_TX);
138 radio_cmac_send(len);
140 ao_led_off(AO_LED_TX);
142 ao_mutex_put(&ao_radio_cmac_mutex);
143 return AO_RADIO_CMAC_OK;
147 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant
150 if (len > AO_CMAC_MAX_LEN)
151 return AO_RADIO_CMAC_LEN_ERROR;
152 ao_mutex_get(&ao_radio_cmac_mutex);
154 ao_led_on(AO_LED_RX);
156 i = radio_cmac_recv(len, timeout);
158 ao_led_off(AO_LED_RX);
160 if (i == AO_RADIO_CMAC_OK)
161 ao_xmemcpy(packet, cmac_data, len);
162 ao_mutex_put(&ao_radio_cmac_mutex);