Esempio n. 1
0
    def test_compute_prf(self):
        keyset_servicer = services.KeysetServicer()
        prf_set_servicer = services.PrfSetServicer()
        template = prf.prf_key_templates.HMAC_SHA256.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

        key_ids_request = testing_api_pb2.PrfSetKeyIdsRequest(keyset=keyset)
        key_ids_response = prf_set_servicer.KeyIds(key_ids_request, self._ctx)
        self.assertEqual(key_ids_response.WhichOneof('result'), 'output')
        self.assertLen(key_ids_response.output.key_id, 1)
        self.assertEqual(key_ids_response.output.key_id[0],
                         key_ids_response.output.primary_key_id)

        output_length = 31
        compute_request = testing_api_pb2.PrfSetComputeRequest(
            keyset=keyset,
            key_id=key_ids_response.output.primary_key_id,
            input_data=b'input_data',
            output_length=output_length)
        compute_response = prf_set_servicer.Compute(compute_request, self._ctx)
        self.assertEqual(compute_response.WhichOneof('result'), 'output')
        self.assertLen(compute_response.output, output_length)
Esempio n. 2
0
 def compute(self, input_data: bytes, output_length: int) -> bytes:
     request = testing_api_pb2.PrfSetComputeRequest(
         keyset=self._keyset,
         key_id=self._key_id,
         input_data=input_data,
         output_length=output_length)
     response = self._stub.Compute(request)
     if response.err:
         raise tink.TinkError(response.err)
     return response.output
Esempio n. 3
0
 def compute(self, input_data: bytes, output_length: int) -> bytes:
     logging.info('Compute PRF in lang %s.', self.lang)
     request = testing_api_pb2.PrfSetComputeRequest(
         keyset=self._keyset,
         key_id=self._key_id,
         input_data=input_data,
         output_length=output_length)
     response = self._stub.Compute(request)
     if response.err:
         logging.info('Error compute PRF in %s: %s', self.lang,
                      response.err)
         raise tink.TinkError(response.err)
     return response.output
Esempio n. 4
0
    def test_compute_prf_fail(self):
        keyset_servicer = services.KeysetServicer()
        prf_set_servicer = services.PrfSetServicer()
        template = prf.prf_key_templates.HMAC_SHA256.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
        key_ids_request = testing_api_pb2.PrfSetKeyIdsRequest(keyset=keyset)
        key_ids_response = prf_set_servicer.KeyIds(key_ids_request, self._ctx)
        self.assertEqual(key_ids_response.WhichOneof('result'), 'output')
        primary_key_id = key_ids_response.output.primary_key_id

        invalid_output_length = 123456
        invalid_compute_request = testing_api_pb2.PrfSetComputeRequest(
            keyset=keyset,
            key_id=primary_key_id,
            input_data=b'input_data',
            output_length=invalid_output_length)
        invalid_compute_response = prf_set_servicer.Compute(
            invalid_compute_request, self._ctx)
        self.assertEqual(invalid_compute_response.WhichOneof('result'), 'err')
        self.assertNotEmpty(invalid_compute_response.err)