private PreKeyBundle createBobPreKeyBundle(AxolotlStore 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;
  }
Пример #2
0
  public static AsymmetricMasterSecret generateAsymmetricMasterSecret(
      Context context, MasterSecret masterSecret) {
    MasterCipher masterCipher = new MasterCipher(masterSecret);
    ECKeyPair keyPair = Curve.generateKeyPair();

    save(context, ASYMMETRIC_LOCAL_PUBLIC_DJB, keyPair.getPublicKey().serialize());
    save(context, ASYMMETRIC_LOCAL_PRIVATE_DJB, masterCipher.encryptKey(keyPair.getPrivateKey()));

    return new AsymmetricMasterSecret(keyPair.getPublicKey(), keyPair.getPrivateKey());
  }
  private void initializeSessionsV3(SessionState aliceSessionState, SessionState bobSessionState)
      throws InvalidKeyException {
    ECKeyPair aliceIdentityKeyPair = Curve.generateKeyPair();
    IdentityKeyPair aliceIdentityKey =
        new IdentityKeyPair(
            new IdentityKey(aliceIdentityKeyPair.getPublicKey()),
            aliceIdentityKeyPair.getPrivateKey());
    ECKeyPair aliceBaseKey = Curve.generateKeyPair();
    ECKeyPair aliceEphemeralKey = Curve.generateKeyPair();

    ECKeyPair alicePreKey = aliceBaseKey;

    ECKeyPair bobIdentityKeyPair = Curve.generateKeyPair();
    IdentityKeyPair bobIdentityKey =
        new IdentityKeyPair(
            new IdentityKey(bobIdentityKeyPair.getPublicKey()), bobIdentityKeyPair.getPrivateKey());
    ECKeyPair bobBaseKey = Curve.generateKeyPair();
    ECKeyPair bobEphemeralKey = bobBaseKey;

    ECKeyPair bobPreKey = Curve.generateKeyPair();

    AliceAxolotlParameters aliceParameters =
        AliceAxolotlParameters.newBuilder()
            .setOurBaseKey(aliceBaseKey)
            .setOurIdentityKey(aliceIdentityKey)
            .setTheirOneTimePreKey(Optional.<ECPublicKey>absent())
            .setTheirRatchetKey(bobEphemeralKey.getPublicKey())
            .setTheirSignedPreKey(bobBaseKey.getPublicKey())
            .setTheirIdentityKey(bobIdentityKey.getPublicKey())
            .create();

    BobAxolotlParameters bobParameters =
        BobAxolotlParameters.newBuilder()
            .setOurRatchetKey(bobEphemeralKey)
            .setOurSignedPreKey(bobBaseKey)
            .setOurOneTimePreKey(Optional.<ECKeyPair>absent())
            .setOurIdentityKey(bobIdentityKey)
            .setTheirIdentityKey(aliceIdentityKey.getPublicKey())
            .setTheirBaseKey(aliceBaseKey.getPublicKey())
            .create();

    RatchetingSession.initializeSession(aliceSessionState, 3, aliceParameters);
    RatchetingSession.initializeSession(bobSessionState, 3, bobParameters);
  }
  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);
    }
  }
Пример #5
0
  public static SignedPreKeyRecord generateSignedPreKey(
      Context context, MasterSecret masterSecret, IdentityKeyPair identityKeyPair) {
    try {
      SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context, masterSecret);
      int signedPreKeyId = 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);
      setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);

      return record;
    } catch (InvalidKeyException e) {
      throw new AssertionError(e);
    }
  }