From 7f091b38b907356c523fd0c081fa032589530505 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Sat, 14 Jan 2017 13:13:49 +0100 Subject: [PATCH] Shell: add `Crypto_box.Public_key_hash` --- src/node/net/p2p.ml | 2 +- src/utils/base48.ml | 1 + src/utils/base48.mli | 3 +++ src/utils/crypto_box.ml | 20 +++++++++++++++++++- src/utils/crypto_box.mli | 11 +++++++++-- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/node/net/p2p.ml b/src/node/net/p2p.ml index 954c28bd3..08cd4c6f7 100644 --- a/src/node/net/p2p.ml +++ b/src/node/net/p2p.ml @@ -773,7 +773,7 @@ module Make (P: PARAMS) = struct let init_peers () = let my_gid = fresh_gid () in - let (my_secret_key, my_public_key) = + let (my_secret_key, my_public_key, _) = Crypto_box.random_keypair () in let my_proof_of_work = Crypto_box.generate_proof_of_work diff --git a/src/utils/base48.ml b/src/utils/base48.ml index f21f50682..b9b1345b8 100644 --- a/src/utils/base48.ml +++ b/src/utils/base48.ml @@ -230,6 +230,7 @@ module Prefix = struct let operation_hash = "\001" let protocol_hash = "\002" let ed25519_public_key_hash = "\003" + let cryptobox_public_key_hash = "\004" let ed25519_public_key = "\012" let ed25519_secret_key = "\013" let ed25519_signature = "\014" diff --git a/src/utils/base48.mli b/src/utils/base48.mli index 26a1f7922..802781391 100644 --- a/src/utils/base48.mli +++ b/src/utils/base48.mli @@ -37,6 +37,9 @@ module Prefix : sig val ed25519_public_key_hash: string (** Prefix for Ed25519 public key hashes: "\003". *) + val cryptobox_public_key_hash: string + (** Prefix for Ed25519 public key hashes: "\004". *) + val ed25519_public_key: string (** Prefix for Ed25519 public key: "\012". *) diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index 237d068a5..e8d58a80d 100644 --- a/src/utils/crypto_box.ml +++ b/src/utils/crypto_box.ml @@ -18,7 +18,19 @@ type nonce = Sodium.Box.nonce type target = int64 list (* used as unsigned intergers... *) exception TargetNot256Bit -let random_keypair = Sodium.Box.random_keypair +module Public_key_hash = Hash.Make_Blake2B (Base48) (struct + let name = "Crypto_box.Public_key_hash" + let title = "A Cryptobox public key ID" + let b48check_prefix = Base48.Prefix.cryptobox_public_key_hash + let size = Some 16 + end) + +let hash pk = + Public_key_hash.hash_bytes [Sodium.Box.Bigbytes.of_public_key pk] + +let random_keypair () = + let sk, pk = Sodium.Box.random_keypair () in + sk, pk, hash pk let random_nonce = Sodium.Box.random_nonce let increment_nonce = Sodium.Box.increment_nonce let box = Sodium.Box.Bigbytes.box @@ -26,6 +38,12 @@ let box_open sk pk msg nonce = try Some (Sodium.Box.Bigbytes.box_open sk pk msg nonce) with | Sodium.Verification_failure -> None +let precompute = Sodium.Box.precompute +let fast_box = Sodium.Box.Bigbytes.fast_box +let fast_box_open ck msg nonce = + try Some (Sodium.Box.Bigbytes.fast_box_open ck msg nonce) with + | Sodium.Verification_failure -> None + let make_target target = if List.length target > 8 then raise TargetNot256Bit ; target diff --git a/src/utils/crypto_box.mli b/src/utils/crypto_box.mli index 5902ebec8..0ae416919 100644 --- a/src/utils/crypto_box.mli +++ b/src/utils/crypto_box.mli @@ -21,15 +21,22 @@ val default_target : target type secret_key type public_key +module Public_key_hash : Hash.HASH +type channel_key val public_key_encoding : public_key Data_encoding.t val secret_key_encoding : secret_key Data_encoding.t -val random_keypair : unit -> secret_key * public_key +val hash : public_key -> Public_key_hash.t +val random_keypair : unit -> secret_key * public_key * Public_key_hash.t val box : secret_key -> public_key -> MBytes.t -> nonce -> MBytes.t - val box_open : secret_key -> public_key -> MBytes.t -> nonce -> MBytes.t option +val precompute : secret_key -> public_key -> channel_key +val fast_box : channel_key -> MBytes.t -> nonce -> MBytes.t +val fast_box_open : channel_key -> MBytes.t -> nonce -> MBytes.t option + val check_proof_of_work : public_key -> nonce -> target -> bool val generate_proof_of_work : public_key -> target -> nonce +