Skip to content

Commit cbef294

Browse files
committed
Switch to using hs-bindgen-generated code
1 parent 10c81ab commit cbef294

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1955
-4601
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,18 @@ jobs:
220220
run: |
221221
cabal test all -j1 --test-show-details=direct
222222
223-
- name: 🛠️ Setup cabal-docspec (Linux)
224-
if: ${{ runner.os == 'Linux' && !startsWith(matrix.ghc-version, '9.2') && !startsWith(matrix.ghc-version, '9.4') }}
225-
uses: jorisdral/actions/setup-cabal-docspec@setup-cabal-docspec-v0
226-
227-
- name: 🧪 Test with cabal-docspec (Linux)
228-
if: ${{ runner.os == 'Linux' && !startsWith(matrix.ghc-version, '9.2') && !startsWith(matrix.ghc-version, '9.4') }}
229-
run: ./scripts/test-cabal-docspec.sh
230-
env:
231-
SKIP_CABAL_BUILD: true
223+
# TODO: temporarily disabled testing with cabal-docspec, because it can not
224+
# parse the cabal file now that it has a custom-setup component
225+
226+
# - name: 🛠️ Setup cabal-docspec (Linux)
227+
# if: ${{ runner.os == 'Linux' && !startsWith(matrix.ghc-version, '9.2') && !startsWith(matrix.ghc-version, '9.4') }}
228+
# uses: jorisdral/actions/setup-cabal-docspec@setup-cabal-docspec-v0
229+
230+
# - name: 🧪 Test with cabal-docspec (Linux)
231+
# if: ${{ runner.os == 'Linux' && !startsWith(matrix.ghc-version, '9.2') && !startsWith(matrix.ghc-version, '9.4') }}
232+
# run: ./scripts/test-cabal-docspec.sh
233+
# env:
234+
# SKIP_CABAL_BUILD: true
232235

233236
################################################################################
234237
# Lint with cabal-gild

botan-bindings/botan-bindings.cabal

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ common language
7878
library
7979
import: warnings, language
8080
hs-source-dirs: src
81-
default-extensions:
82-
NoImplicitPrelude
83-
OverloadedStrings
84-
8581
exposed-modules:
8682
Botan.Bindings.Bcrypt
8783
Botan.Bindings.BlockCipher
@@ -94,7 +90,6 @@ library
9490
Botan.Bindings.KeyWrap
9591
Botan.Bindings.MAC
9692
Botan.Bindings.MPI
97-
Botan.Bindings.Prelude
9893
Botan.Bindings.PubKey
9994
Botan.Bindings.PubKey.DH
10095
Botan.Bindings.PubKey.DSA
@@ -121,8 +116,6 @@ library
121116
Botan.Bindings.X509
122117
Botan.Bindings.ZFEC
123118

124-
other-modules: Paths_botan_bindings
125-
autogen-modules: Paths_botan_bindings
126119
build-depends: base >=4.16 && <4.22
127120
includes: botan/ffi.h
128121

botan-bindings/src/Botan/Bindings/Bcrypt.hs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,11 @@ License : BSD-3-Clause
77
88
Stability : experimental
99
Portability : POSIX
10-
11-
Generate and validate Bcrypt password hashes
1210
-}
1311

