Skip to content

Commit e13a0d3

Browse files
authored
Merge pull request #87 from haskell-cryptography/jdral/create-and-trim
`botan-low`: Use `createAndTrim` rather than `createUpToN`
2 parents 305fab6 + 9899377 commit e13a0d3

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

botan-low/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
`blockCipherDecryptBlocks` that occasionally caused segfaults.
4242
* PATCH: fix an "insufficient buffer space" bug in
4343
`Botan.Low.PubKey.Encrypt.encrypt` and `Botan.Low.PubKey.Decrypt.decrypt`. See
44-
PR [#79](https://github.com/haskell-cryptography/botan/pull/79).
44+
PR [#79](https://github.com/haskell-cryptography/botan/pull/79), PR
45+
[#87](https://github.com/haskell-cryptography/botan/pull/87).
4546
* PATCH: Fix an "insufficient buffer space" bug in
4647
`Botan.Low.Cipher.cipherUpdate`. See PR
4748
[#84](https://github.com/haskell-cryptography/botan/pull/84)

botan-low/src/Botan/Low/PubKey/Decrypt.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ decrypt dec ctext =
7373
alloca $ \szPtr -> do
7474
sz <- decryptOutputLength dec (BS.length ctext)
7575
poke szPtr (fromIntegral sz)
76-
BSI.createUptoN sz $ \outPtr -> do
76+
BSI.createAndTrim sz $ \outPtr -> do
7777
throwBotanIfNegative_ $
7878
botan_pk_op_decrypt
7979
decPtr

botan-low/src/Botan/Low/PubKey/Encrypt.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ encrypt enc rng ptext =
7676
alloca $ \szPtr -> do
7777
sz <- encryptOutputLength enc (BS.length ptext)
7878
poke szPtr (fromIntegral sz)
79-
BSI.createUptoN sz $ \outPtr -> do
79+
BSI.createAndTrim sz $ \outPtr -> do
8080
throwBotanIfNegative_ $
8181
botan_pk_op_encrypt
8282
encPtr

botan-low/src/Botan/Low/SRP6.hs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ srp6GroupSize groupId =
433433
Utility
434434
-------------------------------------------------------------------------------}
435435

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

446-
-- | A version of 'BS.create' that also creates a pointer for the size of the
447-
-- byte string.
446+
-- | Given the maximum size needed and a function to make the contents of a
447+
-- 'ByteString', 'createWithSize' makes the 'ByteString'.
448+
--
449+
-- The generating function is required to write the actual final size (<= the
450+
-- maximum size) to the 'CSize' pointer, and the resulting byte array is
451+
-- reallocated to this size.
452+
--
453+
-- NOTE: this is based on 'BS.createAndTrim'.
448454
createWithSize ::
449455
Int
450456
-> (Ptr Word8 -> Ptr CSize -> IO ())
451457
-> IO ByteString
452458
createWithSize sz k =
453-
BS.createUptoN sz $ \bytesPtr ->
459+
BS.createAndTrim sz $ \bytesPtr ->
454460
alloca $ \lenPtr -> do
455461
poke lenPtr (fromIntegral sz)
456462
k bytesPtr lenPtr
457463
fromIntegral <$> peek lenPtr
458464

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

469-
-- | A version of 'BS.createUptoN'' that also creates a pointer for the size of
470-
-- the byte string.
475+
-- | Like 'createWithSize', but also returns an additional value created by the
476+
-- action.
477+
--
478+
-- NOTE: this is based on 'BS.createAndTrim''.
471479
createWithSize' ::
472480
Int
473481
-> (Ptr Word8 -> Ptr CSize -> IO a)
474482
-> IO (ByteString, a)
475483
createWithSize' sz k =
476-
BS.createUptoN' sz $ \bytesPtr ->
484+
BS.createAndTrim' sz $ \bytesPtr ->
477485
alloca $ \lenPtr -> do
478486
poke lenPtr (fromIntegral sz)
479487
x <- k bytesPtr lenPtr
480488
sz' <- fromIntegral <$> peek lenPtr
481-
pure (sz', x)
489+
pure (0, sz', x)

0 commit comments

Comments
 (0)