private static bool checkInitDuplex(SHA3 sha3) { byte[] keyAndMessage = new byte[72 * 8 - 1]; var rnd = new Random(98732796); for (int i = 0; i < 64; i++) { keyAndMessage[i] = (byte)rnd.Next(0, 255); } keyAndMessage[64] = 0x01; keyAndMessage[71] = 0x80; for (int i = 72; i < keyAndMessage.Length; i++) { keyAndMessage[i] = (byte)rnd.Next(0, 255); } var kam1 = new byte[64]; var kam2 = new byte[keyAndMessage.Length - 72]; BytesBuilder.CopyTo(keyAndMessage, kam1); BytesBuilder.CopyTo(keyAndMessage, kam2, 0, -1, 72); byte[] dupi1 = sha3.getDuplex(kam1); byte[] dupi2 = sha3.getDuplex(kam2, true); byte[] dupe = sha3.getDuplex(keyAndMessage); bool errorFlag = false; int k; for (k = 0; k < dupi1.Length; k++) { if (dupi1[k] != dupe[k]) { errorFlag = true; } } for (int j = 0; j < dupi2.Length /* && k < dupe.Length*/; k++, j++) { if (dupi2[j] != dupe[k]) { errorFlag = true; } } if (errorFlag) { Console.WriteLine("Duplex and init duplex unequal, duplex is incorrect"); } else { Console.WriteLine("Well. Duplex and init duplex equal, duplex is correct."); } return(!errorFlag); }
public override void ProcessCryptoVariant(long variant) { SHA3 gs1 = new SHA3(0); SHA3 gs2 = new SHA3(0); //SHA3 gs3 = new SHA3(0); var key = prepareKey(variant); var s = prepareOIV(variant); gs1.prepareGamma(key, s); var gamma = gs1.getGamma(71); gs2.prepareGamma(gamma, s); gamma = gs1.getDuplex(gamma, true); gamma = gs2.getDuplex(gamma, true); gamma = gs1.getDuplex(gamma, true); gamma = gs2.getDuplex(gamma, true); gamma = gs1.getDuplex(gamma, true); gamma = gs2.getDuplex(gamma, true); if (gamma[0] == 1) { if ((variant & 1) > 0) { c1f++; } else { c1t++; } } calcResult(gamma, variant, key); }
private static bool checkDuplexByHash(SHA3 sha3) { byte[] keyAndMessage = new byte[72 * 8 - 1]; var rnd = new Random(98732796); for (int i = 0; i < 64; i++) { keyAndMessage[i] = (byte)rnd.Next(0, 255); } keyAndMessage[64] = 0x01; keyAndMessage[71] = 0x80; for (int i = 72; i < keyAndMessage.Length; i++) { keyAndMessage[i] = (byte)rnd.Next(0, 255); } byte[] hash = sha3.getHash512(keyAndMessage); byte[] gamma = sha3.getDuplex(keyAndMessage); bool errorFlag = false; for (int i = 0; i < hash.Length; i++) { if (hash[i] != gamma[gamma.Length - 72 + i]) { errorFlag = true; } } if (errorFlag) { Console.WriteLine("Duplex and hash unequal, duplex is incorrect"); } else { Console.WriteLine("Well. Duplex and hash equal, duplex is correct."); } return(!errorFlag); }
public override void ProcessCryptoVariant(long variant) { SHA3 gs1 = new SHA3(0); SHA3 gs2 = new SHA3(0); var key = prepareKey(variant); var s = prepareOIV(variant); var bt = Convert.FromBase64String("GL4C/c+hpZrVQcIg771ujdjHN/2Jqoj+UpS6cZ5fbG7zU1qwXMMg5P3YgnLY9byB1laMMu557wK1J7EewmkKJ0wPjA8u3uVO7oOhmtVSc6rnGMCaTrRHtLVLv9a9dBFnNxGjxbCLOcaGkZZYeBT1KUraXh/5reXVQmedmkijKvEwJ4d3CPSMUoPGca+lv7I="); var g1 = gs1.getDuplex(bt); var g2 = gs2.getDuplex(key); g1 = gs1.getDuplex(g2, true); g2 = gs2.getDuplex(g1, true); g1 = gs1.getDuplex(g2, true); g2 = gs2.getDuplex(g1, true); g1 = gs1.getDuplex(g2, true); g2 = gs2.getDuplex(g1, true); g1 = gs1.getDuplex(g2, true); g2 = gs2.getDuplex(g1, true); g1 = gs1.getDuplex(g2, true); g2 = gs2.getDuplex(g1, true); var gamma = gs1.getDuplexMod(g1, g2); if (gamma[0] == 1) { if ((variant & 1) > 0) { c1f++; } else { c1t++; } } calcResult(gamma, variant, key); }
protected unsafe void processBlocksForGamma2(int SL, byte[] block, byte[] outblock, byte[] k, byte *[] b, bool permutation, int GOST5) { if (b.Length != 6 || k.Length != 5) { throw new ArgumentException("processBlocksForGamma2 b.Length != 6 || k.Length != 5"); } long works = 0; var sync = new Object(); SHA3 sha = null; if (GOST5 >= 13) { shaPP.randomize(block); sha = new SHA3(block.Length); sha.getDuplex(block, false, -1, false); } if (GOST5 < 13) { if ((SL & 7) > 0) { throw new ArgumentException("processBlocksForGamma2 SL & 7 > 0"); } } else { if ((SL & 15) > 0) { throw new ArgumentException("processBlocksForGamma2 SL & 15 > 0: " + SL); } } byte[] Na = (GOST5 >= 13) ? shaPP.nextBytes64() : null; //bytesToint(block, i); int nk2 = 0; int bStep = (GOST5 >= 13) ? 16 : 8; for (int i = 0, j = 0; i < SL; i += bStep, j++) { if (i + bStep > SL) { throw new ArgumentOutOfRangeException("i + bStep >= SL"); } if (GOST5 < 13) { int N = bytesToint(block, i); for (int i1 = 0; i1 < b.Length; i1++) { for (int i2 = i1 + 1; i2 < b.Length; i2++) { if ((N & 1) > 0) { byte *t = b[i1]; b[i1] = b[i2]; b[i2] = t; } N >>= 1; } } N = bytesToint(block, i + 4); for (int i1 = 0; i1 < k.Length; i1++) { for (int i2 = i1 + 1; i2 < k.Length; i2++) { if ((N & 1) > 0) { byte t = k[i1]; k[i1] = k[i2]; k[i2] = t; } N >>= 1; } } } else { if (i > SL - 15) { break; } byte _L = (byte)(j & 7); if (nk2 > Na.Length - 8) { Na = shaPP.nextBytes64(); nk2 = 0; } for (int nk2i = nk2; nk2i < nk2 + 8; nk2i++) { pbfg2Perm(k, ka, (Na[nk2i] & 7) ^ _L); Na[nk2i] >>= 3; pbfg2PPerm(b, ba, (Na[nk2i] & 7) ^ _L); } nk2 += 8; } var _i = i; var _j = j & 1; var k0 = k[0]; var k1 = k[1]; var k2 = k[2]; var k3 = k[3]; var k4 = GOST5 >= 13 ? k[4] : 0; var b0 = b[0]; var b1 = b[1]; var b2 = b[2]; var b3 = b[3]; var b4 = b[4]; var sh = GOST5 >= 13 ? sha.Clone() : null; Interlocked.Increment(ref works); ThreadPool.QueueUserWorkItem ( delegate { try { if (GOST5 >= 13) { var tmp0 = new byte[16]; var tmp1 = new byte[16]; var tmp2 = new byte[16]; BytesBuilder.CopyTo(block, tmp0, 0, -1, _i); BytesBuilder.CopyTo(tmp0, tmp1); BytesBuilder.CopyTo(tmp0, tmp2); BytesBuilder.ToNull(tmp0); tmp1 = sh.getDuplex(tmp2, true, 16, true, 16); ProcessBlock(tmp1, 0, tmp2, 0, workingKey[k0], b4, true); ProcessBlock(tmp1, 8, tmp2, 8, workingKey[k0], b0, true); BytesBuilder.ToNull(tmp1); //sh.getDuplex(bytes1, true, 0, false); tmp1 = sh.getDuplex(tmp2, true, 16, true, 16); ProcessBlock(tmp1, 0, tmp2, 0, workingKey[k1], b0, true); ProcessBlock(tmp1, 8, tmp2, 8, workingKey[k1], b1, true); BytesBuilder.ToNull(tmp1); tmp1 = sh.getDuplex(tmp2, true, 16, true, 16); ProcessBlock(tmp1, 0, tmp2, 0, workingKey[k2], b1, true); ProcessBlock(tmp1, 8, tmp2, 8, workingKey[k2], b2, true); BytesBuilder.ToNull(tmp1); tmp1 = sh.getDuplex(tmp2, true, 16, true, 16); ProcessBlock(tmp1, 0, tmp2, 0, workingKey[k3], b2, true); ProcessBlock(tmp1, 8, tmp2, 8, workingKey[k3], b3, true); BytesBuilder.ToNull(tmp1); tmp1 = sh.getDuplex(tmp2, true, 16, true, 16); ProcessBlock(tmp1, 0, tmp2, 0, workingKey[k4], b3, true); ProcessBlock(tmp1, 8, tmp2, 8, workingKey[k4], b4, true); BytesBuilder.ToNull(tmp1); BytesBuilder.CopyTo(tmp2, outblock, _i); BytesBuilder.ToNull(tmp2); } else if (_j > 0) { ProcessBlock(block, _i, outblock, _i, workingKey[k3], b0, true); ProcessBlock(outblock, _i, block, _i, workingKey[k1], b1, false); ProcessBlock(block, _i, outblock, _i, workingKey[k0], b2, true); ProcessBlock(outblock, _i, block, _i, workingKey[k2], b3, false); ProcessBlock(block, _i, outblock, _i, workingKey[k3], b4, true); } else { ProcessBlock(block, _i, outblock, _i, workingKey[k0], b4, true); ProcessBlock(outblock, _i, block, _i, workingKey[k3], b3, false); ProcessBlock(block, _i, outblock, _i, workingKey[k2], b2, true); ProcessBlock(outblock, _i, block, _i, workingKey[k1], b1, false); ProcessBlock(block, _i, outblock, _i, workingKey[k0], b0, true); } } finally { lock (sync) { Interlocked.Decrement(ref works); Monitor.Pulse(sync); } } } ); } lock (sync) while (Interlocked.Read(ref works) > 0) { Monitor.Wait(sync); } if (permutation) { BlocksPermutation(SL, outblock, block, k, b, GOST5); } }