<commit_before>
open Nocrypto
open Nocrypto.Uncommon
open Nocrypto.Hash

open Ciphersuite
open Packet

let (<+>) = Utils.Cs.(<+>)


(* on-the-wire dh_params <-> (group, pub_message) *)
let dh_params_pack group message =
  let (p, g) = Dh.to_cstruct group in
  { Core.dh_p = p ; dh_g = g ; dh_Ys = message }

and dh_params_unpack { Core.dh_p ; dh_g ; dh_Ys } =
  ( Dh.group ~p:dh_p ~gg:dh_g (), dh_Ys )

let dh_shared group secret public =
  try Some (Dh.shared group secret public)
  with Dh.Invalid_public_key -> None


type 'k stream_cipher = (module Cipher_stream.T    with type key = 'k)
type 'k cbc_cipher    = (module Cipher_block.T_CBC with type key = 'k)
type hash_fn          = (module Hash.T_MAC)

module Ciphers = struct

  let get_mac = function
    | `MD5    -> (module Hash.MD5    : Hash.T_MAC)
    | `SHA1   -> (module Hash.SHA1   : Hash.T_MAC)
    | `SHA256 -> (module Hash.SHA256 : Hash.T_MAC)
    | `SHA384 -> (module Hash.SHA384 : Hash.T_MAC)
    | `SHA512 -> (module Hash.SHA512 : Hash.T_MAC)

  let get_hash = function
    | `MD5    -> (module Hash.MD5    : Hash.T)
    | `SHA1   -> (module Hash.SHA1   : Hash.T)
    | `SHA224 -> (module Hash.SHA224 : Hash.T)
    | `SHA256 -> (module Hash.SHA256 : Hash.T)
    | `SHA384 -> (module Hash.SHA384 : Hash.T)
    | `SHA512 -> (module Hash.SHA512 : Hash.T)

  let digest_size h =
    let h' = get_hash h in
    let module H = (val h' : Hash.T) in H.digest_size


  type keyed =
    | K_Stream : 'k stream_cipher * 'k -> keyed
    | K_CBC    : 'k cbc_cipher    * 'k -> keyed

  (* XXX partial *)
  let get_cipher ~secret = function

    | RC4_128 ->
        let open Cipher_stream in
        K_Stream ( (module ARC4 : Cipher_stream.T with type key = ARC4.key),
                   ARC4.of_secret secret )

    | TRIPLE_DES_EDE_CBC ->
        let open Cipher_block.DES in
        K_CBC ( (module CBC : Cipher_block.T_CBC with type key = CBC.key),
                CBC.of_secret secret )

    | AES_128_CBC ->
        let open Cipher_block.AES in
        K_CBC ( (module CBC : Cipher_block.T_CBC with type key = CBC.key),
                CBC.of_secret secret )

    | AES_256_CBC ->
        let open Cipher_block.AES in
        K_CBC ( (module CBC : Cipher_block.T_CBC with type key = CBC.key),
                CBC.of_secret secret )
end

let hash hash_ctor cs =
  let hasht = Ciphers.get_hash hash_ctor in
  let module H = (val hasht : Hash.T) in
  H.digest cs

let hash_eq hash_ctor ~target cs =
  Utils.Cs.equal target (hash hash_ctor cs)

(* MAC used in TLS *)
let mac (hash, secret) seq ty (v_major, v_minor) data =
  let open Cstruct in

  let prefix = create 13
  and len = len data in

  BE.set_uint64 prefix 0 seq;
  set_uint8 prefix 8 (Packet.content_type_to_int ty);
  set_uint8 prefix 9 v_major;
  set_uint8 prefix 10 v_minor;
  BE.set_uint16 prefix 11 len;

  let hashm = Ciphers.get_mac hash in
  let module H = (val hashm : Hash.T_MAC) in
  H.hmac ~key:secret (prefix <+> data)

let cbc_block (type a) cipher =
  let module C = (val cipher : Cipher_block.T_CBC with type key = a) in C.block_size

let encrypt_stream (type a) ~cipher ~key data =
  let module C = (val cipher : Cipher_stream.T with type key = a) in
  let { C.message ; key } = C.encrypt ~key data in
  (message, key)

let decrypt_stream (type a) ~cipher ~key data =
  let module C = (val cipher : Cipher_stream.T with type key = a) in
  let { C.message ; key } = C.decrypt ~key data in
  (message, key)
<commit_msg>clean internally used funs<commit_after>
open Nocrypto
open Nocrypto.Uncommon
open Nocrypto.Hash

open Ciphersuite
open Packet

let (<+>) = Utils.Cs.(<+>)


(* on-the-wire dh_params <-> (group, pub_message) *)
let dh_params_pack group message =
  let (p, g) = Dh.to_cstruct group in
  { Core.dh_p = p ; dh_g = g ; dh_Ys = message }

and dh_params_unpack { Core.dh_p ; dh_g ; dh_Ys } =
  ( Dh.group ~p:dh_p ~gg:dh_g (), dh_Ys )

let dh_shared group secret public =
  try Some (Dh.shared group secret public)
  with Dh.Invalid_public_key -> None


type 'k stream_cipher = (module Cipher_stream.T    with type key = 'k)
type 'k cbc_cipher    = (module Cipher_block.T_CBC with type key = 'k)

module Ciphers = struct

  let get_hash = function
    | `MD5    -> (module Hash.MD5    : Hash.T)
    | `SHA1   -> (module Hash.SHA1   : Hash.T)
    | `SHA224 -> (module Hash.SHA224 : Hash.T)
    | `SHA256 -> (module Hash.SHA256 : Hash.T)
    | `SHA384 -> (module Hash.SHA384 : Hash.T)
    | `SHA512 -> (module Hash.SHA512 : Hash.T)

  let digest_size h =
    let h' = get_hash h in
    let module H = (val h' : Hash.T) in H.digest_size


  type keyed =
    | K_Stream : 'k stream_cipher * 'k -> keyed
    | K_CBC    : 'k cbc_cipher    * 'k -> keyed

  (* XXX partial *)
  let get_cipher ~secret = function

    | RC4_128 ->
        let open Cipher_stream in
        K_Stream ( (module ARC4 : Cipher_stream.T with type key = ARC4.key),
                   ARC4.of_secret secret )

    | TRIPLE_DES_EDE_CBC ->
        let open Cipher_block.DES in
        K_CBC ( (module CBC : Cipher_block.T_CBC with type key = CBC.key),
                CBC.of_secret secret )

    | AES_128_CBC ->
        let open Cipher_block.AES in
        K_CBC ( (module CBC : Cipher_block.T_CBC with type key = CBC.key),
                CBC.of_secret secret )

    | AES_256_CBC ->
        let open Cipher_block.AES in
        K_CBC ( (module CBC : Cipher_block.T_CBC with type key = CBC.key),
                CBC.of_secret secret )
end

let hash = Hash.digest

let hash_eq hash_ctor ~target cs =
  Utils.Cs.equal target (hash hash_ctor cs)

(* MAC used in TLS *)
let mac (hash, key) seq ty (v_major, v_minor) data =
  let open Cstruct in

  let prefix = create 13
  and len = len data in

  BE.set_uint64 prefix 0 seq;
  set_uint8 prefix 8 (Packet.content_type_to_int ty);
  set_uint8 prefix 9 v_major;
  set_uint8 prefix 10 v_minor;
  BE.set_uint16 prefix 11 len;

  Hash.mac hash ~key (prefix <+> data)

let cbc_block (type a) cipher =
  let module C = (val cipher : Cipher_block.T_CBC with type key = a) in C.block_size

let encrypt_stream (type a) ~cipher ~key data =
  let module C = (val cipher : Cipher_stream.T with type key = a) in
  let { C.message ; key } = C.encrypt ~key data in
  (message, key)

let decrypt_stream (type a) ~cipher ~key data =
  let module C = (val cipher : Cipher_stream.T with type key = a) in
  let { C.message ; key } = C.decrypt ~key data in
  (message, key)

{'fraction_non_alphanumeric': 0.1077666874610106, 'fraction_numerical': 0.021210230817217717, 'mean_word_length': 2.6068616422947133, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '451296', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2338, 'n_words': 726}