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"""Tests for tink.python.tink.streaming_aead_key_templates.""" 15 16from absl.testing import absltest 17from absl.testing import parameterized 18from tink.proto import aes_ctr_hmac_streaming_pb2 19from tink.proto import aes_gcm_hkdf_streaming_pb2 20from tink.proto import common_pb2 21from tink.proto import tink_pb2 22from tink import streaming_aead 23 24 25class StreamingAeadKeyTemplatesTest(parameterized.TestCase): 26 27 def test_create_aes_gcm_hkdf_streaming_key_template(self): 28 # Intentionally using 'weird' or invalid values for parameters, 29 # to test that the function correctly puts them in the resulting template. 30 template = None 31 with self.assertWarns(DeprecationWarning): 32 template = ( 33 streaming_aead.streaming_aead_key_templates 34 .create_aes_gcm_hkdf_streaming_key_template( 35 aes_key_size=42, 36 hash_type=common_pb2.HashType.SHA1, 37 derived_key_size=76, 38 ciphertext_segment_size=64, 39 )) 40 self.assertEqual( 41 'type.googleapis.com/google.crypto.tink.AesGcmHkdfStreamingKey', 42 template.type_url) 43 self.assertEqual(tink_pb2.RAW, template.output_prefix_type) 44 key_format = aes_gcm_hkdf_streaming_pb2.AesGcmHkdfStreamingKeyFormat() 45 key_format.ParseFromString(template.value) 46 self.assertEqual(42, key_format.key_size) 47 self.assertEqual(common_pb2.HashType.SHA1, key_format.params.hkdf_hash_type) 48 self.assertEqual(76, key_format.params.derived_key_size) 49 self.assertEqual(64, key_format.params.ciphertext_segment_size) 50 51 def test_create_aes_ctr_hmac_streaming_key_template(self): 52 # Intentionally using 'weird' or invalid values for parameters, 53 # to test that the function correctly puts them in the resulting template. 54 template = None 55 with self.assertWarns(DeprecationWarning): 56 template = ( 57 streaming_aead.streaming_aead_key_templates 58 .create_aes_ctr_hmac_streaming_key_template( 59 aes_key_size=42, 60 hkdf_hash_type=common_pb2.HashType.SHA1, 61 derived_key_size=76, 62 mac_hash_type=common_pb2.HashType.UNKNOWN_HASH, 63 tag_size=39, 64 ciphertext_segment_size=64, 65 )) 66 self.assertEqual( 67 'type.googleapis.com/google.crypto.tink.AesCtrHmacStreamingKey', 68 template.type_url) 69 self.assertEqual(tink_pb2.RAW, template.output_prefix_type) 70 key_format = aes_ctr_hmac_streaming_pb2.AesCtrHmacStreamingKeyFormat() 71 key_format.ParseFromString(template.value) 72 self.assertEqual(42, key_format.key_size) 73 self.assertEqual(common_pb2.HashType.SHA1, key_format.params.hkdf_hash_type) 74 self.assertEqual(76, key_format.params.derived_key_size) 75 self.assertEqual(common_pb2.HashType.UNKNOWN_HASH, 76 key_format.params.hmac_params.hash) 77 self.assertEqual(39, key_format.params.hmac_params.tag_size) 78 self.assertEqual(64, key_format.params.ciphertext_segment_size) 79 80 81if __name__ == '__main__': 82 absltest.main() 83