def new_primitive_key_pair(key_id, output_prefix_type):
    fake_key = helper.fake_key(
        key_id=key_id,
        key_material_type=tink_pb2.KeyData.ASYMMETRIC_PRIVATE,
        output_prefix_type=output_prefix_type)
    fake_sign = helper.FakePublicKeySign('fakePublicKeySign {}'.format(key_id))
    return fake_sign, fake_key,
Esempio n. 2
0
 def test_init_fails_on_key_with_unknown_prefix(self):
   keyset = tink_pb2.Keyset()
   keyset.key.extend([
       helper.fake_key(key_id=123, output_prefix_type=tink_pb2.UNKNOWN_PREFIX)
   ])
   keyset.primary_key_id = 123
   with self.assertRaises(core.TinkError):
     _ = _keyset_handle(keyset)
Esempio n. 3
0
 def test_primitive_fails_on_empty_keyset(self):
     keyset = tink_pb2.Keyset()
     keyset.key.extend(
         [helper.fake_key(key_id=1, status=tink_pb2.DESTROYED)])
     keyset.primary_key_id = 1
     handle = _keyset_handle(keyset)
     with self.assertRaisesRegex(core.TinkError, 'empty keyset'):
         handle.primitive(aead.Aead)
 def test_primitive_fails_on_key_with_unknown_status(self):
   keyset = tink_pb2.Keyset()
   keyset.key.extend(
       [helper.fake_key(key_id=1234, status=tink_pb2.UNKNOWN_STATUS)])
   keyset.primary_key_id = 1234
   handle = _keyset_handle(keyset)
   with self.assertRaisesRegex(core.TinkError, 'key 1234 has unknown status'):
     handle.primitive(aead.Aead)
 def test_primitive_fails_on_key_with_unknown_prefix(self):
   keyset = tink_pb2.Keyset()
   keyset.key.extend([
       helper.fake_key(key_id=12, output_prefix_type=tink_pb2.UNKNOWN_PREFIX)
   ])
   keyset.primary_key_id = 12
   handle = _keyset_handle(keyset)
   with self.assertRaisesRegex(core.TinkError, 'key 12 has unknown prefix'):
     handle.primitive(aead.Aead)
Esempio n. 6
0
 def test_init_fails_on_key_without_keydata(self):
   keyset = tink_pb2.Keyset()
   key = helper.fake_key(key_id=123)
   key.ClearField('key_data')
   keyset.key.extend([key])
   keyset.primary_key_id = 123
   with self.assertRaises(core.TinkError):
     handle = _keyset_handle(keyset)
     handle.primitive(aead.Aead)
Esempio n. 7
0
def _mac_set(mac_list) -> core.PrimitiveSet[mac.Mac]:
    """Converts a List of Mac in a PrimitiveSet and sets the last primary."""
    mac_set = core.new_primitive_set(mac.Mac)
    for i, primitive in enumerate(mac_list):
        mac_set.set_primary(
            mac_set.add_primitive(
                primitive,
                helper.fake_key(key_id=i, output_prefix_type=tink_pb2.RAW)))
    return mac_set
Esempio n. 8
0
 def test_wrap_with_incompatible_primitive_class_fails(self):
     self.reg.register_primitive_wrapper(_mac_wrapper.MacWrapper())
     pset = core.new_primitive_set(prf.Prf)
     prf1 = helper.FakePrf('FakePrf1')
     pset.set_primary(
         pset.add_primitive(
             prf1,
             helper.fake_key(key_id=1234, output_prefix_type=tink_pb2.RAW)))
     with self.assertRaises(core.TinkError):
         _ = self.reg.wrap(pset, mac.Mac)
