summaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/crypton/Crypto.hs38
-rw-r--r--src/crypto/reflibs/Crypto/Hash.hs4
-rw-r--r--src/crypto/reflibs/Crypto/PubKey/Ed25519.hs32
3 files changed, 56 insertions, 18 deletions
diff --git a/src/crypto/crypton/Crypto.hs b/src/crypto/crypton/Crypto.hs
index 7c98088..e84cec2 100644
--- a/src/crypto/crypton/Crypto.hs
+++ b/src/crypto/crypton/Crypto.hs
@@ -1,3 +1,5 @@
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+
module Crypto (
getEntropy,
getRandomBytes,
@@ -37,9 +39,17 @@ import Crypto.PubKey.Ed25519 qualified as Ed
import Crypto.Random
import Crypto.Random.Entropy
-import Data.ByteArray
+import Data.ByteArray (Bytes, ByteArray)
+import Data.ByteArray qualified as BA
import Data.ByteString (ByteString)
+import ConvertByteString
+
+
+instance ToByteString (Digest a) where
+ toByteString = BA.convert
+ withByteArray = BA.withByteArray
+
type AeadState = C.State
@@ -66,7 +76,7 @@ encrypt = C.encrypt
decrypt :: ByteArray ba => ( ba, Auth ) -> AeadState -> Maybe ba
decrypt ( ctext, auth ) st = do
let ( plain, st' ) = C.decrypt ctext st
- guard (C.finalize st' `constEq` auth)
+ guard (C.finalize st' `BA.constEq` auth)
return plain
aeadFinalize :: AeadState -> Auth
@@ -77,10 +87,14 @@ type EdPublicKey = Ed.PublicKey
type EdSecretKey = Ed.SecretKey
type EdSignature = Ed.Signature
+instance ToByteString EdSecretKey where
+ toByteString = BA.convert
+ withByteArray = BA.withByteArray
+
edPublicKey :: ByteString -> CryptoFailable EdPublicKey
edPublicKey = Ed.publicKey
-edSecretKey :: ByteArrayAccess ba => ba -> CryptoFailable EdSecretKey
+edSecretKey :: ScrubbedBytes -> CryptoFailable EdSecretKey
edSecretKey = Ed.secretKey
edGenerateSecretKey :: IO EdSecretKey
@@ -92,20 +106,28 @@ edToPublic = Ed.toPublic
edSignature :: ByteString -> CryptoFailable EdSignature
edSignature = Ed.signature
-edSign :: ByteArrayAccess ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature
-edSign = Ed.sign
+edSign :: ToByteString ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature
+edSign skey pkey content = Ed.sign skey pkey (toByteString content)
-edVerify :: ByteArrayAccess ba => EdPublicKey -> ba -> EdSignature -> Bool
-edVerify = Ed.verify
+edVerify :: ToByteString ba => EdPublicKey -> ba -> EdSignature -> Bool
+edVerify pkey content sig = Ed.verify pkey (toByteString content) sig
type CxPublicKey = Cx.PublicKey
type CxSecretKey = Cx.SecretKey
type DhSecret = Cx.DhSecret
+instance ToByteString CxSecretKey where
+ toByteString = BA.convert
+ withByteArray = BA.withByteArray
+
+instance ToByteString DhSecret where
+ toByteString = BA.convert
+ withByteArray = BA.withByteArray
+
cxPublicKey :: ByteString -> CryptoFailable CxPublicKey
cxPublicKey = Cx.publicKey
-cxSecretKey :: ByteArrayAccess ba => ba -> CryptoFailable CxSecretKey
+cxSecretKey :: ScrubbedBytes -> CryptoFailable CxSecretKey
cxSecretKey = Cx.secretKey
cxGenerateSecretKey :: IO CxSecretKey
diff --git a/src/crypto/reflibs/Crypto/Hash.hs b/src/crypto/reflibs/Crypto/Hash.hs
index 36308f9..4d83e5d 100644
--- a/src/crypto/reflibs/Crypto/Hash.hs
+++ b/src/crypto/reflibs/Crypto/Hash.hs
@@ -26,9 +26,11 @@ import Foreign.Storable
import System.IO.Unsafe
+import ConvertByteString
+
newtype Digest a = Digest ByteString
- deriving (Eq, Ord, NFData, ByteArrayAccess)
+ deriving (Eq, Ord, NFData, ByteArrayAccess, ToByteString)
data Context a = Context (ForeignPtr Blake2bContext)
diff --git a/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs b/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs
index 88c7302..a4c24c0 100644
--- a/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs
+++ b/src/crypto/reflibs/Crypto/PubKey/Ed25519.hs
@@ -16,10 +16,12 @@ import Crypto.Error
import Crypto.Random
import Crypto.Sign.Ed25519 qualified as Ed
-import Data.ByteArray as BA
+import Data.ByteArray qualified as BA
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
+import ConvertByteString
+
newtype EdSecretKey = EdSecretKey { fromSecretKey :: Ed.SecretKey }
deriving (Eq)
@@ -34,16 +36,28 @@ instance Show EdSecretKey where show = show . fromSecretKey
instance Show EdPublicKey where show = show . fromPublicKey
instance Show EdSignature where show = show . fromSignature
-instance ByteArrayAccess EdSecretKey where
+instance BA.ByteArrayAccess EdSecretKey where
length = BS.length . Ed.openSecretKey . fromSecretKey
- withByteArray = withByteArray . Ed.openSecretKey . fromSecretKey
+ withByteArray = BA.withByteArray . Ed.openSecretKey . fromSecretKey
-instance ByteArrayAccess EdPublicKey where
+instance BA.ByteArrayAccess EdPublicKey where
length = BS.length . Ed.openPublicKey . fromPublicKey
- withByteArray = withByteArray . Ed.openPublicKey . fromPublicKey
+ withByteArray = BA.withByteArray . Ed.openPublicKey . fromPublicKey
-instance ByteArrayAccess EdSignature where
+instance BA.ByteArrayAccess EdSignature where
length = BS.length . Ed.unSignature . fromSignature
+ withByteArray = BA.withByteArray . Ed.unSignature . fromSignature
+
+instance ToByteString EdSecretKey where
+ toByteString = Ed.openSecretKey . fromSecretKey
+ withByteArray = withByteArray . Ed.openSecretKey . fromSecretKey
+
+instance ToByteString EdPublicKey where
+ toByteString = Ed.openPublicKey . fromPublicKey
+ withByteArray = withByteArray . Ed.openPublicKey . fromPublicKey
+
+instance ToByteString EdSignature where
+ toByteString = Ed.unSignature . fromSignature
withByteArray = withByteArray . Ed.unSignature . fromSignature
signatureSize :: Int
@@ -57,16 +71,16 @@ edSignature bs
edPublicKey :: ByteString -> CryptoFailable EdPublicKey
edPublicKey = maybe (CryptoFailed CryptoError) (CryptoPassed . EdPublicKey) . Ed.makePublicKey . convert
-edSecretKey :: ByteArrayAccess ba => ba -> CryptoFailable EdSecretKey
+edSecretKey :: ScrubbedBytes -> CryptoFailable EdSecretKey
edSecretKey = maybe (CryptoFailed CryptoError) (CryptoPassed . EdSecretKey) . Ed.makeSecretKey . convert
edToPublic :: EdSecretKey -> EdPublicKey
edToPublic = EdPublicKey . Ed.secretToPublicKey . fromSecretKey
-edSign :: ByteArrayAccess ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature
+edSign :: ToByteString ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature
edSign (EdSecretKey key) _ = EdSignature . Ed.dsign key . convert
-edVerify :: ByteArrayAccess ba => EdPublicKey -> ba -> EdSignature -> Bool
+edVerify :: ToByteString ba => EdPublicKey -> ba -> EdSignature -> Bool
edVerify (EdPublicKey pub) msg (EdSignature sig) = Ed.dverify pub (convert msg) sig
edGenerateSecretKey :: IO EdSecretKey