This library implements some hash and cryptographic algorithms.

Dependents:   mBuinoBlinky PB_Emma_Ethernet SLOTrashHTTP Garagem ... more

This library implements the following algorithms :

  • RC4
  • AES (AES-128, AES-192, AES-256)
  • DES
  • Triple DES (EDE)
  • MD2
  • MD4
  • MD5
  • SHA-1
  • SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512)

The hash algorithms have been optimized for the mbed and you should get decent performance. However, I did not optimize the ciphers. Also, I did not test extensively these algorithms : it should work but you may find some bugs. Block ciphers support two modes : ECB and CBC.

Warning

If you are using SHA-384 or SHA-512, be aware that it produces large binary files and the compilation (using the online compiler) takes much longer to execute. It may happen that the compiler stops because it timed-out. In this case, just compile again and it should work.

Computing hash

You can compute the hash of some data in two different ways. The first one is the easiest, each hash algorithm has a static method that takes some data and compute the hash from it.

Computing hash using method 1

#include "Crypto.h"
#include "mbed.h"

static const char msg[] = "mbed is great !";

int main()
{
    uint8_t hash[16];
    MD2::computeHash(hash, (uint8_t*)msg, strlen(msg));
    printf("hash: ");
    for(int i = 0; i < 16; ++i)
        printf("%02x", hash[i]);
    printf("\n");
    
    return 0;
}

The second one is slightly slower (around 2-3% slower) but it allows you to compute the hash of some data in several steps (by calling update method). This is the method you should use if you need to compute the hash from a large source and you don't have enough memory to store it in a single buffer.

Computing hash using method 2

#include "Crypto.h"
#include "mbed.h"

static const char msg[] = "mbed is great !";

int main()
{
    uint8_t hash[16];
    MD2 h;
    h.update((uint8_t*)msg, strlen(msg));
    h.finalize(hash);
    printf("hash: ");
    for(int i = 0; i < 16; ++i)
        printf("%02x", hash[i]);
    printf("\n");
    
    return 0;
}

TODO

  • optimize ciphers
  • add doc
Revision:
3:85c6ee25cf3e
Parent:
0:7a1237bd2d13
Child:
4:0da19393bd57
--- a/SHA2_64.cpp	Mon Sep 09 16:16:24 2013 +0000
+++ b/SHA2_64.cpp	Wed Sep 11 17:22:40 2013 +0000
@@ -39,74 +39,26 @@
 
 static uint64_t revWord(uint64_t w)
 {
-    uint8_t buffer[8];
-    buffer[0] = w >> 56;
-    buffer[1] = w >> 48;
-    buffer[2] = w >> 40;
-    buffer[3] = w >> 32;
-    buffer[4] = w >> 24;
-    buffer[5] = w >> 16;
-    buffer[6] = w >> 8;
-    buffer[7] = w;
-    
-    uint64_t res = buffer[7];
-    res <<= 8;
-    res |= buffer[6];
-    res <<= 8;
-    res |= buffer[5];
-    res <<= 8;
-    res |= buffer[4];
-    res <<= 8;
-    res |= buffer[3];
-    res <<= 8;
-    res |= buffer[2];
-    res <<= 8;
-    res |= buffer[1];
-    res <<= 8;
-    res |= buffer[0];
-
-    return res;
-}
-    
-static uint64_t rotLeft(uint64_t w, uint8_t n)
-{
-    return (w << n) | (w >> (64-n));
+    return (w >> 56) 
+        | ((w & 0x00FF000000000000) >> 40) 
+        | ((w & 0x0000FF0000000000) >> 24) 
+        | ((w & 0x000000FF00000000) >> 8) 
+        | ((w & 0x00000000FF000000) << 8) 
+        | ((w & 0x0000000000FF0000) << 24) 
+        | ((w & 0x000000000000FF00) << 40) 
+        | ((w & 0x00000000000000FF) << 56);
 }
 
-static uint64_t rotRight(uint64_t w, uint8_t n)
-{
-    return rotLeft(w,64-n);
-}
-
-static uint64_t CH(uint64_t x, uint64_t y, uint64_t z)
-{
-    return (x & y) ^ ((~x) & z);
-}
-
-static uint64_t MAJ(uint64_t x, uint64_t y, uint64_t z)
-{
-    return (x & y) ^ (x & z) ^ (y & z);
-}
+#define ROTL(W,N) (((W) << (N)) | ((W) >> (64-(N))))
+#define ROTR(W,N) (((W) >> (N)) | ((W) << (64-(N))))
+#define CH(X,Y,Z) (((X) & (Y)) ^ ((~(X)) & (Z)))
+#define MAJ(X,Y,Z) (((X) & (Y)) ^ ((X) & (Z)) ^ ((Y) & (Z)))
+#define BSIG0(X) (ROTR(X,28) ^ ROTR(X,34) ^ ROTR(X,39))
+#define BSIG1(X) (ROTR(X,14) ^ ROTR(X,18) ^ ROTR(X,41))
+#define SSIG0(X) (ROTR((X),1) ^ ROTR((X),8) ^ ((X) >> 7))
+#define SSIG1(X) (ROTR((X),19) ^ ROTR((X),61) ^ ((X) >> 6))
 
-static uint64_t BSIG0(uint64_t x)
-{
-    return rotRight(x,28) ^ rotRight(x,34) ^ rotRight(x,39);
-}
-
-static uint64_t BSIG1(uint64_t x)
-{
-    return rotRight(x,14) ^ rotRight(x,18) ^ rotRight(x,41);
-}
-
-static uint64_t SSIG0(uint64_t x)
-{
-    return rotRight(x,1) ^ rotRight(x,8) ^ (x >> 7);
-}
-
-static uint64_t SSIG1(uint64_t x)
-{
-    return rotRight(x,19) ^ rotRight(x,61) ^ (x>>6);
-}
+    
 
 SHA2_64::SHA2_64(SHA2_64_TYPE t):
 type(t),
@@ -280,37 +232,37 @@
 
 void SHA2_64::computeDigest(SHA2_64_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length)
 {
+    uint64_t lengthBit = length * 8;
     uint64_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
     uint64_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
-    int offset = 0;
-    while(length - offset >= 128)
-    {
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &in[offset]);
-        offset += 128;
-    }
-    uint8_t bufferLength = length-offset;
-    uint8_t buffer[128];
-    memcpy(buffer, &in[offset],bufferLength); 
-    uint16_t padding;
+    
+    int padding;
     if(length % 128 < 112)
         padding = 112 - (length % 128);
     else
         padding = 112 + (128 - (length % 128));
-    buffer[bufferLength] = 0x80;
-    bufferLength++;
-    padding--;
-    while(padding > 0)
+        
+    while(length >= 128)
     {
-        if(bufferLength == 128)
-        {
-            computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-            bufferLength = 0;
-        }
-        buffer[bufferLength] = 0;
-        bufferLength++;
-        padding--;
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in);
+        in += 128;
+        length -= 128;
     }
-    uint64_t lengthBit = length * 8;
+    uint8_t buffer[128];
+    memcpy(buffer, in,length); 
+    buffer[length] = 0x80;
+    length++;
+    padding--;
+
+    if(padding+length == 112)
+        memset(&buffer[length], 0, padding);
+    else
+    {
+        memset(&buffer[length], 0, 128-length);
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+        memset(buffer, 0, length);
+    }
+    
     lengthBit = revWord(lengthBit);
     memset(&buffer[112], 0, 8); 
     memcpy(&buffer[120], &lengthBit, 8);