How to use the tink.python.core.primitive_wrapper.PrimitiveWrapper function in tink

To help you get started, we’ve selected a few tink examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / tink / python / aead / aead_wrapper.py View on Github external
return entry.primitive.decrypt(ciphertext_no_prefix,
                                         associated_data)
        except tink_error.TinkError as e:
          logging.info(
              'ciphertext prefix matches a key, but cannot decrypt: %s', e)
    # Let's try all RAW keys.
    for entry in self._primitive_set.raw_primitives():
      try:
        return entry.primitive.decrypt(ciphertext, associated_data)
      except tink_error.TinkError as e:
        pass
    # nothing works.
    raise tink_error.TinkError('Decryption failed.')


class AeadWrapper(primitive_wrapper.PrimitiveWrapper[aead.Aead]):
  """AeadWrapper is the implementation of PrimitiveWrapper for Aead.

  Key rotation works as follows: each ciphertext is prefixed with the keyId.
  When decrypting, we first try all primitives whose keyId starts with the
  prefix of the ciphertext. If none of these succeed, we try the raw primitives.
  If any succeeds, we return the ciphertext, otherwise we simply raise a
  TinkError.
  """

  def wrap(self, pset: primitive_set.PrimitiveSet) -> aead.Aead:
    return _WrappedAead(pset)

  def primitive_class(self) -> Type[aead.Aead]:
    return aead.Aead
github google / tink / python / hybrid / hybrid_decrypt_wrapper.py View on Github external
context_info)
        except tink_error.TinkError as e:
          logging.info(
              'ciphertext prefix matches a key, but cannot decrypt: %s', e)
    # Let's try all RAW keys.
    for entry in self._primitive_set.raw_primitives():
      try:
        return entry.primitive.decrypt(ciphertext, context_info)
      except tink_error.TinkError as e:
        pass
    # nothing works.
    raise tink_error.TinkError('Decryption failed.')


class HybridDecryptWrapper(
    primitive_wrapper.PrimitiveWrapper[hybrid_decrypt.HybridDecrypt]):
  """HybridDecryptWrapper is the PrimitiveWrapper for HybridDecrypt.

  The returned primitive works with a keyset (rather than a single key). To
  decrypt, the primitive uses the prefix of the ciphertext to efficiently select
  the right key in the set. If the keys associated with the prefix do not work,
  the primitive tries all keys with OutputPrefixType RAW.
  """

  def wrap(self,
           pset: primitive_set.PrimitiveSet) -> hybrid_decrypt.HybridDecrypt:
    return _WrappedHybridDecrypt(pset)

  def primitive_class(self) -> Type[hybrid_decrypt.HybridDecrypt]:
    return hybrid_decrypt.HybridDecrypt
github google / tink / python / signature / public_key_verify_wrapper.py View on Github external
err)

    # No matching key succeeded with verification, try all RAW keys
    for entry in self._primitive_set.raw_primitives():
      try:
        entry.primitive.verify(signature, data)
        # Signature is valid, we can return
        return
      except tink_error.TinkError:
        pass

    raise tink_error.TinkError('invalid signature')


class PublicKeyVerifyWrapper(
    primitive_wrapper.PrimitiveWrapper[public_key_verify.PublicKeyVerify]):
  """WrappedPublicKeyVerify is the PrimitiveWrapper for PublicKeyVerify.

  The returned primitive works with a keyset (rather than a single key). To sign
  a message, it uses the primary key in the keyset, and prepends to the
  signature a certain prefix associated with the primary key.

  The returned primitive works with a keyset (rather than a single key). To
  verify a signature, the primitive uses the prefix of the signature to
  efficiently select the right key in the set. If there is no key associated
  with the prefix or if the keys associated with the prefix do not work, the
  primitive tries all keys with tink_pb2.OutputPrefixType = tink_pb2.RAW.
  """

  def wrap(self, primitives_set: primitive_set.PrimitiveSet
          ) -> _WrappedPublicKeyVerify:
    return _WrappedPublicKeyVerify(primitives_set)
github google / tink / python / core / __init__.py View on Github external
KeysetReader = keyset_reader.KeysetReader
JsonKeysetReader = keyset_reader.JsonKeysetReader
BinaryKeysetReader = keyset_reader.BinaryKeysetReader

KeysetWriter = keyset_writer.KeysetWriter
JsonKeysetWriter = keyset_writer.JsonKeysetWriter
BinaryKeysetWriter = keyset_writer.BinaryKeysetWriter

Registry = registry.Registry

TinkError = tink_error.TinkError

new_primitive_set = primitive_set.new_primitive_set
PrimitiveSet = primitive_set.PrimitiveSet
PrimitiveWrapper = primitive_wrapper.PrimitiveWrapper

crypto_format = _crypto_format
github google / tink / python / daead / deterministic_aead_wrapper.py View on Github external
except tink_error.TinkError as e:
          logging.info(
              'ciphertext prefix matches a key, but cannot decrypt: %s', e)
    # Let's try all RAW keys.
    for entry in self._primitive_set.raw_primitives():
      try:
        return entry.primitive.decrypt_deterministically(ciphertext,
                                                         associated_data)
      except tink_error.TinkError as e:
        pass
    # nothing works.
    raise tink_error.TinkError('Decryption failed.')


class DeterministicAeadWrapper(
    primitive_wrapper.PrimitiveWrapper[deterministic_aead.DeterministicAead]):
  """DeterministicAeadWrapper is a PrimitiveWrapper for DeterministicAead.

  The created primitive works with a keyset (rather than a single key). To
  encrypt a plaintext, it uses the primary key in the keyset, and prepends to
  the ciphertext a certain prefix associated with the primary key. To decrypt,
  the primitive uses the prefix of the ciphertext to efficiently select the
  right key in the set. If the keys associated with the prefix do not work, the
  primitive tries all keys with OutputPrefixType RAW.
  """

  def wrap(self, pset: primitive_set.PrimitiveSet
          ) -> deterministic_aead.DeterministicAead:
    return _WrappedDeterministicAead(pset)

  def primitive_class(self) -> Type[deterministic_aead.DeterministicAead]:
    return deterministic_aead.DeterministicAead