Search in sources :

Example 6 with Signature

use of io.nem.symbol.core.crypto.Signature in project nem2-sdk-java by nemtech.

the class MathUtils method sign.

/**
 * Creates a signature from a key pair and message.
 *
 * @param keyPair The key pair.
 * @param data The message.
 * @return The signature.
 */
public static Signature sign(final KeyPair keyPair, final byte[] data) {
    Hasher hasher64 = Hashes::sha512;
    Hasher hasher32 = Hashes::sha512;
    final byte[] hash = hasher32.hash(keyPair.getPrivateKey().getBytes());
    final byte[] a = Arrays.copyOfRange(hash, 0, 32);
    a[31] &= 0x7F;
    a[31] |= 0x40;
    a[0] &= 0xF8;
    final Ed25519EncodedFieldElement r = new Ed25519EncodedFieldElement(hasher64.hash(Arrays.copyOfRange(hash, 32, 64), data));
    final Ed25519EncodedFieldElement rReduced = reduceModGroupOrder(r);
    final Ed25519GroupElement R = scalarMultiplyGroupElement(Ed25519Group.BASE_POINT, toFieldElement(toBigInteger(rReduced)));
    final Ed25519EncodedFieldElement h = new Ed25519EncodedFieldElement(hasher64.hash(R.encode().getRaw(), keyPair.getPublicKey().getBytes(), data));
    final Ed25519EncodedFieldElement hReduced = reduceModGroupOrder(h);
    final BigInteger S = toBigInteger(rReduced).add(toBigInteger(hReduced).multiply(toBigInteger(a))).mod(Ed25519Group.GROUP_ORDER);
    return new Signature(R.encode().getRaw(), toByteArray(S));
}
Also used : Hasher(io.nem.symbol.core.crypto.Hasher) Signature(io.nem.symbol.core.crypto.Signature) BigInteger(java.math.BigInteger)

Example 7 with Signature

use of io.nem.symbol.core.crypto.Signature in project nem2-sdk-java by nemtech.

the class Transaction method signWith.

/**
 * Serialize and sign transaction creating a new SignedTransaction.
 *
 * @param account The account to sign the transaction.
 * @param generationHash The generation hash for the network.
 * @return {@link SignedTransaction}
 */
public SignedTransaction signWith(final Account account, final String generationHash) {
    final DsaSigner theSigner = CryptoEngines.defaultEngine().createDsaSigner(account.getKeyPair());
    final byte[] bytes = this.serialize();
    final byte[] generationHashBytes = ConvertUtils.getBytes(generationHash);
    final byte[] signingBytes = getSignBytes(bytes, generationHashBytes);
    final Signature theSignature = theSigner.sign(signingBytes);
    final byte[] payload = new byte[bytes.length];
    // Size
    System.arraycopy(bytes, 0, payload, 0, 8);
    System.arraycopy(theSignature.getBytes(), 0, payload, 8, // Signature
    theSignature.getBytes().length);
    System.arraycopy(account.getKeyPair().getPublicKey().getBytes(), 0, payload, 64 + 8, // Signer
    account.getKeyPair().getPublicKey().getBytes().length);
    System.arraycopy(bytes, 104, payload, 104, bytes.length - 104);
    final String hash = createTransactionHash(ConvertUtils.toHex(payload), generationHashBytes);
    return new SignedTransaction(account.getPublicAccount(), ConvertUtils.toHex(payload), hash, type);
}
Also used : DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature)

Example 8 with Signature

use of io.nem.symbol.core.crypto.Signature in project nem2-sdk-java by nemtech.

the class Ed25519DsaSigner method makeSignatureCanonical.

@Override
public Signature makeSignatureCanonical(final Signature signature) {
    final Ed25519EncodedFieldElement s = new Ed25519EncodedFieldElement(Arrays.copyOf(signature.getBinaryS(), 64));
    final Ed25519EncodedFieldElement sModQ = s.modQ();
    return new Signature(signature.getBinaryR(), sModQ.getRaw());
}
Also used : Signature(io.nem.symbol.core.crypto.Signature) Ed25519EncodedFieldElement(io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519EncodedFieldElement)

Example 9 with Signature

use of io.nem.symbol.core.crypto.Signature in project nem2-sdk-java by nemtech.

the class Ed25519DsaSignerTest method isCanonicalReturnsFalseForNonCanonicalSignature.

@Test
public void isCanonicalReturnsFalseForNonCanonicalSignature() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair kp = KeyPair.random(engine);
    final DsaSigner dsaSigner = this.getDsaSigner(kp);
    final byte[] input = RandomUtils.generateRandomBytes();
    // Act:
    final Signature signature = dsaSigner.sign(input);
    final BigInteger nonCanonicalS = engine.getCurve().getGroupOrder().add(signature.getS());
    final Signature nonCanonicalSignature = new Signature(signature.getR(), nonCanonicalS);
    // Assert:
    Assertions.assertFalse(dsaSigner.isCanonicalSignature(nonCanonicalSignature));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature) BigInteger(java.math.BigInteger) DsaSignerTest(io.nem.symbol.core.crypto.DsaSignerTest) Test(org.junit.jupiter.api.Test)

Example 10 with Signature

use of io.nem.symbol.core.crypto.Signature in project nem2-sdk-java by nemtech.

the class Ed25519DsaSignerTest method signReturnsVerifiableSignature.

@Test
public void signReturnsVerifiableSignature() {
    // Arrange:
    final CryptoEngine engine = this.getCryptoEngine();
    final KeyPair keyPair = KeyPair.random(engine);
    for (int i = 0; i < 20; i++) {
        final DsaSigner dsaSigner = this.getDsaSigner(keyPair);
        final byte[] input = RandomUtils.generateRandomBytes();
        // Act:
        final Signature signature1 = dsaSigner.sign(input);
        // Assert:
        Assertions.assertTrue(dsaSigner.verify(input, signature1));
    }
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature) DsaSignerTest(io.nem.symbol.core.crypto.DsaSignerTest) Test(org.junit.jupiter.api.Test)

Aggregations

Signature (io.nem.symbol.core.crypto.Signature)11 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)7 DsaSigner (io.nem.symbol.core.crypto.DsaSigner)7 DsaSignerTest (io.nem.symbol.core.crypto.DsaSignerTest)7 KeyPair (io.nem.symbol.core.crypto.KeyPair)7 Test (org.junit.jupiter.api.Test)7 BigInteger (java.math.BigInteger)4 Hasher (io.nem.symbol.core.crypto.Hasher)2 Ed25519EncodedFieldElement (io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519EncodedFieldElement)2 CryptoException (io.nem.symbol.core.crypto.CryptoException)1 PublicKey (io.nem.symbol.core.crypto.PublicKey)1 Ed25519EncodedGroupElement (io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519EncodedGroupElement)1 Ed25519GroupElement (io.nem.symbol.core.crypto.ed25519.arithmetic.Ed25519GroupElement)1