Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Commit

Permalink
Implement Stream ciphers: Salsa20 crypto_stream_xsalsa20_xor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi YUAN authored and Bruno Oliveira committed Apr 26, 2017
1 parent ef4f22d commit f70f109
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/org/abstractj/kalium/NaCl.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,14 @@ int crypto_scalarmult_curve25519(
// ---------------------------------------------------------------------
// Advanced: Stream ciphers: Salsa20

// TODO
int CRYPTO_STREAM_KEYBYTES = 32;

int CRYPTO_STREAM_NONCEBYTES = 24;

int crypto_stream_xor(
@Out byte[] result, @In byte[] message,
@In @u_int64_t int mlen,
@In byte[] nonce, @In byte[] key);

// ---------------------------------------------------------------------
// Advanced: Stream ciphers: XSalsa20
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/abstractj/kalium/crypto/Advanced.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2013 Bruno Oliveira, and individual contributors
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.abstractj.kalium.crypto;

import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_STREAM_KEYBYTES;
import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_STREAM_NONCEBYTES;
import static org.abstractj.kalium.NaCl.sodium;
import static org.abstractj.kalium.crypto.Util.checkLength;

public class Advanced {

public byte[] crypto_stream_xsalsa20_xor(byte[] message, byte[] nonce, byte[] key) {

checkLength(nonce, CRYPTO_STREAM_NONCEBYTES);
checkLength(key, CRYPTO_STREAM_KEYBYTES);
byte[] buffer = new byte[message.length];
sodium().crypto_stream_xor(buffer, message, message.length, nonce, key);
return buffer;

}
}
82 changes: 82 additions & 0 deletions src/test/java/org/abstractj/kalium/crypto/AdvancedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright 2013 Bruno Oliveira, and individual contributors
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.abstractj.kalium.crypto;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class AdvancedTest {

@Test
public void testXsalsa20HappyFlow() {
Random random = new Random();
Advanced advanced = new Advanced();
byte[] nonce = random.randomBytes(24);
byte[] key = random.randomBytes(32);
String pwd = "This is a test message :-)...";
byte[] plaintext = pwd.getBytes();
byte[] ciphertext = advanced.crypto_stream_xsalsa20_xor(plaintext, nonce, key); // encrypt
plaintext = advanced.crypto_stream_xsalsa20_xor(ciphertext, nonce, key); // decrypt
assertEquals(pwd, new String(plaintext));
}

@Test
public void testXsalsa20IncorrectNonce() {
Random random = new Random();
Advanced advanced = new Advanced();
byte[] nonce = random.randomBytes(24);
byte[] incorrectNonce = random.randomBytes(24);
byte[] key = random.randomBytes(32);
String pwd = "This is a test message :-)...";
byte[] plaintext = pwd.getBytes();
byte[] ciphertext = advanced.crypto_stream_xsalsa20_xor(plaintext, nonce, key); // encrypt
plaintext = advanced.crypto_stream_xsalsa20_xor(ciphertext, incorrectNonce, key); // decrypt
assertNotEquals(pwd, new String(plaintext));
}

@Test
public void testXsalsa20IncorrectKey() {
Random random = new Random();
Advanced advanced = new Advanced();
byte[] nonce = random.randomBytes(24);
byte[] key = random.randomBytes(32);
byte[] incorrectKey = random.randomBytes(32);
String pwd = "This is a test message :-)...";
byte[] plaintext = pwd.getBytes();
byte[] ciphertext = advanced.crypto_stream_xsalsa20_xor(plaintext, nonce, key); // encrypt
plaintext = advanced.crypto_stream_xsalsa20_xor(ciphertext, nonce, incorrectKey); // decrypt
assertNotEquals(pwd, new String(plaintext));
}

@Test
public void testXsalsa20IncorrectKeyAndIncorrectNonce() {
Random random = new Random();
Advanced advanced = new Advanced();
byte[] nonce = random.randomBytes(24);
byte[] incorrectNonce = random.randomBytes(24);
byte[] key = random.randomBytes(32);
byte[] incorrectKey = random.randomBytes(32);
String pwd = "This is a test message :-)...";
byte[] plaintext = pwd.getBytes();
byte[] ciphertext = advanced.crypto_stream_xsalsa20_xor(plaintext, nonce, key); // encrypt
plaintext = advanced.crypto_stream_xsalsa20_xor(ciphertext, incorrectNonce, incorrectKey); // decrypt
assertNotEquals(pwd, new String(plaintext));
}

}

0 comments on commit f70f109

Please sign in to comment.