Skip to content

Commit d96ce6b

Browse files
authored
Merge pull request #34 from luigigubello/adding-symmetric-encryption
Adding symmetric encryption
2 parents 874c6b0 + ff1a452 commit d96ce6b

File tree

9 files changed

+455
-0
lines changed

9 files changed

+455
-0
lines changed

Diff for: examples/AES128/AES128.ino

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
ArduinoCrypto AES128 Example
3+
4+
This sketch demonstrates how to run AES128 encryption and decryption for an input string.
5+
6+
Circuit:
7+
- Nano 33 IoT board
8+
9+
created 13 July 2020
10+
by Luigi Gubello
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <ArduinoBearSSL.h>
16+
17+
uint8_t key[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02};
18+
uint8_t enc_iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
19+
uint8_t dec_iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
20+
uint8_t input[16] = "ArduinoArduino"; // {0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x00,0x00}
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
while (!Serial);
25+
}
26+
27+
void loop() {
28+
29+
Serial.print("Key: ");
30+
printHex(key, 16);
31+
Serial.println(" ");
32+
Serial.print("IV: ");
33+
printHex(enc_iv, 16);
34+
Serial.println(" ");
35+
Serial.print("AES128 Encryption of '");
36+
printHex(input, 16);
37+
Serial.print("' is 0x");
38+
AES128.runEnc(key, 16, input, 16, enc_iv); // expect 0x65D0F7758B094114AFA6D33A5EA0716A
39+
printHex(input, 16);
40+
Serial.println(" ");
41+
Serial.println(" ");
42+
Serial.print("Key: ");
43+
printHex(key, 16);
44+
Serial.println(" ");
45+
Serial.print("IV: ");
46+
printHex(dec_iv, 16);
47+
Serial.println(" ");
48+
Serial.print("AES128 Decryption of '");
49+
printHex(input, 16);
50+
Serial.print("' is 0x");
51+
AES128.runDec(key, 16, input, 16, dec_iv);
52+
printHex(input, 16);
53+
Serial.println(" ");
54+
while (1);
55+
}
56+
57+
void printHex(uint8_t *text, size_t size) {
58+
for (byte i = 0; i < size; i = i + 1) {
59+
if (text[i] < 16) {
60+
Serial.print("0");
61+
}
62+
Serial.print(text[i], HEX);
63+
}
64+
}

Diff for: examples/DES/DES.ino

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
ArduinoCrypto DES Example
3+
4+
This sketch demonstrates how to run DES encryption and decryption for an input string.
5+
6+
Circuit:
7+
- Nano 33 IoT board
8+
9+
created 13 July 2020
10+
by Luigi Gubello
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <ArduinoBearSSL.h>
16+
17+
uint8_t key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02};
18+
uint8_t enc_iv[8] = {0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
19+
uint8_t dec_iv[8] = {0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01};
20+
uint8_t input[8] = "Arduino"; // {0x41,0x72,0x64,0x75,0x69,0x6E,0x6F,0x00}
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
while (!Serial);
25+
}
26+
27+
void loop() {
28+
29+
Serial.print("Key: ");
30+
printHex(key, 8);
31+
Serial.println(" ");
32+
Serial.print("IV: ");
33+
printHex(enc_iv, 8);
34+
Serial.println(" ");
35+
Serial.print("DES Encryption of '");
36+
printHex(input, 8);
37+
Serial.print("' is 0x");
38+
DES.runEnc(key, 8, input, 8, enc_iv); // expect 0x3C21EB6A62D372DB
39+
printHex(input, 8);
40+
Serial.println(" ");
41+
Serial.println(" ");
42+
Serial.print("Key: ");
43+
printHex(key, 8);
44+
Serial.println(" ");
45+
Serial.print("IV: ");
46+
printHex(dec_iv, 8);
47+
Serial.println(" ");
48+
Serial.print("DES Decryption of '");
49+
printHex(input, 8);
50+
Serial.print("' is 0x");
51+
DES.runDec(key, 8, input, 8, dec_iv);
52+
printHex(input, 8);
53+
Serial.println(" ");
54+
while (1);
55+
}
56+
57+
void printHex(uint8_t *text, size_t size) {
58+
for (byte i = 0; i < size; i = i + 1) {
59+
if (text[i] < 16) {
60+
Serial.print("0");
61+
}
62+
Serial.print(text[i], HEX);
63+
}
64+
}

