Search in sources :

Example 1 with SECPPublicKey

use of org.hyperledger.besu.crypto.SECPPublicKey in project besu by hyperledger.

the class ECRECPrecompiledContract method compute.

@Override
public Bytes compute(final Bytes input, final MessageFrame messageFrame) {
    final int size = input.size();
    final Bytes d = size >= 128 ? input : Bytes.wrap(input, MutableBytes.create(128 - size));
    final Bytes32 h = Bytes32.wrap(d, 0);
    // to check the rest of the bytes are zero though.
    if (!d.slice(32, 31).isZero()) {
        return Bytes.EMPTY;
    }
    final int recId = d.get(63) - V_BASE;
    final BigInteger r = d.slice(64, 32).toUnsignedBigInteger();
    final BigInteger s = d.slice(96, 32).toUnsignedBigInteger();
    final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
    final SECPSignature signature;
    try {
        signature = signatureAlgorithm.createSignature(r, s, (byte) recId);
    } catch (final IllegalArgumentException e) {
        return Bytes.EMPTY;
    }
    // the library needs to be updated.
    try {
        final Optional<SECPPublicKey> recovered = signatureAlgorithm.recoverPublicKeyFromSignature(h, signature);
        if (!recovered.isPresent()) {
            return Bytes.EMPTY;
        }
        final Bytes32 hashed = Hash.keccak256(recovered.get().getEncodedBytes());
        final MutableBytes32 result = MutableBytes32.create();
        hashed.slice(12).copyTo(result, 12);
        return result;
    } catch (final IllegalArgumentException e) {
        return Bytes.EMPTY;
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) BigInteger(java.math.BigInteger) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) MutableBytes32(org.apache.tuweni.bytes.MutableBytes32) Bytes32(org.apache.tuweni.bytes.Bytes32) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey) MutableBytes32(org.apache.tuweni.bytes.MutableBytes32)

Example 2 with SECPPublicKey

use of org.hyperledger.besu.crypto.SECPPublicKey in project besu by hyperledger.

the class GenerateBlockchainConfig method importPublicKey.

/**
 * Imports a single public key.
 *
 * @param publicKeyJson The public key.
 */
private void importPublicKey(final JsonNode publicKeyJson) {
    if (publicKeyJson.getNodeType() != JsonNodeType.STRING) {
        throw new IllegalArgumentException("Invalid key json of type: " + publicKeyJson.getNodeType());
    }
    final String publicKeyText = publicKeyJson.asText();
    try {
        final SECPPublicKey publicKey = SIGNATURE_ALGORITHM.get().createPublicKey(Bytes.fromHexString(publicKeyText));
        if (!SIGNATURE_ALGORITHM.get().isValidPublicKey(publicKey)) {
            throw new IllegalArgumentException(new StringBuilder().append(publicKeyText).append(" is not a valid public key for elliptic curve ").append(SIGNATURE_ALGORITHM.get().getCurveName()).toString());
        }
        writeKeypair(publicKey, null);
        LOG.info("Public key imported from configuration.({})", publicKey.toString());
    } catch (final IOException e) {
        LOG.error("An error occurred while trying to import node public key.", e);
    }
}
Also used : IOException(java.io.IOException) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey)

Example 3 with SECPPublicKey

use of org.hyperledger.besu.crypto.SECPPublicKey in project besu by hyperledger.

the class OperatorSubCommandTest method checkPublicKey.

private void checkPublicKey(final File dir, final SignatureAlgorithm signatureAlgorithm) throws IOException {
    String publicKeyHex = readPubFile(dir);
    String privateKeyHex = readPrivFile(dir);
    SECPPrivateKey privateKey = signatureAlgorithm.createPrivateKey(Bytes32.fromHexString(privateKeyHex));
    SECPPublicKey expectedPublicKey = signatureAlgorithm.createPublicKey(privateKey);
    assertThat(publicKeyHex).isEqualTo(expectedPublicKey.getEncodedBytes().toHexString());
}
Also used : SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey)

Example 4 with SECPPublicKey

use of org.hyperledger.besu.crypto.SECPPublicKey in project besu by hyperledger.

the class EncryptedMessage method encryptMsg.

/**
 * Encrypts a message for the specified peer using ECIES.
 *
 * @param bytes The plaintext.
 * @param remoteKey The peer's remote key.
 * @return The ciphertext.
 * @throws InvalidCipherTextException Thrown if encryption failed.
 */
public static Bytes encryptMsg(final Bytes bytes, final SECPPublicKey remoteKey) throws InvalidCipherTextException {
    // TODO: check size.
    final ECIESEncryptionEngine engine = ECIESEncryptionEngine.forEncryption(remoteKey);
    // Do the encryption.
    final Bytes encrypted = engine.encrypt(bytes);
    final Bytes iv = engine.getIv();
    final SECPPublicKey ephPubKey = engine.getEphPubKey();
    // Create the output message by concatenating the ephemeral public key (prefixed with
    // 0x04 to designate uncompressed), IV, and encrypted bytes.
    final MutableBytes answer = MutableBytes.create(1 + ECIESHandshaker.PUBKEY_LENGTH + IV_SIZE + encrypted.size());
    int offset = 0;
    // Set the first byte as 0x04 to specify it's an uncompressed key.
    answer.set(offset, (byte) 0x04);
    ephPubKey.getEncodedBytes().copyTo(answer, offset += 1);
    iv.copyTo(answer, offset += ECIESHandshaker.PUBKEY_LENGTH);
    encrypted.copyTo(answer, offset + iv.size());
    return answer;
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey)

Example 5 with SECPPublicKey

use of org.hyperledger.besu.crypto.SECPPublicKey in project besu by hyperledger.

the class EncryptedMessage method decryptMsgEIP8.

/**
 * Decrypts the ciphertext using our private key.
 *
 * @param msgBytes The ciphertext.
 * @param nodeKey Abstraction of this nodes private key & associated cryptographic operations
 * @return The plaintext.
 * @throws InvalidCipherTextException Thrown if decryption failed.
 */
public static Bytes decryptMsgEIP8(final Bytes msgBytes, final NodeKey nodeKey) throws InvalidCipherTextException {
    final SECPPublicKey ephPubKey = SIGNATURE_ALGORITHM.get().createPublicKey(msgBytes.slice(3, 64));
    // Strip off the IV to use.
    final Bytes iv = msgBytes.slice(3 + 64, IV_SIZE);
    // Extract the encrypted payload.
    final Bytes encrypted = msgBytes.slice(3 + 64 + IV_SIZE);
    // Perform the decryption.
    final ECIESEncryptionEngine decryptor = ECIESEncryptionEngine.forDecryption(nodeKey, ephPubKey, iv);
    return decryptor.decrypt(encrypted, msgBytes.slice(0, 2).toArray());
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey)

Aggregations

SECPPublicKey (org.hyperledger.besu.crypto.SECPPublicKey)14 Bytes (org.apache.tuweni.bytes.Bytes)7 MutableBytes (org.apache.tuweni.bytes.MutableBytes)7 Bytes32 (org.apache.tuweni.bytes.Bytes32)5 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)4 BigInteger (java.math.BigInteger)2 MutableBytes32 (org.apache.tuweni.bytes.MutableBytes32)2 SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)2 IOException (java.io.IOException)1 Nonnull (javax.annotation.Nonnull)1 SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)1 Address (org.hyperledger.besu.datatypes.Address)1 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)1 BytesValueRLPInput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)1 RLPInput (org.hyperledger.besu.ethereum.rlp.RLPInput)1 Before (org.junit.Before)1