Java Code Examples for org.whispersystems.libsignal.ecc.ECPublicKey

The following examples show how to use org.whispersystems.libsignal.ecc.ECPublicKey. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
private AliceSignalProtocolParameters(IdentityKeyPair ourIdentityKey, ECKeyPair ourBaseKey,
                                      IdentityKey theirIdentityKey, ECPublicKey theirSignedPreKey,
                                      ECPublicKey theirRatchetKey, Optional<ECPublicKey> theirOneTimePreKey)
{
  this.ourIdentityKey     = ourIdentityKey;
  this.ourBaseKey         = ourBaseKey;
  this.theirIdentityKey   = theirIdentityKey;
  this.theirSignedPreKey  = theirSignedPreKey;
  this.theirRatchetKey    = theirRatchetKey;
  this.theirOneTimePreKey = theirOneTimePreKey;

  if (ourIdentityKey == null || ourBaseKey == null || theirIdentityKey == null ||
      theirSignedPreKey == null || theirRatchetKey == null || theirOneTimePreKey == null)
  {
    throw new IllegalArgumentException("Null values!");
  }
}
 
Example #2
Source Project: mollyim-android   Author: mollyim   File: MasterSecretUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static AsymmetricMasterSecret getAsymmetricMasterSecret(@NonNull  Context context,
                                                               @Nullable MasterSecret masterSecret)
{
  try {
    byte[] djbPublicBytes   = retrieve(context, ASYMMETRIC_LOCAL_PUBLIC_DJB);
    byte[] djbPrivateBytes  = retrieve(context, ASYMMETRIC_LOCAL_PRIVATE_DJB);

    MasterCipher masterCipher  = new MasterCipher(masterSecret);
    ECPublicKey  djbPublicKey  = masterCipher.decryptPublicKey(djbPublicBytes);
    ECPrivateKey djbPrivateKey = masterCipher.decryptPrivateKey(djbPrivateBytes);

    return new AsymmetricMasterSecret(djbPublicKey, djbPrivateKey);
  } catch (InvalidKeyException ike) {
    throw new SecurityException(ike);
  }
}
 
