{-# LANGUAGE PatternSynonyms #-}

-- | 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CoseSignAlg -> CoseSignAlg -> Bool
$c/= :: CoseSignAlg -> CoseSignAlg -> Bool
== :: CoseSignAlg -> CoseSignAlg -> Bool
$c== :: CoseSignAlg -> CoseSignAlg -> Bool
Eq, Int -> CoseSignAlg -> ShowS
[CoseSignAlg] -> ShowS
CoseSignAlg -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CoseSignAlg] -> ShowS
$cshowList :: [CoseSignAlg] -> ShowS
show :: CoseSignAlg -> String
$cshow :: CoseSignAlg -> String
showsPrec :: Int -> CoseSignAlg -> ShowS
$cshowsPrec :: Int -> CoseSignAlg -> ShowS
Show, Eq 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
min :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
$cmin :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
max :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
$cmax :: CoseSignAlg -> CoseSignAlg -> CoseSignAlg
>= :: CoseSignAlg -> CoseSignAlg -> Bool
$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
compare :: CoseSignAlg -> CoseSignAlg -> Ordering
$ccompare :: CoseSignAlg -> CoseSignAlg -> Ordering
Ord, 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
$cto :: forall x. Rep CoseSignAlg x -> CoseSignAlg
$cfrom :: forall x. CoseSignAlg -> Rep CoseSignAlg x
Generic, [CoseSignAlg] -> Encoding
[CoseSignAlg] -> Value
CoseSignAlg -> Encoding
CoseSignAlg -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CoseSignAlg] -> Encoding
$ctoEncodingList :: [CoseSignAlg] -> Encoding
toJSONList :: [CoseSignAlg] -> Value
$ctoJSONList :: [CoseSignAlg] -> Value
toEncoding :: CoseSignAlg -> Encoding
$ctoEncoding :: CoseSignAlg -> Encoding
toJSON :: CoseSignAlg -> Value
$ctoJSON :: CoseSignAlg -> Value
ToJSON)

-- | 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$c/= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
== :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$c== :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
Eq, Int -> CoseHashAlgECDSA -> ShowS
[CoseHashAlgECDSA] -> ShowS
CoseHashAlgECDSA -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CoseHashAlgECDSA] -> ShowS
$cshowList :: [CoseHashAlgECDSA] -> ShowS
show :: CoseHashAlgECDSA -> String
$cshow :: CoseHashAlgECDSA -> String
showsPrec :: Int -> CoseHashAlgECDSA -> ShowS
$cshowsPrec :: Int -> CoseHashAlgECDSA -> ShowS
Show, Eq 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
min :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
$cmin :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
max :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
$cmax :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> CoseHashAlgECDSA
>= :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Bool
$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
compare :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Ordering
$ccompare :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> Ordering
Ord, Int -> CoseHashAlgECDSA
CoseHashAlgECDSA -> Int
CoseHashAlgECDSA -> [CoseHashAlgECDSA]
CoseHashAlgECDSA -> CoseHashAlgECDSA
CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
CoseHashAlgECDSA
-> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [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
enumFromThenTo :: CoseHashAlgECDSA
-> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFromThenTo :: CoseHashAlgECDSA
-> CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFromTo :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFromTo :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFromThen :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFromThen :: CoseHashAlgECDSA -> CoseHashAlgECDSA -> [CoseHashAlgECDSA]
enumFrom :: CoseHashAlgECDSA -> [CoseHashAlgECDSA]
$cenumFrom :: CoseHashAlgECDSA -> [CoseHashAlgECDSA]
fromEnum :: CoseHashAlgECDSA -> Int
$cfromEnum :: CoseHashAlgECDSA -> Int
toEnum :: Int -> CoseHashAlgECDSA
$ctoEnum :: Int -> CoseHashAlgECDSA
pred :: CoseHashAlgECDSA -> CoseHashAlgECDSA
$cpred :: CoseHashAlgECDSA -> CoseHashAlgECDSA
succ :: CoseHashAlgECDSA -> CoseHashAlgECDSA
$csucc :: CoseHashAlgECDSA -> CoseHashAlgECDSA
Enum, CoseHashAlgECDSA
forall a. a -> a -> Bounded a
maxBound :: CoseHashAlgECDSA
$cmaxBound :: CoseHashAlgECDSA
minBound :: CoseHashAlgECDSA
$cminBound :: CoseHashAlgECDSA
Bounded, 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
$cto :: forall x. Rep CoseHashAlgECDSA x -> CoseHashAlgECDSA
$cfrom :: forall x. CoseHashAlgECDSA -> Rep CoseHashAlgECDSA x
Generic, [CoseHashAlgECDSA] -> Encoding
[CoseHashAlgECDSA] -> Value
CoseHashAlgECDSA -> Encoding
CoseHashAlgECDSA -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CoseHashAlgECDSA] -> Encoding
$ctoEncodingList :: [CoseHashAlgECDSA] -> Encoding
toJSONList :: [CoseHashAlgECDSA] -> Value
$ctoJSONList :: [CoseHashAlgECDSA] -> Value
toEncoding :: CoseHashAlgECDSA -> Encoding
$ctoEncoding :: CoseHashAlgECDSA -> Encoding
toJSON :: CoseHashAlgECDSA -> Value
$ctoJSON :: CoseHashAlgECDSA -> Value
ToJSON)

