{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE StandaloneDeriving #-}

-- | Stability: experimental
-- This module contains definitions for [COSE registry](https://www.iana.org/assignments/cose/cose.xhtml)
-- entries that are relevant for Webauthn COSE public keys. All the types in
-- this module implement the 'Serialise' class, mapping them to the respective
-- CBOR values/labels.
--
-- This modules sometimes uses this
-- [CBOR Grammar](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-struct-13#section-1.4)
-- to describe CBOR value types corresponding to CBOR parameters
module Crypto.WebAuthn.Cose.SignAlg
  ( -- * COSE Algorithms
    CoseSignAlg
      ( ..,
        CoseAlgorithmEdDSA,
        CoseAlgorithmES256,
        CoseAlgorithmES384,
        CoseAlgorithmES512,
        CoseAlgorithmRS256,
        CoseAlgorithmRS384,
        CoseAlgorithmRS512,
        CoseAlgorithmRS1
      ),
    fromCoseSignAlg,
    toCoseSignAlg,

    -- * Hash Algorithms
    CoseHashAlgECDSA (..),
    CoseHashAlgRSA (..),
  )
where

import Codec.CBOR.Decoding (decodeIntCanonical)
import Codec.CBOR.Encoding (encodeInt)
import Codec.Serialise (Serialise)
import Codec.Serialise.Class (decode, encode)
import Data.Aeson (ToJSON)
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.Generics (Generic)

-- | [(spec)](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- All the entries from the [COSE Algorithms registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- limited to the ones that are currently needed for Webauthn. Notably we only
-- care about asymmetric signature algorithms
data CoseSignAlg
  = -- | [(spec)](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.2)
    -- EdDSA
    --
    -- [RFC8032](https://datatracker.ietf.org/doc/html/rfc8032) describes the elliptic curve signature scheme Edwards-curve
    -- Digital Signature Algorithm (EdDSA).  In that document, the signature
    -- algorithm is instantiated using parameters for edwards25519 and
    -- edwards448 curves.  The document additionally describes two variants
    -- of the EdDSA algorithm: Pure EdDSA, where no hash function is applied
    -- to the content before signing, and HashEdDSA, where a hash function
    -- is applied to the content before signing and the result of that hash
    -- function is signed.  For EdDSA, the content to be signed (either the
    -- message or the pre-hash value) is processed twice inside of the
    -- signature algorithm.  For use with COSE, only the pure EdDSA version
    -- is used.
    --
    -- Security considerations are [here](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.2.1)
    CoseSignAlgEdDSA
  | -- | [(spec)](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.1)
    -- ECDSA
    --
    -- ECDSA [DSS] defines a signature algorithm using ECC.  Implementations
    -- SHOULD use a deterministic version of ECDSA such as the one defined
    -- in [RFC6979].
    --
    -- The ECDSA signature algorithm is parameterized with a hash function
    -- (h).  In the event that the length of the hash function output is
    -- greater than the group of the key, the leftmost bytes of the hash
    -- output are used.
    -- ECDSA w/ SHA-256
    --
    -- This document defines ECDSA to work only with the curves P-256,
    -- P-384, and P-521. Future documents may define it to work with other
    -- curves and points in the future.
    --
    -- In order to promote interoperability, it is suggested that SHA-256 be
    -- used only with curve P-256, SHA-384 be used only with curve P-384,
    -- and SHA-512 be used with curve P-521.  This is aligned with the
    -- recommendation in [Section 4 of RFC5480](https://datatracker.ietf.org/doc/html/rfc5480#section-4)
    --
    -- Security considerations are [here](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.1.1)
    CoseSignAlgECDSA CoseHashAlgECDSA
  | -- | [(spec)](https://www.rfc-editor.org/rfc/rfc8812.html#section-2)
    -- The RSASSA-PKCS1-v1_5 signature algorithm is defined in
    -- [RFC8017](https://www.rfc-editor.org/rfc/rfc8812.html#RFC8017).
    -- The RSASSA-PKCS1-v1_5 signature algorithm is parameterized with a hash function (h).
    --
    -- A key of size 2048 bits or larger MUST be used with these algorithms.
    --
    -- Security considerations are [here](https://www.rfc-editor.org/rfc/rfc8812.html#section-5)
    CoseSignAlgRSA CoseHashAlgRSA
  deriving (CoseSignAlg -> CoseSignAlg -> Bool
(CoseSignAlg -> CoseSignAlg -> Bool)
-> (CoseSignAlg -> CoseSignAlg -> Bool) -> Eq CoseSignAlg
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CoseSignAlg -> CoseSignAlg -> Bool
== :: CoseSignAlg -> CoseSignAlg -> Bool
$c/= :: CoseSignAlg -> CoseSignAlg -> Bool
/= :: CoseSignAlg -> CoseSignAlg -> Bool
Eq, Int -> CoseSignAlg -> ShowS
[CoseSignAlg] -> ShowS
CoseSignAlg -> String
(Int -> CoseSignAlg -> ShowS)
-> (CoseSignAlg -> String)
-> ([CoseSignAlg] -> ShowS)
-> Show CoseSignAlg
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CoseSignAlg -> ShowS
showsPrec :: Int -> CoseSignAlg -> ShowS
$cshow :: CoseSignAlg -> String
show :: CoseSignAlg -> String
$cshowList :: [CoseSignAlg] -> ShowS
showList :: [CoseSignAlg] -> ShowS
Show, Eq CoseSignAlg
Eq CoseSignAlg
-> (CoseSignAlg -> CoseSignAlg -> Ordering)
-> (CoseSignAlg -> CoseSignAlg -> Bool)
-> (CoseSignAlg -> CoseSignAlg -> Bool)
-> (CoseSignAlg -> CoseSignAlg -> Bool)
-> (CoseSignAlg -> CoseSignAlg -> Bool)
-> (CoseSignAlg -> CoseSignAlg -> CoseSignAlg)
-> (CoseSignAlg -> CoseSignAlg -> CoseSignAlg)
-> Ord CoseSignAlg
CoseSignAlg -> CoseSignAlg -> Bool
CoseSignAlg -> CoseSignAlg -> Ordering
CoseSignAlg -> CoseSignAlg -> CoseSignAlg
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CoseSignAlg -> CoseSignAlg -> Ordering
compare :: CoseSignAlg -> CoseSignAlg -> Ordering
$c< :: CoseSignAlg -> CoseSignAlg -> Bool
< :: CoseSignAlg -> CoseSignAlg -> Bool
$c<= :: CoseSignAlg -> CoseSignAlg -> Bool
<= :: CoseSignAlg -> CoseSignAlg -> Bool
$c> :: CoseSignAlg -> CoseSignAlg -> Bool
> :: CoseSignAlg -> CoseSignAlg -> Bool
$c>= :: CoseSignAlg -> CoseSignAlg -> Bool
>= :: CoseSignAlg -> CoseSignAlg -> Bool
$cmax :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
max :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
$cmin :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
min :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
Ord, (forall x. CoseSignAlg -> Rep CoseSignAlg x)
-> (forall x. Rep CoseSignAlg x -> CoseSignAlg)
-> Generic CoseSignAlg
forall x. Rep CoseSignAlg x -> CoseSignAlg
forall x. CoseSignAlg -> Rep CoseSignAlg x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CoseSignAlg -> Rep CoseSignAlg x
from :: forall x. CoseSignAlg -> Rep CoseSignAlg x
$cto :: forall x. Rep CoseSignAlg x -> CoseSignAlg
to :: forall x. Rep CoseSignAlg x -> CoseSignAlg
Generic)

-- | An arbitrary and potentially unstable JSON encoding, only intended for
-- logging purposes. To actually encode and decode structures, use the
-- "Crypto.WebAuthn.Encoding" modules
deriving instance ToJSON CoseSignAlg

-- | Hash algorithms that can be used with the ECDSA signature algorithm
data CoseHashAlgECDSA
  = -- | SHA-256
    CoseHashAlgECDSASHA256
  | -- | SHA-384
    CoseHashAlgECDSASHA384
  | -- | SHA-512
    CoseHashAlgECDSASHA512
  deriving (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
(CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool)
-> Eq CoseHashAlgECDSA
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
== :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$c/= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
/= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
Eq, Int -> CoseHashAlgECDSA -> ShowS
[CoseHashAlgECDSA] -> ShowS
CoseHashAlgECDSA -> String
(Int -> CoseHashAlgECDSA -> ShowS)
-> (CoseHashAlgECDSA -> String)
-> ([CoseHashAlgECDSA] -> ShowS)
-> Show CoseHashAlgECDSA
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CoseHashAlgECDSA -> ShowS
showsPrec :: Int -> CoseHashAlgECDSA -> ShowS
$cshow :: CoseHashAlgECDSA -> String
show :: CoseHashAlgECDSA -> String
$cshowList :: [CoseHashAlgECDSA] -> ShowS
showList :: [CoseHashAlgECDSA] -> ShowS
Show, Eq CoseHashAlgECDSA
Eq CoseHashAlgECDSA
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Ordering)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA)
-> Ord CoseHashAlgECDSA
CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
CoseHashAlgECDSA -> CoseHashAlgECDSA -> Ordering
CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Ordering
compare :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Ordering
$c< :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
< :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$c<= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
<= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$c> :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
> :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$c>= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
>= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$cmax :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
max :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
$cmin :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
min :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
Ord, Int -> CoseHashAlgECDSA
CoseHashAlgECDSA -> Int
CoseHashAlgECDSA -> [CoseHashAlgECDSA]
CoseHashAlgECDSA -> CoseHashAlgECDSA
CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
CoseHashAlgECDSA
-> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
(CoseHashAlgECDSA -> CoseHashAlgECDSA)
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA)
-> (Int -> CoseHashAlgECDSA)
-> (CoseHashAlgECDSA -> Int)
-> (CoseHashAlgECDSA -> [CoseHashAlgECDSA])
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA])
-> (CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA])
-> (CoseHashAlgECDSA
    -> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA])
-> Enum CoseHashAlgECDSA
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: CoseHashAlgECDSA -> CoseHashAlgECDSA
succ :: CoseHashAlgECDSA -> CoseHashAlgECDSA
$cpred :: CoseHashAlgECDSA -> CoseHashAlgECDSA
pred :: CoseHashAlgECDSA -> CoseHashAlgECDSA
$ctoEnum :: Int -> CoseHashAlgECDSA
toEnum :: Int -> CoseHashAlgECDSA
$cfromEnum :: CoseHashAlgECDSA -> Int
fromEnum :: CoseHashAlgECDSA -> Int
$cenumFrom :: CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFrom :: CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFromThen :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFromThen :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFromTo :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFromTo :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFromThenTo :: CoseHashAlgECDSA
-> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFromThenTo :: CoseHashAlgECDSA
-> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
Enum, CoseHashAlgECDSA
CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bounded CoseHashAlgECDSA
forall a. a -> a -> Bounded a
$cminBound :: CoseHashAlgECDSA
minBound :: CoseHashAlgECDSA
$cmaxBound :: CoseHashAlgECDSA
maxBound :: CoseHashAlgECDSA
Bounded, (forall x. CoseHashAlgECDSA -> Rep CoseHashAlgECDSA x)
-> (forall x. Rep CoseHashAlgECDSA x -> CoseHashAlgECDSA)
-> Generic CoseHashAlgECDSA
forall x. Rep CoseHashAlgECDSA x -> CoseHashAlgECDSA
forall x. CoseHashAlgECDSA -> Rep CoseHashAlgECDSA x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CoseHashAlgECDSA -> Rep CoseHashAlgECDSA x
from :: forall x. CoseHashAlgECDSA -> Rep CoseHashAlgECDSA x
$cto :: forall x. Rep CoseHashAlgECDSA x -> CoseHashAlgECDSA
to :: forall x. Rep CoseHashAlgECDSA x -> CoseHashAlgECDSA
Generic)

-- | An arbitrary and potentially unstable JSON encoding, only intended for
-- logging purposes. To actually encode and decode structures, use the
-- "Crypto.WebAuthn.Encoding" modules
deriving instance ToJSON CoseHashAlgECDSA

-- | Hash algorithms that can be used with the RSA signature algorithm
data CoseHashAlgRSA
  = -- | SHA-1 (deprecated)
    CoseHashAlgRSASHA1
  | -- | SHA-256
    CoseHashAlgRSASHA256
  | -- | SHA-384
    CoseHashAlgRSASHA384
  | -- | SHA-512
    CoseHashAlgRSASHA512
  deriving (CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
(CoseHashAlgRSA -> CoseHashAlgRSA -> Bool)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> Bool) -> Eq CoseHashAlgRSA
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
== :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$c/= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
/= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
Eq, Int -> CoseHashAlgRSA -> ShowS
[CoseHashAlgRSA] -> ShowS
CoseHashAlgRSA -> String
(Int -> CoseHashAlgRSA -> ShowS)
-> (CoseHashAlgRSA -> String)
-> ([CoseHashAlgRSA] -> ShowS)
-> Show CoseHashAlgRSA
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CoseHashAlgRSA -> ShowS
showsPrec :: Int -> CoseHashAlgRSA -> ShowS
$cshow :: CoseHashAlgRSA -> String
show :: CoseHashAlgRSA -> String
$cshowList :: [CoseHashAlgRSA] -> ShowS
showList :: [CoseHashAlgRSA] -> ShowS
Show, Eq CoseHashAlgRSA
Eq CoseHashAlgRSA
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> Ordering)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> Bool)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> Bool)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> Bool)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> Bool)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA)
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA)
-> Ord CoseHashAlgRSA
CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
CoseHashAlgRSA -> CoseHashAlgRSA -> Ordering
CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CoseHashAlgRSA -> CoseHashAlgRSA -> Ordering
compare :: CoseHashAlgRSA -> CoseHashAlgRSA -> Ordering
$c< :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
< :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$c<= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
<= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$c> :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
> :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$c>= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
>= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$cmax :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
max :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
$cmin :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
min :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
Ord, Int -> CoseHashAlgRSA
CoseHashAlgRSA -> Int
CoseHashAlgRSA -> [CoseHashAlgRSA]
CoseHashAlgRSA -> CoseHashAlgRSA
CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
CoseHashAlgRSA
-> CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
(CoseHashAlgRSA -> CoseHashAlgRSA)
-> (CoseHashAlgRSA -> CoseHashAlgRSA)
-> (Int -> CoseHashAlgRSA)
-> (CoseHashAlgRSA -> Int)
-> (CoseHashAlgRSA -> [CoseHashAlgRSA])
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA])
-> (CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA])
-> (CoseHashAlgRSA
    -> CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA])
