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:
2:473bac39ae7c
Child:
4:0da19393bd57
--- a/MD2.cpp	Mon Sep 09 16:16:24 2013 +0000
+++ b/MD2.cpp	Wed Sep 11 17:22:40 2013 +0000
@@ -28,17 +28,20 @@
 l(0)
 {
     memset(checksum, 0, 16);
-    memset(x, 0, 48);
+    memset(x, 0, 16);
 }
 
 void MD2::computeBlock(uint8_t *checksum2, uint8_t *x2, uint8_t *l2, uint8_t *buffer2)
 {
-    for(int j = 0; j < 16; ++j)
+    if(checksum2 != buffer2)
     {
-        uint8_t c = buffer2[j];
-        *l2 = (checksum2[j] ^= s[c^(*l2)]);
+        for(int j = 0; j < 16; ++j)
+        {
+            uint8_t c = buffer2[j];
+            *l2 = (checksum2[j] ^= s[c^(*l2)]);
+        }
     }
-
+    
     uint32_t *x3 = (uint32_t*)x2;
     uint32_t *buffer3 = (uint32_t*)buffer2;
     
@@ -53,35 +56,94 @@
     
     for(int j = 0; j < 18; ++j)
     {
-        for(int k = 0; k < 48; ++k)
-        {
-            t = (x2[k] ^= s[t]);
-        }
+        t = (x2[0] ^= s[t]);
+        t = (x2[1] ^= s[t]);
+        t = (x2[2] ^= s[t]);
+        t = (x2[3] ^= s[t]);
+        t = (x2[4] ^= s[t]);
+        t = (x2[5] ^= s[t]);
+        t = (x2[6] ^= s[t]);
+        t = (x2[7] ^= s[t]);
+        t = (x2[8] ^= s[t]);
+        t = (x2[9] ^= s[t]);
+        t = (x2[10] ^= s[t]);
+        t = (x2[11] ^= s[t]);
+        t = (x2[12] ^= s[t]);
+        t = (x2[13] ^= s[t]);
+        t = (x2[14] ^= s[t]);
+        t = (x2[15] ^= s[t]);
+        t = (x2[16] ^= s[t]);
+        t = (x2[17] ^= s[t]);
+        t = (x2[18] ^= s[t]);
+        t = (x2[19] ^= s[t]);
+        t = (x2[20] ^= s[t]);
+        t = (x2[21] ^= s[t]);
+        t = (x2[22] ^= s[t]);
+        t = (x2[23] ^= s[t]);            
+        t = (x2[24] ^= s[t]);
+        t = (x2[25] ^= s[t]);
+        t = (x2[26] ^= s[t]);
+        t = (x2[27] ^= s[t]);
+        t = (x2[28] ^= s[t]);
+        t = (x2[29] ^= s[t]);
+        t = (x2[30] ^= s[t]);
+        t = (x2[31] ^= s[t]);
+        t = (x2[32] ^= s[t]);
+        t = (x2[33] ^= s[t]);
+        t = (x2[34] ^= s[t]);
+        t = (x2[35] ^= s[t]);
+        t = (x2[36] ^= s[t]);
+        t = (x2[37] ^= s[t]);
+        t = (x2[38] ^= s[t]);
+        t = (x2[39] ^= s[t]);
+        t = (x2[40] ^= s[t]);
+        t = (x2[41] ^= s[t]);
+        t = (x2[42] ^= s[t]);
+        t = (x2[43] ^= s[t]);
+        t = (x2[44] ^= s[t]);
+        t = (x2[45] ^= s[t]);
+        t = (x2[46] ^= s[t]);
+        t = (x2[47] ^= s[t]);            
+
         t += j;
     }
 }
 
 void MD2::add(uint8_t *in, uint32_t length)
