Upgrade secio to the 2018 edition (#774)

* Upgrade secio to the 2018 edition

* Fix build with features on

* Fix emscripten build
This commit is contained in:
Pierre Krieger
2018-12-13 18:54:28 +01:00
committed by GitHub
parent 7b8e3dd613
commit e2ff74994c
10 changed files with 38 additions and 70 deletions

View File

@ -1,5 +1,6 @@
[package] [package]
name = "libp2p-secio" name = "libp2p-secio"
edition = "2018"
description = "Secio encryption protocol for libp2p" description = "Secio encryption protocol for libp2p"
version = "0.1.0" version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]

View File

@ -23,12 +23,12 @@
//! One important part of the SECIO handshake is negotiating algorithms. This is what this module //! One important part of the SECIO handshake is negotiating algorithms. This is what this module
//! helps you with. //! helps you with.
use error::SecioError; use crate::error::SecioError;
#[cfg(all(feature = "ring", not(target_os = "emscripten")))] #[cfg(all(feature = "ring", not(target_os = "emscripten")))]
use ring::digest; use ring::digest;
use std::cmp::Ordering; use std::cmp::Ordering;
use stream_cipher::Cipher; use crate::stream_cipher::Cipher;
use KeyAgreement; use crate::KeyAgreement;
const ECDH_P256: &str = "P-256"; const ECDH_P256: &str = "P-256";
const ECDH_P384: &str = "P-384"; const ECDH_P384: &str = "P-384";

View File

@ -23,12 +23,13 @@
use bytes::BytesMut; use bytes::BytesMut;
use super::{Hmac, StreamCipher}; use super::{Hmac, StreamCipher};
use error::SecioError; use crate::error::SecioError;
use futures::sink::Sink; use futures::sink::Sink;
use futures::stream::Stream; use futures::stream::Stream;
use futures::Async; use futures::Async;
use futures::Poll; use futures::Poll;
use futures::StartSend; use futures::StartSend;
use log::debug;
use std::cmp::min; use std::cmp::min;
/// Wraps around a `Stream<Item = BytesMut>`. The buffers produced by the underlying stream /// Wraps around a `Stream<Item = BytesMut>`. The buffers produced by the underlying stream

View File

@ -25,7 +25,7 @@ use self::decode::DecoderMiddleware;
use self::encode::EncoderMiddleware; use self::encode::EncoderMiddleware;
use aes_ctr::stream_cipher::StreamCipherCore; use aes_ctr::stream_cipher::StreamCipherCore;
use algo_support::Digest; use crate::algo_support::Digest;
use hmac::{self, Mac}; use hmac::{self, Mac};
use sha2::{Sha256, Sha512}; use sha2::{Sha256, Sha512};
use tokio_io::codec::length_delimited; use tokio_io::codec::length_delimited;
@ -120,19 +120,16 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate tokio; use tokio::runtime::current_thread::Runtime;
extern crate tokio_tcp; use tokio_tcp::{TcpListener, TcpStream};
use self::tokio::runtime::current_thread::Runtime; use crate::stream_cipher::{ctr, Cipher};
use self::tokio_tcp::TcpListener;
use self::tokio_tcp::TcpStream;
use stream_cipher::{ctr, Cipher};
use super::full_codec; use super::full_codec;
use super::DecoderMiddleware; use super::DecoderMiddleware;
use super::EncoderMiddleware; use super::EncoderMiddleware;
use super::Hmac; use super::Hmac;
use algo_support::Digest; use crate::algo_support::Digest;
use crate::error::SecioError;
use bytes::BytesMut; use bytes::BytesMut;
use error::SecioError;
use futures::sync::mpsc::channel; use futures::sync::mpsc::channel;
use futures::{Future, Sink, Stream, stream}; use futures::{Future, Sink, Stream, stream};
use rand; use rand;

View File

