@@ -33,12 +33,12 @@ module Sel.PublicKey.Cipher
33
33
, nonceFromHexByteString
34
34
, nonceToHexByteString
35
35
36
- -- ** Cipher text
37
- , CipherText (.. )
38
- , cipherTextFromHexByteString
39
- , cipherTextToHexText
40
- , cipherTextToHexByteString
41
- , cipherTextToBinary
36
+ -- ** Ciphertext
37
+ , Ciphertext (.. )
38
+ , ciphertextFromHexByteString
39
+ , ciphertextToHexText
40
+ , ciphertextToHexByteString
41
+ , ciphertextToBinary
42
42
43
43
-- ** Encryption and Decryption
44
44
, encrypt
@@ -330,16 +330,16 @@ nonceToHexByteString (Nonce nonceForeignPtr) =
330
330
-- | A ciphertext consisting of an encrypted message and an authentication tag.
331
331
--
332
332
-- @since 0.0.1.0
333
- data CipherText = CipherText
333
+ data Ciphertext = Ciphertext
334
334
{ messageLength :: CULLong
335
- , cipherTextForeignPtr :: ForeignPtr CUChar
335
+ , ciphertextForeignPtr :: ForeignPtr CUChar
336
336
}
337
337
338
338
-- |
339
339
--
340
340
-- @since 0.0.1.0
341
- instance Eq CipherText where
342
- (CipherText messageLength1 hk1) == (CipherText messageLength2 hk2) =
341
+ instance Eq Ciphertext where
342
+ (Ciphertext messageLength1 hk1) == (Ciphertext messageLength2 hk2) =
343
343
let
344
344
messageLength = messageLength1 == messageLength2
345
345
content = foreignPtrEqConstantTime hk1 hk2 (fromIntegral messageLength1)
@@ -349,8 +349,8 @@ instance Eq CipherText where
349
349
-- |
350
350
--
351
351
-- @since 0.0.1.0
352
- instance Ord CipherText where
353
- compare (CipherText messageLength1 hk1) (CipherText messageLength2 hk2) =
352
+ instance Ord Ciphertext where
353
+ compare (Ciphertext messageLength1 hk1) (Ciphertext messageLength2 hk2) =
354
354
let
355
355
messageLength = compare messageLength1 messageLength2
356
356
content = foreignPtrOrdConstantTime hk1 hk2 (fromIntegral messageLength1 + cryptoBoxMACBytes)
@@ -360,69 +360,69 @@ instance Ord CipherText where
360
360
-- | ⚠️ Be prudent as to what you do with it!
361
361
--
362
362
-- @since 0.0.1.0
363
- instance Display CipherText where
364
- displayBuilder = Builder. fromText . cipherTextToHexText
363
+ instance Display Ciphertext where
364
+ displayBuilder = Builder. fromText . ciphertextToHexText
365
365
366
366
-- | ⚠️ Be prudent as to what you do with it!
367
367
--
368
368
-- @since 0.0.1.0
369
- instance Show CipherText where
370
- show = BS. unpackChars . cipherTextToHexByteString
369
+ instance Show Ciphertext where
370
+ show = BS. unpackChars . ciphertextToHexByteString
371
371
372
- -- | Create a 'CipherText ' from a binary 'StrictByteString' that you have obtained on your own,
373
- -- usually from the network or disk. It must be a valid cipherText built from the concatenation
372
+ -- | Create a 'Ciphertext ' from a binary 'StrictByteString' that you have obtained on your own,
373
+ -- usually from the network or disk. It must be a valid ciphertext built from the concatenation
374
374
-- of the encrypted message and the authentication tag.
375
375
--
376
376
-- The input cipher text, once decoded from base16, must be at least of length
377
377
-- 'cryptoBoxMACBytes'.
378
378
--
379
379
-- @since 0.0.1.0
380
- cipherTextFromHexByteString :: StrictByteString -> Either Text CipherText
381
- cipherTextFromHexByteString hexByteString = unsafeDupablePerformIO $
380
+ ciphertextFromHexByteString :: StrictByteString -> Either Text Ciphertext
381
+ ciphertextFromHexByteString hexByteString = unsafeDupablePerformIO $
382
382
case Base16. decodeBase16Untyped hexByteString of
383
383
Right bytestring ->
384
384
if BS. length bytestring >= fromIntegral cryptoBoxMACBytes
385
- then BS. unsafeUseAsCStringLen bytestring $ \ (outsideCipherTextPtr, outsideCipherTextLength ) -> do
386
- cipherTextForeignPtr <- BS. mallocByteString @ CChar outsideCipherTextLength
387
- Foreign. withForeignPtr cipherTextForeignPtr $ \ cipherTextPtr ->
388
- Foreign. copyArray cipherTextPtr outsideCipherTextPtr outsideCipherTextLength
385
+ then BS. unsafeUseAsCStringLen bytestring $ \ (outsideCiphertextPtr, outsideCiphertextLength ) -> do
386
+ ciphertextForeignPtr <- BS. mallocByteString @ CChar outsideCiphertextLength
387
+ Foreign. withForeignPtr ciphertextForeignPtr $ \ ciphertextPtr ->
388
+ Foreign. copyArray ciphertextPtr outsideCiphertextPtr outsideCiphertextLength
389
389
pure $
390
390
Right $
391
- CipherText
392
- (fromIntegral @ Int @ CULLong outsideCipherTextLength - fromIntegral @ CSize @ CULLong cryptoBoxMACBytes)
393
- (Foreign. castForeignPtr @ CChar @ CUChar cipherTextForeignPtr )
391
+ Ciphertext
392
+ (fromIntegral @ Int @ CULLong outsideCiphertextLength - fromIntegral @ CSize @ CULLong cryptoBoxMACBytes)
393
+ (Foreign. castForeignPtr @ CChar @ CUChar ciphertextForeignPtr )
394
394
else pure $ Left $ Text. pack " Cipher text is too short"
395
395
Left msg -> error (Text. unpack msg)
396
396
397
- -- | Convert a 'CipherText ' to a hexadecimal-encoded 'Text'.
397
+ -- | Convert a 'Ciphertext ' to a hexadecimal-encoded 'Text'.
398
398
--
399
399
-- ⚠️ Be prudent as to where you store it!
400
400
--
401
401
-- @since 0.0.1.0
402
- cipherTextToHexText :: CipherText -> Text
403
- cipherTextToHexText = Base16. extractBase16 . Base16. encodeBase16 . cipherTextToBinary
402
+ ciphertextToHexText :: Ciphertext -> Text
403
+ ciphertextToHexText = Base16. extractBase16 . Base16. encodeBase16 . ciphertextToBinary
404
404
405
- -- | Convert a 'CipherText ' to a hexadecimal-encoded 'StrictByteString' in constant time.
405
+ -- | Convert a 'Ciphertext ' to a hexadecimal-encoded 'StrictByteString' in constant time.
406
406
--
407
407
-- ⚠️ Be prudent as to where you store it!
408
408
--
409
409
-- @since 0.0.1.0
410
- cipherTextToHexByteString :: CipherText -> StrictByteString
411
- cipherTextToHexByteString ( CipherText messageLength fPtr) =
410
+ ciphertextToHexByteString :: Ciphertext -> StrictByteString
411
+ ciphertextToHexByteString ( Ciphertext messageLength fPtr) =
412
412
binaryToHex fPtr (cryptoBoxMACBytes + fromIntegral messageLength)
413
413
414
- -- | Convert a 'CipherText ' to a binary 'StrictByteString'.
414
+ -- | Convert a 'Ciphertext ' to a binary 'StrictByteString'.
415
415
--
416
416
-- ⚠️ Be prudent as to where you store it!
417
417
--
418
418
-- @since 0.0.1.0
419
- cipherTextToBinary :: CipherText -> StrictByteString
420
- cipherTextToBinary ( CipherText messageLength fPtr) =
419
+ ciphertextToBinary :: Ciphertext -> StrictByteString
420
+ ciphertextToBinary ( Ciphertext messageLength fPtr) =
421
421
BS. fromForeignPtr0
422
422
(Foreign. castForeignPtr fPtr)
423
423
(fromIntegral messageLength + fromIntegral cryptoBoxMACBytes)
424
424
425
- -- | Create an authenticated 'CipherText ' from a message, a 'SecretKey',
425
+ -- | Create an authenticated 'Ciphertext ' from a message, a 'SecretKey',
426
426
-- and a one-time cryptographic 'Nonce' that must never be re-used with the same
427
427
-- secret key to encrypt another message.
428
428
--
@@ -434,33 +434,33 @@ encrypt
434
434
-- ^ Public key of the recipient
435
435
-> SecretKey
436
436
-- ^ Secret key of the sender
437
- -> IO (Nonce , CipherText )
437
+ -> IO (Nonce , Ciphertext )
438
438
encrypt message (PublicKey publicKeyForeignPtr) (SecretKey secretKeyForeignPtr) =
439
439
BS. unsafeUseAsCStringLen message $ \ (cString, cStringLen) -> do
440
440
(Nonce nonceForeignPtr) <- newNonce
441
- cipherTextForeignPtr <- Foreign. mallocForeignPtrBytes (cStringLen + fromIntegral cryptoBoxMACBytes)
442
- Foreign. withForeignPtr cipherTextForeignPtr $ \ cipherTextPtr ->
441
+ ciphertextForeignPtr <- Foreign. mallocForeignPtrBytes (cStringLen + fromIntegral cryptoBoxMACBytes)
442
+ Foreign. withForeignPtr ciphertextForeignPtr $ \ ciphertextPtr ->
443
443
Foreign. withForeignPtr publicKeyForeignPtr $ \ publicKeyPtr ->
444
444
Foreign. withForeignPtr secretKeyForeignPtr $ \ secretKeyPtr ->
445
445
Foreign. withForeignPtr nonceForeignPtr $ \ noncePtr -> do
446
446
result <-
447
447
cryptoBoxEasy
448
- cipherTextPtr
448
+ ciphertextPtr
449
449
(Foreign. castPtr @ CChar @ CUChar cString)
450
450
(fromIntegral @ Int @ CULLong cStringLen)
451
451
noncePtr
452
452
publicKeyPtr
453
453
secretKeyPtr
454
454
when (result /= 0 ) $ throw EncryptionError
455
- let cipherText = CipherText (fromIntegral @ Int @ CULLong cStringLen) cipherTextForeignPtr
456
- pure (Nonce nonceForeignPtr, cipherText )
455
+ let ciphertext = Ciphertext (fromIntegral @ Int @ CULLong cStringLen) ciphertextForeignPtr
456
+ pure (Nonce nonceForeignPtr, ciphertext )
457
457
458
- -- | Decrypt a 'CipherText ' and authenticated message with the shared
458
+ -- | Decrypt a 'Ciphertext ' and authenticated message with the shared
459
459
-- secret key and the one-time cryptographic nonce.
460
460
--
461
461
-- @since 0.0.1.0
462
462
decrypt
463
- :: CipherText
463
+ :: Ciphertext
464
464
-- ^ Encrypted message you want to decrypt.
465
465
-> PublicKey
466
466
-- ^ Public key of the sender.
@@ -470,19 +470,19 @@ decrypt
470
470
-- ^ Nonce used for encrypting the original message.
471
471
-> Maybe StrictByteString
472
472
decrypt
473
- CipherText {messageLength, cipherTextForeignPtr }
473
+ Ciphertext {messageLength, ciphertextForeignPtr }
474
474
(PublicKey publicKeyForeignPtr)
475
475
(SecretKey secretKeyForeignPtr)
476
476
(Nonce nonceForeignPtr) = unsafeDupablePerformIO $ do
477
477
messagePtr <- Foreign. mallocBytes (fromIntegral @ CULLong @ Int messageLength)
478
- Foreign. withForeignPtr cipherTextForeignPtr $ \ cipherTextPtr ->
478
+ Foreign. withForeignPtr ciphertextForeignPtr $ \ ciphertextPtr ->
479
479
Foreign. withForeignPtr publicKeyForeignPtr $ \ publicKeyPtr ->
480
480
Foreign. withForeignPtr secretKeyForeignPtr $ \ secretKeyPtr ->
481
481
Foreign. withForeignPtr nonceForeignPtr $ \ noncePtr -> do
482
482
result <-
483
483
cryptoBoxOpenEasy
484
484
messagePtr
485
- cipherTextPtr
485
+ ciphertextPtr
486
486
(messageLength + fromIntegral @ CSize @ CULLong cryptoBoxMACBytes)
487
487
noncePtr
488
488
publicKeyPtr
0 commit comments