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:
Thu Sep 12 15:08:51 2013 +0000
Parent:
4:0da19393bd57
Child:
6:19aa835f2bbb
Commit message:
change API & small improvements in SHA-2

Changed in this revision

DES.cpp Show annotated file Show diff for this revision Revisions of this file
DES.h Show annotated file Show diff for this revision Revisions of this file
HashAlgorithm.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
MD5.h 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
SHA1.h Show annotated file Show diff for this revision Revisions of this file
SHA224.cpp Show annotated file Show diff for this revision Revisions of this file
SHA224.h Show annotated file Show diff for this revision Revisions of this file
SHA256.cpp Show annotated file Show diff for this revision Revisions of this file
SHA256.h Show annotated file Show diff for this revision Revisions of this file
SHA2_32.cpp Show annotated file Show diff for this revision Revisions of this file
SHA2_32.h Show annotated file Show diff for this revision Revisions of this file
SHA2_64.cpp Show annotated file Show diff for this revision Revisions of this file
SHA2_64.h Show annotated file Show diff for this revision Revisions of this file
SHA384.cpp Show annotated file Show diff for this revision Revisions of this file
SHA384.h Show annotated file Show diff for this revision Revisions of this file
SHA512.cpp Show annotated file Show diff for this revision Revisions of this file
SHA512.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DES.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -0,0 +1,28 @@
+#include "DES.h"
+
+
+DES::DES(uint8_t *key):
+Cipher()
+{
+//    loadKey(key);
+}
+
+void DES::encrypt(uint8_t *out, uint8_t *in, uint32_t length)
+{
+
+}
+     
+void DES::decrypt(uint8_t *out, uint8_t *in, uint32_t length)
+{
+
+}
+
+uint32_t DES::getBlockSize() const
+{
+    return 8;
+}
+
+CIPHER_TYPE DES::getType() const
+{
+    return STREAM_CIPHER;
+}
--- a/DES.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/DES.h	Thu Sep 12 15:08:51 2013 +0000
@@ -0,0 +1,24 @@
+#ifndef DES_H
+#define DES_H
+
+#include "Cipher.h"
+
+
+class DES : public Cipher
+{
+    public :
+    
+        DES(uint8_t* key);
+        
+        virtual void encrypt(uint8_t *out, uint8_t *in, uint32_t length);        
+        virtual void decrypt(uint8_t *out, uint8_t *in, uint32_t length);        
+        virtual uint32_t getBlockSize() const;
+
+        CIPHER_TYPE getType() const;
+        
+    private :
+    
+        uint8_t keys[16][7];
+};
+
+#endif
--- a/HashAlgorithm.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/HashAlgorithm.h	Thu Sep 12 15:08:51 2013 +0000
@@ -8,8 +8,8 @@
     public :
     
         virtual ~HashAlgorithm();
-        virtual void add(uint8_t *in, uint32_t length) = 0;
-        virtual void computeDigest(uint8_t *out) = 0;
+        virtual void update(uint8_t *in, uint32_t length) = 0;
+        virtual void finalize(uint8_t *out) = 0;
         virtual uint8_t outputSize() const = 0;
 };
 
--- a/MD2.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/MD2.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -114,7 +114,7 @@
     }
 }
 
