summaryrefslogtreecommitdiff
path: root/src/crypto/crypton/Crypto.hs
blob: 7c9808879166cbeb1a8026486b0a9b24e0292803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Crypto (
    getEntropy,
    getRandomBytes,

    Digest, Blake2b_256,
    hashInit, hashUpdates, hashFinalize,
    hashDigestSize,
    digestFromByteString,

    AeadState,
    Nonce, nonce8, nonce12,
    Auth(..), authTag,
    aeadInitialize, aeadAppendAAD,
    aeadFinalize, aeadFinalizeAAD,
    encrypt, decrypt,

    EdPublicKey, EdSecretKey, EdSignature,
    edPublicKey, edSecretKey, edGenerateSecretKey, edToPublic,
    edSignature, edSign, edVerify,
    CxPublicKey, CxSecretKey, DhSecret,
    cxPublicKey, cxSecretKey, cxGenerateSecretKey, cxToPublic,
    diffieHellman,

    CryptoFailable(..),
    maybeCryptoError,
    throwCryptoErrorIO,
) where

import Control.Monad

import Crypto.Cipher.ChaChaPoly1305 qualified as C
import Crypto.Error
import Crypto.Hash
import Crypto.MAC.Poly1305 (Auth(..), authTag)
import Crypto.PubKey.Curve25519 qualified as Cx
import Crypto.PubKey.Ed25519 qualified as Ed
import Crypto.Random
import Crypto.Random.Entropy

import Data.ByteArray
import Data.ByteString (ByteString)


type AeadState = C.State

type Nonce = C.Nonce

nonce8 :: Bytes -> Bytes -> CryptoFailable Nonce
nonce8 = C.nonce8

nonce12 :: Bytes -> CryptoFailable Nonce
nonce12 = C.nonce12

aeadInitialize :: ScrubbedBytes -> Nonce -> CryptoFailable AeadState
aeadInitialize = C.initialize

aeadAppendAAD :: ByteString -> AeadState -> AeadState
aeadAppendAAD = C.appendAAD

aeadFinalizeAAD :: AeadState -> AeadState
aeadFinalizeAAD = C.finalizeAAD

encrypt :: ByteArray ba => ba -> AeadState -> ( ba, AeadState )
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)
    return plain

aeadFinalize :: AeadState -> Auth
aeadFinalize = C.finalize


type EdPublicKey = Ed.PublicKey
type EdSecretKey = Ed.SecretKey
type EdSignature = Ed.Signature

edPublicKey :: ByteString -> CryptoFailable EdPublicKey
edPublicKey = Ed.publicKey

edSecretKey :: ByteArrayAccess ba => ba -> CryptoFailable EdSecretKey
edSecretKey = Ed.secretKey

edGenerateSecretKey :: IO EdSecretKey
edGenerateSecretKey = Ed.generateSecretKey

edToPublic :: EdSecretKey -> EdPublicKey
edToPublic = Ed.toPublic

edSignature :: ByteString -> CryptoFailable EdSignature
edSignature = Ed.signature

edSign :: ByteArrayAccess ba => EdSecretKey -> EdPublicKey -> ba -> EdSignature
edSign = Ed.sign

edVerify :: ByteArrayAccess ba => EdPublicKey -> ba -> EdSignature -> Bool
edVerify = Ed.verify

type CxPublicKey = Cx.PublicKey
type CxSecretKey = Cx.SecretKey
type DhSecret = Cx.DhSecret

cxPublicKey :: ByteString -> CryptoFailable CxPublicKey
cxPublicKey = Cx.publicKey

cxSecretKey :: ByteArrayAccess ba => ba -> CryptoFailable CxSecretKey
cxSecretKey = Cx.secretKey

cxGenerateSecretKey :: IO CxSecretKey
cxGenerateSecretKey = Cx.generateSecretKey

cxToPublic :: CxSecretKey -> CxPublicKey
cxToPublic = Cx.toPublic

diffieHellman :: CxPublicKey -> CxSecretKey -> DhSecret
diffieHellman = Cx.dh