def test_generate_encrypt_decrypt_deterministically(self): keyset_servicer = services.KeysetServicer() daead_servicer = services.DeterministicAeadServicer() template_proto = daead.deterministic_aead_key_templates.AES256_SIV template = template_proto.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.DeterministicAeadEncryptRequest( keyset=keyset, plaintext=plaintext, associated_data=associated_data) enc_response = daead_servicer.EncryptDeterministically( enc_request, self._ctx) self.assertEqual(enc_response.WhichOneof('result'), 'ciphertext') enc_response2 = daead_servicer.EncryptDeterministically( enc_request, self._ctx) self.assertEqual(enc_response2.WhichOneof('result'), 'ciphertext') self.assertEqual(enc_response2.ciphertext, enc_response.ciphertext) ciphertext = enc_response.ciphertext dec_request = testing_api_pb2.DeterministicAeadDecryptRequest( keyset=keyset, ciphertext=ciphertext, associated_data=associated_data) dec_response = daead_servicer.DecryptDeterministically( dec_request, self._ctx) self.assertEqual(dec_response.WhichOneof('result'), 'plaintext') self.assertEqual(dec_response.plaintext, plaintext)
def decrypt_deterministically(self, ciphertext: bytes, associated_data: bytes) -> bytes: """Decrypts.""" dec_request = testing_api_pb2.DeterministicAeadDecryptRequest( keyset=self._keyset, ciphertext=ciphertext, associated_data=associated_data) dec_response = self._stub.DecryptDeterministically(dec_request) if dec_response.err: raise tink.TinkError(dec_response.err) return dec_response.plaintext
def decrypt_deterministically(self, ciphertext: bytes, associated_data: bytes) -> bytes: """Decrypts.""" logging.info('decrypt in lang %s.', self.lang) dec_request = testing_api_pb2.DeterministicAeadDecryptRequest( keyset=self._keyset, ciphertext=ciphertext, associated_data=associated_data) dec_response = self._stub.DecryptDeterministically(dec_request) if dec_response.err: logging.info('error decrypt in %s: %s', self.lang, dec_response.err) raise tink.TinkError(dec_response.err) return dec_response.plaintext
def test_generate_decrypt_deterministically_fail(self): keyset_servicer = services.KeysetServicer() daead_servicer = services.DeterministicAeadServicer() template_proto = daead.deterministic_aead_key_templates.AES256_SIV template = template_proto.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 ciphertext = b'some invalid ciphertext' associated_data = b'associated_data' dec_request = testing_api_pb2.DeterministicAeadDecryptRequest( keyset=keyset, ciphertext=ciphertext, associated_data=associated_data) dec_response = daead_servicer.DecryptDeterministically(dec_request, self._ctx) self.assertEqual(dec_response.WhichOneof('result'), 'err') logging.info('Error in response: %s', dec_response.err) self.assertNotEmpty(dec_response.err)