@@ -34,12 +34,12 @@ module Sel.SecretKey.Cipher
34
34
, nonceFromHexByteString
35
35
, nonceToHexByteString
36
36
37
- -- ** Hash
38
- , Hash
39
- , hashFromHexByteString
40
- , hashToBinary
41
- , hashToHexByteString
42
- , hashToHexText
37
+ -- ** Ciphertext
38
+ , Ciphertext
39
+ , ciphertextFromHexByteString
40
+ , ciphertextToBinary
41
+ , ciphertextToHexByteString
42
+ , ciphertextToHexText
43
43
) where
44
44
45
45
import Control.Monad (void , when )
@@ -75,7 +75,7 @@ import Sel.Internal.Sodium (binaryToHex)
75
75
-- $introduction
76
76
-- "Authenticated Encryption" uses a secret key along with a single-use number
77
77
-- 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.
79
79
--
80
80
-- Encryption is done with the XSalsa20 stream cipher and authentication is done with the
81
81
-- Poly1305 MAC hash.
@@ -213,7 +213,7 @@ instance Show Nonce where
213
213
-- | Generate a new random nonce.
214
214
-- Only use it once per exchanged message.
215
215
--
216
- -- Do not use this outside of hash creation!
216
+ -- Do not use this outside of ciphertext creation!
217
217
newNonce :: IO Nonce
218
218
newNonce = do
219
219
(fPtr :: ForeignPtr CUChar ) <- Foreign. mallocForeignPtrBytes (fromIntegral cryptoSecretboxNonceBytes)
@@ -255,16 +255,16 @@ nonceToHexByteString (Nonce nonceForeignPtr) =
255
255
-- | A ciphertext consisting of an encrypted message and an authentication tag.
256
256
--
257
257
-- @since 0.0.1.0
258
- data Hash = Hash
258
+ data Ciphertext = Ciphertext
259
259
{ messageLength :: CULLong
260
- , hashForeignPtr :: ForeignPtr CUChar
260
+ , ciphertextForeignPtr :: ForeignPtr CUChar
261
261
}
262
262
263
263
-- |
264
264
--
265
265
-- @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) =
268
268
let
269
269
messageLength = messageLength1 == messageLength2
270
270
content =
@@ -278,8 +278,8 @@ instance Eq Hash where
278
278
-- |
279
279
--
280
280
-- @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) =
283
283
let
284
284
messageLength = compare messageLength1 messageLength2
285
285
content =
@@ -293,69 +293,70 @@ instance Ord Hash where
293
293
-- | ⚠️ Be prudent as to what you do with it!
294
294
--
295
295
-- @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
298
298
299
299
-- | ⚠️ Be prudent as to what you do with it!
300
300
--
301
301
-- @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
304
304
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.
308
309
--
309
- -- The input hash must at least of length 'cryptoSecretboxMACBytes'
310
+ -- The input ciphertext must at least of length 'cryptoSecretboxMACBytes'.
310
311
--
311
312
-- @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
315
316
Right bytestring ->
316
317
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
322
323
pure $
323
324
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"
328
329
Left msg -> pure $ Left msg
329
330
330
- -- | Convert a 'Hash ' to a hexadecimal-encoded 'Text'.
331
+ -- | Convert a 'Ciphertext ' to a hexadecimal-encoded 'Text'.
331
332
--
332
333
-- ⚠️ Be prudent as to where you store it!
333
334
--
334
335
-- @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
337
338
338
- -- | Convert a 'Hash ' to a hexadecimal-encoded 'StrictByteString' in constant time.
339
+ -- | Convert a 'Ciphertext ' to a hexadecimal-encoded 'StrictByteString' in constant time.
339
340
--
340
341
-- ⚠️ Be prudent as to where you store it!
341
342
--
342
343
-- @since 0.0.1.0
343
- hashToHexByteString :: Hash -> StrictByteString
344
- hashToHexByteString ( Hash messageLength fPtr) =
344
+ ciphertextToHexByteString :: Ciphertext -> StrictByteString
345
+ ciphertextToHexByteString ( Ciphertext messageLength fPtr) =
345
346
binaryToHex fPtr (cryptoSecretboxMACBytes + fromIntegral messageLength)
346
347
347
- -- | Convert a 'Hash ' to a binary 'StrictByteString'.
348
+ -- | Convert a 'Ciphertext ' to a binary 'StrictByteString'.
348
349
--
349
350
-- ⚠️ Be prudent as to where you store it!
350
351
--
351
352
-- @since 0.0.1.0
352
- hashToBinary :: Hash -> StrictByteString
353
- hashToBinary ( Hash messageLength fPtr) =
353
+ ciphertextToBinary :: Ciphertext -> StrictByteString
354
+ ciphertextToBinary ( Ciphertext messageLength fPtr) =
354
355
BS. fromForeignPtr0
355
356
(Foreign. castForeignPtr fPtr)
356
357
(fromIntegral messageLength + fromIntegral cryptoSecretboxMACBytes)
357
358
358
- -- | Create an authenticated hash from a message, a secret key,
359
+ -- | Create an authenticated ciphertext from a message, a secret key,
359
360
-- and a one-time cryptographic nonce that must never be re-used with the same
360
361
-- secret key to encrypt another message.
361
362
--
@@ -365,46 +366,46 @@ encrypt
365
366
-- ^ Message to encrypt.
366
367
-> SecretKey
367
368
-- ^ Secret key generated with 'newSecretKey'.
368
- -> IO (Nonce , Hash )
369
+ -> IO (Nonce , Ciphertext )
369
370
encrypt message (SecretKey secretKeyForeignPtr) =
370
371
BS. unsafeUseAsCStringLen message $ \ (cString, cStringLen) -> do
371
372
(Nonce nonceForeignPtr) <- newNonce
372
- hashForeignPtr <-
373
+ ciphertextForeignPtr <-
373
374
Foreign. mallocForeignPtrBytes
374
375
(cStringLen + fromIntegral cryptoSecretboxMACBytes)
375
- Foreign. withForeignPtr hashForeignPtr $ \ hashPtr ->
376
+ Foreign. withForeignPtr ciphertextForeignPtr $ \ ciphertextPtr ->
376
377
Foreign. withForeignPtr secretKeyForeignPtr $ \ secretKeyPtr ->
377
378
Foreign. withForeignPtr nonceForeignPtr $ \ noncePtr -> do
378
379
void $
379
380
cryptoSecretboxEasy
380
- hashPtr
381
+ ciphertextPtr
381
382
(Foreign. castPtr @ CChar @ CUChar cString)
382
383
(fromIntegral @ Int @ CULLong cStringLen)
383
384
noncePtr
384
385
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 )
387
388
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.
389
390
--
390
391
-- @since 0.0.1.0
391
392
decrypt
392
- :: Hash
393
+ :: Ciphertext
393
394
-- ^ Encrypted message you want to decrypt.
394
395
-> SecretKey
395
396
-- ^ Secret key used for encrypting the original message.
396
397
-> Nonce
397
398
-- ^ Nonce used for encrypting the original message.
398
399
-> Maybe StrictByteString
399
- decrypt Hash {messageLength, hashForeignPtr } (SecretKey secretKeyForeignPtr) (Nonce nonceForeignPtr) = unsafeDupablePerformIO $ do
400
+ decrypt Ciphertext {messageLength, ciphertextForeignPtr } (SecretKey secretKeyForeignPtr) (Nonce nonceForeignPtr) = unsafeDupablePerformIO $ do
400
401
messagePtr <- Foreign. mallocBytes (fromIntegral @ CULLong @ Int messageLength)
401
- Foreign. withForeignPtr hashForeignPtr $ \ hashPtr ->
402
+ Foreign. withForeignPtr ciphertextForeignPtr $ \ ciphertextPtr ->
402
403
Foreign. withForeignPtr secretKeyForeignPtr $ \ secretKeyPtr ->
403
404
Foreign. withForeignPtr nonceForeignPtr $ \ noncePtr -> do
404
405
result <-
405
406
cryptoSecretboxOpenEasy
406
407
messagePtr
407
- hashPtr
408
+ ciphertextPtr
408
409
(messageLength + fromIntegral cryptoSecretboxMACBytes)
409
410
noncePtr
410
411
secretKeyPtr
0 commit comments