Skip to content

Commit

Permalink
remove offset from counters
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Mar 19, 2024
1 parent 26e4481 commit 1f66e75
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/cipher_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module Counters = struct
val size : int
val add : ctr -> int64 -> ctr
val of_octets : string -> ctr
val unsafe_count_into : ctr -> bytes -> int -> blocks:int -> unit
val unsafe_count_into : ctr -> bytes -> blocks:int -> unit
end

module C64be = struct
Expand All @@ -92,10 +92,10 @@ module Counters = struct
(* Until OCaml 4.13 is lower bound*)
let of_octets cs = Bytes.get_int64_be (Bytes.unsafe_of_string cs) 0
let add = Int64.add
let unsafe_count_into t buf off ~blocks =
let unsafe_count_into t buf ~blocks =
let tmp = Bytes.create 8 in
Bytes.set_int64_be tmp 0 t;
Native.count8be tmp buf off ~blocks
Native.count8be tmp buf ~blocks
end

module C128be = struct
Expand All @@ -108,21 +108,21 @@ module Counters = struct
let w0' = Int64.add w0 n in
let flip = if Int64.logxor w0 w0' < 0L then w0' > w0 else w0' < w0 in
((if flip then Int64.succ w1 else w1), w0')
let unsafe_count_into (w1, w0) buf off ~blocks =
let unsafe_count_into (w1, w0) buf ~blocks =
let tmp = Bytes.create 16 in
Bytes.set_int64_be tmp 0 w1; Bytes.set_int64_be tmp 8 w0;
Native.count16be tmp buf off ~blocks
Native.count16be tmp buf ~blocks
end

module C128be32 = struct
include C128be
let add (w1, w0) n =
let hi = 0xffffffff00000000L and lo = 0x00000000ffffffffL in
(w1, Int64.(logor (logand hi w0) (add n w0 |> logand lo)))
let unsafe_count_into (w1, w0) buf off ~blocks =
let unsafe_count_into (w1, w0) buf ~blocks =
let tmp = Bytes.create 16 in
Bytes.set_int64_be tmp 0 w1; Bytes.set_int64_be tmp 8 w0;
Native.count16be4 tmp buf off ~blocks
Native.count16be4 tmp buf ~blocks
end
end

Expand Down Expand Up @@ -208,13 +208,13 @@ module Modes = struct
let stream ~key ~ctr n =
let blocks = imax 0 n / block_size in
let buf = Bytes.create n in
Ctr.unsafe_count_into ctr ~blocks buf 0 ;
Ctr.unsafe_count_into ctr ~blocks buf ;
Core.encrypt ~key ~blocks (Bytes.unsafe_to_string buf) 0 buf 0 ;
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 ;
Ctr.unsafe_count_into ctr ~blocks:1 buf' ;
Core.encrypt ~key ~blocks:1 (Bytes.unsafe_to_string buf') 0 buf' 0 ;
Bytes.unsafe_blit buf' 0 buf (blocks * block_size) slack
end;
Expand Down
6 changes: 3 additions & 3 deletions src/native.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ end
* Unsolved: bounds-checked XORs are slowing things down considerably... *)
external xor_into_bytes : string -> int -> bytes -> int -> int -> unit = "mc_xor_into_bytes" [@@noalloc]

external count8be : bytes -> bytes -> int -> blocks:int -> unit = "mc_count_8_be" [@@noalloc]
external count16be : bytes -> bytes -> int -> blocks:int -> unit = "mc_count_16_be" [@@noalloc]
external count16be4 : bytes -> bytes -> int -> blocks:int -> unit = "mc_count_16_be_4" [@@noalloc]
external count8be : bytes -> bytes -> blocks:int -> unit = "mc_count_8_be" [@@noalloc]
external count16be : bytes -> bytes -> blocks:int -> unit = "mc_count_16_be" [@@noalloc]
external count16be4 : bytes -> bytes -> blocks:int -> unit = "mc_count_16_be_4" [@@noalloc]

external misc_mode : unit -> int = "mc_misc_mode" [@@noalloc]

Expand Down
2 changes: 1 addition & 1 deletion src/native/mirage_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ CAMLprim value
mc_xor_into_bytes_generic (value b1, value off1, value b2, value off2, value n);

CAMLprim value
mc_count_16_be_4_generic (value ctr, value dst, value off, value blocks);
mc_count_16_be_4_generic (value ctr, value dst, value blocks);

#endif /* H__MIRAGE_CRYPTO */
4 changes: 2 additions & 2 deletions src/native/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ mc_xor_into_bytes_generic (value b1, value off1, value b2, value off2, value n)
}

#define __export_counter(name, f) \
CAMLprim value name (value ctr, value dst, value off, value blocks) { \
CAMLprim value name (value ctr, value dst, value blocks) { \
f ( (uint64_t*) Bp_val (ctr), \
(uint64_t*) _bp_uint8_off (dst, off), Long_val (blocks) ); \
(uint64_t*) _bp_uint8 (dst), Long_val (blocks) ); \
return Val_unit; \
}

Expand Down
6 changes: 3 additions & 3 deletions src/native/misc_sse.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ mc_xor_into_bytes (value b1, value off1, value b2, value off2, value n) {
}

#define __export_counter(name, f) \
CAMLprim value name (value ctr, value dst, value off, value blocks) { \
CAMLprim value name (value ctr, value dst, value blocks) { \
_mc_switch_accel(ssse3, \
name##_generic (ctr, dst, off, blocks), \
name##_generic (ctr, dst, blocks), \
f ( (uint64_t*) Bp_val (ctr), \
(uint64_t*) _bp_uint8_off (dst, off), Long_val (blocks) )) \
(uint64_t*) _bp_uint8 (dst), Long_val (blocks) )) \
return Val_unit; \
}

Expand Down

0 comments on commit 1f66e75

Please sign in to comment.