public void TestExp() { { byte[] buf1 = new byte[16]; buf1[0] = 0x80; byte[] buf2 = new byte[16]; for (int pow = 0; pow != 100; ++pow) { exp.ExponentiateX(pow, buf2); Assert.IsTrue(Arrays.AreEqual(buf1, buf2)); mul.MultiplyH(buf1); } } long[] testPow = new long[] { 10, 1, 8, 17, 24, 13, 2, 13, 2, 3 }; byte[][] testData = new byte[][] { Hex.Decode("9185848a877bd87ba071e281f476e8e7"), Hex.Decode("697ce3052137d80745d524474fb6b290"), Hex.Decode("2696fc47198bb23b11296e4f88720a17"), Hex.Decode("01f2f0ead011a4ae0cf3572f1b76dd8e"), Hex.Decode("a53060694a044e4b7fa1e661c5a7bb6b"), Hex.Decode("39c0392e8b6b0e04a7565c85394c2c4c"), Hex.Decode("519c362d502e07f2d8b7597a359a5214"), Hex.Decode("5a527a393675705e19b2117f67695af4"), Hex.Decode("27fc0901d1d332a53ba4d4386c2109d2"), Hex.Decode("93ca9b57174aabedf8220e83366d7df6"), }; for (int i = 0; i != 10; ++i) { long pow = testPow[i]; byte[] data = Arrays.Clone(testData[i]); byte[] expected = Arrays.Clone(data); for (int j = 0; j < pow; ++j) { mul.MultiplyH(expected); } byte[] H_a = new byte[16]; exp.ExponentiateX(pow, H_a); byte[] actual = multiply(data, H_a); Assert.IsTrue(Arrays.AreEqual(expected, actual)); } }
public int DoFinal(byte[] output, int outOff) { if (totalLength == 0) { InitCipher(); } int num = bufOff; if (forEncryption) { Check.OutputLength(output, outOff, num + macSize, "Output buffer too short"); } else { if (num < macSize) { throw new InvalidCipherTextException("data too short"); } num -= macSize; Check.OutputLength(output, outOff, num, "Output buffer too short"); } if (num > 0) { gCTRPartial(bufBlock, 0, num, output, outOff); } atLength += (uint)atBlockPos; if (atLength > atLengthPre) { if (atBlockPos > 0) { gHASHPartial(S_at, atBlock, 0, atBlockPos); } if (atLengthPre != 0) { GcmUtilities.Xor(S_at, S_atPre); } long pow = (long)(totalLength * 8 + 127 >> 7); byte[] array = new byte[16]; if (exp == null) { exp = new Tables1kGcmExponentiator(); exp.Init(H); } exp.ExponentiateX(pow, array); GcmUtilities.Multiply(S_at, array); GcmUtilities.Xor(S, S_at); } byte[] array2 = new byte[16]; Pack.UInt64_To_BE(atLength * 8, array2, 0); Pack.UInt64_To_BE(totalLength * 8, array2, 8); gHASHBlock(S, array2); byte[] array3 = new byte[16]; cipher.ProcessBlock(J0, 0, array3, 0); GcmUtilities.Xor(array3, S); int num2 = num; macBlock = new byte[macSize]; global::System.Array.Copy((global::System.Array)array3, 0, (global::System.Array)macBlock, 0, macSize); if (forEncryption) { global::System.Array.Copy((global::System.Array)macBlock, 0, (global::System.Array)output, outOff + bufOff, macSize); num2 += macSize; } else { byte[] array4 = new byte[macSize]; global::System.Array.Copy((global::System.Array)bufBlock, num, (global::System.Array)array4, 0, macSize); if (!Arrays.ConstantTimeAreEqual(macBlock, array4)) { throw new InvalidCipherTextException("mac check in GCM failed"); } } Reset(clearMac: false); return(num2); }
public int DoFinal(byte[] output, int outOff) { if (totalLength == 0) { InitCipher(); } int extra = bufOff; if (forEncryption) { Check.OutputLength(output, outOff, extra + macSize, "Output buffer too short"); } else { if (extra < macSize) { throw new InvalidCipherTextException("data too short"); } extra -= macSize; Check.OutputLength(output, outOff, extra, "Output buffer too short"); } if (extra > 0) { gCTRPartial(bufBlock, 0, extra, output, outOff); } atLength += (uint)atBlockPos; if (atLength > atLengthPre) { /* * Some AAD was sent after the cipher started. We determine the difference b/w the hash value * we actually used when the cipher started (S_atPre) and the final hash value calculated (S_at). * Then we carry this difference forward by multiplying by H^c, where c is the number of (full or * partial) cipher-text blocks produced, and adjust the current hash. */ // Finish hash for partial AAD block if (atBlockPos > 0) { gHASHPartial(S_at, atBlock, 0, atBlockPos); } // Find the difference between the AAD hashes if (atLengthPre > 0) { GcmUtilities.Xor(S_at, S_atPre); } // Number of cipher-text blocks produced long c = (long)(((totalLength * 8) + 127) >> 7); // Calculate the adjustment factor byte[] H_c = new byte[16]; if (exp == null) { exp = new Tables1kGcmExponentiator(); exp.Init(H); } exp.ExponentiateX(c, H_c); // Carry the difference forward GcmUtilities.Multiply(S_at, H_c); // Adjust the current hash GcmUtilities.Xor(S, S_at); } // Final gHASH byte[] X = new byte[BlockSize]; Pack.UInt64_To_BE(atLength * 8UL, X, 0); Pack.UInt64_To_BE(totalLength * 8UL, X, 8); gHASHBlock(S, X); // T = MSBt(GCTRk(J0,S)) byte[] tag = new byte[BlockSize]; cipher.ProcessBlock(J0, 0, tag, 0); GcmUtilities.Xor(tag, S); int resultLen = extra; // We place into macBlock our calculated value for T this.macBlock = new byte[macSize]; Array.Copy(tag, 0, macBlock, 0, macSize); if (forEncryption) { // Append T to the message Array.Copy(macBlock, 0, output, outOff + bufOff, macSize); resultLen += macSize; } else { // Retrieve the T value from the message and compare to calculated one byte[] msgMac = new byte[macSize]; Array.Copy(bufBlock, extra, msgMac, 0, macSize); if (!Arrays.ConstantTimeAreEqual(this.macBlock, msgMac)) { throw new InvalidCipherTextException("mac check in GCM failed"); } } Reset(false); return(resultLen); }