-> Enum CoseHashAlgRSA
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: CoseHashAlgRSA -> CoseHashAlgRSA
succ :: CoseHashAlgRSA -> CoseHashAlgRSA
$cpred :: CoseHashAlgRSA -> CoseHashAlgRSA
pred :: CoseHashAlgRSA -> CoseHashAlgRSA
$ctoEnum :: Int -> CoseHashAlgRSA
toEnum :: Int -> CoseHashAlgRSA
$cfromEnum :: CoseHashAlgRSA -> Int
fromEnum :: CoseHashAlgRSA -> Int
$cenumFrom :: CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFrom :: CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFromThen :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFromThen :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFromTo :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFromTo :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFromThenTo :: CoseHashAlgRSA
-> CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFromThenTo :: CoseHashAlgRSA
-> CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
Enum, CoseHashAlgRSA
CoseHashAlgRSA -> CoseHashAlgRSA -> Bounded CoseHashAlgRSA
forall a. a -> a -> Bounded a
$cminBound :: CoseHashAlgRSA
minBound :: CoseHashAlgRSA
$cmaxBound :: CoseHashAlgRSA
maxBound :: CoseHashAlgRSA
Bounded, (forall x. CoseHashAlgRSA -> Rep CoseHashAlgRSA x)
-> (forall x. Rep CoseHashAlgRSA x -> CoseHashAlgRSA)
-> Generic CoseHashAlgRSA
forall x. Rep CoseHashAlgRSA x -> CoseHashAlgRSA
forall x. CoseHashAlgRSA -> Rep CoseHashAlgRSA x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CoseHashAlgRSA -> Rep CoseHashAlgRSA x
from :: forall x. CoseHashAlgRSA -> Rep CoseHashAlgRSA x
$cto :: forall x. Rep CoseHashAlgRSA x -> CoseHashAlgRSA
to :: forall x. Rep CoseHashAlgRSA x -> CoseHashAlgRSA
Generic)

