Skip to content

Commit 8e1c2af

Browse files
committed
Rename Sel.SecretKey.Cipher.Hash to Ciphertext
1 parent c78ca0e commit 8e1c2af

File tree

2 files changed

+62
-61
lines changed

2 files changed

+62
-61
lines changed

sel/src/Sel/SecretKey/Cipher.hs

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ module Sel.SecretKey.Cipher
3434
, nonceFromHexByteString
3535
, nonceToHexByteString
3636

37-
-- ** Hash
38-
, Hash
39-
, hashFromHexByteString
40-
, hashToBinary
41-
, hashToHexByteString
42-
, hashToHexText
37+
-- ** Ciphertext
38+
, Ciphertext
39+
, ciphertextFromHexByteString
40+
, ciphertextToBinary
41+
, ciphertextToHexByteString
42+
, ciphertextToHexText
4343
) where
4444

4545
import Control.Monad (void, when)
@@ -75,7 +75,7 @@ import Sel.Internal.Sodium (binaryToHex)
7575
-- $introduction
7676
-- "Authenticated Encryption" uses a secret key along with a single-use number
7777
-- called a "nonce" to encrypt a message.
78-
-- The resulting hash is accompanied by an authentication tag.
78+
-- The resulting ciphertext is accompanied by an authentication tag.
7979
--
8080
-- Encryption is done with the XSalsa20 stream cipher and authentication is done with the
8181
-- Poly1305 MAC hash.
@@ -213,7 +213,7 @@ instance Show Nonce where
213213
-- | Generate a new random nonce.
214214
-- Only use it once per exchanged message.
215215
--
216-
-- Do not use this outside of hash creation!
216+
-- Do not use this outside of ciphertext creation!
217217
newNonce :: IO Nonce
218218
newNonce = do
219219
(fPtr :: ForeignPtr CUChar) <- Foreign.mallocForeignPtrBytes (fromIntegral cryptoSecretboxNonceBytes)
@@ -255,16 +255,16 @@ nonceToHexByteString (Nonce nonceForeignPtr) =
255255
-- | A ciphertext consisting of an encrypted message and an authentication tag.
256256
--
257257
-- @since 0.0.1.0
258-
data Hash = Hash
258+
data Ciphertext = Ciphertext
259259
{ messageLength :: CULLong
260-
, hashForeignPtr :: ForeignPtr CUChar
260+
, ciphertextForeignPtr :: ForeignPtr CUChar
261261
}
262262

263263
-- |
264264
--
265265
-- @since 0.0.1.0
266-
instance Eq Hash where
267-
(Hash messageLength1 hk1) == (Hash messageLength2 hk2) =
266+
instance Eq Ciphertext where
267+
(Ciphertext messageLength1 hk1) == (Ciphertext messageLength2 hk2) =
268268
let
269269
messageLength = messageLength1 == messageLength2
270270
content =
@@ -278,8 +278,8 @@ instance Eq Hash where
278278
-- |
279279
--
280280
-- @since 0.0.1.0
281-
instance Ord Hash where
282-
compare (Hash messageLength1 hk1) (Hash messageLength2 hk2) =
281+
instance Ord Ciphertext where
282+
compare (Ciphertext messageLength1 hk1) (Ciphertext messageLength2 hk2) =
283283
let
284284
messageLength = compare messageLength1 messageLength2
285285
content =
@@ -293,69 +293,70 @@ instance Ord Hash where
293293
-- | ⚠️ Be prudent as to what you do with it!
294294
--
295295
-- @since 0.0.1.0
296-
instance Display Hash where
297-
displayBuilder = Builder.fromText . hashToHexText
296+
instance Display Ciphertext where
297+
displayBuilder = Builder.fromText . ciphertextToHexText
298298

299299
-- | ⚠️ Be prudent as to what you do with it!
300300
--
301301
-- @since 0.0.1.0
302-
instance Show Hash where
303-
show = BS.unpackChars . hashToHexByteString
302+
instance Show Ciphertext where
303+
show = BS.unpackChars . ciphertextToHexByteString
304304

