Skip to content

Commit

Permalink
Merge branch 'reduce-warnings'
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Mar 6, 2025
2 parents 8997934 + 6dabc91 commit 9560067
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [ 'ubuntu-latest', 'macOS-latest', 'windows-latest' ]
ghc: [ '9.0', '9.2', '9.4', '9.6' ]
ghc: [ '9.0', '9.2', '9.4', '9.6', '9.8', '9.10' ]

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 2 additions & 1 deletion Crypto/Cipher/AESGCMSIV.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Crypto.Cipher.AESGCMSIV (
decrypt,
) where

import Data.Maybe
import Data.Bits
import Data.Word

Expand Down Expand Up @@ -195,4 +196,4 @@ transformTag :: Bytes -> IV AES
transformTag tag = toIV $ B.copyAndFreeze tag $ \ptr ->
peekElemOff ptr 15 >>= pokeElemOff ptr 15 . (.|. (0x80 :: Word8))
where
toIV bs = let Just iv = makeIV (bs :: Bytes) in iv
toIV bs = fromJust $ makeIV (bs :: Bytes)
2 changes: 1 addition & 1 deletion Crypto/Cipher/DES/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ do_round r (ml, mr) kb = (mr, m')
comp_kb = compression_permutation kb'
expa_mr = expansion_permutation mr
res = comp_kb `desXor` expa_mr
res' = tail $ iterate (trans 6) ([], res)
res' = drop 1 $ iterate (trans 6) ([], res)
trans n (_, b) = (take n b, drop n b)
res_s =
concat $
Expand Down
3 changes: 2 additions & 1 deletion Crypto/Cipher/Twofish/Primitive.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

module Crypto.Cipher.Twofish.Primitive (
Twofish,
Expand All @@ -13,8 +14,8 @@ import Crypto.Internal.ByteArray (ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Crypto.Internal.WordArray
import Data.Bits
import Data.List
import Data.Word
import Data.List (foldl')

Check warning on line 18 in Crypto/Cipher/Twofish/Primitive.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.10)

The import of ‘Data.List’ is redundant

Check warning on line 18 in Crypto/Cipher/Twofish/Primitive.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.10)

The import of ‘Data.List’ is redundant

Check warning on line 18 in Crypto/Cipher/Twofish/Primitive.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.10)

The import of ‘Data.List’ is redundant

-- Based on the Golang referance implementation
-- https://github.com/golang/crypto/blob/master/twofish/twofish.go
Expand Down
4 changes: 3 additions & 1 deletion Crypto/MAC/CMAC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ expandIPT'
expandIPT' bytes (Q x y z) =
reverse . setB x . setB y . setB z . setB 0 $ replicate bytes 0
where
setB i ws = hd ++ setBit (head tl) r : tail tl
setB i ws = case tl of
(a:as) -> hd ++ setBit a r : as
_ -> error "expandIPT'"
where
(q, r) = i `quotRem` 8
(hd, tl) = splitAt q ws
1 change: 1 addition & 0 deletions Crypto/Number/Compat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -Wno-deprecations #-}

-- |
-- Module : Crypto.Number.Compat
Expand Down
2 changes: 1 addition & 1 deletion Crypto/Number/F2m.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Crypto.Number.F2m (

import Crypto.Number.Basic
import Data.Bits (setBit, shift, testBit, xor, unsafeShiftR)
import Data.List
import Data.List (foldl')

Check warning on line 28 in Crypto/Number/F2m.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.10)

The import of ‘Data.List’ is redundant

Check warning on line 28 in Crypto/Number/F2m.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.10)

The import of ‘Data.List’ is redundant

Check warning on line 28 in Crypto/Number/F2m.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.10)

The import of ‘Data.List’ is redundant

-- | Binary Polynomial represented by an integer
type BinaryPolynomial = Integer
Expand Down
4 changes: 3 additions & 1 deletion Crypto/PubKey/ECC/DH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ calculatePublic curve d = q
getShared :: Curve -> PrivateNumber -> PublicPoint -> SharedKey
getShared curve db qa = SharedKey $ i2ospOf_ ((nbBits + 7) `div` 8) x
where
Point x _ = pointMul curve db qa
x = case pointMul curve db qa of
Point x' _ -> x'
_ -> error "getShared"
nbBits = curveSizeBits curve
6 changes: 3 additions & 3 deletions Crypto/PubKey/Rabin/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ decrypt oaep pk c =
k = numBytes n
c' = os2ip c
solutions = rights $ toList $ mapTuple (unpad oaep k . i2ospOf_ k) $ sqroot' c' p q a b n
in if length solutions /= 1
then Nothing
else Just $ head solutions
in case solutions of
[x] -> Just x
_ -> Nothing
where
toList (w, x, y, z) = w : x : y : z : []
mapTuple f (w, x, y, z) = (f w, f x, f y, f z)
Expand Down
1 change: 0 additions & 1 deletion Crypto/Random/Probabilistic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Crypto.Random.Probabilistic (

import Crypto.Internal.Compat
import Crypto.Random
import Crypto.Random.Types

-- | This create a random number generator out of thin air with
-- the system entropy; don't generally use as the IO is not exposed
Expand Down
11 changes: 9 additions & 2 deletions tests/ECC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module ECC (tests) where

import Data.Either

import qualified Crypto.ECC as ECC
import Crypto.Error

Expand Down Expand Up @@ -50,6 +52,7 @@ data VectorPoint = VectorPoint
, vpError :: Maybe CryptoError
}

