1# Copyright 2021 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 that keys are consistently accepted or rejected in all languages.""" 15 16from typing import Iterable 17 18from absl.testing import absltest 19from absl.testing import parameterized 20 21import tink 22 23from util import key_util 24from util import testing_servers 25from util import utilities 26 27 28def all_template_names() -> Iterable[str]: 29 for names in utilities.KEY_TEMPLATE_NAMES.values(): 30 for name in names: 31 yield name 32 33 34# These key templates are not defined in these languages. 35UNDEFINED_TEMPLATES = [ 36 ('ECDSA_P384', 'go'), 37 ('ECDSA_P384_SHA384_IEEE_P1363', 'cc'), 38 ('ECDSA_P384_SHA384_IEEE_P1363', 'java'), 39 ('ECDSA_P384_SHA384_IEEE_P1363', 'go'), 40 ('AES128_GCM_HKDF_1MB', 'cc'), 41 ('ECIES_P256_COMPRESSED_HKDF_HMAC_SHA256_AES128_GCM', 'go'), 42 ('ECIES_P256_COMPRESSED_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256', 'go'), 43 ('ECDSA_P256_IEEE_P1363', 'go'), 44 ('ECDSA_P384_IEEE_P1363', 'go'), 45 ('ECDSA_P521_IEEE_P1363', 'go'), 46 ('AES128_EAX_RAW', 'cc'), 47 ('AES256_EAX_RAW', 'cc'), 48 ('AES128_GCM_SIV_RAW', 'cc'), 49 ('AES256_GCM_SIV_RAW', 'cc'), 50 ('AES128_GCM_SIV_RAW', 'go'), 51 ('AES128_GCM_RAW', 'go'), 52 ('AES128_CTR_HMAC_SHA256_RAW', 'cc'), 53 ('AES256_CTR_HMAC_SHA256_RAW', 'cc'), 54 ('AES128_CTR_HMAC_SHA256_RAW', 'go'), 55 ('AES256_CTR_HMAC_SHA256_RAW', 'go'), 56 ('CHACHA20_POLY1305_RAW', 'go'), 57 ('XCHACHA20_POLY1305_RAW', 'cc'), 58 ('XCHACHA20_POLY1305_RAW', 'go'), 59] 60 61 62def setUpModule(): 63 testing_servers.start('key_generation_consistency') 64 65 66def tearDownModule(): 67 testing_servers.stop() 68 69 70class KeyGenerationConsistencyTest(parameterized.TestCase): 71 72 @parameterized.parameters(all_template_names()) 73 def test_key_template_is_consistent(self, template_name): 74 langs = utilities.SUPPORTED_LANGUAGES_BY_TEMPLATE_NAME[ 75 template_name] 76 templates = {} 77 for lang in langs: 78 if (template_name, lang) in UNDEFINED_TEMPLATES: 79 with self.assertRaises( 80 tink.TinkError, 81 msg=('(%s, %s) is in UNDEFINED_TEMPLATES, but does not fail.' % 82 (template_name, lang))): 83 testing_servers.key_template(lang, template_name) 84 continue 85 try: 86 templates[lang] = testing_servers.key_template(lang, template_name) 87 except tink.TinkError as e: 88 self.fail('(%s,%s): %s' % (lang, template_name, e)) 89 if len(templates) <= 1: 90 # nothing to check. 91 return 92 langs = list(templates.keys()) 93 template = templates[langs[0]] 94 for lang in langs[1:]: 95 key_util.assert_tink_proto_equal( 96 self, 97 templates[lang], 98 template, 99 msg=('templates in %s and %s are not equal:' % (langs[0], lang))) 100 101 102if __name__ == '__main__': 103 absltest.main() 104