Skip to content

Commit 2b09344

Browse files
committed
Simplify: in practice, AAD is always either the hash array or null, never a ByteBuffer or a sub-array
1 parent 0edd2e0 commit 2b09344

File tree

6 files changed

+30
-82
lines changed

6 files changed

+30
-82
lines changed

src/main/java/com/eatthepath/noise/CipherState.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public boolean hasKey() {
3939
return this.key != null;
4040
}
4141

42-
public ByteBuffer decrypt(@Nullable final ByteBuffer associatedData, final ByteBuffer ciphertext)
42+
public ByteBuffer decrypt(@Nullable final byte[] associatedData, final ByteBuffer ciphertext)
4343
throws AEADBadTagException {
4444

4545
final ByteBuffer plaintext = ByteBuffer.allocate(getPlaintextLength(ciphertext.remaining()));
@@ -54,7 +54,7 @@ public ByteBuffer decrypt(@Nullable final ByteBuffer associatedData, final ByteB
5454
return plaintext.flip();
5555
}
5656

57-
public int decrypt(@Nullable final ByteBuffer associatedData, final ByteBuffer ciphertext, final ByteBuffer plaintext)
57+
public int decrypt(@Nullable final byte[] associatedData, final ByteBuffer ciphertext, final ByteBuffer plaintext)
5858
throws AEADBadTagException, ShortBufferException {
5959

6060
if (hasKey()) {
@@ -75,8 +75,6 @@ public byte[] decrypt(@Nullable final byte[] associatedData, final byte[] cipher
7575

7676
try {
7777
decrypt(associatedData,
78-
0,
79-
associatedData != null ? associatedData.length : 0,
8078
ciphertext,
8179
0,
8280
ciphertext.length,
@@ -91,8 +89,6 @@ public byte[] decrypt(@Nullable final byte[] associatedData, final byte[] cipher
9189
}
9290

9391
public int decrypt(@Nullable final byte[] associatedData,
94-
final int aadOffset,
95-
final int aadLength,
9692
final byte[] ciphertext,
9793
final int ciphertextOffset,
9894
final int ciphertextLength,
@@ -103,8 +99,6 @@ public int decrypt(@Nullable final byte[] associatedData,
10399
final int plaintextLength = cipher.decrypt(key,
104100
nonce,
105101
associatedData,
106-
aadOffset,
107-
aadLength,
108102
ciphertext,
109103
ciphertextOffset,
110104
ciphertextLength,
@@ -119,7 +113,7 @@ public int decrypt(@Nullable final byte[] associatedData,
119113
}
120114
}
121115

122-
public ByteBuffer encrypt(@Nullable final ByteBuffer associatedData, final ByteBuffer plaintext) {
116+
public ByteBuffer encrypt(@Nullable final byte[] associatedData, final ByteBuffer plaintext) {
123117
final ByteBuffer ciphertext = ByteBuffer.allocate(getCiphertextLength(plaintext.remaining()));
124118

125119
try {
@@ -132,7 +126,7 @@ public ByteBuffer encrypt(@Nullable final ByteBuffer associatedData, final ByteB
132126
return ciphertext.flip();
133127
}
134128

135-
public int encrypt(@Nullable final ByteBuffer associatedData, final ByteBuffer plaintext, final ByteBuffer ciphertext) throws ShortBufferException {
129+
public int encrypt(@Nullable final byte[] associatedData, final ByteBuffer plaintext, final ByteBuffer ciphertext) throws ShortBufferException {
136130
if (hasKey()) {
137131
final int ciphertextLength = cipher.encrypt(key, nonce, associatedData, plaintext, ciphertext);
138132
nonce += 1;
@@ -151,8 +145,6 @@ public byte[] encrypt(@Nullable final byte[] associatedData, final byte[] plaint
151145

152146
try {
153147
encrypt(associatedData,
154-
0,
155-
associatedData != null ? associatedData.length : 0,
156148
plaintext,
157149
0,
158150
plaintext.length,
@@ -167,8 +159,6 @@ public byte[] encrypt(@Nullable final byte[] associatedData, final byte[] plaint
167159
}
168160

169161
public int encrypt(@Nullable final byte[] associatedData,
170-
final int aadOffset,
171-
final int aadLength,
172162
final byte[] plaintext,
173163
final int plaintextOffset,
174164
final int plaintextLength,
@@ -179,8 +169,6 @@ public int encrypt(@Nullable final byte[] associatedData,
179169
final int ciphertextLength = cipher.encrypt(key,
180170
nonce,
181171
associatedData,
182-
aadOffset,
183-
aadLength,
184172
plaintext,
185173
plaintextOffset,
186174
plaintextLength,

src/main/java/com/eatthepath/noise/NoiseHandshake.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,15 @@ private int encryptAndHash(final byte[] plaintext,
418418
final int ciphertextOffset) throws ShortBufferException {
419419

420420
final int ciphertextLength =
421-
cipherState.encrypt(hash, 0, hash.length, plaintext, plaintextOffset, plaintextLength, ciphertext, ciphertextOffset);
421+
cipherState.encrypt(hash, plaintext, plaintextOffset, plaintextLength, ciphertext, ciphertextOffset);
422422

423423
mixHash(ciphertext, ciphertextOffset, ciphertextLength);
424424

425425
return ciphertextLength;
426426
}
427427

428428
private int encryptAndHash(final ByteBuffer plaintext, final ByteBuffer ciphertext) throws ShortBufferException {
429-
final int ciphertextLength = cipherState.encrypt(ByteBuffer.wrap(hash), plaintext, ciphertext);
429+
final int ciphertextLength = cipherState.encrypt(hash, plaintext, ciphertext);
430430

431431
mixHash(ciphertext.slice(ciphertext.position() - ciphertextLength, ciphertextLength));
432432

@@ -440,7 +440,7 @@ private int decryptAndHash(final byte[] ciphertext,
440440
final int plaintextOffset) throws ShortBufferException, AEADBadTagException {
441441

442442
final int plaintextLength =
443-
cipherState.decrypt(hash, 0, hash.length, ciphertext, ciphertextOffset, ciphertextLength, plaintext, plaintextOffset);
443+
cipherState.decrypt(hash, ciphertext, ciphertextOffset, ciphertextLength, plaintext, plaintextOffset);
444444

445445
mixHash(ciphertext, ciphertextOffset, ciphertextLength);
446446

@@ -451,7 +451,7 @@ private int decryptAndHash(final ByteBuffer ciphertext,
451451
final ByteBuffer plaintext) throws ShortBufferException, AEADBadTagException {
452452

453453
final int initialCiphertextPosition = ciphertext.position();
454-
final int plaintextLength = cipherState.decrypt(ByteBuffer.wrap(hash), ciphertext, plaintext);
454+
final int plaintextLength = cipherState.decrypt(hash, ciphertext, plaintext);
455455

456456
mixHash(ciphertext.slice(initialCiphertextPosition, ciphertext.position() - initialCiphertextPosition));
457457

src/main/java/com/eatthepath/noise/NoiseTransportImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public int readMessage(final byte[] ciphertext,
6464
throw new ShortBufferException("Plaintext array after offset is not large enough to hold plaintext");
6565
}
6666

67-
return readerState.decrypt(null, 0, 0,
67+
return readerState.decrypt(null,
6868
ciphertext, ciphertextOffset, ciphertextLength,
6969
plaintext, plaintextOffset);
7070
}
@@ -113,7 +113,7 @@ public int writeMessage(final byte[] plaintext,
113113
throw new ShortBufferException("Ciphertext array after offset is not large enough to hold ciphertext");
114114
}
115115

116-
return writerState.encrypt(null, 0, 0,
116+
return writerState.encrypt(null,
117117
plaintext, plaintextOffset, plaintextLength,
118118
ciphertext, ciphertextOffset);
119119
}

src/main/java/com/eatthepath/noise/component/AbstractNoiseCipher.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,14 @@ private interface CipherFinalizer<T> {
2626
@Override
2727
public int encrypt(final Key key,
2828
final long nonce,
29-
@Nullable final ByteBuffer associatedData,
29+
@Nullable final byte[] associatedData,
3030
final ByteBuffer plaintext,
3131
final ByteBuffer ciphertext) throws ShortBufferException {
3232

3333
initCipher(cipher, Cipher.ENCRYPT_MODE, key, nonce);
3434

3535
if (associatedData != null) {
36-
final byte[] adBytes = new byte[associatedData.remaining()];
37-
associatedData.get(adBytes);
38-
39-
cipher.updateAAD(adBytes);
36+
cipher.updateAAD(associatedData);
4037
}
4138

4239
return finishEncryption(() -> cipher.doFinal(plaintext, ciphertext));
@@ -46,8 +43,6 @@ public int encrypt(final Key key,
4643
public int encrypt(final Key key,
4744
final long nonce,
4845
@Nullable final byte[] associatedData,
49-
final int aadOffset,
50-
final int aadLength,
5146
final byte[] plaintext,
5247
final int plaintextOffset,
5348
final int plaintextLength,
@@ -57,7 +52,7 @@ public int encrypt(final Key key,
5752
initCipher(cipher, Cipher.ENCRYPT_MODE, key, nonce);
5853

5954
if (associatedData != null) {
60-
cipher.updateAAD(associatedData, aadOffset, aadLength);
55+
cipher.updateAAD(associatedData);
6156
}
6257

6358
return finishEncryption(() ->
@@ -67,17 +62,14 @@ public int encrypt(final Key key,
6762
@Override
6863
public int decrypt(final Key key,
6964
final long nonce,
70-
@Nullable final ByteBuffer associatedData,
65+
@Nullable final byte[] associatedData,
7166
final ByteBuffer ciphertext,
7267
final ByteBuffer plaintext) throws AEADBadTagException, ShortBufferException {
7368

7469
initCipher(cipher, Cipher.DECRYPT_MODE, key, nonce);
7570

7671
if (associatedData != null) {
77-
final byte[] adBytes = new byte[associatedData.remaining()];
78-
associatedData.get(adBytes);
79-
80-
cipher.updateAAD(adBytes);
72+
cipher.updateAAD(associatedData);
8173
}
8274

8375
return finishDecryption(() -> cipher.doFinal(ciphertext, plaintext));
@@ -87,8 +79,6 @@ public int decrypt(final Key key,
8779
public int decrypt(final Key key,
8880
final long nonce,
8981
@Nullable final byte[] associatedData,
90-
final int aadOffset,
91-
final int aadLength,
9282
final byte[] ciphertext,
9383
final int ciphertextOffset,
9484
final int ciphertextLength,
@@ -98,7 +88,7 @@ public int decrypt(final Key key,
9888
initCipher(cipher, Cipher.DECRYPT_MODE, key, nonce);
9989

10090
if (associatedData != null) {
101-
cipher.updateAAD(associatedData, aadOffset, aadLength);
91+
cipher.updateAAD(associatedData);
10292
}
10393

10494
return finishDecryption(() ->

src/main/java/com/eatthepath/noise/component/NoiseCipher.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static NoiseCipher getInstance(final String noiseCipherName) throws NoSuchAlgori
7777
*/
7878
default ByteBuffer encrypt(final Key key,
7979
final long nonce,
80-
@Nullable final ByteBuffer associatedData,
80+
@Nullable final byte[] associatedData,
8181
final ByteBuffer plaintext) {
8282

8383
final ByteBuffer ciphertext = ByteBuffer.allocate(getCiphertextLength(plaintext.remaining()));
@@ -121,7 +121,7 @@ default ByteBuffer encrypt(final Key key,
121121
*/
122122
int encrypt(final Key key,
123123
final long nonce,
124-
@Nullable final ByteBuffer associatedData,
124+
@Nullable final byte[] associatedData,
125125
final ByteBuffer plaintext,
126126
final ByteBuffer ciphertext)
127127
throws ShortBufferException;
@@ -150,8 +150,6 @@ default byte[] encrypt(final Key key,
150150
encrypt(key,
151151
nonce,
152152
associatedData,
153-
0,
154-
associatedData != null ? associatedData.length : 0,
155153
plaintext,
156154
0,
157155
plaintext.length,
@@ -175,10 +173,6 @@ default byte[] encrypt(final Key key,
175173
* @param nonce a nonce, which must be unique for the given key
176174
* @param associatedData a byte array containing the associated data (if any) to be used when encrypting the given
177175
* plaintext; may be {@code null}
178-
* @param aadOffset the position within {@code associatedData} where the associated data starts; ignored if
179-
* {@code associatedData} is {@code null}
180-
* @param aadLength the length of the associated data within {@code associatedData}; ignored if {@code associatedData}
181-
* is {@code null}
182176
* @param plaintext a byte array containing the plaintext to encrypt
183177
* @param plaintextOffset the offset within {@code plaintext} where the plaintext begins
184178
* @param plaintextLength the length of the plaintext within {@code plaintext}
@@ -198,8 +192,6 @@ default byte[] encrypt(final Key key,
198192
int encrypt(final Key key,
199193
final long nonce,
200194
@Nullable final byte[] associatedData,
201-
final int aadOffset,
202-
final int aadLength,
203195
final byte[] plaintext,
204196
final int plaintextOffset,
205197
final int plaintextLength,
@@ -229,7 +221,7 @@ int encrypt(final Key key,
229221
*/
230222
default ByteBuffer decrypt(final Key key,
231223
final long nonce,
232-
@Nullable final ByteBuffer associatedData,
224+
@Nullable final byte[] associatedData,
233225
final ByteBuffer ciphertext) throws AEADBadTagException {
234226

235227
final ByteBuffer plaintext = ByteBuffer.allocate(getPlaintextLength(ciphertext.remaining()));
@@ -272,7 +264,7 @@ default ByteBuffer decrypt(final Key key,
272264
*/
273265
int decrypt(final Key key,
274266
final long nonce,
275-
@Nullable final ByteBuffer associatedData,
267+
@Nullable final byte[] associatedData,
276268
final ByteBuffer ciphertext,
277269
final ByteBuffer plaintext)
278270
throws AEADBadTagException, ShortBufferException;
@@ -304,8 +296,6 @@ default byte[] decrypt(final Key key,
304296
decrypt(key,
305297
nonce,
306298
associatedData,
307-
0,
308-
associatedData != null ? associatedData.length : 0,
309299
ciphertext,
310300
0,
311301
ciphertext.length,
@@ -330,10 +320,6 @@ default byte[] decrypt(final Key key,
330320
* @param nonce a nonce, which must be unique for the given key
331321
* @param associatedData a byte array containing the associated data (if any) to be used when verifying the AEAD tag
332322
* for the given ciphertext; may be {@code null}
333-
* @param aadOffset the position within {@code associatedData} where the associated data starts; ignored if
334-
* {@code associatedData} is {@code null}
335-
* @param aadLength the length of the associated data within {@code associatedData}; ignored if {@code associatedData}
336-
* is {@code null}
337323
* @param ciphertext a byte array containing the ciphertext and AEAD tag to be decrypted and verified
338324
* @param ciphertextOffset the position within {@code ciphertext} at which to begin reading the ciphertext and AEAD
339325
* tag
@@ -353,8 +339,6 @@ default byte[] decrypt(final Key key,
353339
int decrypt(final Key key,
354340
final long nonce,
355341
@Nullable final byte[] associatedData,
356-
final int aadOffset,
357-
final int aadLength,
358342
final byte[] ciphertext,
359343
final int ciphertextOffset,
360344
final int ciphertextLength,

src/test/java/com/eatthepath/noise/component/AbstractNoiseCipherTest.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ void encryptDecryptByteArrayInPlace() throws AEADBadTagException, ShortBufferExc
4747
System.arraycopy(plaintextBytes, 0, buffer, 0, plaintextBytes.length);
4848

4949
assertEquals(buffer.length, getNoiseCipher().encrypt(key, nonce,
50-
hash, 0, hash.length,
50+
hash,
5151
buffer, 0, plaintextBytes.length,
5252
buffer, 0));
5353

5454
assertEquals(plaintextBytes.length, getNoiseCipher().decrypt(key, nonce,
55-
hash, 0, hash.length,
55+
hash,
5656
buffer, 0, buffer.length,
5757
buffer, 0));
5858

@@ -66,39 +66,26 @@ void encryptDecryptByteArrayInPlace() throws AEADBadTagException, ShortBufferExc
6666
void encryptDecryptNewByteBuffer() throws AEADBadTagException {
6767
final Key key = generateKey();
6868
final long nonce = ThreadLocalRandom.current().nextLong();
69-
70-
final ByteBuffer hashBuffer;
71-
{
72-
final byte[] hash = new byte[32];
73-
ThreadLocalRandom.current().nextBytes(hash);
74-
75-
hashBuffer = ByteBuffer.wrap(hash);
76-
}
69+
final byte[] hash = new byte[32];
70+
ThreadLocalRandom.current().nextBytes(hash);
7771

7872
final ByteBuffer plaintext = ByteBuffer.wrap("Hark! Plaintext!".getBytes(StandardCharsets.UTF_8));
79-
final ByteBuffer ciphertext = getNoiseCipher().encrypt(key, nonce, hashBuffer, plaintext);
73+
final ByteBuffer ciphertext = getNoiseCipher().encrypt(key, nonce, hash, plaintext);
8074

8175
plaintext.rewind();
82-
hashBuffer.rewind();
8376

8477
assertEquals(ciphertext.remaining(), getNoiseCipher().getCiphertextLength(plaintext.remaining()));
8578
assertEquals(plaintext.remaining(), getNoiseCipher().getPlaintextLength(ciphertext.remaining()));
8679

87-
assertEquals(plaintext, getNoiseCipher().decrypt(key, nonce, hashBuffer, ciphertext));
80+
assertEquals(plaintext, getNoiseCipher().decrypt(key, nonce, hash, ciphertext));
8881
}
8982

9083
@Test
9184
void encryptDecryptByteBufferInPlace() throws AEADBadTagException, ShortBufferException {
9285
final Key key = generateKey();
9386
final long nonce = ThreadLocalRandom.current().nextLong();
94-
95-
final ByteBuffer hashBuffer;
96-
{
97-
final byte[] hash = new byte[32];
98-
ThreadLocalRandom.current().nextBytes(hash);
99-
100-
hashBuffer = ByteBuffer.wrap(hash);
101-
}
87+
final byte[] hash = new byte[32];
88+
ThreadLocalRandom.current().nextBytes(hash);
10289

10390
final byte[] plaintextBytes = "Hark! Plaintext!".getBytes(StandardCharsets.UTF_8);
10491
final byte[] sharedByteArray = new byte[getNoiseCipher().getCiphertextLength(plaintextBytes.length)];
@@ -111,18 +98,17 @@ void encryptDecryptByteBufferInPlace() throws AEADBadTagException, ShortBufferEx
11198
final ByteBuffer ciphertextBuffer = ByteBuffer.wrap(sharedByteArray);
11299

113100
assertEquals(sharedByteArray.length,
114-
getNoiseCipher().encrypt(key, nonce, hashBuffer, plaintextBuffer, ciphertextBuffer));
101+
getNoiseCipher().encrypt(key, nonce, hash, plaintextBuffer, ciphertextBuffer));
115102

116103
assertEquals(plaintextBytes.length, plaintextBuffer.limit());
117104
assertEquals(plaintextBuffer.limit(), plaintextBuffer.position());
118105
assertEquals(sharedByteArray.length, ciphertextBuffer.position());
119106

120-
hashBuffer.rewind();
121107
plaintextBuffer.rewind();
122108
ciphertextBuffer.rewind();
123109

124110
assertEquals(plaintextBytes.length,
125-
getNoiseCipher().decrypt(key, nonce, hashBuffer, ciphertextBuffer, plaintextBuffer));
111+
getNoiseCipher().decrypt(key, nonce, hash, ciphertextBuffer, plaintextBuffer));
126112

127113
assertEquals(plaintextBytes.length, plaintextBuffer.limit());
128114
assertEquals(plaintextBuffer.limit(), plaintextBuffer.position());

0 commit comments

Comments
 (0)