Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/main/java/com/goterl/lazysodium/LazySodium.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,36 @@ public boolean sodiumUnpad(IntByReference unPaddedBuffLen, Pointer buf, int padd

@Override
public boolean sodiumMemZero(byte[] pnt, int len) {
return successful(getSodium().sodium_memzero(pnt, len));
getSodium().sodium_memzero(pnt, len);
return true;
}

@Override
public boolean sodiumMemZero(Pointer pnt, int len) {
getSodium().sodium_memzero(pnt, len);
return true;
}

@Override
public boolean sodiumMLock(byte[] array, int len) {
return successful(getSodium().sodium_mlock(array, len));
}

@Override
public boolean sodiumMLock(Pointer pnt, int len) {
return successful(getSodium().sodium_mlock(pnt, len));
}

@Override
public boolean sodiumMUnlock(byte[] array, int len) {
return successful(getSodium().sodium_munlock(array, len));
}

@Override
public boolean sodiumMUnlock(Pointer pnt, int len) {
return successful(getSodium().sodium_munlock(pnt, len));
}

@Override
public Pointer sodiumMalloc(int size) {
return getSodium().sodium_malloc(size);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/goterl/lazysodium/Sodium.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ public native int sodium_base642bin(byte[] bin,
//// SECURE MEMORY
//// -------------------------------------------|

public native int sodium_memzero(byte[] pnt, int len);
public native void sodium_memzero(byte[] pnt, int len);
public native void sodium_memzero(Pointer pnt, int len);
public native int sodium_mlock(byte[] addr, int len);
public native int sodium_mlock(Pointer addr, int len);
public native int sodium_munlock(byte[] addr, int len);
public native int sodium_munlock(Pointer addr, int len);
public native Pointer sodium_malloc(int size);
public native Pointer sodium_allocarray(int count, int size);
public native void sodium_free(Pointer p);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/goterl/lazysodium/interfaces/SecureMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ interface Native {
*/
boolean sodiumMemZero(byte[] pnt, int len);

/**
* The sodium_memzero() function tries to effectively zero len bytes starting at pnt,
* even if optimizations are being applied to the code.
* @param pnt The pointer to zero out.
* @param len How many bytes to zero out.
* @return True if zeroed
*/
boolean sodiumMemZero(Pointer pnt, int len);

/**
* Locks at least len bytes of memory from the array.
* This can help avoid swapping sensitive data to disk.
Expand All @@ -34,6 +43,15 @@ interface Native {
*/
boolean sodiumMLock(byte[] array, int len);

/**
* Locks at least len bytes of memory from the pointer.
* This can help avoid swapping sensitive data to disk.
* @param pnt pointer to the memory to lock.
* @param len Number of bytes to lock.
* @return True if locked, false otherwise.
*/
boolean sodiumMLock(Pointer pnt, int len);

/**
* Unlocks at least len bytes of memory from the array.
* @param array Array to unlock.
Expand All @@ -42,6 +60,14 @@ interface Native {
*/
boolean sodiumMUnlock(byte[] array, int len);

/**
* Unlocks at least len bytes of memory from the pointer.
* @param pnt pointer to the memory to unlock.
* @param len Number of bytes to unlock.
* @return True if locked, false otherwise.
*/
boolean sodiumMUnlock(Pointer pnt, int len);

/**
* Returns a pointer from which exactly
* size contiguous bytes of memory can be accessed.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/goterl/lazysodium/Ristretto255Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void fromHash() throws Exception {
};

for (int i = 0; i < testInput.length; ++i) {
MessageDigest sha512 = MessageDigest.getInstance("SHA512");
MessageDigest sha512 = MessageDigest.getInstance("SHA-512");

byte[] hashed = sha512.digest(testInput[i].getBytes(StandardCharsets.UTF_8));
RistrettoPoint encoded = lazySodium.cryptoCoreRistretto255FromHash(hashed);
Expand Down
49 changes: 46 additions & 3 deletions src/test/java/com/goterl/lazysodium/SecureMemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,60 @@
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
*/

package com.goterl.lazysodium;import com.sun.jna.Pointer;
package com.goterl.lazysodium;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import junit.framework.TestCase;
import org.junit.Test;

public class SecureMemoryTest extends BaseTest {


@Test
public void memZero() {
byte[] b = new byte[] { 4, 2, 2, 1 };
boolean res = lazySodium.sodiumMemZero(b, b.length);
TestCase.assertTrue(isZero(b));
}

@Test
public void memZeroPtr() {
Pointer p = new Memory(32);
p.write(0, lazySodium.randomBytesBuf(32), 0, 32);
TestCase.assertFalse(isZero(p.getByteArray(0, 32)));

boolean res = lazySodium.sodiumMemZero(p, 32);

TestCase.assertTrue(res);
TestCase.assertTrue(isZero(p.getByteArray(0, 32)));

}

@Test
public void mLock() {
byte[] b = new byte[] { 4, 5, 2, 1 };

boolean res = lazySodium.sodiumMLock(b, b.length);
boolean res2 = lazySodium.sodiumMUnlock(b, b.length);

TestCase.assertTrue(res);
TestCase.assertTrue(res2);
TestCase.assertTrue(isZero(b));
}

@Test
public void mLockPtr() {
Pointer p = new Memory(32);
p.write(0, lazySodium.randomBytesBuf(32), 0, 32);
TestCase.assertFalse(isZero(p.getByteArray(0, 32)));

boolean res = lazySodium.sodiumMLock(p, 32);
boolean res2 = lazySodium.sodiumMUnlock(p, 32);

TestCase.assertTrue(res);
TestCase.assertTrue(res2);
TestCase.assertTrue(isZero(p.getByteArray(0, 32)));
}

@Test
public void malloc() {
int size = 10;
Expand All @@ -39,6 +71,17 @@ public void malloc() {
TestCase.assertEquals(arr.length, size);
}

@Test
public void allocArray() {
int size = 10;

Pointer ptr = lazySodium.sodiumAllocArray(size, 2);

byte[] arr = ptr.getByteArray(0, size * 2);

TestCase.assertEquals(arr.length, size * 2);
}

@Test
public void free() {
int size = 10;
Expand Down