diff --git a/botan-low/CHANGELOG.md b/botan-low/CHANGELOG.md index 53b072be..e987e1df 100644 --- a/botan-low/CHANGELOG.md +++ b/botan-low/CHANGELOG.md @@ -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) diff --git a/botan-low/src/Botan/Low/PubKey/Decrypt.hs b/botan-low/src/Botan/Low/PubKey/Decrypt.hs index ea145754..e4dbd6c0 100644 --- a/botan-low/src/Botan/Low/PubKey/Decrypt.hs +++ b/botan-low/src/Botan/Low/PubKey/Decrypt.hs @@ -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 diff --git a/botan-low/src/Botan/Low/PubKey/Encrypt.hs b/botan-low/src/Botan/Low/PubKey/Encrypt.hs index 375a6b2b..5f34f24b 100644 --- a/botan-low/src/Botan/Low/PubKey/Encrypt.hs +++ b/botan-low/src/Botan/Low/PubKey/Encrypt.hs @@ -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 diff --git a/botan-low/src/Botan/Low/SRP6.hs b/botan-low/src/Botan/Low/SRP6.hs index ab2d8624..a4a4506c 100644 --- a/botan-low/src/Botan/Low/SRP6.hs +++ b/botan-low/src/Botan/Low/SRP6.hs @@ -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 ()) @@ -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) @@ -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)