Skip to content

Commit ce93f94

Browse files
committed
Merge pull request #12 from AndSDev/feature_Signature.detached
Signature.detached() and Signature.detached_verify()
2 parents 6382765 + 9c4712e commit ce93f94

File tree

4 files changed

+99
-15
lines changed

4 files changed

+99
-15
lines changed

src/com/iwebpp/crypto/TweetNacl.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,11 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
695695
* Signs the message using the secret key and returns a signature.
696696
* */
697697
public byte [] detached(byte [] message) {
698-
699-
return null;
698+
byte[] signedMsg = this.sign(message);
699+
byte[] sig = new byte[signatureLength];
700+
for (int i = 0; i < sig.length; i++)
701+
sig[i] = signedMsg[i];
702+
return sig;
700703
}
701704

702705
/*
@@ -705,8 +708,17 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
705708
* returns true if verification succeeded or false if it failed.
706709
* */
707710
public boolean detached_verify(byte [] message, byte [] signature) {
708-
709-
return false;
711+
if (signature.length != signatureLength)
712+
return false;
713+
if (theirPublicKey.length != publicKeyLength)
714+
return false;
715+
byte [] sm = new byte[signatureLength + message.length];
716+
byte [] m = new byte[signatureLength + message.length];
717+
for (int i = 0; i < signatureLength; i++)
718+
sm[i] = signature[i];
719+
for (int i = 0; i < message.length; i++)
720+
sm[i + signatureLength] = message[i];
721+
return (crypto_sign_open(m, -1, sm, sm.length, theirPublicKey) >= 0);
710722
}
711723

712724
/*
@@ -2287,7 +2299,7 @@ private static int unpackneg(long [] r[], byte p[])
22872299
M(chk,0,chk.length, chk,0,chk.length, den,0,den.length);
22882300
if (neq25519(chk, num)!=0) return -1;
22892301

2290-
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);
22912303

22922304
M(r[3],0,r[3].length, r[0],0,r[0].length, r[1],0,r[1].length);
22932305
return 0;

src/com/iwebpp/crypto/TweetNaclFast.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,11 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
762762
* Signs the message using the secret key and returns a signature.
763763
* */
764764
public byte [] detached(byte [] message) {
765-
766-
return null;
765+
byte[] signedMsg = this.sign(message);
766+
byte[] sig = new byte[signatureLength];
767+
for (int i = 0; i < sig.length; i++)
768+
sig[i] = signedMsg[i];
769+
return sig;
767770
}
768771

769772
/*
@@ -772,8 +775,17 @@ public Signature(byte [] theirPublicKey, byte [] mySecretKey) {
772775
* returns true if verification succeeded or false if it failed.
773776
* */
774777
public boolean detached_verify(byte [] message, byte [] signature) {
775-
776-
return false;
778+
if (signature.length != signatureLength)
779+
return false;
780+
if (theirPublicKey.length != publicKeyLength)
781+
return false;
782+
byte [] sm = new byte[signatureLength + message.length];
783+
byte [] m = new byte[signatureLength + message.length];
784+
for (int i = 0; i < signatureLength; i++)
785+
sm[i] = signature[i];
786+
for (int i = 0; i < message.length; i++)
787+
sm[i + signatureLength] = message[i];
788+
return (crypto_sign_open(m, -1, sm, 0, sm.length, theirPublicKey) >= 0);
777789
}
778790

779791
/*
@@ -3250,7 +3262,7 @@ private static int unpackneg(long [] r[], byte p[])
32503262
M(chk, chk, den);
32513263
if (neq25519(chk, num)!=0) return -1;
32523264

3253-
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]);
32543266

32553267
M(r[3], r[0], r[1]);
32563268

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)