Skip to content

Commit

Permalink
Fix chacha20 test
Browse files Browse the repository at this point in the history
When the data is not block aligned we need a temporary space for storing
a chacha20 block. Also, we can no longer assume the destination buffer's
.off is zero as this can now be user provided.
  • Loading branch information
reynir committed Feb 26, 2024
1 parent 3a812fe commit fad0786
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/chacha20.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ let crypt_into ~key ~nonce ?(ctr = 0L) ~dst data =
let state, inc = init ctr ~key ~nonce in
let l = Cstruct.length data in
let block_count = l // block in
let last_len =
let last = l mod block in
if last = 0 then block else last
in
let rec loop i = function
| 0 -> ()
| 1 ->
chacha20_block state i dst ;
Native.xor_into data.buffer (data.off + i) dst.buffer i last_len
let last = l mod block in
if last = 0 then begin
chacha20_block state i dst ;
Native.xor_into data.buffer (data.off + i) dst.buffer (dst.off + i) block
end else begin
let tmp = Cstruct.create_unsafe block in
chacha20_block state tmp.off tmp ;
Native.xor_into data.buffer (data.off + i) tmp.buffer tmp.off last;
Cstruct.blit tmp 0 dst i last
end
| n ->
chacha20_block state i dst ;
Native.xor_into data.buffer (data.off + i) dst.buffer i block ;
Native.xor_into data.buffer (data.off + i) dst.buffer (dst.off + i) block ;
inc state;
loop (i + block) (n - 1)
in
Expand Down

0 comments on commit fad0786

Please sign in to comment.