* ------Waspmote AES_07 Encryption 128 ECB PKCS --------
* This example shows how to encrypt a Waspmote Frame to be sent to Meshlium:
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* Implementation: Yuri Carmona
// Define private key to encrypt message
char nodeID[] = "node_001";
// Define a 16-Byte (AES-128) private key to encrypt message
char password[] = "libeliumlibelium";
// Destination MAC address
char MESHLIUM_ADDRESS[]="0013A2004052414C";
USB.println(F("AES_07 example:"));
////////////////////////////////////////////////
// 1. Create a new Frame with sensor fields
////////////////////////////////////////////////
USB.println(F("1. Creating an ASCII frame"));
// Set correct frame size depending on the networking protocol
// -> XBee-802.15.4 module
// -> Unicast 64Byte addressing (MAC address)
// -> Link Encryption is disabled (HW encryption)
// -> AES Encryption is enabled (SW encryption)
frame.setFrameSize(XBEE_802_15_4, UNICAST_64B, DISABLED, ENABLED);
// get maximum frame size
USB.print(F("Maximum Frame Size:"));
USB.println(frame.getFrameSize(),DEC);
// Create new frame (ASCII)
frame.createFrame(BINARY);
// set frame fields (Battery sensor - uint8_t)
frame.addSensor(SENSOR_BAT, (uint8_t) PWR.getBatteryLevel());
// set frame fields (Temperature in Celsius sensor - float)
frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
frame.addSensor(SENSOR_ACC, ACC.getX(), ACC.getY(), ACC.getZ() );
// Show the original frame via USB port
////////////////////////////////////////////////
// 2. Create Frame with Encrypted contents
////////////////////////////////////////////////
USB.println(F("2. Encrypting Frame"));
/* Calculate encrypted message with ECB cipher mode and ZEROS padding
The Encryption options are:
frame.encryptFrame( AES_128, password );
// Show the Encrypted frame via USB port
////////////////////////////////////////////////
// 3. Send Frame with encrypted contents to Meshlium
////////////////////////////////////////////////
// clear packet structure
memset( &packet, 0x00, sizeof(packet) );
// Choose transmission mode: UNICAST or BROADCAST
// set destination XBee parameters to packet
xbee802.setDestinationParams( &packet, MESHLIUM_ADDRESS, frame.buffer, frame.length );
xbee802.sendXBee(&packet);
if( xbee802.error_TX == 0 )
USB.println(F("sending ok"));
USB.println(F("sending error"));
USB.println(F("*****************************"));