Initial import of crypto libs from SecureMicroMesh

- Imported files from secure micro mesh library
- added Makefiles and included libs into sys/Makefile
dev/timer
Hauke Petersen 9 years ago
parent 881593b981
commit 05419a5547

@ -76,8 +76,23 @@ endif
ifneq (,$(findstring bloom,$(USEMODULE)))
DIRS += bloom
endif
ifneq (,$(findstring crypto,$(USEMODULE)))
DIRS += crypto
ifneq (,$(findstring crypto_3des,$(USEMODULE)))
DIRS += crypto/3des
endif
ifneq (,$(findstring crypto_aes,$(USEMODULE)))
DIRS += crypto/aes
endif
ifneq (,$(findstring crypto_rc5,$(USEMODULE)))
DIRS += crypto/rc5
endif
ifneq (,$(findstring crypto_sha256,$(USEMODULE)))
DIRS += crypto/sha256
endif
ifneq (,$(findstring crypto_skipjack,$(USEMODULE)))
DIRS += crypto/skipjack
endif
ifneq (,$(findstring crypto_twofish,$(USEMODULE)))
DIRS += crypto/twofish
endif
ifneq (,$(findstring random,$(USEMODULE)))
DIRS += random

@ -0,0 +1,533 @@
/*
* Copyright (C) 2013 Freie Universität Berlin, Computer Systems & Telematics
*
* This source code is licensed under the LGPLv2 license,
* See the file LICENSE for more details.
*/
/**
* @ingroup sys_crypto
* @{
*
* @file 3des.c
* @brief implementation of the 3DES cipher-algorithm
*
* @author Freie Universitaet Berlin, Computer Systems & Telematics
* @author Nicolai Schmittberger <nicolai.schmittberger@fu-berlin.de>
* @author Tom St Denis <tomstdenis@gmail.com>, http://libtomcrypt.com
* @author Dobes Vandermeer
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
*
* @date 18.09.2013 14:32:33
*
* @note This implementation is based on a DES implementation included
* in the LibTomCrypt modular cryptographic library.
* The LibTomCrypt library provides various cryptographic
* algorithms in a highly modular and flexible manner.
* The library is free for all purposes without any express
* guarantee it works.
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
* DES code submitted by Dobes Vandermeer
* @}
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "assert.h"
#include "crypto/3des.h"
#include "crypto/ciphers.h"
/*************** GLOBALS ******************/
/**
* @brief Interface to the 3DES cipher
*/
block_cipher_interface_t tripledes_interface = {
"3DES\0",
tripledes_init,
tripledes_encrypt,
tripledes_decrypt,
tripledes_setup_key,
tripledes_get_preferred_block_size
};
/**
* @brief struct for the 3DES key expansion
*/
struct des3_key_s {
uint32_t ek[3][32]; ///< encryption key
uint32_t dk[3][32]; ///< decryption key
} des3_key_s;
/************** PROTOTYPES ***************/
static void cookey(const uint32_t *raw1, uint32_t *keyout);
static void deskey(const uint8_t *key, int decrypt, uint32_t *keyout);
static void desfunc(uint32_t *block, const uint32_t *keys);
static uint8_t des3_key_setup(const uint8_t *key, struct des3_key_s *dkey);
/*****************************************/
/* Use the key schedule specific in the standard (ANSI X3.92-1981) */
static const uint8_t pc1[56] = {
56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3
};
static const uint8_t totrot[16] = {
1, 2, 4, 6,
8, 10, 12, 14,
15, 17, 19, 21,
23, 25, 27, 28
};
static const uint8_t pc2[48] = {
13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31
};
static const uint32_t SP1[64] = {
0x01010400UL, 0x00000000UL, 0x00010000UL, 0x01010404UL,
0x01010004UL, 0x00010404UL, 0x00000004UL, 0x00010000UL,
0x00000400UL, 0x01010400UL, 0x01010404UL, 0x00000400UL,
0x01000404UL, 0x01010004UL, 0x01000000UL, 0x00000004UL,
0x00000404UL, 0x01000400UL, 0x01000400UL, 0x00010400UL,
0x00010400UL, 0x01010000UL, 0x01010000UL, 0x01000404UL,
0x00010004UL, 0x01000004UL, 0x01000004UL, 0x00010004UL,
0x00000000UL, 0x00000404UL, 0x00010404UL, 0x01000000UL,
0x00010000UL, 0x01010404UL, 0x00000004UL, 0x01010000UL,
0x01010400UL, 0x01000000UL, 0x01000000UL, 0x00000400UL,
0x01010004UL, 0x00010000UL, 0x00010400UL, 0x01000004UL,
0x00000400UL, 0x00000004UL, 0x01000404UL, 0x00010404UL,
0x01010404UL, 0x00010004UL, 0x01010000UL, 0x01000404UL,
0x01000004UL, 0x00000404UL, 0x00010404UL, 0x01010400UL,
0x00000404UL, 0x01000400UL, 0x01000400UL, 0x00000000UL,
0x00010004UL, 0x00010400UL, 0x00000000UL, 0x01010004UL
};
static const uint32_t SP2[64] = {
0x80108020UL, 0x80008000UL, 0x00008000UL, 0x00108020UL,
0x00100000UL, 0x00000020UL, 0x80100020UL, 0x80008020UL,
0x80000020UL, 0x80108020UL, 0x80108000UL, 0x80000000UL,
0x80008000UL, 0x00100000UL, 0x00000020UL, 0x80100020UL,
0x00108000UL, 0x00100020UL, 0x80008020UL, 0x00000000UL,
0x80000000UL, 0x00008000UL, 0x00108020UL, 0x80100000UL,
0x00100020UL, 0x80000020UL, 0x00000000UL, 0x00108000UL,
0x00008020UL, 0x80108000UL, 0x80100000UL, 0x00008020UL,
0x00000000UL, 0x00108020UL, 0x80100020UL, 0x00100000UL,
0x80008020UL, 0x80100000UL, 0x80108000UL, 0x00008000UL,
0x80100000UL, 0x80008000UL, 0x00000020UL, 0x80108020UL,
0x00108020UL, 0x00000020UL, 0x00008000UL, 0x80000000UL,
0x00008020UL, 0x80108000UL, 0x00100000UL, 0x80000020UL,
0x00100020UL, 0x80008020UL, 0x80000020UL, 0x00100020UL,
0x00108000UL, 0x00000000UL, 0x80008000UL, 0x00008020UL,
0x80000000UL, 0x80100020UL, 0x80108020UL, 0x00108000UL
};
static const uint32_t SP3[64] = {
0x00000208UL, 0x08020200UL, 0x00000000UL, 0x08020008UL,
0x08000200UL, 0x00000000UL, 0x00020208UL, 0x08000200UL,
0x00020008UL, 0x08000008UL, 0x08000008UL, 0x00020000UL,
0x08020208UL, 0x00020008UL, 0x08020000UL, 0x00000208UL,
0x08000000UL, 0x00000008UL, 0x08020200UL, 0x00000200UL,
0x00020200UL, 0x08020000UL, 0x08020008UL, 0x00020208UL,
0x08000208UL, 0x00020200UL, 0x00020000UL, 0x08000208UL,
0x00000008UL, 0x08020208UL, 0x00000200UL, 0x08000000UL,
0x08020200UL, 0x08000000UL, 0x00020008UL, 0x00000208UL,
0x00020000UL, 0x08020200UL, 0x08000200UL, 0x00000000UL,
0x00000200UL, 0x00020008UL, 0x08020208UL, 0x08000200UL,
0x08000008UL, 0x00000200UL, 0x00000000UL, 0x08020008UL,
0x08000208UL, 0x00020000UL, 0x08000000UL, 0x08020208UL,
0x00000008UL, 0x00020208UL, 0x00020200UL, 0x08000008UL,
0x08020000UL, 0x08000208UL, 0x00000208UL, 0x08020000UL,
0x00020208UL, 0x00000008UL, 0x08020008UL, 0x00020200UL
};
static const uint32_t SP4[64] = {
0x00802001UL, 0x00002081UL, 0x00002081UL, 0x00000080UL,
0x00802080UL, 0x00800081UL, 0x00800001UL, 0x00002001UL,
0x00000000UL, 0x00802000UL, 0x00802000UL, 0x00802081UL,
0x00000081UL, 0x00000000UL, 0x00800080UL, 0x00800001UL,
0x00000001UL, 0x00002000UL, 0x00800000UL, 0x00802001UL,
0x00000080UL, 0x00800000UL, 0x00002001UL, 0x00002080UL,
0x00800081UL, 0x00000001UL, 0x00002080UL, 0x00800080UL,
0x00002000UL, 0x00802080UL, 0x00802081UL, 0x00000081UL,
0x00800080UL, 0x00800001UL, 0x00802000UL, 0x00802081UL,
0x00000081UL, 0x00000000UL, 0x00000000UL, 0x00802000UL,
0x00002080UL, 0x00800080UL, 0x00800081UL, 0x00000001UL,
0x00802001UL, 0x00002081UL, 0x00002081UL, 0x00000080UL,
0x00802081UL, 0x00000081UL, 0x00000001UL, 0x00002000UL,
0x00800001UL, 0x00002001UL, 0x00802080UL, 0x00800081UL,
0x00002001UL, 0x00002080UL, 0x00800000UL, 0x00802001UL,
0x00000080UL, 0x00800000UL, 0x00002000UL, 0x00802080UL
};
static const uint32_t SP5[64] = {
0x00000100UL, 0x02080100UL, 0x02080000UL, 0x42000100UL,
0x00080000UL, 0x00000100UL, 0x40000000UL, 0x02080000UL,
0x40080100UL, 0x00080000UL, 0x02000100UL, 0x40080100UL,
0x42000100UL, 0x42080000UL, 0x00080100UL, 0x40000000UL,
0x02000000UL, 0x40080000UL, 0x40080000UL, 0x00000000UL,
0x40000100UL, 0x42080100UL, 0x42080100UL, 0x02000100UL,
0x42080000UL, 0x40000100UL, 0x00000000UL, 0x42000000UL,
0x02080100UL, 0x02000000UL, 0x42000000UL, 0x00080100UL,
0x00080000UL, 0x42000100UL, 0x00000100UL, 0x02000000UL,
0x40000000UL, 0x02080000UL, 0x42000100UL, 0x40080100UL,
0x02000100UL, 0x40000000UL, 0x42080000UL, 0x02080100UL,
0x40080100UL, 0x00000100UL, 0x02000000UL, 0x42080000UL,
0x42080100UL, 0x00080100UL, 0x42000000UL, 0x42080100UL,
0x02080000UL, 0x00000000UL, 0x40080000UL, 0x42000000UL,
0x00080100UL, 0x02000100UL, 0x40000100UL, 0x00080000UL,
0x00000000UL, 0x40080000UL, 0x02080100UL, 0x40000100UL
};
static const uint32_t SP6[64] = {
0x20000010UL, 0x20400000UL, 0x00004000UL, 0x20404010UL,
0x20400000UL, 0x00000010UL, 0x20404010UL, 0x00400000UL,
0x20004000UL, 0x00404010UL, 0x00400000UL, 0x20000010UL,
0x00400010UL, 0x20004000UL, 0x20000000UL, 0x00004010UL,
0x00000000UL, 0x00400010UL, 0x20004010UL, 0x00004000UL,
0x00404000UL, 0x20004010UL, 0x00000010UL, 0x20400010UL,
0x20400010UL, 0x00000000UL, 0x00404010UL, 0x20404000UL,
0x00004010UL, 0x00404000UL, 0x20404000UL, 0x20000000UL,
0x20004000UL, 0x00000010UL, 0x20400010UL, 0x00404000UL,
0x20404010UL, 0x00400000UL, 0x00004010UL, 0x20000010UL,
0x00400000UL, 0x20004000UL, 0x20000000UL, 0x00004010UL,
0x20000010UL, 0x20404010UL, 0x00404000UL, 0x20400000UL,
0x00404010UL, 0x20404000UL, 0x00000000UL, 0x20400010UL,
0x00000010UL, 0x00004000UL, 0x20400000UL, 0x00404010UL,
0x00004000UL, 0x00400010UL, 0x20004010UL, 0x00000000UL,
0x20404000UL, 0x20000000UL, 0x00400010UL, 0x20004010UL
};
static const uint32_t SP7[64] = {
0x00200000UL, 0x04200002UL, 0x04000802UL, 0x00000000UL,
0x00000800UL, 0x04000802UL, 0x00200802UL, 0x04200800UL,
0x04200802UL, 0x00200000UL, 0x00000000UL, 0x04000002UL,
0x00000002UL, 0x04000000UL, 0x04200002UL, 0x00000802UL,
0x04000800UL, 0x00200802UL, 0x00200002UL, 0x04000800UL,
0x04000002UL, 0x04200000UL, 0x04200800UL, 0x00200002UL,
0x04200000UL, 0x00000800UL, 0x00000802UL, 0x04200802UL,
0x00200800UL, 0x00000002UL, 0x04000000UL, 0x00200800UL,
0x04000000UL, 0x00200800UL, 0x00200000UL, 0x04000802UL,
0x04000802UL, 0x04200002UL, 0x04200002UL, 0x00000002UL,
0x00200002UL, 0x04000000UL, 0x04000800UL, 0x00200000UL,
0x04200800UL, 0x00000802UL, 0x00200802UL, 0x04200800UL,
0x00000802UL, 0x04000002UL, 0x04200802UL, 0x04200000UL,
0x00200800UL, 0x00000000UL, 0x00000002UL, 0x04200802UL,
0x00000000UL, 0x00200802UL, 0x04200000UL, 0x00000800UL,
0x04000002UL, 0x04000800UL, 0x00000800UL, 0x00200002UL
};
static const uint32_t SP8[64] = {
0x10001040UL, 0x00001000UL, 0x00040000UL, 0x10041040UL,
0x10000000UL, 0x10001040UL, 0x00000040UL, 0x10000000UL,
0x00040040UL, 0x10040000UL, 0x10041040UL, 0x00041000UL,
0x10041000UL, 0x00041040UL, 0x00001000UL, 0x00000040UL,
0x10040000UL, 0x10000040UL, 0x10001000UL, 0x00001040UL,
0x00041000UL, 0x00040040UL, 0x10040040UL, 0x10041000UL,
0x00001040UL, 0x00000000UL, 0x00000000UL, 0x10040040UL,
0x10000040UL, 0x10001000UL, 0x00041040UL, 0x00040000UL,
0x00041040UL, 0x00040000UL, 0x10041000UL, 0x00001000UL,
0x00000040UL, 0x10040040UL, 0x00001000UL, 0x00041040UL,
0x10001000UL, 0x00000040UL, 0x10000040UL, 0x10040000UL,
0x10040040UL, 0x10000000UL, 0x00040000UL, 0x10001040UL,
0x00000000UL, 0x10041040UL, 0x00040040UL, 0x10000040UL,
0x10040000UL, 0x10001000UL, 0x10001040UL, 0x00000000UL,
0x10041040UL, 0x00041000UL, 0x00041000UL, 0x00001040UL,
0x00001040UL, 0x00040040UL, 0x10000000UL, 0x10041000UL
};
int tripledes_init(cipher_context_t *context, uint8_t blockSize, uint8_t keySize,
uint8_t *key)
{
uint8_t i;
//printf("%-40s: Entry\r\n", __FUNCTION__);
// 16 byte blocks only
if (blockSize != THREEDES_BLOCK_SIZE) {
printf("%-40s: blockSize != 3DES_BLOCK_SIZE...\r\n", __FUNCTION__);
return 0;
}
//key must be at least 24 Bytes long
if (keySize < 24) {
//fill up by concatenating key to as long as needed
for (i = 0; i < 24; i++) {
context->context[i] = key[(i % keySize)];
}
}
else {
for (i = 0; i < 24; i++) {
context->context[i] = key[i];
}
}
return 1;
}
int tripledes_setup_key(cipher_context_t *context, uint8_t *key,
uint8_t keysize) //To change !!!
{
return tripledes_init(context, tripledes_get_preferred_block_size(),
keysize, key);
}
int tripledes_encrypt(cipher_context_t *context, uint8_t *plain, uint8_t *crypt)
{
int res;
struct des3_key_s *key = malloc(sizeof(des3_key_s));
uint32_t work[2];
if (!key) {
printf("%-40s: [ERROR] Could NOT malloc space for the des3_key_s \
struct.\r\n", __FUNCTION__);
return -1;
}
memset(key, 0, sizeof(des3_key_s));
res = des3_key_setup(context->context, key);
if (res < 0) {
printf("%-40s: [ERROR] des3_key_setup failed with Code %i\r\n",
__FUNCTION__, res);
free(key);
return -2;
}
work[0] = WPA_GET_BE32(plain);
work[1] = WPA_GET_BE32(plain + 4);
desfunc(work, key->ek[0]);
desfunc(work, key->ek[1]);
desfunc(work, key->ek[2]);
WPA_PUT_BE32(crypt, work[0]);
WPA_PUT_BE32(crypt + 4, work[1]);
free(key);
return 1;
}
int tripledes_decrypt(cipher_context_t *context, uint8_t *crypt, uint8_t *plain)
{
int res;
struct des3_key_s *key = malloc(sizeof(des3_key_s));
uint32_t work[2];
if (!key) {
printf("%-40s: [ERROR] Could NOT malloc space for the des3_key_s \
struct.\r\n", __FUNCTION__);
return -1;
}
memset(key, 0, sizeof(des3_key_s));
res = des3_key_setup(context->context, key);
if (res < 0) {
printf("%-40s: [ERROR] des3_key_setup failed with Code %i\r\n",
__FUNCTION__, res);
free(key);
return -2;
}
work[0] = WPA_GET_BE32(crypt);
work[1] = WPA_GET_BE32(crypt + 4);
desfunc(work, key->dk[0]);
desfunc(work, key->dk[1]);
desfunc(work, key->dk[2]);
WPA_PUT_BE32(plain, work[0]);
WPA_PUT_BE32(plain + 4, work[1]);
free(key);
return 1;
}
uint8_t tripledes_get_preferred_block_size()
{
return THREEDES_BLOCK_SIZE;
}
static void cookey(const uint32_t *raw1, uint32_t *keyout)
{
uint32_t *cook;
const uint32_t *raw0;
uint32_t dough[32];
int i;
cook = dough;
for (i = 0; i < 16; i++, raw1++) {
raw0 = raw1++;
*cook = (*raw0 & 0x00fc0000L) << 6;
*cook |= (*raw0 & 0x00000fc0L) << 10;
*cook |= (*raw1 & 0x00fc0000L) >> 10;
*cook++ |= (*raw1 & 0x00000fc0L) >> 6;
*cook = (*raw0 & 0x0003f000L) << 12;
*cook |= (*raw0 & 0x0000003fL) << 16;
*cook |= (*raw1 & 0x0003f000L) >> 4;
*cook++ |= (*raw1 & 0x0000003fL);
}
memcpy(keyout, dough, sizeof(dough));
}
static void deskey(const uint8_t *key, int decrypt, uint32_t *keyout)
{
uint32_t i, j, l, m, n, kn[32];
uint8_t pc1m[56], pcr[56];
for (j = 0; j < 56; j++) {
l = (uint32_t) pc1[j];
m = l & 7;
pc1m[j] = (uint8_t)
((key[l >> 3U] & bytebit[m]) == bytebit[m] ? 1 : 0);
}
for (i = 0; i < 16; i++) {
if (decrypt) {
m = (15 - i) << 1;
}
else {
m = i << 1;
}
n = m + 1;
kn[m] = kn[n] = 0L;
for (j = 0; j < 28; j++) {
l = j + (uint32_t) totrot[i];
if (l < 28) {
pcr[j] = pc1m[l];
}
else {
pcr[j] = pc1m[l - 28];
}
}
for (/* j = 28 */; j < 56; j++) {
l = j + (uint32_t) totrot[i];
if (l < 56) {
pcr[j] = pc1m[l];
}
else {
pcr[j] = pc1m[l - 28];
}
}
for (j = 0; j < 24; j++) {
if ((int) pcr[(int) pc2[j]] != 0) {
kn[m] |= bigbyte[j];
}
if ((int) pcr[(int) pc2[j + 24]] != 0) {
kn[n] |= bigbyte[j];
}
}
}
cookey(kn, keyout);
}
static void desfunc(uint32_t *block, const uint32_t *keys)
{
uint32_t work, right, leftt;
int cur_round;
leftt = block[0];
right = block[1];
work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;
right ^= work;
leftt ^= (work << 4);
work = ((leftt >> 16) ^ right) & 0x0000ffffL;
right ^= work;
leftt ^= (work << 16);
work = ((right >> 2) ^ leftt) & 0x33333333L;
leftt ^= work;
right ^= (work << 2);
work = ((right >> 8) ^ leftt) & 0x00ff00ffL;
leftt ^= work;
right ^= (work << 8);
right = ROLc(right, 1);
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = ROLc(leftt, 1);
for (cur_round = 0; cur_round < 8; cur_round++) {
work = RORc(right, 4) ^ *keys++;
leftt ^= SP7[work & 0x3fL]
^ SP5[(work >> 8) & 0x3fL]
^ SP3[(work >> 16) & 0x3fL]
^ SP1[(work >> 24) & 0x3fL];
work = right ^ *keys++;
leftt ^= SP8[ work & 0x3fL]
^ SP6[(work >> 8) & 0x3fL]
^ SP4[(work >> 16) & 0x3fL]
^ SP2[(work >> 24) & 0x3fL];
work = RORc(leftt, 4) ^ *keys++;
right ^= SP7[ work & 0x3fL]
^ SP5[(work >> 8) & 0x3fL]
^ SP3[(work >> 16) & 0x3fL]
^ SP1[(work >> 24) & 0x3fL];
work = leftt ^ *keys++;
right ^= SP8[ work & 0x3fL]
^ SP6[(work >> 8) & 0x3fL]
^ SP4[(work >> 16) & 0x3fL]
^ SP2[(work >> 24) & 0x3fL];
}
right = RORc(right, 1);
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = RORc(leftt, 1);
work = ((leftt >> 8) ^ right) & 0x00ff00ffL;
right ^= work;
leftt ^= (work << 8);
/* -- */
work = ((leftt >> 2) ^ right) & 0x33333333L;
right ^= work;
leftt ^= (work << 2);
work = ((right >> 16) ^ leftt) & 0x0000ffffL;
leftt ^= work;
right ^= (work << 16);
work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;
leftt ^= work;
right ^= (work << 4);
block[0] = right;
block[1] = leftt;
}
static uint8_t des3_key_setup(const uint8_t *key, struct des3_key_s *dkey)
{
deskey(key, 0, dkey->ek[0]);
deskey(key + 8, 1, dkey->ek[1]);
deskey(key + 16, 0, dkey->ek[2]);
deskey(key, 1, dkey->dk[2]);
deskey(key + 8, 0, dkey->dk[1]);
deskey(key + 16, 1, dkey->dk[0]);
return 1;
}

