Skip to content

Commit 90e8c3a

Browse files
authored
smp agent: use static RNG function to avoid dynamic C stub created by Haskell FFI wrapper (#1556)
1 parent 5685136 commit 90e8c3a

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/Simplex/Messaging/Crypto/SNTRUP761/Bindings.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import Data.Bifunctor (bimap)
1010
import Data.ByteArray (ScrubbedBytes)
1111
import qualified Data.ByteArray as BA
1212
import Data.ByteString (ByteString)
13-
import Foreign (nullPtr)
1413
import Simplex.Messaging.Agent.Store.DB (FromField (..), ToField (..))
1514
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.Defines
1615
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.FFI
17-
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.RNG (withDRG)
16+
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.RNG (rngFuncPtr, withDRG)
1817
import Simplex.Messaging.Encoding
1918
import Simplex.Messaging.Encoding.String
2019

@@ -43,7 +42,7 @@ sntrup761Keypair drg =
4342
c_SNTRUP761_SECRETKEY_SIZE
4443
( \skPtr ->
4544
BA.alloc c_SNTRUP761_PUBLICKEY_SIZE $ \pkPtr ->
46-
withDRG drg $ c_sntrup761_keypair pkPtr skPtr nullPtr
45+
withDRG drg $ \cxtPtr -> c_sntrup761_keypair pkPtr skPtr cxtPtr rngFuncPtr
4746
)
4847

4948
sntrup761Enc :: TVar ChaChaDRG -> KEMPublicKey -> IO (KEMCiphertext, KEMSharedKey)
@@ -54,7 +53,7 @@ sntrup761Enc drg (KEMPublicKey pk) =
5453
c_SNTRUP761_SIZE
5554
( \kPtr ->
5655
BA.alloc c_SNTRUP761_CIPHERTEXT_SIZE $ \cPtr ->
57-
withDRG drg $ c_sntrup761_enc cPtr kPtr pkPtr nullPtr
56+
withDRG drg $ \cxtPtr -> c_sntrup761_enc cPtr kPtr pkPtr cxtPtr rngFuncPtr
5857
)
5958

6059
sntrup761Dec :: KEMCiphertext -> KEMSecretKey -> IO KEMSharedKey
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Simplex.Messaging.Crypto.SNTRUP761.Bindings.RNG
22
( withDRG,
3+
rngFuncPtr,
34
RNGContext,
45
RNGFunc,
56
) where
@@ -12,19 +13,20 @@ import Foreign
1213
import Foreign.C
1314
import qualified Simplex.Messaging.Crypto as C
1415

15-
withDRG :: TVar ChaChaDRG -> (FunPtr RNGFunc -> IO a) -> IO a
16-
withDRG drg = bracket (createRNGFunc drg) freeHaskellFunPtr
16+
withDRG :: TVar ChaChaDRG -> (Ptr RNGContext -> IO a) -> IO a
17+
withDRG drg = bracket (castStablePtrToPtr <$> newStablePtr drg) (freeStablePtr . castPtrToStablePtr)
1718

18-
createRNGFunc :: TVar ChaChaDRG -> IO (FunPtr RNGFunc)
19-
createRNGFunc drg =
20-
mkRNGFunc $ \_ctx sz buf -> do
21-
bs <- atomically $ C.randomBytes (fromIntegral sz) drg
22-
copyByteArrayToPtr bs buf
19+
rngFunc :: RNGFunc
20+
rngFunc cxt sz buf = do
21+
drg <- deRefStablePtr $ castPtrToStablePtr cxt
22+
bs <- atomically $ C.randomBytes (fromIntegral sz) drg
23+
copyByteArrayToPtr bs buf
2324

2425
type RNGContext = ()
2526

2627
-- typedef void random_func (void *ctx, size_t length, uint8_t *dst);
2728
type RNGFunc = Ptr RNGContext -> CSize -> Ptr Word8 -> IO ()
2829

29-
foreign import ccall "wrapper"
30-
mkRNGFunc :: RNGFunc -> IO (FunPtr RNGFunc)
30+
foreign export ccall "haskell_rng_func" rngFunc :: RNGFunc
31+
32+
foreign import ccall "&haskell_rng_func" rngFuncPtr :: FunPtr RNGFunc

0 commit comments

Comments
 (0)