Skip to content

Commit

Permalink
1.2 Stable version (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioblu committed Mar 8, 2016
1 parent e7fd05f commit baaac44
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 52 deletions.
44 changes: 27 additions & 17 deletions Cape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
| | | | | |
| |_____| |_____| |_____
| | | | |
|_____ | | | |_____ version 1.1
|_____ | | | |_____ version 1.2
Cape Copyright (c) 2012-2016, Giovanni Blu Mitolo All rights reserved.
Expand Down Expand Up @@ -39,52 +39,62 @@ of this software, even if advised of the possibility of such damage. */
/* Initiate Cape passing a random string (maximum 255 characters), the number
of hashing iterations you want to perform (max 32767) */

Cape::Cape(char *key, uint8_t iterations) {
Cape::Cape(char *key, uint8_t key_length) {
_key = key;
_iterations = iterations;
_key_length = key_length;

// Generate a 1 byte version of the private key
for(uint8_t i = 0; i < strlen(_key); i++)
_reduced_key ^= key[i];
}

/* Private key, iteration tunable stream chipher algorithm with optional initialization vector */
/* Private key, stream chipher algorithm with masked initialization vector */

void Cape::encrypt(char *data, uint8_t length) {
for(uint8_t i = 0; _iterations > 1 && i < _iterations - 1; i++)
this->crypt((i == 0) ? data : result, length);
this->crypt(result, length, true, 0);
// Hash data with masked initialization vector at the end
this->crypt(data, length, true, 0);
// Further hash result and initialization vector (without adding a new one)
this->crypt(result, length + 1);
}

void Cape::decrypt(char *data, uint8_t length) {
this->crypt(data, length, true, 1);
for(uint8_t i = 0; _iterations > 1 && i < _iterations - 1; i++)
this->crypt(result, length);
// Hash data without triyng to decode initialization vector at the end
this->crypt(data, length);
// Now decrypt decoding initialization vector
this->crypt(result, length - 1, true, 1);
}

void Cape::crypt(char *data, uint8_t length, boolean initialization_vector, boolean side) {
uint8_t i = 0;
uint8_t key_length = strlen(_key);

if(initialization_vector && side) {
// 1 - Hash data with private key and reduced key
for(i = 0; i < length; i++)
data[i] ^= (i ^ _key[(i ^ _reduced_key) % _key_length]);
// 2 - Hash last character to get back real initialization vector
data[length] ^= _reduced_key;

// 3 - Hash all content with the real initialization vector
for(i = 0; i < length; i++)
data[i] ^= data[length];
}

// Hash data with key
for (i = 0; i < length; i++)
result[i] = data[i] ^ _key[i % key_length - 1];
result[i] = data[i] ^ _key[i % _key_length - 1];

if(initialization_vector && !side) {
// 1 - Generate pseudo-random initialization vector
result[length] = this->generate_IV();

// 2 - Hash result using initialization vector
for(i = 0; i < length; i++)
result[i] ^= result[length];

// 3 - Hash initialization vector with reduced private key
result[length] ^= _reduced_key;
// 4 - further hash result with private key and reduced key
for(i = 0; i < length; i++)
result[i] ^= (i ^ _key[(i ^ _reduced_key) % _key_length]);
}
}

uint8_t Cape::generate_IV() {
return (micros() % 254) + 1;;
return (random(0, 256) * millis()) % 255 + 1;
}
9 changes: 4 additions & 5 deletions Cape.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
| | | | | |
| |_____| |_____| |_____
| | | | |
|_____ | | | |_____ version 1.1
|_____ | | | |_____ version 1.2
Cape Copyright (c) 2012-2016, Giovanni Blu Mitolo All rights reserved.
Expand Down Expand Up @@ -43,15 +43,14 @@ of this software, even if advised of the possibility of such damage. */

class Cape {
public:
Cape(char *key, uint8_t iterations = 0);
Cape(char *key, uint8_t key_length = 0);
void crypt(char *data, uint8_t length, boolean initialization_vector = false, boolean side = false);
void encrypt(char *data, uint8_t length);
void decrypt(char *data, uint8_t length);
uint8_t generate_IV();

char result[MAX_LENGTH];
private:
char * _key;
char _reduced_key;
uint8_t _iterations;
char _key_length;
char _reduced_key;
};
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ Cape 1.0 Stable
====
This is an arduino compatible string encryption library. I started working on this to add encryption to [PJON](https://github.com/gioblu/PJON) communications bus system. It is really easy to use but limited by the computational power of the ATmega chip. I am now working to bring it compatible with ESP8266 and other Arduino compatible boards.

Cape uses a private key, an iteration tunable stream chipher algorithm and an optional initialization vector.
Cape uses a private key, stream chipher algorithm and a masked initialization vector.

Instantiate Cape passing as first parameter the encryption key, and as second, the number of iterations you want to perform, that is a value from 1 to 32767 (`int` limit). `MAX_LENGTH` constant in `Cape.h` limits maximum string length, rise to a higher value if necessary, not over 255 otherwise it will not work. Consider that a long encryption key and / or a lot of iterations, leads to a longer computation time:
Instantiate Cape passing as first parameter the encryption key, and as second, its length. `MAX_LENGTH` constant in `Cape.h` limits maximum string length, rise to a higher value if necessary, not over 255 otherwise it will not work. Consider that a long encryption key, leads to a longer computation time:
```cpp
Cape cape("YOUR-ENCRYPTION-KEY");
```
If you want an additional layer of security with `initialization_vector` and procedure iteration
pass as second parameter the number of iterations you want to perform:
```cpp
// Apply initialization vector and iterate 10 times
Cape cape("YOUR-ENCRYPTION-KEY", 10);
Cape cape("YOUR-ENCRYPTION-KEY", 19);
```
To encrypt a string:
```cpp
cape.encrypt("CRYPTMEPLEASE", 13);
```
Inside `cape.result` you find the crypted version of your string
Inside `cape.result` you find the crypted version of your string, with an additional byte at the end, use to encrypt data, called initialization vector:
```cpp
for(uint8_t i = 0; i < 13; i++)
for(uint8_t i = 0; i < 14; i++)
Serial.print(cape.result[i]);
```
If you want to come back from the encrypted data to the original string:
```cpp
cape.decrypt(cape.result, 13);
cape.decrypt(cape.result, 14);
```
and print the original string as before to check all is working and to get back "CRYPTMEPLEASE" string:
Print the original string as before to check all is working and to get back "CRYPTMEPLEASE" string:
```cpp
for(uint8_t i = 0; i < 13; i++)
Serial.print(cape.result[i]);
Expand All @@ -40,7 +34,7 @@ and print the original string as before to check all is working and to get back
| | | | | |
| |_____| |_____| |_____
| | | | |
|_____ | | | |_____ version 1.1
|_____ | | | |_____ version 1.2
Cape Copyright (c) 2012-2016, Giovanni Blu Mitolo All rights reserved.
Expand Down
20 changes: 12 additions & 8 deletions examples/SerialCryptDecrypt/SerialCryptDecrypt.ino
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
#include <Cape.h>

Cape cape("YOUR-ENCRYPTION-KEY", 2);
Cape cape("SECRET-KEY", 10);
// Insert your secret key and its length

void setup() {
Serial.begin(115200);
}

void loop() {
unsigned long time = micros();
cape.encrypt("Hello world!", 12);
cape.encrypt("Original data!", 14);
time = micros() - time;

Serial.println("------------------");
Serial.print("CRIPTED: ");
for(int i = 0; i < 12; i++)
for(int i = 0; i < 14; i++)
Serial.print(cape.result[i]);

Serial.print(" Masked IV: ");
Serial.print((uint8_t)cape.result[14], DEC);
Serial.print(" Computation time: ");
Serial.print(time);
Serial.println(" microseconds");

time = micros();
cape.decrypt(cape.result, 12);
cape.decrypt(cape.result, 15);
// Result String is 1 character longer because of the initialization vector
time = micros() - time;

Serial.print("ORIGINAL: ");
for(int i = 0; i < 12; i++)
for(int i = 0; i < 14; i++)
Serial.print(cape.result[i]);

Serial.print(" Real IV: ");
Serial.print((uint8_t)cape.result[14], DEC);
Serial.print(" Computation time: ");
Serial.print(time);
Serial.println(" microseconds");

delay(150);
delay(500);
}
12 changes: 6 additions & 6 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
# Datatypes (KEYWORD1)
#######################################

Cape KEYWORD1
Cape KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

encrypt KEYWORD2
decrypt KEYWORD2
crypt KEYWORD2
generate_IV KEYWORD2
encrypt KEYWORD2
decrypt KEYWORD2
crypt KEYWORD2
generate_IV KEYWORD2

#######################################
# Instances (KEYWORD2)
Expand All @@ -26,4 +26,4 @@ generate_IV KEYWORD2
# Constants (LITERAL1)
#######################################

MAX_LENGTH LITERAL1
MAX_LENGTH LITERAL1
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Cape
version=1.0
version=1.2
author=Giovanni Blu Mitolo <gioscarab@gmail.com>
maintainer=Giovanni Blu Mitolo <gioscarab@gmail.com>
sentence=Cape is a string encpryption library
paragraph=Provides private key, an iteration tunable stream chipher algorithm with the addition of 1 byte initialization vector.
paragraph=Provides private key, stream chipher with masked initialization vector.
category=Data Processing
url=https://github.com/gioblu/Cape
architectures=avr

0 comments on commit baaac44

Please sign in to comment.