Esempio n. 9
0
  def test_same_key_id_and_prefix_type(self):
    primitive_set = core.new_primitive_set(mac.Mac)
    key1 = helper.fake_key(key_id=1, status=tink_pb2.ENABLED)
    fake_mac1 = helper.FakeMac('FakeMac1')
    primitive_set.add_primitive(fake_mac1, key1)
    key2 = helper.fake_key(key_id=1, status=tink_pb2.DISABLED)
    fake_mac2 = helper.FakeMac('FakeMac2')
    primitive_set.add_primitive(fake_mac2, key2)

    expected_ident = crypto_format.output_prefix(key1)
    entries = primitive_set.primitive(key1)
    self.assertLen(entries, 2)
    self.assertEqual(fake_mac1, entries[0].primitive)
    self.assertEqual(fake_mac2, entries[1].primitive)
    self.assertEqual(tink_pb2.ENABLED, entries[0].status)
    self.assertEqual(tink_pb2.DISABLED, entries[1].status)
    self.assertEqual(expected_ident, entries[0].identifier)
    self.assertEqual(expected_ident, entries[1].identifier)
    self.assertLen(primitive_set.primitive(key2), 2)
Esempio n. 10
0
  def test_primary_returns_primary(self):
    primitive_set = core.new_primitive_set(mac.Mac)
    key = helper.fake_key(key_id=1)
    fake_mac = helper.FakeMac('FakeMac')
    entry = primitive_set.add_primitive(fake_mac, key)
    primitive_set.set_primary(entry)

    entry = primitive_set.primary()
    self.assertEqual(fake_mac, entry.primitive)
    self.assertEqual(tink_pb2.ENABLED, entry.status)
    self.assertEqual(crypto_format.output_prefix(key), entry.identifier)
Esempio n. 11
0
  def test_raw_primitives(self):
    primitive_set = core.new_primitive_set(mac.Mac)
    primitive_set.add_primitive(
        helper.FakeMac('FakeMac1'), helper.fake_key(key_id=1))
    key2 = helper.fake_key(key_id=1, output_prefix_type=tink_pb2.RAW)
    fake_mac2 = helper.FakeMac('FakeMac2')
    primitive_set.add_primitive(fake_mac2, key2)
    key3 = helper.fake_key(
        key_id=3, status=tink_pb2.DISABLED, output_prefix_type=tink_pb2.RAW)
    fake_mac3 = helper.FakeMac('FakeMac3')
    primitive_set.add_primitive(fake_mac3, key3)

    entries = primitive_set.raw_primitives()
    self.assertLen(entries, 2)
    self.assertEqual(fake_mac2, entries[0].primitive)
    self.assertEqual(tink_pb2.ENABLED, entries[0].status)
    self.assertEqual(crypto_format.RAW_PREFIX, entries[0].identifier)
    self.assertEqual(fake_mac3, entries[1].primitive)
    self.assertEqual(tink_pb2.DISABLED, entries[1].status)
    self.assertEqual(crypto_format.RAW_PREFIX, entries[1].identifier)
Esempio n. 12
0
  def test_same_key_id_but_different_prefix_type(self):
    primitive_set = core.new_primitive_set(mac.Mac)
    key1 = helper.fake_key(key_id=1, output_prefix_type=tink_pb2.TINK)
    fake_mac1 = helper.FakeMac('FakeMac1')
    primitive_set.add_primitive(fake_mac1, key1)
    key2 = helper.fake_key(key_id=1, output_prefix_type=tink_pb2.LEGACY)
    fake_mac2 = helper.FakeMac('FakeMac2')
    primitive_set.add_primitive(fake_mac2, key2)

    entries1 = primitive_set.primitive(key1)
    self.assertLen(entries1, 1)
    self.assertEqual(fake_mac1, entries1[0].primitive)
    self.assertEqual(tink_pb2.ENABLED, entries1[0].status)
    self.assertEqual(crypto_format.output_prefix(key1), entries1[0].identifier)

    entries2 = primitive_set.primitive(key2)
    self.assertLen(entries2, 1)
    self.assertEqual(fake_mac2, entries2[0].primitive)
    self.assertEqual(tink_pb2.ENABLED, entries2[0].status)
    self.assertEqual(crypto_format.output_prefix(key2), entries2[0].identifier)
