Skip to content

Commit 4f62075

Browse files
committed
Rename Sel.PublicKey.Cipher.CipherText to Ciphertext
1 parent 8e1c2af commit 4f62075

File tree

3 files changed

+66
-66
lines changed

3 files changed

+66
-66
lines changed

sel/src/Sel/PublicKey/Cipher.hs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ module Sel.PublicKey.Cipher
3333
, nonceFromHexByteString
3434
, nonceToHexByteString
3535

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
4242

4343
-- ** Encryption and Decryption
4444
, encrypt
@@ -330,16 +330,16 @@ nonceToHexByteString (Nonce nonceForeignPtr) =
330330
-- | A ciphertext consisting of an encrypted message and an authentication tag.
331331
--
332332
-- @since 0.0.1.0
333-
data CipherText = CipherText
333+
data Ciphertext = Ciphertext
334334
{ messageLength :: CULLong
335-
, cipherTextForeignPtr :: ForeignPtr CUChar
335+
, ciphertextForeignPtr :: ForeignPtr CUChar
336336
}
337337

338338
-- |
339339
--
340340
-- @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) =
343343
let
344344
messageLength = messageLength1 == messageLength2
345345
content = foreignPtrEqConstantTime hk1 hk2 (fromIntegral messageLength1)
@@ -349,8 +349,8 @@ instance Eq CipherText where
349349
-- |
350350
--
351351
-- @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) =
354354
let
355355
messageLength = compare messageLength1 messageLength2
356356
content = foreignPtrOrdConstantTime hk1 hk2 (fromIntegral messageLength1 + cryptoBoxMACBytes)
@@ -360,69 +360,69 @@ instance Ord CipherText where
360360
-- | ⚠️ Be prudent as to what you do with it!
361361
--
362362
-- @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
365365

366366
-- | ⚠️ Be prudent as to what you do with it!
367367
--
368368
-- @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
371371

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
374374
-- of the encrypted message and the authentication tag.
375375
--
376376
-- The input cipher text, once decoded from base16, must be at least of length
377377
-- 'cryptoBoxMACBytes'.
378378
--
379379
-- @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 $
382382
case Base16.decodeBase16Untyped hexByteString of
383383
Right bytestring ->
384384
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
389389
pure $
390390
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)
394394
else pure $ Left $ Text.pack "Cipher text is too short"
395395
Left msg -> error (Text.unpack msg)
396396

397-
-- | Convert a 'CipherText' to a hexadecimal-encoded 'Text'.
397+
-- | Convert a 'Ciphertext' to a hexadecimal-encoded 'Text'.
398398
--
399399
-- ⚠️ Be prudent as to where you store it!
400400
--
401401
-- @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
404404

405-
-- | Convert a 'CipherText' to a hexadecimal-encoded 'StrictByteString' in constant time.
405+
-- | Convert a 'Ciphertext' to a hexadecimal-encoded 'StrictByteString' in constant time.
406406
--
407407
-- ⚠️ Be prudent as to where you store it!
408408
--
409409
-- @since 0.0.1.0
410-
cipherTextToHexByteString :: CipherText -> StrictByteString
411-
cipherTextToHexByteString (CipherText messageLength fPtr) =
410+
ciphertextToHexByteString :: Ciphertext -> StrictByteString
411+
ciphertextToHexByteString (Ciphertext messageLength fPtr) =
412412
binaryToHex fPtr (cryptoBoxMACBytes + fromIntegral messageLength)
413413

414-
-- | Convert a 'CipherText' to a binary 'StrictByteString'.
414+
-- | Convert a 'Ciphertext' to a binary 'StrictByteString'.
415415
--
416416
-- ⚠️ Be prudent as to where you store it!
417417
--
418418
-- @since 0.0.1.0
419-
cipherTextToBinary :: CipherText -> StrictByteString
420-
cipherTextToBinary (CipherText messageLength fPtr) =
419+
ciphertextToBinary :: Ciphertext -> StrictByteString
420+
ciphertextToBinary (Ciphertext messageLength fPtr) =
421421
BS.fromForeignPtr0
422422
(Foreign.castForeignPtr fPtr)
423423
(fromIntegral messageLength + fromIntegral cryptoBoxMACBytes)
424424

