Ejemplo n.º 1
0
 def test_encrypt_decrypt(self, key_template, supported_langs):
   private_handle = cli_tinkey.generate_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.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, supported_langs):
     keyset_handle = cli_tinkey.generate_keyset_handle(key_template)
     supported_daeads = [
         cli_daead.CliDeterministicAead(lang, keyset_handle)
         for lang in supported_langs
     ]
     unsupported_daeads = [
         cli_daead.CliDeterministicAead(lang, keyset_handle)
         for lang in cli_daead.LANGUAGES if lang not in supported_langs
     ]
     for p in supported_daeads:
         plaintext = (
             b'This is some plaintext message to be encrypted using key_template '
             b'%s using %s for encryption.' %
             (key_template.encode('utf8'), p.lang.encode('utf8')))
         associated_data = (
             b'Some associated data for %s using %s for encryption.' %
             (key_template.encode('utf8'), p.lang.encode('utf8')))
         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.º 3
0
 def test_generate_encrypt_decrypt(self, key_template):
     keyset_handle = cli_tinkey.generate_keyset_handle(key_template)
     primitive = cli_aead.CliAead('java', keyset_handle)
     plaintext = b'plaintext'
     associated_data = b'associated_data'
     ciphertext = primitive.encrypt(plaintext, associated_data)
     output = primitive.decrypt(ciphertext, associated_data)
     self.assertEqual(output, plaintext)
Ejemplo n.º 4
0
 def test_generate_encrypt_decrypt_deterministically(self):
     keyset_handle = cli_tinkey.generate_keyset_handle(
         cli_tinkey.DAEAD_KEY_TEMPLATE)
     p = cli_daead.CliDeterministicAead('java', keyset_handle)
     plaintext = b'plaintext'
     associated_data = b'associated_data'
     ciphertext = p.encrypt_deterministically(plaintext, associated_data)
     output = p.decrypt_deterministically(ciphertext, associated_data)
     self.assertEqual(output, plaintext)
Ejemplo n.º 5
0
 def test_hybrid_generate_encrypt_decrypt(self, key_template):
     private_handle = cli_tinkey.generate_keyset_handle(key_template)
     public_handle = cli_tinkey.public_keyset_handle(private_handle)
     enc = cli_hybrid.CliHybridEncrypt('java', public_handle)
     dec = cli_hybrid.CliHybridDecrypt('java', private_handle)
     plaintext = b'plaintext'
     context_info = b'context_info'
     ciphertext = enc.encrypt(plaintext, context_info)
     output = dec.decrypt(ciphertext, context_info)
     self.assertEqual(output, plaintext)
Ejemplo n.º 6
0
 def test_compute_verify(self, key_template, supported_langs):
     keyset_handle = cli_tinkey.generate_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.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 testEncryptDecrypt(self, key_template, supported_langs):
     private_handle = cli_tinkey.generate_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.encode('utf8'), enc.lang.encode('utf8')))
         context_info = (
             b'Some context info for %s using %s for encryption.' %
             (key_template.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.º 8
0
 def test_mac_generate_compute_verify(self, key_template):
     keyset_handle = cli_tinkey.generate_keyset_handle(key_template)
     p = cli_mac.CliMac('java', keyset_handle)
     data = b'data'
     mac_value = p.compute_mac(data)
     self.assertIsNone(p.verify_mac(mac_value, data))
Ejemplo n.º 9
0
def new_keyset_handle(key_template: tink_pb2.KeyTemplate) -> tink.KeysetHandle:
  if key_template.type_url == _CHACHA20_POLY1305_KEY_TYPES:
    return cli_tinkey.generate_keyset_handle('CHACHA20_POLY1305')
  return tink.new_keyset_handle(key_template)