diff options
Diffstat (limited to 'baseline/source/rijndael_enc/aes.c')
| -rw-r--r-- | baseline/source/rijndael_enc/aes.c | 406 |
1 files changed, 0 insertions, 406 deletions
diff --git a/baseline/source/rijndael_enc/aes.c b/baseline/source/rijndael_enc/aes.c deleted file mode 100644 index c1282a7..0000000 --- a/baseline/source/rijndael_enc/aes.c +++ /dev/null | |||
| @@ -1,406 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ----------------------------------------------------------------------- | ||
| 3 | Copyright (c) 2001 Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK | ||
| 4 | |||
| 5 | TERMS | ||
| 6 | |||
| 7 | Redistribution and use in source and binary forms, with or without | ||
| 8 | modification, are permitted provided that the following conditions | ||
| 9 | are met: | ||
| 10 | 1. Redistributions of source code must retain the above copyright | ||
| 11 | notice, this list of conditions and the following disclaimer. | ||
| 12 | 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | notice, this list of conditions and the following disclaimer in the | ||
| 14 | documentation and/or other materials provided with the distribution. | ||
| 15 | |||
| 16 | This software is provided 'as is' with no guarantees of correctness or | ||
| 17 | fitness for purpose. | ||
| 18 | ----------------------------------------------------------------------- | ||
| 19 | |||
| 20 | FUNCTION | ||
| 21 | |||
| 22 | The AES algorithm Rijndael implemented for block and key sizes of 128, | ||
| 23 | bits (16 bytes) by Brian Gladman. | ||
| 24 | |||
| 25 | This is an implementation of the AES encryption algorithm (Rijndael) | ||
| 26 | designed by Joan Daemen and Vincent Rijmen. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "aes.h" | ||
| 30 | |||
| 31 | #include "aestab.h" | ||
| 32 | |||
| 33 | #define four_tables(x,tab,vf,rf,c) ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \ | ||
| 34 | tab[1][bval(vf(x,1,c),rf(1,c))] ^ \ | ||
| 35 | tab[2][bval(vf(x,2,c),rf(2,c))] ^ \ | ||
| 36 | tab[3][bval(vf(x,3,c),rf(3,c))] ) | ||
| 37 | |||
| 38 | #define vf1(x,r,c) (x) | ||
| 39 | #define rf1(r,c) (r) | ||
| 40 | #define rf2(r,c) ((r-c)&3) | ||
| 41 | |||
| 42 | #define ls_box(x,c) four_tables(x,rijndael_enc_fl_tab,vf1,rf2,c) | ||
| 43 | |||
| 44 | #define inv_mcol(x) four_tables(x,rijndael_enc_im_tab,vf1,rf1,0) | ||
| 45 | |||
| 46 | /* | ||
| 47 | Subroutine to set the block size (if variable) in bytes, legal | ||
| 48 | values being 16, 24 and 32. | ||
| 49 | */ | ||
| 50 | |||
| 51 | #define nc (Ncol) | ||
| 52 | |||
| 53 | /* | ||
| 54 | Initialise the key schedule from the user supplied key. The key | ||
| 55 | length is now specified in bytes - 16, 24 or 32 as appropriate. | ||
| 56 | This corresponds to bit lengths of 128, 192 and 256 bits, and | ||
| 57 | to Nk values of 4, 6 and 8 respectively. | ||
| 58 | */ | ||
| 59 | |||
| 60 | #define mx(t,f) (*t++ = inv_mcol(*f),f++) | ||
| 61 | #define cp(t,f) *t++ = *f++ | ||
| 62 | |||
| 63 | #define cpy(d,s) do { cp(d,s); cp(d,s); cp(d,s); cp(d,s); } while (0) | ||
| 64 | #define mix(d,s) do { mx(d,s); mx(d,s); mx(d,s); mx(d,s); } while (0) | ||
| 65 | |||
| 66 | aes_ret rijndael_enc_set_key( byte in_key[], const word n_bytes, | ||
| 67 | const enum aes_key f, struct aes *cx ) | ||
| 68 | { | ||
| 69 | word *kf, *kt, rci; | ||
| 70 | |||
| 71 | if ( ( n_bytes & 7 ) || n_bytes < 16 || n_bytes > 32 || ( !( f & 1 ) && | ||
| 72 | !( f & 2 ) ) ) | ||
| 73 | return ( n_bytes ? cx->mode &= ~0x03, aes_bad : ( aes_ret )( cx->Nkey << 2 ) ); | ||
| 74 | |||
| 75 | cx->mode = ( cx->mode & ~0x03 ) | ( ( byte )f & 0x03 ); | ||
| 76 | cx->Nkey = n_bytes >> 2; | ||
| 77 | cx->Nrnd = Nr( cx->Nkey, ( word )nc ); | ||
| 78 | |||
| 79 | cx->e_key[0] = word_in( in_key ); | ||
| 80 | cx->e_key[1] = word_in( in_key + 4 ); | ||
| 81 | cx->e_key[2] = word_in( in_key + 8 ); | ||
| 82 | cx->e_key[3] = word_in( in_key + 12 ); | ||
| 83 | |||
| 84 | kf = cx->e_key; | ||
| 85 | kt = kf + nc * ( cx->Nrnd + 1 ) - cx->Nkey; | ||
| 86 | rci = 0; | ||
| 87 | |||
| 88 | switch ( cx->Nkey ) { | ||
| 89 | case 4: | ||
| 90 | _Pragma( "loopbound min 0 max 0" ) | ||
| 91 | do { | ||
| 92 | kf[4] = kf[0] ^ ls_box( kf[3], 3 ) ^ rijndael_enc_rcon_tab[rci++]; | ||
| 93 | kf[5] = kf[1] ^ kf[4]; | ||
| 94 | kf[6] = kf[2] ^ kf[5]; | ||
| 95 | kf[7] = kf[3] ^ kf[6]; | ||
| 96 | kf += 4; | ||
| 97 | } while ( kf < kt ); | ||
| 98 | break; | ||
| 99 | |||
| 100 | case 6: | ||
| 101 | cx->e_key[4] = word_in( in_key + 16 ); | ||
| 102 | cx->e_key[5] = word_in( in_key + 20 ); | ||
| 103 | _Pragma( "loopbound min 0 max 0" ) | ||
| 104 | do { | ||
| 105 | kf[ 6] = kf[0] ^ ls_box( kf[5], 3 ) ^ rijndael_enc_rcon_tab[rci++]; | ||
| 106 | kf[ 7] = kf[1] ^ kf[ 6]; | ||
| 107 | kf[ 8] = kf[2] ^ kf[ 7]; | ||
| 108 | kf[ 9] = kf[3] ^ kf[ 8]; | ||
| 109 | kf[10] = kf[4] ^ kf[ 9]; | ||
| 110 | kf[11] = kf[5] ^ kf[10]; | ||
| 111 | kf += 6; | ||
| 112 | } while ( kf < kt ); | ||
| 113 | break; | ||
| 114 | |||
| 115 | case 8: | ||
| 116 | cx->e_key[4] = word_in( in_key + 16 ); | ||
| 117 | cx->e_key[5] = word_in( in_key + 20 ); | ||
| 118 | cx->e_key[6] = word_in( in_key + 24 ); | ||
| 119 | cx->e_key[7] = word_in( in_key + 28 ); | ||
| 120 | _Pragma( "loopbound min 7 max 7" ) | ||
| 121 | do { | ||
| 122 | kf[ 8] = kf[0] ^ ls_box( kf[7], 3 ) ^ rijndael_enc_rcon_tab[rci++]; | ||
| 123 | kf[ 9] = kf[1] ^ kf[ 8]; | ||
| 124 | kf[10] = kf[2] ^ kf[ 9]; | ||
| 125 | kf[11] = kf[3] ^ kf[10]; | ||
| 126 | kf[12] = kf[4] ^ ls_box( kf[11], 0 ); | ||
| 127 | kf[13] = kf[5] ^ kf[12]; | ||
| 128 | kf[14] = kf[6] ^ kf[13]; | ||
| 129 | kf[15] = kf[7] ^ kf[14]; | ||
| 130 | kf += 8; | ||
| 131 | } while ( kf < kt ); | ||
| 132 | break; | ||
| 133 | } | ||
| 134 | |||
| 135 | if ( ( cx->mode & 3 ) != enc ) { | ||
| 136 | word i; | ||
| 137 | |||
| 138 | kt = cx->d_key + nc * cx->Nrnd; | ||
| 139 | kf = cx->e_key; | ||
| 140 | |||
| 141 | cpy( kt, kf ); | ||
| 142 | kt -= 2 * nc; | ||
| 143 | |||
| 144 | _Pragma( "loopbound min 0 max 0" ) | ||
| 145 | for ( i = 1; i < cx->Nrnd; ++i ) { | ||
| 146 | mix( kt, kf ); | ||
| 147 | kt -= 2 * nc; | ||
| 148 | } | ||
| 149 | |||
| 150 | cpy( kt, kf ); | ||
| 151 | } | ||
| 152 | |||
| 153 | return aes_good; | ||
| 154 | } | ||
| 155 | |||
| 156 | short rijndael_enc_encrypt( unsigned char in_blk[], unsigned char out_blk[], | ||
| 157 | const struct aes *cx ) | ||
| 158 | { | ||
| 159 | const unsigned long *kp = cx->e_key; | ||
| 160 | if ( !( cx->mode & 1 ) ) | ||
| 161 | return 0; | ||
| 162 | unsigned long b0[4]; | ||
| 163 | b0[0] = *( unsigned long * )in_blk ^ kp[0]; | ||
| 164 | b0[1] = *( unsigned long * )( in_blk + 4 )^kp[1]; | ||
| 165 | b0[2] = *( unsigned long * )( in_blk + 8 )^kp[2]; | ||
| 166 | b0[3] = *( unsigned long * )( in_blk + 12 )^kp[3]; | ||
| 167 | kp += 4; | ||
| 168 | unsigned long b1[4]; | ||
| 169 | switch ( cx->Nrnd ) { | ||
| 170 | case 14: | ||
| 171 | b1[0] = kp[0] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b0[0] )] ^ | ||
| 172 | rijndael_enc_ft_tab[1][( ( unsigned char )( b0[1] >> 8 ) )] ^ | ||
| 173 | rijndael_enc_ft_tab[2][( ( unsigned char )( b0[2] >> 16 ) )] ^ | ||
| 174 | rijndael_enc_ft_tab[3][( ( unsigned char )( b0[3] >> 24 ) )] ); | ||
| 175 | b1[1] = kp[1] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b0[1] )] ^ | ||
| 176 | rijndael_enc_ft_tab[1][( ( unsigned char )( b0[2] >> 8 ) )] ^ | ||
| 177 | rijndael_enc_ft_tab[2][( ( unsigned char )( b0[3] >> 16 ) )] ^ | ||
| 178 | rijndael_enc_ft_tab[3][( ( unsigned char )( b0[0] >> 24 ) )] ); | ||
| 179 | b1[2] = kp[2] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b0[2] )] ^ | ||
| 180 | rijndael_enc_ft_tab[1][( ( unsigned char )( b0[3] >> 8 ) )] ^ | ||
| 181 | rijndael_enc_ft_tab[2][( ( unsigned char )( b0[0] >> 16 ) )] ^ | ||
| 182 | rijndael_enc_ft_tab[3][( ( unsigned char )( b0[1] >> 24 ) )] ); | ||
| 183 | b1[3] = kp[3] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b0[3] )] ^ | ||
| 184 | rijndael_enc_ft_tab[1][( ( unsigned char )( b0[0] >> 8 ) )] ^ | ||
| 185 | rijndael_enc_ft_tab[2][( ( unsigned char )( b0[1] >> 16 ) )] ^ | ||
| 186 | rijndael_enc_ft_tab[3][( ( unsigned char )( b0[2] >> 24 ) )] ); | ||
| 187 | b0[0] = ( kp + 4 )[0] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b1[0] )] ^ | ||
| 188 | rijndael_enc_ft_tab[1][( ( unsigned char )( b1[1] >> 8 ) )] ^ | ||
| 189 | rijndael_enc_ft_tab[2][( ( unsigned char )( b1[2] >> 16 ) )] ^ | ||
| 190 | rijndael_enc_ft_tab[3][( ( unsigned char )( b1[3] >> 24 ) )] ); | ||
| 191 | b0[1] = ( kp + 4 )[1] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b1[1] )] ^ | ||
| 192 | rijndael_enc_ft_tab[1][( ( unsigned char )( b1[2] >> 8 ) )] ^ | ||
| 193 | rijndael_enc_ft_tab[2][( ( unsigned char )( b1[3] >> 16 ) )] ^ | ||
| 194 | rijndael_enc_ft_tab[3][( ( unsigned char )( b1[0] >> 24 ) )] ); | ||
| 195 | b0[2] = ( kp + 4 )[2] ^ ( rijndael_enc_ft_tab[0][( ( unsigned char )b1[2] )] ^ | ||
| 196 | rijndael_enc_ft_tab[1][( ( unsigned char )( b1[3] >> 8 ) )] ^ | ||
| 197 | rijndael_enc_ft_tab[2][( ( unsigned char )( b1[0] >> 16 ) )] ^ | ||
| 198 | |||
