Skip to content

Commit

Permalink
Max encryption strength bugfix (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioblu committed Mar 6, 2016
1 parent 44fe446 commit 27e50ff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
28 changes: 14 additions & 14 deletions Cape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
| | | | | |
| |_____| |_____| |_____
| | | | |
|_____ | | | |_____ version 1.0
|_____ | | | |_____ version 1.0
Cape Copyright (c) 2012-2016, Giovanni Blu Mitolo All rights reserved.
Expand All @@ -23,15 +23,15 @@ modification, are permitted provided that the following conditions are met:
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
implied warranties of merchantability and fitness for a particular purpose
are disclaimed. In no event shall the copyright holder or contributors be liable
are disclaimed. In no event shall the copyright holder or contributors be liable
for any direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of the use
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage. */

#include "Cape.h"
Expand Down Expand Up @@ -67,17 +67,17 @@ void Cape::crypt(char *data, uint8_t length, boolean initialization_vector, bool
data[i] ^= data[length - 1];

for (i = 0; i < _encryption_strength; i++) {
_s_box[i] = i;
j = (j + _s_box[i] + _encryption_key[i % key_length]) % _encryption_strength;
_s_box[i] = 0;
j = (j + _s_box[i] + _encryption_key[i % key_length]) % MAX_LENGTH;
swap(_s_box[i], _s_box[j]);
}

i = j = 0;
for (int k = 0; k < length; k++) {
i = (i + 1) % _encryption_strength;
j = (j + _s_box[i]) % _encryption_strength;
for (uint8_t k = 0; k < length; k++) {
i++;
j = (j + _s_box[i]);
swap(_s_box[i], _s_box[j]);
result[k] = data[k] ^ _s_box[(_s_box[i] + _s_box[j]) % _encryption_strength];
result[k] = data[k] ^ _s_box[(_s_box[i] + _s_box[j])];
}

if(initialization_vector && !side) {
Expand Down
19 changes: 10 additions & 9 deletions Cape.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
| | | | | |
| |_____| |_____| |_____
| | | | |
|_____ | | | |_____ version 1.0
|_____ | | | |_____ version 1.0
Cape Copyright (c) 2012-2016, Giovanni Blu Mitolo All rights reserved.
Expand All @@ -23,15 +23,15 @@ modification, are permitted provided that the following conditions are met:
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
implied warranties of merchantability and fitness for a particular purpose
are disclaimed. In no event shall the copyright holder or contributors be liable
are disclaimed. In no event shall the copyright holder or contributors be liable
for any direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of the use
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage. */

#ifndef Cape_h
Expand All @@ -40,6 +40,7 @@ of this software, even if advised of the possibility of such damage. */
#endif

#define swap(a,b) do { int t = _s_box[a]; _s_box[a] = _s_box[b]; _s_box[b] = t; } while(0)

#define MAX_LENGTH 100

class Cape {
Expand All @@ -55,5 +56,5 @@ class Cape {
char * _encryption_key;
uint8_t _encryption_strength;
boolean _iv;
unsigned char _s_box[255];
unsigned char _s_box[MAX_LENGTH];
};
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ This is an arduino compatible string encryption library. I wrote this to add enc

Cape use a private key, an iteration tunable stream chipher algorithm with the addition of 1 byte initialization vector; all this is reversible, so two entities with the same key can share encrypted data and come back to the original content.

Instantiate Cape passing as first parameter the encryption key, and as second, the encryption strength. If set to high values will encrease the computation time needed:
Instantiate Cape passing as first parameter the encryption key, and as second, the encryption strength, that is a value from 1 to 100. `MAX_LENGTH` constant in `Cape.h` limits string maximum length and the maximum encryption strength value permitted (rise to an higher value if necessary, don't higher over 255 or will not work). Consider that a long encryption key and / or a high encryption strength value leads to longer computation time:
```cpp
Cape cape("YOUR-ENCRYPTION-KEY", 2);
Cape cape("YOUR-ENCRYPTION-KEY", 2);
```
If you want an additional layer of security using `initialization_vector` pass true as third parameter:
```cpp
Cape cape("YOUR-ENCRYPTION-KEY", 2, true);
Cape cape("YOUR-ENCRYPTION-KEY", 2, true);
```
To encrypt a string:
```cpp
cape.encrypt("CRYPTMEPLEASE", 13);
cape.encrypt("CRYPTMEPLEASE", 13);
```
Inside cape.result you find the crypted version of your string
```cpp
Expand Down Expand Up @@ -53,14 +53,14 @@ modification, are permitted provided that the following conditions are met:
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
This software is provided by the copyright holders and contributors "as is"
and any express or implied warranties, including, but not limited to, the
implied warranties of merchantability and fitness for a particular purpose
are disclaimed. In no event shall the copyright holder or contributors be liable
are disclaimed. In no event shall the copyright holder or contributors be liable
for any direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of the use
damages (including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption) however
caused and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage. */
```
30 changes: 23 additions & 7 deletions examples/SerialCryptDecrypt/SerialCryptDecrypt.ino
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
#include <Cape.h>

Cape cape("YOUR-PRIVATE-KEY", 2, true);
Cape cape("YOUR-ENCRYPTION-KEY", 50, true);

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

void loop() {

unsigned long time = micros();
cape.encrypt("Hello world!", 12);
for(int i = 0; i < MAX_LENGTH; i++)
time = micros() - time;

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

Serial.println();

Serial.print("Computation time: ");
Serial.print(time);
Serial.println(" microseconds");

time = micros();
cape.decrypt(cape.result, 12);
for(int i = 0; i < MAX_LENGTH; i++)
time = micros() - time;

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

Serial.println();

delay(100);
Serial.print("Computation time: ");
Serial.print(time);
Serial.println(" microseconds");

delay(150);
}

0 comments on commit 27e50ff

Please sign in to comment.