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

Fix overflow in Counter.add16. #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions src/cipher_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ module Counter = struct
let add8 cs i x =
BE.(set_uint64 cs i (Int64.add x (get_uint64 cs i)))

(* FIXME: overflow: higher order bits. *)
let add16 cs i x = add8 cs (i + 8) x
let add16 cs i x = Native.add16be cs.buffer i x

end

Expand Down
1 change: 1 addition & 0 deletions src/native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ external xor_into : buffer -> off -> buffer -> off -> size -> unit = "caml_nc_xo

external count8be : buffer -> off -> buffer -> off -> size -> unit = "caml_nc_count_8_be" [@@noalloc]
external count16be : buffer -> off -> buffer -> off -> size -> unit = "caml_nc_count_16_be" [@@noalloc]
external add16be : buffer -> off -> int64 -> unit = "caml_nc_add_16_be" [@@noalloc]

external blit : buffer -> off -> buffer -> off -> size -> unit = "caml_blit_bigstring_to_bigstring" [@@noalloc]
16 changes: 16 additions & 0 deletions src/native/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ static inline void nc_count_16_be (uint64_t *init, uint64_t *dst, size_t blocks)
}
}

static inline void nc_add_16_be (uint64_t *init, uint64_t n) {
uint64_t h = be64_to_cpu (init[0]);
uint64_t l = be64_to_cpu (init[1]);
uint64_t nl = l + n; /* let it wrap */

if (nl < l)
h++;
init[0] = cpu_to_be64 (h);
init[1] = cpu_to_be64 (nl);
}

CAMLprim value
caml_nc_xor_into (value b1, value off1, value b2, value off2, value n) {
Expand All @@ -70,3 +80,9 @@ caml_nc_count_16_be (value init, value off1, value dst, value off2, value blocks
Long_val (blocks) );
return Val_unit;
}

CAMLprim value
caml_nc_add_16_be (value init, value off, value n) {
nc_add_16_be ( (uint64_t *) _ba_uint8_off (init, off), Int64_val(n));
return Val_unit;
}