From 2845f55157b93f2d45e64a9bcbd23048ce8b6c9e Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Wed, 20 May 2020 23:32:46 +0200 Subject: [PATCH] ADDED: ed25519_keypair_public_key/2, relating a key pair to its public key --- src/prolog/clause_types.rs | 7 ++++-- src/prolog/lib/crypto.pl | 35 +++++++++++++++++------------- src/prolog/machine/system_calls.rs | 18 ++++++++++++++- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/prolog/clause_types.rs b/src/prolog/clause_types.rs index 536c9061..b783ac0c 100644 --- a/src/prolog/clause_types.rs +++ b/src/prolog/clause_types.rs @@ -294,7 +294,8 @@ pub enum SystemClauseType { CryptoDataDecrypt, Ed25519Sign, Ed25519Verify, - Ed25519NewKeyPair + Ed25519NewKeyPair, + Ed25519KeyPairPublicKey } impl SystemClauseType { @@ -485,7 +486,8 @@ impl SystemClauseType { &SystemClauseType::CryptoDataDecrypt => clause_name!("$crypto_data_decrypt"), &SystemClauseType::Ed25519Sign => clause_name!("$ed25519_sign"), &SystemClauseType::Ed25519Verify => clause_name!("$ed25519_verify"), - &SystemClauseType::Ed25519NewKeyPair => clause_name!("$ed25519_new_keypair") + &SystemClauseType::Ed25519NewKeyPair => clause_name!("$ed25519_new_keypair"), + &SystemClauseType::Ed25519KeyPairPublicKey => clause_name!("$ed25519_keypair_public_key") } } @@ -657,6 +659,7 @@ impl SystemClauseType { ("$ed25519_sign", 3) => Some(SystemClauseType::Ed25519Sign), ("$ed25519_verify", 3) => Some(SystemClauseType::Ed25519Verify), ("$ed25519_new_keypair", 1) => Some(SystemClauseType::Ed25519NewKeyPair), + ("$ed25519_keypair_public_key", 2) => Some(SystemClauseType::Ed25519KeyPairPublicKey), _ => None, } } diff --git a/src/prolog/lib/crypto.pl b/src/prolog/lib/crypto.pl index 0723fb29..696bb0e6 100644 --- a/src/prolog/lib/crypto.pl +++ b/src/prolog/lib/crypto.pl @@ -13,21 +13,22 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ :- module(crypto, - [hex_bytes/2, % ?Hex, ?Bytes - crypto_n_random_bytes/2, % +N, -Bytes - crypto_data_hash/3, % +Data, -Hash, +Options - crypto_data_hkdf/4, % +Data, +Length, -Bytes, +Options - crypto_password_hash/2, % +Password, ?Hash - crypto_password_hash/3, % +Password, -Hash, +Options - crypto_data_encrypt/6, % +PlainText, +Algorithm, +Key, +IV, -CipherText, +Options - crypto_data_decrypt/6, % +CipherText, +Algorithm, +Key, +IV, -PlainText, +Options - ed25519_new_keypair/1, % -KeyPair - ed25519_sign/4, % +PrivateKey, +Data, -Signature, +Options - ed25519_verify/4, % +PublicKey, +Data, -Signature, +Options - crypto_name_curve/2, % +Name, -Curve - crypto_curve_order/2, % +Curve, -Order - crypto_curve_generator/2, % +Curve, -Generator - crypto_curve_scalar_mult/4 % +Curve, +Scalar, +Point, -Result + [hex_bytes/2, % ?Hex, ?Bytes + crypto_n_random_bytes/2, % +N, -Bytes + crypto_data_hash/3, % +Data, -Hash, +Options + crypto_data_hkdf/4, % +Data, +Length, -Bytes, +Options + crypto_password_hash/2, % +Password, ?Hash + crypto_password_hash/3, % +Password, -Hash, +Options + crypto_data_encrypt/6, % +PlainText, +Algorithm, +Key, +IV, -CipherText, +Options + crypto_data_decrypt/6, % +CipherText, +Algorithm, +Key, +IV, -PlainText, +Options + ed25519_new_keypair/1, % -KeyPair + ed25519_keypair_public_key/2, % +KeyPair, +PublicKey + ed25519_sign/4, % +PrivateKey, +Data, -Signature, +Options + ed25519_verify/4, % +PublicKey, +Data, -Signature, +Options + crypto_name_curve/2, % +Name, -Curve + crypto_curve_order/2, % +Curve, -Order + crypto_curve_generator/2, % +Curve, -Generator + crypto_curve_scalar_mult/4 % +Curve, +Scalar, +Point, -Result ]). :- use_module(library(error)). @@ -663,6 +664,10 @@ encoding_bytes(utf8, Cs, Bs) :- ed25519_new_keypair(Pair) :- '$ed25519_new_keypair'(Pair). +ed25519_keypair_public_key(Pair0, PublicKey) :- + encoding_bytes(octet, Pair0, Pair), + '$ed25519_keypair_public_key'(Pair, PublicKey). + ed25519_sign(Key0, Data0, Signature, Options) :- options_data_bytes(Options, Data0, Data), encoding_bytes(octet, Key0, Key), diff --git a/src/prolog/machine/system_calls.rs b/src/prolog/machine/system_calls.rs index 38f044b0..895efad8 100644 --- a/src/prolog/machine/system_calls.rs +++ b/src/prolog/machine/system_calls.rs @@ -40,7 +40,7 @@ use crate::crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers}; use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode}; use ring::rand::{SecureRandom, SystemRandom}; -use ring::{digest,hkdf,pbkdf2,aead,signature}; +use ring::{digest,hkdf,pbkdf2,aead,signature::{self,KeyPair}}; use ripemd160::{Ripemd160, Digest}; use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; use blake2::{Blake2s, Blake2b}; @@ -5457,6 +5457,22 @@ impl MachineState { self.unify(self[temp_v!(1)], complete_string); } + &SystemClauseType::Ed25519KeyPairPublicKey => { + let stub1 = MachineError::functor_stub(clause_name!("ed25519_keypair_public_key"), 2); + let bytes = self.integers_to_bytevec(temp_v!(1), stub1); + + let key_pair = match signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(&bytes) { + Ok(kp) => { kp } + _ => { self.fail = true; return Ok(()); } + }; + + let complete_string = { + let buffer = String::from_iter(key_pair.public_key().as_ref().iter().map(|b| *b as char)); + self.heap.put_complete_string(&buffer) + }; + + self.unify(self[temp_v!(2)], complete_string); + } &SystemClauseType::Ed25519Sign => { let stub1 = MachineError::functor_stub(clause_name!("ed25519_sign"), 4); let key = self.integers_to_bytevec(temp_v!(1), stub1); -- 2.54.0