@ -20,11 +20,12 @@
//! Implementation of the key agreement process using the `ring` library. //! Implementation of the key agreement process using the `ring` library.
use crate::{KeyAgreement, SecioError};
use futures::{future, prelude::*}; use futures::{future, prelude::*};
use log::debug;
use ring::agreement as ring_agreement; use ring::agreement as ring_agreement;
use ring::rand as ring_rand; use ring::rand as ring_rand;
use untrusted::Input as UntrustedInput; use untrusted::Input as UntrustedInput;
use {KeyAgreement, SecioError};
impl Into<&'static ring_agreement::Algorithm> for KeyAgreement { impl Into<&'static ring_agreement::Algorithm> for KeyAgreement {
#[inline] #[inline]

View File

@ -20,10 +20,10 @@
//! Implementation of the key agreement process using the WebCrypto API. //! Implementation of the key agreement process using the WebCrypto API.
use crate::{KeyAgreement, SecioError};
use futures::prelude::*; use futures::prelude::*;
use futures::sync::oneshot; use futures::sync::oneshot;
use stdweb::{self, Reference, web::ArrayBuffer, web::TypedArray}; use stdweb::{self, Reference, web::ArrayBuffer, web::TypedArray};
use {KeyAgreement, SecioError};
/// Opaque private key type. /// Opaque private key type.
pub type AgreementPrivateKey = Reference; pub type AgreementPrivateKey = Reference;

View File

@ -21,7 +21,7 @@
//! This module handles the key agreement process. Typically ECDH. //! This module handles the key agreement process. Typically ECDH.
use futures::prelude::*; use futures::prelude::*;
use SecioError; use crate::SecioError;
#[path = "impl_ring.rs"] #[path = "impl_ring.rs"]
#[cfg(not(target_os = "emscripten"))] #[cfg(not(target_os = "emscripten"))]

View File

@ -18,18 +18,19 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use algo_support; use crate::algo_support;
use bytes::BytesMut; use bytes::BytesMut;
use codec::{full_codec, FullCodec, Hmac}; use crate::codec::{full_codec, FullCodec, Hmac};
use stream_cipher::{Cipher, ctr}; use crate::stream_cipher::{Cipher, ctr};
use ed25519_dalek::{PublicKey as Ed25519PublicKey, Signature as Ed25519Signature}; use ed25519_dalek::{PublicKey as Ed25519PublicKey, Signature as Ed25519Signature};
use error::SecioError; use crate::error::SecioError;
use exchange; use crate::exchange;
use futures::future; use futures::future;
use futures::sink::Sink; use futures::sink::Sink;
use futures::stream::Stream; use futures::stream::Stream;
use futures::Future; use futures::Future;
use libp2p_core::PublicKey; use libp2p_core::PublicKey;
use log::{debug, trace};
use protobuf::parse_from_bytes as protobuf_parse_from_bytes; use protobuf::parse_from_bytes as protobuf_parse_from_bytes;
use protobuf::Message as ProtobufMessage; use protobuf::Message as ProtobufMessage;
use rand::{self, RngCore}; use rand::{self, RngCore};
@ -42,12 +43,12 @@ use secp256k1;
use sha2::{Digest as ShaDigestTrait, Sha256, Sha512}; use sha2::{Digest as ShaDigestTrait, Sha256, Sha512};
use std::cmp::{self, Ordering}; use std::cmp::{self, Ordering};
use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use structs_proto::{Exchange, Propose}; use crate::structs_proto::{Exchange, Propose};
use tokio_io::codec::length_delimited; use tokio_io::codec::length_delimited;
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
#[cfg(all(feature = "ring", not(target_os = "emscripten")))] #[cfg(all(feature = "ring", not(target_os = "emscripten")))]
use untrusted::Input as UntrustedInput; use untrusted::Input as UntrustedInput;
use {KeyAgreement, SecioConfig, SecioKeyPairInner}; use crate::{KeyAgreement, SecioConfig, SecioKeyPairInner};
// This struct contains the whole context of a handshake, and is filled progressively // This struct contains the whole context of a handshake, and is filled progressively
// throughout the various parts of the handshake. // throughout the various parts of the handshake.
@ -627,19 +628,16 @@ where ::hmac::Hmac<D>: Clone {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate tokio;
extern crate tokio_tcp;
use bytes::BytesMut; use bytes::BytesMut;
use self::tokio::runtime::current_thread::Runtime; use tokio::runtime::current_thread::Runtime;
use self::tokio_tcp::TcpListener; use tokio_tcp::{TcpListener, TcpStream};
use self::tokio_tcp::TcpStream;
use crate::SecioError; use crate::SecioError;
use super::handshake; use super::handshake;
use super::stretch_key; use super::stretch_key;
use algo_support::Digest; use crate::algo_support::Digest;
use codec::Hmac; use crate::codec::Hmac;
use futures::prelude::*; use futures::prelude::*;
use {SecioConfig, SecioKeyPair}; use crate::{SecioConfig, SecioKeyPair};
#[test] #[test]
#[cfg(all(feature = "ring", not(target_os = "emscripten")))] #[cfg(all(feature = "ring", not(target_os = "emscripten")))]

View File

@ -29,13 +29,6 @@
//! through it. //! through it.
//! //!
//! ```no_run //! ```no_run
//! extern crate futures;
//! extern crate tokio;
//! extern crate tokio_io;
//! extern crate libp2p_core;
//! extern crate libp2p_secio;
//! extern crate libp2p_tcp;
//!
//! # fn main() { //! # fn main() {
//! use futures::Future; //! use futures::Future;
//! use libp2p_secio::{SecioConfig, SecioKeyPair, SecioOutput}; //! use libp2p_secio::{SecioConfig, SecioKeyPair, SecioOutput};
@ -82,36 +75,12 @@
#![recursion_limit = "128"] #![recursion_limit = "128"]
extern crate aes_ctr; // TODO: unfortunately the `js!` macro of stdweb depends on tons of "private" macros, which we
#[cfg(feature = "secp256k1")] // don't want to import manually
extern crate asn1_der;
extern crate bytes;
extern crate ctr;
extern crate ed25519_dalek;
extern crate futures;
extern crate hmac;
extern crate libp2p_core;
#[macro_use]
extern crate log;
extern crate protobuf;
extern crate rand;
#[cfg(not(target_os = "emscripten"))]
extern crate ring;
extern crate rw_stream_sink;
#[cfg(feature = "secp256k1")]
extern crate secp256k1;
extern crate sha2;
#[cfg(target_os = "emscripten")] #[cfg(target_os = "emscripten")]
#[macro_use] #[macro_use]
extern crate stdweb; extern crate stdweb;
extern crate tokio_io;
extern crate twofish;
#[cfg(not(target_os = "emscripten"))]
extern crate untrusted;
#[cfg(feature = "aes-all")]
#[macro_use]
extern crate lazy_static;
pub use self::error::SecioError; pub use self::error::SecioError;
#[cfg(feature = "secp256k1")] #[cfg(feature = "secp256k1")]
@ -121,6 +90,7 @@ use ed25519_dalek::Keypair as Ed25519KeyPair;
use futures::stream::MapErr as StreamMapErr; use futures::stream::MapErr as StreamMapErr;
use futures::{Future, Poll, Sink, StartSend, Stream}; use futures::{Future, Poll, Sink, StartSend, Stream};
use libp2p_core::{PeerId, PublicKey, upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade}}; use libp2p_core::{PeerId, PublicKey, upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade}};
use log::debug;
#[cfg(all(feature = "rsa", not(target_os = "emscripten")))] #[cfg(all(feature = "rsa", not(target_os = "emscripten")))]
use ring::signature::RSAKeyPair; use ring::signature::RSAKeyPair;
use rw_stream_sink::RwStreamSink; use rw_stream_sink::RwStreamSink;
@ -140,9 +110,9 @@ mod handshake;
mod structs_proto; mod structs_proto;
mod stream_cipher; mod stream_cipher;
pub use algo_support::Digest; pub use crate::algo_support::Digest;
pub use exchange::KeyAgreement; pub use crate::exchange::KeyAgreement;
pub use stream_cipher::Cipher; pub use crate::stream_cipher::Cipher;
/// Implementation of the `ConnectionUpgrade` trait of `libp2p_core`. Automatically applies /// Implementation of the `ConnectionUpgrade` trait of `libp2p_core`. Automatically applies
/// secio on any connection. /// secio on any connection.

View File

@ -85,12 +85,12 @@ pub fn ctr(key_size: Cipher, key: &[u8], iv: &[u8]) -> StreamCipher {
#[cfg(all(feature = "aes-all", any(target_arch = "x86_64", target_arch = "x86")))] #[cfg(all(feature = "aes-all", any(target_arch = "x86_64", target_arch = "x86")))]
mod aes_alt { mod aes_alt {
extern crate aesni; use crate::codec::StreamCipher;
use ::codec::StreamCipher;
use ctr::Ctr128; use ctr::Ctr128;
use self::aesni::{Aes128, Aes256}; use aesni::{Aes128, Aes256};
use ctr::stream_cipher::NewFixStreamCipher; use ctr::stream_cipher::NewFixStreamCipher;
use ctr::stream_cipher::generic_array::GenericArray; use ctr::stream_cipher::generic_array::GenericArray;
use lazy_static::lazy_static;
use twofish::Twofish; use twofish::Twofish;
use super::{Cipher, NullCipher}; use super::{Cipher, NullCipher};