425-
-- | Create an authenticated 'CipherText' from a message, a 'SecretKey',
425+
-- | Create an authenticated 'Ciphertext' from a message, a 'SecretKey',
426426
-- and a one-time cryptographic 'Nonce' that must never be re-used with the same
427427
-- secret key to encrypt another message.
428428
--
@@ -434,33 +434,33 @@ encrypt
434434
-- ^ Public key of the recipient
435435
-> SecretKey
436436
-- ^ Secret key of the sender
437-
-> IO (Nonce, CipherText)
437+
-> IO (Nonce, Ciphertext)
438438
encrypt message (PublicKey publicKeyForeignPtr) (SecretKey secretKeyForeignPtr) =
439439
BS.unsafeUseAsCStringLen message $ \(cString, cStringLen) -> do
440440
(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 ->
443443
Foreign.withForeignPtr publicKeyForeignPtr $ \publicKeyPtr ->
444444
Foreign.withForeignPtr secretKeyForeignPtr $ \secretKeyPtr ->
445445
Foreign.withForeignPtr nonceForeignPtr $ \noncePtr -> do
446446
result <-
447447
cryptoBoxEasy
448-
cipherTextPtr
448+
ciphertextPtr
449449
(Foreign.castPtr @CChar @CUChar cString)
450450
(fromIntegral @Int @CULLong cStringLen)
451451
noncePtr
452452
publicKeyPtr
453453
secretKeyPtr
454454
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)
457457

458-
-- | Decrypt a 'CipherText' and authenticated message with the shared
458+
-- | Decrypt a 'Ciphertext' and authenticated message with the shared
459459
-- secret key and the one-time cryptographic nonce.
460460
--
461461
-- @since 0.0.1.0
462462
decrypt
463-
:: CipherText
463+
:: Ciphertext
464464
-- ^ Encrypted message you want to decrypt.
465465
-> PublicKey
466466
-- ^ Public key of the sender.
@@ -470,19 +470,19 @@ decrypt
470470
-- ^ Nonce used for encrypting the original message.
471471
-> Maybe StrictByteString
472472
decrypt
473-
CipherText{messageLength, cipherTextForeignPtr}
473+
Ciphertext{messageLength, ciphertextForeignPtr}
474474
(PublicKey publicKeyForeignPtr)
475475
(SecretKey secretKeyForeignPtr)
476476
(Nonce nonceForeignPtr) = unsafeDupablePerformIO $ do
477477
messagePtr <- Foreign.mallocBytes (fromIntegral @CULLong @Int messageLength)
478-
Foreign.withForeignPtr cipherTextForeignPtr $ \cipherTextPtr ->
478+
Foreign.withForeignPtr ciphertextForeignPtr $ \ciphertextPtr ->
479479
Foreign.withForeignPtr publicKeyForeignPtr $ \publicKeyPtr ->
480480
Foreign.withForeignPtr secretKeyForeignPtr $ \secretKeyPtr ->
481481
Foreign.withForeignPtr nonceForeignPtr $ \noncePtr -> do
482482
result <-
483483
cryptoBoxOpenEasy
484484
messagePtr
485-
cipherTextPtr
485+
ciphertextPtr
486486
(messageLength + fromIntegral @CSize @CULLong cryptoBoxMACBytes)
487487
noncePtr
488488
publicKeyPtr

sel/src/Sel/PublicKey/Seal.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import LibSodium.Bindings.SealedBoxes
4646
import System.IO.Unsafe (unsafeDupablePerformIO)
4747

