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

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Mon Sep 09 12:15:26 2013 +0000
Parent:
0:7a1237bd2d13
Child:
2:473bac39ae7c
Commit message:
remove dynamic memory allocation in MD2, MD5 and SHA-1

Changed in this revision

DES.h Show annotated file Show diff for this revision Revisions of this file
MD2.cpp Show annotated file Show diff for this revision Revisions of this file
MD2.h Show annotated file Show diff for this revision Revisions of this file
MD5.cpp Show annotated file Show diff for this revision Revisions of this file
SHA1.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MD2.cpp	Sat Sep 07 23:47:28 2013 +0000
+++ b/MD2.cpp	Mon Sep 09 12:15:26 2013 +0000
@@ -31,20 +31,20 @@
     memset(x, 0, 48);
 }
 
-void MD2::computeBuffer()
+void MD2::computeBlock(uint8_t *checksum2, uint8_t *x2, uint8_t *l2, uint8_t *buffer2)
 {
     for(int j = 0; j < 16; ++j)
     {
-        uint8_t c = buffer[j];
-        checksum[j] ^= s[c^l];
-        l = checksum[j];
+        uint8_t c = buffer2[j];
+        checksum2[j] ^= s[c^(*l2)];
+        *l2 = checksum2[j];
     }
 
 
     for(int j = 0; j < 16; ++j)
     {
-        x[16+j] = buffer[j];
-        x[32+j] = x[16+j] ^ x[j];
+        x2[16+j] = buffer2[j];
+        x2[32+j] = x2[16+j] ^ x2[j];
     }
 
     uint8_t t = 0;
@@ -53,8 +53,8 @@
     {
         for(int k = 0; k < 48; ++k)
         {
-            x[k] = x[k] ^ s[t];
-            t = x[k];
+            x2[k] = x2[k] ^ s[t];
+            t = x2[k];
         }
         t += j;
     }
@@ -70,11 +70,11 @@
     }
     int offset = 16-bufferLength;
     memcpy(&buffer[bufferLength], in, offset);
-    computeBuffer();
+    computeBlock(checksum, x, &l, buffer);
     while(length-offset > 16)
     {
         memcpy(buffer, &in[offset], 16);
-        computeBuffer();
+        computeBlock(checksum, x, &l, buffer);
         offset += 16;
     }
     if(offset > length)
@@ -88,7 +88,7 @@
     // compute what's left in the buffer
     int padding = 16 - bufferLength;
     memset(&buffer[bufferLength], padding, padding);
