org.whispersystems.libsignal.util.Medium Java Examples

The following examples show how to use org.whispersystems.libsignal.util.Medium. 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
Source File: PreKeyUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public synchronized static SignedPreKeyRecord generateSignedPreKey(Context context, IdentityKeyPair identityKeyPair, boolean active) {
  try {
    SignedPreKeyStore  signedPreKeyStore = new TextSecurePreKeyStore(context);
    int                signedPreKeyId    = TextSecurePreferences.getNextSignedPreKeyId(context);
    ECKeyPair          keyPair           = Curve.generateKeyPair();
    byte[]             signature         = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
    SignedPreKeyRecord record            = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);

    signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
    TextSecurePreferences.setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);

    if (active) {
      TextSecurePreferences.setActiveSignedPreKeyId(context, signedPreKeyId);
    }

    return record;
  } catch (InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
Example #2
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
private PreKeyBundle createBobPreKeyBundle(SignalProtocolStore bobStore) throws InvalidKeyException {
  ECKeyPair bobUnsignedPreKey   = Curve.generateKeyPair();
  int       bobUnsignedPreKeyId = new Random().nextInt(Medium.MAX_VALUE);
  byte[]    bobSignature        = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                           bobSignedPreKey.getPublicKey().serialize());

  PreKeyBundle bobPreKeyBundle = new PreKeyBundle(1, 1,
                                                  bobUnsignedPreKeyId, bobUnsignedPreKey.getPublicKey(),
                                                  bobSignedPreKeyId, bobSignedPreKey.getPublicKey(),
                                                  bobSignature, bobStore.getIdentityKeyPair().getPublicKey());

  bobStore.storeSignedPreKey(bobSignedPreKeyId, new SignedPreKeyRecord(bobSignedPreKeyId, System.currentTimeMillis(), bobSignedPreKey, bobSignature));
  bobStore.storePreKey(bobUnsignedPreKeyId, new PreKeyRecord(bobUnsignedPreKeyId, bobUnsignedPreKey));

  return bobPreKeyBundle;
}
 
Example #3
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
private PreKeyBundle createAlicePreKeyBundle(SignalProtocolStore aliceStore) throws InvalidKeyException {
  ECKeyPair aliceUnsignedPreKey   = Curve.generateKeyPair();
  int       aliceUnsignedPreKeyId = new Random().nextInt(Medium.MAX_VALUE);
  byte[]    aliceSignature        = Curve.calculateSignature(aliceStore.getIdentityKeyPair().getPrivateKey(),
                                                             aliceSignedPreKey.getPublicKey().serialize());

  PreKeyBundle alicePreKeyBundle = new PreKeyBundle(1, 1,
                                                    aliceUnsignedPreKeyId, aliceUnsignedPreKey.getPublicKey(),
                                                    aliceSignedPreKeyId, aliceSignedPreKey.getPublicKey(),
                                                    aliceSignature, aliceStore.getIdentityKeyPair().getPublicKey());

  aliceStore.storeSignedPreKey(aliceSignedPreKeyId, new SignedPreKeyRecord(aliceSignedPreKeyId, System.currentTimeMillis(), aliceSignedPreKey, aliceSignature));
  aliceStore.storePreKey(aliceUnsignedPreKeyId, new PreKeyRecord(aliceUnsignedPreKeyId, aliceUnsignedPreKey));

  return alicePreKeyBundle;
}
 
Example #4
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
private List<PreKeyRecord> generatePreKeys() {
    List<PreKeyRecord> records = new ArrayList<>(ServiceConfig.PREKEY_BATCH_SIZE);

    final int offset = account.getPreKeyIdOffset();
    for (int i = 0; i < ServiceConfig.PREKEY_BATCH_SIZE; i++) {
        int preKeyId = (offset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);

        records.add(record);
    }

    account.addPreKeys(records);
    account.save();

    return records;
}
 
