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:
Mon Sep 09 16:16:24 2013 +0000
Revision:
2:473bac39ae7c
Parent:
1:14a7cea431aa
Child:
3:85c6ee25cf3e
improved performance of MD2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:7a1237bd2d13 1 #include "MD2.h"
feb11 0:7a1237bd2d13 2 #include <string.h>
feb11 0:7a1237bd2d13 3
feb11 0:7a1237bd2d13 4 static const uint8_t s[] =
feb11 0:7a1237bd2d13 5 {
feb11 0:7a1237bd2d13 6 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13,
feb11 0:7a1237bd2d13 7 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA,
feb11 0:7a1237bd2d13 8 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12,
feb11 0:7a1237bd2d13 9 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A,
feb11 0:7a1237bd2d13 10 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
feb11 0:7a1237bd2d13 11 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03,
feb11 0:7a1237bd2d13 12 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6,
feb11 0:7a1237bd2d13 13 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1,
feb11 0:7a1237bd2d13 14 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02,
feb11 0:7a1237bd2d13 15 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
feb11 0:7a1237bd2d13 16 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26,
feb11 0:7a1237bd2d13 17 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52,
feb11 0:7a1237bd2d13 18 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A,
feb11 0:7a1237bd2d13 19 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39,
feb11 0:7a1237bd2d13 20 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
feb11 0:7a1237bd2d13 21 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
feb11 0:7a1237bd2d13 22 };
feb11 0:7a1237bd2d13 23
feb11 0:7a1237bd2d13 24
feb11 0:7a1237bd2d13 25 MD2::MD2():
feb11 0:7a1237bd2d13 26 HashAlgorithm(),
feb11 0:7a1237bd2d13 27 bufferLength(0),
feb11 0:7a1237bd2d13 28 l(0)
feb11 0:7a1237bd2d13 29 {
feb11 0:7a1237bd2d13 30 memset(checksum, 0, 16);
feb11 0:7a1237bd2d13 31 memset(x, 0, 48);
feb11 0:7a1237bd2d13 32 }
feb11 0:7a1237bd2d13 33
feb11 1:14a7cea431aa 34 void MD2::computeBlock(uint8_t *checksum2, uint8_t *x2, uint8_t *l2, uint8_t *buffer2)
feb11 0:7a1237bd2d13 35 {
feb11 0:7a1237bd2d13 36 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 37 {
feb11 1:14a7cea431aa 38 uint8_t c = buffer2[j];
feb11 2:473bac39ae7c 39 *l2 = (checksum2[j] ^= s[c^(*l2)]);
feb11 0:7a1237bd2d13 40 }
feb11 0:7a1237bd2d13 41
feb11 2:473bac39ae7c 42 uint32_t *x3 = (uint32_t*)x2;
feb11 2:473bac39ae7c 43 uint32_t *buffer3 = (uint32_t*)buffer2;
feb11 2:473bac39ae7c 44
feb11 2:473bac39ae7c 45 x3[4] = buffer3[0];
feb11 2:473bac39ae7c 46 x3[5] = buffer3[1];
feb11 2:473bac39ae7c 47 x3[6] = buffer3[2];
feb11 2:473bac39ae7c 48 x3[7] = buffer3[3];
feb11 2:473bac39ae7c 49 for(int j = 0; j < 4; ++j)
feb11 2:473bac39ae7c 50 x3[8+j] = x3[4+j] ^ x3[j];
feb11 2:473bac39ae7c 51
feb11 0:7a1237bd2d13 52 uint8_t t = 0;
feb11 0:7a1237bd2d13 53
feb11 0:7a1237bd2d13 54 for(int j = 0; j < 18; ++j)
feb11 0:7a1237bd2d13 55 {
feb11 0:7a1237bd2d13 56 for(int k = 0; k < 48; ++k)
feb11 0:7a1237bd2d13 57 {
feb11 2:473bac39ae7c 58 t = (x2[k] ^= s[t]);
feb11 0:7a1237bd2d13 59 }
feb11 0:7a1237bd2d13 60 t += j;
feb11 0:7a1237bd2d13 61 }
feb11 0:7a1237bd2d13 62 }
feb11 0:7a1237bd2d13 63
feb11 0:7a1237bd2d13 64 void MD2::add(uint8_t *in, uint32_t length)
feb11 0:7a1237bd2d13 65 {
feb11 0:7a1237bd2d13 66 if(length < 16-bufferLength)
feb11 0:7a1237bd2d13 67 {
feb11 0:7a1237bd2d13 68 memcpy(&buffer[bufferLength], in, length);
feb11 0:7a1237bd2d13 69 bufferLength += length;
feb11 0:7a1237bd2d13 70 return;
feb11 0:7a1237bd2d13 71 }
feb11 0:7a1237bd2d13 72 int offset = 16-bufferLength;
feb11 0:7a1237bd2d13 73 memcpy(&buffer[bufferLength], in, offset);
feb11 1:14a7cea431aa 74 computeBlock(checksum, x, &l, buffer);
feb11 0:7a1237bd2d13 75 while(length-offset > 16)
feb11 0:7a1237bd2d13 76 {
feb11 0:7a1237bd2d13 77 memcpy(buffer, &in[offset], 16);
feb11 1:14a7cea431aa 78 computeBlock(checksum, x, &l, buffer);
feb11 0:7a1237bd2d13 79 offset += 16;
feb11 0:7a1237bd2d13 80 }
feb11 0:7a1237bd2d13 81 if(offset > length)
feb11 0:7a1237bd2d13 82 offset -= 16;
feb11 0:7a1237bd2d13 83 bufferLength = length - offset;
feb11 0:7a1237bd2d13 84 memcpy(buffer, &in[offset], bufferLength);
feb11 0:7a1237bd2d13 85 }
feb11 0:7a1237bd2d13 86
feb11 0:7a1237bd2d13 87 void MD2::computeDigest(uint8_t *digest)
feb11 0:7a1237bd2d13 88 {
feb11 0:7a1237bd2d13 89 // compute what's left in the buffer
feb11 0:7a1237bd2d13 90 int padding = 16 - bufferLength;
feb11 0:7a1237bd2d13 91 memset(&buffer[bufferLength], padding, padding);
feb11 1:14a7cea431aa 92 computeBlock(checksum, x, &l, buffer);
feb11 2:473bac39ae7c 93
feb11 0:7a1237bd2d13 94 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 95 {
feb11 0:7a1237bd2d13 96 x[16+j] = checksum[j];
feb11 0:7a1237bd2d13 97 x[32+j] = x[16+j] ^ x[j];
feb11 0:7a1237bd2d13 98 }
feb11 0:7a1237bd2d13 99
feb11 0:7a1237bd2d13 100 uint8_t t = 0;
feb11 0:7a1237bd2d13 101
feb11 0:7a1237bd2d13 102 for(int j = 0; j < 18; ++j)
feb11 0:7a1237bd2d13 103 {
feb11 0:7a1237bd2d13 104 for(int k = 0; k < 48; ++k)
feb11 0:7a1237bd2d13 105 {
feb11 2:473bac39ae7c 106 t = (x[k] ^= s[t]);
feb11 0:7a1237bd2d13 107 }
feb11 0:7a1237bd2d13 108 t += j;
feb11 0:7a1237bd2d13 109 }
feb11 0:7a1237bd2d13 110
feb11 0:7a1237bd2d13 111 // reset state
feb11 0:7a1237bd2d13 112 bufferLength = 0;
feb11 0:7a1237bd2d13 113 l = 0;
feb11 0:7a1237bd2d13 114 memset(checksum, 0, 16);
feb11 0:7a1237bd2d13 115 memcpy(digest, x, 16);
feb11 0:7a1237bd2d13 116 memset(x,0,48);
feb11 0:7a1237bd2d13 117 }
feb11 0:7a1237bd2d13 118
feb11 0:7a1237bd2d13 119 uint8_t MD2::outputSize() const
feb11 0:7a1237bd2d13 120 {
feb11 0:7a1237bd2d13 121 return 16;
feb11 0:7a1237bd2d13 122 }
feb11 0:7a1237bd2d13 123
feb11 0:7a1237bd2d13 124 void MD2::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
feb11 0:7a1237bd2d13 125 {
feb11 2:473bac39ae7c 126 uint8_t data[80];
feb11 2:473bac39ae7c 127 memset(data, 0, 64);
feb11 2:473bac39ae7c 128 uint8_t *x = data;
feb11 2:473bac39ae7c 129 uint32_t *x2 = (uint32_t*)data;
feb11 2:473bac39ae7c 130 uint8_t *checksum = &data[48];
feb11 2:473bac39ae7c 131 uint32_t *checksum2 = (uint32_t*)&data[48];
feb11 2:473bac39ae7c 132 uint8_t *buffer = &data[64];
feb11 1:14a7cea431aa 133 uint8_t l = 0;
feb11 1:14a7cea431aa 134 uint32_t offset = 0;
feb11 1:14a7cea431aa 135 while(length - offset >= 16)
feb11 0:7a1237bd2d13 136 {
feb11 1:14a7cea431aa 137 computeBlock(checksum, x, &l, &in[offset]);
feb11 1:14a7cea431aa 138 offset += 16;
feb11 1:14a7cea431aa 139 }
feb11 1:14a7cea431aa 140
feb11 1:14a7cea431aa 141 uint8_t bufferLength = length - offset;
feb11 1:14a7cea431aa 142 memcpy(buffer, &in[offset], bufferLength);
feb11 2:473bac39ae7c 143 uint8_t padding = 16-bufferLength;
feb11 2:473bac39ae7c 144 memset(&buffer[bufferLength], padding, padding);
feb11 1:14a7cea431aa 145 computeBlock(checksum, x, &l, buffer);
feb11 1:14a7cea431aa 146
feb11 2:473bac39ae7c 147 for(int j = 0; j < 4; ++j)
feb11 1:14a7cea431aa 148 {
feb11 2:473bac39ae7c 149 x2[4+j] = checksum2[j];
feb11 2:473bac39ae7c 150 x2[8+j] = x2[4+j] ^ x2[j];
feb11 2:473bac39ae7c 151 }
feb11 1:14a7cea431aa 152
feb11 1:14a7cea431aa 153 uint8_t t = 0;
feb11 2:473bac39ae7c 154
feb11 1:14a7cea431aa 155 for(int j = 0; j < 18; ++j)
feb11 1:14a7cea431aa 156 {
feb11 1:14a7cea431aa 157 for(int k = 0; k < 48; ++k)
feb11 0:7a1237bd2d13 158 {
feb11 2:473bac39ae7c 159 t = (x[k] ^= s[t]);
feb11 0:7a1237bd2d13 160 }
feb11 1:14a7cea431aa 161 t += j;
feb11 0:7a1237bd2d13 162 }
feb11 1:14a7cea431aa 163
feb11 0:7a1237bd2d13 164 memcpy(digest, x, 16);
feb11 0:7a1237bd2d13 165 }