| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Cryptography.Ciphers |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Implements Serpent cipher algorithm. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class SerpentCipher : BlockCipher |
| | | 9 | | { |
| | | 10 | | private const int Rounds = 32; |
| | | 11 | | private const int Phi = unchecked((int)0x9E3779B9); // (Sqrt(5) - 1) * 2**31 |
| | | 12 | | |
| | | 13 | | private readonly int[] _workingKey; |
| | | 14 | | |
| | | 15 | | // registers |
| | | 16 | | private int _x0; |
| | | 17 | | private int _x1; |
| | | 18 | | private int _x2; |
| | | 19 | | private int _x3; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="SerpentCipher"/> class. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="key">The key.</param> |
| | | 25 | | /// <param name="mode">The mode.</param> |
| | | 26 | | /// <param name="padding">The padding.</param> |
| | | 27 | | /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| | | 28 | | /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception> |
| | | 29 | | public SerpentCipher(byte[] key, CipherMode mode, CipherPadding padding) |
| | 0 | 30 | | : base(key, 16, mode, padding) |
| | 0 | 31 | | { |
| | 0 | 32 | | var keySize = key.Length * 8; |
| | | 33 | | |
| | 0 | 34 | | if (keySize is not (128 or 192 or 256)) |
| | 0 | 35 | | { |
| | 0 | 36 | | throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize)); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | _workingKey = MakeWorkingKey(key); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="inputBuffer">The input data to encrypt.</param> |
| | | 46 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 47 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 48 | | /// <param name="outputBuffer">The output to which to write encrypted data.</param> |
| | | 49 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 50 | | /// <returns> |
| | | 51 | | /// The number of bytes encrypted. |
| | | 52 | | /// </returns> |
| | | 53 | | public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 0 | 54 | | { |
| | 0 | 55 | | if (inputCount != BlockSize) |
| | 0 | 56 | | { |
| | 0 | 57 | | throw new ArgumentException("inputCount"); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | _x3 = BytesToWord(inputBuffer, inputOffset); |
| | 0 | 61 | | _x2 = BytesToWord(inputBuffer, inputOffset + 4); |
| | 0 | 62 | | _x1 = BytesToWord(inputBuffer, inputOffset + 8); |
| | 0 | 63 | | _x0 = BytesToWord(inputBuffer, inputOffset + 12); |
| | | 64 | | |
| | 0 | 65 | | Sb0(_workingKey[0] ^ _x0, _workingKey[1] ^ _x1, _workingKey[2] ^ _x2, _workingKey[3] ^ _x3); |
| | 0 | 66 | | LT(); |
| | 0 | 67 | | Sb1(_workingKey[4] ^ _x0, _workingKey[5] ^ _x1, _workingKey[6] ^ _x2, _workingKey[7] ^ _x3); |
| | 0 | 68 | | LT(); |
| | 0 | 69 | | Sb2(_workingKey[8] ^ _x0, _workingKey[9] ^ _x1, _workingKey[10] ^ _x2, _workingKey[11] ^ _x3); |
| | 0 | 70 | | LT(); |
| | 0 | 71 | | Sb3(_workingKey[12] ^ _x0, _workingKey[13] ^ _x1, _workingKey[14] ^ _x2, _workingKey[15] ^ _x3); |
| | 0 | 72 | | LT(); |
| | 0 | 73 | | Sb4(_workingKey[16] ^ _x0, _workingKey[17] ^ _x1, _workingKey[18] ^ _x2, _workingKey[19] ^ _x3); |
| | 0 | 74 | | LT(); |
| | 0 | 75 | | Sb5(_workingKey[20] ^ _x0, _workingKey[21] ^ _x1, _workingKey[22] ^ _x2, _workingKey[23] ^ _x3); |
| | 0 | 76 | | LT(); |
| | 0 | 77 | | Sb6(_workingKey[24] ^ _x0, _workingKey[25] ^ _x1, _workingKey[26] ^ _x2, _workingKey[27] ^ _x3); |
| | 0 | 78 | | LT(); |
| | 0 | 79 | | Sb7(_workingKey[28] ^ _x0, _workingKey[29] ^ _x1, _workingKey[30] ^ _x2, _workingKey[31] ^ _x3); |
| | 0 | 80 | | LT(); |
| | 0 | 81 | | Sb0(_workingKey[32] ^ _x0, _workingKey[33] ^ _x1, _workingKey[34] ^ _x2, _workingKey[35] ^ _x3); |
| | 0 | 82 | | LT(); |
| | 0 | 83 | | Sb1(_workingKey[36] ^ _x0, _workingKey[37] ^ _x1, _workingKey[38] ^ _x2, _workingKey[39] ^ _x3); |
| | 0 | 84 | | LT(); |
| | 0 | 85 | | Sb2(_workingKey[40] ^ _x0, _workingKey[41] ^ _x1, _workingKey[42] ^ _x2, _workingKey[43] ^ _x3); |
| | 0 | 86 | | LT(); |
| | 0 | 87 | | Sb3(_workingKey[44] ^ _x0, _workingKey[45] ^ _x1, _workingKey[46] ^ _x2, _workingKey[47] ^ _x3); |
| | 0 | 88 | | LT(); |
| | 0 | 89 | | Sb4(_workingKey[48] ^ _x0, _workingKey[49] ^ _x1, _workingKey[50] ^ _x2, _workingKey[51] ^ _x3); |
| | 0 | 90 | | LT(); |
| | 0 | 91 | | Sb5(_workingKey[52] ^ _x0, _workingKey[53] ^ _x1, _workingKey[54] ^ _x2, _workingKey[55] ^ _x3); |
| | 0 | 92 | | LT(); |
| | 0 | 93 | | Sb6(_workingKey[56] ^ _x0, _workingKey[57] ^ _x1, _workingKey[58] ^ _x2, _workingKey[59] ^ _x3); |
| | 0 | 94 | | LT(); |
| | 0 | 95 | | Sb7(_workingKey[60] ^ _x0, _workingKey[61] ^ _x1, _workingKey[62] ^ _x2, _workingKey[63] ^ _x3); |
| | 0 | 96 | | LT(); |
| | 0 | 97 | | Sb0(_workingKey[64] ^ _x0, _workingKey[65] ^ _x1, _workingKey[66] ^ _x2, _workingKey[67] ^ _x3); |
| | 0 | 98 | | LT(); |
| | 0 | 99 | | Sb1(_workingKey[68] ^ _x0, _workingKey[69] ^ _x1, _workingKey[70] ^ _x2, _workingKey[71] ^ _x3); |
| | 0 | 100 | | LT(); |
| | 0 | 101 | | Sb2(_workingKey[72] ^ _x0, _workingKey[73] ^ _x1, _workingKey[74] ^ _x2, _workingKey[75] ^ _x3); |
| | 0 | 102 | | LT(); |
| | 0 | 103 | | Sb3(_workingKey[76] ^ _x0, _workingKey[77] ^ _x1, _workingKey[78] ^ _x2, _workingKey[79] ^ _x3); |
| | 0 | 104 | | LT(); |
| | 0 | 105 | | Sb4(_workingKey[80] ^ _x0, _workingKey[81] ^ _x1, _workingKey[82] ^ _x2, _workingKey[83] ^ _x3); |
| | 0 | 106 | | LT(); |
| | 0 | 107 | | Sb5(_workingKey[84] ^ _x0, _workingKey[85] ^ _x1, _workingKey[86] ^ _x2, _workingKey[87] ^ _x3); |
| | 0 | 108 | | LT(); |
| | 0 | 109 | | Sb6(_workingKey[88] ^ _x0, _workingKey[89] ^ _x1, _workingKey[90] ^ _x2, _workingKey[91] ^ _x3); |
| | 0 | 110 | | LT(); |
| | 0 | 111 | | Sb7(_workingKey[92] ^ _x0, _workingKey[93] ^ _x1, _workingKey[94] ^ _x2, _workingKey[95] ^ _x3); |
| | 0 | 112 | | LT(); |
| | 0 | 113 | | Sb0(_workingKey[96] ^ _x0, _workingKey[97] ^ _x1, _workingKey[98] ^ _x2, _workingKey[99] ^ _x3); |
| | 0 | 114 | | LT(); |
| | 0 | 115 | | Sb1(_workingKey[100] ^ _x0, _workingKey[101] ^ _x1, _workingKey[102] ^ _x2, _workingKey[103] ^ _x3); |
| | 0 | 116 | | LT(); |
| | 0 | 117 | | Sb2(_workingKey[104] ^ _x0, _workingKey[105] ^ _x1, _workingKey[106] ^ _x2, _workingKey[107] ^ _x3); |
| | 0 | 118 | | LT(); |
| | 0 | 119 | | Sb3(_workingKey[108] ^ _x0, _workingKey[109] ^ _x1, _workingKey[110] ^ _x2, _workingKey[111] ^ _x3); |
| | 0 | 120 | | LT(); |
| | 0 | 121 | | Sb4(_workingKey[112] ^ _x0, _workingKey[113] ^ _x1, _workingKey[114] ^ _x2, _workingKey[115] ^ _x3); |
| | 0 | 122 | | LT(); |
| | 0 | 123 | | Sb5(_workingKey[116] ^ _x0, _workingKey[117] ^ _x1, _workingKey[118] ^ _x2, _workingKey[119] ^ _x3); |
| | 0 | 124 | | LT(); |
| | 0 | 125 | | Sb6(_workingKey[120] ^ _x0, _workingKey[121] ^ _x1, _workingKey[122] ^ _x2, _workingKey[123] ^ _x3); |
| | 0 | 126 | | LT(); |
| | 0 | 127 | | Sb7(_workingKey[124] ^ _x0, _workingKey[125] ^ _x1, _workingKey[126] ^ _x2, _workingKey[127] ^ _x3); |
| | | 128 | | |
| | 0 | 129 | | WordToBytes(_workingKey[131] ^ _x3, outputBuffer, outputOffset); |
| | 0 | 130 | | WordToBytes(_workingKey[130] ^ _x2, outputBuffer, outputOffset + 4); |
| | 0 | 131 | | WordToBytes(_workingKey[129] ^ _x1, outputBuffer, outputOffset + 8); |
| | 0 | 132 | | WordToBytes(_workingKey[128] ^ _x0, outputBuffer, outputOffset + 12); |
| | | 133 | | |
| | 0 | 134 | | return BlockSize; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="inputBuffer">The input data to decrypt.</param> |
| | | 141 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 142 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 143 | | /// <param name="outputBuffer">The output to which to write decrypted data.</param> |
| | | 144 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 145 | | /// <returns> |
| | | 146 | | /// The number of bytes decrypted. |
| | | 147 | | /// </returns> |
| | | 148 | | public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 0 | 149 | | { |
| | 0 | 150 | | if (inputCount != BlockSize) |
| | 0 | 151 | | { |
| | 0 | 152 | | throw new ArgumentException("inputCount"); |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | _x3 = _workingKey[131] ^ BytesToWord(inputBuffer, inputOffset); |
| | 0 | 156 | | _x2 = _workingKey[130] ^ BytesToWord(inputBuffer, inputOffset + 4); |
| | 0 | 157 | | _x1 = _workingKey[129] ^ BytesToWord(inputBuffer, inputOffset + 8); |
| | 0 | 158 | | _x0 = _workingKey[128] ^ BytesToWord(inputBuffer, inputOffset + 12); |
| | | 159 | | |
| | 0 | 160 | | Ib7(_x0, _x1, _x2, _x3); |
| | | 161 | | |
| | 0 | 162 | | _x0 ^= _workingKey[124]; |
| | 0 | 163 | | _x1 ^= _workingKey[125]; |
| | 0 | 164 | | _x2 ^= _workingKey[126]; |
| | 0 | 165 | | _x3 ^= _workingKey[127]; |
| | | 166 | | |
| | 0 | 167 | | InverseLT(); |
| | 0 | 168 | | Ib6(_x0, _x1, _x2, _x3); |
| | | 169 | | |
| | 0 | 170 | | _x0 ^= _workingKey[120]; |
| | 0 | 171 | | _x1 ^= _workingKey[121]; |
| | 0 | 172 | | _x2 ^= _workingKey[122]; |
| | 0 | 173 | | _x3 ^= _workingKey[123]; |
| | | 174 | | |
| | 0 | 175 | | InverseLT(); |
| | 0 | 176 | | Ib5(_x0, _x1, _x2, _x3); |
| | | 177 | | |
| | 0 | 178 | | _x0 ^= _workingKey[116]; |
| | 0 | 179 | | _x1 ^= _workingKey[117]; |
| | 0 | 180 | | _x2 ^= _workingKey[118]; |
| | 0 | 181 | | _x3 ^= _workingKey[119]; |
| | | 182 | | |
| | 0 | 183 | | InverseLT(); |
| | 0 | 184 | | Ib4(_x0, _x1, _x2, _x3); |
| | | 185 | | |
| | 0 | 186 | | _x0 ^= _workingKey[112]; |
| | 0 | 187 | | _x1 ^= _workingKey[113]; |
| | 0 | 188 | | _x2 ^= _workingKey[114]; |
| | 0 | 189 | | _x3 ^= _workingKey[115]; |
| | 0 | 190 | | InverseLT(); |
| | 0 | 191 | | Ib3(_x0, _x1, _x2, _x3); |
| | | 192 | | |
| | 0 | 193 | | _x0 ^= _workingKey[108]; |
| | 0 | 194 | | _x1 ^= _workingKey[109]; |
| | 0 | 195 | | _x2 ^= _workingKey[110]; |
| | 0 | 196 | | _x3 ^= _workingKey[111]; |
| | | 197 | | |
| | 0 | 198 | | InverseLT(); |
| | 0 | 199 | | Ib2(_x0, _x1, _x2, _x3); |
| | | 200 | | |
| | 0 | 201 | | _x0 ^= _workingKey[104]; |
| | 0 | 202 | | _x1 ^= _workingKey[105]; |
| | 0 | 203 | | _x2 ^= _workingKey[106]; |
| | 0 | 204 | | _x3 ^= _workingKey[107]; |
| | | 205 | | |
| | 0 | 206 | | InverseLT(); |
| | 0 | 207 | | Ib1(_x0, _x1, _x2, _x3); |
| | | 208 | | |
| | 0 | 209 | | _x0 ^= _workingKey[100]; |
| | 0 | 210 | | _x1 ^= _workingKey[101]; |
| | 0 | 211 | | _x2 ^= _workingKey[102]; |
| | 0 | 212 | | _x3 ^= _workingKey[103]; |
| | | 213 | | |
| | 0 | 214 | | InverseLT(); |
| | 0 | 215 | | Ib0(_x0, _x1, _x2, _x3); |
| | | 216 | | |
| | 0 | 217 | | _x0 ^= _workingKey[96]; |
| | 0 | 218 | | _x1 ^= _workingKey[97]; |
| | 0 | 219 | | _x2 ^= _workingKey[98]; |
| | 0 | 220 | | _x3 ^= _workingKey[99]; |
| | | 221 | | |
| | 0 | 222 | | InverseLT(); |
| | 0 | 223 | | Ib7(_x0, _x1, _x2, _x3); |
| | | 224 | | |
| | 0 | 225 | | _x0 ^= _workingKey[92]; |
| | 0 | 226 | | _x1 ^= _workingKey[93]; |
| | 0 | 227 | | _x2 ^= _workingKey[94]; |
| | 0 | 228 | | _x3 ^= _workingKey[95]; |
| | | 229 | | |
| | 0 | 230 | | InverseLT(); |
| | 0 | 231 | | Ib6(_x0, _x1, _x2, _x3); |
| | | 232 | | |
| | 0 | 233 | | _x0 ^= _workingKey[88]; |
| | 0 | 234 | | _x1 ^= _workingKey[89]; |
| | 0 | 235 | | _x2 ^= _workingKey[90]; |
| | 0 | 236 | | _x3 ^= _workingKey[91]; |
| | | 237 | | |
| | 0 | 238 | | InverseLT(); |
| | 0 | 239 | | Ib5(_x0, _x1, _x2, _x3); |
| | | 240 | | |
| | 0 | 241 | | _x0 ^= _workingKey[84]; |
| | 0 | 242 | | _x1 ^= _workingKey[85]; |
| | 0 | 243 | | _x2 ^= _workingKey[86]; |
| | 0 | 244 | | _x3 ^= _workingKey[87]; |
| | | 245 | | |
| | 0 | 246 | | InverseLT(); |
| | 0 | 247 | | Ib4(_x0, _x1, _x2, _x3); |
| | | 248 | | |
| | 0 | 249 | | _x0 ^= _workingKey[80]; |
| | 0 | 250 | | _x1 ^= _workingKey[81]; |
| | 0 | 251 | | _x2 ^= _workingKey[82]; |
| | 0 | 252 | | _x3 ^= _workingKey[83]; |
| | | 253 | | |
| | 0 | 254 | | InverseLT(); |
| | 0 | 255 | | Ib3(_x0, _x1, _x2, _x3); |
| | | 256 | | |
| | 0 | 257 | | _x0 ^= _workingKey[76]; |
| | 0 | 258 | | _x1 ^= _workingKey[77]; |
| | 0 | 259 | | _x2 ^= _workingKey[78]; |
| | 0 | 260 | | _x3 ^= _workingKey[79]; |
| | | 261 | | |
| | 0 | 262 | | InverseLT(); |
| | 0 | 263 | | Ib2(_x0, _x1, _x2, _x3); |
| | | 264 | | |
| | 0 | 265 | | _x0 ^= _workingKey[72]; |
| | 0 | 266 | | _x1 ^= _workingKey[73]; |
| | 0 | 267 | | _x2 ^= _workingKey[74]; |
| | 0 | 268 | | _x3 ^= _workingKey[75]; |
| | | 269 | | |
| | 0 | 270 | | InverseLT(); |
| | 0 | 271 | | Ib1(_x0, _x1, _x2, _x3); |
| | | 272 | | |
| | 0 | 273 | | _x0 ^= _workingKey[68]; |
| | 0 | 274 | | _x1 ^= _workingKey[69]; |
| | 0 | 275 | | _x2 ^= _workingKey[70]; |
| | 0 | 276 | | _x3 ^= _workingKey[71]; |
| | | 277 | | |
| | 0 | 278 | | InverseLT(); |
| | 0 | 279 | | Ib0(_x0, _x1, _x2, _x3); |
| | | 280 | | |
| | 0 | 281 | | _x0 ^= _workingKey[64]; |
| | 0 | 282 | | _x1 ^= _workingKey[65]; |
| | 0 | 283 | | _x2 ^= _workingKey[66]; |
| | 0 | 284 | | _x3 ^= _workingKey[67]; |
| | | 285 | | |
| | 0 | 286 | | InverseLT(); |
| | 0 | 287 | | Ib7(_x0, _x1, _x2, _x3); |
| | | 288 | | |
| | 0 | 289 | | _x0 ^= _workingKey[60]; |
| | 0 | 290 | | _x1 ^= _workingKey[61]; |
| | 0 | 291 | | _x2 ^= _workingKey[62]; |
| | 0 | 292 | | _x3 ^= _workingKey[63]; |
| | | 293 | | |
| | 0 | 294 | | InverseLT(); |
| | 0 | 295 | | Ib6(_x0, _x1, _x2, _x3); |
| | | 296 | | |
| | 0 | 297 | | _x0 ^= _workingKey[56]; |
| | 0 | 298 | | _x1 ^= _workingKey[57]; |
| | 0 | 299 | | _x2 ^= _workingKey[58]; |
| | 0 | 300 | | _x3 ^= _workingKey[59]; |
| | | 301 | | |
| | 0 | 302 | | InverseLT(); |
| | 0 | 303 | | Ib5(_x0, _x1, _x2, _x3); |
| | | 304 | | |
| | 0 | 305 | | _x0 ^= _workingKey[52]; |
| | 0 | 306 | | _x1 ^= _workingKey[53]; |
| | 0 | 307 | | _x2 ^= _workingKey[54]; |
| | 0 | 308 | | _x3 ^= _workingKey[55]; |
| | | 309 | | |
| | 0 | 310 | | InverseLT(); |
| | 0 | 311 | | Ib4(_x0, _x1, _x2, _x3); |
| | | 312 | | |
| | 0 | 313 | | _x0 ^= _workingKey[48]; |
| | 0 | 314 | | _x1 ^= _workingKey[49]; |
| | 0 | 315 | | _x2 ^= _workingKey[50]; |
| | 0 | 316 | | _x3 ^= _workingKey[51]; |
| | | 317 | | |
| | 0 | 318 | | InverseLT(); |
| | 0 | 319 | | Ib3(_x0, _x1, _x2, _x3); |
| | | 320 | | |
| | 0 | 321 | | _x0 ^= _workingKey[44]; |
| | 0 | 322 | | _x1 ^= _workingKey[45]; |
| | 0 | 323 | | _x2 ^= _workingKey[46]; |
| | 0 | 324 | | _x3 ^= _workingKey[47]; |
| | | 325 | | |
| | 0 | 326 | | InverseLT(); |
| | 0 | 327 | | Ib2(_x0, _x1, _x2, _x3); |
| | | 328 | | |
| | 0 | 329 | | _x0 ^= _workingKey[40]; |
| | 0 | 330 | | _x1 ^= _workingKey[41]; |
| | 0 | 331 | | _x2 ^= _workingKey[42]; |
| | 0 | 332 | | _x3 ^= _workingKey[43]; |
| | | 333 | | |
| | 0 | 334 | | InverseLT(); |
| | 0 | 335 | | Ib1(_x0, _x1, _x2, _x3); |
| | | 336 | | |
| | 0 | 337 | | _x0 ^= _workingKey[36]; |
| | 0 | 338 | | _x1 ^= _workingKey[37]; |
| | 0 | 339 | | _x2 ^= _workingKey[38]; |
| | 0 | 340 | | _x3 ^= _workingKey[39]; |
| | | 341 | | |
| | 0 | 342 | | InverseLT(); |
| | 0 | 343 | | Ib0(_x0, _x1, _x2, _x3); |
| | | 344 | | |
| | 0 | 345 | | _x0 ^= _workingKey[32]; |
| | 0 | 346 | | _x1 ^= _workingKey[33]; |
| | 0 | 347 | | _x2 ^= _workingKey[34]; |
| | 0 | 348 | | _x3 ^= _workingKey[35]; |
| | | 349 | | |
| | 0 | 350 | | InverseLT(); |
| | 0 | 351 | | Ib7(_x0, _x1, _x2, _x3); |
| | | 352 | | |
| | 0 | 353 | | _x0 ^= _workingKey[28]; |
| | 0 | 354 | | _x1 ^= _workingKey[29]; |
| | 0 | 355 | | _x2 ^= _workingKey[30]; |
| | 0 | 356 | | _x3 ^= _workingKey[31]; |
| | | 357 | | |
| | 0 | 358 | | InverseLT(); |
| | 0 | 359 | | Ib6(_x0, _x1, _x2, _x3); |
| | | 360 | | |
| | 0 | 361 | | _x0 ^= _workingKey[24]; |
| | 0 | 362 | | _x1 ^= _workingKey[25]; |
| | 0 | 363 | | _x2 ^= _workingKey[26]; |
| | 0 | 364 | | _x3 ^= _workingKey[27]; |
| | | 365 | | |
| | 0 | 366 | | InverseLT(); |
| | 0 | 367 | | Ib5(_x0, _x1, _x2, _x3); |
| | | 368 | | |
| | 0 | 369 | | _x0 ^= _workingKey[20]; |
| | 0 | 370 | | _x1 ^= _workingKey[21]; |
| | 0 | 371 | | _x2 ^= _workingKey[22]; |
| | 0 | 372 | | _x3 ^= _workingKey[23]; |
| | | 373 | | |
| | 0 | 374 | | InverseLT(); |
| | 0 | 375 | | Ib4(_x0, _x1, _x2, _x3); |
| | | 376 | | |
| | 0 | 377 | | _x0 ^= _workingKey[16]; |
| | 0 | 378 | | _x1 ^= _workingKey[17]; |
| | 0 | 379 | | _x2 ^= _workingKey[18]; |
| | 0 | 380 | | _x3 ^= _workingKey[19]; |
| | | 381 | | |
| | 0 | 382 | | InverseLT(); |
| | 0 | 383 | | Ib3(_x0, _x1, _x2, _x3); |
| | | 384 | | |
| | 0 | 385 | | _x0 ^= _workingKey[12]; |
| | 0 | 386 | | _x1 ^= _workingKey[13]; |
| | 0 | 387 | | _x2 ^= _workingKey[14]; |
| | 0 | 388 | | _x3 ^= _workingKey[15]; |
| | | 389 | | |
| | 0 | 390 | | InverseLT(); |
| | 0 | 391 | | Ib2(_x0, _x1, _x2, _x3); |
| | | 392 | | |
| | 0 | 393 | | _x0 ^= _workingKey[8]; |
| | 0 | 394 | | _x1 ^= _workingKey[9]; |
| | 0 | 395 | | _x2 ^= _workingKey[10]; |
| | 0 | 396 | | _x3 ^= _workingKey[11]; |
| | | 397 | | |
| | 0 | 398 | | InverseLT(); |
| | 0 | 399 | | Ib1(_x0, _x1, _x2, _x3); |
| | | 400 | | |
| | 0 | 401 | | _x0 ^= _workingKey[4]; |
| | 0 | 402 | | _x1 ^= _workingKey[5]; |
| | 0 | 403 | | _x2 ^= _workingKey[6]; |
| | 0 | 404 | | _x3 ^= _workingKey[7]; |
| | | 405 | | |
| | 0 | 406 | | InverseLT(); |
| | 0 | 407 | | Ib0(_x0, _x1, _x2, _x3); |
| | | 408 | | |
| | 0 | 409 | | WordToBytes(_x3 ^ _workingKey[3], outputBuffer, outputOffset); |
| | 0 | 410 | | WordToBytes(_x2 ^ _workingKey[2], outputBuffer, outputOffset + 4); |
| | 0 | 411 | | WordToBytes(_x1 ^ _workingKey[1], outputBuffer, outputOffset + 8); |
| | 0 | 412 | | WordToBytes(_x0 ^ _workingKey[0], outputBuffer, outputOffset + 12); |
| | | 413 | | |
| | 0 | 414 | | return BlockSize; |
| | 0 | 415 | | } |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Expand a user-supplied key material into a session key. |
| | | 419 | | /// </summary> |
| | | 420 | | /// <param name="key">The user-key bytes to use.</param> |
| | | 421 | | /// <returns> |
| | | 422 | | /// A session key. |
| | | 423 | | /// </returns> |
| | | 424 | | /// <exception cref="ArgumentException"><paramref name="key"/> is not multiple of 4 bytes.</exception> |
| | | 425 | | private int[] MakeWorkingKey(byte[] key) |
| | 0 | 426 | | { |
| | | 427 | | // pad key to 256 bits |
| | 0 | 428 | | var kPad = new int[16]; |
| | | 429 | | int off; |
| | 0 | 430 | | var length = 0; |
| | | 431 | | |
| | 0 | 432 | | for (off = key.Length - 4; off > 0; off -= 4) |
| | 0 | 433 | | { |
| | 0 | 434 | | kPad[length++] = BytesToWord(key, off); |
| | 0 | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | if (off == 0) |
| | 0 | 438 | | { |
| | 0 | 439 | | kPad[length++] = BytesToWord(key, 0); |
| | 0 | 440 | | if (length < 8) |
| | 0 | 441 | | { |
| | 0 | 442 | | kPad[length] = 1; |
| | 0 | 443 | | } |
| | 0 | 444 | | } |
| | | 445 | | else |
| | 0 | 446 | | { |
| | 0 | 447 | | throw new ArgumentException("key must be a multiple of 4 bytes"); |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | // expand the padded key up to 33 x 128 bits of key material |
| | | 451 | | const int amount = (Rounds + 1) * 4; |
| | 0 | 452 | | var w = new int[amount]; |
| | | 453 | | |
| | | 454 | | // compute w0 to w7 from w-8 to w-1 |
| | 0 | 455 | | for (var i = 8; i < 16; i++) |
| | 0 | 456 | | { |
| | 0 | 457 | | kPad[i] = RotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3] ^ kPad[i - 1] ^ Phi ^ (i - 8), 11); |
| | 0 | 458 | | } |
| | | 459 | | |
| | 0 | 460 | | Buffer.BlockCopy(kPad, 8, w, 0, 8); |
| | | 461 | | |
| | | 462 | | // compute w8 to w136 |
| | 0 | 463 | | for (var i = 8; i < amount; i++) |
| | 0 | 464 | | { |
| | 0 | 465 | | w[i] = RotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ Phi ^ i, 11); |
| | 0 | 466 | | } |
| | | 467 | | |
| | | 468 | | // create the working keys by processing w with the Sbox and IP |
| | 0 | 469 | | Sb3(w[0], w[1], w[2], w[3]); |
| | 0 | 470 | | w[0] = _x0; |
| | 0 | 471 | | w[1] = _x1; |
| | 0 | 472 | | w[2] = _x2; |
| | 0 | 473 | | w[3] = _x3; |
| | | 474 | | |
| | 0 | 475 | | Sb2(w[4], w[5], w[6], w[7]); |
| | 0 | 476 | | w[4] = _x0; |
| | 0 | 477 | | w[5] = _x1; |
| | 0 | 478 | | w[6] = _x2; |
| | 0 | 479 | | w[7] = _x3; |
| | | 480 | | |
| | 0 | 481 | | Sb1(w[8], w[9], w[10], w[11]); |
| | 0 | 482 | | w[8] = _x0; |
| | 0 | 483 | | w[9] = _x1; |
| | 0 | 484 | | w[10] = _x2; |
| | 0 | 485 | | w[11] = _x3; |
| | | 486 | | |
| | 0 | 487 | | Sb0(w[12], w[13], w[14], w[15]); |
| | 0 | 488 | | w[12] = _x0; |
| | 0 | 489 | | w[13] = _x1; |
| | 0 | 490 | | w[14] = _x2; |
| | 0 | 491 | | w[15] = _x3; |
| | | 492 | | |
| | 0 | 493 | | Sb7(w[16], w[17], w[18], w[19]); |
| | 0 | 494 | | w[16] = _x0; |
| | 0 | 495 | | w[17] = _x1; |
| | 0 | 496 | | w[18] = _x2; |
| | 0 | 497 | | w[19] = _x3; |
| | | 498 | | |
| | 0 | 499 | | Sb6(w[20], w[21], w[22], w[23]); |
| | 0 | 500 | | w[20] = _x0; |
| | 0 | 501 | | w[21] = _x1; |
| | 0 | 502 | | w[22] = _x2; |
| | 0 | 503 | | w[23] = _x3; |
| | | 504 | | |
| | 0 | 505 | | Sb5(w[24], w[25], w[26], w[27]); |
| | 0 | 506 | | w[24] = _x0; |
| | 0 | 507 | | w[25] = _x1; |
| | 0 | 508 | | w[26] = _x2; |
| | 0 | 509 | | w[27] = _x3; |
| | | 510 | | |
| | 0 | 511 | | Sb4(w[28], w[29], w[30], w[31]); |
| | 0 | 512 | | w[28] = _x0; |
| | 0 | 513 | | w[29] = _x1; |
| | 0 | 514 | | w[30] = _x2; |
| | 0 | 515 | | w[31] = _x3; |
| | | 516 | | |
| | 0 | 517 | | Sb3(w[32], w[33], w[34], w[35]); |
| | 0 | 518 | | w[32] = _x0; |
| | 0 | 519 | | w[33] = _x1; |
| | 0 | 520 | | w[34] = _x2; |
| | 0 | 521 | | w[35] = _x3; |
| | | 522 | | |
| | 0 | 523 | | Sb2(w[36], w[37], w[38], w[39]); |
| | 0 | 524 | | w[36] = _x0; |
| | 0 | 525 | | w[37] = _x1; |
| | 0 | 526 | | w[38] = _x2; |
| | 0 | 527 | | w[39] = _x3; |
| | | 528 | | |
| | 0 | 529 | | Sb1(w[40], w[41], w[42], w[43]); |
| | 0 | 530 | | w[40] = _x0; |
| | 0 | 531 | | w[41] = _x1; |
| | 0 | 532 | | w[42] = _x2; |
| | 0 | 533 | | w[43] = _x3; |
| | | 534 | | |
| | 0 | 535 | | Sb0(w[44], w[45], w[46], w[47]); |
| | 0 | 536 | | w[44] = _x0; |
| | 0 | 537 | | w[45] = _x1; |
| | 0 | 538 | | w[46] = _x2; |
| | 0 | 539 | | w[47] = _x3; |
| | | 540 | | |
| | 0 | 541 | | Sb7(w[48], w[49], w[50], w[51]); |
| | 0 | 542 | | w[48] = _x0; |
| | 0 | 543 | | w[49] = _x1; |
| | 0 | 544 | | w[50] = _x2; |
| | 0 | 545 | | w[51] = _x3; |
| | | 546 | | |
| | 0 | 547 | | Sb6(w[52], w[53], w[54], w[55]); |
| | 0 | 548 | | w[52] = _x0; |
| | 0 | 549 | | w[53] = _x1; |
| | 0 | 550 | | w[54] = _x2; |
| | 0 | 551 | | w[55] = _x3; |
| | | 552 | | |
| | 0 | 553 | | Sb5(w[56], w[57], w[58], w[59]); |
| | 0 | 554 | | w[56] = _x0; |
| | 0 | 555 | | w[57] = _x1; |
| | 0 | 556 | | w[58] = _x2; |
| | 0 | 557 | | w[59] = _x3; |
| | | 558 | | |
| | 0 | 559 | | Sb4(w[60], w[61], w[62], w[63]); |
| | 0 | 560 | | w[60] = _x0; |
| | 0 | 561 | | w[61] = _x1; |
| | 0 | 562 | | w[62] = _x2; |
| | 0 | 563 | | w[63] = _x3; |
| | | 564 | | |
| | 0 | 565 | | Sb3(w[64], w[65], w[66], w[67]); |
| | 0 | 566 | | w[64] = _x0; |
| | 0 | 567 | | w[65] = _x1; |
| | 0 | 568 | | w[66] = _x2; |
| | 0 | 569 | | w[67] = _x3; |
| | | 570 | | |
| | 0 | 571 | | Sb2(w[68], w[69], w[70], w[71]); |
| | 0 | 572 | | w[68] = _x0; |
| | 0 | 573 | | w[69] = _x1; |
| | 0 | 574 | | w[70] = _x2; |
| | 0 | 575 | | w[71] = _x3; |
| | | 576 | | |
| | 0 | 577 | | Sb1(w[72], w[73], w[74], w[75]); |
| | 0 | 578 | | w[72] = _x0; |
| | 0 | 579 | | w[73] = _x1; |
| | 0 | 580 | | w[74] = _x2; |
| | 0 | 581 | | w[75] = _x3; |
| | | 582 | | |
| | 0 | 583 | | Sb0(w[76], w[77], w[78], w[79]); |
| | 0 | 584 | | w[76] = _x0; |
| | 0 | 585 | | w[77] = _x1; |
| | 0 | 586 | | w[78] = _x2; |
| | 0 | 587 | | w[79] = _x3; |
| | | 588 | | |
| | 0 | 589 | | Sb7(w[80], w[81], w[82], w[83]); |
| | 0 | 590 | | w[80] = _x0; |
| | 0 | 591 | | w[81] = _x1; |
| | 0 | 592 | | w[82] = _x2; |
| | 0 | 593 | | w[83] = _x3; |
| | | 594 | | |
| | 0 | 595 | | Sb6(w[84], w[85], w[86], w[87]); |
| | 0 | 596 | | w[84] = _x0; |
| | 0 | 597 | | w[85] = _x1; |
| | 0 | 598 | | w[86] = _x2; |
| | 0 | 599 | | w[87] = _x3; |
| | | 600 | | |
| | 0 | 601 | | Sb5(w[88], w[89], w[90], w[91]); |
| | 0 | 602 | | w[88] = _x0; |
| | 0 | 603 | | w[89] = _x1; |
| | 0 | 604 | | w[90] = _x2; |
| | 0 | 605 | | w[91] = _x3; |
| | | 606 | | |
| | 0 | 607 | | Sb4(w[92], w[93], w[94], w[95]); |
| | 0 | 608 | | w[92] = _x0; |
| | 0 | 609 | | w[93] = _x1; |
| | 0 | 610 | | w[94] = _x2; |
| | 0 | 611 | | w[95] = _x3; |
| | | 612 | | |
| | 0 | 613 | | Sb3(w[96], w[97], w[98], w[99]); |
| | 0 | 614 | | w[96] = _x0; |
| | 0 | 615 | | w[97] = _x1; |
| | 0 | 616 | | w[98] = _x2; |
| | 0 | 617 | | w[99] = _x3; |
| | | 618 | | |
| | 0 | 619 | | Sb2(w[100], w[101], w[102], w[103]); |
| | 0 | 620 | | w[100] = _x0; |
| | 0 | 621 | | w[101] = _x1; |
| | 0 | 622 | | w[102] = _x2; |
| | 0 | 623 | | w[103] = _x3; |
| | | 624 | | |
| | 0 | 625 | | Sb1(w[104], w[105], w[106], w[107]); |
| | 0 | 626 | | w[104] = _x0; |
| | 0 | 627 | | w[105] = _x1; |
| | 0 | 628 | | w[106] = _x2; |
| | 0 | 629 | | w[107] = _x3; |
| | | 630 | | |
| | 0 | 631 | | Sb0(w[108], w[109], w[110], w[111]); |
| | 0 | 632 | | w[108] = _x0; |
| | 0 | 633 | | w[109] = _x1; |
| | 0 | 634 | | w[110] = _x2; |
| | 0 | 635 | | w[111] = _x3; |
| | | 636 | | |
| | 0 | 637 | | Sb7(w[112], w[113], w[114], w[115]); |
| | 0 | 638 | | w[112] = _x0; |
| | 0 | 639 | | w[113] = _x1; |
| | 0 | 640 | | w[114] = _x2; |
| | 0 | 641 | | w[115] = _x3; |
| | | 642 | | |
| | 0 | 643 | | Sb6(w[116], w[117], w[118], w[119]); |
| | 0 | 644 | | w[116] = _x0; |
| | 0 | 645 | | w[117] = _x1; |
| | 0 | 646 | | w[118] = _x2; |
| | 0 | 647 | | w[119] = _x3; |
| | | 648 | | |
| | 0 | 649 | | Sb5(w[120], w[121], w[122], w[123]); |
| | 0 | 650 | | w[120] = _x0; |
| | 0 | 651 | | w[121] = _x1; |
| | 0 | 652 | | w[122] = _x2; |
| | 0 | 653 | | w[123] = _x3; |
| | | 654 | | |
| | 0 | 655 | | Sb4(w[124], w[125], w[126], w[127]); |
| | 0 | 656 | | w[124] = _x0; |
| | 0 | 657 | | w[125] = _x1; |
| | 0 | 658 | | w[126] = _x2; |
| | 0 | 659 | | w[127] = _x3; |
| | | 660 | | |
| | 0 | 661 | | Sb3(w[128], w[129], w[130], w[131]); |
| | 0 | 662 | | w[128] = _x0; |
| | 0 | 663 | | w[129] = _x1; |
| | 0 | 664 | | w[130] = _x2; |
| | 0 | 665 | | w[131] = _x3; |
| | | 666 | | |
| | 0 | 667 | | return w; |
| | 0 | 668 | | } |
| | | 669 | | |
| | | 670 | | private static int RotateLeft(int x, int bits) |
| | 0 | 671 | | { |
| | 0 | 672 | | return (x << bits) | (int) ((uint) x >> (32 - bits)); |
| | 0 | 673 | | } |
| | | 674 | | |
| | | 675 | | private static int RotateRight(int x, int bits) |
| | 0 | 676 | | { |
| | 0 | 677 | | return (int) ((uint) x >> bits) | (x << (32 - bits)); |
| | 0 | 678 | | } |
| | | 679 | | |
| | | 680 | | private static int BytesToWord(byte[] src, int srcOff) |
| | 0 | 681 | | { |
| | 0 | 682 | | return ((src[srcOff] & 0xff) << 24) | ((src[srcOff + 1] & 0xff) << 16) | |
| | 0 | 683 | | ((src[srcOff + 2] & 0xff) << 8) | (src[srcOff + 3] & 0xff); |
| | 0 | 684 | | } |
| | | 685 | | |
| | | 686 | | private static void WordToBytes(int word, byte[] dst, int dstOff) |
| | 0 | 687 | | { |
| | 0 | 688 | | dst[dstOff + 3] = (byte) word; |
| | 0 | 689 | | dst[dstOff + 2] = (byte) ((uint)word >> 8); |
| | 0 | 690 | | dst[dstOff + 1] = (byte) ((uint)word >> 16); |
| | 0 | 691 | | dst[dstOff] = (byte) ((uint)word >> 24); |
| | 0 | 692 | | } |
| | | 693 | | |
| | | 694 | | /* |
| | | 695 | | * The sboxes below are based on the work of Brian Gladman and |
| | | 696 | | * Sam Simpson, whose original notice appears below. |
| | | 697 | | * |
| | | 698 | | * For further details see: |
| | | 699 | | * http://fp.gladman.plus.com/cryptography_technology/serpent/ |
| | | 700 | | * |
| | | 701 | | */ |
| | | 702 | | |
| | | 703 | | /* |
| | | 704 | | * Partially optimised Serpent S Box bool functions derived |
| | | 705 | | * using a recursive descent analyser but without a full search |
| | | 706 | | * of all subtrees. This set of S boxes is the result of work |
| | | 707 | | * by Sam Simpson and Brian Gladman using the spare time on a |
| | | 708 | | * cluster of high capacity servers to search for S boxes with |
| | | 709 | | * this customised search engine. There are now an average of |
| | | 710 | | * 15.375 terms per S box. |
| | | 711 | | * |
| | | 712 | | * Copyright: Dr B. R Gladman (gladman@seven77.demon.co.uk) |
| | | 713 | | * and Sam Simpson (s.simpson@mia.co.uk) |
| | | 714 | | * 17th December 1998 |
| | | 715 | | * |
| | | 716 | | * We hereby give permission for information in this file to be |
| | | 717 | | * used freely subject only to acknowledgement of its origin. |
| | | 718 | | */ |
| | | 719 | | |
| | | 720 | | /// <summary> |
| | | 721 | | /// S0 - { 3, 8,15, 1,10, 6, 5,11,14,13, 4, 2, 7, 0, 9,12 } - 15 terms. |
| | | 722 | | /// </summary> |
| | | 723 | | /// <param name="a">A.</param> |
| | | 724 | | /// <param name="b">The b.</param> |
| | | 725 | | /// <param name="c">The c.</param> |
| | | 726 | | /// <param name="d">The d.</param> |
| | | 727 | | private void Sb0(int a, int b, int c, int d) |
| | 0 | 728 | | { |
| | 0 | 729 | | var t1 = a ^ d; |
| | 0 | 730 | | var t3 = c ^ t1; |
| | 0 | 731 | | var t4 = b ^ t3; |
| | 0 | 732 | | _x3 = (a & d) ^ t4; |
| | 0 | 733 | | var t7 = a ^ (b & t1); |
| | 0 | 734 | | _x2 = t4 ^ (c | t7); |
| | 0 | 735 | | var t12 = _x3 & (t3 ^ t7); |
| | 0 | 736 | | _x1 = (~t3) ^ t12; |
| | 0 | 737 | | _x0 = t12 ^ (~t7); |
| | 0 | 738 | | } |
| | | 739 | | |
| | | 740 | | /// <summary> |
| | | 741 | | /// InvSO - {13, 3,11, 0,10, 6, 5,12, 1,14, 4, 7,15, 9, 8, 2 } - 15 terms. |
| | | 742 | | /// </summary> |
| | | 743 | | /// <param name="a">A.</param> |
| | | 744 | | /// <param name="b">The b.</param> |
| | | 745 | | /// <param name="c">The c.</param> |
| | | 746 | | /// <param name="d">The d.</param> |
| | | 747 | | private void Ib0(int a, int b, int c, int d) |
| | 0 | 748 | | { |
| | 0 | 749 | | var t1 = ~a; |
| | 0 | 750 | | var t2 = a ^ b; |
| | 0 | 751 | | var t4 = d ^ (t1 | t2); |
| | 0 | 752 | | var t5 = c ^ t4; |
| | 0 | 753 | | _x2 = t2 ^ t5; |
| | 0 | 754 | | var t8 = t1 ^ (d & t2); |
| | 0 | 755 | | _x1 = t4 ^ (_x2 & t8); |
| | 0 | 756 | | _x3 = (a & t4) ^ (t5 | _x1); |
| | 0 | 757 | | _x0 = _x3 ^ (t5 ^ t8); |
| | 0 | 758 | | } |
| | | 759 | | |
| | | 760 | | /// <summary> |
| | | 761 | | /// S1 - {15,12, 2, 7, 9, 0, 5,10, 1,11,14, 8, 6,13, 3, 4 } - 14 terms. |
| | | 762 | | /// </summary> |
| | | 763 | | /// <param name="a">A.</param> |
| | | 764 | | /// <param name="b">The b.</param> |
| | | 765 | | /// <param name="c">The c.</param> |
| | | 766 | | /// <param name="d">The d.</param> |
| | | 767 | | private void Sb1(int a, int b, int c, int d) |
| | 0 | 768 | | { |
| | 0 | 769 | | var t2 = b ^ (~a); |
| | 0 | 770 | | var t5 = c ^ (a | t2); |
| | 0 | 771 | | _x2 = d ^ t5; |
| | 0 | 772 | | var t7 = b ^ (d | t2); |
| | 0 | 773 | | var t8 = t2 ^ _x2; |
| | 0 | 774 | | _x3 = t8 ^ (t5 & t7); |
| | 0 | 775 | | var t11 = t5 ^ t7; |
| | 0 | 776 | | _x1 = _x3 ^ t11; |
| | 0 | 777 | | _x0 = t5 ^ (t8 & t11); |
| | 0 | 778 | | } |
| | | 779 | | |
| | | 780 | | /// <summary> |
| | | 781 | | /// InvS1 - { 5, 8, 2,14,15, 6,12, 3,11, 4, 7, 9, 1,13,10, 0 } - 14 steps. |
| | | 782 | | /// </summary> |
| | | 783 | | /// <param name="a">A.</param> |
| | | 784 | | /// <param name="b">The b.</param> |
| | | 785 | | /// <param name="c">The c.</param> |
| | | 786 | | /// <param name="d">The d.</param> |
| | | 787 | | private void Ib1(int a, int b, int c, int d) |
| | 0 | 788 | | { |
| | 0 | 789 | | var t1 = b ^ d; |
| | 0 | 790 | | var t3 = a ^ (b & t1); |
| | 0 | 791 | | var t4 = t1 ^ t3; |
| | 0 | 792 | | _x3 = c ^ t4; |
| | 0 | 793 | | var t7 = b ^ (t1 & t3); |
| | 0 | 794 | | var t8 = _x3 | t7; |
| | 0 | 795 | | _x1 = t3 ^ t8; |
| | 0 | 796 | | var t10 = ~_x1; |
| | 0 | 797 | | var t11 = _x3 ^ t7; |
| | 0 | 798 | | _x0 = t10 ^ t11; |
| | 0 | 799 | | _x2 = t4 ^ (t10 | t11); |
| | 0 | 800 | | } |
| | | 801 | | |
| | | 802 | | /// <summary> |
| | | 803 | | /// S2 - { 8, 6, 7, 9, 3,12,10,15,13, 1,14, 4, 0,11, 5, 2 } - 16 terms. |
| | | 804 | | /// </summary> |
| | | 805 | | /// <param name="a">A.</param> |
| | | 806 | | /// <param name="b">The b.</param> |
| | | 807 | | /// <param name="c">The c.</param> |
| | | 808 | | /// <param name="d">The d.</param> |
| | | 809 | | private void Sb2(int a, int b, int c, int d) |
| | 0 | 810 | | { |
| | 0 | 811 | | var t1 = ~a; |
| | 0 | 812 | | var t2 = b ^ d; |
| | 0 | 813 | | var t3 = c & t1; |
| | 0 | 814 | | _x0 = t2 ^ t3; |
| | 0 | 815 | | var t5 = c ^ t1; |
| | 0 | 816 | | var t6 = c ^ _x0; |
| | 0 | 817 | | var t7 = b & t6; |
| | 0 | 818 | | _x3 = t5 ^ t7; |
| | 0 | 819 | | _x2 = a ^ ((d | t7) & (_x0 | t5)); |
| | 0 | 820 | | _x1 = (t2 ^ _x3) ^ (_x2 ^ (d | t1)); |
| | 0 | 821 | | } |
| | | 822 | | |
| | | 823 | | /// <summary> |
| | | 824 | | /// InvS2 - {12, 9,15, 4,11,14, 1, 2, 0, 3, 6,13, 5, 8,10, 7 } - 16 steps. |
| | | 825 | | /// </summary> |
| | | 826 | | /// <param name="a">A.</param> |
| | | 827 | | /// <param name="b">The b.</param> |
| | | 828 | | /// <param name="c">The c.</param> |
| | | 829 | | /// <param name="d">The d.</param> |
| | | 830 | | private void Ib2(int a, int b, int c, int d) |
| | 0 | 831 | | { |
| | 0 | 832 | | var t1 = b ^ d; |
| | 0 | 833 | | var t2 = ~t1; |
| | 0 | 834 | | var t3 = a ^ c; |
| | 0 | 835 | | var t4 = c ^ t1; |
| | 0 | 836 | | var t5 = b & t4; |
| | 0 | 837 | | _x0 = t3 ^ t5; |
| | 0 | 838 | | var t7 = a | t2; |
| | 0 | 839 | | var t8 = d ^ t7; |
| | 0 | 840 | | var t9 = t3 | t8; |
| | 0 | 841 | | _x3 = t1 ^ t9; |
| | 0 | 842 | | var t11 = ~t4; |
| | 0 | 843 | | var t12 = _x0 | _x3; |
| | 0 | 844 | | _x1 = t11 ^ t12; |
| | 0 | 845 | | _x2 = (d & t11) ^ (t3 ^ t12); |
| | 0 | 846 | | } |
| | | 847 | | |
| | | 848 | | /// <summary> |
| | | 849 | | /// S3 - { 0,15,11, 8,12, 9, 6, 3,13, 1, 2, 4,10, 7, 5,14 } - 16 terms. |
| | | 850 | | /// </summary> |
| | | 851 | | /// <param name="a">A.</param> |
| | | 852 | | /// <param name="b">The b.</param> |
| | | 853 | | /// <param name="c">The c.</param> |
| | | 854 | | /// <param name="d">The d.</param> |
| | | 855 | | private void Sb3(int a, int b, int c, int d) |
| | 0 | 856 | | { |
| | 0 | 857 | | var t1 = a ^ b; |
| | 0 | 858 | | var t2 = a & c; |
| | 0 | 859 | | var t3 = a | d; |
| | 0 | 860 | | var t4 = c ^ d; |
| | 0 | 861 | | var t5 = t1 & t3; |
| | 0 | 862 | | var t6 = t2 | t5; |
| | 0 | 863 | | _x2 = t4 ^ t6; |
| | 0 | 864 | | var t8 = b ^ t3; |
| | 0 | 865 | | var t9 = t6 ^ t8; |
| | 0 | 866 | | var t10 = t4 & t9; |
| | 0 | 867 | | _x0 = t1 ^ t10; |
| | 0 | 868 | | var t12 = _x2 & _x0; |
| | 0 | 869 | | _x1 = t9 ^ t12; |
| | 0 | 870 | | _x3 = (b | d) ^ (t4 ^ t12); |
| | 0 | 871 | | } |
| | | 872 | | |
| | | 873 | | /// <summary> |
| | | 874 | | /// InvS3 - { 0, 9,10, 7,11,14, 6,13, 3, 5,12, 2, 4, 8,15, 1 } - 15 terms. |
| | | 875 | | /// </summary> |
| | | 876 | | /// <param name="a">A.</param> |
| | | 877 | | /// <param name="b">The b.</param> |
| | | 878 | | /// <param name="c">The c.</param> |
| | | 879 | | /// <param name="d">The d.</param> |
| | | 880 | | private void Ib3(int a, int b, int c, int d) |
| | 0 | 881 | | { |
| | 0 | 882 | | var t1 = a | b; |
| | 0 | 883 | | var t2 = b ^ c; |
| | 0 | 884 | | var t3 = b & t2; |
| | 0 | 885 | | var t4 = a ^ t3; |
| | 0 | 886 | | var t5 = c ^ t4; |
| | 0 | 887 | | var t6 = d | t4; |
| | 0 | 888 | | _x0 = t2 ^ t6; |
| | 0 | 889 | | var t8 = t2 | t6; |
| | 0 | 890 | | var t9 = d ^ t8; |
| | 0 | 891 | | _x2 = t5 ^ t9; |
| | 0 | 892 | | var t11 = t1 ^ t9; |
| | 0 | 893 | | var t12 = _x0 & t11; |
| | 0 | 894 | | _x3 = t4 ^ t12; |
| | 0 | 895 | | _x1 = _x3 ^ (_x0 ^ t11); |
| | 0 | 896 | | } |
| | | 897 | | |
| | | 898 | | /// <summary> |
| | | 899 | | /// S4 - { 1,15, 8, 3,12, 0,11, 6, 2, 5, 4,10, 9,14, 7,13 } - 15 terms. |
| | | 900 | | /// </summary> |
| | | 901 | | /// <param name="a">A.</param> |
| | | 902 | | /// <param name="b">The b.</param> |
| | | 903 | | /// <param name="c">The c.</param> |
| | | 904 | | /// <param name="d">The d.</param> |
| | | 905 | | private void Sb4(int a, int b, int c, int d) |
| | 0 | 906 | | { |
| | 0 | 907 | | var t1 = a ^ d; |
| | 0 | 908 | | var t2 = d & t1; |
| | 0 | 909 | | var t3 = c ^ t2; |
| | 0 | 910 | | var t4 = b | t3; |
| | 0 | 911 | | _x3 = t1 ^ t4; |
| | 0 | 912 | | var t6 = ~b; |
| | 0 | 913 | | var t7 = t1 | t6; |
| | 0 | 914 | | _x0 = t3 ^ t7; |
| | 0 | 915 | | var t9 = a & _x0; |
| | 0 | 916 | | var t10 = t1 ^ t6; |
| | 0 | 917 | | var t11 = t4 & t10; |
| | 0 | 918 | | _x2 = t9 ^ t11; |
| | 0 | 919 | | _x1 = (a ^ t3) ^ (t10 & _x2); |
| | 0 | 920 | | } |
| | | 921 | | |
| | | 922 | | /// <summary> |
| | | 923 | | /// InvS4 - { 5, 0, 8, 3,10, 9, 7,14, 2,12,11, 6, 4,15,13, 1 } - 15 terms. |
| | | 924 | | /// </summary> |
| | | 925 | | /// <param name="a">A.</param> |
| | | 926 | | /// <param name="b">The b.</param> |
| | | 927 | | /// <param name="c">The c.</param> |
| | | 928 | | /// <param name="d">The d.</param> |
| | | 929 | | private void Ib4(int a, int b, int c, int d) |
| | 0 | 930 | | { |
| | 0 | 931 | | var t1 = c | d; |
| | 0 | 932 | | var t2 = a & t1; |
| | 0 | 933 | | var t3 = b ^ t2; |
| | 0 | 934 | | var t4 = a & t3; |
| | 0 | 935 | | var t5 = c ^ t4; |
| | 0 | 936 | | _x1 = d ^ t5; |
| | 0 | 937 | | var t7 = ~a; |
| | 0 | 938 | | var t8 = t5 & _x1; |
| | 0 | 939 | | _x3 = t3 ^ t8; |
| | 0 | 940 | | var t10 = _x1 | t7; |
| | 0 | 941 | | var t11 = d ^ t10; |
| | 0 | 942 | | _x0 = _x3 ^ t11; |
| | 0 | 943 | | _x2 = (t3 & t11) ^ (_x1 ^ t7); |
| | 0 | 944 | | } |
| | | 945 | | |
| | | 946 | | /// <summary> |
| | | 947 | | /// S5 - {15, 5, 2,11, 4,10, 9,12, 0, 3,14, 8,13, 6, 7, 1 } - 16 terms. |
| | | 948 | | /// </summary> |
| | | 949 | | /// <param name="a">A.</param> |
| | | 950 | | /// <param name="b">The b.</param> |
| | | 951 | | /// <param name="c">The c.</param> |
| | | 952 | | /// <param name="d">The d.</param> |
| | | 953 | | private void Sb5(int a, int b, int c, int d) |
| | 0 | 954 | | { |
| | 0 | 955 | | var t1 = ~a; |
| | 0 | 956 | | var t2 = a ^ b; |
| | 0 | 957 | | var t3 = a ^ d; |
| | 0 | 958 | | var t4 = c ^ t1; |
| | 0 | 959 | | var t5 = t2 | t3; |
| | 0 | 960 | | _x0 = t4 ^ t5; |
| | 0 | 961 | | var t7 = d & _x0; |
| | 0 | 962 | | var t8 = t2 ^ _x0; |
| | 0 | 963 | | _x1 = t7 ^ t8; |
| | 0 | 964 | | var t10 = t1 | _x0; |
| | 0 | 965 | | var t11 = t2 | t7; |
| | 0 | 966 | | var t12 = t3 ^ t10; |
| | 0 | 967 | | _x2 = t11 ^ t12; |
| | 0 | 968 | | _x3 = (b ^ t7) ^ (_x1 & t12); |
| | 0 | 969 | | } |
| | | 970 | | |
| | | 971 | | /// <summary> |
| | | 972 | | /// InvS5 - { 8,15, 2, 9, 4, 1,13,14,11, 6, 5, 3, 7,12,10, 0 } - 16 terms. |
| | | 973 | | /// </summary> |
| | | 974 | | /// <param name="a">A.</param> |
| | | 975 | | /// <param name="b">The b.</param> |
| | | 976 | | /// <param name="c">The c.</param> |
| | | 977 | | /// <param name="d">The d.</param> |
| | | 978 | | private void Ib5(int a, int b, int c, int d) |
| | 0 | 979 | | { |
| | 0 | 980 | | var t1 = ~c; |
| | 0 | 981 | | var t2 = b & t1; |
| | 0 | 982 | | var t3 = d ^ t2; |
| | 0 | 983 | | var t4 = a & t3; |
| | 0 | 984 | | var t5 = b ^ t1; |
| | 0 | 985 | | _x3 = t4 ^ t5; |
| | 0 | 986 | | var t7 = b | _x3; |
| | 0 | 987 | | var t8 = a & t7; |
| | 0 | 988 | | _x1 = t3 ^ t8; |
| | 0 | 989 | | var t10 = a | d; |
| | 0 | 990 | | var t11 = t1 ^ t7; |
| | 0 | 991 | | _x0 = t10 ^ t11; |
| | 0 | 992 | | _x2 = (b & t10) ^ (t4 | (a ^ c)); |
| | 0 | 993 | | } |
| | | 994 | | |
| | | 995 | | /// <summary> |
| | | 996 | | /// S6 - { 7, 2,12, 5, 8, 4, 6,11,14, 9, 1,15,13, 3,10, 0 } - 15 terms. |
| | | 997 | | /// </summary> |
| | | 998 | | /// <param name="a">A.</param> |
| | | 999 | | /// <param name="b">The b.</param> |
| | | 1000 | | /// <param name="c">The c.</param> |
| | | 1001 | | /// <param name="d">The d.</param> |
| | | 1002 | | private void Sb6(int a, int b, int c, int d) |
| | 0 | 1003 | | { |
| | 0 | 1004 | | var t1 = ~a; |
| | 0 | 1005 | | var t2 = a ^ d; |
| | 0 | 1006 | | var t3 = b ^ t2; |
| | 0 | 1007 | | var t4 = t1 | t2; |
| | 0 | 1008 | | var t5 = c ^ t4; |
| | 0 | 1009 | | _x1 = b ^ t5; |
| | 0 | 1010 | | var t7 = t2 | _x1; |
| | 0 | 1011 | | var t8 = d ^ t7; |
| | 0 | 1012 | | var t9 = t5 & t8; |
| | 0 | 1013 | | _x2 = t3 ^ t9; |
| | 0 | 1014 | | var t11 = t5 ^ t8; |
| | 0 | 1015 | | _x0 = _x2 ^ t11; |
| | 0 | 1016 | | _x3 = (~t5) ^ (t3 & t11); |
| | 0 | 1017 | | } |
| | | 1018 | | |
| | | 1019 | | /// <summary> |
| | | 1020 | | /// InvS6 - {15,10, 1,13, 5, 3, 6, 0, 4, 9,14, 7, 2,12, 8,11 } - 15 terms. |
| | | 1021 | | /// </summary> |
| | | 1022 | | /// <param name="a">A.</param> |
| | | 1023 | | /// <param name="b">The b.</param> |
| | | 1024 | | /// <param name="c">The c.</param> |
| | | 1025 | | /// <param name="d">The d.</param> |
| | | 1026 | | private void Ib6(int a, int b, int c, int d) |
| | 0 | 1027 | | { |
| | 0 | 1028 | | var t1 = ~a; |
| | 0 | 1029 | | var t2 = a ^ b; |
| | 0 | 1030 | | var t3 = c ^ t2; |
| | 0 | 1031 | | var t4 = c | t1; |
| | 0 | 1032 | | var t5 = d ^ t4; |
| | 0 | 1033 | | _x1 = t3 ^ t5; |
| | 0 | 1034 | | var t7 = t3 & t5; |
| | 0 | 1035 | | var t8 = t2 ^ t7; |
| | 0 | 1036 | | var t9 = b | t8; |
| | 0 | 1037 | | _x3 = t5 ^ t9; |
| | 0 | 1038 | | var t11 = b | _x3; |
| | 0 | 1039 | | _x0 = t8 ^ t11; |
| | 0 | 1040 | | _x2 = (d & t1) ^ (t3 ^ t11); |
| | 0 | 1041 | | } |
| | | 1042 | | |
| | | 1043 | | /// <summary> |
| | | 1044 | | /// S7 - { 1,13,15, 0,14, 8, 2,11, 7, 4,12,10, 9, 3, 5, 6 } - 16 terms. |
| | | 1045 | | /// </summary> |
| | | 1046 | | /// <param name="a">A.</param> |
| | | 1047 | | /// <param name="b">The b.</param> |
| | | 1048 | | /// <param name="c">The c.</param> |
| | | 1049 | | /// <param name="d">The d.</param> |
| | | 1050 | | private void Sb7(int a, int b, int c, int d) |
| | 0 | 1051 | | { |
| | 0 | 1052 | | var t1 = b ^ c; |
| | 0 | 1053 | | var t2 = c & t1; |
| | 0 | 1054 | | var t3 = d ^ t2; |
| | 0 | 1055 | | var t4 = a ^ t3; |
| | 0 | 1056 | | var t5 = d | t1; |
| | 0 | 1057 | | var t6 = t4 & t5; |
| | 0 | 1058 | | _x1 = b ^ t6; |
| | 0 | 1059 | | var t8 = t3 | _x1; |
| | 0 | 1060 | | var t9 = a & t4; |
| | 0 | 1061 | | _x3 = t1 ^ t9; |
| | 0 | 1062 | | var t11 = t4 ^ t8; |
| | 0 | 1063 | | var t12 = _x3 & t11; |
| | 0 | 1064 | | _x2 = t3 ^ t12; |
| | 0 | 1065 | | _x0 = (~t11) ^ (_x3 & _x2); |
| | 0 | 1066 | | } |
| | | 1067 | | |
| | | 1068 | | /// <summary> |
| | | 1069 | | /// InvS7 - { 3, 0, 6,13, 9,14,15, 8, 5,12,11, 7,10, 1, 4, 2 } - 17 terms. |
| | | 1070 | | /// </summary> |
| | | 1071 | | /// <param name="a">A.</param> |
| | | 1072 | | /// <param name="b">The b.</param> |
| | | 1073 | | /// <param name="c">The c.</param> |
| | | 1074 | | /// <param name="d">The d.</param> |
| | | 1075 | | private void Ib7(int a, int b, int c, int d) |
| | 0 | 1076 | | { |
| | 0 | 1077 | | var t3 = c | (a & b); |
| | 0 | 1078 | | var t4 = d & (a | b); |
| | 0 | 1079 | | _x3 = t3 ^ t4; |
| | 0 | 1080 | | var t6 = ~d; |
| | 0 | 1081 | | var t7 = b ^ t4; |
| | 0 | 1082 | | var t9 = t7 | (_x3 ^ t6); |
| | 0 | 1083 | | _x1 = a ^ t9; |
| | 0 | 1084 | | _x0 = (c ^ t7) ^ (d | _x1); |
| | 0 | 1085 | | _x2 = (t3 ^ _x1) ^ (_x0 ^ (a & _x3)); |
| | 0 | 1086 | | } |
| | | 1087 | | |
| | | 1088 | | /// <summary> |
| | | 1089 | | /// Apply the linear transformation to the register set. |
| | | 1090 | | /// </summary> |
| | | 1091 | | private void LT() |
| | 0 | 1092 | | { |
| | 0 | 1093 | | var x0 = RotateLeft(_x0, 13); |
| | 0 | 1094 | | var x2 = RotateLeft(_x2, 3); |
| | 0 | 1095 | | var x1 = _x1 ^ x0 ^ x2; |
| | 0 | 1096 | | var x3 = _x3 ^ x2 ^ x0 << 3; |
| | | 1097 | | |
| | 0 | 1098 | | _x1 = RotateLeft(x1, 1); |
| | 0 | 1099 | | _x3 = RotateLeft(x3, 7); |
| | 0 | 1100 | | _x0 = RotateLeft(x0 ^ _x1 ^ _x3, 5); |
| | 0 | 1101 | | _x2 = RotateLeft(x2 ^ _x3 ^ (_x1 << 7), 22); |
| | 0 | 1102 | | } |
| | | 1103 | | |
| | | 1104 | | /// <summary> |
| | | 1105 | | /// Apply the inverse of the linear transformation to the register set. |
| | | 1106 | | /// </summary> |
| | | 1107 | | private void InverseLT() |
| | 0 | 1108 | | { |
| | 0 | 1109 | | var x2 = RotateRight(_x2, 22) ^ _x3 ^ (_x1 << 7); |
| | 0 | 1110 | | var x0 = RotateRight(_x0, 5) ^ _x1 ^ _x3; |
| | 0 | 1111 | | var x3 = RotateRight(_x3, 7); |
| | 0 | 1112 | | var x1 = RotateRight(_x1, 1); |
| | 0 | 1113 | | _x3 = x3 ^ x2 ^ x0 << 3; |
| | 0 | 1114 | | _x1 = x1 ^ x0 ^ x2; |
| | 0 | 1115 | | _x2 = RotateRight(x2, 3); |
| | 0 | 1116 | | _x0 = RotateRight(x0, 13); |
| | 0 | 1117 | | } |
| | | 1118 | | } |
| | | 1119 | | } |