Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ctr_of: avoid big copy #3

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/cipher_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,19 @@ module Modes = struct
let of_secret = Core.e_of_secret

let stream ~key ~ctr n =
let blocks = imax 0 n // block_size in
let buf = Bytes.create (blocks * block_size) in
let blocks = imax 0 n / block_size in
let buf = Bytes.create n in
Ctr.unsafe_count_into ctr ~blocks buf 0 ;
Core.encrypt ~key ~blocks (Bytes.unsafe_to_string buf) 0 buf 0 ;
String.sub (Bytes.unsafe_to_string buf) 0 n
let slack = imax 0 n mod block_size in
if slack <> 0 then begin
let buf' = Bytes.create block_size in
let ctr = Ctr.add ctr (Int64.of_int blocks) in
Ctr.unsafe_count_into ctr ~blocks:1 buf' 0 ;
Core.encrypt ~key ~blocks:1 (Bytes.unsafe_to_string buf') 0 buf' 0 ;
Bytes.blit buf' 0 buf (blocks * block_size) slack
end;
Bytes.unsafe_to_string buf

let encrypt ~key ~ctr src =
let res = Bytes.unsafe_of_string (stream ~key ~ctr (String.length src)) in
Expand Down
Loading