Merge pull request #660 from triska/shorter_crypto
Shorten and improve implementation of several cryptographic routines
This commit is contained in:
@@ -547,8 +547,13 @@ crypto_data_encrypt(PlainText0, Algorithm, Key, IV, CipherText, Options) :-
|
|||||||
( Algorithm = 'chacha20-poly1305' -> true
|
( Algorithm = 'chacha20-poly1305' -> true
|
||||||
; domain_error('chacha20-poly1305', Algorithm, crypto_data_encrypt/6)
|
; domain_error('chacha20-poly1305', Algorithm, crypto_data_encrypt/6)
|
||||||
),
|
),
|
||||||
|
algorithm_key_iv(Algorithm, Key, IV),
|
||||||
'$crypto_data_encrypt'(PlainText, AAD, Encoding, Key, IV, Tag, CipherText).
|
'$crypto_data_encrypt'(PlainText, AAD, Encoding, Key, IV, Tag, CipherText).
|
||||||
|
|
||||||
|
algorithm_key_iv('chacha20-poly1305', Key, IV) :-
|
||||||
|
length(Key, 32),
|
||||||
|
length(IV, 12).
|
||||||
|
|
||||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
crypto_data_decrypt(+CipherText,
|
crypto_data_decrypt(+CipherText,
|
||||||
+Algorithm,
|
+Algorithm,
|
||||||
@@ -592,13 +597,13 @@ crypto_data_decrypt(CipherText0, Algorithm, Key, IV, PlainText, Options) :-
|
|||||||
encoding_chars(Encoding, AAD0, AAD),
|
encoding_chars(Encoding, AAD0, AAD),
|
||||||
must_be(atom, Encoding),
|
must_be(atom, Encoding),
|
||||||
member(Encoding, [utf8,octet]),
|
member(Encoding, [utf8,octet]),
|
||||||
must_be(list, CipherText0),
|
|
||||||
encoding_chars(octet, CipherText0, CipherText1),
|
encoding_chars(octet, CipherText0, CipherText1),
|
||||||
maplist(char_code, TagChars, Tag),
|
maplist(char_code, TagChars, Tag),
|
||||||
append(CipherText1, TagChars, CipherText),
|
append(CipherText1, TagChars, CipherText),
|
||||||
( Algorithm = 'chacha20-poly1305' -> true
|
( Algorithm = 'chacha20-poly1305' -> true
|
||||||
; domain_error('chacha20-poly1305', Algorithm, crypto_data_decrypt/6)
|
; domain_error('chacha20-poly1305', Algorithm, crypto_data_decrypt/6)
|
||||||
),
|
),
|
||||||
|
algorithm_key_iv(Algorithm, Key, IV),
|
||||||
'$crypto_data_decrypt'(CipherText, AAD, Key, IV, Encoding, PlainText).
|
'$crypto_data_decrypt'(CipherText, AAD, Key, IV, Encoding, PlainText).
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5397,21 +5397,10 @@ impl MachineState {
|
|||||||
let encoding = self.atom_argument_to_string(2);
|
let encoding = self.atom_argument_to_string(2);
|
||||||
let bytes = self.string_encoding_bytes(1, &encoding);
|
let bytes = self.string_encoding_bytes(1, &encoding);
|
||||||
|
|
||||||
let algorithm_str = match self.store(self.deref(self[temp_v!(4)])) {
|
let algorithm = self.atom_argument_to_string(4);
|
||||||
Addr::Con(h) if self.heap.atom_at(h) => {
|
|
||||||
if let HeapCellValue::Atom(ref atom, _) = &self.heap[h] {
|
|
||||||
atom.as_str()
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let ints_list =
|
let ints_list =
|
||||||
match algorithm_str {
|
match algorithm.as_str() {
|
||||||
"sha3_224" => { let mut context = Sha3_224::new();
|
"sha3_224" => { let mut context = Sha3_224::new();
|
||||||
context.input(&bytes);
|
context.input(&bytes);
|
||||||
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
|
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
|
||||||
@@ -5434,7 +5423,7 @@ impl MachineState {
|
|||||||
context.input(&bytes);
|
context.input(&bytes);
|
||||||
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
|
Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::from(Addr::Fixnum(*b as isize))))) }
|
||||||
_ => { let ints = digest::digest(
|
_ => { let ints = digest::digest(
|
||||||
match algorithm_str {
|
match algorithm.as_str() {
|
||||||
"sha256" => { &digest::SHA256 }
|
"sha256" => { &digest::SHA256 }
|
||||||
"sha384" => { &digest::SHA384 }
|
"sha384" => { &digest::SHA384 }
|
||||||
"sha512" => { &digest::SHA512 }
|
"sha512" => { &digest::SHA512 }
|
||||||
@@ -5456,21 +5445,12 @@ impl MachineState {
|
|||||||
let stub2 = MachineError::functor_stub(clause_name!("crypto_data_hkdf"), 4);
|
let stub2 = MachineError::functor_stub(clause_name!("crypto_data_hkdf"), 4);
|
||||||
let info = self.integers_to_bytevec(temp_v!(4), stub2);
|
let info = self.integers_to_bytevec(temp_v!(4), stub2);
|
||||||
|
|
||||||
let algorithm = match self.store(self.deref(self[temp_v!(5)])) {
|
let algorithm = self.atom_argument_to_string(5);
|
||||||
Addr::Con(h) if self.heap.atom_at(h) => {
|
|
||||||
if let HeapCellValue::Atom(ref atom, _) = &self.heap[h] {
|
let length = self.store(self.deref(self[temp_v!(6)]));
|
||||||
atom.as_str()
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let length =
|
let length =
|
||||||
match Number::try_from((self[temp_v!(6)], &self.heap)) {
|
match Number::try_from((length, &self.heap)) {
|
||||||
Ok(Number::Fixnum(n)) => {
|
Ok(Number::Fixnum(n)) => {
|
||||||
usize::try_from(n).unwrap()
|
usize::try_from(n).unwrap()
|
||||||
}
|
}
|
||||||
@@ -5485,7 +5465,7 @@ impl MachineState {
|
|||||||
|
|
||||||
let ints_list =
|
let ints_list =
|
||||||
{ let digest_alg =
|
{ let digest_alg =
|
||||||
match algorithm {
|
match algorithm.as_str() {
|
||||||
"sha256" => { hkdf::HKDF_SHA256 }
|
"sha256" => { hkdf::HKDF_SHA256 }
|
||||||
"sha384" => { hkdf::HKDF_SHA384 }
|
"sha384" => { hkdf::HKDF_SHA384 }
|
||||||
"sha512" => { hkdf::HKDF_SHA512 }
|
"sha512" => { hkdf::HKDF_SHA512 }
|
||||||
@@ -5611,26 +5591,17 @@ impl MachineState {
|
|||||||
self.unify(self[temp_v!(6)], complete_string);
|
self.unify(self[temp_v!(6)], complete_string);
|
||||||
}
|
}
|
||||||
&SystemClauseType::CryptoCurveScalarMult => {
|
&SystemClauseType::CryptoCurveScalarMult => {
|
||||||
let curve = match self.store(self.deref(self[temp_v!(1)])) {
|
let curve = self.atom_argument_to_string(1);
|
||||||
Addr::Con(h) if self.heap.atom_at(h) => {
|
let curve_id = match curve.as_str() {
|
||||||
if let HeapCellValue::Atom(ref atom, _) = &self.heap[h] {
|
|
||||||
atom.as_str()
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let curve_id = match curve {
|
|
||||||
"secp112r1" => { Nid::SECP112R1 }
|
"secp112r1" => { Nid::SECP112R1 }
|
||||||
"secp256k1" => { Nid::SECP256K1 }
|
"secp256k1" => { Nid::SECP256K1 }
|
||||||
_ => { unreachable!() }
|
_ => { unreachable!() }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let scalar = self.store(self.deref(self[temp_v!(2)]));
|
||||||
|
|
||||||
let scalar =
|
let scalar =
|
||||||
match Number::try_from((self[temp_v!(2)], &self.heap)) {
|
match Number::try_from((scalar, &self.heap)) {
|
||||||
Ok(Number::Fixnum(n)) => {
|
Ok(Number::Fixnum(n)) => {
|
||||||
Integer::from(n)
|
Integer::from(n)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user