Example #5
Source File: Manager.java    From signald with GNU General Public License v3.0 6 votes vote down vote up
private List<PreKeyRecord> generatePreKeys() throws IOException {
    List<PreKeyRecord> records = new LinkedList<>();

    for (int i = 0; i < PREKEY_BATCH_SIZE; i++) {
        int preKeyId = (accountData.preKeyIdOffset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);

        accountData.axolotlStore.preKeys.storePreKey(preKeyId, record);
        records.add(record);
    }

    accountData.preKeyIdOffset = (accountData.preKeyIdOffset + PREKEY_BATCH_SIZE + 1) % Medium.MAX_VALUE;
    accountData.save();

    return records;
}
 
Example #6
Source File: PreKeyUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private static synchronized int getNextSignedPreKeyId(Context context, AccountContext accountContext) {
    try {
        File nextFile = new File(getSignedPreKeysDirectory(context, accountContext), SignedPreKeyIndex.FILE_NAME);

        if (!nextFile.exists()) {
            return EncryptUtils.getSecureRandom().nextInt(Medium.MAX_VALUE);
        } else {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
            SignedPreKeyIndex index = GsonUtils.INSTANCE.fromJson(reader, SignedPreKeyIndex.class);
            reader.close();
            return index.nextSignedPreKeyId;
        }
    } catch (IOException e) {
        Log.w("PreKeyUtil", e);
        return EncryptUtils.getSecureRandom().nextInt(Medium.MAX_VALUE);
    }
}
 
Example #7
Source File: PreKeyUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private static synchronized int getNextPreKeyId(Context context, AccountContext accountContext) {
    try {
        File nextFile = new File(getPreKeysDirectory(context, accountContext), PreKeyIndex.FILE_NAME);

        if (!nextFile.exists()) {
            return EncryptUtils.getSecureRandom().nextInt(Medium.MAX_VALUE);
        } else {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
            PreKeyIndex index = GsonUtils.INSTANCE.fromJson(reader, PreKeyIndex.class);
            reader.close();
            return index.nextPreKeyId;
        }
    } catch (IOException e) {
        Log.w("PreKeyUtil", e);
        return EncryptUtils.getSecureRandom().nextInt(Medium.MAX_VALUE);
    }
}
 
Example #8
Source File: PreKeyUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public static SignedPreKeyRecord generateSignedPreKey(Context context, AccountContext accountContext, IdentityKeyPair identityKeyPair, boolean active) {
    try {
        SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context, accountContext);
        int signedPreKeyId = getNextSignedPreKeyId(context, accountContext);
        ECKeyPair keyPair = Curve.generateKeyPair();
        byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
        SignedPreKeyRecord record = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);

        signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
        setNextSignedPreKeyId(context, accountContext,(signedPreKeyId + 1) % Medium.MAX_VALUE);

        if (active) {
            setActiveSignedPreKeyId(context, accountContext, signedPreKeyId);
        }

        return record;
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
 
Example #9
Source File: PreKeyUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public static List<PreKeyRecord> generatePreKeys(Context context, AccountContext accountContext) {
    PreKeyStore preKeyStore = new TextSecurePreKeyStore(context, accountContext);
    List<PreKeyRecord> records = new LinkedList<>();
    int preKeyIdOffset = getNextPreKeyId(context, accountContext);

    for (int i = 0; i < BATCH_SIZE; i++) {
        int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);

        preKeyStore.storePreKey(preKeyId, record);
        records.add(record);
    }

    setNextPreKeyId(context, accountContext,(preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
    return records;
}
 
Example #10
Source File: PreKeyUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public synchronized static List<PreKeyRecord> generatePreKeys(Context context) {
  PreKeyStore        preKeyStore    = new TextSecurePreKeyStore(context);
  List<PreKeyRecord> records        = new LinkedList<>();
  int                preKeyIdOffset = TextSecurePreferences.getNextPreKeyId(context);

  for (int i=0;i<BATCH_SIZE;i++) {
    int          preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
    ECKeyPair    keyPair  = Curve.generateKeyPair();
    PreKeyRecord record   = new PreKeyRecord(preKeyId, keyPair);

    preKeyStore.storePreKey(preKeyId, record);
    records.add(record);
  }

  TextSecurePreferences.setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);

  return records;
}
 
