Skip to content

Commit

Permalink
Add SHA-1 back to the library as a legacy algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rweather committed Apr 27, 2018
1 parent 455549f commit 0bd0fa1
Show file tree
Hide file tree
Showing 5 changed files with 627 additions and 3 deletions.
11 changes: 8 additions & 3 deletions doc/crypto.dox
Expand Up @@ -63,17 +63,18 @@ CPU resources are constrained:
\li Block ciphers: Speck, SpeckSmall, SpeckTiny

These algorithms are fairly new, but they are ideal for Arduino devices.
They don't yet appear in any internationally adopted standards yet but any
They don't appear in any internationally adopted standards yet but any
algorithms that are adopted into standards later will be moved to the
core library. Maybe you'll be the one to create that new standard!

\subsection crypto_legacy_algorithms Legacy algorithms

Legacy algorithms in the "libraries/CryptoLegacy" are those that should
probably not be used in new protocol designs, but may be required for
Legacy algorithms in the "libraries/CryptoLegacy" directory are those that
should probably not be used in new protocol designs, but may be required for
backwards-compatibility with older protocols:

\li Block cipher modes: CFB, CBC, OFB
\li Hash algorithms: SHA1

CBC is included in the legacy list because cryptography experts no longer
recommend it for use in newer designs. It was an important mode in the past
Expand Down Expand Up @@ -168,6 +169,7 @@ Ardunino Mega 2560 running at 16 MHz are similar:
<tr><td>Ascon128</td><td align="right">42.71us</td><td align="right">43.07us</td><td align="right">738.68us</td><td align="right">60</td></tr>
<tr><td colspan="5"> </td></tr>
<tr><td>Hash Algorithm</td><td align="right">Hashing (per byte)</td><td align="right">Finalization</td><td> </td><td>State Size (bytes)</td></tr>
<tr><td>SHA1</td><td align="right">21.86us</td><td align="right">1421.86us</td><td align="right"> </td><td align="right">95</td></tr>
<tr><td>SHA256</td><td align="right">43.85us</td><td align="right">2841.04us</td><td align="right"> </td><td align="right">107</td></tr>
<tr><td>SHA512</td><td align="right">122.82us</td><td align="right">15953.42us</td><td align="right"> </td><td align="right">211</td></tr>
<tr><td>SHA3_256</td><td align="right">60.69us</td><td align="right">8180.24us</td><td align="right"> </td><td align="right">205</td></tr>
Expand All @@ -176,6 +178,7 @@ Ardunino Mega 2560 running at 16 MHz are similar:
<tr><td>BLAKE2b</td><td align="right">65.22us</td><td align="right">8375.34us</td><td align="right"> </td><td align="right">211</td></tr>
<tr><td colspan="5"> </td></tr>
<tr><td>Authentication Algorithm</td><td align="right">Hashing (per byte)</td><td align="right">Finalization</td><td align="right">Key Setup</td><td>State Size (bytes)</td></tr>
<tr><td>SHA1 (HMAC mode)</td><td align="right">21.86us</td><td align="right">4290.62us</td><td align="right">1418.49us</td><td align="right">95</td></tr>
<tr><td>SHA256 (HMAC mode)</td><td align="right">43.85us</td><td align="right">8552.61us</td><td align="right">2836.49us</td><td align="right">107</td></tr>
<tr><td>BLAKE2s (Keyed mode)</td><td align="right">20.65us</td><td align="right">1335.25us</td><td align="right">1339.51us</td><td align="right">107</td></tr>
<tr><td>BLAKE2s (HMAC mode)</td><td align="right">20.65us</td><td align="right">4055.56us</td><td align="right">1350.00us</td><td align="right">107</td></tr>
Expand Down Expand Up @@ -257,6 +260,7 @@ All figures are for the Arduino Due running at 84 MHz:
<tr><td>Ascon128</td><td align="right">3.52us</td><td align="right">3.50us</td><td align="right">51.67us</td><td align="right">72</td></tr>
<tr><td colspan="5"> </td></tr>
<tr><td>Hash Algorithm</td><td align="right">Hashing (per byte)</td><td align="right">Finalization</td><td> </td><td>State Size (bytes)</td></tr>
<tr><td>SHA1</td><td align="right">0.94us</td><td align="right">62.34us</td><td align="right"> </td><td align="right">112</td></tr>
<tr><td>SHA256</td><td align="right">1.15us</td><td align="right">76.60us</td><td align="right"> </td><td align="right">120</td></tr>
<tr><td>SHA512</td><td align="right">2.87us</td><td align="right">370.37us</td><td align="right"> </td><td align="right">224</td></tr>
<tr><td>SHA3_256</td><td align="right">5.64us</td><td align="right">735.29us</td><td align="right"> </td><td align="right">224</td></tr>
Expand All @@ -265,6 +269,7 @@ All figures are for the Arduino Due running at 84 MHz:
<tr><td>BLAKE2b</td><td align="right">1.28us</td><td align="right">164.66us</td><td align="right"> </td><td align="right">224</td></tr>
<tr><td colspan="5"> </td></tr>
<tr><td>Authentication Algorithm</td><td align="right">Hashing (per byte)</td><td align="right">Finalization</td><td align="right">Key Setup</td><td>State Size (bytes)</td></tr>
<tr><td>SHA1 (HMAC mode)</td><td align="right">0.94us</td><td align="right">196.74us</td><td align="right">68.06us</td><td align="right">112</td></tr>
<tr><td>SHA256 (HMAC mode)</td><td align="right">1.15us</td><td align="right">238.98us</td><td align="right">80.44us</td><td align="right">120</td></tr>
<tr><td>BLAKE2s (Keyed mode)</td><td align="right">0.80us</td><td align="right">53.39us</td><td align="right">55.10us</td><td align="right">120</td></tr>
<tr><td>BLAKE2s (HMAC mode)</td><td align="right">0.80us</td><td align="right">168.20us</td><td align="right">57.60us</td><td align="right">120</td></tr>
Expand Down
2 changes: 2 additions & 0 deletions host/Crypto/Makefile
Expand Up @@ -65,6 +65,7 @@ SOURCES = \
P521.cpp \
Poly1305.cpp \
RNG_host.cpp \
SHA1.cpp \
SHA256.cpp \
SHA3.cpp \
SHA512.cpp \
Expand Down Expand Up @@ -100,6 +101,7 @@ SKETCHES = \
TestP521/TestP521.ino \
TestP521Math/TestP521Math.ino \
TestPoly1305/TestPoly1305.ino \
TestSHA1/TestSHA1.ino \
TestSHA256/TestSHA256.ino \
TestSHA3_256/TestSHA3_256.ino \
TestSHA3_512/TestSHA3_512.ino \
Expand Down
319 changes: 319 additions & 0 deletions libraries/CryptoLegacy/examples/TestSHA1/TestSHA1.ino
@@ -0,0 +1,319 @@
/*
* Copyright (C) 2015 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

/*
This example runs tests on the SHA1 implementation to verify correct behaviour.
*/