305-
-- | Create a 'Hash' from a binary 'StrictByteString' that you have obtained on your own,
306-
-- usually from the network or disk. It must be a valid hash built from the concatenation
307-
-- of the encrypted message and the authentication tag.
305+
-- | Create a 'Ciphertext' from a hexadecimal-encoded 'StrictByteString' that
306+
-- you have obtained on your own, usually from the network or disk. It must be
307+
-- a valid ciphertext built from the concatenation of the encrypted message and
308+
-- the authentication tag.
308309
--
309-
-- The input hash must at least of length 'cryptoSecretboxMACBytes'
310+
-- The input ciphertext must at least of length 'cryptoSecretboxMACBytes'.
310311
--
311312
-- @since 0.0.1.0
312-
hashFromHexByteString :: StrictByteString -> Either Text Hash
313-
hashFromHexByteString hexHash = unsafeDupablePerformIO $
314-
case Base16.decodeBase16Untyped hexHash of
313+
ciphertextFromHexByteString :: StrictByteString -> Either Text Ciphertext
314+
ciphertextFromHexByteString hexCiphertext = unsafeDupablePerformIO $
315+
case Base16.decodeBase16Untyped hexCiphertext of
315316
Right bytestring ->
316317
if BS.length bytestring >= fromIntegral cryptoSecretboxMACBytes
317-
then BS.unsafeUseAsCStringLen bytestring $ \(outsideHashPtr, outsideHashLength) -> do
318-
hashForeignPtr <- BS.mallocByteString @CChar outsideHashLength -- The foreign pointer that will receive the hash data.
319-
Foreign.withForeignPtr hashForeignPtr $ \hashPtr ->
320-
-- We copy bytes from 'outsideHashPtr' to 'hashPtr'.
321-
Foreign.copyArray hashPtr outsideHashPtr outsideHashLength
318+
then BS.unsafeUseAsCStringLen bytestring $ \(outsideCiphertextPtr, outsideCiphertextLength) -> do
319+
ciphertextForeignPtr <- BS.mallocByteString @CChar outsideCiphertextLength -- The foreign pointer that will receive the ciphertext data.
320+
Foreign.withForeignPtr ciphertextForeignPtr $ \ciphertextPtr ->
321+
-- We copy bytes from 'outsideCiphertextPtr' to 'ciphertextPtr'.
322+
Foreign.copyArray ciphertextPtr outsideCiphertextPtr outsideCiphertextLength
322323
pure $
323324
Right $
324-
Hash
325-
(fromIntegral @Int @CULLong outsideHashLength - fromIntegral @CSize @CULLong cryptoSecretboxMACBytes)
326-
(Foreign.castForeignPtr @CChar @CUChar hashForeignPtr)
327-
else pure $ Left $ Text.pack "Hash is too short"
325+
Ciphertext
326+
(fromIntegral @Int @CULLong outsideCiphertextLength - fromIntegral @CSize @CULLong cryptoSecretboxMACBytes)
327+
(Foreign.castForeignPtr @CChar @CUChar ciphertextForeignPtr)
328+
else pure $ Left $ Text.pack "Ciphertext is too short"
328329
Left msg -> pure $ Left msg
329330

330-
-- | Convert a 'Hash' to a hexadecimal-encoded 'Text'.
331+
-- | Convert a 'Ciphertext' to a hexadecimal-encoded 'Text'.
331332
--
332333
-- ⚠️ Be prudent as to where you store it!
333334
--
334335
-- @since 0.0.1.0
335-
hashToHexText :: Hash -> Text
336-
hashToHexText = Base16.extractBase16 . Base16.encodeBase16 . hashToBinary
336+
ciphertextToHexText :: Ciphertext -> Text
337+
ciphertextToHexText = Base16.extractBase16 . Base16.encodeBase16 . ciphertextToBinary
337338

338-
-- | Convert a 'Hash' to a hexadecimal-encoded 'StrictByteString' in constant time.
339+
-- | Convert a 'Ciphertext' to a hexadecimal-encoded 'StrictByteString' in constant time.
339340
--
340341
-- ⚠️ Be prudent as to where you store it!
341342
--
342343
-- @since 0.0.1.0
343-
hashToHexByteString :: Hash -> StrictByteString
344-
hashToHexByteString (Hash messageLength fPtr) =
344+
ciphertextToHexByteString :: Ciphertext -> StrictByteString
345+
ciphertextToHexByteString (Ciphertext messageLength fPtr) =
345346
binaryToHex fPtr (cryptoSecretboxMACBytes + fromIntegral messageLength)
346347

347-
-- | Convert a 'Hash' to a binary 'StrictByteString'.
348+
-- | Convert a 'Ciphertext' to a binary 'StrictByteString'.
348349
--
349350
-- ⚠️ Be prudent as to where you store it!
350351
--
351352
-- @since 0.0.1.0
352-
hashToBinary :: Hash -> StrictByteString
353-
hashToBinary (Hash messageLength fPtr) =
353+
ciphertextToBinary :: Ciphertext -> StrictByteString
354+
ciphertextToBinary (Ciphertext messageLength fPtr) =
354355
BS.fromForeignPtr0
355356
(Foreign.castForeignPtr fPtr)
356357
(fromIntegral messageLength + fromIntegral cryptoSecretboxMACBytes)
357358

