<<

. 2
( 8)



>>

t16 = t32>>16, x = (x-t.16) + (x<t16) )
#else I* !AVOID-JUMPS (default) *I
#define MUL(x,y) \
((t16 = (y)) ?\
(x=lowl6(x)) ?\
t32 = (word32)x*t16, \
x = low16(t32), \
t16 = t32>>16, \
x = (x-t16)+(x<t16) \
:\
(x = l-t16) \
:\
(x = l-x))
#endif
#endif

static void




Page 639
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

IDEA


ideaciphercbyte *inbuf, byte *outbuf, word16 *key)

register uintl6 xl, x2, x3, x4, ˜2, ˜3;
word16 *in , *out;
#ifndef SMALL-CACHE
register uint.16 t16; I* Temporaries needed by MUL macro *I
register word32 t32;
#endif
int r = IDEAROUNDS;

in = (word16 *)inbuf;
x2 = *in++.
xl = *in++.
x3 = *in++; x4 = *in;
#ifndef HIGHFIRST
xl = (xl >>8) ( (x1<(8);
x2 = (x2 >>8) I (x2<(8);
x3 = (x3 >>8) I (x3<(8);
x4 = (x4 >>8) I (x4<<8);
#endif
do I
MUL(xl,*key++);
x2 += *key++;
x3 += *key++;
MUL(x4, *key++);

s3 = x3;
x3 A= xl;
MUL(x3, *key++);
s2 = x2;
x2 n= x4;
x2 += x3;
MUL(x2, *key++);
x3 += x2:

x4 A= x3;
xl A= x2;

x2 A= s3; x3 A= s2;
I while (--t-j;
MUL(xl, *key++);
x3 += *key++;
x2 += *key++;
MUL(x4, *key);

out = (word16 *)outbuf;
#ifdef HIGHFIRST
*out++ = xl;
*out++ = x3;
*out++ = x2;
*out = x4;
#else I* !HIGHFIRST *I
*out++ = (xl >>8) I (x1<<8);
*out++ = (x3 >>8) I (x3<<8);
*out++ = (x2 >>8) I (x2<(8);
*out = (x4 >>8) ( (x4<(8);




Page 640
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code


#endif
1 I* ideacipher *I

void idea-keycidea-ctx *c, unsigned char *key)1
ideaExpandKey(key,c->ek);
ideaInvertKey(c->ek,c->dk);
1

char *data, int blocks11
void idea-enccidea-ctx *c, unsigned
int i;
unsigned char *d = data;

for(i=O;i<blocks;i++)l
ideaCipher(d,d,c->ek)
d+=8;



char *data, int blocks){
void idea-deccidea-ctx *c, unsigned
int i;
unsigned char *d = data;

for(i=O;i<blocks;i++)l
ideaCipher(d,d,c->dk);
d+=8;



#include <stdio.h>

#ifndef BLOCKS
#ifndef KBYTES
#define KBYTES 1024
#endif
#define BLOCKS (64*KBYTES)
#endif

int
main(void)
I* Test driver for IDEA cipher *I
1
int i, j, k;
idea-ctx c;
byte userkeyLl61;
word16 EK[IDEAKEYLEN], DKrIDEAKEYLENI;
byte XXCEI, YYCEI, ZZ[El;
word32 long-block[lOl; I* 5 blocks *I
long 1;
char *lbp;

I* Make a sample user key for testing... */
for(i=O; i<16; i++)
userkeyCi1 = i+l;

idea-key(&c,userkey);

I* Make a sample plaintext pattern for testing... */




Page 641
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

GOST


for (k=O; k<8; k++)
XX[kl = k;

idea-enc(&c,XX,l); /* encrypt */

lbp = (unsigned char *) long-block;
for(i=O;i<lO;i++) long-block[il = i;
idea_enc(&c,lbp,5);
for(i=O;i<lO;i+=Z) printf("Block %Old = %081x %081x.\n",
i/2,long˜block[il,long˜block[i+ll);

idea_dec(&c,lbp,3);
idea_dec(&c,lbp+24,2);

for(i=O;i<lO;i+=2) printf("Block %Old = %081x %081x.\n",
i/2,long˜block[il,long˜block[i+ll);

/* normal exit */
return 0;
1 I* main *I



GOST
typedef unsigned long u4;
typedef unsigned char byte;

typedef struct 1
u4 k[81;
/* Constant s-boxes -- set up in gostLinit0. */
char k87[256l,k65[256l,k43[256l,k21[2561;
J gost-ctx;

encrypt and decrypt expect full blocks--padding blocks is
/* Note:
All bulk encryption is done in
caller's responsibility.
ECB mode by these calls. Other modes may be added easily
*I
enough.
void gost-enc(gost-ctx *, u4 *, int);
void gost-dec(gost-ctx *, u4 *, int);
void gost-keycgost-ctx *, u4 *);
void gost-initcgost-ctx *);
void gost-destroy(gost-ctx *);

-alpha /* Any other 64.bit machines? */
#ifdef
typedef unsigned int word32;
#else
typedef unsigned long word32;
#endif

kboxinit(gost-ctx *c)
t
int i;

byte k8[161 = (14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6,
9, 0, 7 I;
12, 5,
byte k7C161 = 115, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2,




Page 642
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code


13, 12, 0, 5, 10 );
byte k6[161 = 110, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12,
7, 11, 4, 2, 8 I;
byte k5[161 = 1 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8,
5, 11, 12, 4, 15 I;
byte k4[161 = ( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3,
15, 13, 0, 14, 9 I;
byte k3[161 = 112, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3,
4, 14, 7, 5, 11 1;
byte k2[161 = ( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9,
7, 5, 10, 6, 1 I;
k1[163 = 113, 2, 8, 4, 6, 15, 11, 1, 10,
byte 9, 3,
14, 5, 0, 12, 7 I;
for (i = 0; i < 256; i++) I
c->k87[i] = k8Ci >> 41 << 4 1 k7[i & 151;
c->k65[i] = k6[i >> 41 << 4 ( k5[i & 151;
c->k43[i] = k4[i >> 41 << 4 1 k3Ci & 151;
c->k2l[i] = k2[i >> 41 << 4 1 klCi & 151;



static word32
f(gost-ctx *c,word32 x)
r
x = c->k87[x>>24 & 2553 << 24 1 c->k65[x>>16 & 2551 << 16 1
c->k43[x>> 8 & 2551 << 8 I c->kPlCx & 2551;

/* Rotate left 11 bits */
return x<<ll I x>>(32-11);


void gostcrypt(gost-ctx *c, word32 *d)(
register word32 nl, n2; /* As named in the GOST */

nl = d[Ol;
n2 = d[ll;

/* Instead of swapping halves, swap names each round */
n2 A= f(c,nl+c->k[O]); nl I\= f(c,nZ+c->k[ll);
n2 A= f(c,nl+c->k[21); nl h= f(c,n2+c->k[31);
n2 h= f(c,nl+c->k[41); nl II= f(c,n2+c->k[51);
n2 h= f(c,nl+c->k[6]); nl h= f(c,n2+c->k[71);

n2 I\= f(c,nl+c->k[O]); nl h= f(c,n2+c->k[ll);
n2 A= f(c,nl+c->k[2]); nl n= f(c,n2+c->k[31);
n2 h= f(c,nl+c->k[4]); nl h= f(c,n2+c->k[51);
n2 h= f(c,nl+c->k[6]); nl I\= f(c,n2+c->k[71):

n2 h= f(c,nl+c->k[O]); nl I\= f(c,n2+c->k[ll
n2 A= f(c,nl+c->k[21); nl A= f(c,n2+c->k[31
n2 h= f(c,nl+c->k[4]); nl h= f(c,n2+c->k[51
n2 h= f(c,nl+c->kC61); nl h= f(c,n2+c->k[71

n2 h= f(c,nl+c->k[71); nl n= f(c,n2+c->k[61
n2 A= f(c,nl+c->k[51); nl n= f(c,n2+c->k[41




Page 643
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

GOST


n2 n= f(c,nl+c->k[3]); nl h= f(c,n2+c->k[21);
n2 I\= f(c,nl+c->k[ll); nl A= f(c,nZ+c->k[Ol);

d[Ol = n2; d[ll = nl;


void
gostdecrypt(gostLctx *c, u4 *d)(
register word32 nl, n2; /* As named in the GOST */

nl = d[Ol; n2 = d[ll;

n2 A= f(c,nl+c->k[O]); nl A= f(c,n2+c->k[ll);
n2 n= f(c,nl+c->k[21); nl h= f(c,nZ+c->k[31);
n2 n= f(c,nl+c->k[4]); nl A= f(c,nZ+c->k[51);
n2 A= f(c,nl+c->k[61); nl I\= f(c,nZ+c->kC71);

n2 A= f(c,nl+c->k[7]); nl A= f(c,n2+c->k[61);
n2 I\= f(c,nl+c->k[51); nl n= f(c,n2+c->k[41);
n2 n= f(c,nl+c->k[31); nl h= f(c,n2+c->k[21);
n2 A= f(c,nl+c->k[l]); nl h= f(c,n2+c->k[Ol);

n2 A= f(c,nl+c->k[7]); nl n= f(c,n2+c->k[61);
n2 h= f(c,nl+c->k[5]); nl I\= f(c,n2+c->k[41);
n2 A= f(c,nl+c->k[31); nl I\= f(c,n2+c->kC21);
n2 h= f(c,nl+c->k[ll); nl h= f(c,n2+c->k[Ol);

n2 h= f(c,nl+c->k[7]); nl A= f(c,n2+c->k[61);
n2 n= f(c,nl+c->k[5]); nl A= f(c,n2+c->k[41);
n2 n= f(c,nl+c->k[3]); nl h= f(c,n2+c->k[21);
n2 n= f(c,nl+c->k[l]); nl n= f(c,n2+c->k[Ol);

d[Ol = n2; d[ll = nl;


void gost-enc(gost-ctx *c, u4 *d, int blocks)1
int i;

for(i=O;i<blocks;i++)I
gostcrypt(c,d);
d+=2;



void gost-dec(gost-ctx *c, u4 *d, int blocks){
int i;

for(i=O;i<blocks;i++)I
gostdecrypt(c,d);
d+=2;



void gost-key(gost-ctx *c, ˜4 *k)l
int i;
for(i=O;i<8;i++) c->k[il=kCil;




Page 644
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code




void gost-initcgost-ctx *c)l
kboxinit(c);


void gost-destroy(gost-ctx *c)l
int i;
for(i=O;i<8;i++) c->k[il=O;
1

void main(void)l
gost-ctx gc;
u4 k[8l,data[lOl;
int i:

/* Initialize GOST context. */
gost-init(&gc);

/* Prepare key--a simple key should be OK, with this many rounds! */
for(i=O;i<8;i++) k[i] = i;
gost-key(&gc,k);

/* Try some test vectors. */
data[Ol = 0; dataCl1 = 0;
gostcrypt(&gc,data);
printf("Enc of zero vector: %081x %08lx\n",data[0l,data[ll);
gostcrypt(&gc,data);
printf("Enc of above: %081x %08lx\n",data[0l,data[13);
data[Ol = Oxffffffff; dataCl1 = Oxffffffff;
gostcrypt(&gc,data);
printf("Enc of ones vector: %081x %081x\n",data[0l,data[ll);
gostcrypt(&gc,data);
printf("Enc of above: %081x %08lx\n",data[0l,data[ll);

/* Does gost-deco properly reverse gost-encO? Do
we deal OK with single-block lengths passed in gostLdecO?
Do we deal OK with different lengths passed in? */

/* Init data *I
for(i=O;i<lO;i++) data[il=i;

/* Encrypt data as 5 blocks. */
gost_enc(&gc,data,5);

/* Display encrypted data. */
for(i=O;i<lO;i+=2) printf("Block %02d = %081x %081x\n",
i/2,data[il,data[i+ll);

/* Decrypt in different sized chunks. */
gost-dec(&gc,data,l);
gost_dec(&gc,data+2,4);
printf("\n");

/* Display decrypted data. */




Page 645
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page




for(i=O;i<lO;i+=2) printf("Block %02d = %081x %OElx\n",
i/2,data[il,data[i+ll);

gost-destroy(&gc);




BLOWFISH
#include <math.h>
#include <stdio.h>
#include <stdlib.h:
#include <time.h>

#ifdef little-endian /* Eg: Intel */
#include <alloc.h>
#endif

#include <ctype.h>

#ifdef little-endian /* Eg: Intel */
//include <dir.h>
#include <bios.h>
#endif

#ifdef big-endian
#include <Types.h>
#endif

typedef struct 1
unsigned long S[41[2561,P[181;
I blf-ctx; .

#define MAXKEYBYTES 56 /* 448 bits */
// #define little-endian 1 /* Eg: Intel */
#define big-endian 1 /* Eg: Motorola */

void Blowfish-encipher(blf_ctx *,unsigned long *xl, unsigned long *xr);
void Blowfish-decipher(blf_ctx *,unsigned long *xl, unsigned long *xr);

#define N 16
#define noErr 0
#define DATAERROR -1
#define KEYBYTES 8

FILE* SubkeyFile;

unsigned long F(blf-ctx *bc, unsigned long x1
I
unsigned short a;
unsigned short b;
unsigned short c;
unsigned short d;
unsigned long y;




Page 646
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code


d =x & OxOOFF;
x >>= 8;
c =x & OxOOFF;
x >>= 8;
b =x & OxOOFF;
x >>= 8;
a =x & OxOOFF;
l/y = ((SCOl[al + S[llCbl) A S[21[cl) + S[3l[dl;
y = bc->S[Ol[al + bc->SCll[bl;
A bc->S[21[cl;
Y=Y
y = y + bc->S[3l[dl;

return y;


void Blowfish-encipher(blf_ctx *c,unsigned long *xl, unsigned long *xr)
(
unsigned long Xl;
unsigned long Xr;
unsigned long temp;
short i;

Xl = *x1;
Xr = *xr;

for (i = 0; i < N; ++i) I
Xl = Xl A c->P[il;
Xr = F(c,Xl) h Xr;

temp = Xl;
Xl = Xr;
Xr = temp;


temp = Xl;
Xl = Xr;
Xr = temp;

Xr = Xr h c->PCNl;
Xl = Xl n c->P[N + 11;

*x1 = Xl;
*xr = Xr:


void Blowfish-decipher(blf_ctx *c, unsigned long *xl, unsigned long *xr)
I
unsigned long Xl;
unsigned long Xr;
unsigned long temp;
short i;

Xl = *x1;
Xr = *xr;




Page 647
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

BLOWFISH


for (i = N + 1; i > 1; --i) {
Xl = Xl h c->P[i];
Xr = F(c,Xl) h Xr;

/* Exchange Xl and Xr */
temp = Xl;
Xl = Xr;
Xr = temp;


/* Exchange Xl and Xr */
temp = Xl;
Xl = Xr;
Xr = temp;

Xr = Xr h c->P[ll;
Xl = Xl A c->PCOl;

*x1 = Xl;
*xr = Xr:


short InitializeBlowfishCblf-ctx *c, char key[l, short keybytes)
f
i;
short
J;
short
short k;
short error;
short numread;
unsigned long data;
unsigned long datal;
unsigned long datar;

unsigned long ksO[l = {
Oxd1310ba6, Ox98dfb5ac, Ox2ffd72db, OxdOladfb7, Oxb8elafed, Ox6a267e96,
Oxba7c9045, Oxf12c7f99, Ox24a19947, Oxb3916cf7, Ox0801f2e2, Ox858efc16,
Ox636920d8, Ox71574e69, Oxa458fea3, Oxf4933d7e, OxOd95748f, Ox728eb658,
Ox718bcd58, Ox82154aee, Ox7b54a41d, Oxc25a59b5, Ox9c30d539, Ox2af26013,
Oxc5dlb023, Ox286085f0, Oxca417918, Oxb8db38ef, OxEe79dcb0, Ox603a180e,
Ox6c9eOe8b, OxbOle8a3e, Oxd71577c1, Oxbd314b27, Ox78af2fda, 0x55605˜60,
Oxe65525f3, Oxaa55ab94, 0x57489862, Ox63e81440, Ox55ca396a, Ox2aablOb6,
Ox1141e8ce,
Oxb4cc5c34, Oxa15486af, Ox7c72e993, Oxb3ee1411, Ox636fbc2a,
Ox2ba9c55d, Ox741831f6, Oxce5c3e16, Ox9b87931e, Oxafd6ba33, Ox6c24cf5c,
Ox7a325381, 0x28958677, Ox3b8f4898, Ox6b4bb9af, Oxc4bfe8lb, 0x66282193,
Ox61d809cc, Oxfb21a991, Ox487cac60, Ox5dec8032, Oxef845d5d, Oxe98575b1,
Oxdc262302, Oxeb651b88, Ox23893e81, Oxd396acc5, OxOf6d6ff3, Ox83f44239,
Ox2eOb4482, Oxa4842004, Ox69cEf04a, Ox9elf9b5e, 0x21˜66842, Oxf6e96c9a,
Ox67Oc9c61, Oxabd388f0, Ox6a51aOd2, Oxd8542f68, Ox960fa728, Oxab5133a3,
Ox6eefObGc, Ox137a3be4, Oxba3bf050, Ox7efb2a98, Oxalf1651d, Ox39af0176,
Ox66ca593e, Ox82430e88, Ox8cee8619, Ox456f9fb4, Ox7d84a5c3, Ox3b8b5ebe,
Oxe06f75d8, 0x85˜12073, Ox401a449f, Ox56c16aa6, Ox4ed3aa62, Ox363f7706,
Oxlbfedf72, Ox429b023d, Ox37dOd724, OxdOOa1248, OxdbOfead3, Ox49flc09b,
OxO75372c9, Ox80991b7b, Ox25d479d8, Oxf6eEdef7, Oxe3fe501a, Oxb6794c3b,




Page 648
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

Source Code


Ox976ceObd, Ox04c006ba, Oxcla94fb6, Ox409f60c4, Ox5e5c9ec2, Ox196a2463,
Ox68fb6faf, Ox3e6c53b5, Ox1339b2eb, Ox3b52ec6f, Ox6dfc511f, Ox9b30952c,
0˜˜˜814544, OxafSebd09, Oxbee3d004, Oxde334afd, Ox660f2807, Ox192e4bb3,
OxcOcba857, Ox45c8740f, Oxd20b5f39, Oxb9d3fbdb, Ox5579cObd, Oxla60320a,
Oxd6alOOc6, Ox402c7279, Ox679f25fe, Oxfblfa3cc, Ox8ea5e9f8, Oxdb3222f8,
Ox3c7516df, Oxfd616b15, Ox2f501ec8, Oxad0552ab, Ox323db5fa, Oxfd238760,
Ox53317b48, Ox3eOOdf82, Ox9e5c57bb, OxcaGf8ca0, Oxla87562e, Oxdf1769db,
Oxd542a8f6, Ox287effc3, Oxac6732c6, OxEc4f5573, Ox695b27b0, Oxbbca58c8,
Oxelffa35d, Oxb8fOlla0, OxlOfa3d98, Oxfd2183b8, Ox4afcb56c, Ox2ddld35b,
Ox9a53e479, Oxb6f84565, Oxd28e49bc, Ox4bfb9790, Oxelddf2da, Oxa4cb7e33,
Ox62fb1341, Oxcee4c6e8, Oxef20cada, 0x36774˜01, Oxd07e9efe, Ox2bfllfb4,
Ox95dbda4d, Oxae909198, Oxeaad8e71, Ox6b93d5a0, OxdOEedldO, Oxafc725e0,
Ox8e3c5b2f, Ox8e7594b7, Ox8ff6e2fb, Oxf2122b64, Ox8888b812, Ox900dfOlc,
Ox4fad5ea0, Ox688fc31c, Oxdlcffl91, Oxb3a8clad, Ox2f2f2218, OxbeOe1777,
Oxea752dfe, Ox8b021fa1, Oxe5aOccOf, Oxb56f74e8, Ox18acf3d6, Oxce89e299,
Oxb4a84fe0, Oxfd13eOb7, Ox7cc43b81, Oxd2adaEd9, Ox165fa266, 0x80957705,
Ox93cc7314, Ox211a1477, Oxe6ad2065, Ox77b5fa86, Oxc75442f5, Oxfb9d35cf,
OxebcdafOc, Ox7b3e89a0, Oxd6411bd3, Oxaele7e49, Ox00250e2d, Ox2071b35e,
Ox226800bb, Ox57b8eOaf, Ox2464369b, Oxf009b91e, Ox5563911d, Ox59dfa6aa,
Ox78c14389, Oxd95a537f, Ox207d5ba2, Ox02e5b9c5, 0x83260376, Ox6295cfa9,
Oxllc81968, Ox4e734a41, Oxb3472dca, Ox7b14a94a, Oxlb510052, Ox9a532915,
Oxd60f573f, Oxbc9bc6e4, Ox2b60a476, Ox81e67400, Ox08ba6fb5, Ox571be91f,
Oxf296ec6b, Ox2aOdd915, Oxb6636521, Oxe7b9f9b6, Oxff34052e, 0˜˜5855664,
Ox53b02d5d, Oxa99f8fa1, Ox08ba4799, Ox6e85076al;
unsigned long ksl[l =I
Ox4b7a70e9, Oxb5b32944, Oxdb75092e, Oxc4192623, OxadGeaGbO, Ox49a7df7d,
Ox9cee60b8, Ox8fedb266, Oxecaa8c71, Ox699a17ff, Ox5664526c, OxcPbl9ee1,
Ox193602a5, Ox75094c29, Oxa0591340, Oxe4183a3e, Ox3f54989a, Ox5b429d65,
Ox6b8fe4d6, Ox99f73fd6, Oxald29c07, Oxefe830f5, Ox4d2d38e6, Oxf0255dc1,
Ox4cdd2086, Ox8470eb26, Ox6382e9c6, Ox021ecc5e, Ox09686b3f, Ox3ebaefc9,
Ox3c971814, Ox6b6a70a1, Ox687f3584, Ox52aOe286, Oxb79c5305, Oxaa500737,
Ox3e07841c, Ox7fdeae5c, Ox8e7d44ec, Ox5716f2b8, Oxb03ada37, Oxf0500cOd,
OxfOlclf04, Ox0200b3ff, OxaeOcf51a, Ox3cb574b2, Ox25837a58, Oxdc0921bd,
Oxd19113f9, Ox7ca92ff6, 0x94324773, Ox22f54701, Ox3ae5e581, Ox37c2dadc,
Oxc8b57634, Ox9af3dda7, Oxa9446146, OxOfd0030e, Oxecc8c73e, Oxa4751e41,
Oxe238cd99, Ox3beaOe2f, Ox3280bba1, Ox183eb331, Ox4e548b38, Ox4f6db908,
Ox6f420d03, Oxf60a04bf, Ox2cb81290, Ox24977c79, Ox5679b072, Oxbcaf89af,
Oxde9a771f, Oxd9930810, Oxb38bae12, Oxdccf3f2e, Ox5512721f, Ox2e6b7124,
Ox501adde6, Ox9f84cd87, Ox7a584718, Ox7408da17, Oxbcgfgabc, Oxe94b7d8c,
Oxec7aec3a, Oxdb85ldfa, 0x63094366, Oxc464c3d2, Oxeflc1847, Ox3215d908,
Oxdd433b37, Ox24c2ba16, Ox12a14d43, Ox2a65c451, 0x50940002, Ox133ae4dd,
Ox71dff89e, Ox10314e55, Ox81ac77d6, Ox5f11199b, Ox043556f1, Oxd7a3c76b,
Ox3c11183b, Ox5924a509, Oxf28fe6ed, Ox97flfbfa, OxgebabfEc, Oxle153c6e,
Ox86e34570, Oxeae96fb1, Ox860e5eOa, Ox5a3e2ab3, Ox771fe71c, Ox4e3d06fa,
Ox2965dcb9, Ox99e71dOf, Ox803e89d6, 0x5266˜825, Ox2e4cc978, Ox9clOb36a,
Oxc6150eba, Ox94e2ea78, Oxa5fc3c53, OxleOa2df4, Oxf2f74ea7, Ox361d2b3d,
Ox1939260f, Ox19c27960, Ox5223a708, Oxf71312b6, Oxebadfe6e, Oxeac31f66,
Oxe3bc4595, Oxa67bc883, Oxb17f37d1, OxOlEcff28, Oxc332ddef, Oxbe6c5aa5,
0x65582185, Ox68ab9802, Oxeecea50f, Oxdb2f953b, OxPaef7dad, Ox5b6e2f84,
Ox1521b628, 0x29076170, Oxecdd4775, Ox619f1510, Ox13cca830, Oxeb61bd96,
Ox0334fele, Oxaa0363cf, Oxb5735c90, Ox4c70a239, Oxd59e9eOb, Oxcbaadel4,
Oxeecc86bc, Ox60622ca7, Ox9cab5cab, Oxb2f3846e, Ox648bleaf, Ox19bdfOca,
Oxa02369b9, Ox655abb50, Ox40685a32, Ox3c2ab4b3, Ox319ee9d5, Oxc021b8f7,
Ox9b540b19, Ox875fa099, Ox95f7997e, Ox623d7da8, Oxf837889a, Ox97e32d77,




Page 649
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

BLOWFISH


Oxlled935f, 0x16681281, OxOe358829, Oxc7e61fd6, Ox96dedfa1, Ox7858ba99,
Ox57f584a5, Oxlb227263, Ox9b83c3ff, Oxlac24696, Oxcdb30aeb, Ox532e3054,
OxEfd948e4, Ox6dbc3128, Ox58ebf2ef, Ox34c6ffea, Oxfe28ed61, Oxee7c3c73,
Ox5d4a14d9, Oxe864b7e3, Ox42105d14, Ox203e13e0, Ox45eee2b6, Oxa3aaabea,
Oxdb6c4f15, Oxfacb4fd0, Oxc742f442, Oxef6abbb5, Ox654f3bld, Ox41cd2105,
Oxd81e799e, Ox86854dc7, Oxe44b476a, Ox3d816250, Oxcf62alf2, Ox5b8d2646,
Oxfc8883a0, Oxclc7b6a3, Ox7f1524c3, Ox69cb7492, Ox47848aOb, Ox5692b285,
Ox095bbf00, Oxad19489d, Ox1462b174, Ox23820e00, Ox58428d2a, OxOc55f5ea,
Oxldadf43e, Ox233f7061, Ox3372f092, Ox8d937e41, Oxd65fecf1, Ox6c223bdb,
Ox7cde3759, Oxcbee7460, Ox4085f2a7, Oxce77326e, Oxa6078084, Ox19f8509e,
Oxe8efd855, Ox61d99735, Oxa969a7aa, 0˜˜50˜06˜2, Ox5a04abfc, Ox800bcadc,
Ox9e447a2e, 0˜˜3453484, Oxfdd56705, OxOele9ec9, Oxdb73dbd3, Ox105588cd,
Ox675fda79, Oxe3674340, 0˜˜5˜43465, Ox713e38d8, Ox3d28f89e, Oxf16dff20,
Ox153e21e7, OxEfb03d4a, Oxe6e39f2b, Oxdb83adf71;
unsigned long ks2[1 = {
Oxe93d5a68, Ox948140f7, Oxf64c261c, 0x94692934, Ox411520f7, Ox7602d4f7,
Oxbcf46b2e, Oxd4a20068, Oxd4082471, Ox3320f46a, Ox43b7d4b7, Ox500061af,
Oxle39f62e, 0x97244546, Ox14214f74, Oxbf8b8840, Ox4d95fcld, Ox96b591af,
Ox70f4ddd3, Ox66a02f45, Oxbfbc09ec, Ox03bd9785, Ox7facGdd0, Ox31cb8504,
Ox96eb27b3, Ox55fd3941, Oxda2547e6, OxabcaOaga, 0x28507825, Ox530429f4,
OxOa2c86da, Oxe9b66dfb, Ox68dc1462, Oxd7486900, Ox680ecOa4, Ox27a18dee,
Ox4f3ffea2, Oxe887ad8c, Oxb58ce006, Ox7af4d6b6, Oxaacele7c, Oxd3375fec,
Oxce78a399, Ox406b2a42, Ox20fe9e35, Oxd9f385b9, Oxee39d7ab, Ox3b124e8b,
Oxldc9faf7, Ox4b6d1856, Ox26a36631, Oxeae397b2, Ox3a6efa74, Oxdd5b4332,
Ox6841e7f7, Oxca7820fb, OxfbOaf54e, OxdEfeb397, Ox454056ac, Oxba489527,
Ox55533a3a, Ox20838d87, Oxfe6ba9b7, Oxd096954b, Ox55a867bc, Oxa1159a58,
Oxcca92963, Ox99eldb33, Oxa62a4a56, Ox3f3125f9, Ox5ef47elc, Ox9029317c,
Oxfdf8e802, Ox04272f70, Ox80bb155c, Ox05282ce3, Ox95c11548, Oxe4c66d22,
Ox48c1133f, Oxc70f86dc, Ox07f9c9ee, Ox41041fOf, Ox404779a4, Ox5d886e17,
Ox325f51eb, Oxd59bcOd1, OxfPbccl8f, 0x41113564, Ox257b7834, Ox602a9c60,
Oxdff8e8a3, Oxlf636clb, OxOe12b4c2, Ox02e1329e, Oxaf664fd1, Oxcad18115,
Ox6b2395e0, Ox333e92e1, Ox3b240b62, Oxeebeb922, Ox85b2a20e, Oxe6baOd99,
Oxde720c8c, Ox2da2f728, Oxd0127845, Ox95b794fd, Ox647d0862, Oxe7ccf5f0,
Ox5449a36f, Ox877d48fa, Oxc39dfd27, Oxf33e8dle, OxOa476341, Ox992eff74,
Ox3a6f6eab, Oxf4f8fd37, Oxa812dc60, Oxalebddf8, Ox991be14c, Oxdb6e6bOd,
Oxc67b5510, Ox6d672c37, Ox2765d43b, OxdcdOe804, Oxf1290dc7, OxccOOffa3,
Oxb5390f92, Ox690fedOb, Ox667b9ffb, Oxcedb7d9c, Oxa091cfOb, Oxd9155ea3,
Oxbb132f88, Ox515bad24, Ox7b9479bf, Ox763bd6eb, Ox37392eb3, Oxcc115979,
Ox8026e297, Oxf42e312d, Ox6842ada7, Oxc66a2b3b, Ox12754ccc, Ox782efllc,
Ox6a124237, Oxb79251e7, Ox06albbe6, Ox4bfb6350, Oxla6b1018, Oxllcaedfa,
Ox3d25bdd8, Oxe2elc3c9, 0x44421659, OxOa121386, Oxd90cecGe, Oxd5abeaPa,
Ox64af674e, Oxda86a85f, Oxbebfe988, Ox64e4c3fe, Ox9dbc8057, OxfOf7c086,
Ox60787bf8, Ox6003604d, Oxdlfd8346, Oxf6381fb0, Ox7745ae04, Oxd736fccc,
Ox83426b33, OxfOleab71, Oxb0804187, Ox3c005e5f, Ox77a057be, Oxbde8ae24,
0x55464299, Oxbf582e61, Ox4e58f48f, Oxf2ddfda2, Oxf474ef38, Ox8789bdc2,
Ox5366f9c3, Oxc8b38e74, Oxb475f255, Ox46fcd9b9, Ox7aeb2661, Ox8blddf84,
Ox846aOe79, Ox915f95e2, Ox466e598e, Ox20b45770, Ox8cd55591, Oxc902de4c,
Oxb90bace1, Oxbb8205d0, Oxlla86248, Ox7574a99e, Oxb77f19b6, OxeOa9dc09,
Ox662d09a1, 0˜˜4324633, Oxe85alf02, Ox09fObe8c, Ox4a99a025, Oxld6efe10,
Oxlab93dld, OxOba5a4df, Oxa186f20f, Ox2868f169, Oxdcb7da83, Ox573906fe,
OxalePcegb, Ox4fcd7f52, Ox50115e01, Oxa70683fa, Oxa002b5c4, OxOde6d027,
Ox9af88c27, Ox773f8641, 0˜˜3604˜06, Ox61a806b5, Oxf0177a28, OxcOf586e0,
Ox006058aa, Ox30dc7d62, Oxlle69ed7, Ox2338ea63, Ox53c2dd94, 0˜˜2˜21634,
Oxbbcbee56, Ox90bcb6de, Oxebfc7da1, Oxce591d76, Ox6f05e409, Ox4b7c0188,




Page 650
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code


Ox39720a3d, Ox7c927c24, Ox86e3725f, Ox724d9db9, Oxlac15bb4, Oxd39eb8fc,
Oxed545578, Ox08fca5b5, Oxd83d7cd3, Ox4dadOfc4, Oxle50ef5e, Oxb161e6f8,
Oxa28514d9, Ox6c51133c, Ox6fd5c7e7, Ox56e14ec4, Ox362abfce, Oxddc6c837,
Oxd79a3234, 0x92638212, Ox670efa8e, Ox406000eOt;
unsigned long ks3[1 = {
Ox3a39ce37, Oxd3faf5cf, Oxabc27737, Ox5ac52dlb, Ox5cb0679e, Ox4fa33742,
Oxd3822740, Ox99bc9bbe, Oxd5118e9d, OxbfOf7315, Oxd62dlc7e, Oxc700c47b,
Oxb78clb6b, Ox21a19045, Oxb26eblbe, Ox6a366eb4, Ox5748ab2f, Oxbc946e79,
Oxc6a376d2, Ox6549c2c8, Ox530ff8ee, Ox468dde7d, Oxd5730ald, Ox4cd04dc6,
Ox2939bbdb, Oxa9ba4650, Oxac9526e8, Oxbe5ee304, Oxalfad5f0, Ox6a2d519a,
Ox63ef8ce2, Ox9a86ee22, Oxc089c2b8, Ox43242ef6, Oxa51e03aa, Ox9cf2dOa4,
Ox83c061ba, Ox9be96a4d, Ox8fe51550, Oxba645bd6, Ox2826a2f9, Oxa73a3ae1,
Ox4ba99586, Oxef5562e9, Oxc72fefd3, Oxf752f7da, Ox3f046f69, Ox77faOa59,
Ox80e4a915, Ox87b08601, Ox9b09e6ad, Ox3b3ee593, Oxe990fd5a, Ox9e34d797,
Ox2cfOb7d9, Ox022bEb51, Ox96d5ac3a, Ox017da67d, Oxdlcf3ed6, Ox7c7d2d28,
Oxlf9f25cf, Oxadf2b89b, Ox5ad6b472, Ox5a88f54c, Oxe029ac71, Oxe019a5e6,
Ox47bOacfd, Oxed93fa9b, Oxe8d3c48d, Ox283b57cc, Oxf8d56629, Ox79132e28,
Ox785f0191, Oxed756055, Oxf7960e44, Oxe3d35e8c, Ox15056dd4, OxEEf46dba,
Ox03a16125, Ox0564fObd, Oxc3eb9e15, Ox3c9057a2, Ox97271aec, Oxa93a072a,
Oxlb3f6d9b, Oxle6321f5, Oxf59c66fb, Ox26dcf319, Ox7533d928, Oxb155fdf5,
0x03563482, Ox8aba3cbb, 0x28517711, Oxc20ad9f8, Oxabcc5167, Oxccad925f,
Ox4de81751, Ox3830dc8e, Ox379d5862, Ox9320f991, Oxea7a90c2, Oxfb3e7bce,
Ox5121ce64, Ox774fbe32, OxaEb6e37e, Oxc3293d46, Ox48de5369, Ox6413e680,
Oxa2ae0810, Oxdd6db224, Ox69852dfd, 0x09072166, Oxb39a460a, Ox6445cOdd,
Ox586cdecf, Oxlc20c8ae, Ox5bbef7dd, Oxlb588d40, Oxccd2017f, Ox6bb4e3bb,
Oxdda26a7e, Ox3a59ff45, Ox3e350a44, Oxbcb4cdd5, Ox72eacea8, Oxfa6484bb,
Ox8d6612ae, Oxbf3c6f47, Oxd29be463, Ox542f5d9e, Oxaec2771b, Oxf64e6370,
Ox740eOd8d, Oxe75b1357, Oxf8721671, Oxaf537d5d, Ox4040cb08, Ox4eb4e2cc,
Ox34d2466a, Ox0115af84, Oxelb00428, Ox95983ald, Ox06b89fb4, Oxce6ea048,
OxOllald4b,
Ox6f3f3b82, Ox3520ab82, Ox277227f8, Ox611560b1, Oxe7933fdc,
Oxbb3a792b, Ox344525bd, Oxa08839e1, Ox51ce794b, Ox2f32c9b7, DxaOlfbac9,
OxeOlcc87e, Oxbcc7dlf6, OxcfOlllc3, Oxale8aac7, Oxla908749, Oxd44fbd9a,
OxdOdadecb, Oxd50ada38, Ox0339c32a, Oxc6913667, Ox8df9317c, OxeOb12b4f,
Oxf79e59b7, Ox43f5bb3a, Oxf2d519ff, Ox27d9459c, Oxbf97222c, Ox15e6fc2a,
OxOf91fc71, Ox9b941525, Oxfae59361, Oxceb69ceb, Oxc2a86459, OxlPbaa8d1,
Oxb6c1075e, Oxe3056aOc, OxlOd25065, Oxcb03a442, OxeOec6eOe, Ox1698db3b,
Ox4c98aObe, Ox3278e964, Ox9flf9532, OxeOd392df, Oxd3a0342b, Ox8971f21e,
OxlbOa7441, Ox4ba3348c, Oxc5be7120, Oxc37632d8, Oxdf359f8d, Ox9b992f2e,
Oxe60b6f47, OxOfe3flld, Oxe54cda54, Oxledad891, Oxce6279cf, Oxcd3e7e6f,
Ox1618b166, Oxfd2cld05, Ox848fd2c5, Oxf6fb2299, Oxf523f357, Oxa6327623,
Ox93a83531, Ox56cccd02, Oxacf08162, Ox5a75ebb5, Ox6e163697, Ox88d273cc,
Oxde966292, Ox81b949d0, Ox4c50901b, 0x71˜65614, Oxe6c6c7bd, Ox327a140a,
Ox45eld006, Oxc3f27b9a, Oxc9aa53fd, Ox62a80f00, Oxbb25bfe2, Ox35bdd2f6,
0x71126905, Oxb2040222, Oxb6cbcf7c, Oxcd769c2b, Ox53113ec0, Ox1640e3d3,
Ox38abbd60, Ox2547adf0, Oxba38209c, Oxf746ce76, Ox77afalc5, 0x20756060,
Ox85cbfe4e, Ox8ae88dd8, Ox7aaaf9b0, Ox4cf9aa7e, Ox1948c25c, Ox02fb8a8c,
OxOlc36ae4, OxdGebelf9, Ox90d4f869, Oxa65cdea0, Ox3f09252d, Oxc208e69f,
Oxb74e6132, Oxce77e25b, Ox578fdfe3, Ox3ac372e6);

/* Initialize s-boxes without file read. */
for(i=O;i<256;i++){
c->S[Ol[il = ksO[il;
c->S[ll[i] = ksl[il;
c->S[P][i] = ks2[il;




Page 651
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
BLOWFISH


c->S[3][il = ks3Cil;


j = 0;
for (i = 0; i < N + 2; ++i) 1
data = 0x00000000;
for (k = 0; k < 4; ++k) 1 '
data = (data << 8) ( key[jl;
j=j+l;
if (j >= keybytes) 1
j = 0;
1
I
c->P[il = c->P[il n data;
1

data1 = 0x00000000;
datar = 0x00000000;

for (i = 0; i < N + 2; i += 2) 1
Blowfish-encipher(c,&datal, &datar);

c->P[il = datal;
c->P[i + 11 = datar;
I

for (i = 0; i < 4; ++i) I
for Cj = 0; j < 256; j += 2) 1

Blowfish-encipher(c,&datal, &datar);

c->S[il[jl = datal;
c->S[il[j + 11 = datar;




void blf-key(blf-ctx *c, char *k, int lenI
InitializeBlowfish(c,k,len);


void blf-enc(blf-ctx *c, unsigned long *data, int blocks){
unsigned long *d;
int i;

d = data;
for(i=O;i<blocks;i++){
Blowfish-encipher(c,d,d+l);
d += 2;
1


void blf-dec(blf-ctx *c, unsigned long *data, int blocks){
unsigned long *d;
int i;




Page 652
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page

Source Code


d = data;
for(i=O;i<blocks;i++)1
Blowfish-decipher(c,d,d+l);
d += 2;



void main(void)i
blf-ctx c;
char key[]="AAAAA";
unsigned long data[lOl;
int i;

for(i=O;i<lO;i++) data[il = i;

blf_key(&c,key,5);
blf_enc(&c,data,5);
blf_dec(&c,data,l);
blf_dec(&c,data+2,4);
for(i=O;i<lO;i+=2) printf("Block %Old decrypts to: %081x %081x.\n",
i/2,data[il,data[i+lI);
t



3-Way
OxObOb /* round constant of first encryption round */
#define STRT-E
#define STRTLD Oxblbl /* round constant of first decryption round */
*I
NMBR 11 /* number of rounds is 11
#define

typedef unsigned long int word32 ;
/* the program only works correctly if long = 32bits */
typedef unsigned long ˜4;
typedef unsigned char ul;

typedef struct 1
u4 k[31,ki[31, ercon[NMBR+ll,drcon[NMBR+ll;
t twy-ctx;

I* Note: encrypt and decrypt expect full blocks--padding blocks is
caller's responsibility. All bulk encryption is done in
ECB mode by these calls. Other modes may be added easily
*/
enough.

I* destroy: Context. */
/* Scrub context of all sensitive data. */
void twy-destroy(twy-ctx *);

/* encrypt: Context, ptr to data block, # of blocks. */
void twy-enc(twy-ctx *, u4 *, int);

/* decrypt: Context, ptr to data block, # of blocks. */
void twy_dec(twy-ctx *, u4 *, int);




Page 653
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page



/* key: Context, ptr to key data. */
void twyykey(twy-ctx *, u4 *);

,* A˜˜ODE..............----------˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜-˜˜-˜-------------- *,
/* End of AC code prototypes and structures. */
,* .-....................˜˜˜˜˜˜˜˜˜------˜----.-˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜.. *,

void mu(word32 *a) /* inverts the order of the bits of a */
I
int i ;
word32 bL31 ;

b[Ol = bCl1 = b[21 = 0 ;
for-( i=O ; i<32 ; i++ )

b[Ol <<= 1 ; b[ll <<= 1 ; b[21 <<= 1 ;
if(a[Ol&l) b[21 I= 1 ;
if(a[ll&l) b[ll I= 1 ;
if(a[21&1) b[Ol I= 1 ;
a[21 >>= 1 ;
a[01 >>= 1 ; a[11 >>= 1;
1
a[01 = b[Ol ; a[11 = b[ll ; a[21 = bL21 ;
I

void gamma(word32 *a) /* the nonlinear step */

word32 b[31 ;

(aCllI(-a[2111 ;
b[Ol = a[01 h
(aC211(-a[Ol)) ;
b[ll = a111 A
b[21 = a[21 h (aCOlIC-aLlI)) ;
a[01 = b[Ol a[11 = b[ll ; a[21 = bC21 ;
;
1

void theta(word32 *a) /* the linear step */
(
word32 bL31;

= a[01 h (a[01>>16) A (a[11<<16) h (a[11>>16) h (a[21<<16) h
b[O]
(a[11>>24) A (a[21<<8) A A (a[01<<24) A
(a[21>>8)
(a[21>>16) A (a[01<<16) A (a[21>>24) A (aLO]<< ;
= a[11 A (a[11>>16) A (a[21<<16) h (a[21>>16) A (aC01<<16) A
b[l]
(a[21>>24) n (a[01<<8) h h (a[11<<24) A
(aLO]>>
(a[01>>16) Ih (a[11<<16) A (a[01>>24) h (aC11<<8) ;
h (a[01<<16) n (a[01>>16) h (aC11<<16) A
b[21 = aC21 h (aC21>>16)
A (aC21<<24) A
(a[01>>24) n (aC11<<8) h (a[11>>8)
(a[11>>16) n (aC21<<16) A A (aC21<<8) ;
(a[11>>24)

a[01 = b[Ol ; a[11 = b[ll ; a[21 = bC21 ;
I

void piLl(word32 *a)




Page 654
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code



a[01 = (a[Ol>>lO) A (a[01<<22);
a[21 = (aC21<<1) n (a[21>>31);


void piL2Cword32 *a)

a[01 = (a[O]<<l) A (a[01>>31);
a[21 = (a[2]>>10) A (a[21<<22);


void rho(word32 *a) I* the round function */
I
theta(a) ;
pi-l(a) ;
gamma(a) ;
piL2Ca) ;


void rndcon-gen(word32 strt,word32 *rtab)
I* generates the round constants */
1
int i ;

for(i=O ; i<=NMBR ; i++ )
1
rtab[il = strt ;
strt <<= 1 ;
1;
if( strt&OxlOOOO 1 str t n= 0x1101




I* Modified slightly to fit the caller's needs. *I
void encrypt(twy-ctx *c, word32 *a)
1
char i ;
for( i=O ; i<NMBR ; i++ )

b[o, n= c->k[Ol A (c->ercon[il<<l6) ;
a[11 A= c->k[ll ;
a[21 n= c->k[21 A c->ercon[il ;
rho(a) ;
1
a[01 h= c->k[Ol A (c->ercon[NMBRl<<lG) ;
a[l] n= c->k[ll ;
a[2] A= c->k[21 A c->ercon[NMBRl ;
theta(a) ;
I

I* Modified slightly to meet caller's needs. *I
void decrypt(twy-ctx *c, word32 *a)
i
char i ;

mu(a) ;




Page 655
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page



for-( i=O ; i<NMBR ; i++ )

a[O] A= c->ki[O] A (c->drconCil<<lG) ;
a[l] I\= c->ki[ll ;
a[2] h= c->ki[21 h c->drcon[il ;
rho(a) ;
I
a[01 h= c->ki[Ol A (c->drcon[NMBRl<<lG) ;
a[l] I\= c->ki[ll ;
a[2] n= c->ki[21 A c->drcon[NMBRl ;
theta(a) ;
mu(a) ;


void twy-key(twy-ctx *c, u4 *key){
c->ki[Ol = c->k[Ol = keyLO];
c->ki[ll = c->k[ll = key[ll;
c->ki[Pl = c->k[21 = keyL21;
thetacc->ki);
mu(c->ki);
rndcon-gen(STRT-E,c->ercon);
rndcon-gen(STRT-D,c->drcon);




/* Encrypt in ECB mode. *I
void twy-enc(twy-ctx *c, u4 *data, int blkcnt){
u4 "d;
int i;

d = data;
for(i=O;i<blkcnt;i++) {
encrypt(c,d);
d +=3;
I
I

I* Decrypt in ECB mode. *I
void twy-dec(twy-ctx *c, u4 *data, int blkcnt)(
u4 "d;
int i;

d = data;
for(i=O;i<blkcnt;i++){
decrypt(c,d);
d+=3;



I* Scrub sensitive values from memory before deallocating. */
void twy-destroy(twy-ctx *c)(
int i;

for(i=O;i<3;i++) c->k[i] = c->ki[i] = 0;




Page 656
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code




void printveccchar *chrs, word32 *d){
: %081x %081x %081x \n",chrs,d[2l,d[ll,d[Ol);
printf("%20s


main0
1
twy-ctx gc;
a[9l,k[31;
word32
int i:

I* Test vector 1. *I

kC01=k[11=k[21=0;
a[01=a[11=a[21=1;
twy-key(&gc,k);

˜'**********\n 11);
printf(
printvec("KEY = ",k);
printvec("PLAIN = ",a);
encrypt(&gc,a);
printvec("CIPHER = ",a);

I* Test vector 2. *I

k[O]=6;k[ll=5;k[21=4;
a[0]=3;a[ll=2;a[21=1;
twy-key(&gc,k);

˜˜**********\n" );
printf(
printvec("KEY = ",k);
printvec("PLAIN = ",a);
encrypt(&gc,a);
printvec("CIPHER = ',a);

/* Test vector 3. */

k[2]=0xbcdef012;k[l]=Ox456789ab;k[Ol=OxdefOl234;
a[2]=0x01234567;a[ll=Ox9abcdefO;a[Ol=Ox23456789;
twy-key(&gc,k);

˜**********\n˜) ;
printf(
printvec("KEY = ",k);
printvec("PLAIN = ",a);
encrypt(&gc,a);
printvec("CIPHER = ",a);

I* Test vector 4. *I

k[2]=0xcab920cd;k[ll=Oxd6l44l38;k[Ol=Oxd2fO5b5e;
a[21=0xad21ecf7;a[ll=Ox83ae9dc4;aC01=0x4O59c76e;
twy-key(&gc,k);

"**********\n m) ;
printf(




Page 657
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
RC5


printvec("KEY = ",k);
printvec("PLAIN = ",a);
encrypt(&gc,a);
printvec("CIPHER = ",a);

I" TEST VALUES

: ODOOOOOO00000000 00000000
key
plaintext : 00000001 00000001 00000001
ciphertext : ad21ecf7 83ae9dc4 4059c76e

: 00000004 00000005 00000006
key
plaintext : OOOODOOl OOOOODD2 00000003
ciphertext : cab920cd d6144138 d2f05b5e

: bcdefOl2 456789ab def01234
key
plaintext : 01234567 9abcdefO 23456789
ciphertext : 7cdb76b2 9cdddb6d Daa55dbb

: cab920cd d6144138 d2f05b5e
key
plaintext : ad21ecf7 83ae9dc4 4059c76e
ciphertext : 15b155ed 6b13f17c 478ea871



I* Enc/dec test: */
for(i=O;i<9;i++) a[il=i;
twy_enc(&gc,a,3);
for(i=D;i<g;i+=3) printf("Block %Old encrypts to %081x %081x %OElx\n",
i/3,aCil,aCi+ll,a[i+21);

twy_dec(&gc,a,2);
twy_dec(&gc,a+6,1);

for(i=O;i<9;i+=3) printf("Block %Old decrypts to %081x %081x %081x\n",
i/3,a[il,a[i+ll,a[i+21);
1




RC5
#include <stdio.h>

I* An RC5 context needs to know how many rounds it has, and its subkeys. *I
typedef struct 1
u4 *xk;
int nr;
f rc5-ctx;

/* Where possible, these should be replaced with actual rotate instructions.
For Turbo C++, this is done with -1rotl and -1rotr. *I

#define RDTL32(X,C) (((X)<<(C))I((X)>>(32-(C))))
#define ROTR32(X,C) (((X)>>(C))I((X)<<(32-(C))))




Page 658
Prev. Chapter Home Previous Page
Next Page
Prev. page
Next Page
Source Code


/* Function prototypes fordealing with RC5 basic operations. */
void rc5_init(rc55ctx *, int);
void rc5_destroy(rc5_ctx *I;
void rc5_key(rc5_ctx *, ul *, int);
void rc5_encrypt(rc5_ctx *, u4 *, int);
void rc5_decrypt(rc5_ctx *, u4 *, int);

/* Function implementations for RC5. */

/* Scrub out all sensitive values. */
void rc5_destroy(rc5_ctx *c)l
int i;
for(i=O;i<(c->nr)*Z+Z;i++) c->xk[il=O;
PreeCc->xk);
I

/* Allocate memory for rc5 context's xk and such. */
void rc5_init(rc5_ctx *c, int rounds){
c>nr = rounds;
c->xk = (˜4 *) malloc(4*(rounds*Z+Z));


void rc5-encrypt(rc5-ctx *c, u4 *data, int blocks)1
u4 *d,*sk;
int h,i,rc;

d = data;
sk = (c->xk)+2;
for(h=O;h<blocks;h++){
d[Ol += c->xk[Ol;
d[ll += c->xk[ll;
for(i=O;i<c->nr*Z;i+=2)1
d[Ol h= d[ll.
rc = d[ll & il;
d[ol = ROTL32(d[Ol,rc);
d[Ol += sk[il;
d[ll n= d[Ol.
rc I d[Ol & 31;
d[ll = ROTL32(d[ll,rc);
d[ll += sk[i+ll;
/*printf("Round %03d : %081x %081x sk= %081x %081x\n",i/2,
d[Ol,d[ll,sk[il,sk[i+ll˜;*/

d+=2;



void rc5_decrypt(rc5_ctx *c, u4 *data, int blocks){
u4 *d,*sk;
int h,i,rc;

d = data;
sk = (c->xk)+2;
for(h=O;h<blocks;h++){
for(i=c->nr*Z-2 ;i>=O ;i-=2){


<<

. 2
( 8)



>>