#include <Crypto.h>
#include <SHA1.h>
#include <string.h>

#define HASH_SIZE 20
#define BLOCK_SIZE 64

struct TestHashVector
{
const char *name;
const char *key;
const char *data;
uint8_t hash[HASH_SIZE];
};

static TestHashVector const testVectorSHA1_1 = {
"SHA-1 #1",
0,
"abc",
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C,
0x9C, 0xD0, 0xD8, 0x9D}
};
static TestHashVector const testVectorSHA1_2 = {
"SHA-1 #2",
0,
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
{0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
0xE5, 0x46, 0x70, 0xF1}
};
static TestHashVector const testVectorHMAC_SHA1_1 = {
"HMAC-SHA-1 #1",
"",
"",
{0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08,
0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63,
0x70, 0x69, 0x0e, 0x1d}
};
static TestHashVector const testVectorHMAC_SHA1_2 = {
"HMAC-SHA-1 #2",
"key",
"The quick brown fox jumps over the lazy dog",
{0xde, 0x7c, 0x9b, 0x85, 0xb8, 0xb7, 0x8a, 0xa6,
0xbc, 0x8a, 0x7a, 0x36, 0xf7, 0x0a, 0x90, 0x70,
0x1c, 0x9d, 0xb4, 0xd9}
};