14-
{-# LANGUAGE CApiFFI #-}
15-
1612
module Botan.Bindings.Bcrypt (
17-
pattern BOTAN_BCRYPT_WORK_FACTOR_FAST
18-
, pattern BOTAN_BCRYPT_WORK_FACTOR_GOOD
19-
, pattern BOTAN_BCRYPT_WORK_FACTOR_STRONG
20-
, botan_bcrypt_generate
21-
, botan_bcrypt_is_valid
13+
Safe.botan_bcrypt_generate
14+
, Safe.botan_bcrypt_is_valid
2215
) where
2316

24-
import Botan.Bindings.Prelude
25-
import Botan.Bindings.RNG
26-
27-
-- TODO: Maybe move to Botan.Low.Bcrypt
28-
pattern BOTAN_BCRYPT_WORK_FACTOR_FAST
29-
, BOTAN_BCRYPT_WORK_FACTOR_GOOD
30-
, BOTAN_BCRYPT_WORK_FACTOR_STRONG
31-
:: (Eq a, Num a) => a
32-
33-
-- | Should not cause noticable CPU usage
34-
pattern BOTAN_BCRYPT_WORK_FACTOR_FAST = 12
35-
36-
-- | May cause noticable CPU usage
37-
pattern BOTAN_BCRYPT_WORK_FACTOR_GOOD = 14
38-
39-
-- | May block for several seconds
40-
pattern BOTAN_BCRYPT_WORK_FACTOR_STRONG = 16
41-
42-
{- |
43-
Create a password hash using Bcrypt
44-
45-
Output is formatted bcrypt $2a$...
46-
-}
47-
foreign import capi safe "botan/ffi.h botan_bcrypt_generate"
48-
botan_bcrypt_generate
49-
:: Ptr Word8 -- ^ __out__: buffer holding the password hash, should be of length 64 bytes
50-
-> Ptr CSize -- ^ __out_len__: the desired output length in bytes
51-
-> ConstPtr CChar -- ^ __password__: the password
52-
-> BotanRNG -- ^ __rng__: a random number generator
53-
-> CSize -- ^ __work_factor__: how much work to do to slow down guessing attacks
54-
-> Word32 -- ^ __flags__: should be 0 in current API revision, all other uses are reserved
55-
-- and return BOTAN_FFI_ERROR_BAD_FLAG
56-
-> IO CInt -- ^ 0 on success, a negative value on failure
57-
58-
-- | Check a previously created password hash
59-
foreign import capi safe "botan/ffi.h botan_bcrypt_is_valid"
60-
botan_bcrypt_is_valid
61-
:: ConstPtr CChar -- ^ __pass__: the password to check against
62-
-> ConstPtr CChar -- ^ __hash__: the stored hash to check against
63-
-> IO CInt -- ^ 0 if if this password/hash combination is valid,
64-
-- 1 if the combination is not valid (but otherwise well formed),
65-
-- negative on error
17+
import qualified Botan.Bindings.Generated.Safe as Safe

botan-bindings/src/Botan/Bindings/BlockCipher.hs

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ Ciphers](https://botan.randombit.net/handbook/api_ref/block_cipher.html) section
1515
of the C++ API reference.
1616
-}
1717

