]> Repositorios git - scryer-prolog.git/commitdiff
ADDED: ed25519_keypair_public_key/2, relating a key pair to its public key
authorMarkus Triska <[email protected]>
Wed, 20 May 2020 21:32:46 +0000 (23:32 +0200)
committerMarkus Triska <[email protected]>
Wed, 20 May 2020 21:43:28 +0000 (23:43 +0200)
src/prolog/clause_types.rs
src/prolog/lib/crypto.pl
src/prolog/machine/system_calls.rs

index 536c9061b442474aea77f18922cb14d979325cf5..b783ac0ce3de31d277143bb114761c14192b7b3a 100644 (file)
@@ -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,
         }
     }
index 0723fb296b3ad97530cf7d5394ae9f972bc6c61c..696bb0e691929343797b60e458a23d140ae0a877 100644 (file)
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
 :- 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),
index 38f044b0b26b590e0a5f4a3867259a77886f1c06..895efad8d219a8c2d9587bf6d1207411a65b4fbc 100644 (file)
@@ -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);