Skip to content

Commit

Permalink
test(ffi): cover key generation (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit authored Oct 6, 2023
1 parent 3119d05 commit 67277e6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
24 changes: 24 additions & 0 deletions ffi/dotnet/Devolutions.Picky.Tests/KeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,28 @@ public void PublicToAndFromPkcs1Smoke()

Assert.Equal(key.ToDer(), key2.ToDer());
}

[Fact]
public void GenerateRsa()
{
PrivateKey key = PrivateKey.GenerateRsa(2048);
Assert.Equal(KeyKind.Rsa, key.Kind);
key.ToPem().ToRepr();
}

[Fact]
public void GenerateEd()
{
PrivateKey key = PrivateKey.GenerateEd(EdAlgorithm.Ed25519, false);
Assert.Equal(KeyKind.Ed, key.Kind);
key.ToPem().ToRepr();
}

[Fact]
public void GenerateEc()
{
PrivateKey key = PrivateKey.GenerateEc(EcCurve.NistP384);
Assert.Equal(KeyKind.Ec, key.Kind);
key.ToPem().ToRepr();
}
}
7 changes: 7 additions & 0 deletions ffi/dotnet/Devolutions.Picky/Generated/PrivateKey.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ffi/dotnet/Devolutions.Picky/Generated/RawPrivateKey.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions ffi/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ pub mod ffi {
Ok(Box::new(PrivateKey(key)))
}

// Generates new ed key pair with specified supported algorithm.
// `write_public_key` specifies whether to include public key in the private key file.
// Note that OpenSSL does not support ed keys with public key included.
/// Generates new ed key pair with specified supported algorithm.
///
/// `write_public_key` specifies whether to include public key in the private key file.
/// Note that OpenSSL does not support ed keys with public key included.
pub fn generate_ed(algorithm: EdAlgorithm, write_public_key: bool) -> Result<Box<PrivateKey>, Box<PickyError>> {
let key = picky::key::PrivateKey::generate_ed(algorithm.into(), write_public_key)?;
Ok(Box::new(PrivateKey(key)))
Expand Down
1 change: 1 addition & 0 deletions ffi/wasm/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl PrivateKey {
}

/// Generates new ed key pair with specified supported algorithm.
///
/// `write_public_key` specifies whether to include public key in the private key file.
/// Note that OpenSSL does not support ed keys with public key included.
pub fn generate_ed(algorithm: EdAlgorithm, write_public_key: bool) -> Result<PrivateKey, KeyError> {
Expand Down
9 changes: 5 additions & 4 deletions picky/src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,10 @@ impl PrivateKey {
Ok(Self { kind, inner })
}

// Generates new ed key pair with specified supported algorithm.
// `write_public_key` specifies whether to include public key in the private key file.
// Note that OpenSSL does not support ed keys with public key included.
/// Generates new ed key pair with specified supported algorithm.
///
/// `write_public_key` specifies whether to include public key in the private key file.
/// Note that OpenSSL does not support ed keys with public key included.
pub fn generate_ed(algorithm: EdAlgorithm, write_public_key: bool) -> Result<Self, KeyError> {
use rand::rngs::OsRng;

Expand Down Expand Up @@ -1122,10 +1123,10 @@ mod tests {
ring::signature::EcdsaKeyPair::from_pkcs8(signing_alg, &pkcs8).unwrap();
}

#[test]
// Read from x25519 keys is not supported in `ring`, because it is mainly used for key
// exchange for which key serialization/deserialization is not needed at all. But we support,
// just to be consistent with OpenSSL and RFC https://www.rfc-editor.org/rfc/rfc8410
#[test]
fn ring_understands_picky_pkcs8_ed25519() {
// Make sure we're generating pkcs8 understood by the `ring` crate.
// `ring` is very specific about the format of the ED25519 private key, and in contrast
Expand Down

0 comments on commit 67277e6

Please sign in to comment.