Skip to content

Commit

Permalink
Add ByteBuffer-based methods to NoiseHandshake
Browse files Browse the repository at this point in the history
  • Loading branch information
jchambers committed Mar 2, 2024
1 parent c047bb8 commit 9a4ad76
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/example/java/NoiseHandshakeExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ void interactiveExample() throws InvalidKeySpecException, AEADBadTagException, N
.build();

// -> e (with an empty payload)
final byte[] initiatorEMessage = initiatorHandshake.writeMessage(null);
final byte[] initiatorEMessage = initiatorHandshake.writeMessage((byte[]) null);
responderHandshake.readMessage(initiatorEMessage);

// <- e, ee (with an empty payload)
final byte[] responderEEeMessage = responderHandshake.writeMessage(null);
final byte[] responderEEeMessage = responderHandshake.writeMessage((byte[]) null);
initiatorHandshake.readMessage(responderEEeMessage);

assert initiatorHandshake.isDone();
Expand Down Expand Up @@ -59,7 +59,7 @@ void oneWayExample() throws NoSuchAlgorithmException, InvalidKeySpecException, A
.build();

// -> e, es (with an empty payload)
final byte[] initiatorEphemeralKeyMessage = initiatorHandshake.writeMessage(null);
final byte[] initiatorEphemeralKeyMessage = initiatorHandshake.writeMessage((byte[]) null);
responderHandshake.readMessage(initiatorEphemeralKeyMessage);

assert initiatorHandshake.isDone();
Expand Down Expand Up @@ -93,7 +93,7 @@ void fallbackExample() throws NoSuchAlgorithmException, InvalidKeySpecException,
assertThrows(AEADBadTagException.class, () -> {
// @start region="send-initiator-static-key-message"
// -> e, es, s, ss (with an empty payload)
final byte[] initiatorStaticKeyMessage = ikInitiatorHandshake.writeMessage(null);
final byte[] initiatorStaticKeyMessage = ikInitiatorHandshake.writeMessage((byte[]) null);

// Throws an AEADBadTagException because the initiator has a stale static key for the responder
ikResponderHandshake.readMessage(initiatorStaticKeyMessage);
Expand All @@ -105,7 +105,7 @@ void fallbackExample() throws NoSuchAlgorithmException, InvalidKeySpecException,
ikResponderHandshake.fallbackTo("XXfallback");

// <- e, ee, s, es (with an empty payload)
final byte[] responderStaticKeyMessage = xxFallbackResponderHandshake.writeMessage(null);
final byte[] responderStaticKeyMessage = xxFallbackResponderHandshake.writeMessage((byte[]) null);
// @end

assertThrows(AEADBadTagException.class, () -> {
Expand All @@ -122,7 +122,7 @@ void fallbackExample() throws NoSuchAlgorithmException, InvalidKeySpecException,
xxFallbackInitiatorHandshake.readMessage(responderStaticKeyMessage);

final byte[] initiatorFallbackStaticKeyMessage =
xxFallbackInitiatorHandshake.writeMessage(null);
xxFallbackInitiatorHandshake.writeMessage((byte[]) null);

xxFallbackResponderHandshake.readMessage(initiatorFallbackStaticKeyMessage);

Expand Down
4 changes: 2 additions & 2 deletions src/example/java/NoisePackageExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ void simpleHandshake() throws NoSuchAlgorithmException, NoSuchPatternException,

// @start region="handshake-messages"
// -> e (with no additional payload)
final byte[] eMessage = initiatorHandshake.writeMessage(null);
final byte[] eMessage = initiatorHandshake.writeMessage((byte[]) null);
responderHandshake.readMessage(eMessage);

// <- e, ee (with no additional payload)
final byte[] eEeMessage = responderHandshake.writeMessage(null);
final byte[] eEeMessage = responderHandshake.writeMessage((byte[]) null);
initiatorHandshake.readMessage(eEeMessage);

// At this point, the handshake is finished, and we can "split" the handshake into a Noise transport
Expand Down
4 changes: 2 additions & 2 deletions src/example/java/OverviewExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void transport() throws NoSuchAlgorithmException, InvalidKeySpecException, AEADB
.setComponentsFromProtocolName("Noise_NN_25519_AESGCM_SHA256")
.build();

responderHandshake.readMessage(initiatorHandshake.writeMessage(null));
initiatorHandshake.readMessage(responderHandshake.writeMessage(null));
responderHandshake.readMessage(initiatorHandshake.writeMessage((byte[]) null));
initiatorHandshake.readMessage(responderHandshake.writeMessage((byte[]) null));

// @start region="transport-messages"
final NoiseTransport initiatorTransport = initiatorHandshake.toTransport();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/eatthepath/noise/CipherState.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ByteBuffer decrypt(@Nullable final ByteBuffer associatedData, final ByteB
throw new AssertionError(e);
}

return plaintext;
return plaintext.flip();
}

public int decrypt(@Nullable final ByteBuffer associatedData, final ByteBuffer ciphertext, final ByteBuffer plaintext)
Expand Down Expand Up @@ -133,7 +133,7 @@ public ByteBuffer encrypt(@Nullable final ByteBuffer associatedData, final ByteB
throw new AssertionError(e);
}

return ciphertext;
return ciphertext.flip();
}

public int encrypt(@Nullable final ByteBuffer associatedData, final ByteBuffer plaintext, final ByteBuffer ciphertext) throws ShortBufferException {
Expand Down
Loading

0 comments on commit 9a4ad76

Please sign in to comment.