]> Repositorios git - scryer-prolog.git/commitdiff
ensure proper lengths of key and initialization vector
authorMarkus Triska <[email protected]>
Thu, 6 Aug 2020 21:27:49 +0000 (23:27 +0200)
committerMarkus Triska <[email protected]>
Thu, 6 Aug 2020 21:27:49 +0000 (23:27 +0200)
This avoids crashes when using unsuitable lengths.

src/lib/crypto.pl

index 1f78c8cb4ce1bf20662cad43afde5c12b4a0b41a..e88113c73d96da923bfdd1970117f3c915486a25 100644 (file)
@@ -547,8 +547,13 @@ crypto_data_encrypt(PlainText0, Algorithm, Key, IV, CipherText, Options) :-
         (   Algorithm = 'chacha20-poly1305' -> true
         ;   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).
 
+algorithm_key_iv('chacha20-poly1305', Key, IV) :-
+        length(Key, 32),
+        length(IV, 12).
+
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   crypto_data_decrypt(+CipherText,
                       +Algorithm,
@@ -598,6 +603,7 @@ crypto_data_decrypt(CipherText0, Algorithm, Key, IV, PlainText, Options) :-
         (   Algorithm = 'chacha20-poly1305' -> true
         ;   domain_error('chacha20-poly1305', Algorithm, crypto_data_decrypt/6)
         ),
+        algorithm_key_iv(Algorithm, Key, IV),
         '$crypto_data_decrypt'(CipherText, AAD, Key, IV, Encoding, PlainText).