Search in sources :

Example 1 with UninitializedWalletException

use of com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException in project popstellar by dedis.

the class Wallet method generateKeyFromPath.

/**
 * Generate a PoPToken (i.e. a key pair) from a given path.
 *
 * @param path a String path of the form: m/i/j/k/... where i,j,k,.. are 31-bit integer.
 * @return the generated PoP Token
 * @throws KeyGenerationException if an error occurs
 * @throws UninitializedWalletException if the wallet is not initialized with a seed
 */
private PoPToken generateKeyFromPath(@NonNull String path) throws KeyGenerationException, UninitializedWalletException {
    if (!isSetUp()) {
        throw new UninitializedWalletException();
    }
    // convert the path string in an array of int
    int[] pathValueInt = Arrays.stream(path.split("/")).skip(// remove the first element ('m')
    1).mapToInt(Integer::parseInt).toArray();
    try {
        // derive private and public key
        byte[] privateKey = SLIP10.deriveEd25519PrivateKey(aead.decrypt(seed, new byte[0]), pathValueInt);
        Ed25519PrivateKeyParameters prK = new Ed25519PrivateKeyParameters(privateKey, 0);
        Ed25519PublicKeyParameters puK = prK.generatePublicKey();
        byte[] publicKey = puK.getEncoded();
        return new PoPToken(privateKey, publicKey);
    } catch (GeneralSecurityException e) {
        throw new KeyGenerationException(e);
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) GeneralSecurityException(java.security.GeneralSecurityException) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 2 with UninitializedWalletException

use of com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException in project popstellar by dedis.

the class KeyManagerTest method popTokenRetrievingFailsWhenWalletFails.

@Test
public void popTokenRetrievingFailsWhenWalletFails() throws KeyException {
    PoPToken token = Base64DataUtils.generatePoPToken();
    // create LAO and RollCall
    Lao lao = new Lao("lao", Base64DataUtils.generatePublicKey(), 54213424);
    RollCall rollCall = new RollCall(lao.getId(), 5421364, "rollcall");
    lao.updateRollCall(rollCall.getId(), rollCall);
    KeyManager manager = new KeyManager(androidKeysetManager, wallet);
    // Test with every possible errors
    when(wallet.recoverKey(any(), any(), any())).thenThrow(new KeyGenerationException(new GeneralSecurityException()));
    assertThrows(KeyGenerationException.class, () -> manager.getValidPoPToken(lao));
    verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
    reset(wallet);
    when(wallet.recoverKey(any(), any(), any())).thenThrow(new UninitializedWalletException());
    assertThrows(UninitializedWalletException.class, () -> manager.getValidPoPToken(lao));
    verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
    reset(wallet);
    when(wallet.recoverKey(any(), any(), any())).thenThrow(new InvalidPoPTokenException(token));
    assertThrows(InvalidPoPTokenException.class, () -> manager.getValidPoPToken(lao));
    verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) InvalidPoPTokenException(com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException) GeneralSecurityException(java.security.GeneralSecurityException) RollCall(com.github.dedis.popstellar.model.objects.RollCall) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) Lao(com.github.dedis.popstellar.model.objects.Lao) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) Test(org.junit.Test) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest)

Aggregations

PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)2 KeyGenerationException (com.github.dedis.popstellar.utility.error.keys.KeyGenerationException)2 UninitializedWalletException (com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Lao (com.github.dedis.popstellar.model.objects.Lao)1 RollCall (com.github.dedis.popstellar.model.objects.RollCall)1 InvalidPoPTokenException (com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException)1 HiltAndroidTest (dagger.hilt.android.testing.HiltAndroidTest)1 Ed25519PrivateKeyParameters (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)1 Ed25519PublicKeyParameters (org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)1 Test (org.junit.Test)1