altos: struct ao_log_mega doesn't have a ground temp value
[fw/altos] / src / core / 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 static __pdata uint8_t ao_radio_cmac_len;
25
26 static uint8_t
27 round_len(uint8_t len)
28 {
29         uint8_t rem;
30
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
35          */
36         if (len < AO_CMAC_KEY_LEN)
37                 len = AO_CMAC_KEY_LEN;
38         rem = len % AO_CMAC_KEY_LEN;
39         if (rem != 0)
40                 len += (AO_CMAC_KEY_LEN - rem);
41         return len;
42 }
43
44 /*
45  * Sign and deliver the data sitting in the cmac buffer
46  */
47 static void
48 radio_cmac_send(uint8_t len) __reentrant
49 {
50         uint8_t i;
51
52         len = round_len(len);
53         /* Make sure the AES key is loaded */
54         ao_config_get();
55
56 #if HAS_MONITOR
57         ao_monitor_set(0);
58 #endif
59
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);
63         ao_aes_zero_iv();
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);
67                 else
68                         ao_aes_run(&cmac_data[i], &cmac_data[len]);
69         }
70         ao_mutex_put(&ao_aes_mutex);
71
72         ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN);
73 }
74
75 /*
76  * Receive and validate an incoming packet
77  */
78
79 static int8_t
80 radio_cmac_recv(uint8_t len, uint16_t timeout) __reentrant
81 {
82         uint8_t i;
83
84         len = round_len(len);
85 #if HAS_MONITOR
86         ao_monitor_set(0);
87 #endif
88         if (timeout)
89                 ao_alarm(timeout);
90
91         i = ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2);
92         ao_clear_alarm();
93
94         if (!i) {
95                 ao_radio_cmac_rssi = 0;
96                 return AO_RADIO_CMAC_TIMEOUT;
97         }
98
99         ao_radio_cmac_rssi = (int8_t) (((int8_t) cmac_data[len + AO_CMAC_KEY_LEN]) >> 1) - 74;
100         if (!(cmac_data[len + AO_CMAC_KEY_LEN +1] & AO_RADIO_STATUS_CRC_OK))
101                 return AO_RADIO_CMAC_CRC_ERROR;
102
103         ao_config_get();
104
105         /* Compute the packet signature
106          */
107         ao_mutex_get(&ao_aes_mutex);
108         ao_aes_set_mode(ao_aes_mode_cbc_mac);
109         ao_aes_set_key(ao_config.aes_key);
110         ao_aes_zero_iv();
111         for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
112                 if (i + AO_CMAC_KEY_LEN < len)
113                         ao_aes_run(&cmac_data[i], NULL);
114                 else
115                         ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
116         }
117         ao_mutex_put(&ao_aes_mutex);
118
119         /* Check the packet signature against the signature provided
120          * over the link
121          */
122          
123         if (memcmp(&cmac_data[len],
124                    &cmac_data[len + AO_CMAC_KEY_LEN + 2],
125                    AO_CMAC_KEY_LEN) != 0) {
126                 return AO_RADIO_CMAC_MAC_ERROR;
127         }
128
129         return AO_RADIO_CMAC_OK;
130 }
131
132 int8_t
133 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant
134 {
135         if (len > AO_CMAC_MAX_LEN)
136                 return AO_RADIO_CMAC_LEN_ERROR;
137         ao_mutex_get(&ao_radio_cmac_mutex);
138         ao_xmemcpy(cmac_data, packet, len);
139 #if AO_LED_TX
140         ao_led_on(AO_LED_TX);
141 #endif
142         radio_cmac_send(len);
143 #if AO_LED_TX
144         ao_led_off(AO_LED_TX);
145 #endif
146         ao_mutex_put(&ao_radio_cmac_mutex);
147         return AO_RADIO_CMAC_OK;
148 }
149
150 int8_t
151 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant
152 {
153         uint8_t i;
154         if (len > AO_CMAC_MAX_LEN)
155                 return AO_RADIO_CMAC_LEN_ERROR;
156         ao_mutex_get(&ao_radio_cmac_mutex);
157 #if AO_LED_RX
158         ao_led_on(AO_LED_RX);
159 #endif
160         i = radio_cmac_recv(len, timeout);
161 #if AO_LED_RX
162         ao_led_off(AO_LED_RX);
163 #endif
164         if (i == AO_RADIO_CMAC_OK)
165                 ao_xmemcpy(packet, cmac_data, len);
166         ao_mutex_put(&ao_radio_cmac_mutex);
167         return i;
168 }
169