Example #3
Source Project: mollyim-android   Author: mollyim   File: AsymmetricMasterCipher.java    License: GNU General Public License v3.0 6 votes vote down vote up
public byte[] encryptBytes(byte[] body) {
  try {
    ECPublicKey  theirPublic        = asymmetricMasterSecret.getDjbPublicKey();
    ECKeyPair    ourKeyPair         = Curve.generateKeyPair();
    byte[]       secret             = Curve.calculateAgreement(theirPublic, ourKeyPair.getPrivateKey());
    MasterCipher masterCipher       = getMasterCipherForSecret(secret);
    byte[]       encryptedBodyBytes = masterCipher.encrypt(body);

    PublicKey    ourPublicKey       = new PublicKey(31337, ourKeyPair.getPublicKey());
    byte[]       publicKeyBytes     = ourPublicKey.serialize();

    return Util.combine(publicKeyBytes, encryptedBodyBytes);
  } catch (InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
Example #4
Source Project: bcm-android   Author: bcmapp   File: MasterSecretUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static AsymmetricMasterSecret getAsymmetricMasterSecret(@NonNull AccountContext accountContext,
                                                               @Nullable MasterSecret masterSecret) {
    try {
        byte[] djbPublicBytes = retrieve(accountContext, ASYMMETRIC_LOCAL_PUBLIC_DJB);
        byte[] djbPrivateBytes = retrieve(accountContext, ASYMMETRIC_LOCAL_PRIVATE_DJB);

        ECPublicKey djbPublicKey = null;
        ECPrivateKey djbPrivateKey = null;

        if (djbPublicBytes != null) {
            djbPublicKey = Curve.decodePoint(djbPublicBytes, 0);
        }

        if (masterSecret != null) {
            MasterCipher masterCipher = new MasterCipher(masterSecret);

            if (djbPrivateBytes != null) {
                djbPrivateKey = masterCipher.decryptKey(djbPrivateBytes);
            }
        }

        return new AsymmetricMasterSecret(djbPublicKey, djbPrivateKey);
    } catch (InvalidKeyException | IOException ike) {
        throw new AssertionError(ike);
    }
}
 
Example #5
Source Project: bcm-android   Author: bcmapp   File: AsymmetricMasterCipher.java    License: GNU General Public License v3.0 6 votes vote down vote up
public byte[] encryptBytes(byte[] body) {
  try {
    ECPublicKey  theirPublic        = asymmetricMasterSecret.getDjbPublicKey();
    ECKeyPair    ourKeyPair         = Curve.generateKeyPair();
    byte[]       secret             = Curve.calculateAgreement(theirPublic, ourKeyPair.getPrivateKey());
    MasterCipher masterCipher       = getMasterCipherForSecret(secret);
    byte[]       encryptedBodyBytes = masterCipher.encryptBytes(body);

    PublicKey    ourPublicKey       = new PublicKey(31337, ourKeyPair.getPublicKey());
    byte[]       publicKeyBytes     = ourPublicKey.serialize();

    return Util.combine(publicKeyBytes, encryptedBodyBytes);
  } catch (InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
Example #6
Source Project: Conversations   Author: iNPUTmice   File: IqGenerator.java    License: GNU General Public License v3.0 6 votes vote down vote up
public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
                               final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
    final Element item = new Element("item");
    item.setAttribute("id", "current");
    final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
    final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
    signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
    ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
    signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
    final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
    signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
    final Element identityKeyElement = bundle.addChild("identityKey");
    identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));

    final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
    for (PreKeyRecord preKeyRecord : preKeyRecords) {
        final Element prekey = prekeys.addChild("preKeyPublic");
        prekey.setAttribute("preKeyId", preKeyRecord.getId());
        prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
    }

    return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
}
 
Example #7
Source Project: Pix-Art-Messenger   Author: kriztan   File: IqGenerator.java    License: GNU General Public License v3.0 6 votes vote down vote up
public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
                               final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
    final Element item = new Element("item");
    item.setAttribute("id", "current");
    final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
    final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
    signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
    ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
    signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
    final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
    signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
    final Element identityKeyElement = bundle.addChild("identityKey");
    identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));

    final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
    for (PreKeyRecord preKeyRecord : preKeyRecords) {
        final Element prekey = prekeys.addChild("preKeyPublic");
        prekey.setAttribute("preKeyId", preKeyRecord.getId());
        prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
    }

    return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
}
 
Example #8
Source Project: libsignal-protocol-java   Author: signalapp   File: SignalMessage.java    License: GNU General Public License v3.0 6 votes vote down vote up
public SignalMessage(int messageVersion, SecretKeySpec macKey, ECPublicKey senderRatchetKey,
                     int counter, int previousCounter, byte[] ciphertext,
                     IdentityKey senderIdentityKey,
                     IdentityKey receiverIdentityKey)
{
  byte[] version = {ByteUtil.intsToByteHighAndLow(messageVersion, CURRENT_VERSION)};
  byte[] message = SignalProtos.SignalMessage.newBuilder()
                                             .setRatchetKey(ByteString.copyFrom(senderRatchetKey.serialize()))
                                             .setCounter(counter)
                                             .setPreviousCounter(previousCounter)
                                             .setCiphertext(ByteString.copyFrom(ciphertext))
                                             .build().toByteArray();

  byte[] mac     = getMac(senderIdentityKey, receiverIdentityKey, macKey, ByteUtil.combine(version, message));

  this.serialized       = ByteUtil.combine(version, message, mac);
  this.senderRatchetKey = senderRatchetKey;
  this.counter          = counter;
  this.previousCounter  = previousCounter;
  this.ciphertext       = ciphertext;
  this.messageVersion   = messageVersion;
}
 
