summaryrefslogtreecommitdiff
path: root/src/crypto/reflibs/Crypto
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2026-10-27 22:08:47 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2026-11-06 21:06:49 +0100
commite0762612e95b0816721165c5aba6189809fcde58 (patch)
tree57a2b7eed322086e6e19bfa652798d3dceceb6f6 /src/crypto/reflibs/Crypto
parent0bb8daa7da52240a971a4ce83d97cfb62971ea78 (diff)
ByteString conversion classes
Diffstat (limited to 'src/crypto/reflibs/Crypto')
-rw-r--r--src/crypto/reflibs/Crypto/Hash.hs4
-rw-r--r--src/crypto/reflibs/Crypto/PubKey/Ed25519.hs32
2 files changed, 26 insertions, 10 deletions
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