SHA1 sha1;

byte buffer[128];

bool testHash_N(Hash *hash, const struct TestHashVector *test, size_t inc)
{
size_t size = strlen(test->data);
size_t posn, len;
uint8_t value[HASH_SIZE];

hash->reset();
for (posn = 0; posn < size; posn += inc) {
len = size - posn;
if (len > inc)
len = inc;
hash->update(test->data + posn, len);
}
hash->finalize(value, sizeof(value));
if (memcmp(value, test->hash, sizeof(value)) != 0)
return false;

return true;
}

void testHash(Hash *hash, const struct TestHashVector *test)
{
bool ok;

Serial.print(test->name);
Serial.print(" ... ");

ok = testHash_N(hash, test, strlen(test->data));
ok &= testHash_N(hash, test, 1);
ok &= testHash_N(hash, test, 2);
ok &= testHash_N(hash, test, 5);
ok &= testHash_N(hash, test, 8);
ok &= testHash_N(hash, test, 13);
ok &= testHash_N(hash, test, 16);
ok &= testHash_N(hash, test, 24);
ok &= testHash_N(hash, test, 63);
ok &= testHash_N(hash, test, 64);

if (ok)
Serial.println("Passed");
else
Serial.println("Failed");
}

// Very simple method for hashing a HMAC inner or outer key.
void hashKey(Hash *hash, const uint8_t *key, size_t keyLen, uint8_t pad)
{
size_t posn;
uint8_t buf;
uint8_t result[HASH_SIZE];
if (keyLen <= BLOCK_SIZE) {
hash->reset();
for (posn = 0; posn < BLOCK_SIZE; ++posn) {
if (posn < keyLen)
buf = key[posn] ^ pad;
else
buf = pad;
hash->update(&buf, 1);
}
} else {
hash->reset();
hash->update(key, keyLen);
hash->finalize(result, HASH_SIZE);
hash->reset();
for (posn = 0; posn < BLOCK_SIZE; ++posn) {
if (posn < HASH_SIZE)
buf = result[posn] ^ pad;
else
buf = pad;
hash->update(&buf, 1);
}
}
}

void testHMAC(Hash *hash, size_t keyLen)
{
uint8_t result[HASH_SIZE];

Serial.print("HMAC-SHA-1 keysize=");
Serial.print(keyLen);
Serial.print(" ... ");

// Construct the expected result with a simple HMAC implementation.
memset(buffer, (uint8_t)keyLen, keyLen);
hashKey(hash, buffer, keyLen, 0x36);
memset(buffer, 0xBA, sizeof(buffer));
hash->update(buffer, sizeof(buffer));
hash->finalize(result, HASH_SIZE);
memset(buffer, (uint8_t)keyLen, keyLen);
hashKey(hash, buffer, keyLen, 0x5C);
hash->update(result, HASH_SIZE);
hash->finalize(result, HASH_SIZE);

// Now use the library to compute the HMAC.
hash->resetHMAC(buffer, keyLen);
memset(buffer, 0xBA, sizeof(buffer));
hash->update(buffer, sizeof(buffer));
memset(buffer, (uint8_t)keyLen, keyLen);
hash->finalizeHMAC(buffer, keyLen, buffer, HASH_SIZE);

// Check the result.
if (!memcmp(result, buffer, HASH_SIZE))
Serial.println("Passed");
else
Serial.println("Failed");
}

