Ejemplo n.º 1
0
 def test_encrypt_decrypt(self, key_template_name, supported_langs):
     key_template = supported_key_types.KEY_TEMPLATE[key_template_name]
     private_handle = keyset_builder.new_keyset_handle(key_template)
     supported_signers = [
         cli_signature.CliPublicKeySign(lang, private_handle)
         for lang in supported_langs
     ]
     unsupported_signers = [
         cli_signature.CliPublicKeySign(lang, private_handle)
         for lang in cli_signature.LANGUAGES if lang not in supported_langs
     ]
     public_handle = private_handle.public_keyset_handle()
     supported_verifiers = [
         cli_signature.CliPublicKeyVerify(lang, public_handle)
         for lang in supported_langs
     ]
     unsupported_verifiers = [
         cli_signature.CliPublicKeyVerify(lang, public_handle)
         for lang in cli_signature.LANGUAGES if lang not in supported_langs
     ]
     for signer in supported_signers:
         message = (
             b'A message to be signed using key_template %s in %s.' %
             (key_template_name.encode('utf8'), signer.lang.encode('utf8')))
         sign = signer.sign(message)
         for verifier in supported_verifiers:
             self.assertIsNone(verifier.verify(sign, message))
         for verifier in unsupported_verifiers:
             with self.assertRaises(tink.TinkError):
                 verifier.verify(sign, message)
     for signer in unsupported_signers:
         with self.assertRaises(tink.TinkError):
             _ = signer.sign(message)
Ejemplo n.º 2
0
 def test_encrypt_decrypt(self, key_template_name, supported_langs):
   key_template = supported_key_types.KEY_TEMPLATE[key_template_name]
   keyset_handle = keyset_builder.new_keyset_handle(key_template)
   supported_aeads = [
       cli_aead.CliAead(lang, keyset_handle) for lang in supported_langs
   ]
   unsupported_aeads = [
       cli_aead.CliAead(lang, keyset_handle)
       for lang in cli_aead.LANGUAGES
       if lang not in supported_langs
   ]
   for p in supported_aeads:
     plaintext = (
         b'This is some plaintext message to be encrypted using key_template '
         b'%s using %s for encryption.'
         % (key_template_name.encode('utf8'), p.lang.encode('utf8')))
     associated_data = (
         b'Some associated data for %s using %s for encryption.' %
         (key_template_name.encode('utf8'), p.lang.encode('utf8')))
     ciphertext = p.encrypt(plaintext, associated_data)
     for p2 in supported_aeads:
       output = p2.decrypt(ciphertext, associated_data)
       self.assertEqual(output, plaintext)
     for p2 in unsupported_aeads:
       with self.assertRaises(tink.TinkError):
         p2.decrypt(ciphertext, associated_data)
   for p in unsupported_aeads:
     with self.assertRaises(tink.TinkError):
       p.encrypt(b'plaintext', b'associated_data')
Ejemplo n.º 3
0
 def test_encrypt_decrypt(self, key_template_name, supported_langs):
   key_template = supported_key_types.KEY_TEMPLATE[key_template_name]
   keyset_handle = keyset_builder.new_keyset_handle(key_template)
   supported_daeads = [
       cli_daead.CliDeterministicAead(lang, keyset_handle)
       for lang in supported_langs
   ]
   self.assertNotEmpty(supported_daeads)
   unsupported_daeads = [
       cli_daead.CliDeterministicAead(lang, keyset_handle)
       for lang in cli_daead.LANGUAGES
       if lang not in supported_langs
   ]
   plaintext = (
       b'This is some plaintext message to be encrypted using '
       b'key_template %s.' % key_template_name.encode('utf8'))
   associated_data = (
       b'Some associated data for %s.' % key_template_name.encode('utf8'))
   ciphertext = None
   for p in supported_daeads:
     if ciphertext:
       self.assertEqual(
           ciphertext,
           p.encrypt_deterministically(plaintext, associated_data))
     else:
       ciphertext = p.encrypt_deterministically(plaintext, associated_data)
   for p2 in supported_daeads:
     output = p2.decrypt_deterministically(ciphertext, associated_data)
     self.assertEqual(output, plaintext)
   for p2 in unsupported_daeads:
     with self.assertRaises(tink.TinkError):
       p2.decrypt_deterministically(ciphertext, associated_data)
   for p in unsupported_daeads:
     with self.assertRaises(tink.TinkError):
       p.encrypt_deterministically(b'plaintext', b'associated_data')
