@@ -28,6 +28,10 @@ module Block = struct
28
28
val block_size : int
29
29
val encrypt : key :key -> string -> string
30
30
val decrypt : key :key -> string -> string
31
+ val encrypt_into : key :key -> string -> src_off :int -> bytes -> dst_off :int -> int -> unit
32
+ val decrypt_into : key :key -> string -> src_off :int -> bytes -> dst_off :int -> int -> unit
33
+ val unsafe_encrypt_into : key :key -> string -> src_off :int -> bytes -> dst_off :int -> int -> unit
34
+ val unsafe_decrypt_into : key :key -> string -> src_off :int -> bytes -> dst_off :int -> int -> unit
31
35
end
32
36
33
37
module type CBC = sig
@@ -135,17 +139,43 @@ module Modes = struct
135
139
136
140
let of_secret = Core. of_secret
137
141
138
- let (encrypt, decrypt) =
139
- let ecb xform key src =
140
- let n = String. length src in
141
- if n mod block_size <> 0 then invalid_arg " ECB: length %u" n;
142
- let dst = Bytes. create n in
143
- xform ~key ~blocks: (n / block_size) src 0 dst 0 ;
144
- Bytes. unsafe_to_string dst
145
- in
146
- (fun ~key :(key , _ ) src -> ecb Core. encrypt key src),
147
- (fun ~key :(_ , key ) src -> ecb Core. decrypt key src)
142
+ let unsafe_ecb xform key src src_off dst dst_off len =
143
+ xform ~key ~blocks: (len / block_size) src src_off dst dst_off
144
+
145
+ let ecb xform key src src_off dst dst_off len =
146
+ if len mod block_size <> 0 then
147
+ invalid_arg " ECB: length %u not of block size" len;
148
+ if String. length src - src_off < len then
149
+ invalid_arg " ECB: source length %u - src_off %u < len %u"
150
+ (String. length src) src_off len;
151
+ if Bytes. length dst - dst_off < len then
152
+ invalid_arg " ECB: dst length %u - dst_off %u < len %u"
153
+ (Bytes. length dst) dst_off len;
154
+ unsafe_ecb xform key src src_off dst dst_off len
155
+
156
+ let encrypt_into ~key :(key , _ ) src ~src_off dst ~dst_off len =
157
+ ecb Core. encrypt key src src_off dst dst_off len
158
+
159
+ let unsafe_encrypt_into ~key :(key , _ ) src ~src_off dst ~dst_off len =
160
+ unsafe_ecb Core. encrypt key src src_off dst dst_off len
161
+
162
+ let decrypt_into ~key :(_ , key ) src ~src_off dst ~dst_off len =
163
+ ecb Core. decrypt key src src_off dst dst_off len
148
164
165
+ let unsafe_decrypt_into ~key :(_ , key ) src ~src_off dst ~dst_off len =
166
+ ecb Core. decrypt key src src_off dst dst_off len
167
+
168
+ let encrypt ~key src =
169
+ let len = String. length src in
170
+ let dst = Bytes. create len in
171
+ encrypt_into ~key src ~src_off: 0 dst ~dst_off: 0 len;
172
+ Bytes. unsafe_to_string dst
173
+
174
+ let decrypt ~key src =
175
+ let len = String. length src in
176
+ let dst = Bytes. create len in
177
+ decrypt_into ~key src ~src_off: 0 dst ~dst_off: 0 len;
178
+ Bytes. unsafe_to_string dst
149
179
end
150
180
151
181
module CBC_of (Core : Block.Core ) : Block. CBC = struct
0 commit comments