Search in sources :

Example 1 with SigSpec

use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.

the class MsgSigUtil method validateSignedMessage.

public static SignedMessagePayload validateSignedMessage(SignedMessage sm, NetworkParams params) throws ValidationException {
    try {
        SignedMessagePayload payload = SignedMessagePayload.parseFrom(sm.getPayload());
        ByteString signature = sm.getSignature();
        AddressSpec claim = payload.getClaim();
        if ((claim.getRequiredSigners() != 1) || (claim.getSigSpecsCount() != 1)) {
            throw new ValidationException("Multisig not supported");
        }
        MessageDigest md = DigestUtil.getMD();
        byte[] hash = md.digest(sm.getPayload().toByteArray());
        SigSpec sig_spec = claim.getSigSpecs(0);
        if (!SignatureUtil.checkSignature(sig_spec, ByteString.copyFrom(hash), signature)) {
            throw new ValidationException("Signature match failure");
        }
        if (payload.getTimestamp() > params.getMaxClockSkewMs() + System.currentTimeMillis()) {
            throw new ValidationException("Signed message too far into future");
        }
        return payload;
    } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw new ValidationException(e);
    }
}
Also used : ValidationException(snowblossom.lib.ValidationException) ByteString(com.google.protobuf.ByteString) SignedMessagePayload(snowblossom.proto.SignedMessagePayload) SigSpec(snowblossom.proto.SigSpec) MessageDigest(java.security.MessageDigest) AddressSpec(snowblossom.proto.AddressSpec)

Example 2 with SigSpec

use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.

the class AddressUtilTest method testBasicSpecHash.

@Test
public void testBasicSpecHash() {
    AddressSpec.Builder b = AddressSpec.newBuilder();
    ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
    Random rnd = new Random();
    int keys = rnd.nextInt(100);
    int required = 0;
    if (keys > 0)
        required = rnd.nextInt(keys) + 1;
    b.setRequiredSigners(required);
    bb.putInt(required);
    bb.putInt(keys);
    for (int i = 0; i < keys; i++) {
        byte[] public_key = new byte[rnd.nextInt(800)];
        rnd.nextBytes(public_key);
        int sig_type = rnd.nextInt(12);
        bb.putInt(sig_type);
        bb.putInt(public_key.length);
        bb.put(public_key);
        SigSpec ss = SigSpec.newBuilder().setSignatureType(sig_type).setPublicKey(ByteString.copyFrom(public_key)).build();
        b.addSigSpecs(ss);
    }
    AddressSpecHash found_hash = AddressUtil.getHashForSpec(b.build(), DigestUtil.getMDAddressSpec());
    MessageDigest md = DigestUtil.getMDAddressSpec();
    md.update(bb.array(), 0, bb.position());
    byte[] h = md.digest();
    AddressSpecHash expected_hash = new AddressSpecHash(h);
    Assert.assertEquals(expected_hash, found_hash);
    System.out.println("" + found_hash + " " + expected_hash);
}
Also used : Random(java.util.Random) SigSpec(snowblossom.proto.SigSpec) MessageDigest(java.security.MessageDigest) ByteBuffer(java.nio.ByteBuffer) AddressSpec(snowblossom.proto.AddressSpec) AddressSpecHash(snowblossom.lib.AddressSpecHash) Test(org.junit.Test)

Example 3 with SigSpec

use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.

the class CipherUtilTest method testKeys.

private void testKeys(WalletKeyPair wkp) throws Exception {
    SigSpec sig_spec = SigSpec.newBuilder().setSignatureType(wkp.getSignatureType()).setPublicKey(wkp.getPublicKey()).build();
    Random rnd = new Random();
    for (int i = 0; i < 100; i++) {
        byte[] b = new byte[rnd.nextInt(100000)];
        if (i == 0)
            b = new byte[0];
        rnd.nextBytes(b);
        ByteString input = ByteString.copyFrom(b);
        ByteString output = CipherUtil.encrypt(sig_spec, input);
        ByteString output2 = CipherUtil.encrypt(sig_spec, input);
        Assert.assertTrue(output.size() >= input.size());
        Assert.assertFalse(output.equals(output2));
        ByteString dec = CipherUtil.decrypt(wkp, output);
        Assert.assertEquals(input, dec);
    }
}
Also used : Random(java.util.Random) ByteString(com.google.protobuf.ByteString) SigSpec(snowblossom.proto.SigSpec)

Example 4 with SigSpec

use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.

the class KeyUtilTest method testKeyPair.

private void testKeyPair(WalletKeyPair wkp, String name) throws Exception {
    Random rnd = new Random();
    byte[] b = new byte[Globals.BLOCKCHAIN_HASH_LEN];
    for (int i = 0; i < 8; i++) {
        rnd.nextBytes(b);
        ChainHash hash = new ChainHash(b);
        ByteString sig = SignatureUtil.sign(wkp, hash);
        SigSpec sig_spec = SigSpec.newBuilder().setSignatureType(wkp.getSignatureType()).setPublicKey(wkp.getPublicKey()).build();
        logger.info(String.format("Key report %s Pub size: %d, sig %d", name, wkp.getPublicKey().size(), sig.size()));
        logger.info("Key report: " + HexUtil.getHexString(sig));
        // logger.info("Key report: " + KeyUtil.decomposeASN1Encoded( sig ));
        Assert.assertTrue(SignatureUtil.checkSignature(sig_spec, hash.getBytes(), sig));
    }
}
Also used : ChainHash(snowblossom.lib.ChainHash) Random(java.util.Random) ByteString(com.google.protobuf.ByteString) SigSpec(snowblossom.proto.SigSpec)

Example 5 with SigSpec

use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.

the class AddressUtil method getAddressSpecTypeSummary.

public static String getAddressSpecTypeSummary(AddressSpec spec) throws ValidationException {
    String multi = String.format("%dof%d", spec.getRequiredSigners(), spec.getSigSpecsCount());
    StringBuilder algo_str = new StringBuilder();
    for (int s = 0; s < spec.getSigSpecsCount(); s++) {
        SigSpec sig = spec.getSigSpecs(s);
        String algo = SignatureUtil.getAlgo(sig.getSignatureType());
        if (s > 0)
            algo_str.append(" ");
        algo_str.append(algo);
    }
    return multi + " " + algo_str;
}
Also used : SigSpec(snowblossom.proto.SigSpec) ByteString(com.google.protobuf.ByteString)

Aggregations

SigSpec (snowblossom.proto.SigSpec)7 ByteString (com.google.protobuf.ByteString)5 Random (java.util.Random)3 MessageDigest (java.security.MessageDigest)2 AddressSpec (snowblossom.proto.AddressSpec)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Test (org.junit.Test)1 AddressSpecHash (snowblossom.lib.AddressSpecHash)1 ChainHash (snowblossom.lib.ChainHash)1 ValidationException (snowblossom.lib.ValidationException)1 SignedMessagePayload (snowblossom.proto.SignedMessagePayload)1