Class OmemoKeyUtil<T_IdKeyPair,​T_IdKey,​T_PreKey,​T_SigPreKey,​T_Sess,​T_ECPub,​T_Bundle>

  • Type Parameters:
    T_IdKeyPair - IdentityKeyPair class
    T_IdKey - IdentityKey class
    T_PreKey - PreKey class
    T_SigPreKey - SignedPreKey class
    T_Sess - Session class
    T_ECPub - Elliptic Curve PublicKey class
    T_Bundle - Bundle class
    Direct Known Subclasses:
    SignalOmemoKeyUtil

    public abstract class OmemoKeyUtil<T_IdKeyPair,​T_IdKey,​T_PreKey,​T_SigPreKey,​T_Sess,​T_ECPub,​T_Bundle>
    extends java.lang.Object
    Class that is used to convert bytes to keys and vice versa.
    • Method Detail

      • identityKeyToBytes

        public abstract byte[] identityKeyToBytes​(T_IdKey identityKey)
        Serialize an identityKey into bytes.
        Parameters:
        identityKey - idKey
        Returns:
        byte array representation of the identity key.
      • preKeyToBytes

        public abstract byte[] preKeyToBytes​(T_PreKey preKey)
        Serialize a preKey into a byte array.
        Parameters:
        preKey - preKey
        Returns:
        byte[]
      • preKeyFromBytes

        public abstract T_PreKey preKeyFromBytes​(byte[] bytes)
                                          throws java.io.IOException
        Deserialize a preKey from a byte array.
        Parameters:
        bytes - byte array
        Returns:
        deserialized preKey
        Throws:
        java.io.IOException - when something goes wrong
      • generateOmemoPreKeys

        public abstract java.util.TreeMap<java.lang.Integer,​T_PreKeygenerateOmemoPreKeys​(int startId,
                                                                                                 int count)
        Generate 'count' new PreKeys beginning with id 'startId'. These preKeys are published and can be used by contacts to establish sessions with us.
        Parameters:
        startId - start id
        count - how many keys do we want to generate
        Returns:
        Map of new preKeys
      • signedPreKeyFromBytes

        public abstract T_SigPreKey signedPreKeyFromBytes​(byte[] data)
                                                   throws java.io.IOException
        Deserialize a signedPreKey from a byte array.
        Parameters:
        data - byte array
        Returns:
        deserialized signed preKey
        Throws:
        java.io.IOException - when something goes wrong
      • signedPreKeyToBytes

        public abstract byte[] signedPreKeyToBytes​(T_SigPreKey sigPreKey)
        Serialize a signedPreKey into a byte array.
        Parameters:
        sigPreKey - signedPreKey
        Returns:
        byte array
      • bundleFromOmemoBundle

        public abstract T_Bundle bundleFromOmemoBundle​(OmemoBundleElement bundle,
                                                       OmemoDevice contact,
                                                       int keyId)
                                                throws CorruptedOmemoKeyException
        Build a crypto-lib specific PreKeyBundle (T_Bundle) using a PreKey from the OmemoBundleElement 'bundle'. The PreKeyBundle will contain the identityKey, signedPreKey and signature, as well as a preKey from the OmemoBundleElement.
        Parameters:
        bundle - OmemoBundleElement
        contact - Contact that the bundle belongs to
        keyId - id of the preKey that will be selected from the OmemoBundleElement and that the PreKeyBundle will contain
        Returns:
        PreKeyBundle (T_PreKey)
        Throws:
        CorruptedOmemoKeyException - if some key is damaged or malformed
      • signedPreKeySignatureFromKey

        public abstract byte[] signedPreKeySignatureFromKey​(T_SigPreKey signedPreKey)
        Extract the signature from a signedPreKey.
        Parameters:
        signedPreKey - signedPreKey
        Returns:
        signature as byte array
      • generateOmemoIdentityKeyPair

        public abstract T_IdKeyPair generateOmemoIdentityKeyPair()
        Generate a new IdentityKeyPair. We should always have only one pair and usually keep this for a long time.
        Returns:
        deserialized identity key pair
      • signedPreKeyIdFromKey

        public abstract int signedPreKeyIdFromKey​(T_SigPreKey signedPreKey)
        return the id of the given signedPreKey.
        Parameters:
        signedPreKey - key
        Returns:
        id of the key
      • identityKeyPairToBytes

        public abstract byte[] identityKeyPairToBytes​(T_IdKeyPair identityKeyPair)
        serialize an identityKeyPair into bytes.
        Parameters:
        identityKeyPair - identityKeyPair
        Returns:
        byte array
      • identityKeyFromPair

        public abstract T_IdKey identityKeyFromPair​(T_IdKeyPair pair)
        Extract the public identityKey from an identityKeyPair.
        Parameters:
        pair - keyPair
        Returns:
        public key of the pair
      • identityKeyForBundle

        public abstract byte[] identityKeyForBundle​(T_IdKey identityKey)
        Prepare an identityKey for transport in an OmemoBundleElement (serialize it).
        Parameters:
        identityKey - identityKey that will be transported
        Returns:
        key as byte array
      • preKeyPublicKeyForBundle

        public abstract byte[] preKeyPublicKeyForBundle​(T_ECPub preKey)
        Prepare an elliptic curve preKey for transport in an OmemoBundleElement.
        Parameters:
        preKey - key
        Returns:
        key as byte array
      • preKeyForBundle

        public abstract byte[] preKeyForBundle​(T_PreKey preKey)
        Prepare a preKey for transport in an OmemoBundleElement.
        Parameters:
        preKey - preKey
        Returns:
        key as byte array
      • preKeyPublicKeysForBundle

        public java.util.HashMap<java.lang.Integer,​byte[]> preKeyPublicKeysForBundle​(java.util.TreeMap<java.lang.Integer,​T_PreKey> preKeyHashMap)
        Prepare a whole bunche of preKeys for transport.
        Parameters:
        preKeyHashMap - HashMap of preKeys
        Returns:
        HashMap of byte arrays but with the same keyIds as key
      • signedPreKeyPublicForBundle

        public abstract byte[] signedPreKeyPublicForBundle​(T_SigPreKey signedPreKey)
        Prepare a public signedPreKey for transport in a bundle.
        Parameters:
        signedPreKey - signedPreKey
        Returns:
        signedPreKey as byte array
      • rawSessionFromBytes

        public abstract T_Sess rawSessionFromBytes​(byte[] data)
                                            throws java.io.IOException
        Deserialize a raw OMEMO Session from bytes.
        Parameters:
        data - bytes
        Returns:
        raw OMEMO Session
        Throws:
        java.io.IOException - when something goes wrong
      • rawSessionToBytes

        public abstract byte[] rawSessionToBytes​(T_Sess session)
        Serialize a raw OMEMO session into a byte array.
        Parameters:
        session - raw session
        Returns:
        byte array
      • addInBounds

        public static int addInBounds​(int value,
                                      int added)
        Add integers modulo MAX_VALUE.
        Parameters:
        value - base integer
        added - value that is added to the base value
        Returns:
        (value plus added) modulo Integer.MAX_VALUE