vectorsPoint :: [VectorPoint]
vectorsPoint =
[ VectorPoint
{ vpCurve = Curve ECC.Curve_P256R1
Expand Down Expand Up @@ -252,6 +255,7 @@ vectorsPoint =
}
]

vectorsWeakPoint :: [VectorPoint]
vectorsWeakPoint =
[ VectorPoint
{ vpCurve = Curve ECC.Curve_X25519
Expand Down Expand Up @@ -303,11 +307,12 @@ vectorsWeakPoint =
]

vpEncodedPoint :: VectorPoint -> ByteString
vpEncodedPoint vector = let Right bs = convertFromBase Base16 (vpHex vector) in bs
vpEncodedPoint vector = fromRight (error "vpEncodedPoint") $ convertFromBase Base16 (vpHex vector)

cryptoError :: CryptoFailable a -> Maybe CryptoError
cryptoError = onCryptoFailure Just (const Nothing)

doPointDecodeTest :: Show p => p -> VectorPoint -> TestTree
doPointDecodeTest i vector =
case vpCurve vector of
Curve curve ->
Expand All @@ -316,15 +321,17 @@ doPointDecodeTest i vector =
(show i)
(vpError vector @=? cryptoError (ECC.decodePoint prx $ vpEncodedPoint vector))

doWeakPointECDHTest :: Show p => p -> VectorPoint -> TestTree
doWeakPointECDHTest i vector =
case vpCurve vector of
Curve curve -> testCase (show i) $ do
let prx = Just curve -- using Maybe as Proxy
CryptoPassed public = ECC.decodePoint prx $ vpEncodedPoint vector
public = throwCryptoError $ ECC.decodePoint prx $ vpEncodedPoint vector
keyPair <- ECC.curveGenerateKeyPair prx
vpError vector
@=? cryptoError (ECC.ecdh prx (ECC.keypairGetPrivate keyPair) public)

tests :: TestTree
tests =
testGroup
"ECC"
Expand Down
7 changes: 5 additions & 2 deletions tests/ECDSA.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module ECDSA (tests) where

import Data.Maybe
import qualified Crypto.ECC as ECDSA
import Crypto.Error
import Crypto.Hash
Expand Down Expand Up @@ -32,6 +33,7 @@ instance Arbitrary Curve where
where
makeCurve c name = Curve c (ECC.getCurveByName name) name

arbitraryScalar :: ECC.Curve -> Gen Integer
arbitraryScalar curve = choose (1, n - 1)
where
n = ECC.ecc_n (ECC.common_curve curve)
Expand All @@ -55,6 +57,7 @@ testRecover name = testProperty (show name) $ \ (ArbitraryBS0_2901 msg) -> do
let pub = ECC.signExtendedDigestWith k key digest >>= \ signature -> ECC.recoverDigest curve signature digest
pure $ propertyHold [eqTest "recovery" (Just $ ECC.generateQ curve d) (ECC.public_q <$> pub)]

tests :: TestTree
tests = testGroup "ECDSA"
[ localOption (QuickCheckTests 5) $
testGroup
Expand Down Expand Up @@ -85,8 +88,8 @@ tests = testGroup "ECDSA"
kECDSA = throwCryptoError $ ECDSA.scalarFromInteger prx kECC
privECDSA = throwCryptoError $ ECDSA.scalarFromInteger prx d
pubECDSA = ECDSA.toPublic prx privECDSA
Just sigECC = ECC.signWith kECC privECC hashAlg msg
Just sigECDSA = ECDSA.signWith prx kECDSA privECDSA hashAlg msg
sigECC = fromJust $ ECC.signWith kECC privECC hashAlg msg
sigECDSA = fromJust $ ECDSA.signWith prx kECDSA privECDSA hashAlg msg
sigECDSA' = sigECCToECDSA prx sigECC
msg' = msg `B.append` B.singleton 42
return $
Expand Down
32 changes: 26 additions & 6 deletions tests/KAT_OTP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ totpSHA512Expected =
, (20000000000, 47863826)
]

otpKey = "12345678901234567890" :: ByteString
totpSHA256Key = "12345678901234567890123456789012" :: ByteString
otpKey :: ByteString
otpKey = "12345678901234567890"

totpSHA256Key :: ByteString
totpSHA256Key = "12345678901234567890123456789012"

totpSHA512Key :: ByteString
totpSHA512Key =
"1234567890123456789012345678901234567890123456789012345678901234" :: ByteString
"1234567890123456789012345678901234567890123456789012345678901234"

makeKATs :: (Eq a, Show a) => (t -> a) -> [(t, a)] -> [TestTree]
makeKATs otp expected = concatMap (makeTest otp) (zip3 is counts otps)
where
is :: [Int]
Expand All @@ -69,20 +75,34 @@ makeKATs otp expected = concatMap (makeTest otp) (zip3 is counts otps)
counts = map fst expected
otps = map snd expected

makeTest :: (Eq a1, Show a2, Show a1) => (t -> a1) -> (a2, t, a1) -> [TestTree]
makeTest otp (i, count, password) =
[ testCase (show i) (assertEqual "" password (otp count))
]

Right totpSHA1Params = mkTOTPParams SHA1 0 30 OTP8 TwoSteps
Right totpSHA256Params = mkTOTPParams SHA256 0 30 OTP8 TwoSteps
Right totpSHA512Params = mkTOTPParams SHA512 0 30 OTP8 TwoSteps
totpSHA1Params :: TOTPParams SHA1
totpSHA1Params = case mkTOTPParams SHA1 0 30 OTP8 TwoSteps of
Right x -> x
_ -> error "totpSHA1Params"

totpSHA256Params :: TOTPParams SHA256
totpSHA256Params = case mkTOTPParams SHA256 0 30 OTP8 TwoSteps of
Right x -> x
_ -> error "totpSHA256Params"

totpSHA512Params :: TOTPParams SHA512
totpSHA512Params = case mkTOTPParams SHA512 0 30 OTP8 TwoSteps of
Right x -> x
_ -> error "totpSHA512Params"

-- resynching with the expected value should just return the current counter + 1
prop_resyncExpected :: Word64 -> Word16 -> Bool
prop_resyncExpected ctr window = resynchronize SHA1 OTP6 window key ctr (otp, []) == Just (ctr + 1)
where
key = "1234" :: ByteString
otp = hotp SHA1 OTP6 key ctr

tests :: TestTree
tests =
testGroup
"OTP"
Expand Down
8 changes: 7 additions & 1 deletion tests/KAT_PubKey/ECC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module KAT_PubKey.ECC (eccTests, eccKatTests) where

import Control.Arrow (second)

import Data.List

import qualified Crypto.PubKey.ECC.Prim as ECC
import qualified Crypto.PubKey.ECC.Types as ECC

Expand Down Expand Up @@ -58,6 +60,7 @@ data VectorPoint = VectorPoint
, valid :: Bool
}