-- | An arbitrary and potentially unstable JSON encoding, only intended for
-- logging purposes. To actually encode and decode structures, use the
-- "Crypto.WebAuthn.Encoding" modules
deriving instance ToJSON CoseHashAlgRSA

-- | [(spec)](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.2)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @EdDSA@. Alias for 'CoseSignAlgEdDSA'
--
-- * Name: EdDSA
-- * Description: EdDSA
-- * Recommended: Yes
pattern CoseAlgorithmEdDSA :: CoseSignAlg
pattern $mCoseAlgorithmEdDSA :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmEdDSA :: CoseSignAlg
CoseAlgorithmEdDSA = CoseSignAlgEdDSA

-- | [(spec)](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.1)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @ES256@. Alias for @'CoseSignAlgECDSA' 'CoseHashAlgECDSASHA256'@
--
-- * Name: ES256
-- * Description: ECDSA w/ SHA-256
-- * Recommended: Yes
pattern CoseAlgorithmES256 :: CoseSignAlg
pattern $mCoseAlgorithmES256 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmES256 :: CoseSignAlg
CoseAlgorithmES256 = CoseSignAlgECDSA CoseHashAlgECDSASHA256

-- | [(spec)](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.1)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @ES384@. Alias for @'CoseSignAlgECDSA' 'CoseHashAlgECDSASHA384'@
--
-- * Name: ES384
-- * Description: ECDSA w/ SHA-384
-- * Recommended: Yes
pattern CoseAlgorithmES384 :: CoseSignAlg
pattern $mCoseAlgorithmES384 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmES384 :: CoseSignAlg
CoseAlgorithmES384 = CoseSignAlgECDSA CoseHashAlgECDSASHA384

