-
Notifications
You must be signed in to change notification settings - Fork 14
/
SpritzCryptTest.ino
100 lines (81 loc) · 2.45 KB
/
SpritzCryptTest.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Spritz Cipher MAC Test
*
* This example code test SpritzCipher library encryption/decryption function.
*
* The circuit: No external hardware needed.
*
* by Abderraouf Adjal.
*
* This example code is in the public domain.
*/
/* ArduinoSpritzCipher documentation: <README.md> */
/* ArduinoSpritzCipher is configurable in <SpritzCipher.h> with:
* SPRITZ_TIMING_SAFE_CRUSH, SPRITZ_WIPE_TRACES, SPRITZ_WIPE_TRACES_PARANOID,
* SPRITZ_USE_LIBC.
* For detailed information, read the documentation.
*/
#include <SpritzCipher.h>
/* Data to input */
const byte testMsg[3] = { 'A', 'B', 'C' };
const byte testKey[3] = { 0x00, 0x01, 0x02 };
void testFunc(const byte *msg, byte msgLen, const byte *key, byte keyLen)
{
spritz_ctx s_ctx;
byte buf[8]; /* Output buffer */
unsigned int i;
/* Print MSG */
for (i = 0; i < msgLen; i++) {
Serial.write(msg[i]);
}
Serial.println();
/* Print KEY */
for (i = 0; i < keyLen; i++) {
if (key[i] < 0x10) { /* To print "0F" not "F" */
Serial.write('0');
}
Serial.print(key[i], HEX);
}
Serial.println();
spritz_setup(&s_ctx, key, keyLen);
spritz_crypt(&s_ctx, msg, msgLen, buf);
/* Print Ciphertext */
for (i = 0; i < msgLen; i++) {
if (buf[i] < 0x10) { /* To print "0F" not "F" */
Serial.write('0');
}
Serial.print(buf[i], HEX);
}
Serial.println();
spritz_setup(&s_ctx, key, keyLen);
spritz_crypt(&s_ctx, buf, msgLen, buf);
/* Print MSG after decryption */
for (i = 0; i < msgLen; i++) {
Serial.write(buf[i]);
}
Serial.println();
/* Check the output */
if (spritz_compare(buf, msg, msgLen)) {
/* If the output is wrong "Alert" */
digitalWrite(LED_BUILTIN, HIGH); /* Turn pin LED_BUILTIN On (Most boards have this LED connected to digital pin 13) */
Serial.println("\n** WARNING: Output != Test_Vector **");
}
Serial.println();
}
void setup() {
/* Initialize serial and wait for port to open */
Serial.begin(9600);
while (!Serial) {
; /* Wait for serial port to connect. Needed for Leonardo only */
}
/* initialize digital pin LED_BUILTIN (Most boards have this LED connected to digital pin 13) as an output */
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
Serial.println("[Spritz library encryption/decryption function test]\n");
/* MSG='ABC' KEY=0x00,0x01,0x02 */
testFunc(testMsg, sizeof(testMsg), testKey, sizeof(testKey));
delay(5000); /* Wait 5s */
Serial.println();
}