AES 07: AES 128, EBC, ZEROS with Frame
This example shows how to encrypt a Waspmote Frame to be sent to Meshlium: - AES algorithm - 128-bit key size - ZEROS Padding - ECB Cipher Mode

Required Materials

1 x Waspmote 1 x Battery

Notes

- The battery has to be connected. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------Waspmote AES_07 Encryption 128 ECB PKCS --------
3
*
4
* This example shows how to encrypt a Waspmote Frame to be sent to Meshlium:
5
* - AES algorithm
6
* - 128-bit key size
7
* - ZEROS Padding
8
* - ECB Cipher Mode
9
*
10
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
11
* http://www.libelium.com
12
*
13
* This program is free software: you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation, either version 3 of the License, or
16
* (at your option) any later version.
17
*
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25
*
26
* Version: 3.0
27
* Design: David Gascon
28
* Implementation: Yuri Carmona
29
*/
30
31
#include "WaspAES.h"
32
#include "WaspFrame.h"
33
#include "WaspXBee802.h"
34
35
// Define private key to encrypt message
36
char nodeID[] = "node_001";
37
38
// Define a 16-Byte (AES-128) private key to encrypt message
39
char password[] = "libeliumlibelium";
40
41
// XBee packet structure
42
packetXBee packet;
43
44
// Destination MAC address
45
char MESHLIUM_ADDRESS[]="0013A2004052414C";
46
47
48
void setup()
49
{
50
// init USB port
51
USB.ON();
52
USB.println(F("AES_07 example:"));
53
USB.println();
54
55
// init RTC
56
RTC.ON();
57
// init ACC
58
ACC.ON();
59
60
// set the node ID
61
frame.setID(nodeID);
62
}
63
64
65
void loop()
66
{
67
////////////////////////////////////////////////
68
// 1. Create a new Frame with sensor fields
69
////////////////////////////////////////////////
70
USB.println(F("1. Creating an ASCII frame"));
71
RTC.ON();
72
73
// Set correct frame size depending on the networking protocol
74
// In this case:
75
// -> XBee-802.15.4 module
76
// -> Unicast 64Byte addressing (MAC address)
77
// -> Link Encryption is disabled (HW encryption)
78
// -> AES Encryption is enabled (SW encryption)
79
frame.setFrameSize(XBEE_802_15_4, UNICAST_64B, DISABLED, ENABLED);
80
81
// get maximum frame size
82
USB.print(F("Maximum Frame Size:"));
83
USB.println(frame.getFrameSize(),DEC);
84
85
// Create new frame (ASCII)
86
frame.createFrame(BINARY);
87
88
// set frame fields (Battery sensor - uint8_t)
89
frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
90
// set frame fields (Temperature in Celsius sensor - float)
91
frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
92
// set frame fields
93
frame.addSensor(SENSOR_ACC, ACC.getX(), ACC.getY(), ACC.getZ() );
94
95
// Show the original frame via USB port
96
frame.showFrame();
97
USB.println();
98
99
100
////////////////////////////////////////////////
101
// 2. Create Frame with Encrypted contents
102
////////////////////////////////////////////////
103
USB.println(F("2. Encrypting Frame"));
104
105
/* Calculate encrypted message with ECB cipher mode and ZEROS padding
106
The Encryption options are:
107
- AES_128
108
- AES_192
109
- AES_256
110
*/
111
frame.encryptFrame( AES_128, password );
112
113
// Show the Encrypted frame via USB port
114
frame.showFrame();
115
USB.println();
116
117
118
////////////////////////////////////////////////
119
// 3. Send Frame with encrypted contents to Meshlium
120
////////////////////////////////////////////////
121
122
// init XBee
123
xbee802.ON();
124
125
// clear packet structure
126
memset( &packet, 0x00, sizeof(packet) );
127
// Choose transmission mode: UNICAST or BROADCAST
128
packet.mode = UNICAST;
129
130
// set destination XBee parameters to packet
131
xbee802.setDestinationParams( &packet, MESHLIUM_ADDRESS, frame.buffer, frame.length );
132
133
// send XBee packet
134
xbee802.sendXBee(&packet);
135
136
// check TX flag
137
if( xbee802.error_TX == 0 )
138
{
139
USB.println(F("sending ok"));
140
}
141
else
142
{
143
USB.println(F("sending error"));
144
}
145
146
147
USB.println(F("*****************************"));
148
// wait for five seconds
149
delay(5000);
150
}
Copied!

Output

1
H#
2
AES_07 example:
3
4
1. Creating an ASCII frame
5
Maximum Frame Size:80
6
===============================
7
Current BINARY Frame:
8
Length: 33
9
Frame Type: 0
10
frame (HEX): 3C3D3E001CC00E15176E6F64655F303031230034193E0000CC413F2000CCFF0B04
11
frame (STR): <=> Ànode_001#4>ÌA?Ìÿ 
12
===============================
13
14
2. Encrypting Frame
15
===============================
16
Current ENCRYPTED Frame:
17
Length: 66
18
Frame Type: 97
19
frame (HEX): 3C3D3E613DC00E15176E6F64655F3030312390DB2A02110CDB28F8ECBCCF599F086634A7D0403682E447287C29AD0827838C7A48B1090D5A337D7638215D732EF3A7
20
frame (STR): <=>a=Ànode_001#Û* Û(øì¼ÏYŸf4§Ð@6‚äG(|)­'ƒŒzH± Z3}v8!]s.ó§
21
===============================
22
23
sending ok
24
*****************************
Copied!
Last modified 1yr ago