Diff for: src/AES128.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2019 Arduino SA. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "AES128.h"
26+
27+
AES128Class::AES128Class() :
28+
EncryptionClass(AES128_BLOCK_SIZE, AES128_DIGEST_SIZE)
29+
{
30+
}
31+
32+
AES128Class::~AES128Class()
33+
{
34+
}
35+
36+
int AES128Class::runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
37+
{
38+
br_aes_ct_cbcenc_init(&cbcenc_ctx, key, size);
39+
br_aes_ct_cbcenc_run(&cbcenc_ctx, iv, input, block_size); // block_size must be multiple of 16
40+
41+
return 1;
42+
}
43+
44+
int AES128Class::runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
45+
{
46+
br_aes_ct_cbcdec_init(&cbcdec_ctx, key, size);
47+
br_aes_ct_cbcdec_run(&cbcdec_ctx, iv, input, block_size); // block_size must be multiple of 16
48+
49+
return 1;
50+
}
51+
52+
AES128Class AES128;

Diff for: src/AES128.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2019 Arduino SA. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#ifndef AES128_H
26+
#define AES128_H
27+
28+
#include <bearssl/bearssl_block.h>
29+
30+
#include "Encryption.h"
31+
32+
#define AES128_BLOCK_SIZE 16
33+
#define AES128_DIGEST_SIZE 16
34+
35+
class AES128Class: public EncryptionClass {
36+
37+
public:
38+
AES128Class();
39+
virtual ~AES128Class();
40+
41+
protected:
42+
virtual int runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);
43+
virtual int runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);
44+
45+
private:
46+
br_aes_ct_cbcenc_keys cbcenc_ctx;
47+
br_aes_ct_cbcdec_keys cbcdec_ctx;
48+
};
49+
50+
extern AES128Class AES128;
51+
52+
#endif

Diff for: src/ArduinoBearSSL.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "SHA1.h"
3030
#include "SHA256.h"
3131
#include "MD5.h"
32+
#include "AES128.h"
33+
#include "DES.h"
3234

3335
class ArduinoBearSSLClass {
3436
public:

Diff for: src/DES.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2019 Arduino SA. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "DES.h"
26+
27+
DESClass::DESClass() :
28+
EncryptionClass(DES_BLOCK_SIZE, DES_DIGEST_SIZE)
29+
{
30+
}
31+
32+
DESClass::~DESClass()
33+
{
34+
}
35+
36+
int DESClass::runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
37+
{
38+
br_des_ct_cbcenc_init(&cbcenc_ctx, key, size);
39+
br_des_ct_cbcenc_run(&cbcenc_ctx, iv, input, block_size); // block_size must be multiple of 8
40+
41+
return 1;
42+
}
43+
44+
int DESClass::runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv)
45+
{
46+
br_des_ct_cbcdec_init(&cbcdec_ctx, key, size);
47+
br_des_ct_cbcdec_run(&cbcdec_ctx, iv, input, block_size); // block_size must be multiple of 8
48+
49+
return 1;
50+
}
51+
52+
DESClass DES;

Diff for: src/DES.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2019 Arduino SA. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#ifndef DES_H
26+
#define DES_H
27+
28+
#include <bearssl/bearssl_block.h>
29+
30+
#include "Encryption.h"
31+
32+
#define DES_BLOCK_SIZE 8
33+
#define DES_DIGEST_SIZE 8
34+
35+
class DESClass: public EncryptionClass {
36+
37+
public:
38+
DESClass();
39+
virtual ~DESClass();
40+
41+
protected:
42+
virtual int runEncryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);
43+
virtual int runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t block_size, uint8_t *iv);
44+
45+
private:
46+
br_des_ct_cbcenc_keys cbcenc_ctx;
47+
br_des_ct_cbcdec_keys cbcdec_ctx;
48+
};
49+
50+
extern DESClass DES;
51+
52+
#endif

0 commit comments

Comments
 (0)