-    computeBuffer();
+    computeBlock(checksum, x, &l, buffer);
 
     for(int j = 0; j < 16; ++j)
     {
@@ -123,48 +123,42 @@
 
 void MD2::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
 {
-    uint8_t padding = 16 - (length % 16);
-    int totalLength = length + 16 + padding;
-    uint8_t *buffer = new uint8_t[totalLength];
-    memcpy(buffer, in, length);
-    memset(&buffer[length], padding, padding);
+    uint8_t buffer[16];
     uint8_t checksum[16];
     memset(checksum, 0, 16);
-    uint8_t l = 0;
-    
-    for(int i = 0; i < ((totalLength-16)/16); ++i)
-    {
-        for(int j = 0; j < 16; ++j)
-        {
-            uint8_t c = buffer[i*16+j];
-            checksum[j] ^= s[c^l];
-            l = checksum[j];
-        }
-    }
-    memcpy(&buffer[totalLength-16], checksum, 16);
-    
     uint8_t x[48];
     memset(x,0,48);
-    
-    for(int i = 0; i < (totalLength/16); ++i)
+    uint8_t l = 0;
+    uint32_t offset = 0;
+    while(length - offset >= 16)
     {
-        for(int j = 0; j < 16; ++j)
-        {
-            x[16+j] = buffer[i*16+j];
-            x[32+j] = x[16+j] ^ x[j];
-        }
-    
-        uint8_t t = 0;
+        computeBlock(checksum, x, &l, &in[offset]);
+        offset += 16;
+    }
+
+    uint8_t bufferLength = length - offset;
+    memcpy(buffer, &in[offset], bufferLength);
+    memset(&buffer[bufferLength], 16-bufferLength, 16-bufferLength);
+    computeBlock(checksum, x, &l, buffer);
+
+
+    for(int j = 0; j < 16; ++j)
+    {
+        x[16+j] = checksum[j];
+        x[32+j] = x[16+j] ^ x[j];
+    }
+
+    uint8_t t = 0;
         
-        for(int j = 0; j < 18; ++j)
+    for(int j = 0; j < 18; ++j)
+    {
+        for(int k = 0; k < 48; ++k)
         {
-            for(int k = 0; k < 48; ++k)
-            {
-                t = x[k] = x[k] ^ s[t];
-            }
-            t += j;
+            x[k] = x[k] ^ s[t];
+            t = x[k];
         }
+        t += j;
     }
-    delete[] buffer;
+
     memcpy(digest, x, 16);
 }
--- a/MD2.h	Sat Sep 07 23:47:28 2013 +0000
+++ b/MD2.h	Mon Sep 09 12:15:26 2013 +0000
@@ -16,8 +16,8 @@
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
         
     private :
-    
-        void computeBuffer();
+          
+        static void computeBlock(uint8_t *checksum, uint8_t *x, uint8_t *l2, uint8_t *buffer2);
     
         uint8_t bufferLength;
         uint8_t l;
--- a/MD5.cpp	Sat Sep 07 23:47:28 2013 +0000
+++ b/MD5.cpp	Mon Sep 09 12:15:26 2013 +0000
@@ -97,24 +97,11 @@
     }
     int offset = 64-bufferLength;
     memcpy(&buffer[bufferLength], in, offset);
-    uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d;
     computeRounds(&a, &b, &c, &d, buffer);
-    a += tmpA;
-    b += tmpB;
-    c += tmpC;
-    d += tmpD;
     while(length-offset > 64)
     {
         memcpy(buffer, &in[offset], 64);
-        tmpA = a;
-        tmpB = b;
-        tmpC = c;
-        tmpD = d;
         computeRounds(&a, &b, &c, &d, buffer);
-        a += tmpA;
-        b += tmpB;
-        c += tmpC;
-        d += tmpD;      
         offset += 64;
     }
     if(offset > length)
