diff --git a/src/NATS.Client.Core/NaCl/Internal/Array16.cs b/src/NATS.Client.Core/NaCl/Internal/Array16.cs deleted file mode 100644 index c642a8d42..000000000 --- a/src/NATS.Client.Core/NaCl/Internal/Array16.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace NATS.Client.Core.NaCl.Internal -{ - // Array16 Salsa20 state - // Array16 SHA-512 block - internal struct Array16 - { - public T x0; - public T x1; - public T x2; - public T x3; - public T x4; - public T x5; - public T x6; - public T x7; - public T x8; - public T x9; - public T x10; - public T x11; - public T x12; - public T x13; - public T x14; - public T x15; - } -} diff --git a/src/NATS.Client.Core/NaCl/Internal/Array8.cs b/src/NATS.Client.Core/NaCl/Internal/Array8.cs deleted file mode 100644 index edb52b0ab..000000000 --- a/src/NATS.Client.Core/NaCl/Internal/Array8.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace NATS.Client.Core.NaCl.Internal -{ - // Array8 Poly1305 key - // Array8 SHA-512 state/output - internal struct Array8 - { - public T x0; - public T x1; - public T x2; - public T x3; - public T x4; - public T x5; - public T x6; - public T x7; - } -} diff --git a/src/NATS.Client.Core/NaCl/Internal/ByteIntegerConverter.cs b/src/NATS.Client.Core/NaCl/Internal/ByteIntegerConverter.cs deleted file mode 100644 index a7bcfbcf8..000000000 --- a/src/NATS.Client.Core/NaCl/Internal/ByteIntegerConverter.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; - -namespace NATS.Client.Core.NaCl.Internal -{ - // Loops? Arrays? Never heard of that stuff - // Library avoids unnecessary heap allocations and unsafe code - // so this ugly code becomes necessary :( - internal static class ByteIntegerConverter - { - public static ulong LoadBigEndian64(byte[] buf, int offset) - { - return - (ulong)(buf[offset + 7]) - | (((ulong)(buf[offset + 6])) << 8) - | (((ulong)(buf[offset + 5])) << 16) - | (((ulong)(buf[offset + 4])) << 24) - | (((ulong)(buf[offset + 3])) << 32) - | (((ulong)(buf[offset + 2])) << 40) - | (((ulong)(buf[offset + 1])) << 48) - | (((ulong)(buf[offset + 0])) << 56); - } - - public static void StoreBigEndian64(byte[] buf, int offset, ulong value) - { - buf[offset + 7] = unchecked((byte)value); - buf[offset + 6] = unchecked((byte)(value >> 8)); - buf[offset + 5] = unchecked((byte)(value >> 16)); - buf[offset + 4] = unchecked((byte)(value >> 24)); - buf[offset + 3] = unchecked((byte)(value >> 32)); - buf[offset + 2] = unchecked((byte)(value >> 40)); - buf[offset + 1] = unchecked((byte)(value >> 48)); - buf[offset + 0] = unchecked((byte)(value >> 56)); - } - - public static void Array16LoadBigEndian64(out Array16 output, byte[] input, int inputOffset) - { - output.x0 = LoadBigEndian64(input, inputOffset + 0); - output.x1 = LoadBigEndian64(input, inputOffset + 8); - output.x2 = LoadBigEndian64(input, inputOffset + 16); - output.x3 = LoadBigEndian64(input, inputOffset + 24); - output.x4 = LoadBigEndian64(input, inputOffset + 32); - output.x5 = LoadBigEndian64(input, inputOffset + 40); - output.x6 = LoadBigEndian64(input, inputOffset + 48); - output.x7 = LoadBigEndian64(input, inputOffset + 56); - output.x8 = LoadBigEndian64(input, inputOffset + 64); - output.x9 = LoadBigEndian64(input, inputOffset + 72); - output.x10 = LoadBigEndian64(input, inputOffset + 80); - output.x11 = LoadBigEndian64(input, inputOffset + 88); - output.x12 = LoadBigEndian64(input, inputOffset + 96); - output.x13 = LoadBigEndian64(input, inputOffset + 104); - output.x14 = LoadBigEndian64(input, inputOffset + 112); - output.x15 = LoadBigEndian64(input, inputOffset + 120); - } - } -} diff --git a/src/NATS.Client.Core/NaCl/Internal/Sha512Internal.cs b/src/NATS.Client.Core/NaCl/Internal/Sha512Internal.cs deleted file mode 100644 index 42d78b8f2..000000000 --- a/src/NATS.Client.Core/NaCl/Internal/Sha512Internal.cs +++ /dev/null @@ -1,447 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace NATS.Client.Core.NaCl.Internal -{ - internal static class Sha512Internal - { - private static readonly ulong[] K = new ulong[] - { - 0x428a2f98d728ae22,0x7137449123ef65cd,0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc, - 0x3956c25bf348b538,0x59f111f1b605d019,0x923f82a4af194f9b,0xab1c5ed5da6d8118, - 0xd807aa98a3030242,0x12835b0145706fbe,0x243185be4ee4b28c,0x550c7dc3d5ffb4e2, - 0x72be5d74f27b896f,0x80deb1fe3b1696b1,0x9bdc06a725c71235,0xc19bf174cf692694, - 0xe49b69c19ef14ad2,0xefbe4786384f25e3,0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65, - 0x2de92c6f592b0275,0x4a7484aa6ea6e483,0x5cb0a9dcbd41fbd4,0x76f988da831153b5, - 0x983e5152ee66dfab,0xa831c66d2db43210,0xb00327c898fb213f,0xbf597fc7beef0ee4, - 0xc6e00bf33da88fc2,0xd5a79147930aa725,0x06ca6351e003826f,0x142929670a0e6e70, - 0x27b70a8546d22ffc,0x2e1b21385c26c926,0x4d2c6dfc5ac42aed,0x53380d139d95b3df, - 0x650a73548baf63de,0x766a0abb3c77b2a8,0x81c2c92e47edaee6,0x92722c851482353b, - 0xa2bfe8a14cf10364,0xa81a664bbc423001,0xc24b8b70d0f89791,0xc76c51a30654be30, - 0xd192e819d6ef5218,0xd69906245565a910,0xf40e35855771202a,0x106aa07032bbd1b8, - 0x19a4c116b8d2d0c8,0x1e376c085141ab53,0x2748774cdf8eeb99,0x34b0bcb5e19b48a8, - 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb,0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3, - 0x748f82ee5defb2fc,0x78a5636f43172f60,0x84c87814a1f0ab72,0x8cc702081a6439ec, - 0x90befffa23631e28,0xa4506cebde82bde9,0xbef9a3f7b2c67915,0xc67178f2e372532b, - 0xca273eceea26619c,0xd186b8c721c0c207,0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178, - 0x06f067aa72176fba,0x0a637dc5a2c898a6,0x113f9804bef90dae,0x1b710b35131c471b, - 0x28db77f523047d84,0x32caab7b40c72493,0x3c9ebe0a15c9bebc,0x431d67c49c100d4c, - 0x4cc5d4becb3e42b6,0x597f299cfc657e2a,0x5fcb6fab3ad6faec,0x6c44198c4a475817 - }; - - internal static void Sha512Init(out Array8 state) - { - state.x0 = 0x6a09e667f3bcc908; - state.x1 = 0xbb67ae8584caa73b; - state.x2 = 0x3c6ef372fe94f82b; - state.x3 = 0xa54ff53a5f1d36f1; - state.x4 = 0x510e527fade682d1; - state.x5 = 0x9b05688c2b3e6c1f; - state.x6 = 0x1f83d9abfb41bd6b; - state.x7 = 0x5be0cd19137e2179; - } - - internal static void Core(out Array8 outputState, ref Array8 inputState, ref Array16 input) - { - unchecked - { - var a = inputState.x0; - var b = inputState.x1; - var c = inputState.x2; - var d = inputState.x3; - var e = inputState.x4; - var f = inputState.x5; - var g = inputState.x6; - var h = inputState.x7; - - var w0 = input.x0; - var w1 = input.x1; - var w2 = input.x2; - var w3 = input.x3; - var w4 = input.x4; - var w5 = input.x5; - var w6 = input.x6; - var w7 = input.x7; - var w8 = input.x8; - var w9 = input.x9; - var w10 = input.x10; - var w11 = input.x11; - var w12 = input.x12; - var w13 = input.x13; - var w14 = input.x14; - var w15 = input.x15; - - int t = 0; - while (true) - { - ulong t1, t2; - - {//0 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w0; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//1 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w1; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//2 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w2; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//3 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w3; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//4 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w4; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//5 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w5; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//6 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w6; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//7 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w7; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//8 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w8; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//9 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w9; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//10 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w10; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//11 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w11; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//12 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w12; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//13 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w13; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//14 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w14; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - {//15 - t1 = h + - ((e >> 14) ^ (e << (64 - 14)) ^ (e >> 18) ^ (e << (64 - 18)) ^ (e >> 41) ^ (e << (64 - 41))) + - //Sigma1(e) - ((e & f) ^ (~e & g)) + //Ch(e,f,g) - K[t] + w15; - t2 = ((a >> 28) ^ (a << (64 - 28)) ^ (a >> 34) ^ (a << (64 - 34)) ^ (a >> 39) ^ (a << (64 - 39))) + - //Sigma0(a) - ((a & b) ^ (a & c) ^ (b & c)); //Maj(a,b,c) - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - t++; - } - if (t == 80) - break; - - w0 += ((w14 >> 19) ^ (w14 << (64 - 19)) ^ (w14 >> 61) ^ (w14 << (64 - 61)) ^ (w14 >> 6)) + - w9 + - ((w1 >> 1) ^ (w1 << (64 - 1)) ^ (w1 >> 8) ^ (w1 << (64 - 8)) ^ (w1 >> 7)); - w1 += ((w15 >> 19) ^ (w15 << (64 - 19)) ^ (w15 >> 61) ^ (w15 << (64 - 61)) ^ (w15 >> 6)) + - w10 + - ((w2 >> 1) ^ (w2 << (64 - 1)) ^ (w2 >> 8) ^ (w2 << (64 - 8)) ^ (w2 >> 7)); - w2 += ((w0 >> 19) ^ (w0 << (64 - 19)) ^ (w0 >> 61) ^ (w0 << (64 - 61)) ^ (w0 >> 6)) + - w11 + - ((w3 >> 1) ^ (w3 << (64 - 1)) ^ (w3 >> 8) ^ (w3 << (64 - 8)) ^ (w3 >> 7)); - w3 += ((w1 >> 19) ^ (w1 << (64 - 19)) ^ (w1 >> 61) ^ (w1 << (64 - 61)) ^ (w1 >> 6)) + - w12 + - ((w4 >> 1) ^ (w4 << (64 - 1)) ^ (w4 >> 8) ^ (w4 << (64 - 8)) ^ (w4 >> 7)); - w4 += ((w2 >> 19) ^ (w2 << (64 - 19)) ^ (w2 >> 61) ^ (w2 << (64 - 61)) ^ (w2 >> 6)) + - w13 + - ((w5 >> 1) ^ (w5 << (64 - 1)) ^ (w5 >> 8) ^ (w5 << (64 - 8)) ^ (w5 >> 7)); - w5 += ((w3 >> 19) ^ (w3 << (64 - 19)) ^ (w3 >> 61) ^ (w3 << (64 - 61)) ^ (w3 >> 6)) + - w14 + - ((w6 >> 1) ^ (w6 << (64 - 1)) ^ (w6 >> 8) ^ (w6 << (64 - 8)) ^ (w6 >> 7)); - w6 += ((w4 >> 19) ^ (w4 << (64 - 19)) ^ (w4 >> 61) ^ (w4 << (64 - 61)) ^ (w4 >> 6)) + - w15 + - ((w7 >> 1) ^ (w7 << (64 - 1)) ^ (w7 >> 8) ^ (w7 << (64 - 8)) ^ (w7 >> 7)); - w7 += ((w5 >> 19) ^ (w5 << (64 - 19)) ^ (w5 >> 61) ^ (w5 << (64 - 61)) ^ (w5 >> 6)) + - w0 + - ((w8 >> 1) ^ (w8 << (64 - 1)) ^ (w8 >> 8) ^ (w8 << (64 - 8)) ^ (w8 >> 7)); - w8 += ((w6 >> 19) ^ (w6 << (64 - 19)) ^ (w6 >> 61) ^ (w6 << (64 - 61)) ^ (w6 >> 6)) + - w1 + - ((w9 >> 1) ^ (w9 << (64 - 1)) ^ (w9 >> 8) ^ (w9 << (64 - 8)) ^ (w9 >> 7)); - w9 += ((w7 >> 19) ^ (w7 << (64 - 19)) ^ (w7 >> 61) ^ (w7 << (64 - 61)) ^ (w7 >> 6)) + - w2 + - ((w10 >> 1) ^ (w10 << (64 - 1)) ^ (w10 >> 8) ^ (w10 << (64 - 8)) ^ (w10 >> 7)); - w10 += ((w8 >> 19) ^ (w8 << (64 - 19)) ^ (w8 >> 61) ^ (w8 << (64 - 61)) ^ (w8 >> 6)) + - w3 + - ((w11 >> 1) ^ (w11 << (64 - 1)) ^ (w11 >> 8) ^ (w11 << (64 - 8)) ^ (w11 >> 7)); - w11 += ((w9 >> 19) ^ (w9 << (64 - 19)) ^ (w9 >> 61) ^ (w9 << (64 - 61)) ^ (w9 >> 6)) + - w4 + - ((w12 >> 1) ^ (w12 << (64 - 1)) ^ (w12 >> 8) ^ (w12 << (64 - 8)) ^ (w12 >> 7)); - w12 += ((w10 >> 19) ^ (w10 << (64 - 19)) ^ (w10 >> 61) ^ (w10 << (64 - 61)) ^ (w10 >> 6)) + - w5 + - ((w13 >> 1) ^ (w13 << (64 - 1)) ^ (w13 >> 8) ^ (w13 << (64 - 8)) ^ (w13 >> 7)); - w13 += ((w11 >> 19) ^ (w11 << (64 - 19)) ^ (w11 >> 61) ^ (w11 << (64 - 61)) ^ (w11 >> 6)) + - w6 + - ((w14 >> 1) ^ (w14 << (64 - 1)) ^ (w14 >> 8) ^ (w14 << (64 - 8)) ^ (w14 >> 7)); - w14 += ((w12 >> 19) ^ (w12 << (64 - 19)) ^ (w12 >> 61) ^ (w12 << (64 - 61)) ^ (w12 >> 6)) + - w7 + - ((w15 >> 1) ^ (w15 << (64 - 1)) ^ (w15 >> 8) ^ (w15 << (64 - 8)) ^ (w15 >> 7)); - w15 += ((w13 >> 19) ^ (w13 << (64 - 19)) ^ (w13 >> 61) ^ (w13 << (64 - 61)) ^ (w13 >> 6)) + - w8 + - ((w0 >> 1) ^ (w0 << (64 - 1)) ^ (w0 >> 8) ^ (w0 << (64 - 8)) ^ (w0 >> 7)); - } - - outputState.x0 = inputState.x0 + a; - outputState.x1 = inputState.x1 + b; - outputState.x2 = inputState.x2 + c; - outputState.x3 = inputState.x3 + d; - outputState.x4 = inputState.x4 + e; - outputState.x5 = inputState.x5 + f; - outputState.x6 = inputState.x6 + g; - outputState.x7 = inputState.x7 + h; - } - } - } -} diff --git a/src/NATS.Client.Core/NaCl/Sha512.cs b/src/NATS.Client.Core/NaCl/Sha512.cs index af78223bf..40837d94c 100644 --- a/src/NATS.Client.Core/NaCl/Sha512.cs +++ b/src/NATS.Client.Core/NaCl/Sha512.cs @@ -14,43 +14,29 @@ // Borrowed from https://github.com/CryptoManiac/Ed25519 using System; +using System.Security.Cryptography; using NATS.Client.Core.NaCl.Internal; namespace NATS.Client.Core.NaCl { - internal class Sha512 + internal sealed class Sha512 { - private Array8 _state; - private readonly byte[] _buffer; - private ulong _totalBytes; - public const int BlockSize = 128; - private static readonly byte[] _padding = new byte[] { 0x80 }; - /// /// Allocation and initialization of the new SHA-512 object. /// public Sha512() { - _buffer = new byte[BlockSize];//todo: remove allocation - Init(); + _sha512Inner = SHA512.Create(); } + private readonly SHA512 _sha512Inner; + /// /// Performs an initialization of internal SHA-512 state. /// public void Init() { - Sha512Internal.Sha512Init(out _state); - _totalBytes = 0; - } - - /// - /// Updates internal state with data from the provided array segment. - /// - /// Array segment - public void Update(ArraySegment data) - { - Update(data.Array, data.Offset, data.Count); + _sha512Inner.Initialize(); } /// @@ -61,83 +47,7 @@ public void Update(ArraySegment data) /// Sequence length public void Update(byte[] data, int index, int length) { - // Contract.Requires(data != null); - if (data == null) throw new ArgumentNullException(); - // Contract.Requires(index >=0 && length >= 0); - if (index < 0 || length < 0) throw new ArgumentOutOfRangeException(); - // Contract.Requires((index + length) <= data.Length); - if ((index + length) > data.Length) throw new ArgumentException(); - - Array16 block; - int bytesInBuffer = (int)_totalBytes & (BlockSize - 1); - _totalBytes += (uint)length; - - if (_totalBytes >= ulong.MaxValue / 8) - throw new InvalidOperationException("Too much data"); - // Fill existing buffer - if (bytesInBuffer != 0) - { - var toCopy = Math.Min(BlockSize - bytesInBuffer, length); - Buffer.BlockCopy(data, index, _buffer, bytesInBuffer, toCopy); - index += toCopy; - length -= toCopy; - bytesInBuffer += toCopy; - if (bytesInBuffer == BlockSize) - { - ByteIntegerConverter.Array16LoadBigEndian64(out block, _buffer, 0); - Sha512Internal.Core(out _state, ref _state, ref block); - CryptoBytes.InternalWipe(_buffer, 0, _buffer.Length); - bytesInBuffer = 0; - } - } - // Hash complete blocks without copying - while (length >= BlockSize) - { - ByteIntegerConverter.Array16LoadBigEndian64(out block, data, index); - Sha512Internal.Core(out _state, ref _state, ref block); - index += BlockSize; - length -= BlockSize; - } - // Copy remainder into buffer - if (length > 0) - { - Buffer.BlockCopy(data, index, _buffer, bytesInBuffer, length); - } - } - - /// - /// Finalizes SHA-512 hashing - /// - /// Output buffer - public void Finalize(ArraySegment output) - { - // Contract.Requires(output.Array != null); - if (output.Array == null) throw new ArgumentNullException(); - // Contract.Requires(output.Count == 64); - if (output.Count != 64) throw new ArgumentException(); - - Update(_padding, 0, _padding.Length); - Array16 block; - ByteIntegerConverter.Array16LoadBigEndian64(out block, _buffer, 0); - CryptoBytes.InternalWipe(_buffer, 0, _buffer.Length); - int bytesInBuffer = (int)_totalBytes & (BlockSize - 1); - if (bytesInBuffer > BlockSize - 16) - { - Sha512Internal.Core(out _state, ref _state, ref block); - block = default(Array16); - } - block.x15 = (_totalBytes - 1) * 8; - Sha512Internal.Core(out _state, ref _state, ref block); - - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 0, _state.x0); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 8, _state.x1); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 16, _state.x2); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 24, _state.x3); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 32, _state.x4); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 40, _state.x5); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 48, _state.x6); - ByteIntegerConverter.StoreBigEndian64(output.Array, output.Offset + 56, _state.x7); - _state = default(Array8); + _sha512Inner.TransformBlock(data, index, length, null, 0); } /// @@ -146,21 +56,8 @@ public void Finalize(ArraySegment output) /// Hash bytes public byte[] Finalize() { - var result = new byte[64]; - Finalize(new ArraySegment(result)); - return result; - } - - /// - /// Calculates SHA-512 hash value for the given bytes array. - /// - /// Data bytes array - /// Hash bytes - public static byte[] Hash(byte[] data) - { - // Contract.Requires(data != null); - if (data == null) throw new ArgumentNullException(); - return Hash(data, 0, data.Length); + _ = _sha512Inner.TransformFinalBlock(Array.Empty(), 0, 0); + return _sha512Inner.Hash!; } /// @@ -172,16 +69,9 @@ public static byte[] Hash(byte[] data) /// Hash bytes public static byte[] Hash(byte[] data, int index, int length) { - // Contract.Requires(data != null); - if (data == null) throw new ArgumentNullException(); - // Contract.Requires(index >= 0 && length >= 0); - if (index < 0 || length < 0) throw new ArgumentOutOfRangeException(); - // Contract.Requires((index + length) <= data.Length); - if ((index + length) > data.Length) throw new ArgumentException(); - - var hasher = new Sha512(); - hasher.Update(data, index, length); - return hasher.Finalize(); + ArgumentNullException.ThrowIfNull(data); + ReadOnlySpan dataSpan = data; + return SHA512.HashData(dataSpan.Slice(index, length)); } } }