From e5b530e1b7a61051e02cfdce5b65d257490c7b9c Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Tue, 22 Sep 2020 23:10:33 +0200 Subject: [PATCH 1/4] Adding support to symmetric encryption --- src/Encryption.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++ src/Encryption.h | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/Encryption.cpp create mode 100644 src/Encryption.h diff --git a/src/Encryption.cpp b/src/Encryption.cpp new file mode 100644 index 0000000..85a9bfd --- /dev/null +++ b/src/Encryption.cpp @@ -0,0 +1,62 @@ +/* + * 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 "Encryption.h" + +EncryptionClass::EncryptionClass(int blockSize, int digestSize) : + _blockSize(blockSize), + _digestSize(digestSize) +{ + _data = (uint8_t*)malloc(_digestSize); + _secret = (uint8_t*)malloc(_blockSize); + _ivector = (uint8_t*)malloc(_blockSize); +} + +EncryptionClass::~EncryptionClass() +{ + if (_secret) { + free(_secret); + _secret = NULL; + } + + if (_data) { + free(_data); + _data = NULL; + } + + if (_ivector) { + free(_ivector); + _ivector = NULL; + } +} + +int EncryptionClass::runEnc(uint8_t *_secret, size_t _secretLength, uint8_t *_data, size_t _dataLength, uint8_t *_ivector) +{ + return runEncryption(_secret, _secretLength, _data, _dataLength, _ivector); +} + +int EncryptionClass::runDec(uint8_t *_secret, size_t _secretLength, uint8_t *_data, size_t _dataLength, uint8_t *_ivector) +{ + return runDecryption(_secret, _secretLength, _data, _dataLength, _ivector); +} \ No newline at end of file diff --git a/src/Encryption.h b/src/Encryption.h new file mode 100644 index 0000000..bc489e8 --- /dev/null +++ b/src/Encryption.h @@ -0,0 +1,55 @@ +/* + * 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 ENCRYPTION_H +#define ENCRYPTION_H + +#include + +class EncryptionClass { + +public: + EncryptionClass(int blockSize, int digestSize); + virtual ~EncryptionClass(); + + int runEnc(uint8_t *_secret, size_t _secretLength, uint8_t *_data, size_t _dataLength, uint8_t *_ivector); + int runDec(uint8_t *_secret, size_t _secretLength, uint8_t *_data, size_t _dataLength, uint8_t *_ivector); + +protected: + virtual int runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv) = 0; + virtual int runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv) = 0; + +private: + int _blockSize; + int _digestSize; + + uint8_t* _secret; + int _secretLength; + uint8_t* _ivector; + int _ivectorLength; + uint8_t* _data; + int _dataLength; +}; + +#endif From 908917f59265b9802270a83170ee611ab3444fa2 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Tue, 22 Sep 2020 23:13:18 +0200 Subject: [PATCH 2/4] Adding AES128 --- src/AES128.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/AES128.h | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/ArduinoBearSSL.h | 1 + 3 files changed, 105 insertions(+) create mode 100644 src/AES128.cpp create mode 100644 src/AES128.h diff --git a/src/AES128.cpp b/src/AES128.cpp new file mode 100644 index 0000000..641e286 --- /dev/null +++ b/src/AES128.cpp @@ -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; diff --git a/src/AES128.h b/src/AES128.h new file mode 100644 index 0000000..8597a2d --- /dev/null +++ b/src/AES128.h @@ -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 + +#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 \ No newline at end of file diff --git a/src/ArduinoBearSSL.h b/src/ArduinoBearSSL.h index 5374331..55e56e6 100644 --- a/src/ArduinoBearSSL.h +++ b/src/ArduinoBearSSL.h @@ -29,6 +29,7 @@ #include "SHA1.h" #include "SHA256.h" #include "MD5.h" +#include "AES128.h" class ArduinoBearSSLClass { public: From 587440ef1c13a6c24a4a032aa5b522a127fb70c0 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Tue, 22 Sep 2020 23:14:21 +0200 Subject: [PATCH 3/4] Adding DES --- src/ArduinoBearSSL.h | 1 + src/DES.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/DES.h | 52 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/DES.cpp create mode 100644 src/DES.h diff --git a/src/ArduinoBearSSL.h b/src/ArduinoBearSSL.h index 55e56e6..5296b9f 100644 --- a/src/ArduinoBearSSL.h +++ b/src/ArduinoBearSSL.h @@ -30,6 +30,7 @@ #include "SHA256.h" #include "MD5.h" #include "AES128.h" +#include "DES.h" class ArduinoBearSSLClass { public: diff --git a/src/DES.cpp b/src/DES.cpp new file mode 100644 index 0000000..1fc811f --- /dev/null +++ b/src/DES.cpp @@ -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; diff --git a/src/DES.h b/src/DES.h new file mode 100644 index 0000000..b3c6cfa --- /dev/null +++ b/src/DES.h @@ -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 + +#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 From ff1a4529f67d425aac05ad5fae043a57a1b97937 Mon Sep 17 00:00:00 2001 From: Luigi Gubello Date: Tue, 22 Sep 2020 23:16:54 +0200 Subject: [PATCH 4/4] Adding examples --- examples/AES128/AES128.ino | 64 ++++++++++++++++++++++++++++++++++++++ examples/DES/DES.ino | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 examples/AES128/AES128.ino create mode 100644 examples/DES/DES.ino diff --git a/examples/AES128/AES128.ino b/examples/AES128/AES128.ino new file mode 100644 index 0000000..1cd0c56 --- /dev/null +++ b/examples/AES128/AES128.ino @@ -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 + +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); + } +} diff --git a/examples/DES/DES.ino b/examples/DES/DES.ino new file mode 100644 index 0000000..dbdba38 --- /dev/null +++ b/examples/DES/DES.ino @@ -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 + +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); + } +}