@@ -158,7 +145,10 @@
 
 void MD5::computeRounds(uint32_t *a2, uint32_t *b2, uint32_t *c2, uint32_t *d2, uint8_t *buffer)
 {
+
     uint32_t a = *a2, b = *b2, c = *c2, d = *d2;
+    uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d;
+
     uint32_t x[16];
     for(int j = 0; j < 16; ++j)
         memcpy(&x[j], &buffer[j*4], 4); 
@@ -187,6 +177,11 @@
     ROUND4(a,b,c,d,8,6,57);     ROUND4(d,a,b,c,15,10,58);   ROUND4(c,d,a,b,6,15,59);    ROUND4(b,c,d,a,13,21,60);
     ROUND4(a,b,c,d,4,6,61);     ROUND4(d,a,b,c,11,10,62);   ROUND4(c,d,a,b,2,15,63);    ROUND4(b,c,d,a,9,21,64);
 
+    a += tmpA;
+    b += tmpB;
+    c += tmpC;
+    d += tmpD;
+
     *a2 = a;
     *b2 = b;
     *c2 = c;
@@ -200,30 +195,38 @@
         padding = 56 - (length % 64);
     else
         padding = 56 + (64 - (length % 64));
-    uint32_t totalLength = length + padding + 8;
-    uint8_t *buffer = new uint8_t[totalLength];
-    memcpy(buffer, msg, length);
-    buffer[length] = 0x80;
-    memset(&buffer[length+1], 0, padding-1);
+        
+    uint32_t a = A, b = B, c = C, d = D;
+
+    uint32_t offset = 0;
+    while(length - offset >= 64)
+    {
+        computeRounds(&a, &b, &c, &d, &msg[offset]);
+        offset += 64;
+    }
+    uint8_t buffer[64];
+    memcpy(buffer, &msg[offset], length-offset);
+    uint8_t bufferLength = length - offset;
+    buffer[bufferLength++] = 0x80;
+    padding--;
+    while(padding > 0)
+    {
+        if(bufferLength == 64)
+        {
+            computeRounds(&a, &b, &c, &d, buffer);
+            bufferLength = 0;
+        }
+        buffer[bufferLength++] = 0;
+        padding--;
+    }
     uint64_t lengthBit = length * 8;
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
-    memcpy(&buffer[length+padding], &lengthBitLow, 4);
-    memcpy(&buffer[length+padding+4], &lengthBitHigh, 4);
+    memcpy(&buffer[56], &lengthBitLow, 4);
+    memcpy(&buffer[60], &lengthBitHigh, 4);
     
-    uint32_t a = A, b = B, c = C, d = D;
-    for(int i = 0; i < totalLength/64; ++i)
-    {   
-        uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d;
-        computeRounds(&a, &b, &c, &d, &buffer[64*i]);
-        
-        a += tmpA;
-        b += tmpB;
-        c += tmpC;
-        d += tmpD;
-    }
-    delete[] buffer;
-
+    computeRounds(&a, &b, &c, &d, buffer);
+    
     memcpy(digest, &a, 4);
     memcpy(&digest[4], &b, 4);
     memcpy(&digest[8], &c, 4);
--- a/SHA1.cpp	Sat Sep 07 23:47:28 2013 +0000
+++ b/SHA1.cpp	Mon Sep 09 12:15:26 2013 +0000
@@ -185,11 +185,30 @@
         padding = 56 - (length % 64);
     else
         padding = 56 + (64 - (length % 64));
-    uint32_t totalLength = length + padding + 8;
-    uint8_t *buffer = new uint8_t[totalLength];
-    memcpy(buffer, in, length);
-    buffer[length] = 0x80;
-    memset(&buffer[length+1], 0, padding-1);
+        
+    uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
+    uint32_t offset = 0;
+    while(length - offset >= 64)
+    {
+        computeBlock(&h0,&h1,&h2,&h3,&h4, &in[offset]);
+        offset += 64;
+    }
+
+    uint8_t bufferLength = length - offset;
+    uint8_t buffer[64];
+    memcpy(buffer, &in[offset], bufferLength);
+    buffer[bufferLength++] = 0x80;
+    padding--;
+    while(padding > 0)
+    {
+        if(bufferLength == 64)
+        {
+            computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
+            bufferLength++;
+        }
+        buffer[bufferLength++] = 0;
+        padding--;
+    }
     uint64_t lengthBit = length * 8;
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
@@ -198,19 +217,15 @@
     l[1] = lengthBitLow >> 16;
     l[2] = lengthBitLow >> 8;
     l[3] = lengthBitLow;
-    memcpy(&buffer[length+padding+4], l, 4);
+    memcpy(&buffer[60], l, 4);
     l[0] = lengthBitHigh >> 24;
     l[1] = lengthBitHigh >> 16;
     l[2] = lengthBitHigh >> 8;
     l[3] = lengthBitHigh;
-    memcpy(&buffer[length+padding], l, 4);
+    memcpy(&buffer[56], l, 4);
     
-    uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
-    for(int i = 0; i < totalLength/64;  ++i)
-        computeBlock(&h0,&h1,&h2,&h3,&h4, &buffer[64*i]);
+    computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
 
-    delete[] buffer;
-    
     digest[0] = h0 >> 24;
     digest[1] = h0 >> 16;
     digest[2] = h0 >> 8;