Beispiel #1
0
 def test_fake_mac_fail_wrong_primitive(self):
   mac = helper.FakeMac('Name')
   mac_value = mac.compute_mac(b'data')
   wrong_mac = helper.FakeMac('Wrong Name')
   with self.assertRaisesRegex(core.TinkError,
                               'invalid mac'):
     wrong_mac.verify_mac(mac_value, b'data')
Beispiel #2
0
 def test_wrap_with_primitive_class_success(self):
     self.reg.register_primitive_wrapper(_mac_wrapper.MacWrapper())
     mac1 = helper.FakeMac('FakeMac1')
     mac2 = helper.FakeMac('FakeMac2')
     wrapped_mac = self.reg.wrap(_mac_set([mac1, mac2]), mac.Mac)
     wrapped_mac.verify_mac(mac1.compute_mac(b'data1'), b'data1')
     wrapped_mac.verify_mac(mac2.compute_mac(b'data2'), b'data2')
     wrapped_mac.verify_mac(wrapped_mac.compute_mac(b'data'), b'data')
Beispiel #3
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)
Beispiel #4
0
 def test_list_of_entries_can_be_modified(self):
   primitive_set = core.new_primitive_set(mac.Mac)
   key = helper.fake_key(key_id=1)
   primitive_set.add_primitive(helper.FakeMac('FakeMac'), key)
   entries = primitive_set.primitive(key)
   entries.append('Something')
   self.assertLen(primitive_set.primitive(key), 1)
Beispiel #5
0
 def test_primitive_wrapper_reset(self):
     self.reg.register_primitive_wrapper(_mac_wrapper.MacWrapper())
     self.reg.reset()
     with self.assertRaisesRegex(
             core.TinkError,
             'No PrimitiveWrapper registered for primitive Mac.'):
         self.reg.wrap(_mac_set([helper.FakeMac()]))
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
0
  def test_verify_unknown_mac_fails(self):
    unknown_tag = helper.FakeMac('UnknownfakeMac').compute_mac(b'data')

    pset = core.new_primitive_set(mac.Mac)
    primitive, raw_key = self.new_primitive_key_pair(1234, tink_pb2.RAW)
    new_primitive, new_key = self.new_primitive_key_pair(5678, tink_pb2.TINK)
    pset.add_primitive(primitive, raw_key)
    new_entry = pset.add_primitive(new_primitive, new_key)
    pset.set_primary(new_entry)
    wrapped_mac = mac.MacWrapper().wrap(pset)

    with self.assertRaisesRegex(core.TinkError, 'invalid MAC'):
      wrapped_mac.verify_mac(unknown_tag, b'data')
Beispiel #12
0
 def test_wrap_unknown_primitive(self):
     with self.assertRaisesRegex(
             core.TinkError,
             'No PrimitiveWrapper registered for primitive Mac.'):
         self.reg.wrap(_mac_set([helper.FakeMac()]))
Beispiel #13
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)
Beispiel #14
0
 def wrap(self, _):
     return helper.FakeMac()
Beispiel #15
0
 def test_add_invalid_key_fails(self):
   primitive_set = core.new_primitive_set(mac.Mac)
   key = new_key(MAC_TEMPLATE, key_id=1)
   key.ClearField('output_prefix_type')
   with self.assertRaises(core.TinkError):
     primitive_set.add_primitive(helper.FakeMac(), key)
Beispiel #16
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())
Beispiel #17
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())
Beispiel #18
0
 def test_primitive_wrapper_reset(self):
     self.reg.register_primitive_wrapper(_mac_wrapper.MacWrapper())
     self.reg.reset()
     with self.assertRaises(core.TinkError):
         self.reg.wrap(_mac_set([helper.FakeMac()]), mac.Mac)
Beispiel #19
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
Beispiel #20
0
 def test_fake_mac_success(self):
     mac = helper.FakeMac('Name')
     mac_value = mac.compute_mac(b'data')
     mac.verify_mac(mac_value, b'data')
Beispiel #21
0
 def test_wrap_unknown_primitive(self):
     with self.assertRaises(core.TinkError):
         self.reg.wrap(_mac_set([helper.FakeMac()]), mac.Mac)
Beispiel #22
0
 def test_fake_mac_fail_wrong_data(self):
     mac = helper.FakeMac('Name')
     mac_value = mac.compute_mac(b'data')
     with self.assertRaises(core.TinkError):
         mac.verify_mac(mac_value, b'wrong data')
Beispiel #23
0
 def wrap(self, pset: core.PrimitiveSet) -> mac.Mac:
     _ = pset
     return helper.FakeMac()
Beispiel #24
0
 def test_fake_mac_fail_wrong_data(self):
     mac = helper.FakeMac('Name')
     mac_value = mac.compute_mac(b'data')
     with self.assertRaisesRegex(tink_error.TinkError, 'invalid mac'):
         mac.verify_mac(mac_value, b'wrong data')