altos: Make sure we don't beep out continuity twice in idle mode
[fw/altos] / src / kernel / 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 #include <ao_radio_cmac.h>
20
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];
24
25 static uint8_t
26 round_len(uint8_t len)
27 {
28         uint8_t rem;
29
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
34          */
35         if (len < AO_CMAC_KEY_LEN)
36                 len = AO_CMAC_KEY_LEN;
37         rem = len % AO_CMAC_KEY_LEN;
38         if (rem != 0)
39                 len += (AO_CMAC_KEY_LEN - rem);
40         return len;
41 }
42
43 /*
44  * Sign and deliver the data sitting in the cmac buffer
45  */
46 static void
47 radio_cmac_send(uint8_t len) __reentrant
48 {
49         uint8_t i;
50
51         len = round_len(len);
52         /* Make sure the AES key is loaded */
53         ao_config_get();
54
55 #if HAS_MONITOR
56         ao_monitor_set(0);
57 #endif
58
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);
62         ao_aes_zero_iv();
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);
66                 else
67                         ao_aes_run(&cmac_data[i], &cmac_data[len]);
68         }
69         ao_mutex_put(&ao_aes_mutex);
70
71         ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN);
72 }
73
74 /*
75  * Receive and validate an incoming packet
76  */
77
78 static int8_t
79 radio_cmac_recv(uint8_t len, uint16_t timeout) __reentrant
80 {
81         uint8_t i;
82
83         len = round_len(len);
84 #if HAS_MONITOR
85         ao_monitor_set(0);
86 #endif
87         i = ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2, timeout);
88
89         if (!i) {
90                 ao_radio_cmac_rssi = 0;
91                 return AO_RADIO_CMAC_TIMEOUT;
92         }
93
94         ao_radio_cmac_rssi = ao_radio_rssi;
95         if (!(cmac_data[len + AO_CMAC_KEY_LEN +1] & AO_RADIO_STATUS_CRC_OK))
96                 return AO_RADIO_CMAC_CRC_ERROR;
97
98         ao_config_get();
99
100         /* Compute the packet signature
101          */
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);
105         ao_aes_zero_iv();
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);
109                 else
110                         ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
111         }
112         ao_mutex_put(&ao_aes_mutex);
113
114         /* Check the packet signature against the signature provided
115          * over the link
116          */
117          
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;
122         }
123
124         return AO_RADIO_CMAC_OK;
125 }
126
127 int8_t
128 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant
129 {
130         if (len > AO_CMAC_MAX_LEN)
131                 return AO_RADIO_CMAC_LEN_ERROR;
132         ao_mutex_get(&ao_radio_cmac_mutex);
133         ao_xmemcpy(cmac_data, packet, len);
134 #if AO_LED_TX
135         ao_led_on(AO_LED_TX);
136 #endif
137         radio_cmac_send(len);
138 #if AO_LED_TX
139         ao_led_off(AO_LED_TX);
140 #endif
141         ao_mutex_put(&ao_radio_cmac_mutex);
142         return AO_RADIO_CMAC_OK;
143 }
144
145 int8_t
146 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant
147 {
148         int8_t  i;
149         if (len > AO_CMAC_MAX_LEN)
150                 return AO_RADIO_CMAC_LEN_ERROR;
151         ao_mutex_get(&ao_radio_cmac_mutex);
152 #if AO_LED_RX
153         ao_led_on(AO_LED_RX);
154 #endif
155         i = radio_cmac_recv(len, timeout);
156 #if AO_LED_RX
157         ao_led_off(AO_LED_RX);
158 #endif
159         if (i == AO_RADIO_CMAC_OK)
160                 ao_xmemcpy(packet, cmac_data, len);
161         ao_mutex_put(&ao_radio_cmac_mutex);
162         return i;
163 }
164