From 69f66e79087ef06cfcf9f97d8353b51f02252471 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sun, 6 Nov 2022 11:58:15 -0500 Subject: [PATCH] Turn nonce into a non-effect constant using FFI --- src/Logic.purs | 14 ++++---------- src/ffi.js | 2 ++ src/ffi.purs | 2 ++ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Logic.purs b/src/Logic.purs index 41f735a..2d36250 100644 --- a/src/Logic.purs +++ b/src/Logic.purs @@ -2,7 +2,7 @@ module Aviary.Logic where import Prelude import AviaryFormat.Format (Format, Image, Index, parseIndex) as Format -import Aviary.FFI (arrayBufferToBlob) +import Aviary.FFI (arrayBufferToBlob, nonce) import Aviary.Model (formatToMime, GalleryError(..), Image, ImageData(..), ImageError(..), Model(..)) import Control.Monad.Error.Class (try) @@ -16,11 +16,10 @@ import Data.Either (Either(..), note) import Data.Maybe (Maybe(..)) import Data.Newtype (unwrap) import Data.ArrayBuffer.ArrayBuffer (byteLength) -import Data.ArrayBuffer.Builder (DataBuff, execPut, putDataBuff, putInt8) +import Data.ArrayBuffer.Builder (DataBuff, execPut, putDataBuff) import Data.ArrayBuffer.DataView (whole) import Data.ArrayBuffer.Types (ArrayBuffer) import Data.Filterable (partitionMap) -import Data.Foldable (traverse_) import Data.String (drop) import Parsing (runParserT) import Web.File.Url (createObjectURL) @@ -34,9 +33,6 @@ import Crypto.Subtle.Key.Import (aes) import Crypto.Subtle.Key.Import (importKey) as SC import Crypto.Subtle.Key.Types (CryptoKey, decrypt, raw) -nonceE :: Effect ArrayBuffer -nonceE = execPut $ traverse_ putInt8 [0xd0, 0xc3, 0x75, 0x56, 0x58, 0xc1, 0x7e, 0x5f, 0xd6, 0xcc, 0xb6, 0x76] - databuffToBuffer :: DataBuff -> Effect ArrayBuffer databuffToBuffer = execPut <<< putDataBuff @@ -49,7 +45,6 @@ importKey key = decryptBlob :: CryptoKey -> ArrayBuffer -> ArrayBuffer -> Aff ArrayBuffer decryptBlob key cyphertext nonce = - -- nonce <- makeAff (\_ -> nonceE) Alg.decrypt (Alg.aesGCM nonce Nothing (Just t128)) key cyphertext convertImageFromProtobuf :: Format.Image -> Either GalleryError Image @@ -103,8 +98,7 @@ fetch_gallery_from_page_info = do keyB64 <- drop 1 <$> Location.hash location server <- Location.origin location fileId <- drop 1 <$> Location.pathname location - nonce <- nonceE - pure {keyB64, server, fileId, nonce} + pure {keyB64, server, fileId} case decodeKey urlInfo.keyB64 of Nothing -> pure $ GError MalformedKey Just keyBuffer -> do @@ -113,7 +107,7 @@ fetch_gallery_from_page_info = do case status of 200 -> do encryptedIndex <- arrayBuffer - serializedIndex <- try $ decryptBlob cryptoKey encryptedIndex urlInfo.nonce + serializedIndex <- try $ decryptBlob cryptoKey encryptedIndex nonce case serializedIndex of Left _ -> pure $ GError DecryptFailed Right serializedIndex' -> do diff --git a/src/ffi.js b/src/ffi.js index 1ce89c5..c0b09b0 100644 --- a/src/ffi.js +++ b/src/ffi.js @@ -3,3 +3,5 @@ export function arrayBufferToBlob(mime) { return new Blob([data], {type: mime}) } } + +export const nonce = new Uint8Array([0xd0, 0xc3, 0x75, 0x56, 0x58, 0xc1, 0x7e, 0x5f, 0xd6, 0xcc, 0xb6, 0x76]).buffer diff --git a/src/ffi.purs b/src/ffi.purs index 89f8daf..22f1c06 100644 --- a/src/ffi.purs +++ b/src/ffi.purs @@ -6,3 +6,5 @@ import Web.File.Blob (Blob) -- mimeType :: String -- blobData :: ArrayBuffer foreign import arrayBufferToBlob :: String -> ArrayBuffer -> Blob + +foreign import nonce :: ArrayBuffer