-
Notifications
You must be signed in to change notification settings - Fork 14
/
SpritzMACTest.ino
82 lines (66 loc) · 2.3 KB
/
SpritzMACTest.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
/**
* Spritz Cipher MAC Test
*
* This example code test SpritzCipher library MAC output.
*
* 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 };
/* Test vectors */
/* MSG='ABC' KEY=0x00,0x01,0x02 MAC test vectors */
const byte MACtestVector[32] =
{ 0xbe, 0x8e, 0xdc, 0xf2, 0x76, 0xcf, 0x57, 0xb4,
0x0e, 0xbc, 0x8e, 0x22, 0x43, 0x45, 0x7e, 0x3e,
0xb7, 0xc6, 0x4d, 0x4e, 0x99, 0x1e, 0x93, 0x58,
0xce, 0x81, 0xef, 0xb1, 0x6c, 0xce, 0xc7, 0xed
};
void testFunc(const byte ExpectedOutput[32], const byte *msg, byte msgLen, const byte *key, byte keyLen)
{
byte macLen = 32; /* 256-bit */
byte digest[macLen]; /* Output buffer */
unsigned int i;
spritz_mac(digest, macLen, msg, msgLen, key, keyLen);
for (i = 0; i < sizeof(digest); i++) {
if (digest[i] < 0x10) { /* To print "0F" not "F" */
Serial.write('0');
}
Serial.print(digest[i], HEX);
}
/* Check the output */
if (spritz_compare(digest, ExpectedOutput, sizeof(digest))) {
/* 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 spritz_mac(MSG='ABC' KEY=0x00,0x01,0x02) test]\n");
/* MSG='ABC' KEY=0x00,0x01,0x02 MAC test vectors */
testFunc(MACtestVector, testMsg, sizeof(testMsg), testKey, sizeof(testKey));
delay(5000); /* Wait 5s */
Serial.println();
}