| 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "libfastmint.h" |
| 5 | |
| 6 | int minter_ansi_compact_1_test(void) |
| 7 | { |
| 8 | /* This minter runs on any hardware */ |
| 9 | return 1; |
| 10 | } |
| 11 | |
| 12 | /* #define F1( B, C, D ) ( ( (B) & (C) ) | ( ~(B) & (D) ) ) */ |
| 13 | #define F1( B, C, D ) ( (D) ^ ( (B) & ( (C) ^ (D) ) ) ) |
| 14 | #define F2( B, C, D ) ( (B) ^ (C) ^ (D) ) |
| 15 | /* #define F3( B, C, D ) ( (B) & (C) ) | ( (C) & (D) ) | ( (B) & (D) ) */ |
| 16 | #define F3( B, C, D ) ( ( (B) & ( (C) | (D) )) | ( (C) & (D) ) ) |
| 17 | #define F4( B, C, D ) ( (B) ^ (C) ^ (D) ) |
| 18 | |
| 19 | #define K1 0x5A827999 /* constant used for rounds 0..19 */ |
| 20 | #define K2 0x6ED9EBA1 /* constant used for rounds 20..39 */ |
| 21 | #define K3 0x8F1BBCDC /* constant used for rounds 40..59 */ |
| 22 | #define K4 0xCA62C1D6 /* constant used for rounds 60..79 */ |
| 23 | |
| 24 | #define S(n, X) ( ( (X) << (n) ) | ( (X) >> ( 32 - (n) ) ) ) |
| 25 | #define Wf(t) (W[t] = S(1, W[t-16] ^ W[t-14] ^ W[t-8] ^ W[t-3])) |
| 26 | |
| 27 | #define ROUND(t,A,B,C,D,E,Func,K) \ |
| 28 | E += S(5,A) + Func(B,C,D) + W[t] + K; \ |
| 29 | B = S(30,B); |
| 30 | |
| 31 | #define ROUND5( t, Func, K ) \ |
| 32 | ROUND( t + 0, A, B, C, D, E, Func, K );\ |
| 33 | ROUND( t + 1, E, A, B, C, D, Func, K );\ |
| 34 | ROUND( t + 2, D, E, A, B, C, Func, K );\ |
| 35 | ROUND( t + 3, C, D, E, A, B, Func, K );\ |
| 36 | ROUND( t + 4, B, C, D, E, A, Func, K ) |
| 37 | |
| 38 | #define ROUND20( t, Func, K )\ |
| 39 | ROUND5( t + 0, Func, K );\ |
| 40 | ROUND5( t + 5, Func, K );\ |
| 41 | ROUND5( t + 10, Func, K );\ |
| 42 | ROUND5( t + 15, Func, K ) |
| 43 | |
| 44 | unsigned long minter_ansi_compact_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS) |
| 45 | { |
| 46 | MINTER_CALLBACK_VARS; |
| 47 | unsigned long iters = 0 ; |
| 48 | int t = 0, gotBits = 0, maxBits = (bits > 16) ? 16 : bits; |
| 49 | uInt32 bitMask1Low = 0 , bitMask1High = 0 , s = 0 ; |
| 50 | uInt32 A = 0 , B = 0 , C = 0 , D = 0 , E = 0 ; |
| 51 | uInt32 W[80] = {0}; |
| 52 | uInt32 H[5] = {0}, pH[5] = {0}; |
| 53 | const char *p = encodeAlphabets[EncodeBase64]; |
| 54 | unsigned char *X = (unsigned char*) W; |
| 55 | int addressMask = 0 ; |
| 56 | static const int endTest = 3; |
| 57 | unsigned char *output = (unsigned char*) block; |
| 58 | |
| 59 | if ( *best > 0 ) { maxBits = *best+1; } |
| 60 | |
| 61 | /* Work out whether we need to swap bytes during encoding */ |
| 62 | addressMask = ( *(char*)&endTest ); |
| 63 | |
| 64 | /* Work out which bits to mask out for test */ |
| 65 | if(maxBits < 32) { |
| 66 | if ( bits == 0 ) { bitMask1Low = 0; } else { |
| 67 | bitMask1Low = ~((((uInt32) 1) << (32 - maxBits)) - 1); |
| 68 | } |
| 69 | bitMask1High = 0; |
| 70 | } else { |
| 71 | bitMask1Low = ~0; |
| 72 | bitMask1High = ~((((uInt32) 1) << (64 - maxBits)) - 1); |
| 73 | } |
| 74 | maxBits = 0; |
| 75 | |
| 76 | /* Copy block and IV to internal storage */ |
| 77 | for(t=0; t < 16; t++) |
| 78 | W[t] = GET_WORD(output + t*4); |
| 79 | for(t=0; t < 5; t++) |
| 80 | pH[t] = H[t] = IV[t]; |
| 81 | |
| 82 | /* The Tight Loop - everything in here should be extra efficient */ |
| 83 | for(iters=0; iters < maxIter; iters++) { |
| 84 | |
| 85 | /* Encode iteration count into tail */ |
| 86 | X[(tailIndex - 1) ^ addressMask] = p[(iters ) & 0x3f]; |
| 87 | if(!(iters & 0x3f)) { |
| 88 | if ( iters >> 6 ) { |
| 89 | X[(tailIndex - 2) ^ addressMask] = p[(iters >> 6) & 0x3f]; |
| 90 | } |
| 91 | if ( iters >> 12 ) { |
| 92 | X[(tailIndex - 3) ^ addressMask] = p[(iters >> 12) & 0x3f]; |
| 93 | } |
| 94 | if ( iters >> 18 ) { |
| 95 | X[(tailIndex - 4) ^ addressMask] = p[(iters >> 18) & 0x3f]; |
| 96 | } |
| 97 | if ( iters >> 24 ) { |
| 98 | X[(tailIndex - 5) ^ addressMask] = p[(iters >> 24) & 0x3f]; |
| 99 | } |
| 100 | if ( iters >> 30 ) { |
| 101 | X[(tailIndex - 6) ^ addressMask] = p[(iters >> 30) & 0x3f]; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* Bypass shortcuts below on certain iterations */ |
| 106 | if((!(iters & 0xffffff)) && (tailIndex == 52 || tailIndex == 32)) { |
| 107 | A = H[0]; |
| 108 | B = H[1]; |
| 109 | C = H[2]; |
| 110 | D = H[3]; |
| 111 | E = H[4]; |
| 112 | |
| 113 | for(t=16; t < 32; t++) |
| 114 | Wf(t); |
| 115 | |
| 116 | ROUND( 0, A, B, C, D, E, F1, K1 ); |
| 117 | ROUND( 1, E, A, B, C, D, F1, K1 ); |
| 118 | ROUND( 2, D, E, A, B, C, F1, K1 ); |
| 119 | ROUND( 3, C, D, E, A, B, F1, K1 ); |
| 120 | ROUND( 4, B, C, D, E, A, F1, K1 ); |
| 121 | ROUND( 5, A, B, C, D, E, F1, K1 ); |
| 122 | ROUND( 6, E, A, B, C, D, F1, K1 ); |
| 123 | |
| 124 | if(tailIndex == 52) { |
| 125 | ROUND( 7, D, E, A, B, C, F1, K1 ); |
| 126 | ROUND( 8, C, D, E, A, B, F1, K1 ); |
| 127 | ROUND( 9, B, C, D, E, A, F1, K1 ); |
| 128 | ROUND(10, A, B, C, D, E, F1, K1 ); |
| 129 | ROUND(11, E, A, B, C, D, F1, K1 ); |
| 130 | } |
| 131 | |
| 132 | pH[0] = A; |
| 133 | pH[1] = B; |
| 134 | pH[2] = C; |
| 135 | pH[3] = D; |
| 136 | pH[4] = E; |
| 137 | } |
| 138 | |
| 139 | /* Fill W buffer */ |
| 140 | switch(tailIndex) { |
| 141 | default: |
| 142 | Wf(16); |
| 143 | Wf(17); |
| 144 | Wf(18); |
| 145 | Wf(19); |
| 146 | Wf(22); |
| 147 | Wf(25); |
| 148 | case 32: /* 21,23 */ |
| 149 | Wf(21); /* 24,29,35,37 */ |
| 150 | Wf(24); /* 27,... */ |
| 151 | Wf(27); /* 30,... */ |
| 152 | Wf(30); /* */ |
| 153 | case 52: /* 20,26,28 */ |
| 154 | Wf(20); /* 23,28,34,36 */ |
| 155 | Wf(23); /* 26,31,... */ |
| 156 | Wf(26); /* 29,... */ |
| 157 | Wf(28); /* 31,... */ |
| 158 | Wf(29); /* */ |
| 159 | Wf(31); /* */ |
| 160 | } |
| 161 | for(t=32; t < 80; t++) |
| 162 | Wf(t); |
| 163 | |
| 164 | /* Set up working variables */ |
| 165 | A = pH[0]; |
| 166 | B = pH[1]; |
| 167 | C = pH[2]; |
| 168 | D = pH[3]; |
| 169 | E = pH[4]; |
| 170 | |
| 171 | /* Do the rounds */ |
| 172 | switch(tailIndex) { |
| 173 | default: |
| 174 | ROUND( 0, A, B, C, D, E, F1, K1 ); |
| 175 | ROUND( 1, E, A, B, C, D, F1, K1 ); |
| 176 | ROUND( 2, D, E, A, B, C, F1, K1 ); |
| 177 | ROUND( 3, C, D, E, A, B, F1, K1 ); |
| 178 | ROUND( 4, B, C, D, E, A, F1, K1 ); |
| 179 | ROUND( 5, A, B, C, D, E, F1, K1 ); |
| 180 | ROUND( 6, E, A, B, C, D, F1, K1 ); |
| 181 | case 32: |
| 182 | ROUND( 7, D, E, A, B, C, F1, K1 ); |
| 183 | ROUND( 8, C, D, E, A, B, F1, K1 ); |
| 184 | ROUND( 9, B, C, D, E, A, F1, K1 ); |
| 185 | ROUND(10, A, B, C, D, E, F1, K1 ); |
| 186 | ROUND(11, E, A, B, C, D, F1, K1 ); |
| 187 | case 52: |
| 188 | ROUND(12, D, E, A, B, C, F1, K1 ); |
| 189 | ROUND(13, C, D, E, A, B, F1, K1 ); |
| 190 | ROUND(14, B, C, D, E, A, F1, K1 ); |
| 191 | ROUND(15, A, B, C, D, E, F1, K1 ); |
| 192 | ROUND(16, E, A, B, C, D, F1, K1 ); |
| 193 | ROUND(17, D, E, A, B, C, F1, K1 ); |
| 194 | ROUND(18, C, D, E, A, B, F1, K1 ); |
| 195 | ROUND(19, B, C, D, E, A, F1, K1 ); |
| 196 | } |
| 197 | |
| 198 | ROUND20(20,F2,K2); |
| 199 | ROUND20(40,F3,K3); |
| 200 | ROUND20(60,F4,K4); |
| 201 | |
| 202 | /* Mix in the IV again */ |
| 203 | A += H[0]; |
| 204 | B += H[1]; |
| 205 | C += H[2]; |
| 206 | D += H[3]; |
| 207 | E += H[4]; |
| 208 | |
| 209 | /* Is this the best bit count so far? */ |
| 210 | if(!(A & bitMask1Low) && !(B & bitMask1High)) { |
| 211 | /* Count bits */ |
| 212 | gotBits = 0; |
| 213 | if(A) { |
| 214 | s = A; |
| 215 | while(!(s & 0x80000000)) { |
| 216 | s <<= 1; |
| 217 | gotBits++; |
| 218 | } |
| 219 | } else { |
| 220 | gotBits = 32; |
| 221 | if(B) { |
| 222 | s = B; |
| 223 | while(!(s & 0x80000000)) { |
| 224 | s <<= 1; |
| 225 | gotBits++; |
| 226 | } |
| 227 | } else { |
| 228 | gotBits = 64; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if ( gotBits > *best ) { *best = gotBits; } |
| 233 | /* Regenerate the bit mask */ |
| 234 | maxBits = gotBits+1; |
| 235 | if(maxBits < 32) { |
| 236 | bitMask1Low = ~((((uInt32) 1) << (32 - maxBits)) - 1); |
| 237 | bitMask1High = 0; |
| 238 | } else { |
| 239 | bitMask1Low = ~0; |
| 240 | bitMask1High = ~((((uInt32) 1) << (64 - maxBits)) - 1); |
| 241 | } |
| 242 | |
| 243 | /* Copy this result back to the block buffer */ |
| 244 | for(t=0; t < 16; t++) |
| 245 | PUT_WORD(output + t*4, W[t]); |
| 246 | |
| 247 | /* Is it good enough to bail out? */ |
| 248 | if(gotBits >= bits) { |
| 249 | return iters+1; |
| 250 | } |
| 251 | } |
| 252 | MINTER_CALLBACK(); |
| 253 | } |
| 254 | |
| 255 | return iters+1; |
| 256 | } |