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:
Sat Sep 07 23:47:28 2013 +0000
Revision:
0:7a1237bd2d13
Child:
1:14a7cea431aa
initial import

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 0:7a1237bd2d13 34 void MD2::computeBuffer()
feb11 0:7a1237bd2d13 35 {
feb11 0:7a1237bd2d13 36 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 37 {
feb11 0:7a1237bd2d13 38 uint8_t c = buffer[j];
feb11 0:7a1237bd2d13 39 checksum[j] ^= s[c^l];
feb11 0:7a1237bd2d13 40 l = checksum[j];
feb11 0:7a1237bd2d13 41 }
feb11 0:7a1237bd2d13 42
feb11 0:7a1237bd2d13 43
feb11 0:7a1237bd2d13 44 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 45 {
feb11 0:7a1237bd2d13 46 x[16+j] = buffer[j];
feb11 0:7a1237bd2d13 47 x[32+j] = x[16+j] ^ x[j];
feb11 0:7a1237bd2d13 48 }
feb11 0:7a1237bd2d13 49
feb11 0:7a1237bd2d13 50 uint8_t t = 0;
feb11 0:7a1237bd2d13 51
feb11 0:7a1237bd2d13 52 for(int j = 0; j < 18; ++j)
feb11 0:7a1237bd2d13 53 {
feb11 0:7a1237bd2d13 54 for(int k = 0; k < 48; ++k)
feb11 0:7a1237bd2d13 55 {
feb11 0:7a1237bd2d13 56 x[k] = x[k] ^ s[t];
feb11 0:7a1237bd2d13 57 t = x[k];
feb11 0:7a1237bd2d13 58 }
feb11 0:7a1237bd2d13 59 t += j;
feb11 0:7a1237bd2d13 60 }
feb11 0:7a1237bd2d13 61 }
feb11 0:7a1237bd2d13 62
feb11 0:7a1237bd2d13 63 void MD2::add(uint8_t *in, uint32_t length)
feb11 0:7a1237bd2d13 64 {
feb11 0:7a1237bd2d13 65 if(length < 16-bufferLength)
feb11 0:7a1237bd2d13 66 {
feb11 0:7a1237bd2d13 67 memcpy(&buffer[bufferLength], in, length);
feb11 0:7a1237bd2d13 68 bufferLength += length;
feb11 0:7a1237bd2d13 69 return;
feb11 0:7a1237bd2d13 70 }
feb11 0:7a1237bd2d13 71 int offset = 16-bufferLength;
feb11 0:7a1237bd2d13 72 memcpy(&buffer[bufferLength], in, offset);
feb11 0:7a1237bd2d13 73 computeBuffer();
feb11 0:7a1237bd2d13 74 while(length-offset > 16)
feb11 0:7a1237bd2d13 75 {
feb11 0:7a1237bd2d13 76 memcpy(buffer, &in[offset], 16);
feb11 0:7a1237bd2d13 77 computeBuffer();
feb11 0:7a1237bd2d13 78 offset += 16;
feb11 0:7a1237bd2d13 79 }
feb11 0:7a1237bd2d13 80 if(offset > length)
feb11 0:7a1237bd2d13 81 offset -= 16;
feb11 0:7a1237bd2d13 82 bufferLength = length - offset;
feb11 0:7a1237bd2d13 83 memcpy(buffer, &in[offset], bufferLength);
feb11 0:7a1237bd2d13 84 }
feb11 0:7a1237bd2d13 85
feb11 0:7a1237bd2d13 86 void MD2::computeDigest(uint8_t *digest)
feb11 0:7a1237bd2d13 87 {
feb11 0:7a1237bd2d13 88 // compute what's left in the buffer
feb11 0:7a1237bd2d13 89 int padding = 16 - bufferLength;
feb11 0:7a1237bd2d13 90 memset(&buffer[bufferLength], padding, padding);
feb11 0:7a1237bd2d13 91 computeBuffer();
feb11 0:7a1237bd2d13 92
feb11 0:7a1237bd2d13 93 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 94 {
feb11 0:7a1237bd2d13 95 x[16+j] = checksum[j];
feb11 0:7a1237bd2d13 96 x[32+j] = x[16+j] ^ x[j];
feb11 0:7a1237bd2d13 97 }
feb11 0:7a1237bd2d13 98
feb11 0:7a1237bd2d13 99 uint8_t t = 0;
feb11 0:7a1237bd2d13 100
feb11 0:7a1237bd2d13 101 for(int j = 0; j < 18; ++j)
feb11 0:7a1237bd2d13 102 {
feb11 0:7a1237bd2d13 103 for(int k = 0; k < 48; ++k)
feb11 0:7a1237bd2d13 104 {
feb11 0:7a1237bd2d13 105 x[k] = x[k] ^ s[t];
feb11 0:7a1237bd2d13 106 t = x[k];
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 0:7a1237bd2d13 126 uint8_t padding = 16 - (length % 16);
feb11 0:7a1237bd2d13 127 int totalLength = length + 16 + padding;
feb11 0:7a1237bd2d13 128 uint8_t *buffer = new uint8_t[totalLength];
feb11 0:7a1237bd2d13 129 memcpy(buffer, in, length);
feb11 0:7a1237bd2d13 130 memset(&buffer[length], padding, padding);
feb11 0:7a1237bd2d13 131 uint8_t checksum[16];
feb11 0:7a1237bd2d13 132 memset(checksum, 0, 16);
feb11 0:7a1237bd2d13 133 uint8_t l = 0;
feb11 0:7a1237bd2d13 134
feb11 0:7a1237bd2d13 135 for(int i = 0; i < ((totalLength-16)/16); ++i)
feb11 0:7a1237bd2d13 136 {
feb11 0:7a1237bd2d13 137 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 138 {
feb11 0:7a1237bd2d13 139 uint8_t c = buffer[i*16+j];
feb11 0:7a1237bd2d13 140 checksum[j] ^= s[c^l];
feb11 0:7a1237bd2d13 141 l = checksum[j];
feb11 0:7a1237bd2d13 142 }
feb11 0:7a1237bd2d13 143 }
feb11 0:7a1237bd2d13 144 memcpy(&buffer[totalLength-16], checksum, 16);
feb11 0:7a1237bd2d13 145
feb11 0:7a1237bd2d13 146 uint8_t x[48];
feb11 0:7a1237bd2d13 147 memset(x,0,48);
feb11 0:7a1237bd2d13 148
feb11 0:7a1237bd2d13 149 for(int i = 0; i < (totalLength/16); ++i)
feb11 0:7a1237bd2d13 150 {
feb11 0:7a1237bd2d13 151 for(int j = 0; j < 16; ++j)
feb11 0:7a1237bd2d13 152 {
feb11 0:7a1237bd2d13 153 x[16+j] = buffer[i*16+j];
feb11 0:7a1237bd2d13 154 x[32+j] = x[16+j] ^ x[j];
feb11 0:7a1237bd2d13 155 }
feb11 0:7a1237bd2d13 156
feb11 0:7a1237bd2d13 157 uint8_t t = 0;
feb11 0:7a1237bd2d13 158
feb11 0:7a1237bd2d13 159 for(int j = 0; j < 18; ++j)
feb11 0:7a1237bd2d13 160 {
feb11 0:7a1237bd2d13 161 for(int k = 0; k < 48; ++k)
feb11 0:7a1237bd2d13 162 {
feb11 0:7a1237bd2d13 163 t = x[k] = x[k] ^ s[t];
feb11 0:7a1237bd2d13 164 }
feb11 0:7a1237bd2d13 165 t += j;
feb11 0:7a1237bd2d13 166 }
feb11 0:7a1237bd2d13 167 }
feb11 0:7a1237bd2d13 168 delete[] buffer;
feb11 0:7a1237bd2d13 169 memcpy(digest, x, 16);
feb11 0:7a1237bd2d13 170 }