Ejemplo n.º 4
0
  def test_new_keyset_handle(self):
    keyset_handle = keyset_builder.new_keyset_handle(
        aead.aead_key_templates.AES128_GCM)
    keyset_info = keyset_handle.keyset_info()
    self.assertLen(keyset_info.key_info, 1)
    self.assertEqual(keyset_info.key_info[0].status, tink_pb2.ENABLED)

    p = keyset_handle.primitive(aead.Aead)
    ciphertext = p.encrypt(b'plaintext', b'ad')
    self.assertEqual(p.decrypt(ciphertext, b'ad'), b'plaintext')
Ejemplo n.º 5
0
 def test_encrypt_decrypt(self, key_template_name, supported_langs):
     key_template = supported_key_types.KEY_TEMPLATE[key_template_name]
     private_handle = keyset_builder.new_keyset_handle(key_template)
     supported_decs = [
         cli_hybrid.CliHybridDecrypt(lang, private_handle)
         for lang in supported_langs
     ]
     unsupported_decs = [
         cli_hybrid.CliHybridDecrypt(lang, private_handle)
         for lang in cli_hybrid.LANGUAGES if lang not in supported_langs
     ]
     public_handle = private_handle.public_keyset_handle()
     supported_encs = [
         cli_hybrid.CliHybridEncrypt(lang, public_handle)
         for lang in supported_langs
     ]
     unsupported_encs = [
         cli_hybrid.CliHybridEncrypt(lang, public_handle)
         for lang in cli_hybrid.LANGUAGES if lang not in supported_langs
     ]
     for enc in supported_encs:
         plaintext = (
             b'This is some plaintext message to be encrypted using key_template '
             b'%s in %s.' %
             (key_template_name.encode('utf8'), enc.lang.encode('utf8')))
         context_info = (
             b'Some context info for %s using %s for encryption.' %
             (key_template_name.encode('utf8'), enc.lang.encode('utf8')))
         ciphertext = enc.encrypt(plaintext, context_info)
         for dec in supported_decs:
             output = dec.decrypt(ciphertext, context_info)
             self.assertEqual(output, plaintext)
         for dec in unsupported_decs:
             with self.assertRaises(tink.TinkError):
                 dec.decrypt(ciphertext, context_info)
     for enc in unsupported_encs:
         with self.assertRaises(tink.TinkError):
             enc.encrypt(b'plaintext', b'context_info')
Ejemplo n.º 6
0
 def test_encrypt_decrypt(self, key_template_name, supported_langs):
     key_template = supported_key_types.KEY_TEMPLATE[key_template_name]
     keyset_handle = keyset_builder.new_keyset_handle(key_template)
     supported_macs = [
         cli_mac.CliMac(lang, keyset_handle) for lang in supported_langs
     ]
     unsupported_macs = [
         cli_mac.CliMac(lang, keyset_handle) for lang in cli_mac.LANGUAGES
         if lang not in supported_langs
     ]
     for p in supported_macs:
         data = (
             b'This is some data to be authenticated using key_template '
             b'%s in %s.' %
             (key_template_name.encode('utf8'), p.lang.encode('utf8')))
         mac_value = p.compute_mac(data)
         for p2 in supported_macs:
             self.assertIsNone(p2.verify_mac(mac_value, data))
         for p2 in unsupported_macs:
             with self.assertRaises(tink.TinkError):
                 p2.verify_mac(mac_value, data)
     for p in unsupported_macs:
         with self.assertRaises(tink.TinkError):
             p.compute_mac(data)
Ejemplo n.º 7
0
 def test_new_chacha_keyset_handle(self):
   chacha_template = tink_pb2.KeyTemplate(
       type_url=('type.googleapis.com/google.crypto.tink.' +
                 'ChaCha20Poly1305Key'),
       output_prefix_type=tink_pb2.TINK)
   _ = keyset_builder.new_keyset_handle(chacha_template)