| 1 |
from cryptography.hazmat.primitives.asymmetric.ec import ( |
| 2 |
ECDSA, |
| 3 |
SECP256R1, |
| 4 |
SECP384R1, |
| 5 |
SECP521R1, |
| 6 |
EllipticCurve, |
| 7 |
EllipticCurveSignatureAlgorithm, |
| 8 |
) |
| 9 |
from cryptography.hazmat.primitives.hashes import ( |
| 10 |
SHA1, |
| 11 |
SHA256, |
| 12 |
SHA384, |
| 13 |
SHA512, |
| 14 |
HashAlgorithm, |
| 15 |
) |
| 16 |
|
| 17 |
from .cose import COSECRV, COSEAlgorithmIdentifier |
| 18 |
from .exceptions import UnsupportedAlgorithm, UnsupportedEC2Curve |
| 19 |
|
| 20 |
|
| 21 |
def is_rsa_pkcs(alg_id: COSEAlgorithmIdentifier) -> bool: |
| 22 |
"""Determine if the specified COSE algorithm ID denotes an RSA PKCSv1 public key""" |
| 23 |
return alg_id in ( |
| 24 |
COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_1, |
| 25 |
COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256, |
| 26 |
COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_384, |
| 27 |
COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_512, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def is_rsa_pss(alg_id: COSEAlgorithmIdentifier) -> bool: |
| 32 |
"""Determine if the specified COSE algorithm ID denotes an RSA PSS public key""" |
| 33 |
return alg_id in ( |
| 34 |
COSEAlgorithmIdentifier.RSASSA_PSS_SHA_256, |
| 35 |
COSEAlgorithmIdentifier.RSASSA_PSS_SHA_384, |
| 36 |
COSEAlgorithmIdentifier.RSASSA_PSS_SHA_512, |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def get_ec2_sig_alg(alg_id: COSEAlgorithmIdentifier) -> EllipticCurveSignatureAlgorithm: |
| 41 |
"""Turn an "ECDSA" COSE algorithm identifier into a corresponding signature |
| 42 |
algorithm |
| 43 |
""" |
| 44 |
if alg_id == COSEAlgorithmIdentifier.ECDSA_SHA_256: |
| 45 |
return ECDSA(SHA256()) |
| 46 |
if alg_id == COSEAlgorithmIdentifier.ECDSA_SHA_512: |
| 47 |
return ECDSA(SHA512()) |
| 48 |
|
| 49 |
raise UnsupportedAlgorithm(f"Unrecognized EC2 signature alg {alg_id}") |
| 50 |
|
| 51 |
|
| 52 |
def get_ec2_curve(crv_id: COSECRV) -> EllipticCurve: |
| 53 |
"""Turn an EC2 COSE crv identifier into a corresponding curve""" |
| 54 |
if crv_id == COSECRV.P256: |
| 55 |
return SECP256R1() |
| 56 |
elif crv_id == COSECRV.P384: |
| 57 |
return SECP384R1() |
| 58 |
elif crv_id == COSECRV.P521: |
| 59 |
return SECP521R1() |
| 60 |
|
| 61 |
raise UnsupportedEC2Curve(f"Unrecognized EC2 curve {crv_id}") |
| 62 |
|
| 63 |
|
| 64 |
def get_rsa_pkcs1_sig_alg(alg_id: COSEAlgorithmIdentifier) -> HashAlgorithm: |
| 65 |
"""Turn an "RSASSA_PKCS1" COSE algorithm identifier into a corresponding signature |
| 66 |
algorithm |
| 67 |
""" |
| 68 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_1: |
| 69 |
return SHA1() |
| 70 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256: |
| 71 |
return SHA256() |
| 72 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_384: |
| 73 |
return SHA384() |
| 74 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_512: |
| 75 |
return SHA512() |
| 76 |
|
| 77 |
raise UnsupportedAlgorithm(f"Unrecognized RSA PKCS1 signature alg {alg_id}") |
| 78 |
|
| 79 |
|
| 80 |
def get_rsa_pss_sig_alg(alg_id: COSEAlgorithmIdentifier) -> HashAlgorithm: |
| 81 |
"""Turn an "RSASSA_PSS" COSE algorithm identifier into a corresponding signature |
| 82 |
algorithm |
| 83 |
""" |
| 84 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PSS_SHA_256: |
| 85 |
return SHA256() |
| 86 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PSS_SHA_384: |
| 87 |
return SHA384() |
| 88 |
if alg_id == COSEAlgorithmIdentifier.RSASSA_PSS_SHA_512: |
| 89 |
return SHA512() |
| 90 |
|
| 91 |
raise UnsupportedAlgorithm(f"Unrecognized RSA PSS signature alg {alg_id}") |