Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algorand.Crypto.KeyPair(byte[] privateKey) constructor bug #19

Open
oysterpack opened this issue Feb 18, 2024 · 1 comment
Open

Algorand.Crypto.KeyPair(byte[] privateKey) constructor bug #19

oysterpack opened this issue Feb 18, 2024 · 1 comment
Assignees

Comments

@oysterpack
Copy link

oysterpack commented Feb 18, 2024

public KeyPair(byte[] privateKey)
{
    if (privateKey.Length != SK_SIZE)
        throw new ArgumentException("Incorrect private key length");

    ClearTextPrivateKey= privateKey;

    ed25519PrivateKey = new Ed25519PrivateKeyParameters(ClearTextPrivateKey, 0);
    ed25519PublicKey = ed25519PrivateKey.GeneratePublicKey();
    Pair = new AsymmetricCipherKeyPair(ed25519PrivateKey, ed25519PublicKey); // BUG - parameters are specified in the wrong order - public key comes first
}

The fix is a 1-line code change:

Pair = new AsymmetricCipherKeyPair(ed25519PublicKey, ed25519PrivateKey); 

Simple unit test to reproduce the issue:

[Fact]
public void NewKeyPairFromPrivateKey()
{
    var keyPair1 = new KeyPair(SecureRandom.GetInstance("SHA256PRNG"));
    var keyPair2 = new KeyPair(keyPair1.ClearTextPrivateKey); // THROWS EXCEPTION
    Assert.Equal(keyPair1.ToMnemonic(), keyPair2.ToMnemonic());
}

The above unit test fails with the following exception thrown:

System.ArgumentException: Expected a public key (Parameter 'publicParameter')

System.ArgumentException
Expected a public key (Parameter 'publicParameter')
   at Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair..ctor(AsymmetricKeyParameter publicParameter, AsymmetricKeyParameter privateParameter)
   at Algorand.Crypto.KeyPair..ctor(Byte[] privateKey)
@oysterpack oysterpack changed the title Algorand.Crypto.KeyPair(byte[] privateKey) constructor Algorand.Crypto.KeyPair(byte[] privateKey) constructor bug Feb 18, 2024
@FrankSzendzielarz
Copy link
Owner

OK thx will implement this fix shortly. Strange that I overlooked or didn't receive this notification until now.

@FrankSzendzielarz FrankSzendzielarz self-assigned this Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants