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:
13:ac8e23b98dae
Parent:
10:bc9c23aa3870
--- a/hash/SHA2_32.cpp	Tue Apr 08 19:39:25 2014 +0000
+++ b/hash/SHA2_32.cpp	Sun May 11 11:14:51 2014 +0000
@@ -4,17 +4,18 @@
 
 
 static const uint8_t MASK = 0x0F;
+
 #define W(t) (w[(t)] = SSIG1(w[((t)+14)&MASK]) + w[((t)+9)&MASK] + SSIG0(w[((t)+1)&MASK]) + w[t])
 
 #define ROTL(W,N) (((W) << (N)) | ((W) >> (32-(N))))
-#define ROTR(W,N) (__ror(W,N)) 
+#define ROTR(W,N) (rotRWord(W,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,2) ^ ROTR(X,13) ^ ROTR(X,22))
 #define BSIG1(X) (ROTR(X,6) ^ ROTR(X,11) ^ ROTR(X,25))
 #define SSIG0(X) (ROTR((X),7) ^ ROTR((X),18) ^ ((X) >> 3))
 #define SSIG1(X) (ROTR((X),17) ^ ROTR((X),19) ^ ((X) >> 10))
-#define R(A,B,C,D,E,F,G,H,T,K)  T1 = H + BSIG1(E) + CH(E,F,G) + K + (w[T] = __rev(buffer2[T])); \
+#define R(A,B,C,D,E,F,G,H,T,K)  T1 = H + BSIG1(E) + CH(E,F,G) + K + (w[T] = revWord(buffer2[T])); \
                               T2 = BSIG0(A) + MAJ(A,B,C); \
                               D += T1; \
                               H = T1 + T2;
@@ -34,6 +35,27 @@
     0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
 };
 
+static uint32_t revWord(const uint32_t w)
+{
+#ifdef __CC_ARM
+    return __rev(w);
+#else
+    return (w >> 24)
+         | ((w & 0x00FF0000) >> 8)
+         | ((w & 0x0000FF00) << 8)
+         | ((w & 0x000000FF) << 24);
+#endif
+} 
+
+static uint32_t rotRWord(const uint32_t w, const uint32_t n)
+{
+#ifdef __CC_ARM
+    return __ror(w, n);
+#else
+    return (w >> n) | (w << (32-n));
+#endif
+}
+
 SHA2_32::SHA2_32(SHA_32_TYPE t):
 type(t),
 totalBufferLength(0),
@@ -67,7 +89,7 @@
 
 void SHA2_32::update(uint8_t *data, uint32_t length)
 {
-    if(length < 64-bufferLength)
+    if((int)length < 64-bufferLength)
     {
         memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
@@ -83,7 +105,7 @@
         computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
         offset += 64;
     }
-    if(offset > length)
+    if(offset > (int)length)
         offset -= 64;
     bufferLength = length - offset;
     memcpy(buffer, &data[offset], bufferLength);
@@ -113,23 +135,23 @@
     uint64_t lengthBit = totalBufferLength << 3;
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
-    lengthBitLow = __rev(lengthBitLow);
-    lengthBitHigh = __rev(lengthBitHigh);
+    lengthBitLow = revWord(lengthBitLow);
+    lengthBitHigh = revWord(lengthBitHigh);
     memcpy(&buffer[60], &lengthBitLow, 4);    
     memcpy(&buffer[56], &lengthBitHigh, 4);    
     computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
 
-    hash2[0] = __rev(h0);
-    hash2[1] = __rev(h1);
-    hash2[2] = __rev(h2);
-    hash2[3] = __rev(h3);
-    hash2[4] = __rev(h4);
-    hash2[5] = __rev(h5);
-    hash2[6] = __rev(h6);
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
+    hash2[5] = revWord(h5);
+    hash2[6] = revWord(h6);
 
     
     if(type == SHA_256)
-        hash2[7] = __rev(h7);
+        hash2[7] = revWord(h7);
     
     // reset state
     switch(type)
@@ -202,26 +224,30 @@
     
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
-    lengthBitLow = __rev(lengthBitLow);
+    lengthBitLow = revWord(lengthBitLow);
     memcpy(&buffer[60], &lengthBitLow, 4);
-    lengthBitHigh = __rev(lengthBitHigh);
+    lengthBitHigh = revWord(lengthBitHigh);
     memcpy(&buffer[56], &lengthBitHigh, 4);    
     computeBlock(h, &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], buffer);
 
-    hash2[0] = __rev(h[0]);
-    hash2[1] = __rev(h[1]);
-    hash2[2] = __rev(h[2]);
-    hash2[3] = __rev(h[3]);
-    hash2[4] = __rev(h[4]);
-    hash2[5] = __rev(h[5]);
-    hash2[6] = __rev(h[6]);
+    hash2[0] = revWord(h[0]);
+    hash2[1] = revWord(h[1]);
+    hash2[2] = revWord(h[2]);
+    hash2[3] = revWord(h[3]);
+    hash2[4] = revWord(h[4]);
+    hash2[5] = revWord(h[5]);
+    hash2[6] = revWord(h[6]);
 
     
     if(type == SHA_256)
-        hash2[7] = __rev(h[7]);
+        hash2[7] = revWord(h[7]);
 }
 
-__forceinline void SHA2_32::computeBlock(uint32_t *h02, 
+
+#ifdef __CC_ARM
+__forceinline 
+#endif 
+void SHA2_32::computeBlock(uint32_t *h02, 
                         uint32_t *h12, 
                         uint32_t *h22, 
                         uint32_t *h32, 
@@ -318,4 +344,3 @@
     *h62 += g;
     *h72 += h;
 }
-