interfaces.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. import abc
  5. class CipherBackend(metaclass=abc.ABCMeta):
  6. @abc.abstractmethod
  7. def cipher_supported(self, cipher, mode):
  8. """
  9. Return True if the given cipher and mode are supported.
  10. """
  11. @abc.abstractmethod
  12. def create_symmetric_encryption_ctx(self, cipher, mode):
  13. """
  14. Get a CipherContext that can be used for encryption.
  15. """
  16. @abc.abstractmethod
  17. def create_symmetric_decryption_ctx(self, cipher, mode):
  18. """
  19. Get a CipherContext that can be used for decryption.
  20. """
  21. class HashBackend(metaclass=abc.ABCMeta):
  22. @abc.abstractmethod
  23. def hash_supported(self, algorithm):
  24. """
  25. Return True if the hash algorithm is supported by this backend.
  26. """
  27. @abc.abstractmethod
  28. def create_hash_ctx(self, algorithm):
  29. """
  30. Create a HashContext for calculating a message digest.
  31. """
  32. class HMACBackend(metaclass=abc.ABCMeta):
  33. @abc.abstractmethod
  34. def hmac_supported(self, algorithm):
  35. """
  36. Return True if the hash algorithm is supported for HMAC by this
  37. backend.
  38. """
  39. @abc.abstractmethod
  40. def create_hmac_ctx(self, key, algorithm):
  41. """
  42. Create a context for calculating a message authentication code.
  43. """
  44. class CMACBackend(metaclass=abc.ABCMeta):
  45. @abc.abstractmethod
  46. def cmac_algorithm_supported(self, algorithm):
  47. """
  48. Returns True if the block cipher is supported for CMAC by this backend
  49. """
  50. @abc.abstractmethod
  51. def create_cmac_ctx(self, algorithm):
  52. """
  53. Create a context for calculating a message authentication code.
  54. """
  55. class PBKDF2HMACBackend(metaclass=abc.ABCMeta):
  56. @abc.abstractmethod
  57. def pbkdf2_hmac_supported(self, algorithm):
  58. """
  59. Return True if the hash algorithm is supported for PBKDF2 by this
  60. backend.
  61. """
  62. @abc.abstractmethod
  63. def derive_pbkdf2_hmac(
  64. self, algorithm, length, salt, iterations, key_material
  65. ):
  66. """
  67. Return length bytes derived from provided PBKDF2 parameters.
  68. """
  69. class RSABackend(metaclass=abc.ABCMeta):
  70. @abc.abstractmethod
  71. def generate_rsa_private_key(self, public_exponent, key_size):
  72. """
  73. Generate an RSAPrivateKey instance with public_exponent and a modulus
  74. of key_size bits.
  75. """
  76. @abc.abstractmethod
  77. def rsa_padding_supported(self, padding):
  78. """
  79. Returns True if the backend supports the given padding options.
  80. """
  81. @abc.abstractmethod
  82. def generate_rsa_parameters_supported(self, public_exponent, key_size):
  83. """
  84. Returns True if the backend supports the given parameters for key
  85. generation.
  86. """
  87. @abc.abstractmethod
  88. def load_rsa_private_numbers(self, numbers):
  89. """
  90. Returns an RSAPrivateKey provider.
  91. """
  92. @abc.abstractmethod
  93. def load_rsa_public_numbers(self, numbers):
  94. """
  95. Returns an RSAPublicKey provider.
  96. """
  97. class DSABackend(metaclass=abc.ABCMeta):
  98. @abc.abstractmethod
  99. def generate_dsa_parameters(self, key_size):
  100. """
  101. Generate a DSAParameters instance with a modulus of key_size bits.
  102. """
  103. @abc.abstractmethod
  104. def generate_dsa_private_key(self, parameters):
  105. """
  106. Generate a DSAPrivateKey instance with parameters as a DSAParameters
  107. object.
  108. """
  109. @abc.abstractmethod
  110. def generate_dsa_private_key_and_parameters(self, key_size):
  111. """
  112. Generate a DSAPrivateKey instance using key size only.
  113. """
  114. @abc.abstractmethod
  115. def dsa_hash_supported(self, algorithm):
  116. """
  117. Return True if the hash algorithm is supported by the backend for DSA.
  118. """
  119. @abc.abstractmethod
  120. def dsa_parameters_supported(self, p, q, g):
  121. """
  122. Return True if the parameters are supported by the backend for DSA.
  123. """
  124. @abc.abstractmethod
  125. def load_dsa_private_numbers(self, numbers):
  126. """
  127. Returns a DSAPrivateKey provider.
  128. """
  129. @abc.abstractmethod
  130. def load_dsa_public_numbers(self, numbers):
  131. """
  132. Returns a DSAPublicKey provider.
  133. """
  134. @abc.abstractmethod
  135. def load_dsa_parameter_numbers(self, numbers):
  136. """
  137. Returns a DSAParameters provider.
  138. """
  139. class EllipticCurveBackend(metaclass=abc.ABCMeta):
  140. @abc.abstractmethod
  141. def elliptic_curve_signature_algorithm_supported(
  142. self, signature_algorithm, curve
  143. ):
  144. """
  145. Returns True if the backend supports the named elliptic curve with the
  146. specified signature algorithm.
  147. """
  148. @abc.abstractmethod
  149. def elliptic_curve_supported(self, curve):
  150. """
  151. Returns True if the backend supports the named elliptic curve.
  152. """
  153. @abc.abstractmethod
  154. def generate_elliptic_curve_private_key(self, curve):
  155. """
  156. Return an object conforming to the EllipticCurvePrivateKey interface.
  157. """
  158. @abc.abstractmethod
  159. def load_elliptic_curve_public_numbers(self, numbers):
  160. """
  161. Return an EllipticCurvePublicKey provider using the given numbers.
  162. """
  163. @abc.abstractmethod
  164. def load_elliptic_curve_private_numbers(self, numbers):
  165. """
  166. Return an EllipticCurvePrivateKey provider using the given numbers.
  167. """
  168. @abc.abstractmethod
  169. def elliptic_curve_exchange_algorithm_supported(self, algorithm, curve):
  170. """
  171. Returns whether the exchange algorithm is supported by this backend.
  172. """
  173. @abc.abstractmethod
  174. def derive_elliptic_curve_private_key(self, private_value, curve):
  175. """
  176. Compute the private key given the private value and curve.
  177. """
  178. class PEMSerializationBackend(metaclass=abc.ABCMeta):
  179. @abc.abstractmethod
  180. def load_pem_private_key(self, data, password):
  181. """
  182. Loads a private key from PEM encoded data, using the provided password
  183. if the data is encrypted.
  184. """
  185. @abc.abstractmethod
  186. def load_pem_public_key(self, data):
  187. """
  188. Loads a public key from PEM encoded data.
  189. """
  190. @abc.abstractmethod
  191. def load_pem_parameters(self, data):
  192. """
  193. Load encryption parameters from PEM encoded data.
  194. """
  195. class DERSerializationBackend(metaclass=abc.ABCMeta):
  196. @abc.abstractmethod
  197. def load_der_private_key(self, data, password):
  198. """
  199. Loads a private key from DER encoded data. Uses the provided password
  200. if the data is encrypted.
  201. """
  202. @abc.abstractmethod
  203. def load_der_public_key(self, data):
  204. """
  205. Loads a public key from DER encoded data.
  206. """
  207. @abc.abstractmethod
  208. def load_der_parameters(self, data):
  209. """
  210. Load encryption parameters from DER encoded data.
  211. """
  212. class X509Backend(metaclass=abc.ABCMeta):
  213. @abc.abstractmethod
  214. def load_pem_x509_certificate(self, data):
  215. """
  216. Load an X.509 certificate from PEM encoded data.
  217. """
  218. @abc.abstractmethod
  219. def load_der_x509_certificate(self, data):
  220. """
  221. Load an X.509 certificate from DER encoded data.
  222. """
  223. @abc.abstractmethod
  224. def load_der_x509_csr(self, data):
  225. """
  226. Load an X.509 CSR from DER encoded data.
  227. """
  228. @abc.abstractmethod
  229. def load_pem_x509_csr(self, data):
  230. """
  231. Load an X.509 CSR from PEM encoded data.
  232. """
  233. @abc.abstractmethod
  234. def create_x509_csr(self, builder, private_key, algorithm):
  235. """
  236. Create and sign an X.509 CSR from a CSR builder object.
  237. """
  238. @abc.abstractmethod
  239. def create_x509_certificate(self, builder, private_key, algorithm):
  240. """
  241. Create and sign an X.509 certificate from a CertificateBuilder object.
  242. """
  243. @abc.abstractmethod
  244. def create_x509_crl(self, builder, private_key, algorithm):
  245. """
  246. Create and sign an X.509 CertificateRevocationList from a
  247. CertificateRevocationListBuilder object.
  248. """
  249. @abc.abstractmethod
  250. def create_x509_revoked_certificate(self, builder):
  251. """
  252. Create a RevokedCertificate object from a RevokedCertificateBuilder
  253. object.
  254. """
  255. @abc.abstractmethod
  256. def x509_name_bytes(self, name):
  257. """
  258. Compute the DER encoded bytes of an X509 Name object.
  259. """
  260. class DHBackend(metaclass=abc.ABCMeta):
  261. @abc.abstractmethod
  262. def generate_dh_parameters(self, generator, key_size):
  263. """
  264. Generate a DHParameters instance with a modulus of key_size bits.
  265. Using the given generator. Often 2 or 5.
  266. """
  267. @abc.abstractmethod
  268. def generate_dh_private_key(self, parameters):
  269. """
  270. Generate a DHPrivateKey instance with parameters as a DHParameters
  271. object.
  272. """
  273. @abc.abstractmethod
  274. def generate_dh_private_key_and_parameters(self, generator, key_size):
  275. """
  276. Generate a DHPrivateKey instance using key size only.
  277. Using the given generator. Often 2 or 5.
  278. """
  279. @abc.abstractmethod
  280. def load_dh_private_numbers(self, numbers):
  281. """
  282. Load a DHPrivateKey from DHPrivateNumbers
  283. """
  284. @abc.abstractmethod
  285. def load_dh_public_numbers(self, numbers):
  286. """
  287. Load a DHPublicKey from DHPublicNumbers.
  288. """
  289. @abc.abstractmethod
  290. def load_dh_parameter_numbers(self, numbers):
  291. """
  292. Load DHParameters from DHParameterNumbers.
  293. """
  294. @abc.abstractmethod
  295. def dh_parameters_supported(self, p, g, q=None):
  296. """
  297. Returns whether the backend supports DH with these parameter values.
  298. """
  299. @abc.abstractmethod
  300. def dh_x942_serialization_supported(self):
  301. """
  302. Returns True if the backend supports the serialization of DH objects
  303. with subgroup order (q).
  304. """
  305. class ScryptBackend(metaclass=abc.ABCMeta):
  306. @abc.abstractmethod
  307. def derive_scrypt(self, key_material, salt, length, n, r, p):
  308. """
  309. Return bytes derived from provided Scrypt parameters.
  310. """