123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Runtime.CompilerServices;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes.Gcm;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- #if BESTHTTP_WITH_BURST
- using Unity.Burst;
- using Unity.Collections.LowLevel.Unsafe;
- #endif
- namespace Best.HTTP.Shared.TLS.Crypto.Impl
- {
- [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
- #if BESTHTTP_WITH_BURST
- [BurstCompile]
- #endif
- public sealed class BurstTables8kGcmMultiplier //: IGcmMultiplier
- {
- private byte[] H;
- private GcmUtilities.FieldElement[][] T;
- public void Init(byte[] H)
- {
- if (T == null)
- {
- T = new GcmUtilities.FieldElement[2][];
- }
- else if (Arrays.AreEqual(this.H, H))
- {
- return;
- }
- if (this.H == null)
- this.H = Arrays.Clone(H);
- else
- {
- if (this.H.Length != H.Length)
- Array.Resize(ref this.H, H.Length);
- Array.Copy(H, this.H, H.Length);
- }
- for (int i = 0; i < 2; ++i)
- {
- if (T[i] == null)
- T[i] = new GcmUtilities.FieldElement[256];
- GcmUtilities.FieldElement[] t = T[i];
- // t[0] = 0
- if (i == 0)
- {
- // t[1] = H.p^7
- GcmUtilities.AsFieldElement(this.H, out t[1]);
- GcmUtilities.MultiplyP7(ref t[1]);
- }
- else
- {
- // t[1] = T[i-1][1].p^8
- GcmUtilities.MultiplyP8(ref T[i - 1][1], out t[1]);
- }
- for (int n = 1; n < 128; ++n)
- {
- // t[2.n] = t[n].p^-1
- GcmUtilities.DivideP(ref t[n], out t[n << 1]);
- // t[2.n + 1] = t[2.n] + t[1]
- GcmUtilities.Xor(ref t[n << 1], ref t[1], out t[(n << 1) + 1]);
- }
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public unsafe void MultiplyH(byte[] x)
- {
- fixed (byte* px = x)
- fixed (GcmUtilities.FieldElement* pT0 = this.T[0])
- fixed (GcmUtilities.FieldElement* pT1 = this.T[1])
- MultiplyHImpl(px, pT0, pT1);
- }
- #if BESTHTTP_WITH_BURST
- [BurstCompile]
- #endif
- private static unsafe void MultiplyHImpl(
- #if BESTHTTP_WITH_BURST
- [NoAlias]
- #endif
- byte* px,
- #if BESTHTTP_WITH_BURST
- [NoAlias]
- #endif
- GcmUtilities.FieldElement* pT0,
- #if BESTHTTP_WITH_BURST
- [NoAlias]
- #endif
- GcmUtilities.FieldElement* pT1)
- {
- int vPos = px[15];
- int uPos = px[14];
- ulong z1 = pT0[uPos].n1 ^ pT1[vPos].n1;
- ulong z0 = pT0[uPos].n0 ^ pT1[vPos].n0;
- for (int i = 12; i >= 0; i -= 2)
- {
- vPos = px[i + 1];
- uPos = px[i];
- ulong c = z1 << 48;
- z1 = pT0[uPos].n1 ^ pT1[vPos].n1 ^ ((z1 >> 16) | (z0 << 48));
- z0 = pT0[uPos].n0 ^ pT1[vPos].n0 ^ (z0 >> 16) ^ c ^ (c >> 1) ^ (c >> 2) ^ (c >> 7);
- }
- //GcmUtilities.AsBytes(z0, z1, x);
- //UInt32_To_BE((uint)(n >> 32), bs, off);
- uint n = (uint)(z0 >> 32);
- px[0] = (byte)(n >> 24);
- px[1] = (byte)(n >> 16);
- px[2] = (byte)(n >> 8);
- px[3] = (byte)(n);
- //UInt32_To_BE((uint)(n), bs, off + 4);
- n = (uint)(z0);
- px[4] = (byte)(n >> 24);
- px[5] = (byte)(n >> 16);
- px[6] = (byte)(n >> 8);
- px[7] = (byte)(n);
-
- n = (uint)(z1 >> 32);
- px[8] = (byte)(n >> 24);
- px[9] = (byte)(n >> 16);
- px[10] = (byte)(n >> 8);
- px[11] = (byte)(n);
- //UInt32_To_BE((uint)(n), bs, off + 4);
- n = (uint)(z1);
- px[12] = (byte)(n >> 24);
- px[13] = (byte)(n >> 16);
- px[14] = (byte)(n >> 8);
- px[15] = (byte)(n);
- }
- }
- }
- #pragma warning restore
- #endif
|