Example #9
Source Project: libsignal-protocol-java   Author: signalapp   File: SessionState.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void setMessageKeys(ECPublicKey senderEphemeral, MessageKeys messageKeys) {
  Pair<Chain,Integer> chainAndIndex       = getReceiverChain(senderEphemeral);
  Chain               chain               = chainAndIndex.first();
  Chain.MessageKey    messageKeyStructure = Chain.MessageKey.newBuilder()
                                                            .setCipherKey(ByteString.copyFrom(messageKeys.getCipherKey().getEncoded()))
                                                            .setMacKey(ByteString.copyFrom(messageKeys.getMacKey().getEncoded()))
                                                            .setIndex(messageKeys.getCounter())
                                                            .setIv(ByteString.copyFrom(messageKeys.getIv().getIV()))
                                                            .build();

  Chain.Builder updatedChain = chain.toBuilder().addMessageKeys(messageKeyStructure);

  if (updatedChain.getMessageKeysCount() > MAX_MESSAGE_KEYS) {
    updatedChain.removeMessageKeys(0);
  }

  this.sessionStructure = this.sessionStructure.toBuilder()
                                               .setReceiverChains(chainAndIndex.second(),
                                                                  updatedChain.build())
                                               .build();
}
 
Example #10
Source Project: libsignal-protocol-java   Author: signalapp   File: SessionState.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void addReceiverChain(ECPublicKey senderRatchetKey, ChainKey chainKey) {
  Chain.ChainKey chainKeyStructure = Chain.ChainKey.newBuilder()
                                                   .setKey(ByteString.copyFrom(chainKey.getKey()))
                                                   .setIndex(chainKey.getIndex())
                                                   .build();

  Chain chain = Chain.newBuilder()
                     .setChainKey(chainKeyStructure)
                     .setSenderRatchetKey(ByteString.copyFrom(senderRatchetKey.serialize()))
                     .build();

  this.sessionStructure = this.sessionStructure.toBuilder().addReceiverChains(chain).build();

  if (this.sessionStructure.getReceiverChainsList().size() > 5) {
    this.sessionStructure = this.sessionStructure.toBuilder()
                                                 .removeReceiverChains(0)
                                                 .build();
  }
}
 
Example #11
Source Project: libsignal-protocol-java   Author: signalapp   File: SessionState.java    License: GNU General Public License v3.0 6 votes vote down vote up
public boolean hasMessageKeys(ECPublicKey senderEphemeral, int counter) {
  Pair<Chain,Integer> chainAndIndex = getReceiverChain(senderEphemeral);
  Chain               chain         = chainAndIndex.first();

  if (chain == null) {
    return false;
  }

  List<Chain.MessageKey> messageKeyList = chain.getMessageKeysList();

  for (Chain.MessageKey messageKey : messageKeyList) {
    if (messageKey.getIndex() == counter) {
      return true;
    }
  }

  return false;
}
 
Example #12
Source Project: mollyim-android   Author: mollyim   File: MasterCipher.java    License: GNU General Public License v3.0 5 votes vote down vote up
public ECPublicKey decryptPublicKey(byte[] key) throws InvalidKeyException {
  try {
    return Curve.decodePoint(decrypt(key, "ECPublicKey".getBytes()), 0);
  } catch (GeneralSecurityException ge) {
    throw new InvalidKeyException(ge);
  }
}
 
Example #13
Source Project: libsignal-protocol-java   Author: signalapp   File: RatchetingSession.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void initializeSession(SessionState sessionState, SymmetricSignalProtocolParameters parameters)
    throws InvalidKeyException
{
  if (isAlice(parameters.getOurBaseKey().getPublicKey(), parameters.getTheirBaseKey())) {
    AliceSignalProtocolParameters.Builder aliceParameters = AliceSignalProtocolParameters.newBuilder();

    aliceParameters.setOurBaseKey(parameters.getOurBaseKey())
                   .setOurIdentityKey(parameters.getOurIdentityKey())
                   .setTheirRatchetKey(parameters.getTheirRatchetKey())
                   .setTheirIdentityKey(parameters.getTheirIdentityKey())
                   .setTheirSignedPreKey(parameters.getTheirBaseKey())
                   .setTheirOneTimePreKey(Optional.<ECPublicKey>absent());

    RatchetingSession.initializeSession(sessionState, aliceParameters.create());
  } else {
    BobSignalProtocolParameters.Builder bobParameters = BobSignalProtocolParameters.newBuilder();

    bobParameters.setOurIdentityKey(parameters.getOurIdentityKey())
                 .setOurRatchetKey(parameters.getOurRatchetKey())
                 .setOurSignedPreKey(parameters.getOurBaseKey())
                 .setOurOneTimePreKey(Optional.<ECKeyPair>absent())
                 .setTheirBaseKey(parameters.getTheirBaseKey())
                 .setTheirIdentityKey(parameters.getTheirIdentityKey());

    RatchetingSession.initializeSession(sessionState, bobParameters.create());
  }
}
 
