padding.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import annotations
  5. import abc
  6. from cryptography.hazmat.primitives import hashes
  7. from cryptography.hazmat.primitives._asymmetric import (
  8. AsymmetricPadding as AsymmetricPadding,
  9. )
  10. from cryptography.hazmat.primitives.asymmetric import rsa
  11. class PKCS1v15(AsymmetricPadding):
  12. name = "EMSA-PKCS1-v1_5"
  13. class _MaxLength:
  14. "Sentinel value for `MAX_LENGTH`."
  15. class _Auto:
  16. "Sentinel value for `AUTO`."
  17. class _DigestLength:
  18. "Sentinel value for `DIGEST_LENGTH`."
  19. class PSS(AsymmetricPadding):
  20. MAX_LENGTH = _MaxLength()
  21. AUTO = _Auto()
  22. DIGEST_LENGTH = _DigestLength()
  23. name = "EMSA-PSS"
  24. _salt_length: int | _MaxLength | _Auto | _DigestLength
  25. def __init__(
  26. self,
  27. mgf: MGF,
  28. salt_length: int | _MaxLength | _Auto | _DigestLength,
  29. ) -> None:
  30. self._mgf = mgf
  31. if not isinstance(
  32. salt_length, (int, _MaxLength, _Auto, _DigestLength)
  33. ):
  34. raise TypeError(
  35. "salt_length must be an integer, MAX_LENGTH, "
  36. "DIGEST_LENGTH, or AUTO"
  37. )
  38. if isinstance(salt_length, int) and salt_length < 0:
  39. raise ValueError("salt_length must be zero or greater.")
  40. self._salt_length = salt_length
  41. @property
  42. def mgf(self) -> MGF:
  43. return self._mgf
  44. class OAEP(AsymmetricPadding):
  45. name = "EME-OAEP"
  46. def __init__(
  47. self,
  48. mgf: MGF,
  49. algorithm: hashes.HashAlgorithm,
  50. label: bytes | None,
  51. ):
  52. if not isinstance(algorithm, hashes.HashAlgorithm):
  53. raise TypeError("Expected instance of hashes.HashAlgorithm.")
  54. self._mgf = mgf
  55. self._algorithm = algorithm
  56. self._label = label
  57. @property
  58. def algorithm(self) -> hashes.HashAlgorithm:
  59. return self._algorithm
  60. @property
  61. def mgf(self) -> MGF:
  62. return self._mgf
  63. class MGF(metaclass=abc.ABCMeta):
  64. _algorithm: hashes.HashAlgorithm
  65. class MGF1(MGF):
  66. MAX_LENGTH = _MaxLength()
  67. def __init__(self, algorithm: hashes.HashAlgorithm):
  68. if not isinstance(algorithm, hashes.HashAlgorithm):
  69. raise TypeError("Expected instance of hashes.HashAlgorithm.")
  70. self._algorithm = algorithm
  71. def calculate_max_pss_salt_length(
  72. key: rsa.RSAPrivateKey | rsa.RSAPublicKey,
  73. hash_algorithm: hashes.HashAlgorithm,
  74. ) -> int:
  75. if not isinstance(key, (rsa.RSAPrivateKey, rsa.RSAPublicKey)):
  76. raise TypeError("key must be an RSA public or private key")
  77. # bit length - 1 per RFC 3447
  78. emlen = (key.key_size + 6) // 8
  79. salt_length = emlen - hash_algorithm.digest_size - 2
  80. assert salt_length >= 0
  81. return salt_length