Skip to content

Commit fa3e8d6

Browse files
Igor Egorovortyomkaturuslan
authored
Add Noise Protocol Support (#90)
Signed-off-by: Igor Egorov <[email protected]> Co-authored-by: artyom-yurin <[email protected]> Co-authored-by: turuslan <[email protected]>
1 parent 6f0e310 commit fa3e8d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3741
-121
lines changed

example/01-echo/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ The last two examples can be used to test a compatibility between the implementa
99

1010
## C++ Server and Client
1111

12-
Currently, `libp2p_echo_server` can operate in two modes - via Plaintext or SECIO security protocols.
13-
By default, it is launched in SECIO secured mode.
12+
Currently, `libp2p_echo_server` can operate in two modes - via Plaintext or Noise security protocols.
13+
By default, it is launched in Noise secured mode.
1414
To run it with Plaintext exclusive mode just add `-insecure` command-line argument.
1515

1616
```bash

example/01-echo/go.mod

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ module github.com/libp2p/go-libp2p-examples
33
require (
44
github.com/gogo/protobuf v1.3.1
55
github.com/google/uuid v1.1.1
6-
github.com/ipfs/go-datastore v0.3.1
7-
github.com/ipfs/go-log v0.0.1
8-
github.com/libp2p/go-libp2p v0.5.0
6+
github.com/ipfs/go-datastore v0.4.4
7+
github.com/ipfs/go-log v1.0.3
8+
github.com/libp2p/go-libp2p v0.8.1
99
github.com/libp2p/go-libp2p-autonat-svc v0.1.0
10-
github.com/libp2p/go-libp2p-circuit v0.1.4
10+
github.com/libp2p/go-libp2p-circuit v0.2.1
1111
github.com/libp2p/go-libp2p-connmgr v0.2.1
12-
github.com/libp2p/go-libp2p-core v0.3.0
13-
github.com/libp2p/go-libp2p-discovery v0.2.0
12+
github.com/libp2p/go-libp2p-core v0.5.1
13+
github.com/libp2p/go-libp2p-discovery v0.3.0
1414
github.com/libp2p/go-libp2p-kad-dht v0.4.1
15+
github.com/libp2p/go-libp2p-noise v0.1.2
1516
github.com/libp2p/go-libp2p-quic-transport v0.2.2
1617
github.com/libp2p/go-libp2p-routing v0.1.0
17-
github.com/libp2p/go-libp2p-secio v0.2.1
18-
github.com/libp2p/go-libp2p-swarm v0.2.2
19-
github.com/libp2p/go-libp2p-tls v0.1.2
20-
github.com/multiformats/go-multiaddr v0.2.0
21-
github.com/multiformats/go-multiaddr-net v0.1.1
18+
github.com/libp2p/go-libp2p-secio v0.2.2
19+
github.com/libp2p/go-libp2p-swarm v0.2.3
20+
github.com/libp2p/go-libp2p-tls v0.1.3
21+
github.com/multiformats/go-multiaddr v0.2.1
22+
github.com/multiformats/go-multiaddr-net v0.1.4
23+
github.com/sabhiram/go-tracey v0.0.0-20180906172802-620b5b676e3c // indirect
2224
github.com/whyrusleeping/go-logging v0.0.1
2325
)
2426

example/01-echo/libp2p_client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ import (
1717
"github.com/libp2p/go-libp2p-core/network"
1818
"github.com/libp2p/go-libp2p-core/peer"
1919
"github.com/libp2p/go-libp2p-core/peerstore"
20+
noise "github.com/libp2p/go-libp2p-noise"
21+
2022

2123
golog "github.com/ipfs/go-log"
2224
ma "github.com/multiformats/go-multiaddr"
25+
gologging "github.com/whyrusleeping/go-logging"
2326
)
2427

2528
// makeBasicHost creates a LibP2P host with a random peer ID listening on the
@@ -52,6 +55,8 @@ func makeBasicHost(listenPort int, insecure bool, randseed int64) (host.Host, er
5255

5356
if insecure {
5457
opts = append(opts, libp2p.NoSecurity)
58+
} else {
59+
opts = append(opts, libp2p.Security(noise.ID, noise.New))
5560
}
5661

5762
basicHost, err := libp2p.New(context.Background(), opts...)
@@ -81,7 +86,7 @@ func main() {
8186
// string IDs (i.e. "swarm"). We can control the verbosity level for
8287
// all loggers with:
8388
// golog.SetAllLoggers(golog.LevelInfo) // Change to DEBUG for extra info
84-
golog.SetDebugLogging()
89+
golog.SetAllLoggers(golog.LogLevel(gologging.INFO)) // Change to DEBUG for extra info
8590

8691
// Parse options from the command line
8792
listenF := flag.Int("l", 0, "wait for incoming connections")

example/01-echo/libp2p_echo_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <libp2p/host/basic_host.hpp>
1313
#include <libp2p/injector/host_injector.hpp>
1414
#include <libp2p/protocol/echo.hpp>
15+
#include <libp2p/security/noise.hpp>
1516
#include <libp2p/security/plaintext.hpp>
16-
#include <libp2p/security/secio.hpp>
1717

1818
bool isInsecure(int argc, char **argv) {
1919
if (2 == argc) {
@@ -34,7 +34,7 @@ struct ServerContext {
3434
ServerContext initSecureServer(const libp2p::crypto::KeyPair &keypair) {
3535
auto injector = libp2p::injector::makeHostInjector(
3636
libp2p::injector::useKeyPair(keypair),
37-
libp2p::injector::useSecurityAdaptors<libp2p::security::Secio>());
37+
libp2p::injector::useSecurityAdaptors<libp2p::security::Noise>());
3838
auto host = injector.create<std::shared_ptr<libp2p::Host>>();
3939
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>();
4040
return {.host = host, .io_context = context};

include/libp2p/common/byteutil.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,31 @@ namespace libp2p::common {
2222
*/
2323
ByteArray &putUint16BE(ByteArray &bytes, uint16_t n);
2424

25+
/**
26+
* Put a 16-bit number into the byte array in Little Endian encoding
27+
*/
28+
ByteArray &putUint16LE(ByteArray &bytes, uint16_t n);
29+
2530
/**
2631
* Put an 32-bit number into the byte array in Big Endian encoding
2732
*/
2833
ByteArray &putUint32BE(ByteArray &bytes, uint32_t n);
2934

35+
/**
36+
* Put a 32-bit number into the byte array in Little Endian encoding
37+
*/
38+
ByteArray &putUint32LE(ByteArray &bytes, uint32_t n);
39+
3040
/**
3141
* Put an 64-bit number into the byte array in Big Endian encoding
3242
*/
3343
ByteArray &putUint64BE(ByteArray &bytes, uint64_t n);
3444

45+
/**
46+
* Put a 64-bit number into the byte array in Little Endian encoding
47+
*/
48+
ByteArray &putUint64LE(ByteArray &bytes, uint64_t n);
49+
3550
/**
3651
* Convert value, to which the pointer (\param v) references, to the value of
3752
* (\tparam T)

include/libp2p/crypto/chachapoly.hpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_INCLUDE_LIBP2P_CRYPTO_CHACHAPOLY_HPP
7+
#define LIBP2P_INCLUDE_LIBP2P_CRYPTO_CHACHAPOLY_HPP
8+
9+
#include <array>
10+
11+
#include <gsl/span>
12+
#include <libp2p/common/byteutil.hpp>
13+
#include <libp2p/common/types.hpp>
14+
#include <libp2p/crypto/common.hpp>
15+
#include <libp2p/outcome/outcome.hpp>
16+
17+
namespace libp2p::crypto::chachapoly {
18+
using libp2p::common::ByteArray;
19+
using Key = std::array<uint8_t, 32>;
20+
using Nonce = std::array<uint8_t, 12>;
21+
22+
class ChaCha20Poly1305 {
23+
public:
24+
virtual ~ChaCha20Poly1305() = default;
25+
26+
/**
27+
* Does authenticated encryption with associated data (AEAD)
28+
* @param plaintext to cipher
29+
* @param nonce - custom specified nonce bytes
30+
* @param associated_data - data for message authentication
31+
* @return ciphertext bytes
32+
*/
33+
virtual outcome::result<ByteArray> encrypt(
34+
const Nonce &nonce, gsl::span<const uint8_t> plaintext,
35+
gsl::span<const uint8_t> aad) = 0;
36+
37+
/**
38+
* Does authenticated decryption with associated data (AEAD)
39+
* @param ciphertext bytes to decrypt
40+
* @param nonce - custom specified nonce bytes
41+
* @param associated_data - data for message authentication
42+
* @return plaintext bytes
43+
*/
44+
virtual outcome::result<ByteArray> decrypt(
45+
const Nonce &nonce, gsl::span<const uint8_t> ciphertext,
46+
gsl::span<const uint8_t> aad) = 0;
47+
48+
/**
49+
* Convert 64-bit integer to 12-bit long byte sequence with four zero bytes
50+
* at the beginning
51+
* @param n - an integer to convert
52+
* @return - bytes vector
53+
*/
54+
inline Nonce uint64toNonce(uint64_t n) const {
55+
ByteArray result(4, 0);
56+
result.reserve(12);
57+
libp2p::common::putUint64LE(result, n);
58+
Nonce nonce;
59+
std::copy_n(result.begin(), nonce.size(), nonce.begin());
60+
return nonce;
61+
}
62+
};
63+
64+
} // namespace libp2p::crypto::chachapoly
65+
66+
#endif // LIBP2P_INCLUDE_LIBP2P_CRYPTO_CHACHAPOLY_HPP
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_INCLUDE_LIBP2P_CRYPTO_CHACHAPOLY_CHACHAPOLY_IMPL_HPP
7+
#define LIBP2P_INCLUDE_LIBP2P_CRYPTO_CHACHAPOLY_CHACHAPOLY_IMPL_HPP
8+
9+
#include <openssl/evp.h>
10+
#include <libp2p/common/logger.hpp>
11+
#include <libp2p/crypto/chachapoly.hpp>
12+
13+
namespace libp2p::crypto::chachapoly {
14+
15+
class ChaCha20Poly1305Impl : public ChaCha20Poly1305 {
16+
public:
17+
explicit ChaCha20Poly1305Impl(Key key);
18+
19+
outcome::result<ByteArray> encrypt(const Nonce &nonce,
20+
gsl::span<const uint8_t> plaintext,
21+
gsl::span<const uint8_t> aad) override;
22+
23+
outcome::result<ByteArray> decrypt(const Nonce &nonce,
24+
gsl::span<const uint8_t> ciphertext,
25+
gsl::span<const uint8_t> aad) override;
26+
27+
private:
28+
const Key key_;
29+
const EVP_CIPHER *cipher_;
30+
const int block_size_;
31+
libp2p::common::Logger log_ =
32+
libp2p::common::createLogger("ChaChaPolyImpl");
33+
};
34+
35+
} // namespace libp2p::crypto::chachapoly
36+
37+
#endif // LIBP2P_INCLUDE_LIBP2P_CRYPTO_CHACHAPOLY_CHACHAPOLY_IMPL_HPP

include/libp2p/crypto/common_functions.hpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@
66
#ifndef LIBP2P_COMMON_FUNCTIONS_HPP
77
#define LIBP2P_COMMON_FUNCTIONS_HPP
88

9+
#include <algorithm>
10+
#include <array>
911
#include <memory>
1012
#include <vector>
11-
#include <array>
1213

1314
#include <openssl/ec.h>
1415
#include <openssl/evp.h>
1516
#include <gsl/span>
1617
#include <libp2p/outcome/outcome.hpp>
1718

1819
namespace libp2p::crypto {
20+
21+
template <typename Array>
22+
std::vector<uint8_t> asVector(const Array &key) {
23+
std::vector<uint8_t> result;
24+
result.resize(key.size(), 0);
25+
std::copy_n(key.begin(), key.size(), result.begin());
26+
return result;
27+
}
28+
29+
template <typename Array>
30+
Array asArray(const std::vector<uint8_t> &bytes) {
31+
Array key;
32+
std::copy_n(bytes.begin(), key.size(), key.begin());
33+
return key;
34+
}
35+
1936
/**
2037
* Initializes EC_KEY structure with private and public key from private key
2138
* bytes
@@ -66,10 +83,9 @@ namespace libp2p::crypto {
6683
* @param key - EC public key
6784
* @return signature status or error code
6885
*/
69-
outcome::result<bool> VerifyEcSignature(
70-
gsl::span<const uint8_t> digest,
71-
gsl::span<const uint8_t> signature,
72-
const std::shared_ptr<EC_KEY> &key);
86+
outcome::result<bool> VerifyEcSignature(gsl::span<const uint8_t> digest,
87+
gsl::span<const uint8_t> signature,
88+
const std::shared_ptr<EC_KEY> &key);
7389

7490
} // namespace libp2p::crypto
7591

include/libp2p/crypto/hasher.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_SRC_CRYPTO_HASHER_HPP
7+
#define LIBP2P_SRC_CRYPTO_HASHER_HPP
8+
9+
#include <gsl/span>
10+
#include <libp2p/crypto/common.hpp>
11+
#include <libp2p/outcome/outcome.hpp>
12+
13+
namespace libp2p::crypto {
14+
15+
using libp2p::crypto::common::HashType;
16+
17+
class Hasher {
18+
public:
19+
virtual ~Hasher() = default;
20+
21+
/// appends a new chunk of data
22+
virtual outcome::result<void> write(gsl::span<const uint8_t> data) = 0;
23+
24+
/**
25+
* Calculates the current digest.
26+
* Does not affect the internal state.
27+
* New data still could be fed via write method.
28+
*/
29+
virtual outcome::result<std::vector<uint8_t>> digest() = 0;
30+
31+
/// resets the internal state
32+
virtual outcome::result<void> reset() = 0;
33+
34+
/// hash size in bytes
35+
virtual size_t digestSize() const = 0;
36+
37+
/// block size in bytes for the most optimal hash update via write method
38+
virtual size_t blockSize() const = 0;
39+
40+
/// runtime identifiable hasher type
41+
virtual HashType hashType() const = 0;
42+
};
43+
} // namespace libp2p::crypto
44+
45+
#endif // LIBP2P_SRC_CRYPTO_HASHER_HPP

include/libp2p/crypto/hmac_provider.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@
99
#include <gsl/span>
1010
#include <libp2p/common/types.hpp>
1111
#include <libp2p/crypto/common.hpp>
12+
#include <libp2p/crypto/hasher.hpp>
1213
#include <libp2p/outcome/outcome.hpp>
1314

1415
namespace libp2p::crypto::hmac {
16+
17+
using ByteArray = libp2p::common::ByteArray;
18+
using HashType = common::HashType;
19+
20+
/// HMAC that supports stream data feeding interface
21+
class HmacProviderCtr : public Hasher {};
22+
1523
/**
1624
* @class HmacProvider provides HMAC functionality
1725
* allows calculating message authentication code
1826
* involving a cryptographic hash function
1927
* and a secret cryptographic key
2028
*/
2129
class HmacProvider {
22-
using HashType = common::HashType;
23-
2430
public:
25-
using ByteArray = libp2p::common::ByteArray;
26-
2731
virtual ~HmacProvider() = default;
32+
2833
/**
2934
* @brief calculates digests
3035
* @param hash_type hash type

0 commit comments

Comments
 (0)