Example #14
Source Project: bcm-android   Author: bcmapp   File: PreKeyEntity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public ECPublicKey deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (null != json) {
        try {
            String publicSting = json.getAsString();
            return Curve.decodePoint(Base64.decodeWithoutPadding(publicSting), 0);
        } catch (Throwable e) {
            throw new JsonParseException("unknown public key", e);
        }
    }
    return null;
}
 
Example #15
Source Project: signald   Author: thefinn93   File: Manager.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
    Map<String, String> query = getQueryMap(linkUri.getRawQuery());
    String deviceIdentifier = query.get("uuid");
    String publicKeyEncoded = query.get("pub_key");

    if (isEmpty(deviceIdentifier) || isEmpty(publicKeyEncoded)) {
        throw new RuntimeException("Invalid device link uri");
    }

    ECPublicKey deviceKey = Curve.decodePoint(Base64.decode(publicKeyEncoded), 0);

    addDevice(deviceIdentifier, deviceKey);
}
 
Example #16
Source Project: signald   Author: thefinn93   File: Manager.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
    IdentityKeyPair identityKeyPair = accountData.axolotlStore.identityKeyStore.getIdentityKeyPair();
    String verificationCode = accountManager.getNewDeviceVerificationCode();

    // TODO send profile key
    accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, Optional.<byte[]>absent(), verificationCode);
}
 
Example #17
Source Project: signald   Author: thefinn93   File: Manager.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static CertificateValidator getCertificateValidator() {
    try {
        ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(BuildConfig.UNIDENTIFIED_SENDER_TRUST_ROOT), 0);
        return new CertificateValidator(unidentifiedSenderTrustRoot);
    } catch (InvalidKeyException | IOException e) {
        throw new AssertionError(e);
    }
}
 
Example #18
Source Project: libsignal-service-java   Author: signalapp   File: SignalServiceAccountManager.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void addDevice(String deviceIdentifier,
                      ECPublicKey deviceKey,
                      IdentityKeyPair identityKeyPair,
                      Optional<byte[]> profileKey,
                      String code)
    throws InvalidKeyException, IOException
{
  ProvisioningCipher       cipher  = new ProvisioningCipher(deviceKey);
  ProvisionMessage.Builder message = ProvisionMessage.newBuilder()
                                                     .setIdentityKeyPublic(ByteString.copyFrom(identityKeyPair.getPublicKey().serialize()))
                                                     .setIdentityKeyPrivate(ByteString.copyFrom(identityKeyPair.getPrivateKey().serialize()))
                                                     .setProvisioningCode(code)
                                                     .setProvisioningVersion(ProvisioningVersion.CURRENT_VALUE);
  if (userE164 != null) {
    message.setNumber(userE164);
  }

  if (userUuid != null) {
    message.setUuid(userUuid.toString());
  }

  if (profileKey.isPresent()) {
    message.setProfileKey(ByteString.copyFrom(profileKey.get()));
  }

  byte[] ciphertext = cipher.encrypt(message.build());
  this.pushServiceSocket.sendProvisioningMessage(deviceIdentifier, ciphertext);
}
 
Example #19
Source Project: Smack   Author: igniterealtime   File: SignalOmemoService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected SignalOmemoRatchet instantiateOmemoRatchet(
        OmemoManager manager,
        OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
                SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> store) {

    return new SignalOmemoRatchet(manager, getOmemoStoreBackend());
}
 
