private void performTest(int number, TestCase testCase) { byte[] plaintext = testCase.Plaintext; byte[] output = new byte[plaintext.Length]; XSalsa20Engine engine = new XSalsa20Engine(); engine.Init(false, new ParametersWithIV(new KeyParameter(testCase.Key), testCase.Iv)); engine.ProcessBytes(testCase.Plaintext, 0, testCase.Plaintext.Length, output, 0); if (!Arrays.AreEqual(testCase.Ciphertext, output)) { Fail("mismatch on " + number, Hex.ToHexString(testCase.Ciphertext), Hex.ToHexString(output)); } }
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/ /// <summary> /// Buils the StreamCipher /// </summary> /// <param name="algorithm">SymmetrcStreamAlgorithm enum, algorithm name</param> /// <returns>IStreamCipher with the algorithm Stream Engine</returns> private IStreamCipher getCipherEngine(SymmetricStreamAlgorithm algorithm) { IStreamCipher engine = null; switch (algorithm) { case SymmetricStreamAlgorithm.RC4: engine = new RC4Engine(); break; case SymmetricStreamAlgorithm.HC128: engine = new HC128Engine(); break; case SymmetricStreamAlgorithm.HC256: engine = new HC256Engine(); break; case SymmetricStreamAlgorithm.SALSA20: engine = new Salsa20Engine(); break; case SymmetricStreamAlgorithm.CHACHA20: engine = new ChaChaEngine(); break; case SymmetricStreamAlgorithm.XSALSA20: engine = new XSalsa20Engine(); break; case SymmetricStreamAlgorithm.ISAAC: engine = new IsaacEngine(); break; case SymmetricStreamAlgorithm.VMPC: engine = new VmpcEngine(); break; default: this.GetError().setError("SS005", "Cipher " + algorithm + " not recognised."); break; } return(engine); }
private byte[] EncryptXSalsa20Poly1305(byte[] bytes, byte[] key, byte[] nonce) { var salsa = new XSalsa20Engine(); var poly = new Poly1305(); salsa.Init(true, new ParametersWithIV(new KeyParameter(key), nonce)); byte[] subKey = new byte[key.Length]; salsa.ProcessBytes(subKey, 0, key.Length, subKey, 0); byte[] output = new byte[bytes.Length + poly.GetMacSize()]; salsa.ProcessBytes(bytes, 0, bytes.Length, output, poly.GetMacSize()); poly.Init(new KeyParameter(subKey)); poly.BlockUpdate(output, poly.GetMacSize(), bytes.Length); poly.DoFinal(output, 0); return(output); }
private static unsafe void BlockMix (uint *B, // 16*2*r int Boffset, uint *Bp, // 16*2*r uint Bpoffset, uint *x, // 16 uint *y, // 16*2*r -- unnecessary but it allows us to alias B and Bp uint *scratch1, // 16 /*uint* scratch2, // 16 */ int r) { int k = Boffset, m = 0, n = 16 * r; CopyExtensions.CopyMemory((byte *)(B + ((2 * r - 1) * 16)), (byte *)x, 16 * sizeof(uint)); for (int i = 0; i < r; i++) { for (int j = 0; j < 16; j++) { scratch1[j] = x[j] ^ B[j + k]; } XSalsa20Engine.HSalsaUnsafe(8, scratch1, x); CopyExtensions.CopyMemory((byte *)x, (byte *)(y + m), 16 * sizeof(uint)); k += 16; for (int j = 0; j < 16; j++) { scratch1[j] = x[j] ^ B[j + k]; } XSalsa20Engine.HSalsaUnsafe(8, scratch1, x); CopyExtensions.CopyMemory((byte *)x, (byte *)(y + m + n), 16 * sizeof(uint)); k += 16; m += 16; } CopyExtensions.CopyMemory((byte *)y, (byte *)(Bp + Bpoffset), n * 2 * sizeof(uint)); }
private static void BlockMix (uint[] B, // 16*2*r int Boffset, uint[] Bp, // 16*2*r int Bpoffset, uint[] x, // 16 uint[] y, // 16*2*r -- unnecessary but it allows us to alias B and Bp uint[] scratch1, // 16 /*uint[] scratch2, // 16 */ int r) { int k = Boffset, m = 0, n = 16 * r; Array.Copy(B, (2 * r - 1) * 16, x, 0, 16); for (int i = 0; i < r; i++) { for (int j = 0; j < scratch1.Length; j++) { scratch1[j] = x[j] ^ B[j + k]; } XSalsa20Engine.HSalsa(8, scratch1, 0, x, 0); Array.Copy(x, 0, y, m, 16); k += 16; for (int j = 0; j < scratch1.Length; j++) { scratch1[j] = x[j] ^ B[j + k]; } XSalsa20Engine.HSalsa(8, scratch1, 0, x, 0); Array.Copy(x, 0, y, m + n, 16); k += 16; m += 16; } y.DeepCopy_NoChecks(0, Bp, Bpoffset, y.Length); }
public BcXSalsa20Crypto(byte[] key, byte[] iv) : base(key, iv) { _engine = new XSalsa20Engine(); _engine.Init(default, new ParametersWithIV(new KeyParameter(key), iv));
internal static void KeyExchangeOutputHashNaCl(byte[] sharedKey, int offset) { XSalsa20Engine.HSalsa20(sharedKey, 0, sharedKey, HSalsaNonceZeroes); }