18-
{-# LANGUAGE CApiFFI #-}
18+
{-# LANGUAGE OverloadedStrings #-}
1919

2020
module Botan.Bindings.BlockCipher (
21-
BotanBlockCipherStruct
22-
, BotanBlockCipher (..)
23-
, botan_block_cipher_destroy
24-
, botan_block_cipher_init
25-
, botan_block_cipher_clear
21+
Types.Botan_block_cipher_t(..)
22+
, Types.Botan_block_cipher_struct
23+
, Safe.botan_block_cipher_destroy
24+
, botan_block_cipher_destroy_ptr
25+
, Safe.botan_block_cipher_init
26+
, Safe.botan_block_cipher_clear
2627
, botan_block_cipher_set_key
27-
, botan_block_cipher_block_size
28+
, Safe.botan_block_cipher_block_size
2829
, botan_block_cipher_encrypt_blocks
2930
, botan_block_cipher_decrypt_blocks
30-
, botan_block_cipher_name
31-
, botan_block_cipher_get_keyspec
31+
, Safe.botan_block_cipher_name
32+
, Safe.botan_block_cipher_get_keyspec
3233
-- * Available ciphers
3334
-- $available-ciphers
3435
, pattern BOTAN_BLOCK_CIPHER_AES_128
@@ -57,89 +58,27 @@ module Botan.Bindings.BlockCipher (
5758
, pattern BOTAN_BLOCK_CIPHER_TWOFISH
5859
) where
5960

60-
import Botan.Bindings.Prelude
61+
import qualified Botan.Bindings.Generated as Types
62+
import qualified Botan.Bindings.Generated.FunPtr as FunPtr
63+
import qualified Botan.Bindings.Generated.Safe as Safe
64+
import Data.String (IsString)
65+
import Data.Word (Word8)
66+
import Foreign.C.Types (CInt, CSize)
67+
import Foreign.Ptr (FunPtr, Ptr)
68+
import HsBindgen.Runtime.ConstPtr (ConstPtr)
6169

62-
{-------------------------------------------------------------------------------
63-
Block ciphers
64-
-------------------------------------------------------------------------------}
6570

66-
-- | Opaque BlockCipher struct
67-
data {-# CTYPE "botan/ffi.h" "struct botan_block_cipher_struct" #-} BotanBlockCipherStruct
68-
69-
-- | Botan BlockCipher object
70-
newtype {-# CTYPE "botan/ffi.h" "botan_block_cipher_t" #-} BotanBlockCipher
71-
= MkBotanBlockCipher { runBotanBlockCipher :: Ptr BotanBlockCipherStruct }
72-
deriving newtype (Eq, Ord, Storable)
73-
74-
-- | Destroy a block cipher object
75-
--
76-
-- NOTE: this a binding to the /address/ of the @botan_block_cipher_destroy@ C
77-
-- function.
78-
foreign import capi safe "botan/ffi.h &botan_block_cipher_destroy"
79-
botan_block_cipher_destroy
80-
:: FinalizerPtr BotanBlockCipherStruct
81-
82-
-- | Initialize a block cipher object
83-
foreign import capi safe "botan/ffi.h botan_block_cipher_init"
84-
botan_block_cipher_init
85-
:: Ptr BotanBlockCipher -- ^ __bc__
86-
-> ConstPtr CChar -- ^ __cipher_name__
87-
-> IO CInt
88-
89-
-- | Reinitializes the block cipher
90-
foreign import capi safe "botan/ffi.h botan_block_cipher_clear"
91-
botan_block_cipher_clear
92-
:: BotanBlockCipher -- ^ __bc__
93-
-> IO CInt -- ^ 0 on success, a negative value on failure
94-
95-
-- | Set the key for a block cipher instance
96-
foreign import capi safe "botan/ffi.h botan_block_cipher_set_key"
97-
botan_block_cipher_set_key
98-
:: BotanBlockCipher -- ^ __bc__
99-
-> ConstPtr Word8 -- ^ __key[]__
100-
-> CSize -- ^ __len__
101-
-> IO CInt
102-
103-
-- | Return the positive block size of this block cipher, or negative to indicate an error
104-
foreign import capi safe "botan/ffi.h botan_block_cipher_block_size"
105-
botan_block_cipher_block_size
106-
:: BotanBlockCipher -- ^ __bc__
107-
-> IO CInt
108-
109-
-- | Encrypt one or more blocks with the cipher
110-
foreign import capi safe "botan/ffi.h botan_block_cipher_encrypt_blocks"
111-
botan_block_cipher_encrypt_blocks
112-
:: BotanBlockCipher -- ^ __bc__
113-
-> ConstPtr Word8 -- ^ __in[]__
114-
-> Ptr Word8 -- ^ __out[]__
115-
-> CSize -- ^ __blocks__
116-
-> IO CInt
117-
118-
-- | Decrypt one or more blocks with the cipher
119-
foreign import capi safe "botan/ffi.h botan_block_cipher_decrypt_blocks"
120-
botan_block_cipher_decrypt_blocks
121-
:: BotanBlockCipher -- ^ __bc__
122-
-> ConstPtr Word8 -- ^ __in[]__
123-
-> Ptr Word8 -- ^ __out[]__
124-
-> CSize -- ^ __blocks__
125-
-> IO CInt
126-
127-
-- | Get the name of this block cipher
128-
foreign import capi safe "botan/ffi.h botan_block_cipher_name"
129-
botan_block_cipher_name
130-
:: BotanBlockCipher -- ^ __cipher__: the object to read
131-
-> Ptr CChar -- ^ __name__: output buffer
132-
-> Ptr CSize -- ^ __name_len__: on input, the length of buffer, on success the number of bytes written
133-
-> IO CInt
134-
135-
-- | Get the key length limits of this block cipher
136-
foreign import capi safe "botan/ffi.h botan_block_cipher_get_keyspec"
137-
botan_block_cipher_get_keyspec
138-
:: BotanBlockCipher -- ^ __cipher__: the object to read
139-
-> Ptr CSize -- ^ __out_minimum_keylength__: if non-NULL, will be set to minimum keylength of cipher
140-
-> Ptr CSize -- ^ __out_maximum_keylength__: if non-NULL, will be set to maximum keylength of cipher
141-
-> Ptr CSize -- ^ __out_keylength_modulo__: if non-NULL will be set to byte multiple of valid keys
142-
-> IO CInt
71+
botan_block_cipher_destroy_ptr :: FunPtr (Types.Botan_block_cipher_t -> IO CInt)
72+
botan_block_cipher_destroy_ptr = FunPtr.botan_block_cipher_destroy
73+
74+
botan_block_cipher_set_key :: Types.Botan_block_cipher_t -> ConstPtr Word8 -> CSize -> IO CInt
75+
botan_block_cipher_set_key = Safe.botan_block_cipher_set_key_wrapper
76+
77+
botan_block_cipher_encrypt_blocks :: Types.Botan_block_cipher_t -> ConstPtr Word8 -> Ptr Word8 -> CSize -> IO CInt
78+
botan_block_cipher_encrypt_blocks = Safe.botan_block_cipher_encrypt_blocks_wrapper
79+
80+
botan_block_cipher_decrypt_blocks :: Types.Botan_block_cipher_t -> ConstPtr Word8 -> Ptr Word8 -> CSize -> IO CInt
81+
botan_block_cipher_decrypt_blocks = Safe.botan_block_cipher_decrypt_blocks_wrapper
14382

14483
{-------------------------------------------------------------------------------
14584
Available ciphers
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{-|
2+
Module : Botan.Bindings.Cipher
3+
Description : Symmetric cipher modes
4+
Copyright : (c) 2023-2024, Apotheca Labs
5+
(c) 2024-2025, Haskell Foundation
6+
License : BSD-3-Clause
7+
8+
Stability : experimental
9+
Portability : POSIX
10+
-}
11+
12+
{-# LANGUAGE OverloadedStrings #-}
13+
14+
module Botan.Bindings.Cipher (
15+
Types.Botan_cipher_t (..)
16+
, Types.Botan_cipher_struct
17+
, botan_cipher_destroy_ptr
18+
, Types.bOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION
19+
, Types.bOTAN_CIPHER_INIT_FLAG_ENCRYPT
20+
, Types.bOTAN_CIPHER_INIT_FLAG_DECRYPT
21+
, Safe.botan_cipher_init
22+
, Safe.botan_cipher_name
23+
, Safe.botan_cipher_output_length
24+
, Safe.botan_cipher_valid_nonce_length
25+
, Safe.botan_cipher_get_tag_length
26+
, Safe.botan_cipher_get_default_nonce_length
27+
, Safe.botan_cipher_get_update_granularity
28+
, Safe.botan_cipher_get_ideal_update_granularity
29+
, Safe.botan_cipher_query_keylen
30+
, Safe.botan_cipher_get_keyspec
31+
, Safe.botan_cipher_set_key
32+
, Safe.botan_cipher_reset
33+
, Safe.botan_cipher_set_associated_data
34+
, Safe.botan_cipher_start
35+
, Types.bOTAN_CIPHER_UPDATE_FLAG_FINAL
36+
, botan_cipher_update
37+
, Safe.botan_cipher_clear
38+
39+
, pattern BOTAN_CIPHER_MODE_CBC
40+
, pattern BOTAN_CIPHER_MODE_CFB
41+
, pattern BOTAN_CIPHER_MODE_XTS
42+
43+
, pattern BOTAN_CBC_PADDING_PKCS7
44+
, pattern BOTAN_CBC_PADDING_ONE_AND_ZEROS
45+
, pattern BOTAN_CBC_PADDING_X9_23
46+
, pattern BOTAN_CBC_PADDING_ESP
47+
, pattern BOTAN_CBC_PADDING_CTS
48+
, pattern BOTAN_CBC_PADDING_NO_PADDING
49+
50+
, pattern BOTAN_AEAD_CHACHA20POLY1305
51+
52+
, pattern BOTAN_AEAD_MODE_GCM
53+
, pattern BOTAN_AEAD_MODE_OCB
54+
, pattern BOTAN_AEAD_MODE_EAX
55+
, pattern BOTAN_AEAD_MODE_SIV
56+
, pattern BOTAN_AEAD_MODE_CCM
57+
) where
58+
59+
import qualified Botan.Bindings.Generated as Types
60+
import qualified Botan.Bindings.Generated.FunPtr as FunPtr
61+
import qualified Botan.Bindings.Generated.Safe as Safe
62+
import Data.String (IsString)
63+
import Data.Word (Word32, Word8)
64+
import Foreign.C.Types (CInt, CSize)
65+
import Foreign.Ptr (FunPtr, Ptr)
66+
import HsBindgen.Runtime.ConstPtr (ConstPtr)
67+
68+
botan_cipher_destroy_ptr :: FunPtr (Types.Botan_cipher_t -> IO CInt)
69+
botan_cipher_destroy_ptr = FunPtr.botan_cipher_destroy
70+
71+
botan_cipher_update :: Types.Botan_cipher_t -> Word32 -> Ptr Word8 -> CSize -> Ptr CSize -> ConstPtr Word8 -> CSize -> Ptr CSize -> IO CInt
72+
botan_cipher_update = Safe.botan_cipher_update_wrapper
73+
74+
pattern BOTAN_CIPHER_MODE_CBC
75+
, BOTAN_CIPHER_MODE_CFB
76+
, BOTAN_CIPHER_MODE_XTS
77+
:: (Eq a, IsString a) => a
78+
79+
pattern BOTAN_CIPHER_MODE_CBC = "CBC"
80+
pattern BOTAN_CIPHER_MODE_CFB = "CFB"
81+
pattern BOTAN_CIPHER_MODE_XTS = "XTS"
82+
83+
pattern BOTAN_CBC_PADDING_PKCS7
84+
, BOTAN_CBC_PADDING_ONE_AND_ZEROS
85+
, BOTAN_CBC_PADDING_X9_23
86+
, BOTAN_CBC_PADDING_ESP
87+
, BOTAN_CBC_PADDING_CTS
88+
, BOTAN_CBC_PADDING_NO_PADDING
89+
:: (Eq a, IsString a) => a
90+
91+
pattern BOTAN_CBC_PADDING_PKCS7 = "PKCS7"
92+
pattern BOTAN_CBC_PADDING_ONE_AND_ZEROS = "OneAndZeros"
93+
pattern BOTAN_CBC_PADDING_X9_23 = "X9.23"
94+
pattern BOTAN_CBC_PADDING_ESP = "ESP"
95+
pattern BOTAN_CBC_PADDING_CTS = "CTS"
96+
pattern BOTAN_CBC_PADDING_NO_PADDING = "NoPadding"
97+
98+
pattern BOTAN_AEAD_CHACHA20POLY1305
99+
:: (Eq a, IsString a) => a
100+
101+
pattern BOTAN_AEAD_CHACHA20POLY1305 = "ChaCha20Poly1305"
102+
103+
pattern BOTAN_AEAD_MODE_GCM
104+
, BOTAN_AEAD_MODE_OCB
105+
, BOTAN_AEAD_MODE_EAX
106+
, BOTAN_AEAD_MODE_SIV
107+
, BOTAN_AEAD_MODE_CCM
108+
:: (Eq a, IsString a) => a
109+
110+
pattern BOTAN_AEAD_MODE_GCM = "GCM"
111+
pattern BOTAN_AEAD_MODE_OCB = "OCB"
112+
pattern BOTAN_AEAD_MODE_EAX = "EAX"
113+
pattern BOTAN_AEAD_MODE_SIV = "SIV"
114+
pattern BOTAN_AEAD_MODE_CCM = "CCM"

0 commit comments

Comments
 (0)