| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using System.Runtime.CompilerServices;
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- using System.Buffers.Binary;
- using System.Runtime.InteropServices;
- #endif
- #if NETCOREAPP3_0_OR_GREATER
- using System.Runtime.InteropServices;
- using System.Runtime.Intrinsics;
- using System.Runtime.Intrinsics.X86;
- #endif
- using BestHTTP.PlatformSupport.Memory;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes.Gcm;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace BestHTTP.Connections.TLS.Crypto.Impl
- {
- /// <summary>
- /// Implements the Galois/Counter mode (GCM) detailed in NIST Special Publication 800-38D.
- /// </summary>
- [BestHTTP.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
- #if BESTHTTP_WITH_BURST
- [Unity.Burst.BurstCompile]
- #endif
- public sealed class FastGcmBlockCipher
- : IAeadBlockCipher
- {
- private static IGcmMultiplier CreateGcmMultiplier()
- {
- #if NETCOREAPP3_0_OR_GREATER
- // TODO Prefer more tightly coupled test
- if (Pclmulqdq.IsSupported)
- {
- return new BasicGcmMultiplier();
- }
- #endif
- return new Tables8kGcmMultiplier();
- }
- public const int BlockSize = 16;
- byte[] ctrBlock = new byte[BlockSize];
- private readonly IBlockCipher cipher;
- private readonly BurstTables8kGcmMultiplier multiplier;
- private IGcmExponentiator exp;
- // These fields are set by Init and not modified by processing
- private bool forEncryption;
- private bool initialised;
- private int macSize;
- private byte[] lastKey;
- private byte[] nonce;
- private byte[] initialAssociatedText;
- private byte[] H;
- private byte[] J0;
- // These fields are modified during processing
- private byte[] bufBlock;
- private byte[] macBlock;
- private byte[] S, S_at, S_atPre;
- private byte[] counter;
- private uint counter32;
- private uint blocksRemaining;
- private int bufOff;
- private ulong totalLength;
- private byte[] atBlock;
- private int atBlockPos;
- private ulong atLength;
- private ulong atLengthPre;
- public FastGcmBlockCipher(
- IBlockCipher c)
- : this(c, null)
- {
- }
- public FastGcmBlockCipher(
- IBlockCipher c,
- IGcmMultiplier m)
- {
- if (c.GetBlockSize() != BlockSize)
- throw new ArgumentException("cipher required with a block size of " + BlockSize + ".");
- //if (m == null)
- //{
- // m = CreateGcmMultiplier();
- //}
- this.cipher = c;
- this.multiplier = new BurstTables8kGcmMultiplier();
- }
- public string AlgorithmName => cipher.AlgorithmName + "/GCM";
- public IBlockCipher UnderlyingCipher => cipher;
- public int GetBlockSize()
- {
- return BlockSize;
- }
- /// <remarks>
- /// MAC sizes from 32 bits to 128 bits (must be a multiple of 8) are supported. The default is 128 bits.
- /// Sizes less than 96 are not recommended, but are supported for specialized applications.
- /// </remarks>
- public void Init(bool forEncryption, ICipherParameters parameters)
- {
- this.forEncryption = forEncryption;
- //this.macBlock = null;
- if (this.macBlock != null)
- Array.Clear(this.macBlock, 0, this.macBlock.Length);
- this.initialised = true;
- KeyParameter keyParam;
- byte[] newNonce = null;
- if (parameters is AeadParameters)
- {
- AeadParameters param = (AeadParameters)parameters;
- newNonce = param.GetNonce();
- initialAssociatedText = param.GetAssociatedText();
- int macSizeBits = param.MacSize;
- if (macSizeBits < 32 || macSizeBits > 128 || macSizeBits % 8 != 0)
- {
- throw new ArgumentException("Invalid value for MAC size: " + macSizeBits);
- }
- macSize = macSizeBits / 8;
- keyParam = param.Key;
- }
- else if (parameters is ParametersWithIV)
- {
- ParametersWithIV param = (ParametersWithIV)parameters;
- newNonce = param.GetIV();
- initialAssociatedText = null;
- macSize = 16;
- keyParam = (KeyParameter)param.Parameters;
- }
- else
- {
- throw new ArgumentException("invalid parameters passed to GCM");
- }
- int bufLength = forEncryption ? BlockSize : (BlockSize + macSize);
- this.bufBlock = new byte[bufLength];
- if (newNonce == null || newNonce.Length < 1)
- {
- throw new ArgumentException("IV must be at least 1 byte");
- }
- if (forEncryption)
- {
- if (nonce != null && Arrays.AreEqual(nonce, newNonce))
- {
- if (keyParam == null)
- {
- throw new ArgumentException("cannot reuse nonce for GCM encryption");
- }
- if (lastKey != null && Arrays.AreEqual(lastKey, keyParam.GetKey()))
- {
- throw new ArgumentException("cannot reuse nonce for GCM encryption");
- }
- }
- }
- nonce = newNonce;
- if (keyParam != null)
- {
- lastKey = keyParam.GetKey();
- }
- // TODO Restrict macSize to 16 if nonce length not 12?
- // Cipher always used in forward mode
- // if keyParam is null we're reusing the last key.
- if (keyParam != null)
- {
- cipher.Init(true, keyParam);
- this.H = new byte[BlockSize];
- cipher.ProcessBlock(H, 0, H, 0);
- // if keyParam is null we're reusing the last key and the multiplier doesn't need re-init
- multiplier.Init(H);
- exp = null;
- }
- else if (this.H == null)
- {
- throw new ArgumentException("Key must be specified in initial init");
- }
- this.J0 = new byte[BlockSize];
- if (nonce.Length == 12)
- {
- Array.Copy(nonce, 0, J0, 0, nonce.Length);
- this.J0[BlockSize - 1] = 0x01;
- }
- else
- {
- gHASH(J0, nonce, nonce.Length);
- byte[] X = new byte[BlockSize];
- Pack.UInt64_To_BE((ulong)nonce.Length * 8UL, X, 8);
- gHASHBlock(J0, X);
- }
- this.S = new byte[BlockSize];
- this.S_at = new byte[BlockSize];
- this.S_atPre = new byte[BlockSize];
- this.atBlock = new byte[BlockSize];
- this.atBlockPos = 0;
- this.atLength = 0;
- this.atLengthPre = 0;
- this.counter = Arrays.Clone(J0);
- this.counter32 = Pack.BE_To_UInt32(counter, 12);
- this.blocksRemaining = uint.MaxValue - 1; // page 8, len(P) <= 2^39 - 256, 1 block used by tag
- this.bufOff = 0;
- this.totalLength = 0;
- if (initialAssociatedText != null)
- {
- ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length);
- }
- }
- public byte[] GetMac()
- {
- return macBlock == null
- ? new byte[macSize]
- : Arrays.Clone(macBlock);
- }
- public int GetOutputSize(int len)
- {
- int totalData = len + bufOff;
- if (forEncryption)
- {
- return totalData + macSize;
- }
- return totalData < macSize ? 0 : totalData - macSize;
- }
- public int GetUpdateOutputSize(int len)
- {
- int totalData = len + bufOff;
- if (!forEncryption)
- {
- if (totalData < macSize)
- {
- return 0;
- }
- totalData -= macSize;
- }
- return totalData - totalData % BlockSize;
- }
- public void ProcessAadByte(byte input)
- {
- CheckStatus();
- atBlock[atBlockPos] = input;
- if (++atBlockPos == BlockSize)
- {
- // Hash each block as it fills
- gHASHBlock(S_at, atBlock);
- atBlockPos = 0;
- atLength += BlockSize;
- }
- }
- public void ProcessAadBytes(byte[] inBytes, int inOff, int len)
- {
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- ProcessAadBytes(inBytes.AsSpan(inOff, len));
- #else
- CheckStatus();
- if (atBlockPos > 0)
- {
- int available = BlockSize - atBlockPos;
- if (len < available)
- {
- Array.Copy(inBytes, inOff, atBlock, atBlockPos, len);
- atBlockPos += len;
- return;
- }
- Array.Copy(inBytes, inOff, atBlock, atBlockPos, available);
- gHASHBlock(S_at, atBlock);
- atLength += BlockSize;
- inOff += available;
- len -= available;
- //atBlockPos = 0;
- }
- int inLimit = inOff + len - BlockSize;
- while (inOff <= inLimit)
- {
- gHASHBlock(S_at, inBytes, inOff);
- atLength += BlockSize;
- inOff += BlockSize;
- }
- atBlockPos = BlockSize + inLimit - inOff;
- Array.Copy(inBytes, inOff, atBlock, 0, atBlockPos);
- #endif
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- public void ProcessAadBytes(ReadOnlySpan<byte> input)
- {
- CheckStatus();
- if (atBlockPos > 0)
- {
- int available = BlockSize - atBlockPos;
- if (input.Length < available)
- {
- input.CopyTo(atBlock.AsSpan(atBlockPos));
- atBlockPos += input.Length;
- return;
- }
- input[..available].CopyTo(atBlock.AsSpan(atBlockPos));
- gHASHBlock(S_at, atBlock);
- atLength += BlockSize;
- input = input[available..];
- //atBlockPos = 0;
- }
- while (input.Length >= BlockSize)
- {
- gHASHBlock(S_at, input);
- atLength += BlockSize;
- input = input[BlockSize..];
- }
- input.CopyTo(atBlock);
- atBlockPos = input.Length;
- }
- #endif
- private void InitCipher()
- {
- if (atLength > 0)
- {
- Array.Copy(S_at, 0, S_atPre, 0, BlockSize);
- atLengthPre = atLength;
- }
- // Finish hash for partial AAD block
- if (atBlockPos > 0)
- {
- gHASHPartial(S_atPre, atBlock, 0, atBlockPos);
- atLengthPre += (uint)atBlockPos;
- }
- if (atLengthPre > 0)
- {
- Array.Copy(S_atPre, 0, S, 0, BlockSize);
- }
- }
- public int ProcessByte(byte input, byte[] output, int outOff)
- {
- CheckStatus();
- bufBlock[bufOff] = input;
- if (++bufOff == bufBlock.Length)
- {
- if (forEncryption)
- {
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- EncryptBlock(bufBlock, output.AsSpan(outOff));
- #else
- EncryptBlock(bufBlock, 0, output, outOff);
- #endif
- bufOff = 0;
- }
- else
- {
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- DecryptBlock(bufBlock, output.AsSpan(outOff));
- #else
- DecryptBlock(bufBlock, 0, output, outOff);
- #endif
- Array.Copy(bufBlock, BlockSize, bufBlock, 0, macSize);
- bufOff = macSize;
- }
- return BlockSize;
- }
- return 0;
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- public int ProcessByte(byte input, Span<byte> output)
- {
- CheckStatus();
- bufBlock[bufOff] = input;
- if (++bufOff == bufBlock.Length)
- {
- if (forEncryption)
- {
- EncryptBlock(bufBlock, output);
- bufOff = 0;
- }
- else
- {
- DecryptBlock(bufBlock, output);
- Array.Copy(bufBlock, BlockSize, bufBlock, 0, macSize);
- bufOff = macSize;
- }
- return BlockSize;
- }
- return 0;
- }
- #endif
- public int ProcessBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
- {
- CheckStatus();
- Check.DataLength(input, inOff, len, "input buffer too short");
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- return ProcessBytes(input.AsSpan(inOff, len), Spans.FromNullable(output, outOff));
- #else
- int resultLen = 0;
- if (forEncryption)
- {
- if (bufOff > 0)
- {
- int available = BlockSize - bufOff;
- if (len < available)
- {
- Array.Copy(input, inOff, bufBlock, bufOff, len);
- bufOff += len;
- return 0;
- }
- Array.Copy(input, inOff, bufBlock, bufOff, available);
- EncryptBlock(bufBlock, 0, output, outOff);
- inOff += available;
- len -= available;
- resultLen = BlockSize;
- //bufOff = 0;
- }
- int inLimit1 = inOff + len - BlockSize;
- int inLimit2 = inLimit1 - BlockSize;
- while (inOff <= inLimit2)
- {
- EncryptBlocks2(input, inOff, output, outOff + resultLen);
- inOff += BlockSize * 2;
- resultLen += BlockSize * 2;
- }
- if (inOff <= inLimit1)
- {
- EncryptBlock(input, inOff, output, outOff + resultLen);
- inOff += BlockSize;
- resultLen += BlockSize;
- }
- bufOff = BlockSize + inLimit1 - inOff;
- Array.Copy(input, inOff, bufBlock, 0, bufOff);
- }
- else
- {
- int available = bufBlock.Length - bufOff;
- if (len < available)
- {
- Array.Copy(input, inOff, bufBlock, bufOff, len);
- bufOff += len;
- return 0;
- }
- if (bufOff >= BlockSize)
- {
- DecryptBlock(bufBlock, 0, output, outOff);
- Array.Copy(bufBlock, BlockSize, bufBlock, 0, bufOff -= BlockSize);
- resultLen = BlockSize;
- available += BlockSize;
- if (len < available)
- {
- Array.Copy(input, inOff, bufBlock, bufOff, len);
- bufOff += len;
- return resultLen;
- }
- }
- int inLimit1 = inOff + len - bufBlock.Length;
- int inLimit2 = inLimit1 - BlockSize;
- available = BlockSize - bufOff;
- Array.Copy(input, inOff, bufBlock, bufOff, available);
- DecryptBlock(bufBlock, 0, output, outOff + resultLen);
- inOff += available;
- resultLen += BlockSize;
- //bufOff = 0;
- while (inOff <= inLimit2)
- {
- DecryptBlocks2(input, inOff, output, outOff + resultLen);
- inOff += BlockSize * 2;
- resultLen += BlockSize * 2;
- }
- if (inOff <= inLimit1)
- {
- DecryptBlock(input, inOff, output, outOff + resultLen);
- inOff += BlockSize;
- resultLen += BlockSize;
- }
- bufOff = bufBlock.Length + inLimit1 - inOff;
- Array.Copy(input, inOff, bufBlock, 0, bufOff);
- }
- return resultLen;
- #endif
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- public unsafe int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
- {
- CheckStatus();
- int resultLen = 0;
- if (forEncryption)
- {
- if (bufOff > 0)
- {
- int available = BlockSize - bufOff;
- if (input.Length < available)
- {
- input.CopyTo(bufBlock.AsSpan(bufOff));
- bufOff += input.Length;
- return 0;
- }
- input[..available].CopyTo(bufBlock.AsSpan(bufOff));
- EncryptBlock(bufBlock, output);
- input = input[available..];
- resultLen = BlockSize;
- //bufOff = 0;
- }
- while (input.Length >= BlockSize * 2)
- {
- EncryptBlocks2(input, output[resultLen..]);
- input = input[(BlockSize * 2)..];
- resultLen += BlockSize * 2;
- }
- if (input.Length >= BlockSize)
- {
- EncryptBlock(input, output[resultLen..]);
- input = input[BlockSize..];
- resultLen += BlockSize;
- }
- bufOff = input.Length;
- input.CopyTo(bufBlock);
- }
- else
- {
- int available = bufBlock.Length - bufOff;
- if (input.Length < available)
- {
- input.CopyTo(bufBlock.AsSpan(bufOff));
- bufOff += input.Length;
- return 0;
- }
- if (bufOff >= BlockSize)
- {
- DecryptBlock(bufBlock, output);
- Array.Copy(bufBlock, BlockSize, bufBlock, 0, bufOff -= BlockSize);
- resultLen = BlockSize;
- available += BlockSize;
- if (input.Length < available)
- {
- input.CopyTo(bufBlock.AsSpan(bufOff));
- bufOff += input.Length;
- return resultLen;
- }
- }
- int inLimit1 = bufBlock.Length;
- int inLimit2 = inLimit1 + BlockSize;
- available = BlockSize - bufOff;
- input[..available].CopyTo(bufBlock.AsSpan(bufOff));
- DecryptBlock(bufBlock, output[resultLen..]);
- input = input[available..];
- resultLen += BlockSize;
- //bufOff = 0;
- while (input.Length >= inLimit2)
- {
- //DecryptBlocks2(input, output[resultLen..]);
- var outputSample = output[resultLen..];
- if (totalLength == 0)
- {
- InitCipher();
- }
- Span<byte> ctrBlock = stackalloc byte[BlockSize];
- //GetNextCtrBlock(ctrBlock);
- if (blocksRemaining == 0)
- throw new InvalidOperationException("Attempt to process too many blocks");
- blocksRemaining--;
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- BinaryPrimitives.WriteUInt32BigEndian(counter.AsSpan(12), ++counter32);
- #else
- Pack.UInt32_To_BE(++counter32, counter, 12);
- #endif
- cipher.ProcessBlock(counter, ctrBlock);
- ReadOnlySpan<uint> uInput = MemoryMarshal.Cast<byte, uint>(input);
- Span<uint> uS = MemoryMarshal.Cast<byte, uint>(S);
- ReadOnlySpan<uint> uCtrBlock = MemoryMarshal.Cast<byte, uint>(ctrBlock);
- Span<uint> uOutput = MemoryMarshal.Cast<byte, uint>(outputSample);
- uS[0] ^= uInput[0];
- uOutput[0] = uInput[0] ^ uCtrBlock[0];
- uS[1] ^= uInput[1];
- uOutput[1] = uInput[1] ^ uCtrBlock[1];
- uS[2] ^= uInput[2];
- uOutput[2] = uInput[2] ^ uCtrBlock[2];
- uS[3] ^= uInput[3];
- uOutput[3] = uInput[3] ^ uCtrBlock[3];
- multiplier.MultiplyH(S);
- input = input[BlockSize..];
- outputSample = outputSample[BlockSize..];
- //GetNextCtrBlock(ctrBlock);
- if (blocksRemaining == 0)
- throw new InvalidOperationException("Attempt to process too many blocks");
- blocksRemaining--;
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- BinaryPrimitives.WriteUInt32BigEndian(counter.AsSpan(12), ++counter32);
- #else
- Pack.UInt32_To_BE(++counter32, counter, 12);
- #endif
- cipher.ProcessBlock(counter, ctrBlock);
- uInput = MemoryMarshal.Cast<byte, uint>(input);
- uS = MemoryMarshal.Cast<byte, uint>(S);
- uCtrBlock = MemoryMarshal.Cast<byte, uint>(ctrBlock);
- uOutput = MemoryMarshal.Cast<byte, uint>(outputSample);
- uS[0] ^= uInput[0];
- uOutput[0] = uInput[0] ^ uCtrBlock[0];
- uS[1] ^= uInput[1];
- uOutput[1] = uInput[1] ^ uCtrBlock[1];
- uS[2] ^= uInput[2];
- uOutput[2] = uInput[2] ^ uCtrBlock[2];
- uS[3] ^= uInput[3];
- uOutput[3] = uInput[3] ^ uCtrBlock[3];
- multiplier.MultiplyH(S);
- totalLength += BlockSize * 2;
- //input = input[(BlockSize * 2)..];
- input = input[BlockSize..];
- resultLen += BlockSize * 2;
- }
- if (input.Length >= inLimit1)
- {
- DecryptBlock(input, output[resultLen..]);
- input = input[BlockSize..];
- resultLen += BlockSize;
- }
- bufOff = input.Length;
- input.CopyTo(bufBlock);
- }
- return resultLen;
- }
- #endif
- public int DoFinal(byte[] output, int outOff)
- {
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- return DoFinal(output.AsSpan(outOff));
- #else
- CheckStatus();
- 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)
- {
- ProcessPartial(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 BasicGcmExponentiator();
- 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;
- #endif
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- public int DoFinal(Span<byte> output)
- {
- CheckStatus();
- if (totalLength == 0)
- {
- InitCipher();
- }
- int extra = bufOff;
- if (forEncryption)
- {
- Check.OutputLength(output, extra + macSize, "output buffer too short");
- }
- else
- {
- if (extra < macSize)
- throw new InvalidCipherTextException("data too short");
- extra -= macSize;
- Check.OutputLength(output, extra, "output buffer too short");
- }
- if (extra > 0)
- {
- ProcessPartial(bufBlock.AsSpan(0, extra), output);
- }
- 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 BasicGcmExponentiator();
- 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
- Span<byte> X = stackalloc byte[BlockSize];
- Pack.UInt64_To_BE(atLength * 8UL, X);
- Pack.UInt64_To_BE(totalLength * 8UL, X[8..]);
- gHASHBlock(S, X);
- // T = MSBt(GCTRk(J0,S))
- Span<byte> tag = stackalloc byte[BlockSize];
- cipher.ProcessBlock(J0, tag);
- GcmUtilities.Xor(tag, S);
- int resultLen = extra;
- // We place into macBlock our calculated value for T
- this.macBlock = new byte[macSize];
- tag[..macSize].CopyTo(macBlock);
- if (forEncryption)
- {
- // Append T to the message
- macBlock.CopyTo(output[bufOff..]);
- resultLen += macSize;
- }
- else
- {
- // Retrieve the T value from the message and compare to calculated one
- Span<byte> msgMac = stackalloc byte[macSize];
- bufBlock.AsSpan(extra, macSize).CopyTo(msgMac);
- if (!Arrays.ConstantTimeAreEqual(this.macBlock, msgMac))
- throw new InvalidCipherTextException("mac check in GCM failed");
- }
- Reset(false);
- return resultLen;
- }
- #endif
- public void Reset()
- {
- Reset(true);
- }
- private void Reset(bool clearMac)
- {
- // note: we do not reset the nonce.
- S = new byte[BlockSize];
- S_at = new byte[BlockSize];
- S_atPre = new byte[BlockSize];
- atBlock = new byte[BlockSize];
- atBlockPos = 0;
- atLength = 0;
- atLengthPre = 0;
- counter = Arrays.Clone(J0);
- counter32 = Pack.BE_To_UInt32(counter, 12);
- blocksRemaining = uint.MaxValue - 1;
- bufOff = 0;
- totalLength = 0;
- if (bufBlock != null)
- {
- Arrays.Fill(bufBlock, 0);
- }
- if (clearMac)
- {
- macBlock = null;
- }
- if (forEncryption)
- {
- initialised = false;
- }
- else
- {
- if (initialAssociatedText != null)
- {
- ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length);
- }
- }
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- private void DecryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
- {
- Check.OutputLength(output, BlockSize, "output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- Span<byte> ctrBlock = stackalloc byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- #if BESTHTTP_WITH_BURST
- FastGcmBlockCipherHelper.DecryptBlock(input, output, ctrBlock, S.AsSpan(), BlockSize);
- #elif NETCOREAPP3_0_OR_GREATER
- if (Sse2.IsSupported && Unsafe.SizeOf<Vector128<byte>>() == BlockSize)
- {
- var t0 = MemoryMarshal.Read<Vector128<byte>>(input);
- var t1 = MemoryMarshal.Read<Vector128<byte>>(ctrBlock);
- var t2 = MemoryMarshal.Read<Vector128<byte>>(S.AsSpan());
- t1 = Sse2.Xor(t1, t0);
- t2 = Sse2.Xor(t2, t0);
- MemoryMarshal.Write(output, ref t1);
- MemoryMarshal.Write(S.AsSpan(), ref t2);
- }
- else
- #else
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = input[i + 0];
- byte c1 = input[i + 1];
- byte c2 = input[i + 2];
- byte c3 = input[i + 3];
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- output[i + 0] = (byte)(c0 ^ ctrBlock[i + 0]);
- output[i + 1] = (byte)(c1 ^ ctrBlock[i + 1]);
- output[i + 2] = (byte)(c2 ^ ctrBlock[i + 2]);
- output[i + 3] = (byte)(c3 ^ ctrBlock[i + 3]);
- }
- }
- #endif
- multiplier.MultiplyH(S);
- totalLength += BlockSize;
- }
- private void DecryptBlocks2(ReadOnlySpan<byte> input, Span<byte> outputSample)
- {
- Check.OutputLength(outputSample, BlockSize * 2, "output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- Span<byte> ctrBlock = stackalloc byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = input[i + 0];
- byte c1 = input[i + 1];
- byte c2 = input[i + 2];
- byte c3 = input[i + 3];
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outputSample[i + 0] = (byte)(c0 ^ ctrBlock[i + 0]);
- outputSample[i + 1] = (byte)(c1 ^ ctrBlock[i + 1]);
- outputSample[i + 2] = (byte)(c2 ^ ctrBlock[i + 2]);
- outputSample[i + 3] = (byte)(c3 ^ ctrBlock[i + 3]);
- }
- multiplier.MultiplyH(S);
- input = input[BlockSize..];
- outputSample = outputSample[BlockSize..];
- GetNextCtrBlock(ctrBlock);
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = input[i + 0];
- byte c1 = input[i + 1];
- byte c2 = input[i + 2];
- byte c3 = input[i + 3];
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outputSample[i + 0] = (byte)(c0 ^ ctrBlock[i + 0]);
- outputSample[i + 1] = (byte)(c1 ^ ctrBlock[i + 1]);
- outputSample[i + 2] = (byte)(c2 ^ ctrBlock[i + 2]);
- outputSample[i + 3] = (byte)(c3 ^ ctrBlock[i + 3]);
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize * 2;
- }
- private void EncryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
- {
- Check.OutputLength(output, BlockSize, "output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- Span<byte> ctrBlock = stackalloc byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- #if NETCOREAPP3_0_OR_GREATER
- if (Sse2.IsSupported && Unsafe.SizeOf<Vector128<byte>>() == BlockSize)
- {
- var t0 = MemoryMarshal.Read<Vector128<byte>>(input);
- var t1 = MemoryMarshal.Read<Vector128<byte>>(ctrBlock);
- var t2 = MemoryMarshal.Read<Vector128<byte>>(S.AsSpan());
- t1 = Sse2.Xor(t1, t0);
- t2 = Sse2.Xor(t2, t1);
- MemoryMarshal.Write(output, ref t1);
- MemoryMarshal.Write(S.AsSpan(), ref t2);
- }
- else
- #endif
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = (byte)(ctrBlock[i + 0] ^ input[i + 0]);
- byte c1 = (byte)(ctrBlock[i + 1] ^ input[i + 1]);
- byte c2 = (byte)(ctrBlock[i + 2] ^ input[i + 2]);
- byte c3 = (byte)(ctrBlock[i + 3] ^ input[i + 3]);
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- output[i + 0] = c0;
- output[i + 1] = c1;
- output[i + 2] = c2;
- output[i + 3] = c3;
- }
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize;
- }
- private void EncryptBlocks2(ReadOnlySpan<byte> input, Span<byte> output)
- {
- Check.OutputLength(output, BlockSize * 2, "Output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- Span<byte> ctrBlock = stackalloc byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- #if NETCOREAPP3_0_OR_GREATER
- if (Sse2.IsSupported && Unsafe.SizeOf<Vector128<byte>>() == BlockSize)
- {
- var t0 = MemoryMarshal.Read<Vector128<byte>>(input);
- var t1 = MemoryMarshal.Read<Vector128<byte>>(ctrBlock);
- var t2 = MemoryMarshal.Read<Vector128<byte>>(S.AsSpan());
- t1 = Sse2.Xor(t1, t0);
- t2 = Sse2.Xor(t2, t1);
- MemoryMarshal.Write(output, ref t1);
- MemoryMarshal.Write(S.AsSpan(), ref t2);
- }
- else
- #endif
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = (byte)(ctrBlock[i + 0] ^ input[i + 0]);
- byte c1 = (byte)(ctrBlock[i + 1] ^ input[i + 1]);
- byte c2 = (byte)(ctrBlock[i + 2] ^ input[i + 2]);
- byte c3 = (byte)(ctrBlock[i + 3] ^ input[i + 3]);
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- output[i + 0] = c0;
- output[i + 1] = c1;
- output[i + 2] = c2;
- output[i + 3] = c3;
- }
- }
- multiplier.MultiplyH(S);
- input = input[BlockSize..];
- output = output[BlockSize..];
- GetNextCtrBlock(ctrBlock);
- #if NETCOREAPP3_0_OR_GREATER
- if (Sse2.IsSupported && Unsafe.SizeOf<Vector128<byte>>() == BlockSize)
- {
- var t0 = MemoryMarshal.Read<Vector128<byte>>(input);
- var t1 = MemoryMarshal.Read<Vector128<byte>>(ctrBlock);
- var t2 = MemoryMarshal.Read<Vector128<byte>>(S.AsSpan());
- t1 = Sse2.Xor(t1, t0);
- t2 = Sse2.Xor(t2, t1);
- MemoryMarshal.Write(output, ref t1);
- MemoryMarshal.Write(S.AsSpan(), ref t2);
- }
- else
- #endif
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = (byte)(ctrBlock[i + 0] ^ input[i + 0]);
- byte c1 = (byte)(ctrBlock[i + 1] ^ input[i + 1]);
- byte c2 = (byte)(ctrBlock[i + 2] ^ input[i + 2]);
- byte c3 = (byte)(ctrBlock[i + 3] ^ input[i + 3]);
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- output[i + 0] = c0;
- output[i + 1] = c1;
- output[i + 2] = c2;
- output[i + 3] = c3;
- }
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize * 2;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void GetNextCtrBlock(Span<byte> block)
- {
- if (blocksRemaining == 0)
- throw new InvalidOperationException("Attempt to process too many blocks");
- blocksRemaining--;
- Pack.UInt32_To_BE(++counter32, counter, 12);
- cipher.ProcessBlock(counter, block);
- }
- private void ProcessPartial(Span<byte> partialBlock, Span<byte> output)
- {
- Span<byte> ctrBlock = stackalloc byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- if (forEncryption)
- {
- GcmUtilities.Xor(partialBlock, ctrBlock, partialBlock.Length);
- gHASHPartial(S, partialBlock);
- }
- else
- {
- gHASHPartial(S, partialBlock);
- GcmUtilities.Xor(partialBlock, ctrBlock, partialBlock.Length);
- }
- partialBlock.CopyTo(output);
- totalLength += (uint)partialBlock.Length;
- }
- #else
- private void DecryptBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
- {
- Check.OutputLength(outBuf, outOff, BlockSize, "Output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- //byte[] ctrBlock = new byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = inBuf[inOff + i + 0];
- byte c1 = inBuf[inOff + i + 1];
- byte c2 = inBuf[inOff + i + 2];
- byte c3 = inBuf[inOff + i + 3];
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outBuf[outOff + i + 0] = (byte)(c0 ^ ctrBlock[i + 0]);
- outBuf[outOff + i + 1] = (byte)(c1 ^ ctrBlock[i + 1]);
- outBuf[outOff + i + 2] = (byte)(c2 ^ ctrBlock[i + 2]);
- outBuf[outOff + i + 3] = (byte)(c3 ^ ctrBlock[i + 3]);
- }
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize;
- }
- private void DecryptBlocks2(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
- {
- Check.OutputLength(outBuf, outOff, BlockSize * 2, "Output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- //byte[] ctrBlock = new byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = inBuf[inOff + i + 0];
- byte c1 = inBuf[inOff + i + 1];
- byte c2 = inBuf[inOff + i + 2];
- byte c3 = inBuf[inOff + i + 3];
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outBuf[outOff + i + 0] = (byte)(c0 ^ ctrBlock[i + 0]);
- outBuf[outOff + i + 1] = (byte)(c1 ^ ctrBlock[i + 1]);
- outBuf[outOff + i + 2] = (byte)(c2 ^ ctrBlock[i + 2]);
- outBuf[outOff + i + 3] = (byte)(c3 ^ ctrBlock[i + 3]);
- }
- }
- multiplier.MultiplyH(S);
- inOff += BlockSize;
- outOff += BlockSize;
- GetNextCtrBlock(ctrBlock);
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = inBuf[inOff + i + 0];
- byte c1 = inBuf[inOff + i + 1];
- byte c2 = inBuf[inOff + i + 2];
- byte c3 = inBuf[inOff + i + 3];
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outBuf[outOff + i + 0] = (byte)(c0 ^ ctrBlock[i + 0]);
- outBuf[outOff + i + 1] = (byte)(c1 ^ ctrBlock[i + 1]);
- outBuf[outOff + i + 2] = (byte)(c2 ^ ctrBlock[i + 2]);
- outBuf[outOff + i + 3] = (byte)(c3 ^ ctrBlock[i + 3]);
- }
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize * 2;
- }
- private void EncryptBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
- {
- Check.OutputLength(outBuf, outOff, BlockSize, "Output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- //byte[] ctrBlock = new byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = (byte)(ctrBlock[i + 0] ^ inBuf[inOff + i + 0]);
- byte c1 = (byte)(ctrBlock[i + 1] ^ inBuf[inOff + i + 1]);
- byte c2 = (byte)(ctrBlock[i + 2] ^ inBuf[inOff + i + 2]);
- byte c3 = (byte)(ctrBlock[i + 3] ^ inBuf[inOff + i + 3]);
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outBuf[outOff + i + 0] = c0;
- outBuf[outOff + i + 1] = c1;
- outBuf[outOff + i + 2] = c2;
- outBuf[outOff + i + 3] = c3;
- }
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize;
- }
- private void EncryptBlocks2(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
- {
- Check.OutputLength(outBuf, outOff, BlockSize * 2, "Output buffer too short");
- if (totalLength == 0)
- {
- InitCipher();
- }
- //byte[] ctrBlock = new byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = (byte)(ctrBlock[i + 0] ^ inBuf[inOff + i + 0]);
- byte c1 = (byte)(ctrBlock[i + 1] ^ inBuf[inOff + i + 1]);
- byte c2 = (byte)(ctrBlock[i + 2] ^ inBuf[inOff + i + 2]);
- byte c3 = (byte)(ctrBlock[i + 3] ^ inBuf[inOff + i + 3]);
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outBuf[outOff + i + 0] = c0;
- outBuf[outOff + i + 1] = c1;
- outBuf[outOff + i + 2] = c2;
- outBuf[outOff + i + 3] = c3;
- }
- }
- multiplier.MultiplyH(S);
- inOff += BlockSize;
- outOff += BlockSize;
- GetNextCtrBlock(ctrBlock);
- {
- for (int i = 0; i < BlockSize; i += 4)
- {
- byte c0 = (byte)(ctrBlock[i + 0] ^ inBuf[inOff + i + 0]);
- byte c1 = (byte)(ctrBlock[i + 1] ^ inBuf[inOff + i + 1]);
- byte c2 = (byte)(ctrBlock[i + 2] ^ inBuf[inOff + i + 2]);
- byte c3 = (byte)(ctrBlock[i + 3] ^ inBuf[inOff + i + 3]);
- S[i + 0] ^= c0;
- S[i + 1] ^= c1;
- S[i + 2] ^= c2;
- S[i + 3] ^= c3;
- outBuf[outOff + i + 0] = c0;
- outBuf[outOff + i + 1] = c1;
- outBuf[outOff + i + 2] = c2;
- outBuf[outOff + i + 3] = c3;
- }
- }
- multiplier.MultiplyH(S);
- totalLength += BlockSize * 2;
- }
- private void GetNextCtrBlock(byte[] block)
- {
- if (blocksRemaining == 0)
- throw new InvalidOperationException("Attempt to process too many blocks");
- blocksRemaining--;
- Pack.UInt32_To_BE(++counter32, counter, 12);
- cipher.ProcessBlock(counter, 0, block, 0);
- }
- private void ProcessPartial(byte[] buf, int off, int len, byte[] output, int outOff)
- {
- //byte[] ctrBlock = new byte[BlockSize];
- GetNextCtrBlock(ctrBlock);
- if (forEncryption)
- {
- GcmUtilities.Xor(buf, off, ctrBlock, 0, len);
- gHASHPartial(S, buf, off, len);
- }
- else
- {
- gHASHPartial(S, buf, off, len);
- GcmUtilities.Xor(buf, off, ctrBlock, 0, len);
- }
- Array.Copy(buf, off, output, outOff, len);
- totalLength += (uint)len;
- }
- #endif
- private void gHASH(byte[] Y, byte[] b, int len)
- {
- for (int pos = 0; pos < len; pos += BlockSize)
- {
- int num = System.Math.Min(len - pos, BlockSize);
- gHASHPartial(Y, b, pos, num);
- }
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || _UNITY_2021_2_OR_NEWER_
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void gHASHBlock(byte[] Y, ReadOnlySpan<byte> b)
- {
- GcmUtilities.Xor(Y, b);
- multiplier.MultiplyH(Y);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void gHASHPartial(byte[] Y, ReadOnlySpan<byte> b)
- {
- GcmUtilities.Xor(Y, b, b.Length);
- multiplier.MultiplyH(Y);
- }
- #else
- private void gHASHBlock(byte[] Y, byte[] b)
- {
- GcmUtilities.Xor(Y, b);
- multiplier.MultiplyH(Y);
- }
- private void gHASHBlock(byte[] Y, byte[] b, int off)
- {
- GcmUtilities.Xor(Y, b, off);
- multiplier.MultiplyH(Y);
- }
- #endif
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void gHASHPartial(byte[] Y, byte[] b, int off, int len)
- {
- GcmUtilities.Xor(Y, b, off, len);
- multiplier.MultiplyH(Y);
- }
- private void CheckStatus()
- {
- if (!initialised)
- {
- if (forEncryption)
- {
- throw new InvalidOperationException("GCM cipher cannot be reused for encryption");
- }
- throw new InvalidOperationException("GCM cipher needs to be initialised");
- }
- }
- }
- }
- #pragma warning restore
- #endif
|