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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_radio_cmac.h>
22 static uint8_t ao_radio_cmac_mutex;
23 int8_t ao_radio_cmac_rssi;
24 static uint8_t cmac_data[AO_CMAC_MAX_LEN + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN];
27 round_len(uint8_t len)
31 /* Make sure we transfer at least one packet, and
32 * then make sure every packet is full. Note that
33 * there is no length encoded, and that the receiver
34 * must deal with any extra bytes in the packet
36 if (len < AO_CMAC_KEY_LEN)
37 len = AO_CMAC_KEY_LEN;
38 rem = len % AO_CMAC_KEY_LEN;
40 len += (uint8_t) (AO_CMAC_KEY_LEN - rem);
45 * Sign and deliver the data sitting in the cmac buffer
48 radio_cmac_send(uint8_t len)
53 /* Make sure the AES key is loaded */
60 ao_mutex_get(&ao_aes_mutex);
61 ao_aes_set_mode(ao_aes_mode_cbc_mac);
62 ao_aes_set_key(ao_config.aes_key);
64 for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
65 if (i + AO_CMAC_KEY_LEN < len)
66 ao_aes_run(&cmac_data[i], NULL);
68 ao_aes_run(&cmac_data[i], &cmac_data[len]);
70 ao_mutex_put(&ao_aes_mutex);
72 ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN);
76 * Receive and validate an incoming packet
80 radio_cmac_recv(uint8_t len, AO_TICK_TYPE timeout)
88 i = ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2, timeout);
91 ao_radio_cmac_rssi = 0;
92 return AO_RADIO_CMAC_TIMEOUT;
95 if (!(cmac_data[len + AO_CMAC_KEY_LEN +1] & AO_RADIO_STATUS_CRC_OK))
96 return AO_RADIO_CMAC_CRC_ERROR;
100 /* Compute the packet signature
102 ao_mutex_get(&ao_aes_mutex);
103 ao_aes_set_mode(ao_aes_mode_cbc_mac);
104 ao_aes_set_key(ao_config.aes_key);
106 for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
107 if (i + AO_CMAC_KEY_LEN < len)
108 ao_aes_run(&cmac_data[i], NULL);
110 ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
112 ao_mutex_put(&ao_aes_mutex);
114 /* Check the packet signature against the signature provided
118 if (memcmp(&cmac_data[len],
119 &cmac_data[len + AO_CMAC_KEY_LEN + 2],
120 AO_CMAC_KEY_LEN) != 0) {
121 return AO_RADIO_CMAC_MAC_ERROR;
124 ao_radio_cmac_rssi = ao_radio_rssi;
126 return AO_RADIO_CMAC_OK;
130 ao_radio_cmac_send(void *packet, uint8_t len)
132 if (len > AO_CMAC_MAX_LEN)
133 return AO_RADIO_CMAC_LEN_ERROR;
134 ao_mutex_get(&ao_radio_cmac_mutex);
135 memcpy(cmac_data, packet, len);
137 ao_led_on(AO_LED_TX);
139 radio_cmac_send(len);
141 ao_led_off(AO_LED_TX);
143 ao_mutex_put(&ao_radio_cmac_mutex);
144 return AO_RADIO_CMAC_OK;
148 ao_radio_cmac_recv(void *packet, uint8_t len, AO_TICK_TYPE timeout)
151 if (len > AO_CMAC_MAX_LEN)
152 return AO_RADIO_CMAC_LEN_ERROR;
153 ao_mutex_get(&ao_radio_cmac_mutex);
155 ao_led_on(AO_LED_RX);
157 i = radio_cmac_recv(len, timeout);
159 ao_led_off(AO_LED_RX);
161 if (i == AO_RADIO_CMAC_OK)
162 memcpy(packet, cmac_data, len);
163 ao_mutex_put(&ao_radio_cmac_mutex);