vectorsPoint :: [VectorPoint]
vectorsPoint =
[ VectorPoint
{ curve = ECC.getCurveByName ECC.SEC_p192r1
Expand Down Expand Up @@ -133,6 +136,7 @@ vectorsPoint =
}
]

doPointValidTest :: Show a => a -> VectorPoint -> TestTree
doPointValidTest i vector =
testCase
(show i)
Expand All @@ -147,6 +151,7 @@ arbitraryPoint aCurve =
n = ECC.ecc_n (ECC.common_curve aCurve)
pointGen = ECC.pointBaseMul aCurve <$> choose (1, n - 1)

eccTests :: TestTree
eccTests =
testGroup
"ECC"
Expand Down Expand Up @@ -182,6 +187,7 @@ eccTests =
]
]

eccKatTests :: IO TestTree
eccKatTests = do
res <-
testKatLoad "KATs/ECC-PKV.txt" (map (second (map toVector)) . katLoaderSimple)
Expand Down Expand Up @@ -224,6 +230,6 @@ eccKatTests = do
undefined
(valueHexInteger qx)
(valueHexInteger qy)
(head res /= 'F')
("F" `isPrefixOf` res)
Just _ -> error ("ERROR: " ++ show kvs)
Nothing -> error ("ERROR: " ++ show kvs) -- VectorPoint undefined 0 0 True
9 changes: 6 additions & 3 deletions tests/KAT_PubKey/RSA.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

module KAT_PubKey.RSA (rsaTests) where

import Data.Either
import Crypto.Hash
import qualified Crypto.PubKey.RSA as RSA
import qualified Crypto.PubKey.RSA.PKCS15 as RSA

import Imports

import Data.Either (isRight)

data VectorRSA = VectorRSA
{ size :: Int
, msg :: ByteString
Expand All @@ -24,6 +23,7 @@ data VectorRSA = VectorRSA
, sig :: Either RSA.Error ByteString
}

vectorsSHA1 :: [VectorRSA]
vectorsSHA1 =
[ VectorRSA
{ size = 2048 `div` 8
Expand Down Expand Up @@ -104,16 +104,19 @@ vectorToPublic vector =
vectorHasSignature :: VectorRSA -> Bool
vectorHasSignature = isRight . sig

doSignatureTest :: Show a => a -> VectorRSA -> TestTree
doSignatureTest i vector = testCase (show i) (expected @=? actual)
where
expected = sig vector
actual = RSA.sign Nothing (Just SHA1) (vectorToPrivate vector) (msg vector)

doVerifyTest :: Show a => a -> VectorRSA -> TestTree
doVerifyTest i vector = testCase (show i) (True @=? actual)
where
actual = RSA.verify (Just SHA1) (vectorToPublic vector) (msg vector) bs
Right bs = sig vector
bs = fromRight (error "doVerifyTest") $ sig vector

rsaTests :: TestTree
rsaTests =
testGroup
"RSA"
Expand Down

0 comments on commit 9560067

Please sign in to comment.