-- | 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$c/= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
== :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$c== :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
Eq, Int -> CoseHashAlgRSA -> ShowS
[CoseHashAlgRSA] -> ShowS
CoseHashAlgRSA -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CoseHashAlgRSA] -> ShowS
$cshowList :: [CoseHashAlgRSA] -> ShowS
show :: CoseHashAlgRSA -> String
$cshow :: CoseHashAlgRSA -> String
showsPrec :: Int -> CoseHashAlgRSA -> ShowS
$cshowsPrec :: Int -> CoseHashAlgRSA -> ShowS
Show, Eq 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
min :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
$cmin :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
max :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
$cmax :: CoseHashAlgRSA -> CoseHashAlgRSA -> CoseHashAlgRSA
>= :: CoseHashAlgRSA -> CoseHashAlgRSA -> Bool
$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
compare :: CoseHashAlgRSA -> CoseHashAlgRSA -> Ordering
$ccompare :: CoseHashAlgRSA -> CoseHashAlgRSA -> Ordering
Ord, Int -> CoseHashAlgRSA
CoseHashAlgRSA -> Int
CoseHashAlgRSA -> [CoseHashAlgRSA]
CoseHashAlgRSA -> CoseHashAlgRSA
CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
CoseHashAlgRSA
-> CoseHashAlgRSA -> CoseHashAlgRSA -> [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
enumFromThenTo :: CoseHashAlgRSA
-> CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFromThenTo :: CoseHashAlgRSA
-> CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFromTo :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFromTo :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFromThen :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFromThen :: CoseHashAlgRSA -> CoseHashAlgRSA -> [CoseHashAlgRSA]
enumFrom :: CoseHashAlgRSA -> [CoseHashAlgRSA]
$cenumFrom :: CoseHashAlgRSA -> [CoseHashAlgRSA]
fromEnum :: CoseHashAlgRSA -> Int
$cfromEnum :: CoseHashAlgRSA -> Int
toEnum :: Int -> CoseHashAlgRSA
$ctoEnum :: Int -> CoseHashAlgRSA
pred :: CoseHashAlgRSA -> CoseHashAlgRSA
$cpred :: CoseHashAlgRSA -> CoseHashAlgRSA
succ :: CoseHashAlgRSA -> CoseHashAlgRSA
$csucc :: CoseHashAlgRSA -> CoseHashAlgRSA
Enum, CoseHashAlgRSA
forall a. a -> a -> Bounded a
maxBound :: CoseHashAlgRSA
$cmaxBound :: CoseHashAlgRSA
minBound :: CoseHashAlgRSA
$cminBound :: CoseHashAlgRSA
Bounded, 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
$cto :: forall x. Rep CoseHashAlgRSA x -> CoseHashAlgRSA
$cfrom :: forall x. CoseHashAlgRSA -> Rep CoseHashAlgRSA x
Generic, [CoseHashAlgRSA] -> Encoding
[CoseHashAlgRSA] -> Value
CoseHashAlgRSA -> Encoding
CoseHashAlgRSA -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CoseHashAlgRSA] -> Encoding
$ctoEncodingList :: [CoseHashAlgRSA] -> Encoding
toJSONList :: [CoseHashAlgRSA] -> Value
$ctoJSONList :: [CoseHashAlgRSA] -> Value
toEncoding :: CoseHashAlgRSA -> Encoding
$ctoEncoding :: CoseHashAlgRSA -> Encoding
toJSON :: CoseHashAlgRSA -> Value
$ctoJSON :: CoseHashAlgRSA -> Value
ToJSON)

-- | [(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 $bCoseAlgorithmEdDSA :: CoseSignAlg
$mCoseAlgorithmEdDSA :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmES256 :: CoseSignAlg
$mCoseAlgorithmES256 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmES384 :: CoseSignAlg
$mCoseAlgorithmES384 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmES512 :: CoseSignAlg
$mCoseAlgorithmES512 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmRS256 :: CoseSignAlg
$mCoseAlgorithmRS256 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmRS384 :: CoseSignAlg
$mCoseAlgorithmRS384 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmRS512 :: CoseSignAlg
$mCoseAlgorithmRS512 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 $bCoseAlgorithmRS1 :: CoseSignAlg
$mCoseAlgorithmRS1 :: forall {r}. CoseSignAlg -> ((# #) -> r) -> ((# #) -> r) -> r
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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p. Num p => CoseSignAlg -> p
fromCoseSignAlg
  decode :: forall s. Decoder s CoseSignAlg
decode = do
    Int
int <- forall s. Decoder s Int
decodeIntCanonical
    case forall a. (Eq a, Num a, Show a) => a -> Either Text CoseSignAlg
toCoseSignAlg Int
int of
      Right CoseSignAlg
res -> forall (f :: * -> *) a. Applicative f => a -> f a
pure CoseSignAlg
res
      Left Text
err -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail 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) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA1)
toCoseSignAlg (-259) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA512)
toCoseSignAlg (-258) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA384)
toCoseSignAlg (-257) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgRSA -> CoseSignAlg
CoseSignAlgRSA CoseHashAlgRSA
CoseHashAlgRSASHA256)
toCoseSignAlg (-36) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgECDSA -> CoseSignAlg
CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA512)
toCoseSignAlg (-35) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgECDSA -> CoseSignAlg
CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA384)
toCoseSignAlg (-8) = forall (f :: * -> *) a. Applicative f => a -> f a
pure CoseSignAlg
CoseSignAlgEdDSA
toCoseSignAlg (-7) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (CoseHashAlgECDSA -> CoseSignAlg
CoseSignAlgECDSA CoseHashAlgECDSA
CoseHashAlgECDSASHA256)
toCoseSignAlg a
value = forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Unknown COSE algorithm value " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (forall a. Show a => a -> String
show a
value)