Skip to content

Commit 55b97c5

Browse files
committed
Merge pull request #7 from franks42/master
In TweetNacl.java, added additional methods for Box' box, open, after…
2 parents 988a508 + fed4fdb commit 55b97c5

File tree

2 files changed

+271
-33
lines changed

2 files changed

+271
-33
lines changed

src/com/iwebpp/crypto/TweetNacl.java

Lines changed: 101 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,29 @@ private byte[] generateNonce() {
8383
* */
8484
///public byte_buf_t box(byte [] message) {
8585
public byte [] box(byte [] message) {
86+
87+
return box(message, generateNonce());
88+
89+
}
90+
91+
/*
92+
* @description
93+
* Encrypt and authenticates message using peer's public key,
94+
* our secret key, and the explicitly provided nonce.
95+
* Caller is responsible for ensuring that nonce is unique
96+
* for each distinct message for a key pair.
97+
*
98+
* Returns an encrypted and authenticated message,
99+
* which is nacl.box.overheadLength longer than the original message.
100+
* */
101+
///public byte_buf_t box(byte [] message) {
102+
public byte [] box(byte [] message, byte [] theNonce) {
86103

87104
// check message
88-
if (!(message!=null && message.length>0))
105+
if (!(message!=null && message.length>0 &&
106+
theNonce!=null && theNonce.length==nonceLength))
89107
return null;
90108

91-
// generate nonce
92-
byte [] n = generateNonce();
93-
94109
// message buffer
95110
byte [] m = new byte[message.length + zerobytesLength];
96111

@@ -100,7 +115,7 @@ private byte[] generateNonce() {
100115
for (int i = 0; i < message.length; i ++)
101116
m[i+zerobytesLength] = message[i];
102117

103-
if (0 != crypto_box(c, m, m.length, n, theirPublicKey, mySecretKey))
118+
if (0 != crypto_box(c, m, m.length, theNonce, theirPublicKey, mySecretKey))
104119
return null;
105120

106121
// wrap byte_buf_t on c offset@boxzerobytesLength
@@ -121,13 +136,24 @@ private byte[] generateNonce() {
121136
* Returns the original message, or null if authentication fails.
122137
* */
123138
public byte [] open(byte [] box) {
139+
140+
return open(box, generateNonce());
141+
142+
}
143+
144+
/*
145+
* @description
146+
* Authenticates and decrypts the given box with peer's public key,
147+
* our secret key, and the explicitly provided nonce.
148+
*
149+
* Returns the original message, or null if authentication fails.
150+
* */
151+
public byte [] open(byte [] box, byte [] theNonce) {
124152
// check message
125-
if (!(box!=null && box.length>boxzerobytesLength))
153+
if (!(box!=null && box.length>boxzerobytesLength &&
154+
theNonce!=null && theNonce.length==nonceLength))
126155
return null;
127156

128-
// generate nonce
129-
byte [] n = generateNonce();
130-
131157
// cipher buffer
132158
byte [] c = new byte[box.length + boxzerobytesLength];
133159

@@ -137,7 +163,7 @@ private byte[] generateNonce() {
137163
for (int i = 0; i < box.length; i++)
138164
c[i+boxzerobytesLength] = box[i];
139165

140-
if (0 != crypto_box_open(m, c, c.length, n, theirPublicKey, mySecretKey))
166+
if (0 != crypto_box_open(m, c, c.length, theNonce, theirPublicKey, mySecretKey))
141167
return null;
142168

143169
// wrap byte_buf_t on m offset@zerobytesLength
@@ -169,13 +195,22 @@ private byte[] generateNonce() {
169195
* Same as nacl.box, but uses a shared key precomputed with nacl.box.before.
170196
* */
171197
public byte [] after(byte [] message) {
198+
199+
return after(message, generateNonce());
200+
201+
}
202+
203+
/*
204+
* @description
205+
* Same as nacl.box, but uses a shared key precomputed with nacl.box.before
206+
* and explicitly provided nonce
207+
* */
208+
public byte [] after(byte [] message, byte [] theNonce) {
172209
// check message
173-
if (!(message!=null && message.length>0))
210+
if (!(message!=null && message.length>0 &&
211+
theNonce!=null && theNonce.length==nonceLength))
174212
return null;
175213

176-
// generate nonce
177-
byte [] n = generateNonce();
178-
179214
// message buffer
180215
byte [] m = new byte[message.length + zerobytesLength];
181216

@@ -185,7 +220,7 @@ private byte[] generateNonce() {
185220
for (int i = 0; i < message.length; i ++)
186221
m[i+zerobytesLength] = message[i];
187222

188-
if (0 != crypto_box_afternm(c, m, m.length, n, sharedKey))
223+
if (0 != crypto_box_afternm(c, m, m.length, theNonce, sharedKey))
189224
return null;
190225

191226
// wrap byte_buf_t on c offset@boxzerobytesLength
@@ -204,13 +239,23 @@ private byte[] generateNonce() {
204239
* but uses a shared key pre-computed with nacl.box.before.
205240
* */
206241
public byte [] open_after(byte [] box) {
242+
243+
return open_after(box, generateNonce());
244+
245+
}
246+
247+
/*
248+
* @description
249+
* Same as nacl.box.open,
250+
* but uses a shared key pre-computed with nacl.box.before,
251+
* and explicitly passed nonce
252+
* */
253+
public byte [] open_after(byte [] box, byte [] theNonce) {
207254
// check message
208-
if (!(box!=null && box.length>boxzerobytesLength))
255+
if (!(box!=null && box.length>boxzerobytesLength &&
256+
theNonce!=null && theNonce.length==nonceLength))
209257
return null;
210258

211-
// generate nonce
212-
byte [] n = generateNonce();
213-
214259
// cipher buffer
215260
byte [] c = new byte[box.length + boxzerobytesLength];
216261

@@ -220,7 +265,7 @@ private byte[] generateNonce() {
220265
for (int i = 0; i < box.length; i++)
221266
c[i+boxzerobytesLength] = box[i];
222267

223-
if (crypto_box_open_afternm(m, c, c.length, n, sharedKey) != 0)
268+
if (crypto_box_open_afternm(m, c, c.length, theNonce, sharedKey) != 0)
224269
return null;
225270

226271
// wrap byte_buf_t on m offset@zerobytesLength
@@ -379,13 +424,27 @@ private byte[] generateNonce() {
379424
* */
380425
///public byte_buf_t box(byte [] message) {
381426
public byte [] box(byte [] message) {
427+
428+
return box(message, generateNonce());
429+
430+
}
431+
432+
/*
433+
* @description
434+
* Encrypt and authenticates message using the key
435+
* and the explicitly passed nonce.
436+
* The nonce must be unique for each distinct message for this key.
437+
*
438+
* Returns an encrypted and authenticated message,
439+
* which is nacl.secretbox.overheadLength longer than the original message.
440+
* */
441+
///public byte_buf_t box(byte [] message) {
442+
public byte [] box(byte [] message, byte [] theNonce) {
382443
// check message
383-
if (!(message!=null && message.length>0))
444+
if (!(message!=null && message.length>0 &&
445+
theNonce!=null && theNonce.length==nonceLength))
384446
return null;
385447

386-
// generate nonce
387-
byte [] n = generateNonce();
388-
389448
// message buffer
390449
byte [] m = new byte[message.length + zerobytesLength];
391450

@@ -395,7 +454,7 @@ private byte[] generateNonce() {
395454
for (int i = 0; i < message.length; i ++)
396455
m[i+zerobytesLength] = message[i];
397456

398-
if (0 != crypto_secretbox(c, m, m.length, n, key))
457+
if (0 != crypto_secretbox(c, m, m.length, theNonce, key))
399458
return null;
400459

401460
// TBD optimizing ...
@@ -417,13 +476,24 @@ private byte[] generateNonce() {
417476
* Returns the original message, or null if authentication fails.
418477
* */
419478
public byte [] open(byte [] box) {
479+
480+
return open(box, generateNonce());
481+
482+
}
483+
484+
/*
485+
* @description
486+
* Authenticates and decrypts the given secret box
487+
* using the key and the explicitly passed nonce.
488+
*
489+
* Returns the original message, or null if authentication fails.
490+
* */
491+
public byte [] open(byte [] box, byte [] theNonce) {
420492
// check message
421-
if (!(box!=null && box.length>boxzerobytesLength))
493+
if (!(box!=null && box.length>boxzerobytesLength &&
494+
theNonce!=null && theNonce.length==nonceLength))
422495
return null;
423496

424-
// generate nonce
425-
byte [] n = generateNonce();
426-
427497
// cipher buffer
428498
byte [] c = new byte[box.length + boxzerobytesLength];
429499

@@ -433,7 +503,7 @@ private byte[] generateNonce() {
433503
for (int i = 0; i < box.length; i++)
434504
c[i+boxzerobytesLength] = box[i];
435505

436-
if (0 != crypto_secretbox_open(m, c, c.length, n, key))
506+
if (0 != crypto_secretbox_open(m, c, c.length, theNonce, key))
437507
return null;
438508

439509
// wrap byte_buf_t on m offset@zerobytesLength
@@ -1885,7 +1955,7 @@ public static int crypto_sign_open(byte [] m, long dummy /* *mlen not used*/, by
18851955
* */
18861956
private static final SecureRandom jrandom = new SecureRandom();
18871957

1888-
private static void randombytes(byte [] x, int len) {
1958+
public static void randombytes(byte [] x, int len) {
18891959
int ret = len % 8;
18901960
long rnd;
18911961

0 commit comments

Comments
 (0)