-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathlorawan_simple.h
160 lines (126 loc) · 4.13 KB
/
lorawan_simple.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _LORAWAN_SIMPLE_H
#define _LORAWAN_SIMPLE_H
#include <string.h>
#include <unistd.h>
// Configuration needed for aes-cbc-cmac.h
#define TINY_AES_C_IMPLEMENTATION
#define TINY_AES_ECB 1
#include "tiny-AES-c.h"
#include "aes-cbc-cmac.h"
static int GenerateLoRaWANPacket( uint8_t * raw_payload_with_b0, const uint8_t * inner_payload_raw, int inner_payload_len, const uint8_t * payload_key, const uint8_t * network_skey, const uint8_t * devaddress, int frame )
{
int i;
uint8_t * raw_payload = raw_payload_with_b0 + 16;
uint8_t * pl = raw_payload;
struct AES_ctx payload_key_ctx;
AES_init_ctx( &payload_key_ctx, payload_key );
// MAC header
// 010 Unconfirmed data uplink.
// 000 RFU
// 00 Major Version
*(pl++) = ( 0b01000000 );
// Frame header
// Device address (4 bytes)
memcpy( pl, devaddress, 4 );
pl += 4;
// Frame Control
// MSB
// 0 = ADR (cannot do rate adaptation)
// 0 = Not confirming ADR.
// 0 = ACK
// 0 = Class B (we are not a class B device because we can't schedule downlinks)
// 0000 = fopts length (no fopts)
// LSB
*(pl++) = 0b00000000;
// Frame Count
*(pl++) = frame;
*(pl++) = frame>>8;
// Frame Port
*(pl++) = 1; // Port (1-223 are application specific)
int padded_pad_length = ( inner_payload_len + AES_BLOCKLEN - 1 ) & (~(AES_BLOCKLEN-1));
int nr_blocks = padded_pad_length / AES_BLOCKLEN;
uint8_t pad[padded_pad_length];
for( i = 0; i < nr_blocks; i++ )
{
// Add CMAC. Construct b0
uint8_t * Ai = &pad[i*AES_BLOCKLEN];
*(Ai++) = 0x01;
*(Ai++) = 0;
*(Ai++) = 0;
*(Ai++) = 0;
*(Ai++) = 0;
*(Ai++) = 0; // Uplink = 0
*(Ai++) = devaddress[0];
*(Ai++) = devaddress[1];
*(Ai++) = devaddress[2];
*(Ai++) = devaddress[3];
*(Ai++) = (frame>>0) & 0xff;
*(Ai++) = (frame>>8) & 0xff;
*(Ai++) = (frame>>16) & 0xff;
*(Ai++) = (frame>>24) & 0xff;
*(Ai++) = 0;
*(Ai++) = i+1;
}
for( i = 0; i < nr_blocks; i++ )
{
AES_ECB_encrypt( &payload_key_ctx, &pad[i*AES_BLOCKLEN] );
}
// uint8_t * start_of_payload = pl;
for( i = 0; i < inner_payload_len; i++ )
{
*(pl++) = pad[i] ^ inner_payload_raw[i];
}
int to_cmac_size = pl - raw_payload_with_b0;
int length_of_frame_without_b0 = pl - raw_payload;
uint8_t * b0 = raw_payload_with_b0;
*(b0++) = 0x49;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = devaddress[0];
*(b0++) = devaddress[1];
*(b0++) = devaddress[2];
*(b0++) = devaddress[3];
*(b0++) = (frame>>0) & 0xff;
*(b0++) = (frame>>8) & 0xff;
*(b0++) = (frame>>16) & 0xff;
*(b0++) = (frame>>24) & 0xff;
*(b0++) = 0x00;
*(b0++) = length_of_frame_without_b0;
uint8_t mac[AES_BLOCKLEN];
//printf( "To CMAC: %d\n", to_cmac_size );
//for ( i = 0; i < to_cmac_size; i++ )
//{
// printf( "%02x ", raw_payload_with_b0[i] );
//}
//printf( "\n" );
AES_CMAC( network_skey, raw_payload_with_b0, to_cmac_size, mac );
*(pl++) = mac[0];
*(pl++) = mac[1];
*(pl++) = mac[2];
*(pl++) = mac[3];
//printf( "MAC %02x%02x%02x%02x %02x%02x%02x%02x\n", mac[0], mac[1], mac[2], mac[3], mac[12], mac[13], mac[14], mac[15] );
return pl - raw_payload_with_b0 - 16;
}
#endif