git.iko.soy/rowtype-yoga/purescript-yoga-subtlecrypto
on master help sign in
wrap/unwrap, decrypt
commitc9a4a76985b1e4ab73df9efaf66f5a9c6d1513ae authorAthan Clark <athan.clark@localcooking.com> dateWed 16 Jan 2019, 23:26 (8 years ago) parent65f0ed1
5 files changed+142−2
src/Crypto/Subtle/AES.purs+22−2 src/Crypto/Subtle/Encrypt.js+4 src/Crypto/Subtle/Encrypt.purs+72 src/Crypto/Subtle/Key/Wrap.js+4 src/Crypto/Subtle/Key/Wrap.purs+40
src/Crypto/Subtle/AES.purs+22−2
@@ -1,6 +1,7 @@
11module Crypto.Subtle.AES
22 ( AESAlgorithm, aesCTR, aesCBC, aesGCM, aesKW
33 , AESBitLength, l128, l192, l256
4+ , AESTagLength, t32, t64, t96, t104, t112, t120, t128
45 ) where
56
67
@@ -10,11 +11,11 @@ newtype AESAlgorithm = AESAlgorithm String
1011aesCTR :: AESAlgorithm
1112aesCTR = AESAlgorithm "AES-CTR"
1213aesCBC :: AESAlgorithm
13−aesCBC = AESAlgorithm "ES-CBC"
14+aesCBC = AESAlgorithm "AES-CBC"
1415aesGCM :: AESAlgorithm
1516aesGCM = AESAlgorithm "AES-GCM"
1617aesKW :: AESAlgorithm
17−aesKW = AESAlgorithm "ES-KW"
18+aesKW = AESAlgorithm "AES-KW"
1819
1920
2021
@@ -26,3 +27,22 @@ l192 :: AESBitLength
2627l192 = AESBitLength 192
2728l256 :: AESBitLength
2829l256 = AESBitLength 256
30+
31+
32+
33+newtype AESTagLength = AESTagLength Int
34+
35+t32 :: AESTagLength
36+t32 = AESTagLength 32
37+t64 :: AESTagLength
38+t64 = AESTagLength 64
39+t96 :: AESTagLength
40+t96 = AESTagLength 96
41+t104 :: AESTagLength
42+t104 = AESTagLength 104
43+t112 :: AESTagLength
44+t112 = AESTagLength 112
45+t120 :: AESTagLength
46+t120 = AESTagLength 120
47+t128 :: AESTagLength
48+t128 = AESTagLength 128
src/Crypto/Subtle/Encrypt.jsnew file+4
@@ -0,0 +1,4 @@
1+"use strict";
2+
3+exports.encryptImpl = crypto.subtle.encrypt;
4+exports.decryptImpl = crypto.subtle.decrypt;
src/Crypto/Subtle/Encrypt.pursnew file+72
@@ -0,0 +1,72 @@
1+module Crypto.Subtle.Encrypt
2+ ( encrypt, decrypt
3+ , EncryptAlgorithm, rsaOAEP, aesCTR, aesCBC, aesGCM, aesKW
4+ ) where
5+
6+import Crypto.Subtle.Key.Types (CryptoKey)
7+import Crypto.Subtle.AES (AESTagLength)
8+
9+import Prelude ((<<<), (<$))
10+import Data.Function.Uncurried (Fn3, runFn3)
11+import Data.Tuple (Tuple (..))
12+import Data.Maybe (Maybe (..))
13+import Data.Either (Either (..))
14+import Data.ArrayBuffer.Types (ArrayBuffer)
15+import Effect.Promise (Promise, runPromise)
16+import Effect.Aff (Aff, makeAff, nonCanceler)
17+import Unsafe.Coerce (unsafeCoerce)
18+
19+
20+
21+foreign import data EncryptAlgorithm :: Type
22+
23+rsaOAEP :: Maybe ArrayBuffer -- ^ Label
24+ -> EncryptAlgorithm
25+rsaOAEP mL = case mL of
26+ Nothing -> unsafeCoerce {name: "RSA_OAEP"}
27+ Just l -> unsafeCoerce {name: "RSA_OAEP", label: l}
28+
29+aesCTR :: ArrayBuffer -- ^ Counter
30+ -> Int -- ^ Counter length
31+ -> EncryptAlgorithm
32+aesCTR c l = unsafeCoerce {name: "AES-CTR", counter: c, length: l}
33+
34+aesCBC :: ArrayBuffer -- ^ Initialization vector
35+ -> EncryptAlgorithm
36+aesCBC i = unsafeCoerce {name: "AES-CBC", iv: i}
37+
38+aesGCM :: ArrayBuffer -- ^ Initialization vector
39+ -> Maybe ArrayBuffer -- ^ Additional data
40+ -> Maybe AESTagLength -- ^ Tag length
41+ -> EncryptAlgorithm
42+aesGCM i mD mT = case Tuple mD mT of
43+ Tuple Nothing Nothing -> unsafeCoerce {name: "AES-GCM", iv: i}
44+ Tuple (Just d) (Just t) -> unsafeCoerce {name: "AES-GCM", iv: i, additionalData: d, tagLength: t}
45+ Tuple (Just d) Nothing -> unsafeCoerce {name: "AES-GCM", iv: i, additionalData: d}
46+ Tuple Nothing (Just t) -> unsafeCoerce {name: "AES-GCM", iv: i, tagLength: t}
47+
48+aesKW :: EncryptAlgorithm
49+aesKW = unsafeCoerce {name: "AES-KW"}
50+
51+
52+
53+
54+foreign import encryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
55+
56+encrypt :: EncryptAlgorithm
57+ -> CryptoKey
58+ -> ArrayBuffer
59+ -> Aff ArrayBuffer
60+encrypt a k x = makeAff \resolve ->
61+ nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 encryptImpl a k x)
62+
63+
64+
65+foreign import decryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer)
66+
67+decrypt :: EncryptAlgorithm
68+ -> CryptoKey
69+ -> ArrayBuffer
70+ -> Aff (Maybe ArrayBuffer)
71+decrypt a k x = makeAff \resolve ->
72+ nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn3 decryptImpl a k x)
src/Crypto/Subtle/Key/Wrap.jsnew file+4
@@ -0,0 +1,4 @@
1+"use strict";
2+
3+exports.wrapKeyImpl = crypto.subtle.wrapKey;
4+exports.unwrapKeyImpl = crypto.subtle.unwrapKey;
src/Crypto/Subtle/Key/Wrap.pursnew file+40
@@ -0,0 +1,40 @@
1+module Crypto.Subtle.Key.Wrap
2+ where
3+
4+import Crypto.Subtle.Key.Types (CryptoKey, ExternalFormat, CryptoKeyUsage)
5+import Crypto.Subtle.Key.Import (ImportAlgorithm)
6+import Crypto.Subtle.Encrypt (EncryptAlgorithm)
7+
8+import Prelude ((<<<), (<$))
9+import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7)
10+import Data.Maybe (Maybe (..))
11+import Data.Either (Either (..))
12+import Data.ArrayBuffer.Types (ArrayBuffer)
13+import Effect.Promise (Promise, runPromise)
14+import Effect.Aff (Aff, makeAff, nonCanceler)
15+
16+
17+
18+foreign import wrapKeyImpl :: Fn4 ExternalFormat CryptoKey CryptoKey EncryptAlgorithm (Promise ArrayBuffer)
19+
20+wrapKey :: ExternalFormat
21+ -> CryptoKey -- ^ Subject key
22+ -> CryptoKey -- ^ Wrapping key
23+ -> EncryptAlgorithm
24+ -> Aff ArrayBuffer
25+wrapKey f x k a = makeAff \resolve ->
26+ nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 wrapKeyImpl f x k a)
27+
28+
29+foreign import unwrapKeyImpl :: Fn7 ExternalFormat ArrayBuffer CryptoKey EncryptAlgorithm ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey)
30+
31+unwrapKey :: ExternalFormat
32+ -> ArrayBuffer -- ^ Wrapped key
33+ -> CryptoKey -- ^ Unwrapping key
34+ -> EncryptAlgorithm
35+ -> ImportAlgorithm
36+ -> Boolean -- ^ Extractable
37+ -> Array CryptoKeyUsage
38+ -> Aff (Maybe CryptoKey)
39+unwrapKey f x k a i e u = makeAff \resolve ->
40+ nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn7 unwrapKeyImpl f x k a i e u)