Example #1
0
    def test_generate_encrypt_decrypt(self):
        keyset_servicer = services.KeysetServicer()
        aead_servicer = services.AeadServicer()

        template = aead.aead_key_templates.AES128_GCM.SerializeToString()
        gen_request = testing_api_pb2.KeysetGenerateRequest(template=template)
        gen_response = keyset_servicer.Generate(gen_request, self._ctx)
        self.assertEqual(gen_response.WhichOneof('result'), 'keyset')
        keyset = gen_response.keyset
        plaintext = b'The quick brown fox jumps over the lazy dog'
        associated_data = b'associated_data'
        enc_request = testing_api_pb2.AeadEncryptRequest(
            keyset=keyset,
            plaintext=plaintext,
            associated_data=associated_data)
        enc_response = aead_servicer.Encrypt(enc_request, self._ctx)
        self.assertEqual(enc_response.WhichOneof('result'), 'ciphertext')
        ciphertext = enc_response.ciphertext
        dec_request = testing_api_pb2.AeadDecryptRequest(
            keyset=keyset,
            ciphertext=ciphertext,
            associated_data=associated_data)
        dec_response = aead_servicer.Decrypt(dec_request, self._ctx)
        self.assertEqual(dec_response.WhichOneof('result'), 'plaintext')
        self.assertEqual(dec_response.plaintext, plaintext)
Example #2
0
 def encrypt(self, plaintext: bytes, associated_data: bytes) -> bytes:
     enc_request = testing_api_pb2.AeadEncryptRequest(
         keyset=self._keyset,
         plaintext=plaintext,
         associated_data=associated_data)
     enc_response = self._stub.Encrypt(enc_request)
     if enc_response.err:
         raise tink.TinkError(enc_response.err)
     return enc_response.ciphertext
Example #3
0
 def encrypt(self, plaintext: bytes, associated_data: bytes) -> bytes:
     logging.info('encrypt in lang %s.', self.lang)
     enc_request = testing_api_pb2.AeadEncryptRequest(
         keyset=self._keyset,
         plaintext=plaintext,
         associated_data=associated_data)
     enc_response = self._stub.Encrypt(enc_request)
     if enc_response.err:
         logging.info('error encrypt in %s: %s', self.lang,
                      enc_response.err)
         raise tink.TinkError(enc_response.err)
     return enc_response.ciphertext
Example #4
0
 def test_generate_encrypt_decrypt(self):
     t = time.time()
     template = aead.aead_key_templates.AES128_GCM.SerializeToString()
     gen_request = testing_api_pb2.GenerateKeysetRequest(template=template)
     gen_response = self._keyset_stub.Generate(gen_request)
     self.assertEmpty(gen_response.err)
     keyset = gen_response.keyset
     plaintext = b'The quick brown fox jumps over the lazy dog'
     associated_data = b'associated_data'
     enc_request = testing_api_pb2.AeadEncryptRequest(
         keyset=keyset,
         plaintext=plaintext,
         associated_data=associated_data)
     enc_response = self._aead_stub.Encrypt(enc_request)
     self.assertEmpty(enc_response.err)
     ciphertext = enc_response.ciphertext
     dec_request = testing_api_pb2.AeadDecryptRequest(
         keyset=keyset,
         ciphertext=ciphertext,
         associated_data=associated_data)
     dec_response = self._aead_stub.Decrypt(dec_request)
     self.assertEmpty(dec_response.err)
     self.assertEqual(dec_response.plaintext, plaintext)
     logging.info('Testing took %s s', time.time() - t)