ensure proper lengths of key and initialization vector

This avoids crashes when using unsuitable lengths.
This commit is contained in:
Markus Triska
2020-08-06 23:27:49 +02:00
parent 674483a4c6
commit c55cc3c472

View File

@@ -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,
@@ -598,6 +603,7 @@ crypto_data_decrypt(CipherText0, Algorithm, Key, IV, PlainText, Options) :-
( 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).