void testHMAC(Hash *hash, const struct TestHashVector *test)
{
uint8_t result[HASH_SIZE];

Serial.print(test->name);
Serial.print(" ... ");

hash->resetHMAC(test->key, strlen(test->key));
hash->update(test->data, strlen(test->data));
hash->finalizeHMAC(test->key, strlen(test->key), result, sizeof(result));

if (!memcmp(result, test->hash, HASH_SIZE))
Serial.println("Passed");
else
Serial.println("Failed");
}

void perfHash(Hash *hash)
{
unsigned long start;
unsigned long elapsed;
int count;

Serial.print("Hashing ... ");

for (size_t posn = 0; posn < sizeof(buffer); ++posn)
buffer[posn] = (uint8_t)posn;

hash->reset();
start = micros();
for (count = 0; count < 1000; ++count) {
hash->update(buffer, sizeof(buffer));
}
elapsed = micros() - start;

Serial.print(elapsed / (sizeof(buffer) * 1000.0));
Serial.print("us per byte, ");
Serial.print((sizeof(buffer) * 1000.0 * 1000000.0) / elapsed);
Serial.println(" bytes per second");
}

void perfFinalize(Hash *hash)
{
unsigned long start;
unsigned long elapsed;
int count;

Serial.print("Finalizing ... ");

hash->reset();
hash->update("abc", 3);
start = micros();
for (count = 0; count < 1000; ++count) {
hash->finalize(buffer, hash->hashSize());
}
elapsed = micros() - start;

Serial.print(elapsed / 1000.0);
Serial.print("us per op, ");
Serial.print((1000.0 * 1000000.0) / elapsed);
Serial.println(" ops per second");
}

void perfHMAC(Hash *hash)
{
unsigned long start;
unsigned long elapsed;
int count;

Serial.print("HMAC Reset ... ");

for (size_t posn = 0; posn < sizeof(buffer); ++posn)
buffer[posn] = (uint8_t)posn;

start = micros();
for (count = 0; count < 1000; ++count) {
hash->resetHMAC(buffer, hash->hashSize());
}
elapsed = micros() - start;

Serial.print(elapsed / 1000.0);
Serial.print("us per op, ");
Serial.print((1000.0 * 1000000.0) / elapsed);
Serial.println(" ops per second");

Serial.print("HMAC Finalize ... ");

hash->resetHMAC(buffer, hash->hashSize());
hash->update("abc", 3);
start = micros();
for (count = 0; count < 1000; ++count) {
hash->finalizeHMAC(buffer, hash->hashSize(), buffer, hash->hashSize());
}
elapsed = micros() - start;

Serial.print(elapsed / 1000.0);
Serial.print("us per op, ");
Serial.print((1000.0 * 1000000.0) / elapsed);
Serial.println(" ops per second");
}

void setup()
{
Serial.begin(9600);

Serial.println();

Serial.print("State Size ...");
Serial.println(sizeof(SHA1));
Serial.println();

Serial.println("Test Vectors:");
testHash(&sha1, &testVectorSHA1_1);
testHash(&sha1, &testVectorSHA1_2);
testHMAC(&sha1, &testVectorHMAC_SHA1_1);
testHMAC(&sha1, &testVectorHMAC_SHA1_2);
testHMAC(&sha1, (size_t)0);
testHMAC(&sha1, 1);
testHMAC(&sha1, HASH_SIZE);
testHMAC(&sha1, BLOCK_SIZE);
testHMAC(&sha1, BLOCK_SIZE + 1);
testHMAC(&sha1, sizeof(buffer));

Serial.println();

Serial.println("Performance Tests:");
perfHash(&sha1);
perfFinalize(&sha1);
perfHMAC(&sha1);
}

void loop()
{
}

0 comments on commit 0bd0fa1

Please sign in to comment.