Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/com/pusher/client/crypto/nacl/Poly1305.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private static double longBitsToDouble(long bits) {
return (double) s * (double) m * Math.pow(2, e - 1075);
}

public static boolean verify(byte mac[], byte m[], byte key[]) {
byte tmp[] = sum(m, key);
public static boolean verify(byte[] mac, byte[] m, byte[] key) {
byte[] tmp = sum(m, key);
//Util.printHex("tmp", tmp);
//Util.printHex("mac", mac);
return Subtle.constantTimeCompare(tmp, mac);
Expand All @@ -67,9 +67,9 @@ public static boolean verify(byte mac[], byte m[], byte key[]) {
// Sum generates an authenticator for m using a one-time key and puts the
// 16-byte result into out. Authenticating two different messages with the same
// key allows an attacker to forge messages at will.
public static byte[] sum(byte m[], byte key[]) {
byte r[] = key.clone();
byte s[] = new byte[16];
public static byte[] sum(byte[] m, byte[] key) {
byte[] r = key.clone();
byte[] s = new byte[16];
for (int i = 0; i < s.length; i++) {
s[i] = key[i + 16];
}
Expand Down Expand Up @@ -1516,7 +1516,7 @@ public static byte[] sum(byte m[], byte key[]) {
f2 += (s23);
f3 += s33;

byte out[] = new byte[16];
byte[] out = new byte[16];
out[0] = (byte) (f0);
f0 >>= 8;
out[1] = (byte) (f0);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/pusher/client/crypto/nacl/Salsa.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private static long mask(byte x) {

// core applies the Salsa20 core function to 16-byte input in, 32-byte key k,
// and 16-byte constant c, and puts the result into 64-byte array out.
public static byte[] core(byte in[], byte k[], byte c[]) {
byte out[] = new byte[64];
public static byte[] core(byte[] in, byte[] k, byte[] c) {
byte[] out = new byte[64];
long mask = 0xFFFFFFFFl;

long j0 = mask & (mask(c[0]) | mask(c[1]) << 8 | mask(c[2]) << 16 | mask(c[3]) << 24);
Expand Down Expand Up @@ -253,10 +253,10 @@ public static byte[] core(byte in[], byte k[], byte c[]) {
// XORKeyStream crypts bytes from in to out using the given key and counters.
// In and out may be the same slice but otherwise should not overlap. Counter
// contains the raw salsa20 counter bytes (both nonce and block counter).
public static byte[] XORKeyStream(byte in[], byte counter[], byte key[]) {
byte out[] = in.clone();
byte block[];
byte counterCopy[] = counter.clone();
public static byte[] XORKeyStream(byte[] in, byte[] counter, byte[] key) {
byte[] out = in.clone();
byte[] block;
byte[] counterCopy = counter.clone();

int count = 0;
while (in.length >= 64) {
Expand All @@ -272,7 +272,7 @@ public static byte[] XORKeyStream(byte in[], byte counter[], byte key[]) {
counterCopy[i] = (byte) (u);
u >>= 8;
}
byte temp[] = in.clone();
byte[] temp = in.clone();
in = new byte[in.length - 64];
for (int i = 0; i < in.length; i++) {
in[i] = temp[i + 64];
Expand Down Expand Up @@ -388,7 +388,7 @@ public static byte[] HSalsa20(byte[] in, byte[] k, byte[] c) {
x15 ^= mask & (u << 18 | u >>> (32 - 18));
}

byte out[] = new byte[32];
byte[] out = new byte[32];
out[0] = (byte) x0;
out[1] = (byte) (x0 >> 8);
out[2] = (byte) (x0 >> 16);
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/pusher/client/crypto/nacl/SecretBoxOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,46 @@ public SecretBoxOpener(byte[] key) {
this.key = key;
}

public byte[] open(byte box[], byte nonce[]) throws AuthenticityException {
public byte[] open(byte[] box, byte[] nonce) throws AuthenticityException {
checkNotNull(key, "key has been cleared, create new instance");

byte subKey[] = new byte[32];
byte counter[] = new byte[16];
byte[] subKey = new byte[32];
byte[] counter = new byte[16];
setup(subKey, counter, nonce, key);

// The Poly1305 key is generated by encrypting 32 bytes of zeros. Since
// Salsa20 works with 64-byte blocks, we also generate 32 bytes of
// keystream as a side effect.
byte firstBlock[] = new byte[64];
byte[] firstBlock = new byte[64];
for (int i = 0; i < firstBlock.length; i++) {
firstBlock[i] = 0;
}
firstBlock = Salsa.XORKeyStream(firstBlock, counter, subKey);

byte poly1305Key[] = new byte[32];
byte[] poly1305Key = new byte[32];
for (int i = 0; i < poly1305Key.length; i++) {
poly1305Key[i] = firstBlock[i];
}
byte tag[] = new byte[Poly1305.TAG_SIZE];
byte[] tag = new byte[Poly1305.TAG_SIZE];
for (int i = 0; i < tag.length; i++) {
tag[i] = box[i];
}

byte cipher[] = new byte[box.length - Poly1305.TAG_SIZE];
byte[] cipher = new byte[box.length - Poly1305.TAG_SIZE];
for (int i = 0; i < cipher.length; i++) {
cipher[i] = box[i + Poly1305.TAG_SIZE];
}
if (!Poly1305.verify(tag, cipher, poly1305Key)) {
throw new AuthenticityException();
}

byte ret[] = new byte[box.length - OVERHEAD];
byte[] ret = new byte[box.length - OVERHEAD];
for (int i = 0; i < ret.length; i++) {
ret[i] = box[i + OVERHEAD];
}
// We XOR up to 32 bytes of box with the keystream generated from
// the first block.
byte firstMessageBlock[] = new byte[ret.length];
byte[] firstMessageBlock = new byte[ret.length];
if (ret.length > 32) {
firstMessageBlock = new byte[32];
}
Expand All @@ -94,11 +94,11 @@ public byte[] open(byte box[], byte nonce[]) throws AuthenticityException {
}

counter[8] = 1;
byte newbox[] = new byte[box.length - (firstMessageBlock.length + OVERHEAD)];
byte[] newbox = new byte[box.length - (firstMessageBlock.length + OVERHEAD)];
for (int i = 0; i < newbox.length; i++) {
newbox[i] = box[i + firstMessageBlock.length + OVERHEAD];
}
byte rest[] = Salsa.XORKeyStream(newbox, counter, subKey);
byte[] rest = Salsa.XORKeyStream(newbox, counter, subKey);
// Now decrypt the rest.

for (int i = firstMessageBlock.length; i < ret.length; i++) {
Expand All @@ -118,14 +118,14 @@ public void clearKey() {
}

// subKey = byte[32], counter = byte[16], nonce = byte[24], key = byte[32]
private void setup(byte subKey[], byte counter[], byte nonce[], byte key[]) {
private void setup(byte[] subKey, byte[] counter, byte[] nonce, byte[] key) {
// We use XSalsa20 for encryption so first we need to generate a
// key and nonce with HSalsa20.
byte hNonce[] = new byte[16];
byte[] hNonce = new byte[16];
for (int i = 0; i < hNonce.length; i++) {
hNonce[i] = nonce[i];
}
byte newSubKey[] = Salsa.HSalsa20(hNonce, key, Salsa.SIGMA);
byte[] newSubKey = Salsa.HSalsa20(hNonce, key, Salsa.SIGMA);
for (int i = 0; i < subKey.length; i++) {
subKey[i] = newSubKey[i];
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pusher/client/crypto/nacl/Subtle.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ a copy of this software and associated documentation files (the "Software"),
package com.pusher.client.crypto.nacl;

public class Subtle {
public static boolean constantTimeCompare(byte x[], byte y[]) {
public static boolean constantTimeCompare(byte[] x, byte[] y) {
if (x.length != y.length) {
return false;
}
Expand Down