@ -0,0 +1,9 @@
SRC = 3des.c
OBJ = $(SRC:%.c=$(BINDIR)%.o)
DEP = $(SRC:%.c=$(BINDIR)%.d)
MODULE = crypto_3des
include $(RIOTBASE)/Makefile.base

@ -1,9 +0,0 @@
INCLUDES = -I../include
MODULE = crypto
include $(RIOTBASE)/Makefile.base
ifeq ($(strip $(BOARD)),msba2)
$(warning sha256 produces wrong results on msba2 with our old toolchain)
endif

@ -0,0 +1,9 @@
SRC = aes.c
OBJ = $(SRC:%.c=$(BINDIR)%.o)
DEP = $(SRC:%.c=$(BINDIR)%.d)
MODULE = crypto_aes
include $(RIOTBASE)/Makefile.base

File diff suppressed because it is too large Load Diff

@ -0,0 +1,12 @@
/*
* Copyright (C) 2013 Freie Universität Berlin
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @defgroup sys_crypto Crypto
* @brief The crypto module is a lose collection of different crypto and hash algorithms
*/

@ -0,0 +1,9 @@
SRC = rc5.c
OBJ = $(SRC:%.c=$(BINDIR)%.o)
DEP = $(SRC:%.c=$(BINDIR)%.d)
MODULE = crypto_rc5
include $(RIOTBASE)/Makefile.base

