From f572bc851d520a56eebb0a6cea38144051168a8a Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 25 Mar 2019 15:22:56 +0100 Subject: openpgp-ffi: Wrap crypto::SessionKey and crypto::Password. --- ffi/Cargo.toml | 2 +- ffi/src/lib.rs | 1 + openpgp-ffi/Cargo.toml | 2 +- openpgp-ffi/include/sequoia/openpgp/crypto.h | 58 ++++++++++++++++++++++++++++ openpgp-ffi/include/sequoia/openpgp/types.h | 14 +++++++ openpgp-ffi/src/crypto.rs | 48 ++++++++++++++++++++++- openpgp-ffi/src/lib.rs | 1 + 7 files changed, 122 insertions(+), 4 deletions(-) diff --git a/ffi/Cargo.toml b/ffi/Cargo.toml index d49b1bbf..833757d3 100644 --- a/ffi/Cargo.toml +++ b/ffi/Cargo.toml @@ -31,11 +31,11 @@ lazy_static = "1.0.0" libc = "0.2.33" memsec = "0.5.4" native-tls = "0.2.0" +nettle = "5.0" time = "0.1.40" [dev-dependencies] filetime = "0.2" -nettle = "5.0" [lib] crate-type = ["cdylib", "staticlib"] diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index eb539cae..1a68d87e 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -113,6 +113,7 @@ extern crate lazy_static; extern crate libc; extern crate native_tls; extern crate memsec; +extern crate nettle; extern crate sequoia_ffi_macros; use sequoia_ffi_macros::{ diff --git a/openpgp-ffi/Cargo.toml b/openpgp-ffi/Cargo.toml index 9cb1d4ef..df891875 100644 --- a/openpgp-ffi/Cargo.toml +++ b/openpgp-ffi/Cargo.toml @@ -27,11 +27,11 @@ failure = "0.1.2" lazy_static = "1.0.0" libc = "0.2.33" memsec = "0.5.4" +nettle = "5.0" time = "0.1.40" [dev-dependencies] filetime = "0.2" -nettle = "5.0" [lib] crate-type = ["lib", "cdylib", "staticlib"] diff --git a/openpgp-ffi/include/sequoia/openpgp/crypto.h b/openpgp-ffi/include/sequoia/openpgp/crypto.h index 6db828ef..44e035dc 100644 --- a/openpgp-ffi/include/sequoia/openpgp/crypto.h +++ b/openpgp-ffi/include/sequoia/openpgp/crypto.h @@ -3,6 +3,64 @@ #include +/*/ +/// Creates a new session key. +/*/ +pgp_session_key_t pgp_session_key_new (size_t size); + +/*/ +/// Creates a new session key from a buffer. +/*/ +pgp_session_key_t pgp_session_key_from_bytes (uint8_t *buf, size_t size); + +/*/ +/// Frees a session key. +/*/ +void pgp_session_key_free (pgp_session_key_t); + +/*/ +/// Returns a human readable description of this object suitable for +/// debugging. +/*/ +char *pgp_session_key_debug (const pgp_session_key_t fp); + +/*/ +/// Clones the session key. +/*/ +pgp_session_key_t pgp_session_key_clone (pgp_session_key_t session_key); + +/*/ +/// Compares session keys. +/*/ +int pgp_session_key_equal (const pgp_session_key_t a, + const pgp_session_key_t b); + +/*/ +/// Creates a new password from a buffer. +/*/ +pgp_password_t pgp_password_from_bytes (uint8_t *buf, size_t size); + +/*/ +/// Frees a password. +/*/ +void pgp_password_free (pgp_password_t); + +/*/ +/// Returns a human readable description of this object suitable for +/// debugging. +/*/ +char *pgp_password_debug (const pgp_password_t fp); + +/*/ +/// Clones the password. +/*/ +pgp_password_t pgp_password_clone (pgp_password_t password); + +/*/ +/// Compares passwords. +/*/ +int pgp_password_equal (const pgp_password_t a, const pgp_password_t b); + typedef struct pgp_mpis_secret_key *pgp_mpis_secret_key_t; /*/ diff --git a/openpgp-ffi/include/sequoia/openpgp/types.h b/openpgp-ffi/include/sequoia/openpgp/types.h index 375832f9..2ba7fcdc 100644 --- a/openpgp-ffi/include/sequoia/openpgp/types.h +++ b/openpgp-ffi/include/sequoia/openpgp/types.h @@ -1,6 +1,20 @@ #ifndef SEQUOIA_OPENPGP_TYPES_H #define SEQUOIA_OPENPGP_TYPES_H +/*/ +/// Holds a session key. +/// +/// The session key is cleared when dropped. +/*/ +typedef struct pgp_session_key *pgp_session_key_t; + +/*/ +/// Holds a password. +/// +/// The password is cleared when dropped. +/*/ +typedef struct pgp_password *pgp_password_t; + /*/ /// Holds a fingerprint. /*/ diff --git a/openpgp-ffi/src/crypto.rs b/openpgp-ffi/src/crypto.rs index 7a453b60..540bcfd4 100644 --- a/openpgp-ffi/src/crypto.rs +++ b/openpgp-ffi/src/crypto.rs @@ -4,13 +4,57 @@ //! //! [`sequoia-openpgp::crypto`]: ../../sequoia_openpgp/crypto/index.html -extern crate sequoia_openpgp; -use self::sequoia_openpgp::{ +use libc::{size_t, uint8_t}; +use nettle::Yarrow; + +extern crate sequoia_openpgp as openpgp; +use self::openpgp::{ crypto, }; use super::packet::key::Key; use MoveFromRaw; +use MoveIntoRaw; + +/// Holds a session key. +/// +/// The session key is cleared when dropped. +#[::ffi_wrapper_type(prefix = "pgp_", name = "session_key", + derive = "Clone, Debug, PartialEq")] +pub struct SessionKey(openpgp::crypto::SessionKey); + +/// Creates a new session key. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_session_key_new(size: size_t) -> *mut SessionKey { + openpgp::crypto::SessionKey::new(&mut Yarrow::default(), size) + .move_into_raw() +} + +/// Creates a new session key from a buffer. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_session_key_from_bytes(buf: *const uint8_t, size: size_t) + -> *mut SessionKey { + let buf = unsafe { + ::std::slice::from_raw_parts(buf, size) + }; + openpgp::crypto::SessionKey::from(buf).move_into_raw() +} + +/// Holds a password. +/// +/// The password is cleared when dropped. +#[::ffi_wrapper_type(prefix = "pgp_", name = "password", + derive = "Clone, Debug, PartialEq")] +pub struct Password(openpgp::crypto::Password); + +/// Creates a new password from a buffer. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_password_from_bytes(buf: *const uint8_t, size: size_t) -> *mut Password { + let buf = unsafe { + ::std::slice::from_raw_parts(buf, size) + }; + openpgp::crypto::Password::from(buf).move_into_raw() +} /// Frees a signer. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] diff --git a/openpgp-ffi/src/lib.rs b/openpgp-ffi/src/lib.rs index 49e73f91..a98a6076 100644 --- a/openpgp-ffi/src/lib.rs +++ b/openpgp-ffi/src/lib.rs @@ -319,6 +319,7 @@ extern crate failure; extern crate lazy_static; extern crate libc; extern crate memsec; +extern crate nettle; extern crate sequoia_ffi_macros; use sequoia_ffi_macros::{ -- cgit v1.2.3