Skip to content

Commit

Permalink
move sized-memory out of lithium
Browse files Browse the repository at this point in the history
  • Loading branch information
Promethea Raschke committed Jan 17, 2018
1 parent 2ecbffc commit 9123fe0
Show file tree
Hide file tree
Showing 30 changed files with 208 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Expand Up @@ -20,7 +20,7 @@ build:
coverage:
stage: coverage
script:
- nix-env -f "<nixpkgs>" -iA git xz make
- nix-env -f "<nixpkgs>" -iA git xz gnumake
- git clone https://github.com/jlengyel/codecov-haskell
- cd codecov-haskell
- stack install .
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -4,4 +4,4 @@ lithium uses [Semantic Versioning][].
The change log is available through the [releases on GitHub][].

[Semantic Versioning]: http://semver.org/spec/v2.0.0.html
[releases on GitHub]: https://github.com/ether-ash/lithium/releases
[releases on GitHub]: https://github.com/eth-r/lithium/releases
1 change: 1 addition & 0 deletions Crypto/Lithium/Aead.hs
Expand Up @@ -38,6 +38,7 @@ import Crypto.Lithium.Internal.Util
import Foundation
import Control.DeepSeq
import Data.ByteArray as B
import Data.ByteArray.Sized
import Data.ByteString as BS