@ -0,0 +1,192 @@
/*
* Copyright (C) 2013 Freie Universität Berlin, Computer Systems & Telematics
*
* This source code is licensed under the LGPLv2 license,
* See the file LICENSE for more details.
*/
/**
* @ingroup sys_crypto
* @{
*
* @file rc5.c
* @brief implementation of the RC5 cipher-algorithm
*
* @author Freie Universität Berlin, Computer Systems & Telematics
* @author Nicolai Schmittberger <nicolai.schmittberger@fu-berlin.de>
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
* @author Naveen Sastry
*
* @}
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "crypto/rc5.h"
#include "crypto/ciphers.h"
/**
* Define a fixed blocksize of 8 bytes
*/
#define BLOCK_SIZE (8U)
/**
* @brief Interface to the rc5 cipher
*/
block_cipher_interface_t rc5_interface = {
"RC5\0",
rc5_init,
rc5_encrypt,
rc5_decrypt,
rc5_setup_key,
rc5_get_preferred_block_size
};
int rc5_init(cipher_context_t *context, uint8_t blockSize, uint8_t keySize, uint8_t *key)
{
(void)keySize;
// 8 byte blocks only
if (blockSize != BLOCK_SIZE) {
return 0;
}
return rc5_setup_key(context, key, 0);
}
int rc5_encrypt(cipher_context_t *context, uint8_t *block,
uint8_t *cipherBlock)
{
register uint32_t l;
register uint32_t r;
register uint32_t *s = ((rc5_context_t *) context->context)->skey;
uint8_t i, tmp;
c2l(block, l);
block += 4;
c2l(block, r);
l += *s++;
r += *s++;
for (i = RC5_ROUNDS; i > 0; i--) {
l ^= r;
tmp = r;
tmp &= 0x1f;
rotl32(l, tmp);
l += *s++;
r ^= l;
tmp = l;
tmp &= 0x1f;
rotl32(r, tmp);
r += *s++;
}
l2c(l, cipherBlock);
cipherBlock += 4;
l2c(r, cipherBlock);
return 1;
}
int rc5_decrypt(cipher_context_t *context, uint8_t *cipherBlock,
uint8_t *plainBlock)
{
register uint32_t l;
register uint32_t r;
register uint32_t *s = ((rc5_context_t *) context->context)->skey +
(2 * RC5_ROUNDS) + 1;
uint8_t i, tmp;
c2l(cipherBlock, l);
cipherBlock += 4;
c2l(cipherBlock, r);
for (i = RC5_ROUNDS; i > 0; i--) {
r -= *s--;
tmp = l;
tmp &= 0x1f;
rotr32(r, tmp);
r ^= l;
l -= *s--;
tmp = r;
tmp &= 0x1f;
rotr32(l, tmp);
l ^= r;
}
r -= *s--;
l -= *s;
l2c(l, plainBlock);
plainBlock += 4;
l2c(r, plainBlock);
return 1;
}
int rc5_setup_key(cipher_context_t *context, uint8_t *key, uint8_t keysize)
{
(void)keysize;
uint32_t *L, l, A, B, *S, k;
uint8_t ii, jj, m;
int8_t i;
uint8_t tmp[8];
S = ((rc5_context_t *)context->context)->skey;
//dumpBuffer ("RC5M:setupKey K", (uint8_t *)key, 8);
c2l(key, l);
L = (uint32_t *) tmp;
L[0] = l;
key += 4;
c2l(key, l);
L[1] = l;
S[0] = RC5_32_P;
//dumpBuffer ("RC5M:setupKey L", (uint8_t *)L, 8);
for (i = 1; i < 2 * RC5_ROUNDS + 2; i++) {
S[i] = (S[i - 1] + RC5_32_Q);
/* sum =(*S+RC5_32_Q)&RC5_32_MASK;
* S++;
* S = sum;
*/
}
//dumpBuffer ("RC5M: setupKey S", (uint8_t *)S, 2 * (RC5_ROUNDS +1) * 4);
ii = jj = 0;
A = B = 0;
S = ((rc5_context_t *)context->context)->skey;
for (i = 3 * (2 * RC5_ROUNDS + 2) - 1; i >= 0; i--) {
k = (*S + A + B)&RC5_32_MASK;
rotl32((k), (3));
A = *S = k;
S++;
m = ((char)(A + B)) & 0x1f;
k = (*L + A + B)&RC5_32_MASK;
rotl32((k), (m));
B = *L = k;
if (++ii >= 2 * RC5_ROUNDS + 2) {
ii = 0;
S = ((rc5_context_t *)context->context)->skey;
}
jj ^= 4;
L = (uint32_t *)(&tmp[jj]);
}
return 1;
}
/**
* Returns the preferred block size that this cipher operates with. It is
* always safe to call this function before the init() call has been made.
*
* @return the preferred block size for this cipher. In the case where the
* cipher operates with multiple block sizes, this will pick one
* particular size (deterministically).
*/
uint8_t rc5_get_preferred_block_size()
{
return BLOCK_SIZE;
}

