Beispiel #1
0
    def test_generate_hybrid_encrypt_decrypt(self):
        keyset_servicer = services.KeysetServicer()
        hybrid_servicer = services.HybridServicer()

        tp = hybrid.hybrid_key_templates.ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM
        template = tp.SerializeToString()
        gen_request = testing_api_pb2.KeysetGenerateRequest(template=template)
        gen_response = keyset_servicer.Generate(gen_request, self._ctx)
        self.assertEmpty(gen_response.err)
        private_keyset = gen_response.keyset

        pub_request = testing_api_pb2.KeysetPublicRequest(
            private_keyset=private_keyset)
        pub_response = keyset_servicer.Public(pub_request, self._ctx)
        self.assertEqual(pub_response.WhichOneof('result'), 'public_keyset')
        public_keyset = pub_response.public_keyset

        plaintext = b'The quick brown fox jumps over the lazy dog'
        context_info = b'context_info'
        enc_request = testing_api_pb2.HybridEncryptRequest(
            public_keyset=public_keyset,
            plaintext=plaintext,
            context_info=context_info)
        enc_response = hybrid_servicer.Encrypt(enc_request, self._ctx)
        self.assertEqual(enc_response.WhichOneof('result'), 'ciphertext')
        ciphertext = enc_response.ciphertext

        dec_request = testing_api_pb2.HybridDecryptRequest(
            private_keyset=private_keyset,
            ciphertext=ciphertext,
            context_info=context_info)
        dec_response = hybrid_servicer.Decrypt(dec_request, self._ctx)
        self.assertEqual(dec_response.WhichOneof('result'), 'plaintext')
        self.assertEqual(dec_response.plaintext, plaintext)
Beispiel #2
0
 def encrypt(self, plaintext: bytes, context_info: bytes) -> bytes:
     enc_request = testing_api_pb2.HybridEncryptRequest(
         public_keyset=self._public_handle,
         plaintext=plaintext,
         context_info=context_info)
     enc_response = self._stub.Encrypt(enc_request)
     if enc_response.err:
         raise tink.TinkError(enc_response.err)
     return enc_response.ciphertext
Beispiel #3
0
 def encrypt(self, plaintext: bytes, context_info: bytes) -> bytes:
     logging.info('hybrid Sencrypt in lang %s.', self.lang)
     enc_request = testing_api_pb2.HybridEncryptRequest(
         public_keyset=self._public_handle,
         plaintext=plaintext,
         context_info=context_info)
     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