Skip to content

Adding symmetric encryption #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/AES128/AES128.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
ArduinoCrypto AES128 Example

This sketch demonstrates how to run AES128 encryption and decryption for an input string.

Circuit:
- Nano 33 IoT board

created 13 July 2020
by Luigi Gubello

This example code is in the public domain.
*/

#include <ArduinoBearSSL.h>

uint8_t key[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02};
uint8_t enc_iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
uint8_t dec_iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
uint8_t input[16] = "ArduinoArduino"; // {0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x00,0x00}

void setup() {
Serial.begin(9600);
while (!Serial);
}

void loop() {

Serial.print("Key: ");
printHex(key, 16);
Serial.println(" ");
Serial.print("IV: ");
printHex(enc_iv, 16);
Serial.println(" ");
Serial.print("AES128 Encryption of '");
printHex(input, 16);
Serial.print("' is 0x");
AES128.runEnc(key, 16, input, 16, enc_iv); // expect 0x65D0F7758B094114AFA6D33A5EA0716A
printHex(input, 16);
Serial.println(" ");
Serial.println(" ");
Serial.print("Key: ");
printHex(key, 16);
Serial.println(" ");
Serial.print("IV: ");
printHex(dec_iv, 16);
Serial.println(" ");
Serial.print("AES128 Decryption of '");
printHex(input, 16);
Serial.print("' is 0x");
AES128.runDec(key, 16, input, 16, dec_iv);
printHex(input, 16);
Serial.println(" ");
while (1);
}

void printHex(uint8_t *text, size_t size) {
for (byte i = 0; i < size; i = i + 1) {
if (text[i] < 16) {
Serial.print("0");
}
Serial.print(text[i], HEX);
}
}
64 changes: 64 additions & 0 deletions examples/DES/DES.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
ArduinoCrypto DES Example

This sketch demonstrates how to run DES encryption and decryption for an input string.

Circuit:
- Nano 33 IoT board

created 13 July 2020
by Luigi Gubello

This example code is in the public domain.
*/

#include <ArduinoBearSSL.h>

uint8_t key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02};
uint8_t enc_iv[8] = {0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
uint8_t dec_iv[8] = {0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
uint8_t input[8] = "Arduino"; // {0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x00}

void setup() {
Serial.begin(9600);
while (!Serial);
}

void loop() {

Serial.print("Key: ");
printHex(key, 8);
Serial.println(" ");
Serial.print("IV: ");
printHex(enc_iv, 8);
Serial.println(" ");
Serial.print("DES Encryption of '");
printHex(input, 8);
Serial.print("' is 0x");
DES.runEnc(key, 8, input, 8, enc_iv); // expect 0x3C21EB6A62D372DB
printHex(input, 8);
Serial.println(" ");
Serial.println(" ");
Serial.print("Key: ");
printHex(key, 8);
Serial.println(" ");
Serial.print("IV: ");
printHex(dec_iv, 8);
Serial.println(" ");
Serial.print("DES Decryption of '");
printHex(input, 8);
Serial.print("' is 0x");
DES.runDec(key, 8, input, 8, dec_iv);
printHex(input, 8);
Serial.println(" ");
while (1);
}

void printHex(uint8_t *text, size_t size) {
for (byte i = 0; i < size; i = i + 1) {
if (text[i] < 16) {
Serial.print("0");
}
Serial.print(text[i], HEX);
}
}
52 changes: 52 additions & 0 deletions src/AES128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 Arduino SA. All rights reserved.
*
* 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.
*/

#include "AES128.h"

AES128Class::AES128Class() :
EncryptionClass(AES128_BLOCK_SIZE, AES128_DIGEST_SIZE)
{
}

AES128Class::~AES128Class()
{
}

int AES128Class::runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
{
br_aes_ct_cbcenc_init(&cbcenc_ctx, key, size);
br_aes_ct_cbcenc_run(&cbcenc_ctx, iv, input, block_size); // block_size must be multiple of 16

return 1;
}

int AES128Class::runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
{
br_aes_ct_cbcdec_init(&cbcdec_ctx, key, size);
br_aes_ct_cbcdec_run(&cbcdec_ctx, iv, input, block_size); // block_size must be multiple of 16

return 1;
}

AES128Class AES128;
52 changes: 52 additions & 0 deletions src/AES128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 Arduino SA. All rights reserved.
*
* 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 AES128_H
#define AES128_H

#include <bearssl/bearssl_block.h>

#include "Encryption.h"

#define AES128_BLOCK_SIZE 16
#define AES128_DIGEST_SIZE 16

class AES128Class: public EncryptionClass {

public:
AES128Class();
virtual ~AES128Class();

protected:
virtual int runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);
virtual int runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);

private:
br_aes_ct_cbcenc_keys cbcenc_ctx;
br_aes_ct_cbcdec_keys cbcdec_ctx;
};

extern AES128Class AES128;

#endif
2 changes: 2 additions & 0 deletions src/ArduinoBearSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "SHA1.h"
#include "SHA256.h"
#include "MD5.h"
#include "AES128.h"
#include "DES.h"

class ArduinoBearSSLClass {
public:
Expand Down
52 changes: 52 additions & 0 deletions src/DES.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 Arduino SA. All rights reserved.
*
* 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.
*/

#include "DES.h"

DESClass::DESClass() :
EncryptionClass(DES_BLOCK_SIZE, DES_DIGEST_SIZE)
{
}

DESClass::~DESClass()
{
}

int DESClass::runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
{
br_des_ct_cbcenc_init(&cbcenc_ctx, key, size);
br_des_ct_cbcenc_run(&cbcenc_ctx, iv, input, block_size); // block_size must be multiple of 8

return 1;
}

int DESClass::runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
{
br_des_ct_cbcdec_init(&cbcdec_ctx, key, size);
br_des_ct_cbcdec_run(&cbcdec_ctx, iv, input, block_size); // block_size must be multiple of 8

return 1;
}

DESClass DES;
52 changes: 52 additions & 0 deletions src/DES.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 Arduino SA. All rights reserved.
*
* 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 DES_H
#define DES_H

#include <bearssl/bearssl_block.h>

#include "Encryption.h"

#define DES_BLOCK_SIZE 8
#define DES_DIGEST_SIZE 8

class DESClass: public EncryptionClass {

public:
DESClass();
virtual ~DESClass();

protected:
virtual int runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);
virtual int runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);

private:
br_des_ct_cbcenc_keys cbcenc_ctx;
br_des_ct_cbcdec_keys cbcdec_ctx;
};

extern DESClass DES;

#endif
Loading