358-
-- | Create an authenticated hash from a message, a secret key,
359+
-- | Create an authenticated ciphertext from a message, a secret key,
359360
-- and a one-time cryptographic nonce that must never be re-used with the same
360361
-- secret key to encrypt another message.
361362
--
@@ -365,46 +366,46 @@ encrypt
365366
-- ^ Message to encrypt.
366367
-> SecretKey
367368
-- ^ Secret key generated with 'newSecretKey'.
368-
-> IO (Nonce, Hash)
369+
-> IO (Nonce, Ciphertext)
369370
encrypt message (SecretKey secretKeyForeignPtr) =
370371
BS.unsafeUseAsCStringLen message $ \(cString, cStringLen) -> do
371372
(Nonce nonceForeignPtr) <- newNonce
372-
hashForeignPtr <-
373+
ciphertextForeignPtr <-
373374
Foreign.mallocForeignPtrBytes
374375
(cStringLen + fromIntegral cryptoSecretboxMACBytes)
375-
Foreign.withForeignPtr hashForeignPtr $ \hashPtr ->
376+
Foreign.withForeignPtr ciphertextForeignPtr $ \ciphertextPtr ->
376377
Foreign.withForeignPtr secretKeyForeignPtr $ \secretKeyPtr ->
377378
Foreign.withForeignPtr nonceForeignPtr $ \noncePtr -> do
378379
void $
379380
cryptoSecretboxEasy
380-
hashPtr
381+
ciphertextPtr
381382
(Foreign.castPtr @CChar @CUChar cString)
382383
(fromIntegral @Int @CULLong cStringLen)
383384
noncePtr
384385
secretKeyPtr
385-
let hash = Hash (fromIntegral @Int @CULLong cStringLen) hashForeignPtr
386-
pure (Nonce nonceForeignPtr, hash)
386+
let ciphertext = Ciphertext (fromIntegral @Int @CULLong cStringLen) ciphertextForeignPtr
387+
pure (Nonce nonceForeignPtr, ciphertext)
387388

388-
-- | Decrypt a hashed and authenticated message with the shared secret key and the one-time cryptographic nonce.
389+
-- | Decrypt an encrypted and authenticated message with the shared secret key and the one-time cryptographic nonce.
389390
--
390391
-- @since 0.0.1.0
391392
decrypt
392-
:: Hash
393+
:: Ciphertext
393394
-- ^ Encrypted message you want to decrypt.
394395
-> SecretKey
395396
-- ^ Secret key used for encrypting the original message.
396397
-> Nonce
397398
-- ^ Nonce used for encrypting the original message.
398399
-> Maybe StrictByteString
399-
decrypt Hash{messageLength, hashForeignPtr} (SecretKey secretKeyForeignPtr) (Nonce nonceForeignPtr) = unsafeDupablePerformIO $ do
400+
decrypt Ciphertext{messageLength, ciphertextForeignPtr} (SecretKey secretKeyForeignPtr) (Nonce nonceForeignPtr) = unsafeDupablePerformIO $ do
400401
messagePtr <- Foreign.mallocBytes (fromIntegral @CULLong @Int messageLength)
401-
Foreign.withForeignPtr hashForeignPtr $ \hashPtr ->
402+
Foreign.withForeignPtr ciphertextForeignPtr $ \ciphertextPtr ->
402403
Foreign.withForeignPtr secretKeyForeignPtr $ \secretKeyPtr ->
403404
Foreign.withForeignPtr nonceForeignPtr $ \noncePtr -> do
404405
result <-
405406
cryptoSecretboxOpenEasy
406407
messagePtr
407-
hashPtr
408+
ciphertextPtr
408409
(messageLength + fromIntegral cryptoSecretboxMACBytes)
409410
noncePtr
410411
secretKeyPtr

sel/test/Test/SecretKey/Cipher.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec =
1515
[ testCase "Encrypt a message with a secret key and a nonce" testEncryptMessage
1616
, testCase "Round-trip nonce serialisation" testNonceSerdeRoundtrip
1717
, testCase "Round-trip secret key serialisation" testSecretKeySerdeRoundtrip
18-
, testCase "Round-trip hash serialisation" testHashSerdeRoundtrip
18+
, testCase "Round-trip ciphertext serialisation" testCiphertextSerdeRoundtrip
1919
]
2020

2121
testEncryptMessage :: Assertion
@@ -41,9 +41,9 @@ testSecretKeySerdeRoundtrip = do
4141
secretKey2 <- assertRight $ secretKeyFromHexByteString . unsafeSecretKeyToHexByteString $ secretKey
4242
assertEqual "Roundtripping secret key" secretKey secretKey2
4343

44-
testHashSerdeRoundtrip :: Assertion
45-
testHashSerdeRoundtrip = do
44+
testCiphertextSerdeRoundtrip :: Assertion
45+
testCiphertextSerdeRoundtrip = do
4646
secretKey <- newSecretKey
47-
(_, hash) <- encrypt "" secretKey
48-
hash2 <- assertRight $ hashFromHexByteString . hashToHexByteString $ hash
49-
assertEqual "Roundtripping hash" hash hash2
47+
(_, ciphertext) <- encrypt "" secretKey
48+
ciphertext2 <- assertRight $ ciphertextFromHexByteString . ciphertextToHexByteString $ ciphertext
49+
assertEqual "Roundtripping ciphertext" ciphertext ciphertext2

0 commit comments

Comments
 (0)