Example #11
Source File: SignalBot.java    From signal-bot with GNU General Public License v3.0 5 votes vote down vote up
private void refreshPreKeys(IdentityKeyPair identityKeyPair) throws IOException, InvalidKeyException {
    int initialPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
    List<PreKeyRecord> records = KeyHelper.generatePreKeys(initialPreKeyId, BATCH_SIZE);
    records.forEach((v) -> this.protocolStore.storePreKey(v.getId(), v));
    int signedPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
    SignedPreKeyRecord signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair, signedPreKeyId);
    this.protocolStore.storeSignedPreKey(signedPreKey.getId(), signedPreKey);
    this.accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKey, records);
}
 
Example #12
Source File: Manager.java    From signald with GNU General Public License v3.0 5 votes vote down vote up
private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) throws IOException {
    try {
        ECKeyPair keyPair = Curve.generateKeyPair();
        byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
        SignedPreKeyRecord record = new SignedPreKeyRecord(accountData.nextSignedPreKeyId, System.currentTimeMillis(), keyPair, signature);

        accountData.axolotlStore.storeSignedPreKey(accountData.nextSignedPreKeyId, record);
        accountData.nextSignedPreKeyId = (accountData.nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
        accountData.save();

        return record;
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
 
Example #13
Source File: SessionBuilder.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private Optional<Integer> processV3(SessionRecord sessionRecord, PreKeySignalMessage message)
    throws UntrustedIdentityException, InvalidKeyIdException, InvalidKeyException
{

  if (sessionRecord.hasSessionState(message.getMessageVersion(), message.getBaseKey().serialize())) {
    Log.w(TAG, "We've already setup a session for this V3 message, letting bundled message fall through...");
    return Optional.absent();
  }

  ECKeyPair ourSignedPreKey = signedPreKeyStore.loadSignedPreKey(message.getSignedPreKeyId()).getKeyPair();

  BobSignalProtocolParameters.Builder parameters = BobSignalProtocolParameters.newBuilder();

  parameters.setTheirBaseKey(message.getBaseKey())
            .setTheirIdentityKey(message.getIdentityKey())
            .setOurIdentityKey(identityKeyStore.getIdentityKeyPair())
            .setOurSignedPreKey(ourSignedPreKey)
            .setOurRatchetKey(ourSignedPreKey);

  if (message.getPreKeyId().isPresent()) {
    parameters.setOurOneTimePreKey(Optional.of(preKeyStore.loadPreKey(message.getPreKeyId().get()).getKeyPair()));
  } else {
    parameters.setOurOneTimePreKey(Optional.<ECKeyPair>absent());
  }

  if (!sessionRecord.isFresh()) sessionRecord.archiveCurrentState();

  RatchetingSession.initializeSession(sessionRecord.getSessionState(), parameters.create());

  sessionRecord.getSessionState().setLocalRegistrationId(identityKeyStore.getLocalRegistrationId());
  sessionRecord.getSessionState().setRemoteRegistrationId(message.getRegistrationId());
  sessionRecord.getSessionState().setAliceBaseKey(message.getBaseKey().serialize());

  if (message.getPreKeyId().isPresent() && message.getPreKeyId().get() != Medium.MAX_VALUE) {
    return message.getPreKeyId();
  } else {
    return Optional.absent();
  }
}
 
Example #14
Source File: TextSecurePreferences.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static int getNextSignedPreKeyId(@NonNull Context context) {
  return getIntegerPreference(context, NEXT_SIGNED_PRE_KEY_ID, new SecureRandom().nextInt(Medium.MAX_VALUE));
}
 
Example #15
Source File: SignalAccount.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
public void addPreKeys(Collection<PreKeyRecord> records) {
    for (PreKeyRecord record : records) {
        signalProtocolStore.storePreKey(record.getId(), record);
    }
    preKeyIdOffset = (preKeyIdOffset + records.size()) % Medium.MAX_VALUE;
}
 
Example #16
Source File: SignalAccount.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
public void addSignedPreKey(SignedPreKeyRecord record) {
    signalProtocolStore.storeSignedPreKey(record.getId(), record);
    nextSignedPreKeyId = (nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
}
 
Example #17
Source File: TextSecurePreferences.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static int getNextPreKeyId(@NonNull Context context) {
  return getIntegerPreference(context, NEXT_PRE_KEY_ID, new SecureRandom().nextInt(Medium.MAX_VALUE));
}