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;
}
}
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);
}
}
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());
}
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;
}
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());
}
Aggregations