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)
def main(unused_argv): aead.register() daead.register() hybrid.register() mac.register() prf.register() signature.register() streaming_aead.register() jwt.register_jwt_mac() fake_kms.register_client() server = grpc.server(futures.ThreadPoolExecutor(max_workers=2)) testing_api_pb2_grpc.add_MetadataServicer_to_server( services.MetadataServicer(), server) testing_api_pb2_grpc.add_KeysetServicer_to_server( services.KeysetServicer(), server) testing_api_pb2_grpc.add_AeadServicer_to_server(services.AeadServicer(), server) testing_api_pb2_grpc.add_DeterministicAeadServicer_to_server( services.DeterministicAeadServicer(), server) testing_api_pb2_grpc.add_MacServicer_to_server(services.MacServicer(), server) testing_api_pb2_grpc.add_PrfSetServicer_to_server( services.PrfSetServicer(), server) testing_api_pb2_grpc.add_HybridServicer_to_server( services.HybridServicer(), server) testing_api_pb2_grpc.add_SignatureServicer_to_server( services.SignatureServicer(), server) testing_api_pb2_grpc.add_StreamingAeadServicer_to_server( services.StreamingAeadServicer(), server) testing_api_pb2_grpc.add_JwtServicer_to_server(jwt_service.JwtServicer(), server) server.add_secure_port('[::]:%d' % FLAGS.port, grpc.local_server_credentials()) server.start() server.wait_for_termination()
def test_generate_hybrid_encrypt_decrypt_fail(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.assertEqual(gen_response.WhichOneof('result'), 'keyset') private_keyset = gen_response.keyset dec_request = testing_api_pb2.HybridDecryptRequest( private_keyset=private_keyset, ciphertext=b'invalid ciphertext', context_info=b'context_info') dec_response = hybrid_servicer.Decrypt(dec_request, self._ctx) self.assertEqual(dec_response.WhichOneof('result'), 'err') self.assertNotEmpty(dec_response.err)