Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 1 | # 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 | |
| 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
| 16 | from cryptography import utils |
| 17 | from cryptography.exceptions import UnsupportedAlgorithm |
Alex Gaynor | 2d02857 | 2014-01-30 11:36:10 -0800 | [diff] [blame] | 18 | from cryptography.hazmat.backends.interfaces import ( |
Alex Gaynor | 020ae0f | 2014-01-30 11:38:43 -0800 | [diff] [blame] | 19 | CipherBackend, HashBackend, HMACBackend, PBKDF2HMACBackend |
Alex Gaynor | 2d02857 | 2014-01-30 11:36:10 -0800 | [diff] [blame] | 20 | ) |
Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 21 | |
| 22 | |
| 23 | @utils.register_interface(CipherBackend) |
Alex Gaynor | 2b8b415 | 2014-01-30 11:32:35 -0800 | [diff] [blame] | 24 | @utils.register_interface(HashBackend) |
Alex Gaynor | 2d02857 | 2014-01-30 11:36:10 -0800 | [diff] [blame] | 25 | @utils.register_interface(HMACBackend) |
Alex Gaynor | 020ae0f | 2014-01-30 11:38:43 -0800 | [diff] [blame] | 26 | @utils.register_interface(PBKDF2HMACBackend) |
Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 27 | class PrioritizedMultiBackend(object): |
| 28 | name = "multibackend" |
| 29 | |
| 30 | def __init__(self, backends): |
| 31 | self._backends = backends |
| 32 | |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 33 | def _filtered_backends(self, interface): |
| 34 | for b in self._backends: |
| 35 | if isinstance(b, interface): |
| 36 | yield b |
| 37 | |
Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 38 | def cipher_supported(self, algorithm, mode): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 39 | return any( |
| 40 | b.cipher_supported(algorithm, mode) |
| 41 | for b in self._filtered_backends(CipherBackend) |
| 42 | ) |
Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 43 | |
| 44 | def create_symmetric_encryption_ctx(self, algorithm, mode): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 45 | for b in self._filtered_backends(CipherBackend): |
Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 46 | 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 Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 53 | for b in self._filtered_backends(CipherBackend): |
Alex Gaynor | fcaf976 | 2014-01-30 11:23:57 -0800 | [diff] [blame] | 54 | try: |
| 55 | return b.create_symmetric_decryption_ctx(algorithm, mode) |
| 56 | except UnsupportedAlgorithm: |
| 57 | pass |
| 58 | raise UnsupportedAlgorithm |
Alex Gaynor | 2b8b415 | 2014-01-30 11:32:35 -0800 | [diff] [blame] | 59 | |
| 60 | def hash_supported(self, algorithm): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 61 | return any( |
| 62 | b.hash_supported(algorithm) |
| 63 | for b in self._filtered_backends(HashBackend) |
| 64 | ) |
Alex Gaynor | 2b8b415 | 2014-01-30 11:32:35 -0800 | [diff] [blame] | 65 | |
| 66 | def create_hash_ctx(self, algorithm): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 67 | for b in self._filtered_backends(HashBackend): |
Alex Gaynor | 2b8b415 | 2014-01-30 11:32:35 -0800 | [diff] [blame] | 68 | try: |
| 69 | return b.create_hash_ctx(algorithm) |
| 70 | except UnsupportedAlgorithm: |
| 71 | pass |
| 72 | raise UnsupportedAlgorithm |
Alex Gaynor | 2d02857 | 2014-01-30 11:36:10 -0800 | [diff] [blame] | 73 | |
| 74 | def hmac_supported(self, algorithm): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 75 | return any( |
| 76 | b.hmac_supported(algorithm) |
| 77 | for b in self._filtered_backends(HMACBackend) |
| 78 | ) |
Alex Gaynor | 2d02857 | 2014-01-30 11:36:10 -0800 | [diff] [blame] | 79 | |
| 80 | def create_hmac_ctx(self, key, algorithm): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 81 | for b in self._filtered_backends(HMACBackend): |
Alex Gaynor | 2d02857 | 2014-01-30 11:36:10 -0800 | [diff] [blame] | 82 | try: |
| 83 | return b.create_hmac_ctx(key, algorithm) |
| 84 | except UnsupportedAlgorithm: |
| 85 | pass |
| 86 | raise UnsupportedAlgorithm |
Alex Gaynor | 020ae0f | 2014-01-30 11:38:43 -0800 | [diff] [blame] | 87 | |
| 88 | def pbkdf2_hmac_supported(self, algorithm): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 89 | return any( |
| 90 | b.pbkdf2_hmac_supported(algorithm) |
| 91 | for b in self._filtered_backends(PBKDF2HMACBackend) |
| 92 | ) |
Alex Gaynor | 020ae0f | 2014-01-30 11:38:43 -0800 | [diff] [blame] | 93 | |
| 94 | def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations, |
| 95 | key_material): |
Alex Gaynor | ebc5161 | 2014-01-30 15:42:37 -0800 | [diff] [blame] | 96 | for b in self._filtered_backends(PBKDF2HMACBackend): |
Alex Gaynor | 020ae0f | 2014-01-30 11:38:43 -0800 | [diff] [blame] | 97 | try: |
| 98 | return b.derive_pbkdf2_hmac( |
| 99 | algorithm, length, salt, iterations, key_material |
| 100 | ) |
| 101 | except UnsupportedAlgorithm: |
| 102 | pass |
| 103 | raise UnsupportedAlgorithm |