Search in sources :

Example 1 with KeyStoreData

use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.

the class KeyStoreTest method encryptWithKdfAndCipherFunction.

@ParameterizedTest
@MethodSource("encryptWithKdfAndCipherArguments")
void encryptWithKdfAndCipherFunction(final KdfParam kdfParam, final Bytes expectedChecksum, final Bytes encryptedCipherMessage) {
    final KeyStoreData keyStoreData = KeyStore.encrypt(BLS_PRIVATE_KEY, BLS_PUB_KEY, PASSWORD, "", kdfParam, CIPHER);
    assertThat(keyStoreData.getCrypto().getChecksum().getMessage()).isEqualTo(expectedChecksum);
    assertThat(keyStoreData.getCrypto().getCipher().getMessage()).isEqualTo(encryptedCipherMessage);
    assertThat(keyStoreData.getVersion()).isEqualTo(KeyStoreData.KEYSTORE_VERSION);
    assertThat(keyStoreData.getPubkey()).isEqualTo(BLS_PUB_KEY);
    assertThat(keyStoreData.getUuid()).isNotNull();
}
Also used : KeyStoreData(tech.pegasys.signers.bls.keystore.model.KeyStoreData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with KeyStoreData

use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.

the class KeyStoreTest method encryptSaveAndReloadKeyStore.

private void encryptSaveAndReloadKeyStore(final Path tempDir, final KdfParam kdfParam) throws IOException {
    final KeyStoreData keyStoreData = KeyStore.encrypt(BLS_PRIVATE_KEY, BLS_PUB_KEY, PASSWORD, "", kdfParam, CIPHER);
    final Path tempKeyStoreFile = Files.createTempFile(tempDir, "keystore", ".json");
    assertThatCode(() -> KeyStoreLoader.saveToFile(tempKeyStoreFile, keyStoreData)).doesNotThrowAnyException();
    // reload it back
    final KeyStoreData loadedKeyStore = KeyStoreLoader.loadFromFile(tempKeyStoreFile);
    assertThat(loadedKeyStore.getUuid()).isEqualByComparingTo(keyStoreData.getUuid());
    assertThat(loadedKeyStore.getCrypto().getChecksum().getMessage()).isEqualTo(keyStoreData.getCrypto().getChecksum().getMessage());
}
Also used : Path(java.nio.file.Path) KeyStoreData(tech.pegasys.signers.bls.keystore.model.KeyStoreData)

Example 3 with KeyStoreData

use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.

the class KeyStoreTest method invalidPasswordValidation.

private void invalidPasswordValidation(final String resourcePath) {
    final KeyStoreData keyStoreData = loadKeyStoreFromResource(resourcePath);
    assertThat(KeyStore.validatePassword("invalidpassword", keyStoreData)).isFalse();
}
Also used : KeyStoreData(tech.pegasys.signers.bls.keystore.model.KeyStoreData)

Example 4 with KeyStoreData

use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.

the class KeyStore method encrypt.

/**
 * Encrypt the given BLS12-381 key with specified password.
 *
 * @param blsPrivateKey BLS12-381 private key in Bytes to encrypt. It is not validated to be a
 *     valid BLS12-381 key.
 * @param blsPublicKey BLS12-381 public key in Bytes. It is not validated and stored as it is.
 * @param password The password to use for encryption
 * @param path Path as defined in EIP-2334. Can be empty String.
 * @param kdfParam crypto function such as scrypt or PBKDF2 and related parameters such as dklen,
 *     salt etc.
 * @param cipher cipher function and iv parameter to use.
 * @return The constructed KeyStore with encrypted BLS Private Key as cipher.message and other
 *     details as defined by the EIP-2335 standard.
 */
public static KeyStoreData encrypt(final Bytes blsPrivateKey, final Bytes blsPublicKey, final String password, final String path, final KdfParam kdfParam, final Cipher cipher) {
    checkNotNull(blsPrivateKey, "PrivateKey cannot be null");
    checkNotNull(blsPublicKey, "PublicKey cannot be null");
    checkNotNull(password, "Password cannot be null");
    checkNotNull(path, "Path cannot be null");
    checkNotNull(kdfParam, "KDFParam cannot be null");
    checkNotNull(cipher, "Cipher cannot be null");
    kdfParam.validate();
    cipher.validate();
    final Crypto crypto = encryptUsingCipherFunction(blsPrivateKey, password, kdfParam, cipher);
    return new KeyStoreData(crypto, blsPublicKey, path);
}
Also used : Crypto(tech.pegasys.signers.bls.keystore.model.Crypto) KeyStoreData(tech.pegasys.signers.bls.keystore.model.KeyStoreData)

Example 5 with KeyStoreData

use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.

the class KeyStoreLoader method loadFromString.

public static KeyStoreData loadFromString(final String keystoreString) {
    try {
        final KeyStoreData keyStoreData = OBJECT_MAPPER.readValue(keystoreString, KeyStoreData.class);
        keyStoreData.validate();
        return keyStoreData;
    } catch (final JsonParseException e) {
        throw new KeyStoreValidationException("Invalid KeyStore: " + e.getMessage(), e);
    } catch (final JsonMappingException e) {
        throw convertToKeyStoreValidationException(e);
    } catch (final IOException e) {
        LOG.error("Unexpected IO error while reading KeyStore: " + e.getMessage());
        throw new KeyStoreValidationException("Unexpected IO error while reading KeyStore: " + e.getMessage(), e);
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) KeyStoreData(tech.pegasys.signers.bls.keystore.model.KeyStoreData) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

KeyStoreData (tech.pegasys.signers.bls.keystore.model.KeyStoreData)29 IOException (java.io.IOException)9 Bytes (org.apache.tuweni.bytes.Bytes)8 Path (java.nio.file.Path)7 KeyStoreValidationException (tech.pegasys.signers.bls.keystore.KeyStoreValidationException)5 Cipher (tech.pegasys.signers.bls.keystore.model.Cipher)5 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)5 KdfParam (tech.pegasys.signers.bls.keystore.model.KdfParam)4 Test (org.junit.jupiter.api.Test)3 Pbkdf2Param (tech.pegasys.signers.bls.keystore.model.Pbkdf2Param)3 SCryptParam (tech.pegasys.signers.bls.keystore.model.SCryptParam)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 FileNotFoundException (java.io.FileNotFoundException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Files (java.nio.file.Files)2 AbstractMap (java.util.AbstractMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2