From e2c1b82234401c29acfb7f314a287600fa196fee Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Sat, 30 May 2015 10:38:39 +0100 Subject: [PATCH] [tests] add first P256 test --- cryptonite.cabal | 1 + tests/KAT_PubKey.hs | 2 ++ tests/KAT_PubKey/P256.hs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 tests/KAT_PubKey/P256.hs diff --git a/cryptonite.cabal b/cryptonite.cabal index a7885b4..62160ea 100644 --- a/cryptonite.cabal +++ b/cryptonite.cabal @@ -254,6 +254,7 @@ Test-Suite test-cryptonite KAT_PubKey.ECDSA KAT_PubKey.OAEP KAT_PubKey.PSS + KAT_PubKey.P256 KAT_PubKey KAT_RC4 KAT_Scrypt diff --git a/tests/KAT_PubKey.hs b/tests/KAT_PubKey.hs index 8548e40..13dd71e 100644 --- a/tests/KAT_PubKey.hs +++ b/tests/KAT_PubKey.hs @@ -17,6 +17,7 @@ import KAT_PubKey.DSA import KAT_PubKey.ECC import KAT_PubKey.ECDSA import Utils +import qualified KAT_PubKey.P256 as P256 data VectorMgf = VectorMgf { seed :: ByteString , dbMask :: ByteString @@ -39,6 +40,7 @@ tests = testGroup "PubKey" , dsaTests , eccTests , ecdsaTests + , P256.tests ] --newKats = [ eccKatTests ] diff --git a/tests/KAT_PubKey/P256.hs b/tests/KAT_PubKey/P256.hs new file mode 100644 index 0000000..cb0b978 --- /dev/null +++ b/tests/KAT_PubKey/P256.hs @@ -0,0 +1,36 @@ +{-# LANGUAGE OverloadedStrings #-} +module KAT_PubKey.P256 (tests) where + +import Control.Arrow (second) + +import qualified Crypto.PubKey.ECC.Types as ECC +import qualified Crypto.PubKey.ECC.Prim as ECC +import qualified Crypto.PubKey.ECC.P256 as P256 + +import Test.Tasty.KAT +import Test.Tasty.KAT.FileLoader +import Data.ByteArray (Bytes) +import Crypto.Number.Serialize (i2ospOf) +import Crypto.Error + +import Imports + +tests = testGroup "P256" + [ testGroup "scalar" + [ testProperty "marshalling" $ \(Positive r') -> + let r = r' `mod` curveN + rBytes = i2ospScalar r + in case P256.scalarFromBinary rBytes of + CryptoFailed err -> error (show err) + CryptoPassed scalar -> rBytes `propertyEq` P256.scalarToBinary scalar + ] + ] + where + curve = ECC.getCurveByName ECC.SEC_p256r1 + curveN = ECC.ecc_n . ECC.common_curve $ curve + + i2ospScalar :: Integer -> Bytes + i2ospScalar i = + case i2ospOf 32 i of + Nothing -> error "invalid size of P256 scalar" + Just b -> b