-- | [(spec)](https://datatracker.ietf.org/doc/html/draft-ietf-cose-rfc8152bis-algs-12#section-2.1)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @ES512@. Alias for @'CoseSignAlgECDSA' 'CoseHashAlgECDSASHA512'@
--
-- * Name: ES512
-- * Description: ECDSA w/ SHA-512
-- * Recommended: Yes
pattern CoseAlgorithmES512 :: CoseSignAlg
pattern $mCoseAlgorithmES512 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmES512 :: CoseSignAlg
CoseAlgorithmES512 = CoseSignAlgECDSA CoseHashAlgECDSASHA512

-- | [(spec)](https://www.rfc-editor.org/rfc/rfc8812.html#section-2)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @RS256@. Alias for @'CoseSignAlgRSA' 'CoseHashAlgRSASHA256'@
--
-- * Name: RS256
-- * Description: RSASSA-PKCS1-v1_5 using SHA-256
-- * Recommended: No
pattern CoseAlgorithmRS256 :: CoseSignAlg
pattern $mCoseAlgorithmRS256 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmRS256 :: CoseSignAlg
CoseAlgorithmRS256 = CoseSignAlgRSA CoseHashAlgRSASHA256

-- | [(spec)](https://www.rfc-editor.org/rfc/rfc8812.html#section-2)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @RS384@. Alias for @'CoseSignAlgRSA' 'CoseHashAlgRSASHA384'@
--
-- * Name: RS384
-- * Description: RSASSA-PKCS1-v1_5 using SHA-384
-- * Recommended: No
pattern CoseAlgorithmRS384 :: CoseSignAlg
pattern $mCoseAlgorithmRS384 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmRS384 :: CoseSignAlg
CoseAlgorithmRS384 = CoseSignAlgRSA CoseHashAlgRSASHA384

-- | [(spec)](https://www.rfc-editor.org/rfc/rfc8812.html#section-2)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @RS512@. Alias for @'CoseSignAlgRSA' 'CoseHashAlgRSASHA512'@
--
-- * Name: RS512
-- * Description: RSASSA-PKCS1-v1_5 using SHA-512
-- * Recommended: No
pattern CoseAlgorithmRS512 :: CoseSignAlg
pattern $mCoseAlgorithmRS512 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmRS512 :: CoseSignAlg
CoseAlgorithmRS512 = CoseSignAlgRSA CoseHashAlgRSASHA512

-- | [(spec)](https://www.rfc-editor.org/rfc/rfc8812.html#section-2)
-- [Cose Algorithm registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms)
-- entry @RS1@. Alias for @'CoseSignAlgRSA' 'CoseHashAlgRSASHA1'@
--
-- * Name: RS1
-- * Description: RSASSA-PKCS1-v1_5 using SHA-1
-- * Recommended: Deprecated
pattern CoseAlgorithmRS1 :: CoseSignAlg
pattern $mCoseAlgorithmRS1 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
$bCoseAlgorithmRS1 :: CoseSignAlg
CoseAlgorithmRS1 = CoseSignAlgRSA CoseHashAlgRSASHA1

-- | Serialises COSE Algorithms using the @Value@ column from the
-- [COSE Algorithms registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
-- This uses the 'fromCoseSignAlg' and 'toCoseSignAlg' functions to do the
-- encoding and decoding respectively.
instance Serialise CoseSignAlg where
  encode :: CoseSignAlg -> Encoding
encode = Int -> Encoding
encodeInt (Int -> Encoding)
-> (CoseSignAlg -> Int) -> CoseSignAlg -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoseSignAlg -> Int
forall p. Num p => CoseSignAlg -> p
fromCoseSignAlg
  decode :: forall s. Decoder s CoseSignAlg
decode = do
    Int
int <- Decoder s Int
forall s. Decoder s Int
decodeIntCanonical
    case Int -> Either Text CoseSignAlg
forall a. (Eq a, Num a, Show a) => a -> Either Text CoseSignAlg
toCoseSignAlg Int
int of
      Right CoseSignAlg
res -> CoseSignAlg -> Decoder s CoseSignAlg
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CoseSignAlg
res
      Left Text
err -> String -> Decoder s CoseSignAlg
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s CoseSignAlg)
-> String -> Decoder s CoseSignAlg
forall a b. (a -> b) -> a -> b
$ Text -> String
Text.unpack Text
err

-- | Converts a 'CoseSignAlg' to the corresponding integer value from the
-- [COSE Algorithms registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
-- The inverse operation is 'toCoseSignAlg'
fromCoseSignAlg :: (Num p) => CoseSignAlg -> p
fromCoseSignAlg :: forall p. Num p => CoseSignAlg -> p
fromCoseSignAlg (CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA1) = -p
65535
fromCoseSignAlg (CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA512) = -p
259
fromCoseSignAlg (CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA384) = -p
258
fromCoseSignAlg (CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA256) = -p
257
fromCoseSignAlg (CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA512) = -p
36
fromCoseSignAlg (CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA384) = -p
35
fromCoseSignAlg CoseSignAlg
CoseSignAlgEdDSA = -p
8
fromCoseSignAlg (CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA256) = -p
7

-- | Converts an integer value to the corresponding 'CoseSignAlg' from the
-- [COSE Algorithms registry](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
-- Returns an error if the integer doesn't represent a known algorithm.
-- The inverse operation is 'fromCoseSignAlg'
toCoseSignAlg :: (Eq a, Num a, Show a) => a -> Either Text CoseSignAlg
toCoseSignAlg :: forall a. (Eq a, Num a, Show a) => a -> Either Text CoseSignAlg
toCoseSignAlg (-65535) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA1)
toCoseSignAlg (-259) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA512)
toCoseSignAlg (-258) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA384)
toCoseSignAlg (-257) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA256)
toCoseSignAlg (-36) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgECDSA -> CoseSignAlg
CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA512)
toCoseSignAlg (-35) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgECDSA -> CoseSignAlg
CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA384)
toCoseSignAlg (-8) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CoseSignAlg
CoseSignAlgEdDSA
toCoseSignAlg (-7) = CoseSignAlg -> Either Text CoseSignAlg
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgECDSA -> CoseSignAlg
CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA256)
toCoseSignAlg a
value = Text -> Either Text CoseSignAlg
forall a b. a -> Either a b
Left (Text -> Either Text CoseSignAlg)
-> Text -> Either Text CoseSignAlg
forall a b. (a -> b) -> a -> b
$ Text
"Unknown COSE algorithm value " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (a -> String
forall a. Show a => a -> String
show a
value)