ADDED: Support for SHA-3 algorithms in crypto_data_hash/3

This commit is contained in:
Markus Triska
2020-05-19 10:50:33 +02:00
parent e0e3b180e7
commit f5c2f6f9e9
3 changed files with 31 additions and 10 deletions

View File

@@ -36,3 +36,4 @@ rustyline = "6.0.0"
unicode_reader = "1.0.0" unicode_reader = "1.0.0"
ring = "0.16.13" ring = "0.16.13"
ripemd160 = "0.8.0" ripemd160 = "0.8.0"
sha3 = "0.8.2"

View File

@@ -159,10 +159,10 @@ crypto_random_byte(B) :- '$crypto_random_byte'(B).
- algorithm(+A) - algorithm(+A)
where A is one of ripemd160, sha256, sha384, sha512, where A is one of ripemd160, sha256, sha384, sha512,
sha512_256, or a variable. If A is a variable, then it is sha512_256, sha3_224, sha3_256, sha3_384, sha3_512, or a
unified with the default algorithm, which is an algorithm that variable. If A is a variable, then it is unified with the
is considered cryptographically secure at the time of this default algorithm, which is an algorithm that is considered
writing. cryptographically secure at the time of this writing.
- encoding(+Encoding) - encoding(+Encoding)
The default encoding is utf8. The alternative is octet, The default encoding is utf8. The alternative is octet,
to treat the input as a list of raw bytes. to treat the input as a list of raw bytes.
@@ -215,6 +215,10 @@ hash_algorithm(sha256).
hash_algorithm(sha512). hash_algorithm(sha512).
hash_algorithm(sha384). hash_algorithm(sha384).
hash_algorithm(sha512_256). hash_algorithm(sha512_256).
hash_algorithm(sha3_224).
hash_algorithm(sha3_256).
hash_algorithm(sha3_384).
hash_algorithm(sha3_512).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -230,10 +234,9 @@ hash_algorithm(sha512_256).
Admissible options are: Admissible options are:
- algorithm(+Algorithm) - algorithm(+Algorithm)
A hashing algorithm as specified to crypto_data_hash/3. The One of sha256, sha384 or sha512. If you specify a variable,
default is a cryptographically secure algorithm. If you then it is unified with the algorithm that was used, which is a
specify a variable, then it is unified with the algorithm cryptographically secure algorithm by default.
that was used, which is a cryptographically secure algorithm.
- info(+Info) - info(+Info)
Optional context and application specific information, Optional context and application specific information,
specified as a list of bytes or characters. The default is []. specified as a list of bytes or characters. The default is [].

View File

@@ -42,6 +42,7 @@ use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode};
use ring::rand::{SecureRandom, SystemRandom}; use ring::rand::{SecureRandom, SystemRandom};
use ring::{digest,hkdf,pbkdf2,aead,error}; use ring::{digest,hkdf,pbkdf2,aead,error};
use ripemd160::{Ripemd160, Digest}; use ripemd160::{Ripemd160, Digest};
use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512};
pub fn get_key() -> KeyEvent { pub fn get_key() -> KeyEvent {
let key; let key;
@@ -5219,7 +5220,23 @@ impl MachineState {
}; };
let ints_list = let ints_list =
if algorithm_str == "ripemd160" { if algorithm_str == "sha3_224" {
let mut context = Sha3_224::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
} else if algorithm_str == "sha3_256" {
let mut context = Sha3_256::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
} else if algorithm_str == "sha3_384" {
let mut context = Sha3_384::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
} else if algorithm_str == "sha3_512" {
let mut context = Sha3_512::new();
context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
} else if algorithm_str == "ripemd160" {
let mut context = Ripemd160::new(); let mut context = Ripemd160::new();
context.input(&bytes); context.input(&bytes);
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b)))))) Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
@@ -5278,7 +5295,7 @@ impl MachineState {
"sha256" => { hkdf::HKDF_SHA256 } "sha256" => { hkdf::HKDF_SHA256 }
"sha384" => { hkdf::HKDF_SHA384 } "sha384" => { hkdf::HKDF_SHA384 }
"sha512" => { hkdf::HKDF_SHA512 } "sha512" => { hkdf::HKDF_SHA512 }
_ => { unreachable!() } _ => { self.fail = true; return Ok(()); }
}; };
let salt = hkdf::Salt::new(digest_alg, &salt); let salt = hkdf::Salt::new(digest_alg, &salt);
let mut bytes : Vec<u8> = Vec::new(); let mut bytes : Vec<u8> = Vec::new();