Example #20
Source Project: libsignal-protocol-java   Author: signalapp   File: SessionState.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void setUnacknowledgedPreKeyMessage(Optional<Integer> preKeyId, int signedPreKeyId, ECPublicKey baseKey) {
  PendingPreKey.Builder pending = PendingPreKey.newBuilder()
                                               .setSignedPreKeyId(signedPreKeyId)
                                               .setBaseKey(ByteString.copyFrom(baseKey.serialize()));

  if (preKeyId.isPresent()) {
    pending.setPreKeyId(preKeyId.get());
  }

  this.sessionStructure = this.sessionStructure.toBuilder()
                                               .setPendingPreKey(pending.build())
                                               .build();
}
 
Example #21
Source Project: signal-cli   Author: AsamK   File: Manager.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
    IdentityKeyPair identityKeyPair = getIdentityKeyPair();
    String verificationCode = accountManager.getNewDeviceVerificationCode();

    accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, Optional.of(account.getProfileKey().serialize()), verificationCode);
    account.setMultiDevice(true);
    account.save();
}
 
Example #22
Source Project: signal-cli   Author: AsamK   File: Utils.java    License: GNU General Public License v3.0 5 votes vote down vote up
static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws IOException, InvalidKeyException {
    Map<String, String> query = getQueryMap(linkUri.getRawQuery());
    String deviceIdentifier = query.get("uuid");
    String publicKeyEncoded = query.get("pub_key");

    if (isEmpty(deviceIdentifier) || isEmpty(publicKeyEncoded)) {
        throw new RuntimeException("Invalid device link uri");
    }

    ECPublicKey deviceKey = Curve.decodePoint(Base64.decode(publicKeyEncoded), 0);

    return new DeviceLinkInfo(deviceIdentifier, deviceKey);
}
 
Example #23
Source Project: libsignal-protocol-java   Author: signalapp   File: SessionState.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void setReceiverChainKey(ECPublicKey senderEphemeral, ChainKey chainKey) {
  Pair<Chain,Integer> chainAndIndex = getReceiverChain(senderEphemeral);
  Chain               chain         = chainAndIndex.first();

  Chain.ChainKey chainKeyStructure = Chain.ChainKey.newBuilder()
                                                   .setKey(ByteString.copyFrom(chainKey.getKey()))
                                                   .setIndex(chainKey.getIndex())
                                                   .build();

  Chain updatedChain = chain.toBuilder().setChainKey(chainKeyStructure).build();

  this.sessionStructure = this.sessionStructure.toBuilder()
                                               .setReceiverChains(chainAndIndex.second(), updatedChain)
                                               .build();
}
 
Example #24
Source Project: Silence   Author: SilenceIM   File: MasterSecretUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static AsymmetricMasterSecret getAsymmetricMasterSecret(Context context,
                                                               MasterSecret masterSecret)
{
  try {
    byte[] djbPublicBytes   = retrieve(context, ASYMMETRIC_LOCAL_PUBLIC_DJB);
    byte[] djbPrivateBytes  = retrieve(context, ASYMMETRIC_LOCAL_PRIVATE_DJB);

    ECPublicKey  djbPublicKey  = null;
    ECPrivateKey djbPrivateKey = null;

    if (djbPublicBytes != null) {
      djbPublicKey = Curve.decodePoint(djbPublicBytes, 0);
    }

    if (masterSecret != null) {
      MasterCipher masterCipher = new MasterCipher(masterSecret);

      if (djbPrivateBytes != null) {
        djbPrivateKey = masterCipher.decryptKey(djbPrivateBytes);
      }
    }

    return new AsymmetricMasterSecret(djbPublicKey, djbPrivateKey);
  } catch (InvalidKeyException | IOException ike) {
    throw new AssertionError(ike);
  }
}
 
