diff options
| author | Joshua Bakita <bakitajoshua@gmail.com> | 2019-10-07 19:13:39 -0400 |
|---|---|---|
| committer | Joshua Bakita <bakitajoshua@gmail.com> | 2019-10-07 19:13:39 -0400 |
| commit | 386b7d3366f1359a265da207a9cafa3edf553b64 (patch) | |
| tree | c76120c2c138faed822e4ae386be6ef22a738a78 /baseline/source/rijndael_dec/rijndael_dec.c | |
| parent | 54a3f7091a2146b29c73a6fdc4b62a5c4ad7a3d8 (diff) | |
Reorganize and commit all the modified TACLeBench code and run scripts
Diffstat (limited to 'baseline/source/rijndael_dec/rijndael_dec.c')
| -rw-r--r-- | baseline/source/rijndael_dec/rijndael_dec.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/baseline/source/rijndael_dec/rijndael_dec.c b/baseline/source/rijndael_dec/rijndael_dec.c new file mode 100644 index 0000000..4082eff --- /dev/null +++ b/baseline/source/rijndael_dec/rijndael_dec.c | |||
| @@ -0,0 +1,195 @@ | |||
| 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_dec is an implementation of the AES decryption | ||
| 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_dec_libc.h" | ||
| 44 | |||
| 45 | /* | ||
| 46 | Global variable definitions | ||
| 47 | */ | ||
| 48 | unsigned char rijndael_dec_key[32]; | ||
| 49 | int rijndael_dec_key_len; | ||
| 50 | |||
| 51 | extern unsigned char rijndael_dec_data[]; | ||
| 52 | struct rijndael_dec_FILE rijndael_dec_fin; | ||
| 53 | |||
| 54 | int rijndael_dec_checksum = 0; | ||
| 55 | |||
| 56 | /* | ||
| 57 | Forward declaration of functions | ||
| 58 | */ | ||
| 59 | void rijndael_dec_init( void ); | ||
| 60 | int rijndael_dec_return( void ); | ||
| 61 | void rijndael_dec_fillrand( unsigned char *buf, int len ); | ||
| 62 | void rijndael_dec_decfile( struct rijndael_dec_FILE *fin, struct aes *ctx ); | ||
| 63 | void rijndael_dec_main( void ); | ||
| 64 | |||
| 65 | void rijndael_dec_init( void ) | ||
| 66 | { | ||
| 67 | /* create a pseudo-file for the input*/ | ||
| 68 | rijndael_dec_fin.data = rijndael_dec_data; | ||
| 69 | rijndael_dec_fin.size = 32768; | ||
| 70 | rijndael_dec_fin.cur_pos = 0; | ||
| 71 | |||
| 72 | unsigned i; | ||
| 73 | volatile int x = 0; | ||
| 74 | rijndael_dec_fin.size ^= x; | ||
| 75 | _Pragma( "loopbound min 32768 max 32768" ) | ||
| 76 | for ( i = 0; i < rijndael_dec_fin.size; i++ ) | ||
| 77 | rijndael_dec_fin.data[i] ^= x; | ||
| 78 | |||
| 79 | /* this is a pointer to the hexadecimal key digits */ | ||
| 80 | const volatile char *cp = | ||
| 81 | "1234567890abcdeffedcba09876543211234567890abcdeffedcba0987654321"; | ||
| 82 | char ch; | ||
| 83 | int by = 0; | ||
| 84 | |||
| 85 | i = 0; /* this is a count for the input digits processed */ | ||
| 86 | _Pragma( "loopbound min 64 max 64" ) | ||
| 87 | while ( i < 64 && *cp ) { /* the maximum key length is 32 bytes and */ | ||
| 88 | /* hence at most 64 hexadecimal digits */ | ||
| 89 | ch = rijndael_dec_toupper( *cp++ ); /* process a hexadecimal digit */ | ||
| 90 | if ( ch >= '0' && ch <= '9' ) | ||
| 91 | by = ( by << 4 ) + ch - '0'; | ||
| 92 | else | ||
| 93 | if ( ch >= 'A' && ch <= 'F' ) | ||
| 94 | by = ( by << 4 ) + ch - 'A' + 10; | ||
| 95 | else { /* error if not hexadecimal */ | ||
| 96 | rijndael_dec_checksum = -2; | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | /* store a key byte for each pair of hexadecimal digits */ | ||
| 101 | if ( i++ & 1 ) | ||
| 102 | rijndael_dec_key[i / 2 - 1] = by & 0xff; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( *cp ) { | ||
| 106 | rijndael_dec_checksum = -3; | ||
| 107 | return; | ||
| 108 | } else | ||
| 109 | if ( i < 32 || ( i & 15 ) ) { | ||
| 110 | rijndael_dec_checksum = -4; | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | rijndael_dec_key_len = i / 2; | ||
| 115 | } | ||
| 116 | |||
| 117 | int rijndael_dec_return( void ) | ||
| 118 | { | ||
| 119 | return ( ( rijndael_dec_checksum == ( int )262180 ) ? 0 : -1 ); | ||
| 120 | } | ||
| 121 | |||
| 122 | void rijndael_dec_decfile( struct rijndael_dec_FILE *fin, struct aes *ctx ) | ||
| 123 | { | ||
| 124 | unsigned char inbuf1[16], inbuf2[16], outbuf[16], *bp1, *bp2, *tp; | ||
| 125 | int i; | ||
| 126 | |||
| 127 | |||
| 128 | rijndael_dec_fread( inbuf1, 1, 16, fin ); | ||
| 129 | |||
| 130 | i = rijndael_dec_fread( inbuf2, 1, 16, | ||
| 131 | fin ); /* read 1st encrypted file block */ | ||
| 132 | |||
| 133 | if ( i && i != 16 ) { | ||
| 134 | rijndael_dec_checksum = -10; | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | rijndael_dec_decrypt( inbuf2, outbuf, | ||
| 139 | ctx ); /* decrypt it */ | ||
| 140 | |||
| 141 | rijndael_dec_checksum += outbuf[15]; | ||
| 142 | |||
| 143 | _Pragma( "loopbound min 16 max 16" ) | ||
| 144 | for ( i = 0; i < 16; ++i ) /* xor with previous input */ | ||
| 145 | outbuf[i] ^= inbuf1[i]; | ||
| 146 | |||
| 147 | bp1 = inbuf1; /* set up pointers to two input buffers */ | ||
| 148 | bp2 = inbuf2; | ||
| 149 | |||
| 150 | /* TODO: this is necessarily an input-dependent loop bound */ | ||
| 151 | _Pragma( "loopbound min 19491 max 91491" ) | ||
| 152 | while ( 1 ) { | ||
| 153 | i = rijndael_dec_fread( bp1, 1, 16, fin ); /* read next encrypted block */ | ||
| 154 | /* to first input buffer */ | ||
| 155 | if ( i != 16 ) /* no more bytes in input - the decrypted */ | ||
| 156 | break; /* partial final buffer needs to be output */ | ||
| 157 | |||
| 158 | /* if a block has been read the previous block must have been */ | ||
| 159 | /* full lnegth so we can now write it out */ | ||
| 160 | |||
| 161 | rijndael_dec_decrypt( bp1, outbuf, ctx ); /* decrypt the new input block and */ | ||
| 162 | |||
| 163 | rijndael_dec_checksum += outbuf[15]; | ||
| 164 | |||
| 165 | _Pragma( "loopbound min 16 max 16" ) | ||
| 166 | for ( i = 0; i < 16; ++i ) /* xor it with previous input block */ | ||
| 167 | outbuf[i] ^= bp2[i]; | ||
| 168 | |||
| 169 | /* swap buffer pointers */ | ||
| 170 | tp = bp1, bp1 = bp2, bp2 = tp; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | void _Pragma( "entrypoint" ) rijndael_dec_main( void ) | ||
| 175 | { | ||
| 176 | struct aes ctx[1]; | ||
| 177 | |||
| 178 | /* decryption in Cipher Block Chaining mode */ | ||
| 179 | rijndael_dec_set_key( rijndael_dec_key, rijndael_dec_key_len, dec, ctx ); | ||
| 180 | rijndael_dec_decfile( &rijndael_dec_fin, ctx ); | ||
| 181 | } | ||
| 182 | |||
| 183 | int main(int argc, char** argv) | ||
| 184 | { | ||
| 185 | SET_UP | ||
| 186 | for (jobsComplete=-1; jobsComplete<maxJobs; jobsComplete++){ | ||
| 187 | START_LOOP | ||
| 188 | rijndael_dec_init(); | ||
| 189 | rijndael_dec_main(); | ||
| 190 | STOP_LOOP | ||
| 191 | } | ||
| 192 | WRITE_TO_FILE< | ||
