-
Notifications
You must be signed in to change notification settings - Fork 14
/
SpritzHashTest.ino
118 lines (100 loc) · 3.52 KB
/
SpritzHashTest.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Spritz Cipher Hash Test
*
* This example code test SpritzCipher library hash output
* using test vectors from Spritz paper "RS14.pdf" Page 30:
* <https://people.csail.mit.edu/rivest/pubs/RS14.pdf>
*
* 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 testData1[3] = { 'A', 'B', 'C' };
const byte testData2[4] = { 's', 'p', 'a', 'm' };
const byte testData3[7] = { 'a', 'r', 'c', 'f', 'o', 'u', 'r' };
/* Test vectors */
/* Data 'ABC' hash test vectors */
const byte testVector1[32] =
{ 0x02, 0x8f, 0xa2, 0xb4, 0x8b, 0x93, 0x4a, 0x18,
0x62, 0xb8, 0x69, 0x10, 0x51, 0x3a, 0x47, 0x67,
0x7c, 0x1c, 0x2d, 0x95, 0xec, 0x3e, 0x75, 0x70,
0x78, 0x6f, 0x1c, 0x32, 0x8b, 0xbd, 0x4a, 0x47
};
/* Data 'spam' hash test vectors */
const byte testVector2[32] =
{ 0xac, 0xbb, 0xa0, 0x81, 0x3f, 0x30, 0x0d, 0x3a,
0x30, 0x41, 0x0d, 0x14, 0x65, 0x74, 0x21, 0xc1,
0x5b, 0x55, 0xe3, 0xa1, 0x4e, 0x32, 0x36, 0xb0,
0x39, 0x89, 0xe7, 0x97, 0xc7, 0xaf, 0x47, 0x89
};
/* Data 'arcfour' hash test vectors */
const byte testVector3[32] =
{ 0xff, 0x8c, 0xf2, 0x68, 0x09, 0x4c, 0x87, 0xb9,
0x5f, 0x74, 0xce, 0x6f, 0xee, 0x9d, 0x30, 0x03,
0xa5, 0xf9, 0xfe, 0x69, 0x44, 0x65, 0x3c, 0xd5,
0x0e, 0x66, 0xbf, 0x18, 0x9c, 0x63, 0xf6, 0x99
};
void testFunc(const byte ExpectedOutput[32], const byte *data, byte dataLen)
{
byte hashLen = 32; /* 256-bit */
byte digest[hashLen]; /* Output buffer */
byte digest_2[hashLen]; /* Output buffer for chunk by chunk API */
spritz_ctx hash_ctx; /* the CTX for chunk by chunk API */
unsigned int i;
/* Print input */
for (i = 0; i < dataLen; i++) {
Serial.write(data[i]);
}
Serial.println();
spritz_hash_setup(&hash_ctx);
/* For easy test: code add a byte each time */
for (i = 0; i < dataLen; i++) {
spritz_hash_update(&hash_ctx, data + i, 1);
}
spritz_hash_final(&hash_ctx, digest_2, hashLen);
spritz_hash(digest, hashLen, data, dataLen);
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)) || spritz_compare(digest_2, ExpectedOutput, sizeof(digest_2))) {
/* 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_hash*() test]\n");
/* Data: ABC */
testFunc(testVector1, testData1, sizeof(testData1));
/* Data: spam */
testFunc(testVector2, testData2, sizeof(testData2));
/* Data: arcfour */
testFunc(testVector3, testData3, sizeof(testData3));
delay(5000); /* Wait 5s */
Serial.println();
}