{-|
Expand Down
2 changes: 0 additions & 2 deletions Crypto/Lithium/Internal/Util.hs
Expand Up @@ -3,7 +3,6 @@
module Crypto.Lithium.Internal.Util
( module Foundation.Foreign
, module Crypto.Lithium.Util.Init
, module Crypto.Lithium.Util.Nat
, module Crypto.Lithium.Util.Phantom
, module Crypto.Lithium.Util.Random
, module Crypto.Lithium.Util.Secret
Expand All @@ -19,7 +18,6 @@ import Foundation.Foreign
import System.IO.Unsafe ( unsafePerformIO )

import Crypto.Lithium.Util.Init
import Crypto.Lithium.Util.Nat
import Crypto.Lithium.Util.Phantom
import Crypto.Lithium.Util.Random
import Crypto.Lithium.Util.Secret
1 change: 1 addition & 0 deletions Crypto/Lithium/SecretBox.hs
Expand Up @@ -32,6 +32,7 @@ module Crypto.Lithium.SecretBox
import Crypto.Lithium.Unsafe.SecretBox (Key)
import qualified Crypto.Lithium.Unsafe.SecretBox as U
import Crypto.Lithium.Internal.Util
import Data.ByteArray.Sized
import Foundation
import Control.DeepSeq
import Data.ByteString as BS
Expand Down
42 changes: 17 additions & 25 deletions Crypto/Lithium/Types.hs
Expand Up @@ -20,31 +20,23 @@ module Crypto.Lithium.Types
, type Encoder
, type Decoder

, N
, fromN

, emptyN
, allocRetN

, maybeToN
, coerceToN
, convertN

, appendN

, takeN'
, takeN

, dropN'
, dropN

, tailN'
, tailN

, splitN'
, splitN

, xorN
, Sized
, unSized

, empty
, allocRet

, asSized
, coerce
, convert
, append
, append3
, take
, drop
, tail
, split
, split3
, xor

, Secret

Expand Down
27 changes: 14 additions & 13 deletions Crypto/Lithium/Unsafe/Aead.hs
Expand Up @@ -66,6 +66,7 @@ import Crypto.Lithium.Internal.Util
import Crypto.Lithium.Unsafe.Types

import Data.ByteArray as B
import Data.ByteArray.Sized as Sized

import Control.DeepSeq
import Foundation hiding (splitAt)
Expand Down Expand Up @@ -121,7 +122,7 @@ aead (Key key) (Nonce nonce) message aad =
-- ^ Length of associated data

(_e, ciphertext) = unsafePerformIO $
allocRet clen $ \pc ->
B.allocRet clen $ \pc ->
withSecret key $ \pk ->
withByteArray nonce $ \pn ->
withByteArray message $ \pm ->
Expand All @@ -138,7 +139,7 @@ openAead (Key k) (Nonce n) ciphertext aad =
withLithium $ -- Ensure Sodium is initialized

let (e, message) = unsafePerformIO $
allocRet (B.length ciphertext - macSize) $ \pm ->
B.allocRet (B.length ciphertext - macSize) $ \pm ->
withSecret k $ \pk ->
withByteArray n $ \pn ->
withByteArray ciphertext $ \pc ->
Expand Down Expand Up @@ -176,7 +177,7 @@ openAeadPrefix (Key key) ciphertext aad =
-- ^ Length of associated data

(e, message) = unsafePerformIO $
allocRet mlen $ \pmessage ->
B.allocRet mlen $ \pmessage ->
withSecret key $ \pkey ->
withByteArray ciphertext $ \pc ->
withByteArray aad $ \padata ->
Expand Down Expand Up @@ -208,11 +209,11 @@ aeadN :: forall l a.
aeadN (Key key) (Nonce nonce) secret aad =
withLithium $

let mlen = asNum (ByteSize @l)
let mlen = theNat @l
alen = B.length aad

(_e, ciphertext) = unsafePerformIO $
allocRetN $ \pc ->
Sized.allocRet $ \pc ->
withSecret key $ \pk ->
withByteArray nonce $ \pn ->
withSecret secret $ \pm ->
Expand All @@ -230,7 +231,7 @@ openAeadN :: forall l a.
openAeadN (Key k) (Nonce n) ciphertext aad =
withLithium $

let mlen = asNum (ByteSize @l)
let mlen = theNat @l
clen = mlen + macSize
alen = B.length aad
(e, message) = unsafePerformIO $
Expand All @@ -257,8 +258,8 @@ aeadDetached (Key key) (Nonce nonce) message aad =
alen = B.length aad

((_e, mac), ciphertext) = unsafePerformIO $
allocRet (B.length message) $ \pc ->
allocRetN $ \pmac ->
B.allocRet (B.length message) $ \pc ->
Sized.allocRet $ \pmac ->
withSecret key $ \pk ->
withByteArray nonce $ \pn ->
withByteArray message $ \pm ->
Expand All @@ -278,7 +279,7 @@ openAeadDetached (Key key) (Nonce nonce) (Mac mac) ciphertext aad =
alen = B.length aad

(e, message) = unsafePerformIO $
allocRet clen $ \pm ->
B.allocRet clen $ \pm ->
withSecret key $ \pk ->
withByteArray nonce $ \pn ->
withByteArray mac $ \pmac ->
Expand All @@ -297,12 +298,12 @@ aeadDetachedN :: forall l a. (KnownNat l, ByteArrayAccess a)
aeadDetachedN (Key key) (Nonce nonce) message aad =
withLithium $

let mlen = asNum (ByteSize @l)
let mlen = theNat @l
alen = B.length aad

((_e, mac), ciphertext) = unsafePerformIO $
allocRetN $ \pc ->
allocRetN $ \pmac ->
Sized.allocRet $ \pc ->
Sized.allocRet $ \pmac ->
withSecret key $ \pk ->
withByteArray nonce $ \pn ->
withSecret message $ \pm ->
Expand All @@ -318,7 +319,7 @@ openAeadDetachedN :: forall l a. (KnownNat l, ByteArrayAccess a)
openAeadDetachedN (Key key) (Nonce nonce) (Mac mac) ciphertext aad =
withLithium $

let clen = asNum (ByteSize @l)
let clen = theNat @l
alen = B.length aad

(e, message) = unsafePerformIO $
Expand Down
3 changes: 2 additions & 1 deletion Crypto/Lithium/Unsafe/Auth.hs
Expand Up @@ -44,6 +44,7 @@ import Crypto.Lithium.Internal.Util
import Crypto.Lithium.Unsafe.Types

import Data.ByteArray as B
import Data.ByteArray.Sized as Sized

import Control.DeepSeq
import Foundation hiding (splitAt)
Expand Down Expand Up @@ -87,7 +88,7 @@ auth (Key key) message =
-- ^ Length of message

(_e, mac) = unsafePerformIO $
allocRetN $ \pmac ->
Sized.allocRet $ \pmac ->
withSecret key $ \pkey ->
withByteArray message $ \pmessage ->
sodium_auth pmac
Expand Down
33 changes: 17 additions & 16 deletions Crypto/Lithium/Unsafe/Box.hs
Expand Up @@ -109,6 +109,7 @@ import Crypto.Lithium.Unsafe.Types
import Control.DeepSeq
import Foundation
import Data.ByteArray as B
import Data.ByteArray.Sized as Sized

{-|
Opaque 'box' secret key type, wrapping the sensitive data in 'ScrubbedBytes' to
Expand Down Expand Up @@ -172,7 +173,7 @@ makeKeypair s =

unKeypair :: Keypair -> SecretN (SecretKeyBytes + PublicKeyBytes)
unKeypair (Keypair (SecretKey sk) (PublicKey pk)) =
appendN <$> sk <*> concealN pk
Sized.append <$> sk <*> concealN pk

asKeypair :: Decoder Keypair
asKeypair = decodeSecret makeKeypair
Expand Down Expand Up @@ -221,7 +222,7 @@ fromMac = encodeWith unMac
newKeypair :: IO Keypair
newKeypair = withLithium $ do
((_e, sk), pk) <-
allocRetN $ \ppk ->
Sized.allocRet $ \ppk ->
allocSecretN $ \psk ->
sodium_box_keypair ppk psk
let sk' = SecretKey sk
Expand All @@ -231,7 +232,7 @@ newKeypair = withLithium $ do
seedKeypair :: Seed -> Keypair
seedKeypair (Seed s) = withLithium $
let ((_e, sk), pk) = unsafePerformIO $
allocRetN $ \ppk ->
Sized.allocRet $ \ppk ->
allocSecretN $ \psk ->
withSecret s $ \ps ->
sodium_box_seed_keypair ppk psk ps
Expand All @@ -251,7 +252,7 @@ box (PublicKey pk) (SecretKey sk) (Nonce n) message =
clen = mlen + macSize

(_e, ciphertext) = unsafePerformIO $
allocRet clen $ \pctext ->
B.allocRet clen $ \pctext ->
withByteArray pk $ \ppk ->
withSecret sk $ \psk ->
withByteArray n $ \pnonce ->
Expand All @@ -270,7 +271,7 @@ openBox (PublicKey pk) (SecretKey sk) (Nonce n) ciphertext =
mlen = clen - macSize

(e, message) = unsafePerformIO $
allocRet mlen $ \pmessage ->
B.allocRet mlen $ \pmessage ->
withByteArray pk $ \ppk ->
withSecret sk $ \psk ->
withByteArray n $ \pnonce ->
Expand Down Expand Up @@ -310,7 +311,7 @@ openBoxPrefix pk sk ciphertext =
-- -- ciphertext - (nonce + mac)

-- (e, message) = unsafePerformIO $
-- allocRet mlen $ \pmessage ->
-- B.allocRet mlen $ \pmessage ->
-- -- Allocate plaintext
-- withByteArray pk $ \ppk ->
-- withSecret sk $ \psk ->
Expand All @@ -331,8 +332,8 @@ detachedBox :: ByteOp m c
=> PublicKey -> SecretKey -> Nonce -> m -> (c, Mac)
detachedBox (PublicKey pk) (SecretKey sk) (Nonce n) message = withLithium $
let ((_e, mac), ciphertext) = unsafePerformIO $
allocRet (B.length message) $ \pc ->
allocRetN $ \pmac ->
B.allocRet (B.length message) $ \pc ->
Sized.allocRet $ \pmac ->
withByteArray pk $ \ppk ->
withSecret sk $ \psk ->
withByteArray n $ \pn ->
Expand All @@ -344,7 +345,7 @@ openDetachedBox :: ByteOp c m
=> PublicKey -> SecretKey -> Nonce -> Mac -> c -> Maybe m
openDetachedBox (PublicKey pk) (SecretKey sk) (Nonce n) (Mac mac) ciphertext = withLithium $
let (e, message) = unsafePerformIO $
allocRet (B.length ciphertext) $ \pm ->
B.allocRet (B.length ciphertext) $ \pm ->
withByteArray mac $ \pmac ->
withByteArray pk $ \ppk ->
withSecret sk $ \psk ->
Expand Down Expand Up @@ -405,7 +406,7 @@ box' (SharedKey k) (Nonce n) message =
clen = mlen + macSize

(_e, ciphertext) = unsafePerformIO $
allocRet clen $ \pctext ->
B.allocRet clen $ \pctext ->
withSecret k $ \pkey ->
withByteArray n $ \pnonce ->
withByteArray message $ \pmessage ->
Expand All @@ -423,7 +424,7 @@ openBox' (SharedKey k) (Nonce n) ciphertext =
mlen = clen - macSize

(e, message) = unsafePerformIO $
allocRet mlen $ \pmessage ->
B.allocRet mlen $ \pmessage ->
withSecret k $ \pkey ->
withByteArray n $ \pnonce ->
withByteArray ciphertext $ \pctext ->
Expand All @@ -438,8 +439,8 @@ detachedBox' :: ByteOp m c
=> SharedKey -> Nonce -> m -> (c, Mac)
detachedBox' (SharedKey k) (Nonce n) message = withLithium $
let ((_e, mac), ciphertext) = unsafePerformIO $
allocRet (B.length message) $ \pc ->
allocRetN $ \pmac ->
B.allocRet (B.length message) $ \pc ->
Sized.allocRet $ \pmac ->
withSecret k $ \pkey ->
withByteArray n $ \pn ->
withByteArray message $ \pm ->
Expand All @@ -452,7 +453,7 @@ openDetachedBox' :: ByteOp c m
=> SharedKey -> Nonce -> Mac -> c -> Maybe m
openDetachedBox' (SharedKey k) (Nonce n) (Mac mac) ciphertext = withLithium $
let (e, message) = unsafePerformIO $
allocRet (B.length ciphertext) $ \pm ->
B.allocRet (B.length ciphertext) $ \pm ->
withByteArray mac $ \pmac ->
withSecret k $ \pkey ->
withByteArray n $ \pn ->
Expand All @@ -475,7 +476,7 @@ sealBox (PublicKey pk) message =
clen = mlen + sealSize

(_e, ciphertext) <-
allocRet clen $ \pctext ->
B.allocRet clen $ \pctext ->
withByteArray pk $ \ppk ->
withByteArray message $ \pmessage ->
sodium_box_seal pctext
Expand All @@ -491,7 +492,7 @@ openSealedBox (Keypair (SecretKey sk) (PublicKey pk)) ciphertext =
mlen = clen - sealSize

(e, message) = unsafePerformIO $
allocRet mlen $ \pmessage ->
B.allocRet mlen $ \pmessage ->
withByteArray pk $ \ppk ->
withSecret sk $ \psk ->
withByteArray ciphertext $ \pctext ->
Expand Down
6 changes: 3 additions & 3 deletions Crypto/Lithium/Unsafe/Derive.hs
Expand Up @@ -49,10 +49,10 @@ import Foundation
import Control.DeepSeq
import Data.ByteArray as B
import Data.ByteString.Char8 as BC
import Data.ByteArray.Sized as Sized

import Crypto.Lithium.Internal.Util
import Crypto.Lithium.Internal.Derive
import Crypto.Lithium.Unsafe.Types

-- deriveable types

Expand Down Expand Up @@ -153,7 +153,7 @@ Byte arrays longer than 8 bytes are truncated
Byte arrays shorter than 8 bytes have zeros appended to the value
-}
makeContext :: ByteArray a => a -> Context
makeContext bs = Context $ coerceToN $ B.convert bs
makeContext bs = Context $ Sized.coerce $ B.convert bs

newtype Subkey (context :: Symbol) subkeyType = Subkey
{ unSubkey :: subkeyType } deriving (Eq, Ord, Show, NFData)
Expand All @@ -163,7 +163,7 @@ deriveSecretN :: forall l. (KnownNat l)
deriveSecretN (MasterKey master) (SubkeyId i) context =
withLithium $

let slen = asNum $ ByteSize @l
let slen = theNat @l
(_e, subkey) = unsafePerformIO $
allocSecretN $ \psubkey ->
withSecret master $ \pmaster ->
Expand Down

0 comments on commit 9123fe0

Please sign in to comment.