Skip to content

Commit 9c4712e

Browse files
committed
Fixed an implicit extension byte in TweetNacl.unpackneg and TweetNaclFast.unpackneg.
Test includes.
1 parent 0db77f3 commit 9c4712e

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

src/com/iwebpp/crypto/TweetNacl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,7 @@ private static int unpackneg(long [] r[], byte p[])
22992299
M(chk,0,chk.length, chk,0,chk.length, den,0,den.length);
23002300
if (neq25519(chk, num)!=0) return -1;
23012301

2302-
if (par25519(r[0]) == (p[31]>>7)) Z(r[0],0,r[0].length, gf0,0,gf0.length, r[0],0,r[0].length);
2302+
if (par25519(r[0]) == ((p[31]&0xFF)>>7)) Z(r[0],0,r[0].length, gf0,0,gf0.length, r[0],0,r[0].length);
23032303

23042304
M(r[3],0,r[3].length, r[0],0,r[0].length, r[1],0,r[1].length);
23052305
return 0;

src/com/iwebpp/crypto/TweetNaclFast.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,7 @@ private static int unpackneg(long [] r[], byte p[])
32623262
M(chk, chk, den);
32633263
if (neq25519(chk, num)!=0) return -1;
32643264

3265-
if (par25519(r[0]) == (p[31]>>>7)) Z(r[0], gf0, r[0]);
3265+
if (par25519(r[0]) == ((p[31]&0xFF)>>>7)) Z(r[0], gf0, r[0]);
32663266

32673267
M(r[3], r[0], r[1]);
32683268

src/com/iwebpp/crypto/tests/TweetNaclFastTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,34 @@ private boolean testHash() throws UnsupportedEncodingException {
524524

525525
return true;
526526
}
527-
527+
528+
private boolean testSignDetached(String seedStr) throws UnsupportedEncodingException {
529+
Log.d(TAG, "seed:@" + System.currentTimeMillis());
530+
531+
byte[] seed = TweetNaclFast.hexDecode(seedStr);
532+
TweetNaclFast.Signature.KeyPair kp = TweetNaclFast.Signature.keyPair_fromSeed(seed);
533+
534+
String testString = "test string";
535+
byte[] bytes = testString.getBytes();
536+
537+
TweetNaclFast.Signature s1 = new TweetNaclFast.Signature(null, kp.getSecretKey());
538+
Log.d(TAG, "\ndetached...@" + System.currentTimeMillis());
539+
byte[] signature = s1.detached(bytes);
540+
Log.d(TAG, "...detached@" + System.currentTimeMillis());
541+
542+
TweetNaclFast.Signature s2 = new TweetNaclFast.Signature(kp.getPublicKey(), null);
543+
Log.d(TAG, "\nverify...@" + System.currentTimeMillis());
544+
boolean result = s2.detached_verify(bytes, signature);
545+
Log.d(TAG, "...verify@" + System.currentTimeMillis());
546+
547+
if(result) {
548+
Log.d(TAG, "verify success @" + testString);
549+
} else {
550+
Log.e(TAG, "verify failed @" + testString);
551+
}
552+
553+
return true;
554+
}
528555
/*
529556
* bench test using tweetnacl.c, tweetnacl.js result
530557
* */
@@ -541,13 +568,16 @@ public void run() {
541568
try {
542569
///testSecretBox();
543570
///testSecretBoxNonce();
544-
testBox();
571+
///testBox();
545572
///testBoxNonce();
546573
///testBoxKalium();
547574

548575
///testHash();
549576
///testSign();
550-
577+
578+
testSignDetached("ac49000da11249ea3510941703a7e21a39837c4d2d5300daebbd532df20f8135");
579+
testSignDetached("e56f0eef73ade8f79bc1d16a99cbc5e4995afd8c14adb49410ecd957aecc8d02");
580+
551581
///testBench();
552582
} catch (UnsupportedEncodingException e) {
553583
// TODO Auto-generated catch block

src/com/iwebpp/crypto/tests/TweetNaclTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.UnsupportedEncodingException;
77
import com.iwebpp.crypto.TweetNacl;
8+
import com.iwebpp.crypto.TweetNaclFast;
9+
810
import static com.iwebpp.crypto.TweetNacl.Box.nonceLength;
911

1012
public final class TweetNaclTest {
@@ -423,7 +425,34 @@ private boolean testHash() throws UnsupportedEncodingException {
423425

424426
return true;
425427
}
426-
428+
429+
private boolean testSignDetached(String seedStr) throws UnsupportedEncodingException {
430+
Log.d(TAG, "seed:@" + System.currentTimeMillis());
431+
432+
byte[] seed = TweetNaclFast.hexDecode(seedStr);
433+
TweetNacl.Signature.KeyPair kp = TweetNacl.Signature.keyPair_fromSeed(seed);
434+
435+
String testString = "test string";
436+
byte[] bytes = testString.getBytes();
437+
438+
TweetNacl.Signature s1 = new TweetNacl.Signature(null, kp.getSecretKey());
439+
Log.d(TAG, "\ndetached...@" + System.currentTimeMillis());
440+
byte[] signature = s1.detached(bytes);
441+
Log.d(TAG, "...detached@" + System.currentTimeMillis());
442+
443+
TweetNacl.Signature s2 = new TweetNacl.Signature(kp.getPublicKey(), null);
444+
Log.d(TAG, "\nverify...@" + System.currentTimeMillis());
445+
boolean result = s2.detached_verify(bytes, signature);
446+
Log.d(TAG, "...verify@" + System.currentTimeMillis());
447+
448+
if(result) {
449+
Log.d(TAG, "verify success @" + testString);
450+
} else {
451+
Log.e(TAG, "verify failed @" + testString);
452+
}
453+
454+
return true;
455+
}
427456
/*
428457
* bench test using tweetnacl.c, tweetnacl.js result
429458
* */
@@ -445,7 +474,8 @@ public void run() {
445474

446475
testHash();
447476
testSign();
448-
477+
testSignDetached("ac49000da11249ea3510941703a7e21a39837c4d2d5300daebbd532df20f8135");
478+
testSignDetached("e56f0eef73ade8f79bc1d16a99cbc5e4995afd8c14adb49410ecd957aecc8d02");
449479
///testBench();
450480
} catch (UnsupportedEncodingException e) {
451481
// TODO Auto-generated catch block

0 commit comments

Comments
 (0)