Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion botan-low/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
`blockCipherDecryptBlocks` that occasionally caused segfaults.
* PATCH: fix an "insufficient buffer space" bug in
`Botan.Low.PubKey.Encrypt.encrypt` and `Botan.Low.PubKey.Decrypt.decrypt`. See
PR [#79](https://github.com/haskell-cryptography/botan/pull/79).
PR [#79](https://github.com/haskell-cryptography/botan/pull/79), PR
[#87](https://github.com/haskell-cryptography/botan/pull/87).
* PATCH: Fix an "insufficient buffer space" bug in
`Botan.Low.Cipher.cipherUpdate`. See PR
[#84](https://github.com/haskell-cryptography/botan/pull/84)
Expand Down
2 changes: 1 addition & 1 deletion botan-low/src/Botan/Low/PubKey/Decrypt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ decrypt dec ctext =
alloca $ \szPtr -> do
sz <- decryptOutputLength dec (BS.length ctext)
poke szPtr (fromIntegral sz)
BSI.createUptoN sz $ \outPtr -> do
BSI.createAndTrim sz $ \outPtr -> do
throwBotanIfNegative_ $
botan_pk_op_decrypt
decPtr
Expand Down
2 changes: 1 addition & 1 deletion botan-low/src/Botan/Low/PubKey/Encrypt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ encrypt enc rng ptext =
alloca $ \szPtr -> do
sz <- encryptOutputLength enc (BS.length ptext)
poke szPtr (fromIntegral sz)
BSI.createUptoN sz $ \outPtr -> do
BSI.createAndTrim sz $ \outPtr -> do
throwBotanIfNegative_ $
botan_pk_op_encrypt
encPtr
Expand Down
30 changes: 19 additions & 11 deletions botan-low/src/Botan/Low/SRP6.hs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ srp6GroupSize groupId =
Utility
-------------------------------------------------------------------------------}

-- | A version of 'BS.create' that determines the size of the byte string based
-- on an argument 'DLGroupName'.
-- | Like 'createWithSize', but we determine the maximum size of the byte string
-- based on an argument 'DLGroupName'.
createWithGroupSize ::
DLGroupName
-> (Ptr Word8 -> Ptr CSize -> IO ())
Expand All @@ -443,21 +443,27 @@ createWithGroupSize groupId k = do
sz <- srp6GroupSize groupId
createWithSize sz k

-- | A version of 'BS.create' that also creates a pointer for the size of the
-- byte string.
-- | Given the maximum size needed and a function to make the contents of a
-- 'ByteString', 'createWithSize' makes the 'ByteString'.
--
-- The generating function is required to write the actual final size (<= the
-- maximum size) to the 'CSize' pointer, and the resulting byte array is
-- reallocated to this size.
--
-- NOTE: this is based on 'BS.createAndTrim'.
createWithSize ::
Int
-> (Ptr Word8 -> Ptr CSize -> IO ())
-> IO ByteString
createWithSize sz k =
BS.createUptoN sz $ \bytesPtr ->
BS.createAndTrim sz $ \bytesPtr ->
alloca $ \lenPtr -> do
poke lenPtr (fromIntegral sz)
k bytesPtr lenPtr
fromIntegral <$> peek lenPtr

-- | A version of 'BS.createUptoN'' that determines the size of the byte string
-- based on an argument 'DLGroupName'.
-- | Like 'createWithGroupSize', but also returns an additional value created by
-- the action.
createWithGroupSize' ::
DLGroupName
-> (Ptr Word8 -> Ptr CSize -> IO a)
Expand All @@ -466,16 +472,18 @@ createWithGroupSize' groupId k = do
sz <- srp6GroupSize groupId
createWithSize' sz k

-- | A version of 'BS.createUptoN'' that also creates a pointer for the size of
-- the byte string.
-- | Like 'createWithSize', but also returns an additional value created by the
-- action.
--
-- NOTE: this is based on 'BS.createAndTrim''.
createWithSize' ::
Int
-> (Ptr Word8 -> Ptr CSize -> IO a)
-> IO (ByteString, a)
createWithSize' sz k =
BS.createUptoN' sz $ \bytesPtr ->
BS.createAndTrim' sz $ \bytesPtr ->
alloca $ \lenPtr -> do
poke lenPtr (fromIntegral sz)
x <- k bytesPtr lenPtr
sz' <- fromIntegral <$> peek lenPtr
pure (sz', x)
pure (0, sz', x)