upstream: https://github.com/mirage/mirage-crypto
at main 139 lines 5.0 kB view raw
1(** C accelerated cryptographic primitives. 2 3 Low-level bindings to platform-optimised implementations of AES, DES, 4 ChaCha20, Poly1305, and GHASH. *) 5 6(** AES block cipher. *) 7module AES : sig 8 external enc : string -> int -> bytes -> int -> string -> int -> int -> unit 9 = "mc_aes_enc_bc" "mc_aes_enc" 10 [@@noalloc] 11 (** [enc src src_off dst dst_off rk rk_off blocks] encrypts [blocks] AES 12 blocks from [src] into [dst] using round keys [rk]. *) 13 14 external dec : string -> int -> bytes -> int -> string -> int -> int -> unit 15 = "mc_aes_dec_bc" "mc_aes_dec" 16 [@@noalloc] 17 (** [dec src src_off dst dst_off rk rk_off blocks] decrypts [blocks] AES 18 blocks from [src] into [dst] using round keys [rk]. *) 19 20 external derive_e : string -> bytes -> int -> unit = "mc_aes_derive_e_key" 21 [@@noalloc] 22 (** [derive_e key rk rounds] derives the encryption round-key schedule from 23 [key] into [rk]. *) 24 25 external derive_d : string -> bytes -> int -> string option -> unit 26 = "mc_aes_derive_d_key" 27 [@@noalloc] 28 (** [derive_d key rk rounds ekey] derives the decryption round-key schedule. 29 If [ekey] is provided, it is used as a precomputed encryption schedule. *) 30 31 external rk_s : int -> int = "mc_aes_rk_size" 32 [@@noalloc] 33 (** [rk_s rounds] is the round-key buffer size in bytes for [rounds] rounds. 34 *) 35 36 external mode : unit -> int = "mc_aes_mode" 37 [@@noalloc] 38 (** [mode ()] detects the AES implementation: [0] for generic, [1] for AES-NI. 39 *) 40end 41 42(** Triple DES block cipher. *) 43module DES : sig 44 external ddes : string -> int -> bytes -> int -> int -> string -> unit 45 = "mc_des_ddes_bc" "mc_des_ddes" 46 [@@noalloc] 47 (** [ddes src src_off dst dst_off blocks ks] encrypts or decrypts [blocks] DES 48 blocks using key schedule [ks]. *) 49 50 external des3key : bytes -> int -> bytes -> unit = "mc_des_des3key" 51 [@@noalloc] 52 (** [des3key key mode ks] derives a Triple-DES key schedule from [key] into 53 [ks]. [mode] selects encryption or decryption. *) 54 55 external k_s : unit -> int = "mc_des_key_size" 56 [@@noalloc] 57 (** [k_s ()] is the key-schedule buffer size in bytes. *) 58end 59 60(** ChaCha20 stream cipher. *) 61module Chacha : sig 62 external round : int -> bytes -> bytes -> int -> unit = "mc_chacha_round" 63 [@@noalloc] 64 (** [round count state dst off] performs [count] ChaCha20 rounds on [state], 65 writing output into [dst] at offset [off]. *) 66end 67 68(** Poly1305 message authentication. *) 69module Poly1305 : sig 70 external init : bytes -> string -> unit = "mc_poly1305_init" 71 [@@noalloc] 72 (** [init ctx key] initialises the Poly1305 context [ctx] with [key]. *) 73 74 external update : bytes -> string -> int -> int -> unit = "mc_poly1305_update" 75 [@@noalloc] 76 (** [update ctx data off len] feeds [len] bytes from [data] at [off] into 77 [ctx]. *) 78 79 external finalize : bytes -> bytes -> int -> unit = "mc_poly1305_finalize" 80 [@@noalloc] 81 (** [finalize ctx mac off] writes the final MAC tag into [mac] at [off]. *) 82 83 external ctx_size : unit -> int = "mc_poly1305_ctx_size" 84 [@@noalloc] 85 (** [ctx_size ()] is the Poly1305 context size in bytes. *) 86 87 external mac_size : unit -> int = "mc_poly1305_mac_size" 88 [@@noalloc] 89 (** [mac_size ()] is the MAC tag size in bytes (16). *) 90end 91 92(** GHASH universal hash for GCM. *) 93module GHASH : sig 94 external keysize : unit -> int = "mc_ghash_key_size" 95 [@@noalloc] 96 (** [keysize ()] is the GHASH key buffer size in bytes. *) 97 98 external keyinit : string -> bytes -> unit = "mc_ghash_init_key" 99 [@@noalloc] 100 (** [keyinit key buf] derives the GHASH subkey into [buf]. *) 101 102 external ghash : string -> bytes -> string -> int -> int -> unit = "mc_ghash" 103 [@@noalloc] 104 (** [ghash key hash data off len] updates [hash] with [len] bytes from [data] 105 at [off] using the GHASH [key]. *) 106 107 external mode : unit -> int = "mc_ghash_mode" 108 [@@noalloc] 109 (** [mode ()] detects the GHASH implementation: [0] for generic, [1] for 110 PCLMULQDQ. *) 111end 112 113external xor_into_bytes : string -> int -> bytes -> int -> int -> unit 114 = "mc_xor_into_bytes" 115[@@noalloc] 116(** [xor_into_bytes src src_off dst dst_off len] XORs [len] bytes from [src] at 117 [src_off] into [dst] at [dst_off]. *) 118 119external count8be : ctr:bytes -> bytes -> off:int -> blocks:int -> unit 120 = "mc_count_8_be" 121[@@noalloc] 122(** [count8be ~ctr buf ~off ~blocks] writes [blocks] big-endian 8-byte counter 123 values into [buf]. *) 124 125external count16be : ctr:bytes -> bytes -> off:int -> blocks:int -> unit 126 = "mc_count_16_be" 127[@@noalloc] 128(** [count16be ~ctr buf ~off ~blocks] writes [blocks] big-endian 16-byte counter 129 values into [buf]. *) 130 131external count16be4 : ctr:bytes -> bytes -> off:int -> blocks:int -> unit 132 = "mc_count_16_be_4" 133[@@noalloc] 134(** [count16be4 ~ctr buf ~off ~blocks] writes [blocks] big-endian 16-byte 135 counter values into [buf], incrementing only the lower 32 bits. *) 136 137external misc_mode : unit -> int = "mc_misc_mode" 138[@@noalloc] 139(** [misc_mode ()] detects hardware XOR acceleration. *)