summaryrefslogtreecommitdiffstats
path: root/baseline/source/rijndael_enc/rijndael_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'baseline/source/rijndael_enc/rijndael_enc.c')
-rw-r--r--baseline/source/rijndael_enc/rijndael_enc.c242
1 files changed, 0 insertions, 242 deletions
diff --git a/baseline/source/rijndael_enc/rijndael_enc.c b/baseline/source/rijndael_enc/rijndael_enc.c
deleted file mode 100644
index f0582c9..0000000
--- a/baseline/source/rijndael_enc/rijndael_enc.c
+++ /dev/null
@@ -1,242 +0,0 @@
1/*
2
3 This program is part of the TACLeBench benchmark suite.
4 Version V 2.0
5
6 Name: rijndael_enc
7
8 Author: Dr Brian Gladman
9
10 Function: rijndael_enc is an implementation of the AES encryption
11 algorithm (Rijndael).
12
13 Source: security section of MiBench
14
15 Changes: Add computation of a checksum, refactoring
16
17 License: see below
18
19*/
20
21/*
22 -----------------------------------------------------------------------
23 Copyright (c) 2001 Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK
24
25 TERMS
26
27 Redistribution and use in source and binary forms, with or without
28 modification, are permitted provided that the following conditions
29 are met:
30 1. Redistributions of source code must retain the above copyright
31 notice, this list of conditions and the following disclaimer.
32 2. Redistributions in binary form must reproduce the above copyright
33 notice, this list of conditions and the following disclaimer in the
34 documentation and/or other materials provided with the distribution.
35
36 This software is provided 'as is' with no guarantees of correctness or
37 fitness for purpose.
38 -----------------------------------------------------------------------
39*/
40
41#include "extra.h"
42#include "aes.h"
43#include "rijndael_enc_libc.h"
44
45/*
46 Global variable definitions
47*/
48unsigned char rijndael_enc_key[32];
49int rijndael_enc_key_len;
50
51extern unsigned char rijndael_enc_data[];
52struct rijndael_enc_FILE rijndael_enc_fin;
53
54int rijndael_enc_checksum = 0;
55
56/*
57 Forward declaration of functions
58*/
59void rijndael_enc_init( void );
60int rijndael_enc_return( void );
61void rijndael_enc_fillrand( unsigned char *buf, int len );
62void rijndael_enc_encfile( struct rijndael_enc_FILE *fin, struct aes *ctx );
63void rijndael_enc_main( void );
64
65void rijndael_enc_init( void )
66{
67 /* create a pseudo-file for the input*/
68 rijndael_enc_fin.data = rijndael_enc_data;
69 rijndael_enc_fin.size = 31369;
70 rijndael_enc_fin.cur_pos = 0;
71 rijndael_enc_checksum = 0;
72
73 unsigned i;
74 volatile int x = 0;
75 rijndael_enc_fin.size ^= x;
76 _Pragma( "loopbound min 31369 max 31369" )
77 for ( i = 0; i < rijndael_enc_fin.size; i++ )
78 rijndael_enc_fin.data[i] ^= x;
79
80 /* this is a pointer to the hexadecimal key digits */
81 const volatile char *cp =
82 "1234567890abcdeffedcba09876543211234567890abcdeffedcba0987654321";
83 char ch;
84 int by = 0;
85
86 i = 0; /* this is a count for the input digits processed */
87 _Pragma( "loopbound min 64 max 64" )
88 while ( i < 64 && *cp ) { /* the maximum key length is 32 bytes and */
89 /* hence at most 64 hexadecimal digits */
90 ch = rijndael_enc_toupper( *cp++ ); /* process a hexadecimal digit */
91 if ( ch >= '0' && ch <= '9' )
92 by = ( by << 4 ) + ch - '0';
93 else
94 if ( ch >= 'A' && ch <= 'F' )
95 by = ( by << 4 ) + ch - 'A' + 10;
96 else { /* error if not hexadecimal */
97 rijndael_enc_checksum = -2;
98 return;
99 }
100
101 /* store a key byte for each pair of hexadecimal digits */
102 if ( i++ & 1 )
103 rijndael_enc_key[i / 2 - 1] = by & 0xff;
104 }
105
106 if ( *cp ) {
107 rijndael_enc_checksum = -3;
108 return;
109 } else
110 if ( i < 32 || ( i & 15 ) ) {
111 rijndael_enc_checksum = -4;
112 return;
113 }
114
115 rijndael_enc_key_len = i / 2;
116}
117
118int rijndael_enc_return( void )
119{
120 return ( ( rijndael_enc_checksum == ( int )249509 ) ? 0 : -1 );
121}
122
123/* A Pseudo Random Number Generator (PRNG) used for the */
124/* Initialisation Vector. The PRNG is George Marsaglia's */
125/* Multiply-With-Carry (MWC) PRNG that concatenates two */
126/* 16-bit MWC generators: */
127/* x(n)=36969 * x(n-1) + carry mod 2^16 */
128/* y(n)=18000 * y(n-1) + carry mod 2^16 */
129/* to produce a combined PRNG with a period of about 2^60. */
130
131#define RAND(a,b) (((a = 36969 * (a & 65535) + (a >> 16)) << 16) + (b = 18000 * (b & 65535) + (b >> 16)) )
132
133void rijndael_enc_fillrand( unsigned char *buf, int len )
134{
135 static unsigned long a[2], mt = 1, count = 4;
136 static char r[4];
137 int i;
138
139 if ( mt ) {
140 mt = 0;
141 a[0] = 0xeaf3;
142 a[1] = 0x35fe;
143 }
144
145 _Pragma( "loopbound min 1 max 16" )
146 for ( i = 0; i < len; ++i ) {
147 if ( count == 4 ) {
148 *( unsigned long * )r = RAND( a[0], a[1] );
149 count = 0;
150 }
151
152 buf[i] = r[count++];
153 }
154}
155
156void rijndael_enc_encfile( struct rijndael_enc_FILE *fin, struct aes *ctx )
157{
158 unsigned char inbuf[16], outbuf[16];
159 long int flen;
160 unsigned long i = 0, l = 0;
161
162 rijndael_enc_fillrand( outbuf,
163 16 ); /* set an IV for CBC mode */
164 flen = fin->size;
165
166 rijndael_enc_fillrand( inbuf,
167 1 ); /* make top 4 bits of a byte random */
168 l = 15; /* and store the length of the last */
169 /* block in the lower 4 bits */
170 inbuf[0] = ( ( char )flen & 15 ) | ( inbuf[0] & ~15 );
171
172 /* TODO: this is necessarily an input-dependent loop bound */
173 _Pragma( "loopbound min 1961 max 1961" )
174 while ( !rijndael_enc_feof(
175 fin ) ) { /* loop to encrypt the input file */
176 /* input 1st 16 bytes to buf[1..16] */
177 i = rijndael_enc_fread( inbuf + 16 - l, 1, l, fin ); /* on 1st round byte[0] */
178 /* is the length code */
179 if ( i < l ) break; /* if end of the input file reached */
180
181 _Pragma( "loopbound min 16 max 16" )
182 for ( i = 0; i < 16; ++i ) /* xor in previous cipher text */
183 inbuf[i] ^= outbuf[i];
184
185 rijndael_enc_encrypt( inbuf, outbuf,
186 ctx ); /* and do the encryption */
187
188 rijndael_enc_checksum += outbuf[15];
189
190 /* in all but first round read 16 */
191 l = 16; /* bytes into the buffer */
192 }
193
194 /* except for files of length less than two blocks we now have one */
195 /* byte from the previous block and 'i' bytes from the current one */
196 /* to encrypt and 15 - i empty buffer positions. For files of less */