Example #25
Source Project: Smack   Author: igniterealtime   File: SignalOmemoRatchet.java    License: Apache License 2.0 5 votes vote down vote up
SignalOmemoRatchet(OmemoManager omemoManager,
                          OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord,
                                         SessionRecord, SignalProtocolAddress, ECPublicKey, PreKeyBundle,
                                         SessionCipher> store) {
    super(omemoManager, store);
    this.storeConnector = new SignalOmemoStoreConnector(omemoManager, store);
}
 
Example #26
Source Project: libsignal-protocol-java   Author: signalapp   File: SenderKeyRecord.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void addSenderKeyState(int id, int iteration, byte[] chainKey, ECPublicKey signatureKey) {
  senderKeyStates.addFirst(new SenderKeyState(id, iteration, chainKey, signatureKey));

  if (senderKeyStates.size() > MAX_STATES) {
    senderKeyStates.removeLast();
  }
}
 
Example #27
Source Project: Smack   Author: igniterealtime   File: SignalOmemoKeyUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ECPublicKey ellipticCurvePublicKeyFromBytes(byte[] data) throws CorruptedOmemoKeyException {
    if (data == null) return null;
    try {
        return Curve.decodePoint(data, 0);
    } catch (InvalidKeyException e) {
        throw new CorruptedOmemoKeyException(e);
    }
}
 
Example #28
public SenderKeyDistributionMessage(int id, int iteration, byte[] chainKey, ECPublicKey signatureKey) {
  byte[] version = {ByteUtil.intsToByteHighAndLow(CURRENT_VERSION, CURRENT_VERSION)};
  byte[] protobuf = SignalProtos.SenderKeyDistributionMessage.newBuilder()
                                                             .setId(id)
                                                             .setIteration(iteration)
                                                             .setChainKey(ByteString.copyFrom(chainKey))
                                                             .setSigningKey(ByteString.copyFrom(signatureKey.serialize()))
                                                             .build().toByteArray();

  this.id           = id;
  this.iteration    = iteration;
  this.chainKey     = chainKey;
  this.signatureKey = signatureKey;
  this.serialized   = ByteUtil.combine(version, protobuf);
}
 
Example #29
Source Project: libsignal-protocol-java   Author: signalapp   File: PreKeySignalMessage.java    License: GNU General Public License v3.0 5 votes vote down vote up
public PreKeySignalMessage(int messageVersion, int registrationId, Optional<Integer> preKeyId,
                           int signedPreKeyId, ECPublicKey baseKey, IdentityKey identityKey,
                           SignalMessage message)
{
  this.version        = messageVersion;
  this.registrationId = registrationId;
  this.preKeyId       = preKeyId;
  this.signedPreKeyId = signedPreKeyId;
  this.baseKey        = baseKey;
  this.identityKey    = identityKey;
  this.message        = message;

  SignalProtos.PreKeySignalMessage.Builder builder =
      SignalProtos.PreKeySignalMessage.newBuilder()
                                      .setSignedPreKeyId(signedPreKeyId)
                                      .setBaseKey(ByteString.copyFrom(baseKey.serialize()))
                                      .setIdentityKey(ByteString.copyFrom(identityKey.serialize()))
                                      .setMessage(ByteString.copyFrom(message.serialize()))
                                      .setRegistrationId(registrationId);

  if (preKeyId.isPresent()) {
    builder.setPreKeyId(preKeyId.get());
  }

  byte[] versionBytes = {ByteUtil.intsToByteHighAndLow(this.version, CURRENT_VERSION)};
  byte[] messageBytes = builder.build().toByteArray();

  this.serialized = ByteUtil.combine(versionBytes, messageBytes);
}
 
Example #30
Source Project: libsignal-protocol-java   Author: signalapp   File: SignedPreKeyRecord.java    License: GNU General Public License v3.0 5 votes vote down vote up
public ECKeyPair getKeyPair() {
  try {
    ECPublicKey publicKey = Curve.decodePoint(this.structure.getPublicKey().toByteArray(), 0);
    ECPrivateKey privateKey = Curve.decodePrivatePoint(this.structure.getPrivateKey().toByteArray());

    return new ECKeyPair(publicKey, privateKey);
  } catch (InvalidKeyException e) {
    throw new AssertionError(e);
  }
}