• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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"""Pre-generated KeyTemplate for Aead.
16
17One can use these templates to generate a new tink_pb2.Keyset with
18tink_pb2.KeysetHandle. To generate a new keyset that contains a single
19aes_eax_pb2.AesEaxKey, one can do:
20handle = keyset_handle.KeysetHandle(aead_key_templates.AES128_EAX).
21"""
22
23from tink.proto import aes_cmac_prf_pb2
24from tink.proto import common_pb2
25from tink.proto import hkdf_prf_pb2
26from tink.proto import hmac_prf_pb2
27from tink.proto import tink_pb2
28
29_AES_CMAC_PRF_KEY_TYPE_URL = (
30    'type.googleapis.com/google.crypto.tink.AesCmacPrfKey')
31_HMAC_PRF_KEY_TYPE_URL = 'type.googleapis.com/google.crypto.tink.HmacPrfKey'
32_HKDF_PRF_KEY_TYPE_URL = 'type.googleapis.com/google.crypto.tink.HkdfPrfKey'
33
34
35def _create_aes_cmac_key_template(key_size: int) -> tink_pb2.KeyTemplate:
36  """Creates an AES CMAC PRF KeyTemplate, and fills in its values."""
37  key_format = aes_cmac_prf_pb2.AesCmacPrfKeyFormat()
38  key_format.key_size = key_size
39  key_format.version = 0
40  key_template = tink_pb2.KeyTemplate(
41      value=key_format.SerializeToString(),
42      type_url=_AES_CMAC_PRF_KEY_TYPE_URL,
43      output_prefix_type=tink_pb2.RAW,
44  )
45  return key_template
46
47
48def _create_hmac_key_template(
49    key_size: int, hash_type: common_pb2.HashType) -> tink_pb2.KeyTemplate:
50  """Creates an HMAC PRF KeyTemplate, and fills in its values."""
51  key_format = hmac_prf_pb2.HmacPrfKeyFormat()
52  key_format.params.hash = hash_type
53  key_format.key_size = key_size
54  key_format.version = 0
55  key_template = tink_pb2.KeyTemplate(
56      value=key_format.SerializeToString(),
57      type_url=_HMAC_PRF_KEY_TYPE_URL,
58      output_prefix_type=tink_pb2.RAW,
59  )
60  return key_template
61
62
63def _create_hkdf_key_template(
64    key_size: int, hash_type: common_pb2.HashType) -> tink_pb2.KeyTemplate:
65  """Creates an HKDF PRF KeyTemplate, and fills in its values."""
66  key_format = hkdf_prf_pb2.HkdfPrfKeyFormat()
67  key_format.params.hash = hash_type
68  key_format.key_size = key_size
69  key_format.version = 0
70  key_template = tink_pb2.KeyTemplate()
71  key_template.value = key_format.SerializeToString()
72  key_template.type_url = _HKDF_PRF_KEY_TYPE_URL
73  key_template.output_prefix_type = tink_pb2.RAW
74  return key_template
75
76
77AES_CMAC = _create_aes_cmac_key_template(key_size=32)
78HMAC_SHA256 = _create_hmac_key_template(
79    key_size=32, hash_type=common_pb2.SHA256)
80HMAC_SHA512 = _create_hmac_key_template(
81    key_size=64, hash_type=common_pb2.SHA512)
82HKDF_SHA256 = _create_hkdf_key_template(
83    key_size=32, hash_type=common_pb2.SHA256)
84