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
Committer:
feb11
Date:
Sun May 11 13:36:45 2014 +0000
Revision:
14:f04410cef037
Parent:
8:a090264e9b2d
CBC mode completed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 8:a090264e9b2d 1 #include "BlockCipher.h"
feb11 8:a090264e9b2d 2 #include <string.h>
feb11 8:a090264e9b2d 3
feb11 8:a090264e9b2d 4 BlockCipher::BlockCipher(uint32_t bs, BLOCK_CIPHER_MODE m, uint8_t *iv):
feb11 8:a090264e9b2d 5 Cipher(),
feb11 8:a090264e9b2d 6 blockSize(bs),
feb11 8:a090264e9b2d 7 mode(m),
feb11 8:a090264e9b2d 8 IV(0)
feb11 8:a090264e9b2d 9 {
feb11 8:a090264e9b2d 10 if(mode == CBC_MODE)
feb11 8:a090264e9b2d 11 {
feb11 8:a090264e9b2d 12 IV = new uint8_t[blockSize];
feb11 8:a090264e9b2d 13 memcpy(IV, iv, blockSize);
feb11 8:a090264e9b2d 14 }
feb11 8:a090264e9b2d 15 }
feb11 8:a090264e9b2d 16
feb11 8:a090264e9b2d 17 BlockCipher::~BlockCipher()
feb11 8:a090264e9b2d 18 {
feb11 8:a090264e9b2d 19 if(IV != 0)
feb11 8:a090264e9b2d 20 delete[] IV;
feb11 8:a090264e9b2d 21 }
feb11 8:a090264e9b2d 22
feb11 8:a090264e9b2d 23 CIPHER_TYPE BlockCipher::getType() const
feb11 8:a090264e9b2d 24 {
feb11 8:a090264e9b2d 25 return BLOCK_CIPHER;
feb11 8:a090264e9b2d 26 }
feb11 8:a090264e9b2d 27
feb11 8:a090264e9b2d 28 uint32_t BlockCipher::getBlockSize() const
feb11 8:a090264e9b2d 29 {
feb11 8:a090264e9b2d 30 return blockSize;
feb11 8:a090264e9b2d 31 }
feb11 8:a090264e9b2d 32
feb11 8:a090264e9b2d 33 void BlockCipher::encrypt(uint8_t *out, uint8_t *in, uint32_t length)
feb11 8:a090264e9b2d 34 {
feb11 14:f04410cef037 35 uint8_t *tmp = 0;
feb11 14:f04410cef037 36 if(mode == CBC_MODE)
feb11 14:f04410cef037 37 tmp = new uint8_t[getBlockSize()];
feb11 8:a090264e9b2d 38 for(uint32_t i = 0; i < length; i += getBlockSize())
feb11 8:a090264e9b2d 39 {
feb11 14:f04410cef037 40 if(mode == CBC_MODE)
feb11 14:f04410cef037 41 {
feb11 14:f04410cef037 42 memcpy(tmp, &in[i], getBlockSize());
feb11 14:f04410cef037 43 for(int j = 0; j < (int)getBlockSize(); ++j)
feb11 14:f04410cef037 44 tmp[j] ^= IV[j];
feb11 14:f04410cef037 45
feb11 14:f04410cef037 46 encryptBlock(&out[i], tmp);
feb11 8:a090264e9b2d 47
feb11 14:f04410cef037 48 memcpy(IV, &out[i], getBlockSize());
feb11 14:f04410cef037 49 }
feb11 14:f04410cef037 50 else
feb11 14:f04410cef037 51 encryptBlock(&out[i], &in[i]);
feb11 8:a090264e9b2d 52 }
feb11 14:f04410cef037 53 if(mode == CBC_MODE)
feb11 14:f04410cef037 54 delete[] tmp;
feb11 8:a090264e9b2d 55 }
feb11 8:a090264e9b2d 56
feb11 8:a090264e9b2d 57 void BlockCipher::decrypt(uint8_t *out, uint8_t *in, uint32_t length)
feb11 8:a090264e9b2d 58 {
feb11 8:a090264e9b2d 59 for(uint32_t i = 0; i < length; i += getBlockSize())
feb11 8:a090264e9b2d 60 {
feb11 14:f04410cef037 61 if(mode == CBC_MODE)
feb11 14:f04410cef037 62 {
feb11 14:f04410cef037 63 decryptBlock(&out[i], &in[i]);
feb11 14:f04410cef037 64 for(int j = 0; j < (int)getBlockSize(); ++j)
feb11 14:f04410cef037 65 out[i+j] ^= IV[j];
feb11 8:a090264e9b2d 66
feb11 14:f04410cef037 67 memcpy(IV, &in[i], getBlockSize());
feb11 14:f04410cef037 68 }
feb11 14:f04410cef037 69 else
feb11 14:f04410cef037 70 decryptBlock(&out[i], &in[i]);
feb11 8:a090264e9b2d 71 }
feb11 8:a090264e9b2d 72 }