Esempio n. 13
0
 def test_primitive_returns_entry(self):
     key = helper.fake_key(key_id=1)
     fake_mac = helper.FakeMac('FakeMac')
     primitive_set = core.new_primitive_set(mac.Mac)
     primitive_set.add_primitive(fake_mac, key)
     entries = primitive_set.primitive(key)
     self.assertLen(entries, 1)
     entry = entries[0]
     self.assertEqual(fake_mac, entry.primitive)
     self.assertEqual(tink_pb2.ENABLED, entry.status)
     self.assertEqual(core.crypto_format.output_prefix(key),
                      entry.identifier)
Esempio n. 14
0
 def test_init_fails_without_primary_key_present(self):
   keyset = tink_pb2.Keyset()
   keyset.key.extend([helper.fake_key(key_id=123)])
   with self.assertRaises(core.TinkError):
     _ = _keyset_handle(keyset)
Esempio n. 15
0
 def new_primitive_key_pair(self, key_id):
     fake_key = helper.fake_key(key_id=key_id,
                                output_prefix_type=tink_pb2.RAW)
     fake_prf_set = helper.FakePrfSet('fakePrfSet {}'.format(key_id))
     return fake_prf_set, fake_key
Esempio n. 16
0
 def test_init_fails_on_keyset_with_only_destroyed_keys(self):
   keyset = tink_pb2.Keyset()
   keyset.key.extend([helper.fake_key(key_id=123, status=tink_pb2.DESTROYED)])
   keyset.primary_key_id = 123
   with self.assertRaises(core.TinkError):
     _ = _keyset_handle(keyset)
Esempio n. 17
0
 def test_init_with_fake_key_success(self):
   keyset = tink_pb2.Keyset()
   keyset.key.extend([helper.fake_key(key_id=123)])
   keyset.primary_key_id = 123
   _ = _keyset_handle(keyset)
Esempio n. 18
0
 def new_primitive_key_pair(self, key_id, output_prefix_type):
     fake_key = helper.fake_key(key_id=key_id,
                                output_prefix_type=output_prefix_type)
     fake_aead = helper.FakeAead('fakeAead {}'.format(key_id))
     return fake_aead, fake_key
Esempio n. 19
0
 def test_add_invalid_key_fails(self):
   primitive_set = core.new_primitive_set(mac.Mac)
   key = helper.fake_key()
   key.ClearField('output_prefix_type')
   with self.assertRaisesRegex(core.TinkError, 'invalid OutputPrefixType'):
     primitive_set.add_primitive(helper.FakeMac(), key)
Esempio n. 20
0
 def test_primary_returns_none(self):
   primitive_set = core.new_primitive_set(mac.Mac)
   primitive_set.add_primitive(
       helper.FakeMac('FakeMac'), helper.fake_key(key_id=1))
   self.assertEqual(primitive_set.primary(), None)
Esempio n. 21
0
 def test_add_wrong_primitive_fails(self):
   primitive_set = core.new_primitive_set(aead.Aead)
   with self.assertRaises(core.TinkError):
     primitive_set.add_primitive(helper.FakeMac(), helper.fake_key())
Esempio n. 22
0
 def test_add_wrong_primitive_fails(self):
   primitive_set = core.new_primitive_set(aead.Aead)
   with self.assertRaisesRegex(core.TinkError,
                               'The primitive is not an instance of '):
     primitive_set.add_primitive(helper.FakeMac(), helper.fake_key())
def _primitive_key_pair(key_id):
    fake_key = helper.fake_key(key_id=key_id, output_prefix_type=tink_pb2.RAW)
    fake_saead = fake_streaming_aead.FakeRawStreamingAead(
        'fakeStreamingAead {}'.format(key_id))
    return fake_saead, fake_key
Esempio n. 24
0
 def test_unknown_key_returns_empty_list(self):
   primitive_set = core.new_primitive_set(mac.Mac)
   unknown_key = helper.fake_key(key_id=1)
   self.assertEqual(primitive_set.primitive(unknown_key), [])
Esempio n. 25
0
 def new_primitive_key_pair(self, key_id, output_prefix_type):
     fake_key = helper.fake_key(key_id=key_id,
                                output_prefix_type=output_prefix_type)
     fake_mac = helper.FakeMac('fakeMac {}'.format(key_id))
     return fake_mac, fake_key