Skip to content

Commit

Permalink
Merge pull request #34 from hannesm/no-rresult
Browse files Browse the repository at this point in the history
remove rresult dependency
  • Loading branch information
hannesm authored Oct 28, 2021
2 parents 8620610 + eb2bc81 commit 58660aa
Show file tree
Hide file tree
Showing 18 changed files with 738 additions and 673 deletions.
1 change: 0 additions & 1 deletion awa.opam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ depends: [
"cstruct-unix"
"cstruct-sexp"
"sexplib"
"rresult"
"mtime"
"logs"
"fmt"
Expand Down
21 changes: 11 additions & 10 deletions lib/channel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Rresult.R
open Sexplib.Conv

open Util

(*
* Channel entry
*)
Expand Down Expand Up @@ -62,22 +63,22 @@ let input_data t data =
Printf.printf "channel input_data: discarding %d bytes (window size)\n%!"
(Cstruct.length left);
let new_win = Int32.sub t.us.win len in
Util.guard Int32.(new_win >= zero) "window underflow" >>= fun () ->
let* () = guard Int32.(new_win >= zero) "window underflow" in
let win, adjust =
if new_win < Ssh.channel_win_adj_threshold then
Ssh.channel_win_len, Int32.sub Ssh.channel_win_len new_win
else
new_win, Int32.zero
in
Util.guard (Int32.(adjust >= zero)) "adjust underflow" >>= fun () ->
let* () = guard (Int32.(adjust >= zero)) "adjust underflow" in
assert Int32.(adjust >= zero);
let t = { t with us = { t.us with win } } in
let msg = if adjust <> Int32.zero then
Some (Ssh.Msg_channel_window_adjust (t.them.id, adjust))
else
None
in
ok (t, data, msg)
Ok (t, data, msg)

let output_data t data =
let fragment data =
Expand All @@ -96,18 +97,18 @@ let output_data t data =
Ssh.Msg_channel_data (t.them.id, frag) :: frags)
i [] |> List.rev
in
let tosend = Util.cs_join t.tosend data in
let tosend = cs_join t.tosend data in
let len = min (Cstruct.length tosend) (Int32.to_int t.them.win) in
let data, tosend = Cstruct.split tosend len in
let win = Int32.sub t.them.win (Int32.of_int len) in
Util.guard Int32.(win >= zero) "window underflow" >>= fun () ->
let* () = guard Int32.(win >= zero) "window underflow" in
let t = { t with tosend; them = { t.them with win } } in
ok (t, fragment data)
Ok (t, fragment data)

let adjust_window t len =
let win = Int32.add t.them.win len in
(* XXX this does not handle up to 4GB correctly. *)
Util.guard Int32.(win > zero) "window overflow" >>= fun () ->
let* () = guard Int32.(win > zero) "window overflow" in
let data = t.tosend in
let t = { t with tosend = Cstruct.create 0; them = { t.them with win } } in
output_data t data
Expand Down Expand Up @@ -148,12 +149,12 @@ let next_free db =
let add ~id ~win ~max_pkt db =
(* Find the next available free channel *)
match next_free db with
| None -> error `No_channels_left
| None -> Error `No_channels_left
| Some key ->
let them = make_end id win max_pkt in
let us = make_end key Ssh.channel_win_len Ssh.channel_max_pkt_len in
let c = make ~us ~them in
ok (c, Channel_map.add key c db)
Ok (c, Channel_map.add key c db)

let update c db = Channel_map.add c.us.id c db

Expand Down
35 changes: 17 additions & 18 deletions lib/cipher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Rresult.R
open Mirage_crypto.Cipher_block.AES

type t =
Expand Down Expand Up @@ -53,15 +52,15 @@ let to_string = function
| Chacha20_poly1305 -> "[email protected]"

let of_string = function
| "none" -> ok Plaintext
| "aes128-ctr" -> ok Aes128_ctr
| "aes192-ctr" -> ok Aes192_ctr
| "aes256-ctr" -> ok Aes256_ctr
| "aes128-cbc" -> ok Aes128_cbc
| "aes192-cbc" -> ok Aes192_cbc
| "aes256-cbc" -> ok Aes256_cbc
| "[email protected]" -> ok Chacha20_poly1305
| s -> error ("Unknown cipher " ^ s)
| "none" -> Ok Plaintext
| "aes128-ctr" -> Ok Aes128_ctr
| "aes192-ctr" -> Ok Aes192_ctr
| "aes256-ctr" -> Ok Aes256_ctr
| "aes128-cbc" -> Ok Aes128_cbc
| "aes192-cbc" -> Ok Aes192_cbc
| "aes256-cbc" -> Ok Aes256_cbc
| "[email protected]" -> Ok Chacha20_poly1305
| s -> Error ("Unknown cipher " ^ s)

let key_len = function
| Plaintext -> 0
Expand Down Expand Up @@ -89,27 +88,27 @@ let mac_len = function
| Chacha20_poly1305 -> Mirage_crypto.Poly1305.mac_size
| _ -> 0

let known s = is_ok (of_string s)
let known s = Result.is_ok (of_string s)

(* For some reason Nocrypto CTR modifies ctr in place, CBC returns next *)
let enc_dec enc ~len seq cipher buf =
let open Mirage_crypto.Cipher_block in
match cipher.cipher_key with
| Plaintext_key -> ok (buf, cipher)
| Plaintext_key -> Ok (buf, cipher)
| Aes_ctr_key (key, iv) ->
let f = if enc then AES.CTR.encrypt else AES.CTR.decrypt in
let buf = f ~key ~ctr:iv buf in
let next_iv = AES.CTR.next_ctr ~ctr:iv buf in
let cipher_key = Aes_ctr_key (key, next_iv) in
let key = { cipher with cipher_key } in
ok (buf, key)
Ok (buf, key)
| Aes_cbc_key (key, iv) ->
let f = if enc then AES.CBC.encrypt else AES.CBC.decrypt in
let buf = f ~key ~iv buf in
let next_iv = AES.CBC.next_iv ~iv buf in
let cipher_key = Aes_cbc_key (key, next_iv) in
let cipher = { cipher with cipher_key } in
ok (buf, cipher)
Ok (buf, cipher)
| Chacha20_poly1305_key (len_key, key) ->
let nonce =
let b = Cstruct.create 8 in
Expand All @@ -128,11 +127,11 @@ let enc_dec enc ~len seq cipher buf =
let enc_msg = c_data msg in
let out = Cstruct.append enc_len enc_msg in
let tag = mac out in
ok (Cstruct.append out tag, cipher)
Ok (Cstruct.append out tag, cipher)
else
begin
if len then
ok (c_len buf, cipher)
Ok (c_len buf, cipher)
else
let c, tag =
let off = Cstruct.length buf - Mirage_crypto.Poly1305.mac_size in
Expand All @@ -144,9 +143,9 @@ let enc_dec enc ~len seq cipher buf =
and dec_msg = c_data enc_msg
in
if Cstruct.equal ctag tag then
ok (Cstruct.append dec_len dec_msg, cipher)
Ok (Cstruct.append dec_len dec_msg, cipher)
else
error "tag verification failed"
Error "tag verification failed"
end

let encrypt ~len seq cipher buf =
Expand Down
Loading

0 comments on commit 58660aa

Please sign in to comment.