Adding DES and Triple-DES
This commit is contained in:
parent
57e6e95bbd
commit
c3885c4a83
|
@ -25,16 +25,15 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include <stdlib.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "des.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
||||
const char des_str[] PROGMEM = "DES";
|
||||
const char des_str[] = "DES";
|
||||
|
||||
const uint8_t des_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(64),
|
||||
KS_TYPE_TERMINATOR };
|
||||
const uint8_t des_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(64),
|
||||
KS_TYPE_TERMINATOR };
|
||||
static
|
||||
void des_dummy_enc(void* block, void* key){
|
||||
des_enc(block, block, key);
|
||||
|
@ -45,7 +44,7 @@ void des_dummy_dec(void* block, void* key){
|
|||
des_dec(block, block, key);
|
||||
}
|
||||
|
||||
const bcdesc_t des_desc PROGMEM = {
|
||||
const bcdesc_t des_desc = {
|
||||
BCDESC_TYPE_BLOCKCIPHER,
|
||||
BC_INIT_TYPE_1,
|
||||
des_str,
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "des.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
|
|
@ -25,18 +25,17 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include <stdlib.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "serpent.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
||||
const char serpent_str[] PROGMEM = "serpent";
|
||||
const char serpent_str[] = "serpent";
|
||||
|
||||
const uint8_t serpent_keysize_desc[] PROGMEM = { KS_TYPE_RANGE, KS_INT(1), KS_INT(256),
|
||||
KS_TYPE_TERMINATOR };
|
||||
const uint8_t serpent_keysize_desc[] = { KS_TYPE_RANGE, KS_INT(1), KS_INT(256),
|
||||
KS_TYPE_TERMINATOR };
|
||||
|
||||
const bcdesc_t serpent_desc PROGMEM = {
|
||||
const bcdesc_t serpent_desc = {
|
||||
BCDESC_TYPE_BLOCKCIPHER,
|
||||
BC_INIT_TYPE_2,
|
||||
serpent_str,
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "serpent.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
|
|
@ -25,28 +25,26 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include <stdlib.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "des.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
||||
const char tdes_str[] PROGMEM = "TDES";
|
||||
const char tdes_str[] = "TDES";
|
||||
|
||||
const uint8_t tdes_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(192),
|
||||
KS_TYPE_TERMINATOR };
|
||||
const uint8_t tdes_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(192),
|
||||
KS_TYPE_TERMINATOR };
|
||||
|
||||
static
|
||||
void tdes_dummy_enc(void* block, void* key){
|
||||
|
||||
void tdes_dummy_enc(void* block, const void* key){
|
||||
tdes_enc(block, block, key);
|
||||
}
|
||||
|
||||
static
|
||||
void tdes_dummy_dec(void* block, void* key){
|
||||
void tdes_dummy_dec(void* block, const void* key){
|
||||
tdes_dec(block, block, key);
|
||||
}
|
||||
|
||||
const bcdesc_t tdes_desc PROGMEM = {
|
||||
const bcdesc_t tdes_desc = {
|
||||
BCDESC_TYPE_BLOCKCIPHER,
|
||||
BC_INIT_TYPE_1,
|
||||
tdes_str,
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "des.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
|
|
@ -25,28 +25,26 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include <stdlib.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "des.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
||||
const char tdes2_str[] PROGMEM = "TDES-2";
|
||||
#include <string.h>
|
||||
|
||||
const uint8_t tdes2_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(128),
|
||||
KS_TYPE_TERMINATOR };
|
||||
const char tdes2_str[] = "TDES-2";
|
||||
|
||||
static
|
||||
void tdes_dummy_enc(void* block, void* key){
|
||||
const uint8_t tdes2_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(128),
|
||||
KS_TYPE_TERMINATOR };
|
||||
|
||||
void tdes2_dummy_enc(void* block, const void* key){
|
||||
tdes_enc(block, block, key);
|
||||
}
|
||||
|
||||
static
|
||||
void tdes_dummy_dec(void* block, void* key){
|
||||
void tdes2_dummy_dec(void* block, const void* key){
|
||||
tdes_dec(block, block, key);
|
||||
}
|
||||
|
||||
static
|
||||
void tdes2_init(void* key, void* ctx){
|
||||
memcpy(ctx, key, 16);
|
||||
memcpy((uint8_t*)ctx+16, key, 8);
|
||||
|
@ -54,15 +52,15 @@ void tdes2_init(void* key, void* ctx){
|
|||
|
||||
|
||||
|
||||
const bcdesc_t tdes2_desc PROGMEM = {
|
||||
const bcdesc_t tdes2_desc = {
|
||||
BCDESC_TYPE_BLOCKCIPHER,
|
||||
BC_INIT_TYPE_1,
|
||||
tdes2_str,
|
||||
24,
|
||||
64,
|
||||
{(void_fpt)tdes2_init},
|
||||
{(void_fpt)tdes_dummy_enc},
|
||||
{(void_fpt)tdes_dummy_dec},
|
||||
{(void_fpt)tdes2_dummy_enc},
|
||||
{(void_fpt)tdes2_dummy_dec},
|
||||
(bc_free_fpt)NULL,
|
||||
tdes2_keysize_desc
|
||||
};
|
||||
|
|
|
@ -28,7 +28,6 @@
|
|||
#ifndef BCAL_TDES2_H_
|
||||
#define BCAL_TDES2_H_
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include "blockcipher_descriptor.h"
|
||||
#include "des.h"
|
||||
#include "keysize_descriptor.h"
|
||||
|
|
|
@ -0,0 +1,384 @@
|
|||
/* des.c */
|
||||
/*
|
||||
This file is part of the ARM-Crypto-Lib.
|
||||
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* \file des.c
|
||||
* \author Daniel Otte
|
||||
* \email daniel.otte@rub.de
|
||||
* \date 2007-06-16
|
||||
* \brief DES and EDE-DES implementation
|
||||
* \license GPLv3 or later
|
||||
*
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
const uint8_t sbox[256] = {
|
||||
/* S-box 1 */
|
||||
0xE4, 0xD1, 0x2F, 0xB8, 0x3A, 0x6C, 0x59, 0x07,
|
||||
0x0F, 0x74, 0xE2, 0xD1, 0xA6, 0xCB, 0x95, 0x38,
|
||||
0x41, 0xE8, 0xD6, 0x2B, 0xFC, 0x97, 0x3A, 0x50,
|
||||
0xFC, 0x82, 0x49, 0x17, 0x5B, 0x3E, 0xA0, 0x6D,
|
||||
/* S-box 2 */
|
||||
0xF1, 0x8E, 0x6B, 0x34, 0x97, 0x2D, 0xC0, 0x5A,
|
||||
0x3D, 0x47, 0xF2, 0x8E, 0xC0, 0x1A, 0x69, 0xB5,
|
||||
0x0E, 0x7B, 0xA4, 0xD1, 0x58, 0xC6, 0x93, 0x2F,
|
||||
0xD8, 0xA1, 0x3F, 0x42, 0xB6, 0x7C, 0x05, 0xE9,
|
||||
/* S-box 3 */
|
||||
0xA0, 0x9E, 0x63, 0xF5, 0x1D, 0xC7, 0xB4, 0x28,
|
||||
0xD7, 0x09, 0x34, 0x6A, 0x28, 0x5E, 0xCB, 0xF1,
|
||||
0xD6, 0x49, 0x8F, 0x30, 0xB1, 0x2C, 0x5A, 0xE7,
|
||||
0x1A, 0xD0, 0x69, 0x87, 0x4F, 0xE3, 0xB5, 0x2C,
|
||||
/* S-box 4 */
|
||||
0x7D, 0xE3, 0x06, 0x9A, 0x12, 0x85, 0xBC, 0x4F,
|
||||
0xD8, 0xB5, 0x6F, 0x03, 0x47, 0x2C, 0x1A, 0xE9,
|
||||
0xA6, 0x90, 0xCB, 0x7D, 0xF1, 0x3E, 0x52, 0x84,
|
||||
0x3F, 0x06, 0xA1, 0xD8, 0x94, 0x5B, 0xC7, 0x2E,
|
||||
/* S-box 5 */
|
||||
0x2C, 0x41, 0x7A, 0xB6, 0x85, 0x3F, 0xD0, 0xE9,
|
||||
0xEB, 0x2C, 0x47, 0xD1, 0x50, 0xFA, 0x39, 0x86,
|
||||
0x42, 0x1B, 0xAD, 0x78, 0xF9, 0xC5, 0x63, 0x0E,
|
||||
0xB8, 0xC7, 0x1E, 0x2D, 0x6F, 0x09, 0xA4, 0x53,
|
||||
/* S-box 6 */
|
||||
0xC1, 0xAF, 0x92, 0x68, 0x0D, 0x34, 0xE7, 0x5B,
|
||||
0xAF, 0x42, 0x7C, 0x95, 0x61, 0xDE, 0x0B, 0x38,
|
||||
0x9E, 0xF5, 0x28, 0xC3, 0x70, 0x4A, 0x1D, 0xB6,
|
||||
0x43, 0x2C, 0x95, 0xFA, 0xBE, 0x17, 0x60, 0x8D,
|
||||
/* S-box 7 */
|
||||
0x4B, 0x2E, 0xF0, 0x8D, 0x3C, 0x97, 0x5A, 0x61,
|
||||
0xD0, 0xB7, 0x49, 0x1A, 0xE3, 0x5C, 0x2F, 0x86,
|
||||
0x14, 0xBD, 0xC3, 0x7E, 0xAF, 0x68, 0x05, 0x92,
|
||||
0x6B, 0xD8, 0x14, 0xA7, 0x95, 0x0F, 0xE2, 0x3C,
|
||||
/* S-box 8 */
|
||||
0xD2, 0x84, 0x6F, 0xB1, 0xA9, 0x3E, 0x50, 0xC7,
|
||||
0x1F, 0xD8, 0xA3, 0x74, 0xC5, 0x6B, 0x0E, 0x92,
|
||||
0x7B, 0x41, 0x9C, 0xE2, 0x06, 0xAD, 0xF3, 0x58,
|
||||
0x21, 0xE7, 0x4A, 0x8D, 0xFC, 0x90, 0x35, 0x6B
|
||||
};
|
||||
|
||||
const uint8_t e_permtab[] ={
|
||||
4, 6, /* 4 bytes in 6 bytes out*/
|
||||
32, 1, 2, 3, 4, 5,
|
||||
4, 5, 6, 7, 8, 9,
|
||||
8, 9, 10, 11, 12, 13,
|
||||
12, 13, 14, 15, 16, 17,
|
||||
16, 17, 18, 19, 20, 21,
|
||||
20, 21, 22, 23, 24, 25,
|
||||
24, 25, 26, 27, 28, 29,
|
||||
28, 29, 30, 31, 32, 1
|
||||
};
|
||||
|
||||
const uint8_t p_permtab[] ={
|
||||
4, 4, /* 32 bit -> 32 bit */
|
||||
16, 7, 20, 21,
|
||||
29, 12, 28, 17,
|
||||
1, 15, 23, 26,
|
||||
5, 18, 31, 10,
|
||||
2, 8, 24, 14,
|
||||
32, 27, 3, 9,
|
||||
19, 13, 30, 6,
|
||||
22, 11, 4, 25
|
||||
};
|
||||
|
||||
const uint8_t ip_permtab[] ={
|
||||
8, 8, /* 64 bit -> 64 bit */
|
||||
58, 50, 42, 34, 26, 18, 10, 2,
|
||||
60, 52, 44, 36, 28, 20, 12, 4,
|
||||
62, 54, 46, 38, 30, 22, 14, 6,
|
||||
64, 56, 48, 40, 32, 24, 16, 8,
|
||||
57, 49, 41, 33, 25, 17, 9, 1,
|
||||
59, 51, 43, 35, 27, 19, 11, 3,
|
||||
61, 53, 45, 37, 29, 21, 13, 5,
|
||||
63, 55, 47, 39, 31, 23, 15, 7
|
||||
};
|
||||
|
||||
const uint8_t inv_ip_permtab[] ={
|
||||
8, 8, /* 64 bit -> 64 bit */
|
||||
40, 8, 48, 16, 56, 24, 64, 32,
|
||||
39, 7, 47, 15, 55, 23, 63, 31,
|
||||
38, 6, 46, 14, 54, 22, 62, 30,
|
||||
37, 5, 45, 13, 53, 21, 61, 29,
|
||||
36, 4, 44, 12, 52, 20, 60, 28,
|
||||
35, 3, 43, 11, 51, 19, 59, 27,
|
||||
34, 2, 42, 10, 50, 18, 58, 26,
|
||||
33, 1, 41, 9, 49, 17, 57, 25
|
||||
};
|
||||
|
||||
const uint8_t pc1_permtab[] ={
|
||||
8, 7, /* 64 bit -> 56 bit*/
|
||||
57, 49, 41, 33, 25, 17, 9,
|
||||
1, 58, 50, 42, 34, 26, 18,
|
||||
10, 2, 59, 51, 43, 35, 27,
|
||||
19, 11, 3, 60, 52, 44, 36,
|
||||
63, 55, 47, 39, 31, 23, 15,
|
||||
7, 62, 54, 46, 38, 30, 22,
|
||||
14, 6, 61, 53, 45, 37, 29,
|
||||
21, 13, 5, 28, 20, 12, 4
|
||||
};
|
||||
|
||||
const uint8_t pc2_permtab[] ={
|
||||
7, 6, /* 56 bit -> 48 bit */
|
||||
14, 17, 11, 24, 1, 5,
|
||||
3, 28, 15, 6, 21, 10,
|
||||
23, 19, 12, 4, 26, 8,
|
||||
16, 7, 27, 20, 13, 2,
|
||||
41, 52, 31, 37, 47, 55,
|
||||
30, 40, 51, 45, 33, 48,
|
||||
44, 49, 39, 56, 34, 53,
|
||||
46, 42, 50, 36, 29, 32
|
||||
};
|
||||
|
||||
const uint8_t splitin6bitword_permtab[] = {
|
||||
8, 8, /* 64 bit -> 64 bit */
|
||||
64, 64, 1, 6, 2, 3, 4, 5,
|
||||
64, 64, 7, 12, 8, 9, 10, 11,
|
||||
64, 64, 13, 18, 14, 15, 16, 17,
|
||||
64, 64, 19, 24, 20, 21, 22, 23,
|
||||
64, 64, 25, 30, 26, 27, 28, 29,
|
||||
64, 64, 31, 36, 32, 33, 34, 35,
|
||||
64, 64, 37, 42, 38, 39, 40, 41,
|
||||
64, 64, 43, 48, 44, 45, 46, 47
|
||||
};
|
||||
|
||||
const uint8_t shiftkey_permtab[] = {
|
||||
7, 7, /* 56 bit -> 56 bit */
|
||||
2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17,
|
||||
18, 19, 20, 21, 22, 23, 24, 25,
|
||||
26, 27, 28, 1,
|
||||
30, 31, 32, 33, 34, 35, 36, 37,
|
||||
38, 39, 40, 41, 42, 43, 44, 45,
|
||||
46, 47, 48, 49, 50, 51, 52, 53,
|
||||
54, 55, 56, 29
|
||||
};
|
||||
|
||||
const uint8_t shiftkeyinv_permtab[] = {
|
||||
7, 7,
|
||||
28, 1, 2, 3, 4, 5, 6, 7,
|
||||
8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23,
|
||||
24, 25, 26, 27,
|
||||
56, 29, 30, 31, 32, 33, 34, 35,
|
||||
36, 37, 38, 39, 40, 41, 42, 43,
|
||||
44, 45, 46, 47, 48, 49, 50, 51,
|
||||
52, 53, 54, 55
|
||||
};
|
||||
|
||||
/*
|
||||
1 0
|
||||
1 0
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
----
|
||||
1 0
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
2 1
|
||||
1 0
|
||||
*/
|
||||
#define ROTTABLE 0x7EFC
|
||||
#define ROTTABLE_INV 0x3F7E
|
||||
/******************************************************************************/
|
||||
|
||||
void permute(uint8_t *ptable, const uint8_t *in, uint8_t *out){
|
||||
uint8_t ib, ob; /* in-bytes and out-bytes */
|
||||
uint8_t byte, bit; /* counter for bit and byte */
|
||||
ib = ptable[0];
|
||||
ob = ptable[1];
|
||||
ptable = &(ptable[2]);
|
||||
for(byte=0; byte<ob; ++byte){
|
||||
uint8_t x,t=0;
|
||||
for(bit=0; bit<8; ++bit){
|
||||
x=*ptable++ -1 ;
|
||||
t<<=1;
|
||||
if((in[x/8]) & (0x80>>(x%8)) ){
|
||||
t|=0x01;
|
||||
}
|
||||
}
|
||||
out[byte]=t;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void changeendian32(uint32_t * a){
|
||||
*a = (*a & 0x000000FF) << 24 |
|
||||
(*a & 0x0000FF00) << 8 |
|
||||
(*a & 0x00FF0000) >> 8 |
|
||||
(*a & 0xFF000000) >> 24;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
static inline
|
||||
void shiftkey(uint8_t *key){
|
||||
uint8_t k[7];
|
||||
memcpy(k, key, 7);
|
||||
permute((uint8_t*)shiftkey_permtab, k, key);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
static inline
|
||||
void shiftkey_inv(uint8_t *key){
|
||||
uint8_t k[7];
|
||||
memcpy(k, key, 7);
|
||||
permute((uint8_t*)shiftkeyinv_permtab, k, key);
|
||||
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
static inline
|
||||
uint64_t splitin6bitwords(uint64_t a){
|
||||
uint64_t ret=0;
|
||||
a &= 0x0000ffffffffffffLL;
|
||||
permute((uint8_t*)splitin6bitword_permtab, (uint8_t*)&a, (uint8_t*)&ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static inline
|
||||
uint8_t substitute(uint8_t a, uint8_t * sbp){
|
||||
uint8_t x;
|
||||
x = sbp[a>>1];
|
||||
x = (a&1)?x&0x0F:x>>4;
|
||||
return x;
|
||||
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
uint32_t des_f(uint32_t r, uint8_t* kr){
|
||||
uint8_t i;
|
||||
uint32_t t=0,ret;
|
||||
uint64_t data;
|
||||
uint8_t *sbp; /* sboxpointer */
|
||||
permute((uint8_t*)e_permtab, (uint8_t*)&r, (uint8_t*)&data);
|
||||
for(i=0; i<7; ++i)
|
||||
((uint8_t*)&data)[i] ^= kr[i];
|
||||
|
||||
/* Sbox substitution */
|
||||
data = splitin6bitwords(data);
|
||||
sbp=(uint8_t*)sbox;
|
||||
for(i=0; i<8; ++i){
|
||||
uint8_t x;
|
||||
x = substitute(((uint8_t*)&data)[i], sbp);
|
||||
t<<=4;
|
||||
t |= x;
|
||||
sbp += 32;
|
||||
}
|
||||
changeendian32(&t);
|
||||
|
||||
permute((uint8_t*)p_permtab,(uint8_t*)&t, (uint8_t*)&ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void des_enc(void* out, const void* in, const void* key){
|
||||
#define R *((uint32_t*)&(data[4]))
|
||||
#define L *((uint32_t*)&(data[0]))
|
||||
|
||||
uint8_t data[8],kr[6],k[7];
|
||||
uint8_t i;
|
||||
|
||||
permute((uint8_t*)ip_permtab, (uint8_t*)in, data);
|
||||
permute((uint8_t*)pc1_permtab, (const uint8_t*)key, k);
|
||||
for(i=0; i<8; ++i){
|
||||
shiftkey(k);
|
||||
if(ROTTABLE&((1<<((i<<1)+0))) )
|
||||
shiftkey(k);
|
||||
permute((uint8_t*)pc2_permtab, k, kr);
|
||||
L ^= des_f(R, kr);
|
||||
|
||||
shiftkey(k);
|
||||
if(ROTTABLE&((1<<((i<<1)+1))) )
|
||||
shiftkey(k);
|
||||
permute((uint8_t*)pc2_permtab, k, kr);
|
||||
R ^= des_f(L, kr);
|
||||
|
||||
}
|
||||
/* L <-> R*/
|
||||
R ^= L;
|
||||
L ^= R;
|
||||
R ^= L;
|
||||
|
||||
permute((uint8_t*)inv_ip_permtab, data, (uint8_t*)out);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void des_dec(void* out, const void* in, const uint8_t* key){
|
||||
#define R *((uint32_t*)&(data[4]))
|
||||
#define L *((uint32_t*)&(data[0]))
|
||||
|
||||
uint8_t data[8],kr[6],k[7];
|
||||
int8_t i;
|
||||
permute((uint8_t*)ip_permtab, (uint8_t*)in, data);
|
||||
permute((uint8_t*)pc1_permtab, (const uint8_t*)key, k);
|
||||
for(i=7; i>=0; --i){
|
||||
|
||||
permute((uint8_t*)pc2_permtab, k, kr);
|
||||
L ^= des_f(R, kr);
|
||||
shiftkey_inv(k);
|
||||
if(ROTTABLE&((1<<((i<<1)+1))) ){
|
||||
shiftkey_inv(k);
|
||||
}
|
||||
|
||||
permute((uint8_t*)pc2_permtab, k, kr);
|
||||
R ^= des_f(L, kr);
|
||||
shiftkey_inv(k);
|
||||
if(ROTTABLE&((1<<((i<<1)+0))) ){
|
||||
shiftkey_inv(k);
|
||||
}
|
||||
|
||||
}
|
||||
/* L <-> R*/
|
||||
R ^= L;
|
||||
L ^= R;
|
||||
R ^= L;
|
||||
|
||||
permute((uint8_t*)inv_ip_permtab, data, (uint8_t*)out);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void tdes_enc(void* out, void* in, const void* key){
|
||||
des_enc(out, in, (uint8_t*)key + 0);
|
||||
des_dec(out, out, (uint8_t*)key + 8);
|
||||
des_enc(out, out, (uint8_t*)key +16);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void tdes_dec(void* out, void* in, const uint8_t* key){
|
||||
des_dec(out, in, (uint8_t*)key +16);
|
||||
des_enc(out, out, (uint8_t*)key + 8);
|
||||
des_dec(out, out, (uint8_t*)key + 0);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
/* des.h */
|
||||
/*
|
||||
This file is part of the ARM-Crypto-Lib.
|
||||
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* \file des.h
|
||||
* \author Daniel Otte
|
||||
* \date 2007-06-16
|
||||
* \brief des and tdes declarations
|
||||
* \license GPLv3 or later
|
||||
*
|
||||
*/
|
||||
#ifndef DES_H_
|
||||
#define DES_H_
|
||||
|
||||
/* the FIPS 46-3 (1999-10-25) name for triple DES is triple data encryption algorithm so TDEA.
|
||||
* Also we only implement the three key mode */
|
||||
|
||||
/** \def tdea_enc
|
||||
* \brief defining an alias for void tdes_enc(void* out, const void* in, const void* key)
|
||||
*/
|
||||
|
||||
/** \def tdea_dec
|
||||
* \brief defining an alias for void tdes_dec(void* out, const void* in, const void* key)
|
||||
*/
|
||||
|
||||
#define tdea_enc tdes_enc
|
||||
#define tdea_dec tdes_dec
|
||||
|
||||
/** \fn void des_enc(void* out, const void* in, const void* key)
|
||||
* \brief encrypt a block with DES
|
||||
*
|
||||
* This function encrypts a block of 64 bits (8 bytes) with the DES algorithm.
|
||||
* Key expansion is done automatically. The key is 64 bits long, but note that
|
||||
* only 56 bits are used (the LSB of each byte is dropped). The input and output
|
||||
* blocks may overlap.
|
||||
*
|
||||
* \param out pointer to the block (64 bit = 8 byte) where the ciphertext is written to
|
||||
* \param in pointer to the block (64 bit = 8 byte) where the plaintext is read from
|
||||
* \param key pointer to the key (64 bit = 8 byte)
|
||||
*/
|
||||
void des_enc(void* out, const void* in, const void* key);
|
||||
|
||||
/** \fn void des_dec(void* out, const void* in, const void* key)
|
||||
* \brief decrypt a block with DES
|
||||
*
|
||||
* This function decrypts a block of 64 bits (8 bytes) with the DES algorithm.
|
||||
* Key expansion is done automatically. The key is 64 bits long, but note that
|
||||
* only 56 bits are used (the LSB of each byte is dropped). The input and output
|
||||
* blocks may overlap.
|
||||
*
|
||||
* \param out pointer to the block (64 bit = 8 byte) where the plaintext is written to
|
||||
* \param in pointer to the block (64 bit = 8 byte) where the ciphertext is read from
|
||||
* \param key pointer to the key (64 bit = 8 byte)
|
||||
*/
|
||||
void des_dec(void* out, const void* in, const void* key);
|
||||
|
||||
/** \fn void tdes_enc(void* out, const void* in, const void* key)
|
||||
* \brief encrypt a block with Tripple-DES
|
||||
*
|
||||
* This function encrypts a block of 64 bits (8 bytes) with the Tripple-DES (EDE)
|
||||
* algorithm. Key expansion is done automatically. The key is 192 bits long, but
|
||||
* note that only 178 bits are used (the LSB of each byte is dropped). The input
|
||||
* and output blocks may overlap.
|
||||
*
|
||||
* \param out pointer to the block (64 bit = 8 byte) where the ciphertext is written to
|
||||
* \param in pointer to the block (64 bit = 8 byte) where the plaintext is read from
|
||||
* \param key pointer to the key (192 bit = 24 byte)
|
||||
*/
|
||||
void tdes_enc(void* out, const void* in, const void* key);
|
||||
|
||||
/** \fn void tdes_dec(void* out, const void* in, const void* key)
|
||||
* \brief decrypt a block with Tripple-DES
|
||||
*
|
||||
* This function decrypts a block of 64 bits (8 bytes) with the Tripple-DES (EDE)
|
||||
* algorithm. Key expansion is done automatically. The key is 192 bits long, but
|
||||
* note that only 178 bits are used (the LSB of each byte is dropped). The input
|
||||
* and output blocks may overlap.
|
||||
*
|
||||
* \param out pointer to the block (64 bit = 8 byte) where the plaintext is written to
|
||||
* \param in pointer to the block (64 bit = 8 byte) where the ciphertext is read from
|
||||
* \param key pointer to the key (192 bit = 24 byte)
|
||||
*/
|
||||
void tdes_dec(void* out, const void* in, const void* key);
|
||||
|
||||
#endif /*DES_H_*/
|
|
@ -0,0 +1,13 @@
|
|||
# Makefile for DES
|
||||
ALGO_NAME := DES
|
||||
|
||||
# comment out the following line for removement of DES from the build process
|
||||
BLOCK_CIPHERS += $(ALGO_NAME)
|
||||
|
||||
$(ALGO_NAME)_DIR := des/
|
||||
$(ALGO_NAME)_OBJ := des.o
|
||||
$(ALGO_NAME)_INCDIR := bcal/
|
||||
$(ALGO_NAME)_TEST_BIN := main-des-test.o bcal_des.o $(CLI_STD) $(BCAL_STD)
|
||||
$(ALGO_NAME)_NESSIE_TEST := "nessie"
|
||||
$(ALGO_NAME)_PERFORMANCE_TEST := "performance"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
# Makefile for Triple-DES
|
||||
ALGO_NAME := TDES
|
||||
|
||||
# comment out the following line for removement of Triple-DES from the build process
|
||||
BLOCK_CIPHERS += $(ALGO_NAME)
|
||||
|
||||
$(ALGO_NAME)_DIR := des/
|
||||
$(ALGO_NAME)_OBJ := des.o
|
||||
$(ALGO_NAME)_INCDIR := bcal/
|
||||
$(ALGO_NAME)_TEST_BIN := main-tdes-test.o bcal_tdes.o bcal_tdes2.o $(CLI_STD) $(BCAL_STD)
|
||||
$(ALGO_NAME)_NESSIE_TEST := "nessie"
|
||||
$(ALGO_NAME)_PERFORMANCE_TEST := "performance"
|
||||
|
|
@ -0,0 +1,122 @@
|
|||
/* main-des-test.c */
|
||||
/*
|
||||
This file is part of the ARM-Crypto-Lib.
|
||||
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/*
|
||||
* des test-suit
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "config.h"
|
||||
#include "cli.h"
|
||||
#include "dump.h"
|
||||
#include "uart_lowlevel.h"
|
||||
#include "sysclock.h"
|
||||
#include "hw_gptm.h"
|
||||
|
||||
#include "des.h"
|
||||
#include "nessie_bc_test.h"
|
||||
#include "performance_test.h"
|
||||
#include "bcal-performance.h"
|
||||
#include "bcal_des.h"
|
||||
|
||||
char* algo_name = "DES";
|
||||
|
||||
void uart0_putc(char byte){
|
||||
uart_putc(UART_0, byte);
|
||||
}
|
||||
|
||||
char uart0_getc(void){
|
||||
return uart_getc(UART_0);
|
||||
}
|
||||
|
||||
const bcdesc_t* algolist[] = {
|
||||
(bcdesc_t*)&des_desc,
|
||||
NULL
|
||||
};
|
||||
/*****************************************************************************
|
||||
* additional validation-functions *
|
||||
*****************************************************************************/
|
||||
void des_init_dummy(const void* key, uint16_t keysize_b, void* ctx){
|
||||
memcpy(ctx, key, 8);
|
||||
}
|
||||
|
||||
void des_enc_dummy(void* buffer, void* ctx){
|
||||
des_enc(buffer, buffer, ctx);
|
||||
}
|
||||
|
||||
void des_dec_dummy(void* buffer, void* ctx){
|
||||
des_dec(buffer, buffer, ctx);
|
||||
}
|
||||
|
||||
void testrun_nessie_des(void){
|
||||
nessie_bc_init();
|
||||
nessie_bc_ctx.blocksize_B = 8;
|
||||
nessie_bc_ctx.keysize_b = 64;
|
||||
nessie_bc_ctx.name = algo_name;
|
||||
nessie_bc_ctx.ctx_size_B = 8;
|
||||
nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)des_enc_dummy;
|
||||
nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)des_dec_dummy;
|
||||
nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)des_init_dummy;
|
||||
|
||||
nessie_bc_run();
|
||||
}
|
||||
|
||||
|
||||
void testrun_performance_des(void){
|
||||
bcal_performance_multiple(algolist);
|
||||
}
|
||||
/*****************************************************************************
|
||||
* main
|
||||
*****************************************************************************/
|
||||
|
||||
const char nessie_str[] = "nessie";
|
||||
const char test_str[] = "test";
|
||||
const char performance_str[] = "performance";
|
||||
const char echo_str[] = "echo";
|
||||
|
||||
cmdlist_entry_t cmdlist[] = {
|
||||
{ nessie_str, NULL, testrun_nessie_des },
|
||||
{ test_str, NULL, testrun_nessie_des },
|
||||
{ performance_str, NULL, testrun_performance_des},
|
||||
{ echo_str, (void*)1, (void_fpt)echo_ctrl},
|
||||
{ NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
int main (void){
|
||||
sysclk_set_freq(SYS_FREQ);
|
||||
sysclk_mosc_verify_enable();
|
||||
uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE);
|
||||
gptm_set_timer_32periodic(TIMER0);
|
||||
|
||||
cli_rx = uart0_getc;
|
||||
cli_tx = uart0_putc;
|
||||
|
||||
for(;;){
|
||||
cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
|
||||
cli_putstr(algo_name);
|
||||
cli_putstr("; ");
|
||||
cli_putstr(__DATE__);
|
||||
cli_putc(' ');
|
||||
cli_putstr(__TIME__);
|
||||
cli_putstr(")\r\nloaded and running\r\n");
|
||||
cmd_interface(cmdlist);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
/* main-tdes-test.c */
|
||||
/*
|
||||
This file is part of the ARM-Crypto-Lib.
|
||||
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/*
|
||||
* tdes test-suit
|
||||
*
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "config.h"
|
||||
#include "cli.h"
|
||||
#include "dump.h"
|
||||
#include "uart_lowlevel.h"
|
||||
#include "sysclock.h"
|
||||
#include "hw_gptm.h"
|
||||
|
||||
#include "nessie_bc_test.h"
|
||||
#include "performance_test.h"
|
||||
#include "bcal-performance.h"
|
||||
#include "bcal_tdes.h"
|
||||
#include "bcal_tdes2.h"
|
||||
|
||||
char* algo_name = "TDES";
|
||||
|
||||
void uart0_putc(char byte){
|
||||
uart_putc(UART_0, byte);
|
||||
}
|
||||
|
||||
char uart0_getc(void){
|
||||
return uart_getc(UART_0);
|
||||
}
|
||||
|
||||
const bcdesc_t* algolist[] = {
|
||||
(bcdesc_t*)&tdes_desc,
|
||||
(bcdesc_t*)&tdes2_desc,
|
||||
NULL
|
||||
};
|
||||
|
||||
/*****************************************************************************
|
||||
* additional validation-functions *
|
||||
*****************************************************************************/
|
||||
void tdes_init_dummy(const void* key, uint16_t keysize_b, void* ctx){
|
||||
memcpy(ctx, key, 8*3);
|
||||
}
|
||||
|
||||
void tdes_enc_dummy(void* buffer, void* ctx){
|
||||
tdes_enc(buffer, buffer, ctx);
|
||||
}
|
||||
|
||||
void tdes_dec_dummy(void* buffer, void* ctx){
|
||||
tdes_dec(buffer, buffer, ctx);
|
||||
}
|
||||
|
||||
void testrun_nessie_tdes(void){
|
||||
nessie_bc_init();
|
||||
nessie_bc_ctx.blocksize_B = 8;
|
||||
nessie_bc_ctx.keysize_b = 192;
|
||||
nessie_bc_ctx.name = algo_name;
|
||||
nessie_bc_ctx.ctx_size_B = 8*3;
|
||||
nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)tdes_enc_dummy;
|
||||
nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)tdes_dec_dummy;
|
||||
nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)tdes_init_dummy;
|
||||
|
||||
nessie_bc_run();
|
||||
}
|
||||
|
||||
|
||||
void testrun_performance_tdes(void){
|
||||
bcal_performance_multiple(algolist);
|
||||
}
|
||||
/*****************************************************************************
|
||||
* main *
|
||||
*****************************************************************************/
|
||||
|
||||
const char nessie_str[] = "nessie";
|
||||
const char test_str[] = "test";
|
||||
const char performance_str[] = "performance";
|
||||
const char echo_str[] = "echo";
|
||||
|
||||
cmdlist_entry_t cmdlist[] = {
|
||||
{ nessie_str, NULL, testrun_nessie_tdes},
|
||||
{ test_str, NULL, testrun_nessie_tdes},
|
||||
{ performance_str, NULL, testrun_performance_tdes},
|
||||
{ echo_str, (void*)1, (void_fpt)echo_ctrl},
|
||||
{ NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
int main (void){
|
||||
sysclk_set_freq(SYS_FREQ);
|
||||
sysclk_mosc_verify_enable();
|
||||
uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE);
|
||||
gptm_set_timer_32periodic(TIMER0);
|
||||
|
||||
cli_rx = uart0_getc;
|
||||
cli_tx = uart0_putc;
|
||||
|
||||
for(;;){
|
||||
cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
|
||||
cli_putstr(algo_name);
|
||||
cli_putstr("; ");
|
||||
cli_putstr(__DATE__);
|
||||
cli_putc(' ');
|
||||
cli_putstr(__TIME__);
|
||||
cli_putstr(")\r\nloaded and running\r\n");
|
||||
cmd_interface(cmdlist);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue