• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Python wrapper of the wrapped C++ AEAD key manager."""
16
17from tink import core
18from tink.aead import _aead
19from tink.aead import _aead_wrapper
20from tink.aead import _kms_aead_key_manager
21from tink.cc.pybind import tink_bindings
22
23
24class AeadCcToPyWrapper(_aead.Aead):
25  """Transforms C++ Aead primitive into a Python primitive."""
26
27  def __init__(self, cc_primitive: tink_bindings.Aead):
28    self._aead = cc_primitive
29
30  @core.use_tink_errors
31  def encrypt(self, plaintext: bytes, associated_data: bytes) -> bytes:
32    return self._aead.encrypt(plaintext, associated_data)
33
34  @core.use_tink_errors
35  def decrypt(self, plaintext: bytes, associated_data: bytes) -> bytes:
36    return self._aead.decrypt(plaintext, associated_data)
37
38
39def register() -> None:
40  """Registers all AEAD key managers and AEAD wrapper in the Registry."""
41  tink_bindings.register()
42  for ident in (
43      'AesCtrHmacAeadKey',
44      'AesGcmKey',
45      'AesGcmSivKey',
46      'AesEaxKey',
47      'XChaCha20Poly1305Key',
48  ):
49    type_url = 'type.googleapis.com/google.crypto.tink.{}'.format(ident)
50    key_manager = core.KeyManagerCcToPyWrapper(
51        tink_bindings.AeadKeyManager.from_cc_registry(type_url), _aead.Aead,
52        AeadCcToPyWrapper)
53    core.Registry.register_key_manager(key_manager, new_key_allowed=True)
54  core.Registry.register_primitive_wrapper(_aead_wrapper.AeadWrapper())
55  core.Registry.register_key_manager(
56      _kms_aead_key_manager.KmsAeadKeyManager(), new_key_allowed=True
57  )
58  core.Registry.register_key_manager(
59      _kms_aead_key_manager.KmsEnvelopeAeadKeyManager(), new_key_allowed=True
60  )
61