-{
-    if(length < 16-bufferLength)
+{ 
+    if(bufferLength == 0)
+    {
+        while(length >= 16)
+        {
+            computeBlock(checksum, x, &l, in);
+            length -= 16;
+            in += 16;
+        }
+        bufferLength = length;
+        memcpy(buffer, in, length);
+    }
+    else if(length < 16-bufferLength)
     {
         memcpy(&buffer[bufferLength], in, length);
         bufferLength += length;
-        return;
     }
-    int offset = 16-bufferLength;
-    memcpy(&buffer[bufferLength], in, offset);
-    computeBlock(checksum, x, &l, buffer);
-    while(length-offset > 16)
+    else
     {
-        memcpy(buffer, &in[offset], 16);
+        int offset = 16-bufferLength;
+        memcpy(&buffer[bufferLength], in, offset);
         computeBlock(checksum, x, &l, buffer);
-        offset += 16;
+        in += offset;
+        length -= offset;
+        while(length >= 16)
+        {
+            computeBlock(checksum, x, &l, in);
+            in += 16;
+            length -= 16;
+        }
+        bufferLength = length;
+        memcpy(buffer, &in, length);
     }
-    if(offset > length)
-        offset -= 16;
-    bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
+    
 }
 
 void MD2::computeDigest(uint8_t *digest)
@@ -90,30 +152,19 @@
     int padding = 16 - bufferLength;
     memset(&buffer[bufferLength], padding, padding);
     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];
-    }
+    computeBlock(checksum, x, &l, checksum);
+    memcpy(digest, x, 16);
 
-    uint8_t t = 0;
-        
-    for(int j = 0; j < 18; ++j)
-    {
-        for(int k = 0; k < 48; ++k)
-        {
-            t = (x[k] ^= s[t]);
-        }
-        t += j;
-    }
-    
+    uint32_t *x2 = (uint32_t*)x;
+    uint32_t *checksum2 = (uint32_t*)checksum;
+
     // reset state
     bufferLength = 0;
     l = 0;
-    memset(checksum, 0, 16);
-    memcpy(digest, x, 16);
-    memset(x,0,48);
+    checksum2[0] = x2[0] = 0;
+    checksum2[1] = x2[1] = 0;
+    checksum2[2] = x2[2] = 0;
+    checksum2[3] = x2[3] = 0;
 }
 
 uint8_t MD2::outputSize() const
@@ -123,43 +174,23 @@
 
 void MD2::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
 {
-    uint8_t data[80];
-    memset(data, 0, 64);
-    uint8_t *x = data;
-    uint32_t *x2 = (uint32_t*)data;
-    uint8_t *checksum = &data[48];
-    uint32_t *checksum2 = (uint32_t*)&data[48];
-    uint8_t *buffer = &data[64];
+    uint8_t x[48];
+    uint8_t checksum[16];
+    uint8_t buffer[16];
+    memset(x, 0, 16);
+    memset(checksum, 0, 16);
     uint8_t l = 0;
-    uint32_t offset = 0;
-    while(length - offset >= 16)
+    while(length >= 16)
     {
-        computeBlock(checksum, x, &l, &in[offset]);
-        offset += 16;
+        computeBlock(checksum, x, &l, in);
+        length -= 16;
+        in += 16;
     }
 
-    uint8_t bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
-    uint8_t padding = 16-bufferLength;
-    memset(&buffer[bufferLength], padding, padding);
+    memcpy(buffer, in, length);
+    uint8_t padding = 16-length;
+    memset(&buffer[length], padding, padding);
     computeBlock(checksum, x, &l, buffer);
-
-    for(int j = 0; j < 4; ++j)
-    {
-        x2[4+j] = checksum2[j];
-        x2[8+j] = x2[4+j] ^ x2[j];
-    }   
-
-    uint8_t t = 0;
-
-    for(int j = 0; j < 18; ++j)
-    {
-        for(int k = 0; k < 48; ++k)
-        {
-            t = (x[k] ^= s[t]);
-        }
-        t += j;
-    }
-
+    computeBlock(checksum,x, &l, checksum);
     memcpy(digest, x, 16);
 }