4848
import Sel.PublicKey.Cipher
49-
( CipherText (CipherText)
49+
( Ciphertext (Ciphertext)
5050
, EncryptionError (..)
5151
, KeyPairGenerationException
5252
, PublicKey (PublicKey)
@@ -92,50 +92,50 @@ seal
9292
-- ^ Message to encrypt
9393
-> PublicKey
9494
-- ^ Public key of the recipient
95-
-> IO CipherText
95+
-> IO Ciphertext
9696
seal messageByteString (PublicKey publicKeyFptr) = do
9797
BS.unsafeUseAsCStringLen messageByteString $ \(messagePtr, messageLen) -> do
98-
cipherTextForeignPtr <-
98+
ciphertextForeignPtr <-
9999
Foreign.mallocForeignPtrBytes
100100
(messageLen + fromIntegral cryptoBoxSealbytes)
101101
Foreign.withForeignPtr publicKeyFptr $ \publicKeyPtr ->
102-
Foreign.withForeignPtr cipherTextForeignPtr $ \cipherTextPtr -> do
102+
Foreign.withForeignPtr ciphertextForeignPtr $ \ciphertextPtr -> do
103103
result <-
104104
cryptoBoxSeal
105-
cipherTextPtr
105+
ciphertextPtr
106106
(Foreign.castPtr @CChar @CUChar messagePtr)
107107
(fromIntegral @Int @CULLong messageLen)
108108
publicKeyPtr
109109
when (result /= 0) $ throw EncryptionError
110110
pure $
111-
CipherText
111+
Ciphertext
112112
(fromIntegral @Int @CULLong messageLen)
113-
cipherTextForeignPtr
113+
ciphertextForeignPtr
114114

115115
-- | Open a sealed message from an unknown sender.
116116
-- You need your public and secret keys.
117117
--
118118
-- @since 0.0.1.0
119119
open
120-
:: CipherText
120+
:: Ciphertext
121121
-- ^ Cipher to decrypt
122122
-> PublicKey
123123
-- ^ Public key of the recipient
124124
-> SecretKey
125125
-- ^ Secret key of the recipient
126126
-> Maybe StrictByteString
127127
open
128-
(CipherText messageLen cipherForeignPtr)
128+
(Ciphertext messageLen cipherForeignPtr)
129129
(PublicKey publicKeyFPtr)
130130
(SecretKey secretKeyFPtr) = unsafeDupablePerformIO $ do
131131
messagePtr <- Foreign.mallocBytes (fromIntegral @CULLong @Int messageLen)
132-
Foreign.withForeignPtr cipherForeignPtr $ \cipherTextPtr ->
132+
Foreign.withForeignPtr cipherForeignPtr $ \ciphertextPtr ->
133133
Foreign.withForeignPtr publicKeyFPtr $ \publicKeyPtr ->
134134
Foreign.withForeignPtr secretKeyFPtr $ \secretKeyPtr -> do
135135
result <-
136136
cryptoBoxSealOpen
137137
messagePtr
138-
cipherTextPtr
138+
ciphertextPtr
139139
(messageLen + fromIntegral @CSize @CULLong cryptoBoxSealbytes)
140140
publicKeyPtr
141141
secretKeyPtr

sel/test/Test/PublicKey/Cipher.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec =
1515
[ testCase "Encrypt a message with public-key encryption" testEncryptMessage
1616
, testCase "Round-trip nonce serialisation" testNonceSerdeRoundtrip
1717
, testCase "Round-trip keys serialisation" testKeysSerdeRoundtrip
18-
, testCase "Round-trip cipher text serialisation" testCipherTextSerdeRoundtrip
18+
, testCase "Round-trip cipher text serialisation" testCiphertextSerdeRoundtrip
1919
]
2020

2121
testEncryptMessage :: Assertion
@@ -46,10 +46,10 @@ testKeysSerdeRoundtrip = do
4646
(pk2, sk2) <- assertRight $ keyPairFromHexByteStrings hexPk hexSk
4747
assertEqual "Roundtripping keys serialisation" (pk1, sk1) (pk2, sk2)
4848

49-
testCipherTextSerdeRoundtrip :: Assertion
50-
testCipherTextSerdeRoundtrip = do
49+
testCiphertextSerdeRoundtrip :: Assertion
50+
testCiphertextSerdeRoundtrip = do
5151
(publicKey, secretKey) <- newKeyPair
52-
(_, cipherText) <- encrypt "hello hello" publicKey secretKey
53-
let hexCipherText = cipherTextToHexByteString cipherText
54-
cipherText2 <- assertRight $ cipherTextFromHexByteString hexCipherText
55-
assertEqual "Roundtripping cipher text serialisation" cipherText cipherText2
52+
(_, ciphertext) <- encrypt "hello hello" publicKey secretKey
53+
let hexCiphertext = ciphertextToHexByteString ciphertext
54+
ciphertext2 <- assertRight $ ciphertextFromHexByteString hexCiphertext
55+
assertEqual "Roundtripping cipher text serialisation" ciphertext ciphertext2

0 commit comments

Comments
 (0)