blob: 94152370b13f33dae095b1b23deb18eecf6d911d [file] [log] [blame]
Alex Gaynorfcaf9762014-01-30 11:23:57 -08001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14from __future__ import absolute_import, division, print_function
15
16from cryptography import utils
17from cryptography.exceptions import UnsupportedAlgorithm
Alex Gaynor2d028572014-01-30 11:36:10 -080018from cryptography.hazmat.backends.interfaces import (
Alex Gaynor020ae0f2014-01-30 11:38:43 -080019 CipherBackend, HashBackend, HMACBackend, PBKDF2HMACBackend
Alex Gaynor2d028572014-01-30 11:36:10 -080020)
Alex Gaynorfcaf9762014-01-30 11:23:57 -080021
22
23@utils.register_interface(CipherBackend)
Alex Gaynor2b8b4152014-01-30 11:32:35 -080024@utils.register_interface(HashBackend)
Alex Gaynor2d028572014-01-30 11:36:10 -080025@utils.register_interface(HMACBackend)
Alex Gaynor020ae0f2014-01-30 11:38:43 -080026@utils.register_interface(PBKDF2HMACBackend)
Alex Gaynorfcaf9762014-01-30 11:23:57 -080027class PrioritizedMultiBackend(object):
28 name = "multibackend"
29
30 def __init__(self, backends):
31 self._backends = backends
32
Alex Gaynorebc51612014-01-30 15:42:37 -080033 def _filtered_backends(self, interface):
34 for b in self._backends:
35 if isinstance(b, interface):
36 yield b
37
Alex Gaynorfcaf9762014-01-30 11:23:57 -080038 def cipher_supported(self, algorithm, mode):
Alex Gaynorebc51612014-01-30 15:42:37 -080039 return any(
40 b.cipher_supported(algorithm, mode)
41 for b in self._filtered_backends(CipherBackend)
42 )
Alex Gaynorfcaf9762014-01-30 11:23:57 -080043
44 def create_symmetric_encryption_ctx(self, algorithm, mode):
Alex Gaynorebc51612014-01-30 15:42:37 -080045 for b in self._filtered_backends(CipherBackend):
Alex Gaynorfcaf9762014-01-30 11:23:57 -080046 try:
47 return b.create_symmetric_encryption_ctx(algorithm, mode)
48 except UnsupportedAlgorithm:
49 pass
50 raise UnsupportedAlgorithm
51
52 def create_symmetric_decryption_ctx(self, algorithm, mode):
Alex Gaynorebc51612014-01-30 15:42:37 -080053 for b in self._filtered_backends(CipherBackend):
Alex Gaynorfcaf9762014-01-30 11:23:57 -080054 try:
55 return b.create_symmetric_decryption_ctx(algorithm, mode)
56 except UnsupportedAlgorithm:
57 pass
58 raise UnsupportedAlgorithm
Alex Gaynor2b8b4152014-01-30 11:32:35 -080059
60 def hash_supported(self, algorithm):
Alex Gaynorebc51612014-01-30 15:42:37 -080061 return any(
62 b.hash_supported(algorithm)
63 for b in self._filtered_backends(HashBackend)
64 )
Alex Gaynor2b8b4152014-01-30 11:32:35 -080065
66 def create_hash_ctx(self, algorithm):
Alex Gaynorebc51612014-01-30 15:42:37 -080067 for b in self._filtered_backends(HashBackend):
Alex Gaynor2b8b4152014-01-30 11:32:35 -080068 try:
69 return b.create_hash_ctx(algorithm)
70 except UnsupportedAlgorithm:
71 pass
72 raise UnsupportedAlgorithm
Alex Gaynor2d028572014-01-30 11:36:10 -080073
74 def hmac_supported(self, algorithm):
Alex Gaynorebc51612014-01-30 15:42:37 -080075 return any(
76 b.hmac_supported(algorithm)
77 for b in self._filtered_backends(HMACBackend)
78 )
Alex Gaynor2d028572014-01-30 11:36:10 -080079
80 def create_hmac_ctx(self, key, algorithm):
Alex Gaynorebc51612014-01-30 15:42:37 -080081 for b in self._filtered_backends(HMACBackend):
Alex Gaynor2d028572014-01-30 11:36:10 -080082 try:
83 return b.create_hmac_ctx(key, algorithm)
84 except UnsupportedAlgorithm:
85 pass
86 raise UnsupportedAlgorithm
Alex Gaynor020ae0f2014-01-30 11:38:43 -080087
88 def pbkdf2_hmac_supported(self, algorithm):
Alex Gaynorebc51612014-01-30 15:42:37 -080089 return any(
90 b.pbkdf2_hmac_supported(algorithm)
91 for b in self._filtered_backends(PBKDF2HMACBackend)
92 )
Alex Gaynor020ae0f2014-01-30 11:38:43 -080093
94 def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations,
95 key_material):
Alex Gaynorebc51612014-01-30 15:42:37 -080096 for b in self._filtered_backends(PBKDF2HMACBackend):
Alex Gaynor020ae0f2014-01-30 11:38:43 -080097 try:
98 return b.derive_pbkdf2_hmac(
99 algorithm, length, salt, iterations, key_material
100 )
101 except UnsupportedAlgorithm:
102 pass
103 raise UnsupportedAlgorithm