@ -0,0 +1,9 @@
SRC = sha256.c
OBJ = $(SRC:%.c=$(BINDIR)%.o)
DEP = $(SRC:%.c=$(BINDIR)%.d)
MODULE = crypto_sha256
include $(RIOTBASE)/Makefile.base

@ -27,9 +27,23 @@
* $FreeBSD: src/lib/libmd/sha256c.c,v 1.2 2006/01/17 15:35:56 phk Exp $
*/
/**
* @ingroup sys_crypto
* @{
*
* @file sha256.c
* @brief SHA256 hash function implementation
*
* @author Colin Percival
* @author Christian Mehlis
* @author Rene Kijewski
*
* @}
*/
#include <string.h>
#include "sha256.h"
#include "crypto/sha256.h"
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
/* Copy a vector of big-endian uint32_t into a vector of bytes */
@ -94,7 +108,7 @@ static const uint32_t K[64] = {
* SHA256 block compression function. The 256-bit state is transformed via
* the 512-bit input block to produce a new state.
*/
static void SHA256_Transform(uint32_t *state, const unsigned char block[64])
static void sha256_transform(uint32_t *state, const unsigned char block[64])
{
uint32_t W[64];
uint32_t S[8];
@ -136,7 +150,7 @@ static unsigned char PAD[64] = {
};
/* Add padding and terminating bit-count. */
static void SHA256_Pad(SHA256_CTX *ctx)
static void sha256_pad(sha256_context_t *ctx)
{
/*
* Convert length to a vector of bytes -- we do this now rather
@ -148,14 +162,14 @@ static void SHA256_Pad(SHA256_CTX *ctx)
/* Add 1--64 bytes so that the resulting length is 56 mod 64 */
uint32_t r = (ctx->count[1] >> 3) & 0x3f;
uint32_t plen = (r < 56) ? (56 - r) : (120 - r);
SHA256_Update(ctx, PAD, (size_t) plen);
sha256_update(ctx, PAD, (size_t) plen);
/* Add the terminating bit-count */
SHA256_Update(ctx, len, 8);
sha256_update(ctx, len, 8);
}
/* SHA-256 initialization. Begins a SHA-256 operation. */
void SHA256_Init(SHA256_CTX *ctx)
void sha256_init(sha256_context_t *ctx)
{
/* Zero bits processed so far */
ctx->count[0] = ctx->count[1] = 0;
@ -172,7 +186,7 @@ void SHA256_Init(SHA256_CTX *ctx)
}
/* Add bytes into the hash */
void SHA256_Update(SHA256_CTX *ctx, const void *in, size_t len)
void sha256_update(sha256_context_t *ctx, const void *in, size_t len)
{
/* Number of bytes left in the buffer from previous updates */
uint32_t r = (ctx->count[1] >> 3) & 0x3f;
@ -198,13 +212,13 @@ void SHA256_Update(SHA256_CTX *ctx, const void *in, size_t len)
const unsigned char *src = in;
memcpy(&ctx->buf[r], src, 64 - r);
SHA256_Transform(ctx->state, ctx->buf);
sha256_transform(ctx->state, ctx->buf);
src += 64 - r;
len -= 64 - r;
/* Perform complete blocks */
while (len >= 64) {
SHA256_Transform(ctx->state, src);
sha256_transform(ctx->state, src);
src += 64;
len -= 64;
}
@ -217,10 +231,10 @@ void SHA256_Update(SHA256_CTX *ctx, const void *in, size_t len)
* SHA-256 finalization. Pads the input data, exports the hash value,
* and clears the context state.
*/
void SHA256_Final(unsigned char digest[32], SHA256_CTX *ctx)
void sha256_final(unsigned char digest[32], sha256_context_t *ctx)
{
/* Add padding */
SHA256_Pad(ctx);
sha256_pad(ctx);
/* Write the hash */
be32enc_vect(digest, ctx->state, 32);
@ -229,18 +243,18 @@ void SHA256_Final(unsigned char digest[32], SHA256_CTX *ctx)
memset((void *) ctx, 0, sizeof(*ctx));
}
unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
unsigned char *sha256(const unsigned char *d, size_t n, unsigned char *md)
{
SHA256_CTX c;
sha256_context_t c;
static unsigned char m[SHA256_DIGEST_LENGTH];
if (md == NULL) {
md = m;
}
SHA256_Init(&c);
SHA256_Update(&c, d, n);
SHA256_Final(md, &c);
sha256_init(&c);
sha256_update(&c, d, n);
sha256_final(md, &c);
return md;
}

@ -0,0 +1,9 @@
SRC = skipjack.c
OBJ = $(SRC:%.c=$(BINDIR)%.o)
DEP = $(SRC:%.c=$(BINDIR)%.d)
MODULE = crypto_skipjack
include $(RIOTBASE)/Makefile.base

@ -0,0 +1,351 @@
/*
* Copyright (C) 2013 Freie Universität Berlin, Computer Systems & Telematics
*
* This source code is licensed under the LGPLv2 license,
* See the file LICENSE for more details.
*/
/**
* @ingroup sys_crypto
* @{
*
* @file skipjack.c
* @brief implementation of the SkipJack Cipher-Algorithm
*
* @author Freie Universitaet Berlin, Computer Systems & Telematics
* @author Nicolai Schmittberger <nicolai.schmittberger@fu-berlin.de>
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
* @author Naveen Sastry
*
* @}
*/
/*
* From the NIST description of SkipJack.
*/
// our context: we just expand the key to 20 bytes.
//
// we have two options for the expansion:
// 1. no expansion. advantage: 10byte context. disadvantage: mucks up
// the G box code with ifs / mods. Alternatively adds lots of code and
// muckiness.
// 2. expand key to 128 bytes. Makes G boxes easy to write, and minimal
// code expansion. disadvantage: wasted memory
// 3. expand key to 20 bytes. G boxes still simple, the encode and decode
// functions are a little more complicated, but still more or less
// managable. this is what we've implemented.
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "crypto/ciphers.h"
#include "crypto/skipjack.h"
/**
* @brief Define a fixed block size of 8 bytes
*/
#define BLOCK_SIZE (8U)
/**
* @brief Interface to the skipjack cipher
*/
block_cipher_interface_t skipjack_interface = {
"SkipJack\0",
skipjack_init,
skipjack_encrypt,
skipjack_decrypt,
skipjack_setup_key,
skipjack_get_preferred_block_size
};
// F-BOX
// It can live in either RAM (faster access) or program memory (save ram,
// but slower access). The type CRYPTO_TABLE_TYPE, defined in crypto.h
// defines where we drop the table and how we access it. This is necessary
// to compile for the PC target since it doesn't support tables in
// program memory the same way.
static const uint8_t SJ_F[] /*__attribute__((C))*/ = {
0xA3, 0xD7, 0x09, 0x83, 0xF8, 0x48, 0xF6, 0xF4, 0xB3, 0x21, 0x15, 0x78,
0x99, 0xB1, 0xAF, 0xF9, 0xE7, 0x2D, 0x4D, 0x8A, 0xCE, 0x4C, 0xCA, 0x2E,
0x52, 0x95, 0xD9, 0x1E, 0x4E, 0x38, 0x44, 0x28, 0x0A, 0xDF, 0x02, 0xA0,
0x17, 0xF1, 0x60, 0x68, 0x12, 0xB7, 0x7A, 0xC3, 0xE9, 0xFA, 0x3D, 0x53,
0x96, 0x84, 0x6B, 0xBA, 0xF2, 0x63, 0x9A, 0x19, 0x7C, 0xAE, 0xE5, 0xF5,
0xF7, 0x16, 0x6A, 0xA2, 0x39, 0xB6, 0x7B, 0x0F, 0xC1, 0x93, 0x81, 0x1B,
0xEE, 0xB4, 0x1A, 0xEA, 0xD0, 0x91, 0x2F, 0xB8, 0x55, 0xB9, 0xDA, 0x85,
0x3F, 0x41, 0xBF, 0xE0, 0x5A, 0x58, 0x80, 0x5F, 0x66, 0x0B, 0xD8, 0x90,
0x35, 0xD5, 0xC0, 0xA7, 0x33, 0x06, 0x65, 0x69, 0x45, 0x00, 0x94, 0x56,
0x6D, 0x98, 0x9B, 0x76, 0x97, 0xFC, 0xB2, 0xC2, 0xB0, 0xFE, 0xDB, 0x20,
0xE1, 0xEB, 0xD6, 0xE4, 0xDD, 0x47, 0x4A, 0x1D, 0x42, 0xED, 0x9E, 0x6E,
0x49, 0x3C, 0xCD, 0x43, 0x27, 0xD2, 0x07, 0xD4, 0xDE, 0xC7, 0x67, 0x18,
0x89, 0xCB, 0x30, 0x1F, 0x8D, 0xC6, 0x8F, 0xAA, 0xC8, 0x74, 0xDC, 0xC9,
0x5D, 0x5C, 0x31, 0xA4, 0x70, 0x88, 0x61, 0x2C, 0x9F, 0x0D, 0x2B, 0x87,
0x50, 0x82, 0x54, 0x64, 0x26, 0x7D, 0x03, 0x40, 0x34, 0x4B, 0x1C, 0x73,
0xD1, 0xC4, 0xFD, 0x3B, 0xCC, 0xFB, 0x7F, 0xAB, 0xE6, 0x3E, 0x5B, 0xA5,
0xAD, 0x04, 0x23, 0x9C, 0x14, 0x51, 0x22, 0xF0, 0x29, 0x79, 0x71, 0x7E,
0xFF, 0x8C, 0x0E, 0xE2, 0x0C, 0xEF, 0xBC, 0x72, 0x75, 0x6F, 0x37, 0xA1,
0xEC, 0xD3, 0x8E, 0x62, 0x8B, 0x86, 0x10, 0xE8, 0x08, 0x77, 0x11, 0xBE,
0x92, 0x4F, 0x24, 0xC5, 0x32, 0x36, 0x9D, 0xCF, 0xF3, 0xA6, 0xBB, 0xAC,
0x5E, 0x6C, 0xA9, 0x13, 0x57, 0x25, 0xB5, 0xE3, 0xBD, 0xA8, 0x3A, 0x01,
0x05, 0x59, 0x2A, 0x46
};
int skipjack_init(cipher_context_t *context, uint8_t blockSize, uint8_t keySize,
uint8_t *key)
{
// 8 byte blocks only
if (blockSize != BLOCK_SIZE) {
return 0;
}
return skipjack_setup_key(context, key, keySize);
}
/**
* @brief convert 2x uint8_t to uint16_t
*
* @param c pointer to the 2x uint8_t input
* @param s pointer to the resulting uint16_t
*
*/
static void c2sM(uint8_t *c, uint16_t *s)
{
memcpy(s, c, sizeof(uint16_t));
return;
}
/**
* @brief convert one uint16_t to 2x uint8_t
*
* @param s pointer to the uint16_t input
* @param c pointer to the first resulting uint8_ts
*/
static void s2cM(uint16_t s, uint8_t *c)
{
memcpy(c, &s, sizeof(uint16_t));
return;
}
int skipjack_encrypt(cipher_context_t *context, uint8_t *plainBlock,
uint8_t *cipherBlock)
{
// prologue 10 pushs = 20 cycles
/*register*/ uint8_t counter = 1;
/*register*/ uint8_t *skey = ((skipjack_context_t *)context->context)->skey;
/*register*/ uint16_t w1, w2, w3, w4, tmp;
/*register*/ uint8_t bLeft, bRight;
//dumpBuffer("SkipJack.encrypt: plainBlock", plainBlock, 8);
c2sM(plainBlock, &w1);
plainBlock += 2;
c2sM(plainBlock, &w2);
plainBlock += 2;
c2sM(plainBlock, &w3);
plainBlock += 2;
c2sM(plainBlock, &w4);
plainBlock += 2;
/*
* code if we had expanded key to 128 bytes. this is what the code below
* does, but after every 5 operations, it resets the where we are
* in the key back to the beginning of the skey. so our loops end up
* looking a little funny.
*
* while (counter < 9)
* RULE_A(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
* while (counter < 17)
* RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
* while (counter < 25)
* RULE_A(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
* while (counter < 33)
* RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
*/
while (counter < 6) { // 5x
RULE_A(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey;
while (counter < 9) { // 3x
RULE_A(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
while (counter < 11) { // 2x
RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey;
while (counter < 16) { // 5x
RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey;
// 1x
RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
while (counter < 21) { // 4x
RULE_A(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey;
while (counter < 25) { // 4x
RULE_A(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
// 1x
RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
skey = ((skipjack_context_t *)context->context)->skey;
while (counter < 31) { // 5x
RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey;
while (counter < 33) { // 2x
RULE_B(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
s2cM(w1, cipherBlock);
cipherBlock += 2;
s2cM(w2, cipherBlock);
cipherBlock += 2;
s2cM(w3, cipherBlock);
cipherBlock += 2;
s2cM(w4, cipherBlock);
cipherBlock += 2;
return 1;
}
int skipjack_decrypt(cipher_context_t *context, uint8_t *cipherBlock,
uint8_t *plainBlock)
{
/*register*/ uint8_t counter = 32;
/*register*/ uint8_t *skey = ((skipjack_context_t *)context->context)->skey + 4;
/*register*/ uint16_t w1, w2, w3, w4, tmp;
/*register*/ uint8_t bLeft, bRight;
//dumpBuffer("SkipJack.decrypt: cipherBlock", cipherBlock, 8);
c2sM(cipherBlock, &w1);
cipherBlock += 2;
c2sM(cipherBlock, &w2);
cipherBlock += 2;
c2sM(cipherBlock, &w3);
cipherBlock += 2;
c2sM(cipherBlock, &w4);
/*
// code if we had expanded key to 128 bytes. this is what the code below
// does, but after every 5 operations, it resets the where we are
// in the key back to the beginning of the skey. so our loops end up
// looking a little funny.
while (counter > 24)
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
while (counter > 16)
RULE_A_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
while (counter > 8)
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
while (counter > 0)
RULE_A_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight );
*/
while (counter > 30) { //2x
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey + 16;
while (counter > 25) { //5x
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey + 16;
//1x
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
while (counter > 20) { //4x
RULE_A_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey + 16;
while (counter > 16) { //4x
RULE_A_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
//1x
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
skey = ((skipjack_context_t *)context->context)->skey + 16;
while (counter > 10) { //5x
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey + 16;
while (counter > 8) { // 2x
RULE_B_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
while (counter > 5) { // 3x
RULE_A_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
skey = ((skipjack_context_t *)context->context)->skey + 16;
while (counter > 0) { // 5x
RULE_A_INV(skey, w1, w2, w3, w4, counter, tmp, bLeft, bRight);
}
s2cM(w1, plainBlock);
plainBlock += 2;
s2cM(w2, plainBlock);
plainBlock += 2;
s2cM(w3, plainBlock);
plainBlock += 2;
s2cM(w4, plainBlock);
return 1;
}
int skipjack_setup_key(cipher_context_t *context, uint8_t *key, uint8_t keysize)
{
int i = 0;
uint8_t *skey = ((skipjack_context_t *)context->context)->skey;
// for keys which are smaller than 160 bits, concatenate until they reach
// 160 bits in size. Note that key expansion is just concatenation.
if (keysize < CIPHERS_KEYSIZE) {
//fill up by concatenating key to as long as needed
for (i = 0; i < CIPHERS_KEYSIZE; i++) {
skey[i] = key[(i % keysize)];
}
}
else {
for (i = 0; i < CIPHERS_KEYSIZE; i++) {
skey[i] = key[i];
}
}
return 1;
}
uint8_t skipjack_get_preferred_block_size()
{
return BLOCK_SIZE;
}

@ -0,0 +1,9 @@
SRC = twofish.c
OBJ = $(SRC:%.c=$(BINDIR)%.o)
DEP = $(SRC:%.c=$(BINDIR)%.d)
MODULE = crypto_twofish
include $(RIOTBASE)/Makefile.base

@ -0,0 +1,759 @@
/*
* Copyright (C) 2013 Freie Universität Berlin, Computer Systems & Telematics
*
* This source code is licensed under the LGPLv2 license,
* See the file LICENSE for more details.
*/
/**
* @ingroup sys_crypto
* @{
*
* @file twofish.c
* @brief implementation of the twofish cipher-algorithm
*
* @author Freie Universitaet Berlin, Computer Systems & Telematics
* @author Nicolai Schmittberger <nicolai.schmittberger@fu-berlin.de>
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
* @author Matthew Skala <mskala@ansuz.sooke.bc.ca>
*
* @}
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "crypto/twofish.h"
#include "crypto/ciphers.h"
//prototype
static int twofish_set_key(twofish_context_t *ctx, uint8_t *key, uint8_t keylen);
// twofish interface
block_cipher_interface_t twofish_interface = {
"TWOFISH\0",
twofish_init,
twofish_encrypt,
twofish_decrypt,
twofish_setup_key,
twofish_get_preferred_block_size
};
/* These two tables are the q0 and q1 permutations, exactly as described in
* the Twofish paper. */
static const uint8_t q0[256] = {
0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78,
0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30,
0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82,
0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE,
0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B,
0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45,
0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7,
0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF,
0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8,
0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED,
0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90,
0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B,
0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B,
0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F,
0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A,
0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17,
0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72,
0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68,
0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4,
0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42,
0x4A, 0x5E, 0xC1, 0xE0
};
static const uint8_t q1[256] = {