xxx
This commit is contained in:
parent
70b349099a
commit
325b6d4961
|
@ -1,7 +1,5 @@
|
|||
package org.tcpid.key;
|
||||
|
||||
import static org.libsodium.jni.NaCl.sodium;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.libsodium.jni.crypto.Hash;
|
||||
|
|
|
@ -54,48 +54,54 @@ public class OPRETECParser extends OPRETBaseHandler {
|
|||
}
|
||||
}
|
||||
|
||||
private static byte[] doubleDec(final PublicKey MK, final PublicKey VK, final byte[] cipher, final byte[] nonce) {
|
||||
private static byte[] doubleDec(final PublicKey MK, final PublicKey VK, final byte[] cipher) {
|
||||
|
||||
final KeyPair Epair = new KeyPair(HMACSHA512256.of(MK.toBytes(), Bytes.concat(nonce, VK.toBytes())));
|
||||
final KeyPair Epair = new KeyPair(HMACSHA512256.of(VK.toBytes(), MK.toBytes()));
|
||||
|
||||
final Box boxVK = new Box(VK, Epair.getPrivateKey());
|
||||
|
||||
final byte[] nonceVK = Arrays.copyOfRange(
|
||||
HMACSHA512256.of(VK.toBytes(), Bytes.concat(nonce, Epair.getPrivateKey().toBytes())), 0, NONCE_BYTES);
|
||||
final byte[] nonceVK = Arrays.copyOfRange(HMACSHA512256.of(Epair.getPublicKey().toBytes(),
|
||||
Bytes.concat(Epair.getPrivateKey().toBytes(), VK.toBytes())), 0, NONCE_BYTES);
|
||||
|
||||
final byte[] cipherMK = boxVK.decrypt(nonceVK, cipher);
|
||||
|
||||
final Box boxMK = new Box(MK, Epair.getPrivateKey());
|
||||
|
||||
final byte[] nonceMK = Arrays.copyOfRange(
|
||||
HMACSHA512256.of(MK.toBytes(), Bytes.concat(nonce, Epair.getPrivateKey().toBytes())), 0, NONCE_BYTES);
|
||||
final byte[] nonceMK = Arrays.copyOfRange(HMACSHA512256.of(Epair.getPublicKey().toBytes(),
|
||||
Bytes.concat(Epair.getPrivateKey().toBytes(), MK.toBytes())), 0, NONCE_BYTES);
|
||||
|
||||
final byte[] clear = boxMK.decrypt(nonceMK, cipherMK);
|
||||
|
||||
System.err.println("cipher = " + Utils.HEX.encode(cipher));
|
||||
System.err.println("reverse = " + Utils.HEX.encode(boxVK.encrypt(nonceVK, boxMK.encrypt(nonceMK, clear))));
|
||||
|
||||
return clear;
|
||||
}
|
||||
|
||||
private static byte[] doubleEnc(final KeyPair MKpair, final KeyPair VKpair, final byte[] clear,
|
||||
final byte[] nonce) {
|
||||
private static byte[] doubleEnc(final KeyPair MKpair, final KeyPair VKpair, final byte[] clear) {
|
||||
|
||||
final KeyPair Epair = new KeyPair(HMACSHA512256.of(MKpair.getPublicKey().toBytes(),
|
||||
Bytes.concat(nonce, VKpair.getPublicKey().toBytes())));
|
||||
final KeyPair Epair = new KeyPair(
|
||||
HMACSHA512256.of(VKpair.getPublicKey().toBytes(), MKpair.getPublicKey().toBytes()));
|
||||
|
||||
final Box boxMK = new Box(Epair.getPublicKey(), MKpair.getPrivateKey());
|
||||
|
||||
final byte[] nonceMK = Arrays.copyOfRange(
|
||||
HMACSHA512256.of(MKpair.getPublicKey().toBytes(), Bytes.concat(nonce, Epair.getPrivateKey().toBytes())),
|
||||
HMACSHA512256.of(Epair.getPublicKey().toBytes(),
|
||||
Bytes.concat(Epair.getPrivateKey().toBytes(), MKpair.getPublicKey().toBytes())),
|
||||
0, NONCE_BYTES);
|
||||
|
||||
final byte[] cipherMK = boxMK.encrypt(nonceMK, clear);
|
||||
System.err.println("cipherMK len = " + cipherMK.length);
|
||||
|
||||
final Box boxVK = new Box(Epair.getPublicKey(), VKpair.getPrivateKey());
|
||||
|
||||
final byte[] nonceVK = Arrays.copyOfRange(
|
||||
HMACSHA512256.of(VKpair.getPublicKey().toBytes(), Bytes.concat(nonce, Epair.getPrivateKey().toBytes())),
|
||||
HMACSHA512256.of(Epair.getPublicKey().toBytes(),
|
||||
Bytes.concat(Epair.getPrivateKey().toBytes(), VKpair.getPublicKey().toBytes())),
|
||||
0, NONCE_BYTES);
|
||||
|
||||
final byte[] cipherVK = boxVK.encrypt(nonceVK, cipherMK);
|
||||
System.err.println("cipherVK len = " + cipherVK.length);
|
||||
|
||||
return cipherVK;
|
||||
}
|
||||
|
@ -174,7 +180,7 @@ public class OPRETECParser extends OPRETBaseHandler {
|
|||
HMACSHA512256.HMACSHA512256_BYTES);
|
||||
return false;
|
||||
}
|
||||
// TODO: test assert(HMACSHA512_KEYBYTES == SECRETKEY_BYTES)
|
||||
|
||||
if (SECRETKEY_BYTES != HMACSHA512256.HMACSHA512256_BYTES) {
|
||||
logger.error("SECRETKEY_BYTES != HMACSHA512256.HMACSHA512256_BYTES: {} > {}", SECRETKEY_BYTES,
|
||||
HMACSHA512256.HMACSHA512256_BYTES);
|
||||
|
@ -199,11 +205,13 @@ public class OPRETECParser extends OPRETBaseHandler {
|
|||
|
||||
final KeyPair mkpair = msk.getKeyPair();
|
||||
final KeyPair vkpair = subkey.getKeyPair();
|
||||
final byte[] nonce = Arrays.copyOfRange(HASH.sha256("TEST".getBytes()), 0, 8);
|
||||
final byte[] cipher = doubleEnc(mkpair, vkpair, "TEST".getBytes(), nonce);
|
||||
final VerifyKey nvk = msk.getNextValidSubKey(1L).getVerifyKey();
|
||||
final byte[] cipher = doubleEnc(mkpair, vkpair, nvk.toBytes());
|
||||
try {
|
||||
final byte[] chk = doubleDec(msk.getVerifyKey().getPublicKey(), vk.getPublicKey(), cipher, nonce);
|
||||
return Arrays.equals(chk, "TEST".getBytes());
|
||||
final byte[] chk = doubleDec(msk.getVerifyKey().getPublicKey(), vk.getPublicKey(), cipher);
|
||||
System.err.println("cipher len = " + cipher.length);
|
||||
System.err.println("cipher len = " + Utils.HEX.encode(cipher));
|
||||
return Arrays.equals(chk, nvk.toBytes());
|
||||
} catch (final Exception e) {
|
||||
logger.error("doubleEnc -> doubleDec failed!");
|
||||
return false;
|
||||
|
|
Loading…
Reference in a new issue