-void MD2::add(uint8_t *in, uint32_t length)
+void MD2::update(uint8_t *in, uint32_t length)
 { 
     if(bufferLength == 0)
     {
@@ -151,7 +151,7 @@
     
 }
 
-void MD2::computeDigest(uint8_t *digest)
+void MD2::finalize(uint8_t *digest)
 {
     // compute what's left in the buffer
     int padding = 16 - bufferLength;
--- a/MD2.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/MD2.h	Thu Sep 12 15:08:51 2013 +0000
@@ -9,8 +9,8 @@
     
         MD2();
         
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
         
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
--- a/MD5.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/MD5.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -50,7 +50,7 @@
     return 16;
 }
 
-void MD5::add(uint8_t *in, uint32_t length)
+void MD5::update(uint8_t *in, uint32_t length)
 {
     if(length < 64-bufferLength)
     {
@@ -75,7 +75,7 @@
     totalBufferLength += length;
 }
 
-void MD5::computeDigest(uint8_t *digest)
+void MD5::finalize(uint8_t *digest)
 {
     uint16_t padding;
     if(totalBufferLength % 64 < 56)
@@ -90,7 +90,7 @@
     {
         memset(&buffer[bufferLength], 0, 64-bufferLength);
         computeRounds(&a, &b, &c, &d, buffer);
-        memset(buffer, 0, bufferLength);
+        memset(buffer, 0, 56);
     }
     uint64_t lengthBit = totalBufferLength << 3;
     uint32_t lengthBitLow = lengthBit;
@@ -179,7 +179,7 @@
     {
         memset(&buffer[length], 0, 64-length);
         computeRounds(&a, &b, &c, &d, msg);
-        memset(buffer, 0, length);
+        memset(buffer, 0, 56);
     }
 
     uint32_t lengthBitLow = lengthBit;
--- a/MD5.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/MD5.h	Thu Sep 12 15:08:51 2013 +0000
@@ -10,8 +10,8 @@
     
         MD5();
         
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
         
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
--- a/SHA1.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA1.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -59,7 +59,7 @@
     return 20;
 }
 
-void SHA1::add(uint8_t *in, uint32_t length)
+void SHA1::update(uint8_t *in, uint32_t length)
 {
     if(length < 64-bufferLength)
     {
@@ -84,7 +84,7 @@
     totalBufferLength += length;
 }
 
-void SHA1::computeDigest(uint8_t *digest)
+void SHA1::finalize(uint8_t *digest)
 {
     uint32_t *digest2 = (uint32_t*)digest;
     uint16_t padding;
@@ -101,7 +101,7 @@
     {
         memset(&buffer[bufferLength], 0, 64-bufferLength);
         computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
-        memset(buffer, 0, 48);
+        memset(buffer, 0, 56);
     }
     
     uint64_t lengthBit = totalBufferLength << 3;
@@ -201,7 +201,7 @@
     {
         memset(&buffer[length], 0, 64-length);
         computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
-        memset(buffer, 0, length);
+        memset(buffer, 0, 56);
     }
 
     uint32_t lengthBitLow = lengthBit;
--- a/SHA1.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA1.h	Thu Sep 12 15:08:51 2013 +0000
@@ -10,8 +10,8 @@
     
         SHA1();
         
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
         
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
--- a/SHA224.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA224.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -7,14 +7,14 @@
 {
 }
 
-void SHA224::add(uint8_t *in, uint32_t length)
+void SHA224::update(uint8_t *in, uint32_t length)
 {
-    algo.add(in, length);
+    algo.update(in, length);
 }
 
-void SHA224::computeDigest(uint8_t *out)
+void SHA224::finalize(uint8_t *out)
 {
-    algo.computeDigest(out);
+    algo.finalize(out);
 }
 
 uint8_t SHA224::outputSize() const
--- a/SHA224.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA224.h	Thu Sep 12 15:08:51 2013 +0000
@@ -9,8 +9,8 @@
     public :
 
         SHA224();
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
 
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
--- a/SHA256.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA256.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -7,14 +7,14 @@
 {
 }
 
-void SHA256::add(uint8_t *in, uint32_t length)
+void SHA256::update(uint8_t *in, uint32_t length)
 {
-    algo.add(in, length);
+    algo.update(in, length);
 }
 
-void SHA256::computeDigest(uint8_t *out)
+void SHA256::finalize(uint8_t *out)
 {
-    algo.computeDigest(out);
+    algo.finalize(out);
 }
 
 uint8_t SHA256::outputSize() const
--- a/SHA256.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA256.h	Thu Sep 12 15:08:51 2013 +0000
@@ -10,8 +10,8 @@
     public :
 
         SHA256();
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
 
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
--- a/SHA2_32.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA2_32.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -66,7 +66,7 @@
     }
 }
 
-void SHA2_32::add(uint8_t *in, uint32_t length)
+void SHA2_32::update(uint8_t *in, uint32_t length)
 {
     if(length < 64-bufferLength)
     {
@@ -91,7 +91,7 @@
     totalBufferLength += length;
 }
 
-void SHA2_32::computeDigest(uint8_t *digest)
+void SHA2_32::finalize(uint8_t *digest)
 {
     uint16_t padding;
     if(totalBufferLength % 64 < 56)
@@ -107,7 +107,7 @@
     {
         memset(&buffer[bufferLength], 0, 64-bufferLength);
         computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-        memset(buffer, 0, 48);
+        memset(buffer, 0, 56);
     }
     
     uint64_t lengthBit = totalBufferLength << 3;
@@ -295,7 +295,7 @@
     {
         memset(&buffer[length], 0, 64-length);
         computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-        memset(buffer, 0, 48);
+        memset(buffer, 0, 56);
     }
     
     uint32_t lengthBitLow = lengthBit;
--- a/SHA2_32.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA2_32.h	Thu Sep 12 15:08:51 2013 +0000
@@ -14,8 +14,8 @@
     public :
     
         SHA2_32(SHA_32_TYPE type);
-        void add(uint8_t *in, uint32_t length);
-        void computeDigest(uint8_t *digest);
+        void update(uint8_t *in, uint32_t length);
+        void finalize(uint8_t *digest);
         static void computeDigest(SHA_32_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length);
         
     private : 
--- a/SHA2_64.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA2_64.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -15,8 +15,8 @@
 
 static uint64_t revWord(uint64_t w)
 {
-    return __rev((w & 0xFFFFFFFF00000000) >> 32) 
-         | ((uint64_t)(__rev(w & 0x00000000FFFFFFFF)) << 32);
+    return __rev(w >> 32) 
+         | ((uint64_t)(__rev(w)) << 32);
 }
 
 #define ROTL(W,N) (((W) << (N)) | ((W) >> (64-(N))))
@@ -65,7 +65,7 @@
     }
 }
 
-void SHA2_64::add(uint8_t *in, uint32_t length)
+void SHA2_64::update(uint8_t *in, uint32_t length)
 {
     if(length < 128-bufferLength)
     {
@@ -90,24 +90,31 @@
     totalBufferLength += length;
 }
 
-void SHA2_64::computeDigest(uint8_t *digest)
+void SHA2_64::finalize(uint8_t *digest)
 {
-    uint16_t padding;
+    uint64_t lengthBit = totalBufferLength << 3;
+    uint32_t padding;
     if(totalBufferLength % 128 < 112)
         padding = 112 - (totalBufferLength % 128);
     else
         padding = 112 + (128 - (totalBufferLength % 128));
-    uint8_t val = 0x80;
-    add(&val, 1);
-    val = 0;
-    for(int i = 0; i < padding-1; ++i)
-        add(&val,1);
-    totalBufferLength -= padding;
-    uint64_t lengthBit = 0;
-    add((uint8_t*)&lengthBit, 8);
-    lengthBit = (totalBufferLength - 8) * 8;
+
+    buffer[bufferLength++] = 0x80;
+    padding--;
+    if(padding+bufferLength == 112)
+        memset(&buffer[bufferLength], 0, padding);
+    else
+    {
+        memset(&buffer[bufferLength], 0, 64-bufferLength);
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+        memset(buffer, 0, 112);
+    }
+    
     lengthBit = revWord(lengthBit);
-    add((uint8_t*)&lengthBit, 8);
+    memcpy(&buffer[120], &lengthBit, 8);    
+    memset(&buffer[112], 0, 8);    
+    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+
 
     h0 = revWord(h0);
     h1 = revWord(h1);
@@ -330,7 +337,7 @@
     {
         memset(&buffer[length], 0, 128-length);
         computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-        memset(buffer, 0, length);
+        memset(buffer, 0, 112);
     }
     
     lengthBit = revWord(lengthBit);
--- a/SHA2_64.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA2_64.h	Thu Sep 12 15:08:51 2013 +0000
@@ -14,8 +14,8 @@
     public :
 
         SHA2_64(SHA2_64_TYPE type);
-        void add(uint8_t *in, uint32_t length);
-        void computeDigest(uint8_t *digest);
+        void update(uint8_t *in, uint32_t length);
+        void finalize(uint8_t *digest);
         static void computeDigest(SHA2_64_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length);
 
     private :
--- a/SHA384.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA384.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -7,14 +7,14 @@
 {
 }
 
-void SHA384::add(uint8_t *in, uint32_t length)
+void SHA384::update(uint8_t *in, uint32_t length)
 {
-    algo.add(in, length);
+    algo.update(in, length);
 }
 
-void SHA384::computeDigest(uint8_t *out)
+void SHA384::finalize(uint8_t *out)
 {
-    algo.computeDigest(out);
+    algo.finalize(out);
 }
 
 uint8_t SHA384::outputSize() const
--- a/SHA384.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA384.h	Thu Sep 12 15:08:51 2013 +0000
@@ -10,8 +10,8 @@
     public :
 
         SHA384();
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
 
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
--- a/SHA512.cpp	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA512.cpp	Thu Sep 12 15:08:51 2013 +0000
@@ -7,14 +7,14 @@
 {
 }
 
-void SHA512::add(uint8_t *in, uint32_t length)
+void SHA512::update(uint8_t *in, uint32_t length)
 {
-    algo.add(in, length);
+    algo.update(in, length);
 }
 
-void SHA512::computeDigest(uint8_t *out)
+void SHA512::finalize(uint8_t *out)
 {
-    algo.computeDigest(out);
+    algo.finalize(out);
 }
 
 uint8_t SHA512::outputSize() const
--- a/SHA512.h	Thu Sep 12 10:18:57 2013 +0000
+++ b/SHA512.h	Thu Sep 12 15:08:51 2013 +0000
@@ -10,8 +10,8 @@
     public :
 
         SHA512();
-        virtual void add(uint8_t *in, uint32_t length);
-        virtual void computeDigest(uint8_t *out);
+        virtual void update(uint8_t *in, uint32_t length);
+        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
 
         static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);