Skip to content

Commit

Permalink
revise block_size check
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Jun 18, 2024
1 parent 08a8b16 commit acf74f4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/cipher_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,16 @@ module Modes = struct

let of_secret = Core.of_secret

let block_size_check ?(off = 0) ~iv cs =
let check_block_size ~iv len =
if String.length iv <> block then
invalid_arg "CBC: IV length %u not of block size" (String.length iv);
if (String.length cs - off) mod block <> 0 then
invalid_arg "CBC: argument length %u (off %u) not of block size"
(String.length cs) off
if len mod block <> 0 then
invalid_arg "CBC: argument length %u not of block size"
len
[@@inline]

let next_iv ?(off = 0) cs ~iv =
block_size_check ~iv cs ~off ;
check_block_size ~iv (String.length cs - off) ;
if String.length cs > off then
String.sub cs (String.length cs - block_size) block_size
else iv
Expand All @@ -243,7 +243,7 @@ module Modes = struct
unsafe_encrypt_into_inplace ~key ~iv dst ~dst_off len

let encrypt_into ~key ~iv src ~src_off dst ~dst_off len =
block_size_check ~off:src_off ~iv src;
check_block_size ~iv len;
check_offset ~tag:"CBC" ~buf:"src" ~off:src_off ~len (String.length src);
check_offset ~tag:"CBC" ~buf:"dst" ~off:dst_off ~len (Bytes.length dst);
unsafe_encrypt_into ~key ~iv src ~src_off dst ~dst_off len
Expand All @@ -262,7 +262,7 @@ module Modes = struct
end

let decrypt_into ~key ~iv src ~src_off dst ~dst_off len =
block_size_check ~off:src_off ~iv src;
check_block_size ~iv len;
check_offset ~tag:"CBC" ~buf:"src" ~off:src_off ~len (String.length src);
check_offset ~tag:"CBC" ~buf:"dst" ~off:dst_off ~len (Bytes.length dst);
unsafe_decrypt_into ~key ~iv src ~src_off dst ~dst_off len
Expand Down

0 comments on commit acf74f4

Please sign in to comment.