Mostly done in theory now. Not responding to last packet however.

This commit is contained in:
Manevolent
2018-07-25 11:38:22 -04:00
parent 4c7d011b01
commit 2dd96d39f3
26 changed files with 6361 additions and 11 deletions
@@ -0,0 +1,34 @@
package Punisher.NaCl;
public class CryptoBytes
{
public static Boolean ConstantTimeEquals(byte[] x, int xOffset, byte[] y, int yOffset, int length) throws Exception
{
if (x == null) throw new Exception("x");
if (xOffset < 0) throw new Exception("xOffset" + "xOffset < 0");
if (y == null) throw new Exception("y");
if (yOffset < 0) throw new Exception("yOffset" + "yOffset < 0");
if (length < 0) throw new Exception("length" + "length < 0");
if (x.length - xOffset < length) throw new Exception("xOffset + length > x.Length");
if (y.length - yOffset < length) throw new Exception("yOffset + length > y.Length");
return InternalConstantTimeEquals(x, xOffset, y, yOffset, length);
}
public static boolean InternalConstantTimeEquals(byte[] x, int xOffset, byte[] y, int yOffset, int length)
{
int result = 0;
for (int i = 0; i < length; i++)
{
result |= x[xOffset + i] ^ y[yOffset + i];
}
return result == 0; // Check const time
}
public static void Wipe(byte[] data)
{
for (int i = 0; i < data.length; i++)
data[i] = 0;
}
}
+85
View File
@@ -0,0 +1,85 @@
package Punisher.NaCl;
public class Ed25519
{
public static int PublicKeySizeInBytes = 32;
public static int SignatureSizeInBytes = 64;
public static int ExpandedPrivateKeySizeInBytes = 32 * 2;
public static int PrivateKeySeedSizeInBytes = 32;
public static int SharedKeySizeInBytes = 32;
public static Boolean Verify(byte[] signature, byte[] message, byte[] publicKey) throws Exception
{
if (signature == null)
throw new Exception("signature");
if (message == null)
throw new Exception("message");
if (publicKey == null)
throw new Exception("publicKey");
if (signature.length != SignatureSizeInBytes)
throw new Exception("Signature size must be " + SignatureSizeInBytes);
if (publicKey.length != PublicKeySizeInBytes)
throw new Exception("Public key size must be "+ PublicKeySizeInBytes);
return Ed25519Operations.crypto_sign_verify(signature, 0, message, 0, message.length, publicKey, 0);
}
public static void Sign(byte[] signature, byte[] message, byte[] expandedPrivateKey) throws Exception
{
if (signature == null)
throw new Exception("signature.Array");
if (signature.length != SignatureSizeInBytes)
throw new Exception("signature.Count");
if (expandedPrivateKey == null)
throw new Exception("expandedPrivateKey.Array");
if (expandedPrivateKey.length != ExpandedPrivateKeySizeInBytes)
throw new Exception("expandedPrivateKey.Count");
if (message == null)
throw new Exception("message.Array");
Ed25519Operations.crypto_sign2(signature,0, message, 0, message.length, expandedPrivateKey, 0);
}
public static byte[] Sign(byte[] message, byte[] expandedPrivateKey) throws Exception
{
byte[] signature = new byte[SignatureSizeInBytes];
Sign(signature,message,expandedPrivateKey);
return signature;
}
public static byte[] PublicKeyFromSeed(byte[] privateKeySeed) throws Exception
{
byte[] privateKey = new byte[ExpandedPrivateKeySizeInBytes];
byte[] publicKey = new byte[PublicKeySizeInBytes];
KeyPairFromSeed( publicKey, privateKey, privateKeySeed);
CryptoBytes.Wipe(privateKey);
return publicKey;
}
public static byte[] ExpandedPrivateKeyFromSeed(byte[] privateKeySeed) throws Exception
{
byte[] privateKey = new byte[ExpandedPrivateKeySizeInBytes];
byte[] publicKey = new byte[PublicKeySizeInBytes];
KeyPairFromSeed( publicKey, privateKey, privateKeySeed);
CryptoBytes.Wipe(publicKey);
return privateKey;
}
public static void KeyPairFromSeed( byte[] publicKey, byte[] expandedPrivateKey, byte[] privateKeySeed) throws Exception
{
if (privateKeySeed == null)
throw new Exception("privateKeySeed");
if (privateKeySeed.length != PrivateKeySeedSizeInBytes)
throw new Exception("privateKeySeed");
if (expandedPrivateKey == null)
throw new Exception("expandedPrivateKey");
if (expandedPrivateKey.length != ExpandedPrivateKeySizeInBytes)
throw new Exception("expandedPrivateKey");
if (publicKey == null)
throw new Exception("publicKey");
if (publicKey.length != PublicKeySizeInBytes)
throw new Exception("publicKey");
Ed25519Operations.crypto_sign_keypair(publicKey, 0, expandedPrivateKey, 0, privateKeySeed, 0);
}
}
@@ -0,0 +1,90 @@
package Punisher.NaCl;
import Punisher.NaCl.Internal.Sha512;
import Punisher.NaCl.Internal.Ed25519Ref10.*;
public class Ed25519Operations
{
public static Boolean crypto_sign_verify(byte[] sig, int sigoffset, byte[] m, int moffset, int mlen, byte[] pk, int pkoffset) throws Exception
{
byte[] h;
byte[] checkr = new byte[32];
GroupElementP3 A = new GroupElementP3();
GroupElementP2 R = new GroupElementP2();
if ((sig[sigoffset + 63] & 224) != 0) return false;
if (GroupOperations.ge_frombytes_negate_vartime(A, pk, pkoffset) != 0) return false;
Sha512 hasher = new Sha512();
hasher.Update(sig, sigoffset, 32);
hasher.Update(pk, pkoffset, 32);
hasher.Update(m, moffset, mlen);
h = hasher.Finish();
ScalarOperations.sc_reduce(h);
byte[] sm32 = new byte[32];// todo: remove allocation
System.arraycopy(sig, sigoffset + 32, sm32, 0, 32);
GroupOperations.ge_double_scalarmult_vartime(R, h, A, sm32);
GroupOperations.ge_tobytes(checkr, 0, R);
Boolean result = CryptoBytes.ConstantTimeEquals(checkr, 0, sig, sigoffset, 32);
CryptoBytes.Wipe(h);
CryptoBytes.Wipe(checkr);
return result;
}
public static void crypto_sign2(byte[] sig, int sigoffset, byte[] m, int moffset, int mlen, byte[] sk, int skoffset) throws Exception
{
byte[] az;
byte[] r;
byte[] hram;
GroupElementP3 R = new GroupElementP3();
Sha512 hasher = new Sha512();
{
hasher.Update(sk, skoffset, 32);
az = hasher.Finish();
ScalarOperations.sc_clamp(az, 0);
hasher.Init();
hasher.Update(az, 32, 32);
hasher.Update(m, moffset, mlen);
r = hasher.Finish();
ScalarOperations.sc_reduce(r);
GroupOperations.ge_scalarmult_base(R, r, 0);
GroupOperations.ge_p3_tobytes(sig, sigoffset, R);
hasher.Init();
hasher.Update(sig, sigoffset, 32);
hasher.Update(sk, skoffset + 32, 32);
hasher.Update(m, moffset, mlen);
hram = hasher.Finish();
ScalarOperations.sc_reduce(hram);
byte[] s = new byte[32];// todo: remove allocation
System.arraycopy(sig, sigoffset + 32, s, 0, 32);
ScalarOperations.sc_muladd(s, hram, az, r);
System.arraycopy(s, 0, sig, sigoffset + 32, 32);
CryptoBytes.Wipe(s);
}
}
public static void crypto_sign_keypair(byte[] pk, int pkoffset, byte[] sk, int skoffset, byte[] seed, int seedoffset) throws Exception
{
GroupElementP3 A = new GroupElementP3();
int i;
System.arraycopy(seed, seedoffset, sk, skoffset, 32);
byte[] h = Sha512.Hash(sk, skoffset, 32); // ToDo: Remove alloc
ScalarOperations.sc_clamp(h, 0);
GroupOperations.ge_scalarmult_base(A, h, 0);
GroupOperations.ge_p3_tobytes(pk, pkoffset, A);
for (i = 0; i < 32; ++i)
sk[skoffset + 32 + i] = pk[pkoffset + i];
CryptoBytes.Wipe(h);
}
}
@@ -0,0 +1,21 @@
package Punisher.NaCl.Internal;
public class Array16<T>
{
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;
}
@@ -0,0 +1,16 @@
package Punisher.NaCl.Internal;
// Array8<UInt32> Poly1305 key
// Array8<UInt64> SHA-512 state/output
public class Array8<T>
{
public T x0;
public T x1;
public T x2;
public T x3;
public T x4;
public T x5;
public T x6;
public T x7;
}
@@ -0,0 +1,184 @@
package Punisher.NaCl.Internal;
public class ByteIntegerConverter
{
public static int LoadLittleEndian32(byte[] buf, int offset)
{
return
(int)(buf[offset + 0]& 0xFF)
| (((int)(buf[offset + 1])& 0xFF) << 8)
| (((int)(buf[offset + 2])& 0xFF) << 16)
| (((int)(buf[offset + 3])& 0xFF) << 24);
}
public static void StoreLittleEndian32(byte[] buf, int offset, int value)
{
buf[offset + 0] = ((byte)value);
buf[offset + 1] = ((byte)(value >>> 8));
buf[offset + 2] = ((byte)(value >>> 16));
buf[offset + 3] = ((byte)(value >>> 24));
}
public static long LoadBigEndian64(byte[] buf, int offset)
{
return (long)(buf[offset + 7] & 0xFF)
| (((long)(buf[offset + 6]) & 0xFF) << 8)
| (((long)(buf[offset + 5]) & 0xFF) << 16)
| (((long)(buf[offset + 4]) & 0xFF) << 24)
| (((long)(buf[offset + 3]) & 0xFF) << 32)
| (((long)(buf[offset + 2]) & 0xFF) << 40)
| (((long)(buf[offset + 1]) & 0xFF) << 48)
| (((long)(buf[offset + 0]) & 0xFF) << 56);
//return N;
}
public static void StoreBigEndian64(byte[] buf, int offset, long value)
{
buf[offset + 7] = ((byte)value);
buf[offset + 6] = ((byte)(value >>> 8));
buf[offset + 5] = ((byte)(value >>> 16));
buf[offset + 4] = ((byte)(value >>> 24));
buf[offset + 3] = ((byte)(value >>> 32));
buf[offset + 2] = ((byte)(value >>> 40));
buf[offset + 1] = ((byte)(value >>> 48));
buf[offset + 0] = ((byte)(value >>> 56));
}
/*public static void XorLittleEndian32(byte[] buf, int offset, UInt32 value)
{
buf[offset + 0] ^= (byte)value;
buf[offset + 1] ^= (byte)(value >> 8);
buf[offset + 2] ^= (byte)(value >> 16);
buf[offset + 3] ^= (byte)(value >> 24);
}*/
/*public static void XorLittleEndian32(byte[] output, int outputOffset, byte[] input, int inputOffset, UInt32 value)
{
output[outputOffset + 0] = (byte)(input[inputOffset + 0] ^ value);
output[outputOffset + 1] = (byte)(input[inputOffset + 1] ^ (value >> 8));
output[outputOffset + 2] = (byte)(input[inputOffset + 2] ^ (value >> 16));
output[outputOffset + 3] = (byte)(input[inputOffset + 3] ^ (value >> 24));
}*/
public static void Array8LoadLittleEndian32( Array8<Integer> output, byte[] input, int inputOffset)
{
output.x0 = LoadLittleEndian32(input, inputOffset + 0);
output.x1 = LoadLittleEndian32(input, inputOffset + 4);
output.x2 = LoadLittleEndian32(input, inputOffset + 8);
output.x3 = LoadLittleEndian32(input, inputOffset + 12);
output.x4 = LoadLittleEndian32(input, inputOffset + 16);
output.x5 = LoadLittleEndian32(input, inputOffset + 20);
output.x6 = LoadLittleEndian32(input, inputOffset + 24);
output.x7 = LoadLittleEndian32(input, inputOffset + 28);
}
public static void Array16LoadBigEndian64( Array16<Long> 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);
}
// ToDo: Only used in tests. Remove?
public static void Array16LoadLittleEndian32( Array16<Integer> output, byte[] input, int inputOffset)
{
output.x0 = LoadLittleEndian32(input, inputOffset + 0);
output.x1 = LoadLittleEndian32(input, inputOffset + 4);
output.x2 = LoadLittleEndian32(input, inputOffset + 8);
output.x3 = LoadLittleEndian32(input, inputOffset + 12);
output.x4 = LoadLittleEndian32(input, inputOffset + 16);
output.x5 = LoadLittleEndian32(input, inputOffset + 20);
output.x6 = LoadLittleEndian32(input, inputOffset + 24);
output.x7 = LoadLittleEndian32(input, inputOffset + 28);
output.x8 = LoadLittleEndian32(input, inputOffset + 32);
output.x9 = LoadLittleEndian32(input, inputOffset + 36);
output.x10 = LoadLittleEndian32(input, inputOffset + 40);
output.x11 = LoadLittleEndian32(input, inputOffset + 44);
output.x12 = LoadLittleEndian32(input, inputOffset + 48);
output.x13 = LoadLittleEndian32(input, inputOffset + 52);
output.x14 = LoadLittleEndian32(input, inputOffset + 56);
output.x15 = LoadLittleEndian32(input, inputOffset + 60);
}
/*public static void Array16LoadLittleEndian32(out Array16<UInt32> output, byte[] input, int inputOffset, int inputLength)
{
Array8<UInt32> temp;
if (inputLength > 32)
{
output.x0 = LoadLittleEndian32(input, inputOffset + 0);
output.x1 = LoadLittleEndian32(input, inputOffset + 4);
output.x2 = LoadLittleEndian32(input, inputOffset + 8);
output.x3 = LoadLittleEndian32(input, inputOffset + 12);
output.x4 = LoadLittleEndian32(input, inputOffset + 16);
output.x5 = LoadLittleEndian32(input, inputOffset + 20);
output.x6 = LoadLittleEndian32(input, inputOffset + 24);
output.x7 = LoadLittleEndian32(input, inputOffset + 28);
Array8LoadLittleEndian32(out temp, input, inputOffset + 32, inputLength - 32);
output.x8 = temp.x0;
output.x9 = temp.x1;
output.x10 = temp.x2;
output.x11 = temp.x3;
output.x12 = temp.x4;
output.x13 = temp.x5;
output.x14 = temp.x6;
output.x15 = temp.x7;
}
else
{
Array8LoadLittleEndian32(out temp, input, inputOffset, inputLength);
output.x0 = temp.x0;
output.x1 = temp.x1;
output.x2 = temp.x2;
output.x3 = temp.x3;
output.x4 = temp.x4;
output.x5 = temp.x5;
output.x6 = temp.x6;
output.x7 = temp.x7;
output.x8 = 0;
output.x9 = 0;
output.x10 = 0;
output.x11 = 0;
output.x12 = 0;
output.x13 = 0;
output.x14 = 0;
output.x15 = 0;
}
}*/
public static void Array16StoreLittleEndian32(byte[] output, int outputOffset, Array16<Integer> input)
{
StoreLittleEndian32(output, outputOffset + 0, input.x0);
StoreLittleEndian32(output, outputOffset + 4, input.x1);
StoreLittleEndian32(output, outputOffset + 8, input.x2);
StoreLittleEndian32(output, outputOffset + 12, input.x3);
StoreLittleEndian32(output, outputOffset + 16, input.x4);
StoreLittleEndian32(output, outputOffset + 20, input.x5);
StoreLittleEndian32(output, outputOffset + 24, input.x6);
StoreLittleEndian32(output, outputOffset + 28, input.x7);
StoreLittleEndian32(output, outputOffset + 32, input.x8);
StoreLittleEndian32(output, outputOffset + 36, input.x9);
StoreLittleEndian32(output, outputOffset + 40, input.x10);
StoreLittleEndian32(output, outputOffset + 44, input.x11);
StoreLittleEndian32(output, outputOffset + 48, input.x12);
StoreLittleEndian32(output, outputOffset + 52, input.x13);
StoreLittleEndian32(output, outputOffset + 56, input.x14);
StoreLittleEndian32(output, outputOffset + 60, input.x15);
}
}
@@ -0,0 +1,53 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
public class FieldElement
{
public int x0;
public int x1;
public int x2;
public int x3;
public int x4;
public int x5;
public int x6;
public int x7;
public int x8;
public int x9;
// public static readonly FieldElement Zero = new FieldElement();
// public static readonly FieldElement One = new FieldElement() { x0 = 1 };
public void ReInit()
{
x0 = 0;
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
x5 = 0;
x6 = 0;
x7 = 0;
x8 = 0;
x9 = 0;
}
public FieldElement()
{
}
public FieldElement(int... elements)
{
// InternalAssert.Assert(elements.length == 10,
// "elements.Length != 10");
x0 = elements[0];
x1 = elements[1];
x2 = elements[2];
x3 = elements[3];
x4 = elements[4];
x5 = elements[5];
x6 = elements[6];
x7 = elements[7];
x8 = elements[8];
x9 = elements[9];
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,34 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
/*
ge means group element.
Here the group is the set of pairs (x,y) of field elements (see fe.h)
satisfying -x^2 + y^2 = 1 + d x^2y^2
where d = -121665/121666.
Representations:
ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
ge_precomp (Duif): (y+x,y-x,2dxy)
*/
public class GroupElementCached
{
public FieldElement YplusX;
public FieldElement YminusX;
public FieldElement Z;
public FieldElement T2d;
public GroupElementCached()
{
YplusX = new FieldElement();
YminusX = new FieldElement();
Z = new FieldElement();
T2d = new FieldElement();
}
} ;
@@ -0,0 +1,17 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
public class GroupElementP1P1
{
public FieldElement X;
public FieldElement Y;
public FieldElement Z;
public FieldElement T;
public GroupElementP1P1()
{
X = new FieldElement();
Y = new FieldElement();
Z = new FieldElement();
T = new FieldElement();
}
}
@@ -0,0 +1,15 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
public class GroupElementP2
{
public FieldElement X;
public FieldElement Y;
public FieldElement Z;
public GroupElementP2()
{
X = new FieldElement();
Y = new FieldElement();
Z = new FieldElement();
}
}
@@ -0,0 +1,25 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
public class GroupElementP3
{
public FieldElement X;
public FieldElement Y;
public FieldElement Z;
public FieldElement T;
public void ReInit()
{
X = new FieldElement();
Y = new FieldElement();
Z = new FieldElement();
T = new FieldElement();
}
public GroupElementP3()
{
X = new FieldElement();
Y = new FieldElement();
Z = new FieldElement();
T = new FieldElement();
}
}
@@ -0,0 +1,22 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
public class GroupElementPreComp
{
public FieldElement yplusx;
public FieldElement yminusx;
public FieldElement xy2d;
public GroupElementPreComp()
{
yplusx = new FieldElement();
yminusx = new FieldElement();
xy2d = new FieldElement();
}
public GroupElementPreComp(FieldElement yplusx, FieldElement yminusx, FieldElement xy2d)
{
this.yplusx = yplusx;
this.yminusx = yminusx;
this.xy2d = xy2d;
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,637 @@
package Punisher.NaCl.Internal.Ed25519Ref10;
public class ScalarOperations
{
static long load_3(byte[] input, int offset)
{
long result;
result = (long)(input[offset + 0]& 0xFF);
result |= ((long)input[offset + 1]& 0xFF) << 8;
result |= ((long)input[offset + 2]& 0xFF) << 16;
return result;
}
static long load_4(byte[] input, int offset)
{
long result;
result = (long)(input[offset + 0]& 0xFF);
result |= ((long)input[offset + 1]& 0xFF) << 8;
result |= ((long)input[offset + 2]& 0xFF) << 16;
result |= ((long)input[offset + 3]& 0xFF) << 24;
return result;
}
/*
Input:
s[0]+256*s[1]+...+256^63*s[63] = s
Output:
s[0]+256*s[1]+...+256^31*s[31] = s mod l
where l = 2^252 + 27742317777372353535851937790883648493.
Overwrites s in place.
*/
public static void sc_reduce(byte[] s)
{
long s0 = 2097151 & load_3(s, 0);
long s1 = 2097151 & (load_4(s, 2) >> 5);
long s2 = 2097151 & (load_3(s, 5) >> 2);
long s3 = 2097151 & (load_4(s, 7) >> 7);
long s4 = 2097151 & (load_4(s, 10) >> 4);
long s5 = 2097151 & (load_3(s, 13) >> 1);
long s6 = 2097151 & (load_4(s, 15) >> 6);
long s7 = 2097151 & (load_3(s, 18) >> 3);
long s8 = 2097151 & load_3(s, 21);
long s9 = 2097151 & (load_4(s, 23) >> 5);
long s10 = 2097151 & (load_3(s, 26) >> 2);
long s11 = 2097151 & (load_4(s, 28) >> 7);
long s12 = 2097151 & (load_4(s, 31) >> 4);
long s13 = 2097151 & (load_3(s, 34) >> 1);
long s14 = 2097151 & (load_4(s, 36) >> 6);
long s15 = 2097151 & (load_3(s, 39) >> 3);
long s16 = 2097151 & load_3(s, 42);
long s17 = 2097151 & (load_4(s, 44) >> 5);
long s18 = 2097151 & (load_3(s, 47) >> 2);
long s19 = 2097151 & (load_4(s, 49) >> 7);
long s20 = 2097151 & (load_4(s, 52) >> 4);
long s21 = 2097151 & (load_3(s, 55) >> 1);
long s22 = 2097151 & (load_4(s, 57) >> 6);
long s23 = (load_4(s, 60) >> 3);
long carry0;
long carry1;
long carry2;
long carry3;
long carry4;
long carry5;
long carry6;
long carry7;
long carry8;
long carry9;
long carry10;
long carry11;
long carry12;
long carry13;
long carry14;
long carry15;
long carry16;
s11 += s23 * 666643;
s12 += s23 * 470296;
s13 += s23 * 654183;
s14 -= s23 * 997805;
s15 += s23 * 136657;
s16 -= s23 * 683901;
s23 = 0;
s10 += s22 * 666643;
s11 += s22 * 470296;
s12 += s22 * 654183;
s13 -= s22 * 997805;
s14 += s22 * 136657;
s15 -= s22 * 683901;
s22 = 0;
s9 += s21 * 666643;
s10 += s21 * 470296;
s11 += s21 * 654183;
s12 -= s21 * 997805;
s13 += s21 * 136657;
s14 -= s21 * 683901;
s21 = 0;
s8 += s20 * 666643;
s9 += s20 * 470296;
s10 += s20 * 654183;
s11 -= s20 * 997805;
s12 += s20 * 136657;
s13 -= s20 * 683901;
s20 = 0;
s7 += s19 * 666643;
s8 += s19 * 470296;
s9 += s19 * 654183;
s10 -= s19 * 997805;
s11 += s19 * 136657;
s12 -= s19 * 683901;
s19 = 0;
s6 += s18 * 666643;
s7 += s18 * 470296;
s8 += s18 * 654183;
s9 -= s18 * 997805;
s10 += s18 * 136657;
s11 -= s18 * 683901;
s18 = 0;
carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
carry12 = (s12 + (1 << 20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
carry14 = (s14 + (1 << 20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
carry16 = (s16 + (1 << 20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
carry13 = (s13 + (1 << 20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
carry15 = (s15 + (1 << 20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
s5 += s17 * 666643;
s6 += s17 * 470296;
s7 += s17 * 654183;
s8 -= s17 * 997805;
s9 += s17 * 136657;
s10 -= s17 * 683901;
s17 = 0;
s4 += s16 * 666643;
s5 += s16 * 470296;
s6 += s16 * 654183;
s7 -= s16 * 997805;
s8 += s16 * 136657;
s9 -= s16 * 683901;
s16 = 0;
s3 += s15 * 666643;
s4 += s15 * 470296;
s5 += s15 * 654183;
s6 -= s15 * 997805;
s7 += s15 * 136657;
s8 -= s15 * 683901;
s15 = 0;
s2 += s14 * 666643;
s3 += s14 * 470296;
s4 += s14 * 654183;
s5 -= s14 * 997805;
s6 += s14 * 136657;
s7 -= s14 * 683901;
s14 = 0;
s1 += s13 * 666643;
s2 += s13 * 470296;
s3 += s13 * 654183;
s4 -= s13 * 997805;
s5 += s13 * 136657;
s6 -= s13 * 683901;
s13 = 0;
s0 += s12 * 666643;
s1 += s12 * 470296;
s2 += s12 * 654183;
s3 -= s12 * 997805;
s4 += s12 * 136657;
s5 -= s12 * 683901;
s12 = 0;
carry0 = (s0 + (1 << 20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
carry2 = (s2 + (1 << 20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
carry4 = (s4 + (1 << 20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
carry1 = (s1 + (1 << 20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
carry3 = (s3 + (1 << 20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
carry5 = (s5 + (1 << 20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
s0 += s12 * 666643;
s1 += s12 * 470296;
s2 += s12 * 654183;
s3 -= s12 * 997805;
s4 += s12 * 136657;
s5 -= s12 * 683901;
s12 = 0;
carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
s0 += s12 * 666643;
s1 += s12 * 470296;
s2 += s12 * 654183;
s3 -= s12 * 997805;
s4 += s12 * 136657;
s5 -= s12 * 683901;
s12 = 0;
carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
{
s[0] = (byte)(s0 >> 0);
s[1] = (byte)(s0 >> 8);
s[2] = (byte)((s0 >> 16) | (s1 << 5));
s[3] = (byte)(s1 >> 3);
s[4] = (byte)(s1 >> 11);
s[5] = (byte)((s1 >> 19) | (s2 << 2));
s[6] = (byte)(s2 >> 6);
s[7] = (byte)((s2 >> 14) | (s3 << 7));
s[8] = (byte)(s3 >> 1);
s[9] = (byte)(s3 >> 9);
s[10] = (byte)((s3 >> 17) | (s4 << 4));
s[11] = (byte)(s4 >> 4);
s[12] = (byte)(s4 >> 12);
s[13] = (byte)((s4 >> 20) | (s5 << 1));
s[14] = (byte)(s5 >> 7);
s[15] = (byte)((s5 >> 15) | (s6 << 6));
s[16] = (byte)(s6 >> 2);
s[17] = (byte)(s6 >> 10);
s[18] = (byte)((s6 >> 18) | (s7 << 3));
s[19] = (byte)(s7 >> 5);
s[20] = (byte)(s7 >> 13);
s[21] = (byte)(s8 >> 0);
s[22] = (byte)(s8 >> 8);
s[23] = (byte)((s8 >> 16) | (s9 << 5));
s[24] = (byte)(s9 >> 3);
s[25] = (byte)(s9 >> 11);
s[26] = (byte)((s9 >> 19) | (s10 << 2));
s[27] = (byte)(s10 >> 6);
s[28] = (byte)((s10 >> 14) | (s11 << 7));
s[29] = (byte)(s11 >> 1);
s[30] = (byte)(s11 >> 9);
s[31] = (byte)(s11 >> 17);
}
}
public static void sc_clamp(byte[] s, int offset)
{
s[offset + 0] &= 248;
s[offset + 31] &= 127;
s[offset + 31] |= 64;
}
/*
Input:
a[0]+256*a[1]+...+256^31*a[31] = a
b[0]+256*b[1]+...+256^31*b[31] = b
c[0]+256*c[1]+...+256^31*c[31] = c
Output:
s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
where l = 2^252 + 27742317777372353535851937790883648493.
*/
public static void sc_muladd(byte[] s, byte[] a, byte[] b, byte[] c)
{
long a0 = 2097151 & load_3(a, 0);
long a1 = 2097151 & (load_4(a, 2) >> 5);
long a2 = 2097151 & (load_3(a, 5) >> 2);
long a3 = 2097151 & (load_4(a, 7) >> 7);
long a4 = 2097151 & (load_4(a, 10) >> 4);
long a5 = 2097151 & (load_3(a, 13) >> 1);
long a6 = 2097151 & (load_4(a, 15) >> 6);
long a7 = 2097151 & (load_3(a, 18) >> 3);
long a8 = 2097151 & load_3(a, 21);
long a9 = 2097151 & (load_4(a, 23) >> 5);
long a10 = 2097151 & (load_3(a, 26) >> 2);
long a11 = (load_4(a, 28) >> 7);
long b0 = 2097151 & load_3(b, 0);
long b1 = 2097151 & (load_4(b, 2) >> 5);
long b2 = 2097151 & (load_3(b, 5) >> 2);
long b3 = 2097151 & (load_4(b, 7) >> 7);
long b4 = 2097151 & (load_4(b, 10) >> 4);
long b5 = 2097151 & (load_3(b, 13) >> 1);
long b6 = 2097151 & (load_4(b, 15) >> 6);
long b7 = 2097151 & (load_3(b, 18) >> 3);
long b8 = 2097151 & load_3(b, 21);
long b9 = 2097151 & (load_4(b, 23) >> 5);
long b10 = 2097151 & (load_3(b, 26) >> 2);
long b11 = (load_4(b, 28) >> 7);
long c0 = 2097151 & load_3(c, 0);
long c1 = 2097151 & (load_4(c, 2) >> 5);
long c2 = 2097151 & (load_3(c, 5) >> 2);
long c3 = 2097151 & (load_4(c, 7) >> 7);
long c4 = 2097151 & (load_4(c, 10) >> 4);
long c5 = 2097151 & (load_3(c, 13) >> 1);
long c6 = 2097151 & (load_4(c, 15) >> 6);
long c7 = 2097151 & (load_3(c, 18) >> 3);
long c8 = 2097151 & load_3(c, 21);
long c9 = 2097151 & (load_4(c, 23) >> 5);
long c10 = 2097151 & (load_3(c, 26) >> 2);
long c11 = (load_4(c, 28) >> 7);
long s0;
long s1;
long s2;
long s3;
long s4;
long s5;
long s6;
long s7;
long s8;
long s9;
long s10;
long s11;
long s12;
long s13;
long s14;
long s15;
long s16;
long s17;
long s18;
long s19;
long s20;
long s21;
long s22;
long s23;
long carry0;
long carry1;
long carry2;
long carry3;
long carry4;
long carry5;
long carry6;
long carry7;
long carry8;
long carry9;
long carry10;
long carry11;
long carry12;
long carry13;
long carry14;
long carry15;
long carry16;
long carry17;
long carry18;
long carry19;
long carry20;
long carry21;
long carry22;
s0 = c0 + a0 * b0;
s1 = c1 + a0 * b1 + a1 * b0;
s2 = c2 + a0 * b2 + a1 * b1 + a2 * b0;
s3 = c3 + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0;
s4 = c4 + a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0;
s5 = c5 + a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0;
s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0;
s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1 + a7 * b0;
s8 = c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2 + a7 * b1 + a8 * b0;
s9 = c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0;
s10 = c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0;
s11 = c11 + a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0;
s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1;
s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2;
s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 + a10 * b4 + a11 * b3;
s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 + a11 * b4;
s16 = a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5;
s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6;
s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7;
s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8;
s20 = a9 * b11 + a10 * b10 + a11 * b9;
s21 = a10 * b11 + a11 * b10;
s22 = a11 * b11;
s23 = 0;
carry0 = (s0 + (1 << 20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
carry2 = (s2 + (1 << 20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
carry4 = (s4 + (1 << 20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
carry12 = (s12 + (1 << 20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
carry14 = (s14 + (1 << 20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
carry16 = (s16 + (1 << 20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
carry18 = (s18 + (1 << 20)) >> 21; s19 += carry18; s18 -= carry18 << 21;
carry20 = (s20 + (1 << 20)) >> 21; s21 += carry20; s20 -= carry20 << 21;
carry22 = (s22 + (1 << 20)) >> 21; s23 += carry22; s22 -= carry22 << 21;
carry1 = (s1 + (1 << 20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
carry3 = (s3 + (1 << 20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
carry5 = (s5 + (1 << 20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
carry13 = (s13 + (1 << 20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
carry15 = (s15 + (1 << 20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
carry17 = (s17 + (1 << 20)) >> 21; s18 += carry17; s17 -= carry17 << 21;
carry19 = (s19 + (1 << 20)) >> 21; s20 += carry19; s19 -= carry19 << 21;
carry21 = (s21 + (1 << 20)) >> 21; s22 += carry21; s21 -= carry21 << 21;
s11 += s23 * 666643;
s12 += s23 * 470296;
s13 += s23 * 654183;
s14 -= s23 * 997805;
s15 += s23 * 136657;
s16 -= s23 * 683901;
s23 = 0;
s10 += s22 * 666643;
s11 += s22 * 470296;
s12 += s22 * 654183;
s13 -= s22 * 997805;
s14 += s22 * 136657;
s15 -= s22 * 683901;
s22 = 0;
s9 += s21 * 666643;
s10 += s21 * 470296;
s11 += s21 * 654183;
s12 -= s21 * 997805;
s13 += s21 * 136657;
s14 -= s21 * 683901;
s21 = 0;
s8 += s20 * 666643;
s9 += s20 * 470296;
s10 += s20 * 654183;
s11 -= s20 * 997805;
s12 += s20 * 136657;
s13 -= s20 * 683901;
s20 = 0;
s7 += s19 * 666643;
s8 += s19 * 470296;
s9 += s19 * 654183;
s10 -= s19 * 997805;
s11 += s19 * 136657;
s12 -= s19 * 683901;
s19 = 0;
s6 += s18 * 666643;
s7 += s18 * 470296;
s8 += s18 * 654183;
s9 -= s18 * 997805;
s10 += s18 * 136657;
s11 -= s18 * 683901;
s18 = 0;
carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
carry12 = (s12 + (1 << 20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
carry14 = (s14 + (1 << 20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
carry16 = (s16 + (1 << 20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
carry13 = (s13 + (1 << 20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
carry15 = (s15 + (1 << 20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
s5 += s17 * 666643;
s6 += s17 * 470296;
s7 += s17 * 654183;
s8 -= s17 * 997805;
s9 += s17 * 136657;
s10 -= s17 * 683901;
s17 = 0;
s4 += s16 * 666643;
s5 += s16 * 470296;
s6 += s16 * 654183;
s7 -= s16 * 997805;
s8 += s16 * 136657;
s9 -= s16 * 683901;
s16 = 0;
s3 += s15 * 666643;
s4 += s15 * 470296;
s5 += s15 * 654183;
s6 -= s15 * 997805;
s7 += s15 * 136657;
s8 -= s15 * 683901;
s15 = 0;
s2 += s14 * 666643;
s3 += s14 * 470296;
s4 += s14 * 654183;
s5 -= s14 * 997805;
s6 += s14 * 136657;
s7 -= s14 * 683901;
s14 = 0;
s1 += s13 * 666643;
s2 += s13 * 470296;
s3 += s13 * 654183;
s4 -= s13 * 997805;
s5 += s13 * 136657;
s6 -= s13 * 683901;
s13 = 0;
s0 += s12 * 666643;
s1 += s12 * 470296;
s2 += s12 * 654183;
s3 -= s12 * 997805;
s4 += s12 * 136657;
s5 -= s12 * 683901;
s12 = 0;
carry0 = (s0 + (1 << 20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
carry2 = (s2 + (1 << 20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
carry4 = (s4 + (1 << 20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
carry1 = (s1 + (1 << 20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
carry3 = (s3 + (1 << 20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
carry5 = (s5 + (1 << 20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
s0 += s12 * 666643;
s1 += s12 * 470296;
s2 += s12 * 654183;
s3 -= s12 * 997805;
s4 += s12 * 136657;
s5 -= s12 * 683901;
s12 = 0;
carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
s0 += s12 * 666643;
s1 += s12 * 470296;
s2 += s12 * 654183;
s3 -= s12 * 997805;
s4 += s12 * 136657;
s5 -= s12 * 683901;
s12 = 0;
carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
{
s[0] = (byte)(s0 >> 0);
s[1] = (byte)(s0 >> 8);
s[2] = (byte)((s0 >> 16) | (s1 << 5));
s[3] = (byte)(s1 >> 3);
s[4] = (byte)(s1 >> 11);
s[5] = (byte)((s1 >> 19) | (s2 << 2));
s[6] = (byte)(s2 >> 6);
s[7] = (byte)((s2 >> 14) | (s3 << 7));
s[8] = (byte)(s3 >> 1);
s[9] = (byte)(s3 >> 9);
s[10] = (byte)((s3 >> 17) | (s4 << 4));
s[11] = (byte)(s4 >> 4);
s[12] = (byte)(s4 >> 12);
s[13] = (byte)((s4 >> 20) | (s5 << 1));
s[14] = (byte)(s5 >> 7);
s[15] = (byte)((s5 >> 15) | (s6 << 6));
s[16] = (byte)(s6 >> 2);
s[17] = (byte)(s6 >> 10);
s[18] = (byte)((s6 >> 18) | (s7 << 3));
s[19] = (byte)(s7 >> 5);
s[20] = (byte)(s7 >> 13);
s[21] = (byte)(s8 >> 0);
s[22] = (byte)(s8 >> 8);
s[23] = (byte)((s8 >> 16) | (s9 << 5));
s[24] = (byte)(s9 >> 3);
s[25] = (byte)(s9 >> 11);
s[26] = (byte)((s9 >> 19) | (s10 << 2));
s[27] = (byte)(s10 >> 6);
s[28] = (byte)((s10 >> 14) | (s11 << 7));
s[29] = (byte)(s11 >> 1);
s[30] = (byte)(s11 >> 9);
s[31] = (byte)(s11 >> 17);
}
}
}
@@ -0,0 +1,12 @@
package Punisher.NaCl.Internal;
public class InternalAssert
{
public static void Assert(Boolean condition, String string) throws Exception
{
if (!condition)
throw new Exception("An assertion in Punisher.NaCl failed " + string);
}
}
@@ -0,0 +1,122 @@
package Punisher.NaCl.Internal;
import Punisher.NaCl.CryptoBytes;
public class Sha512
{
private Array8<Long> _state = new Array8<Long>() ;
private byte[] _buffer;
private long _totalBytes;
public int BlockSize = 128;
private static byte[] _padding = new byte[] { (byte)0x80 };
public Sha512()
{
_buffer = new byte[BlockSize];
Init();
}
public void Init()
{
Sha512Internal.Sha512Init(_state);
_totalBytes = 0;
}
public void Update(byte[] data, int offset, int count) throws Exception
{
if (data == null)
throw new Exception("data");
if (offset < 0)
throw new Exception("offset");
if (count < 0)
throw new Exception("count");
if (data.length - offset < count)
throw new Exception("Requires offset + count <= data.Length");
Array16<Long> block = new Array16<Long>();
int bytesInBuffer = (int)_totalBytes & (BlockSize - 1);
_totalBytes += (int)count;
if (_totalBytes >= Long.MAX_VALUE / 8)
throw new Exception("Too much data");
// Fill existing buffer
if (bytesInBuffer != 0)
{
int toCopy = Math.min(BlockSize - bytesInBuffer, count);
System.arraycopy(data, offset, _buffer, bytesInBuffer, toCopy);
offset += toCopy;
count -= toCopy;
bytesInBuffer += toCopy;
if (bytesInBuffer == BlockSize)
{
ByteIntegerConverter.Array16LoadBigEndian64(block, _buffer, 0);
Sha512Internal.Core(_state, _state, block);
CryptoBytes.Wipe(_buffer);
bytesInBuffer = 0;
}
}
// Hash complete blocks without copying
while (count >= BlockSize)
{
ByteIntegerConverter.Array16LoadBigEndian64(block, data, offset);
Sha512Internal.Core(_state, _state, block);
offset += BlockSize;
count -= BlockSize;
}
// Copy remainder into buffer
if (count > 0)
{
System.arraycopy(data, offset, _buffer, bytesInBuffer, count);
}
}
public void Finish(byte [] output) throws Exception
{
if (output == null)
throw new Exception("output.Array");
if (output.length != 64)
throw new Exception("output.Count must be 64");
Update(_padding, 0, _padding.length);
Array16<Long> block = new Array16<Long>();
ByteIntegerConverter.Array16LoadBigEndian64(block, _buffer, 0);
CryptoBytes.Wipe(_buffer);
int bytesInBuffer = (int)_totalBytes & (BlockSize - 1);
if (bytesInBuffer > BlockSize - 16)
{
Sha512Internal.Core(_state, _state, block);
block = new Array16<Long>();
}
block.x15 = (_totalBytes - 1) * 8;
Sha512Internal.Core(_state, _state, block);
ByteIntegerConverter.StoreBigEndian64(output, 0, _state.x0);
ByteIntegerConverter.StoreBigEndian64(output, 8, _state.x1);
ByteIntegerConverter.StoreBigEndian64(output, 16, _state.x2);
ByteIntegerConverter.StoreBigEndian64(output, 24, _state.x3);
ByteIntegerConverter.StoreBigEndian64(output, 32, _state.x4);
ByteIntegerConverter.StoreBigEndian64(output, 40, _state.x5);
ByteIntegerConverter.StoreBigEndian64(output, 48, _state.x6);
ByteIntegerConverter.StoreBigEndian64(output, 56, _state.x7);
_state = new Array8<Long>();
}
public byte[] Finish() throws Exception
{
byte[] result = new byte[64];
Finish(result);
return result;
}
public static byte[] Hash(byte[] data) throws Exception
{
return Hash(data, 0, data.length);
}
public static byte[] Hash(byte[] data, int offset, int count) throws Exception
{
Sha512 hasher = new Sha512();
hasher.Update(data, offset, count);
return hasher.Finish();
}
}
@@ -0,0 +1,380 @@
package Punisher.NaCl.Internal;
public class Sha512Internal
{
private static final long[] K = new long[]{0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL,
0xab1c5ed5da6d8118L, 0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L, 0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L,
0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L,
0x76f988da831153b5L, 0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL,
0x142929670a0e6e70L, 0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL, 0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L,
0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL,
0x106aa07032bbd1b8L, 0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L,
0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL, 0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L,
0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL,
0x1b710b35131c471bL, 0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L};
public static void Sha512Init(Array8<Long> state)
{
state.x0 = 0x6a09e667f3bcc908L;
state.x1 = 0xbb67ae8584caa73bL;
state.x2 = 0x3c6ef372fe94f82bL;
state.x3 = 0xa54ff53a5f1d36f1L;
state.x4 = 0x510e527fade682d1L;
state.x5 = 0x9b05688c2b3e6c1fL;
state.x6 = 0x1f83d9abfb41bd6bL;
state.x7 = 0x5be0cd19137e2179L;
}
public static void Core(Array8<Long> outputState, Array8<Long> inputState, Array16<Long> input)
{
Long a = inputState.x0;
Long b = inputState.x1;
Long c = inputState.x2;
Long d = inputState.x3;
Long e = inputState.x4;
Long f = inputState.x5;
Long g = inputState.x6;
Long h = inputState.x7;
Long w0 = input.x0;
Long w1 = input.x1;
Long w2 = input.x2;
Long w3 = input.x3;
Long w4 = input.x4;
Long w5 = input.x5;
Long w6 = input.x6;
Long w7 = input.x7;
Long w8 = input.x8;
Long w9 = input.x9;
Long w10 = input.x10;
Long w11 = input.x11;
Long w12 = input.x12;
Long w13 = input.x13;
Long w14 = input.x14;
Long w15 = input.x15;
int t = 0;
while (true)
{
Long 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;
}
}
@@ -0,0 +1,84 @@
package Punisher.NaCl;
import java.security.SecureRandom;
public class Test_Ed25519
{
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes)
{
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++)
{
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void main(String[] args) throws Exception
{
System.out.println("Test_Ed25519");
SecureRandom sr = new SecureRandom();
int trueC = 0;
for (int i = 0; i < 500; i++)
{
// byte[] Seed = new byte[]{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0,
// 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3};
byte[] Seed = new byte[]{0x34, (byte) 0x9E, (byte) 0x74, (byte) 0x52, (byte) 0xFA, (byte) 0x01, (byte) 0x8A, (byte) 0xD0, (byte) 0xE1, (byte) 0xE7, (byte) 0xA6, (byte) 0x3A, (byte) 0x77,
(byte) 0xF9, (byte) 0xE7, (byte) 0x78, (byte) 0x29, (byte) 0x30, (byte) 0x48, (byte) 0xE2, (byte) 0x93, (byte) 0xDE, (byte) 0x58, (byte) 0x3C, (byte) 0xC6, (byte) 0x84,
(byte) 0xFF, (byte) 0x56, (byte) 0xC7, (byte) 0x4D, (byte) 0xF8, (byte) 0xA4};
sr.nextBytes(Seed);
//byte posNext = (byte)(sr.nextInt() & 0x1F);
Boolean doCheck = sr.nextBoolean();
byte[] pk = Ed25519.PublicKeyFromSeed(Seed);
byte[] sk = Ed25519.ExpandedPrivateKeyFromSeed(Seed);
byte[] sig = Ed25519.Sign(new byte[32], sk);
byte[] sss = new byte[32];
if(doCheck)
{
/*byte oldVal = sss[posNext];
byte newVal = 0;
do
{
newVal = (byte)(sr.nextInt() & 0xFF);
}
while(oldVal == newVal);
sss[posNext] = newVal; */
sr.nextBytes(sss);
}
else
{
}
Boolean bb = Ed25519.Verify(sig, sss, pk);
// System.out.println("TEST: " + i);
// System.out.println("bb : " + bb);
if (!doCheck || (doCheck && !bb))
{
trueC++;
// System.out.println("PK : " + bytesToHex(pk));
// System.out.println("SK : " + bytesToHex(sk));
// System.out.println("sig : " + bytesToHex(sig));
}
}
System.out.println("True Count: " + trueC);
}
}
@@ -80,7 +80,6 @@ public class SimpleCommand implements Command {
} else { } else {
String[] commandParamParts = commandParamText.split("=", 2); String[] commandParamParts = commandParamText.split("=", 2);
Ts3Logging.debug(commandParamText);
parameterList.add(new CommandSingleParameter( parameterList.add(new CommandSingleParameter(
commandParamParts[0].toLowerCase(), commandParamParts[0].toLowerCase(),
commandParamParts.length > 1 ? commandParamParts.length > 1 ?
@@ -1,5 +1,6 @@
package com.github.manevolent.ts3j.identity; package com.github.manevolent.ts3j.identity;
import com.github.manevolent.ts3j.util.Ts3Crypt;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator; import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECDomainParameters;
@@ -56,4 +57,8 @@ public class LocalIdentity extends Identity {
return localIdentity; return localIdentity;
} }
public byte[] sign(byte[] data) {
return Ts3Crypt.createSignature(privateKey, data);
}
} }
@@ -1,7 +1,10 @@
package com.github.manevolent.ts3j.license; package com.github.manevolent.ts3j.license;
import Punisher.NaCl.Internal.Ed25519Ref10.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.DigestException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -67,7 +70,7 @@ public class License {
} }
public int getSize() { public int getSize() {
return 32 + 1 + 8 + use.getSize(); return 1 + 32 + 1 + 8 + use.getSize();
} }
public ByteBuffer write(ByteBuffer buffer) { public ByteBuffer write(ByteBuffer buffer) {
@@ -168,15 +171,60 @@ public class License {
write(buffer); write(buffer);
MessageDigest digest; MessageDigest digest;
try { try {
digest = MessageDigest.getInstance("SHA512"); digest = MessageDigest.getInstance("SHA512");
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return digest.digest(buffer.array()); digest.update(buffer.array(), 1, buffer.array().length - 1);
return digest.digest();
} }
return computedHash; return computedHash;
} }
public byte[] deriveKey(byte[] parent) {
// ScalarOperations.sc_clamp(Hash);
ScalarOperations.sc_clamp(getHash(), 0);
// GroupOperations.ge_frombytes_negate_vartime(out var pubkey, Key);
GroupElementP3 pubkey = new GroupElementP3();
GroupOperations.ge_frombytes_negate_vartime(pubkey, getPublicKey(), 0);
// GroupOperations.ge_frombytes_negate_vartime(out var parkey, parent);
GroupElementP3 parkey = new GroupElementP3();
GroupOperations.ge_frombytes_negate_vartime(parkey, parent, 0);
GroupElementP1P1 res = new GroupElementP1P1();
GroupOperations.ge_scalarmult_vartime(res, getHash(), pubkey);
GroupElementCached pargrp = new GroupElementCached();
GroupOperations.ge_p3_to_cached(pargrp, parkey);
GroupElementP3 r = new GroupElementP3();
GroupOperations.ge_p1p1_to_p3(r, res);
GroupElementP1P1 a = new GroupElementP1P1() ;
GroupOperations.ge_add(a, r, pargrp);
GroupElementP3 r2 = new GroupElementP3();
GroupOperations.ge_p1p1_to_p3(r2, a);
byte[] final_ = new byte[32];
GroupOperations.ge_p3_tobytes(final_, 0, r2);
final_[1] ^= 0x80;
return final_;
}
public static byte[] deriveKey(List<License> blocks) {
byte[] round = ROOT_KEY;
for (License block : blocks) round = block.deriveKey(round);
return round;
}
} }
@@ -1,6 +1,7 @@
package com.github.manevolent.ts3j.license; package com.github.manevolent.ts3j.license;
import com.github.manevolent.ts3j.enums.LicenseType; import com.github.manevolent.ts3j.enums.LicenseType;
import com.github.manevolent.ts3j.util.Ts3Logging;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@@ -27,6 +28,7 @@ public class ServerLicenseUse extends LicenseUse {
public ByteBuffer write(ByteBuffer buffer) { public ByteBuffer write(ByteBuffer buffer) {
buffer.put(type); buffer.put(type);
buffer.put(unknown); buffer.put(unknown);
License.writeNullTerminatedLicenseString(buffer, issuer); License.writeNullTerminatedLicenseString(buffer, issuer);
return buffer; return buffer;
@@ -36,6 +38,7 @@ public class ServerLicenseUse extends LicenseUse {
public ByteBuffer read(ByteBuffer buffer) { public ByteBuffer read(ByteBuffer buffer) {
type = buffer.get(); type = buffer.get();
buffer.get(unknown); buffer.get(unknown);
issuer = License.readNullTerminatedLicenseString(buffer); issuer = License.readNullTerminatedLicenseString(buffer);
return buffer; return buffer;
@@ -10,6 +10,7 @@ import com.github.manevolent.ts3j.protocol.packet.Packet2Command;
import com.github.manevolent.ts3j.protocol.packet.Packet8Init1; import com.github.manevolent.ts3j.protocol.packet.Packet8Init1;
import com.github.manevolent.ts3j.util.Ts3Crypt; import com.github.manevolent.ts3j.util.Ts3Crypt;
import com.github.manevolent.ts3j.util.Ts3Logging; import com.github.manevolent.ts3j.util.Ts3Logging;
import javafx.util.Pair;
import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.ECPoint;
import java.io.IOException; import java.io.IOException;
@@ -165,21 +166,45 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
null : null :
Base64.getDecoder().decode(command.get("tvd").getValue()); Base64.getDecoder().decode(command.get("tvd").getValue());
byte[] random = Base64.getDecoder().decode(command.get("beta").getValue()); // beta byte[] beta = Base64.getDecoder().decode(command.get("beta").getValue()); // beta
byte[] publicKeyBytes = Base64.getDecoder().decode(command.get("omega").getValue()); // omega byte[] omega = Base64.getDecoder().decode(command.get("omega").getValue()); // omega
byte[] proof = Base64.getDecoder().decode(command.get("proof").getValue()); // ecdh_sign(1) byte[] proof = Base64.getDecoder().decode(command.get("proof").getValue()); // ecdh_sign(1)
Pair<byte[], byte[]> keyPair = Ts3Crypt.generateKeypair();
// 3.2.1.1 Verify integrity // 3.2.1.1 Verify integrity
// The proof parameter is the sign of the l parameter (not base64 encoded). The client can verify the l // The proof parameter is the sign of the l parameter (not base64 encoded). The client can verify the l
// parameter with the public key of the server which is sent in omega. // parameter with the public key of the server which is sent in omega.
ECPoint publicKey = Ts3Crypt.decodePublicKey(publicKeyBytes); ECPoint publicKey = Ts3Crypt.decodePublicKey(omega);
if (!Ts3Crypt.verifySignature(publicKey, license, proof)) if (!Ts3Crypt.verifySignature(publicKey, license, proof))
throw new SecurityException("invalid proof signature: " + Ts3Logging.getHex(proof)); throw new SecurityException("invalid proof signature: " + Ts3Logging.getHex(proof));
// 3.2.2.2 Parsing the license // generate proof for clientek
List<License> licenses = License.readLicenses(ByteBuffer.wrap(license)); byte[] toSign = new byte[86];
System.arraycopy(keyPair.getKey(), 0, toSign, 0, 32);
System.arraycopy(beta, 0, toSign, 32, 54);
byte[] sign = getClient().getLocalIdentity().sign(toSign);
Ts3Logging.debug("Created proof (clientek): " + Ts3Logging.getHex(sign));
// Send clientek (telling them the shared secret)
Packet2Command clientek = new Packet2Command(ProtocolRole.CLIENT);
clientek.setText(
new SimpleCommand(
"clientek", ProtocolRole.CLIENT,
new CommandSingleParameter("ek", Base64.getEncoder().encodeToString(keyPair.getKey())),
new CommandSingleParameter("proof", Base64.getEncoder().encodeToString(sign))
).build()
);
getClient().send(clientek);
Ts3Crypt.cryptoInit2(license, alphaBytes, omega, proof, beta, keyPair.getValue());
// send client init...
// DONE!
} }
} }
@@ -1,9 +1,16 @@
package com.github.manevolent.ts3j.util; package com.github.manevolent.ts3j.util;
import Punisher.NaCl.Internal.Ed25519Ref10.GroupElementP2;
import Punisher.NaCl.Internal.Ed25519Ref10.GroupElementP3;
import Punisher.NaCl.Internal.Ed25519Ref10.GroupOperations;
import Punisher.NaCl.Internal.Ed25519Ref10.ScalarOperations;
import Punisher.NaCl.Internal.Sha512;
import com.github.manevolent.ts3j.license.License;
import javafx.util.Pair;
import org.bouncycastle.asn1.*; import org.bouncycastle.asn1.*;
import org.bouncycastle.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.signers.DSADigestSigner; import org.bouncycastle.crypto.signers.DSADigestSigner;
import org.bouncycastle.crypto.signers.ECDSASigner; import org.bouncycastle.crypto.signers.ECDSASigner;
@@ -14,12 +21,92 @@ import org.bouncycastle.math.ec.ECPoint;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; import java.util.List;
public final class Ts3Crypt { public final class Ts3Crypt {
public static Ts3Crypt.SecureChannelParameters cryptoInit2(byte[] license, byte[] alpha,
byte[] omega, byte[] proof,
byte[] beta, byte[] privateKey) {
// 3.2.2.2 Parsing the license
List<License> licenses = License.readLicenses(ByteBuffer.wrap(license));
byte[] key = License.deriveKey(licenses);
Ts3Logging.debug("Computed license key: " + Ts3Logging.getHex(key));
byte[] sharedSecret = Ts3Crypt.generateSharedSecret2(key, privateKey);
Ts3Logging.debug("Computed shared secret: " + Ts3Logging.getHex(sharedSecret));
Ts3Crypt.SecureChannelParameters parameters =
Ts3Crypt.getSecureParameters(alpha, beta, sharedSecret);
Ts3Logging.debug("Computed fake fingerprint: " + Ts3Logging.getHex(parameters.getFakeSignature()));
Ts3Logging.debug("Computed IV struct: " + Ts3Logging.getHex(parameters.getIvStruct()));
return parameters;
}
private static void xor(byte[] a, int aoffs, byte[] b, int boffs, int len, byte[] outBuf, int outOffs)
{
if (a.length < len || b.length < len || outBuf.length < len)
throw new ArrayIndexOutOfBoundsException();
for (int i = 0; i < len; i++)
outBuf[i + outOffs] = (byte)(a[i + aoffs] ^ b[i + boffs]);
}
public static SecureChannelParameters getSecureParameters(byte[] alpha, byte[] beta, byte[] sharedKey) {
if (beta.length != 10 && beta.length != 54)
throw new IllegalArgumentException("invalid beta size (" + beta.length + ")");
byte[] fakeSignature = new byte[8];
byte[] ivStruct = new byte[10 + beta.length];
xor(sharedKey, 0, alpha, 0, alpha.length, ivStruct, 0);
xor(sharedKey, 10, beta, 0, beta.length, ivStruct, 10);
byte[] buffer;
try {
buffer = MessageDigest.getInstance("SHA1").digest(ivStruct);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
System.arraycopy(buffer, 0, fakeSignature, 0, 8);
return new SecureChannelParameters(fakeSignature, ivStruct);
}
public static byte[] generateSharedSecret2(byte[] publicKey, byte[] privateKey) {
byte[] privateKeyCopy = new byte[32];
System.arraycopy(privateKey, 0, privateKeyCopy, 0, 32);
privateKeyCopy[31] &= 0x7F;
GroupElementP3 pub1 = new GroupElementP3();
GroupOperations.ge_frombytes_negate_vartime(pub1, publicKey, 0);
GroupElementP2 mul = new GroupElementP2();
GroupOperations.ge_scalarmult_vartime(mul, privateKeyCopy, pub1);
byte[] sharedTmp = new byte[32];
GroupOperations.ge_tobytes(sharedTmp, 0, mul);
sharedTmp[31] ^= 0x80;
try {
return Sha512.Hash(sharedTmp);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static ECDomainParameters getDomainParameters() { private static ECDomainParameters getDomainParameters() {
ECNamedCurveParameterSpec ecp = ECNamedCurveTable.getParameterSpec("prime256v1"); ECNamedCurveParameterSpec ecp = ECNamedCurveTable.getParameterSpec("prime256v1");
return new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); return new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed());
@@ -35,6 +122,16 @@ public final class Ts3Crypt {
return signer.verifySignature(signature); return signer.verifySignature(signature);
} }
public static byte[] createSignature(BigInteger privateKey, byte[] data) {
DSADigestSigner signer = new DSADigestSigner(new ECDSASigner(), new SHA256Digest());
ECPrivateKeyParameters signingKey = new ECPrivateKeyParameters(privateKey, getDomainParameters());
signer.init(true, signingKey);
signer.update(data, 0, data.length);
return signer.generateSignature();
}
/** /**
* From http://stackoverflow.com/questions/2409618/how-do-i-decode-a-der-encoded-string-in-java * From http://stackoverflow.com/questions/2409618/how-do-i-decode-a-der-encoded-string-in-java
*/ */
@@ -79,4 +176,37 @@ public final class Ts3Crypt {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static Pair<byte[], byte[]> generateKeypair() {
byte[] privateKey = new byte[32];
new SecureRandom().nextBytes(privateKey);
ScalarOperations.sc_clamp(privateKey, 0);
GroupElementP3 A = new GroupElementP3();
GroupOperations.ge_scalarmult_base(A, privateKey, 0);
byte[] publicKey = new byte[32];
GroupOperations.ge_p3_tobytes(publicKey, 0, A);
return new Pair<>(publicKey, privateKey);
}
public static final class SecureChannelParameters {
private final byte[] fakeSignature;
private final byte[] ivStruct;
public SecureChannelParameters(byte[] fakeSignature, byte[] ivStruct) {
this.fakeSignature = fakeSignature;
this.ivStruct = ivStruct;
}
public byte[] getFakeSignature() {
return fakeSignature;
}
public byte[] getIvStruct() {
return ivStruct;
}
}
} }