Java Code Examples for org.whispersystems.libsignal.ecc.Curve#decodePrivatePoint()

The following examples show how to use org.whispersystems.libsignal.ecc.Curve#decodePrivatePoint() . 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: SignedPreKeyDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public @Nullable SignedPreKeyRecord getSignedPreKey(int keyId) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();

  try (Cursor cursor = database.query(TABLE_NAME, null, KEY_ID + " = ?",
                                      new String[] {String.valueOf(keyId)},
                                      null, null, null))
  {
    if (cursor != null && cursor.moveToFirst()) {
      try {
        ECPublicKey  publicKey  = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
        byte[]       signature  = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(SIGNATURE)));
        long         timestamp  = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));

        return new SignedPreKeyRecord(keyId, timestamp, new ECKeyPair(publicKey, privateKey), signature);
      } catch (InvalidKeyException | IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return null;
}
 
Example 2
Source File: SignedPreKeyDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public @NonNull List<SignedPreKeyRecord> getAllSignedPreKeys() {
  SQLiteDatabase           database = databaseHelper.getReadableDatabase();
  List<SignedPreKeyRecord> results  = new LinkedList<>();

  try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      try {
        int          keyId      = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_ID));
        ECPublicKey  publicKey  = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
        byte[]       signature  = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(SIGNATURE)));
        long         timestamp  = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));

        results.add(new SignedPreKeyRecord(keyId, timestamp, new ECKeyPair(publicKey, privateKey), signature));
      } catch (InvalidKeyException | IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return results;
}
 
Example 3
Source File: OneTimePreKeyDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public @Nullable PreKeyRecord getPreKey(int keyId) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();

  try (Cursor cursor = database.query(TABLE_NAME, null, KEY_ID + " = ?",
                                      new String[] {String.valueOf(keyId)},
                                      null, null, null))
  {
    if (cursor != null && cursor.moveToFirst()) {
      try {
        ECPublicKey  publicKey  = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));

        return new PreKeyRecord(keyId, new ECKeyPair(publicKey, privateKey));
      } catch (InvalidKeyException | IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return null;
}
 
Example 4
Source File: SessionState.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public IdentityKeyPair getPendingKeyExchangeIdentityKey() throws InvalidKeyException {
  IdentityKey publicKey = new IdentityKey(sessionStructure.getPendingKeyExchange()
                                                          .getLocalIdentityKey().toByteArray(), 0);

  ECPrivateKey privateKey = Curve.decodePrivatePoint(sessionStructure.getPendingKeyExchange()
                                                                     .getLocalIdentityKeyPrivate()
                                                                     .toByteArray());

  return new IdentityKeyPair(publicKey, privateKey);
}
 
Example 5
Source File: SessionState.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public ECKeyPair getPendingKeyExchangeRatchetKey() throws InvalidKeyException {
  ECPublicKey publicKey   = Curve.decodePoint(sessionStructure.getPendingKeyExchange()
                                                              .getLocalRatchetKey().toByteArray(), 0);

  ECPrivateKey privateKey = Curve.decodePrivatePoint(sessionStructure.getPendingKeyExchange()
                                                                     .getLocalRatchetKeyPrivate()
                                                                     .toByteArray());

  return new ECKeyPair(publicKey, privateKey);
}
 
Example 6
Source File: SessionState.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public ECKeyPair getPendingKeyExchangeBaseKey() throws InvalidKeyException {
  ECPublicKey publicKey   = Curve.decodePoint(sessionStructure.getPendingKeyExchange()
                                                              .getLocalBaseKey().toByteArray(), 0);

  ECPrivateKey privateKey = Curve.decodePrivatePoint(sessionStructure.getPendingKeyExchange()
                                                                     .getLocalBaseKeyPrivate()
                                                                     .toByteArray());

  return new ECKeyPair(publicKey, privateKey);
}
 
Example 7
Source File: SessionState.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public ECKeyPair getSenderRatchetKeyPair() {
  ECPublicKey  publicKey  = getSenderRatchetKey();
  ECPrivateKey privateKey = Curve.decodePrivatePoint(sessionStructure.getSenderChain()
                                                                     .getSenderRatchetKeyPrivate()
                                                                     .toByteArray());

  return new ECKeyPair(publicKey, privateKey);
}
 
Example 8
Source File: PreKeyRecord.java    From libsignal-protocol-java with 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);
  }
}
 
Example 9
Source File: SignedPreKeyRecord.java    From libsignal-protocol-java with 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);
  }
}
 
Example 10
Source File: IdentityKeyPair.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public IdentityKeyPair(byte[] serialized) throws InvalidKeyException {
  try {
    IdentityKeyPairStructure structure = IdentityKeyPairStructure.parseFrom(serialized);
    this.publicKey  = new IdentityKey(structure.getPublicKey().toByteArray(), 0);
    this.privateKey = Curve.decodePrivatePoint(structure.getPrivateKey().toByteArray());
  } catch (InvalidProtocolBufferException e) {
    throw new InvalidKeyException(e);
  }
}
 
Example 11
Source File: MasterCipher.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public ECPrivateKey decryptKey(byte[] key)
    throws org.whispersystems.libsignal.InvalidKeyException
{
  try {
    return Curve.decodePrivatePoint(decryptBytes(key));
  } catch (InvalidMessageException ime) {
    throw new org.whispersystems.libsignal.InvalidKeyException(ime);
  }
}
 
Example 12
Source File: MasterCipher.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public ECPrivateKey decryptKey(byte[] key)
    throws org.whispersystems.libsignal.InvalidKeyException
{
  try {
    return Curve.decodePrivatePoint(decryptBytes(key));
  } catch (InvalidMessageException ime) {
    throw new org.whispersystems.libsignal.InvalidKeyException(ime);
  }
}
 
Example 13
Source File: IdentityKeyUtil.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static ECKeyPair rebuildIdentityKeys(@NonNull AccountContext accountContext, byte[] privateKeyBytes) throws Exception {

        ECPrivateKey ecPrivateKey = Curve.decodePrivatePoint(privateKeyBytes);
        byte[] publicKeyBytes = ByteUtil.combine(new byte[]{Curve.DJB_TYPE}, BCMPrivateKeyUtils.INSTANCE.generatePublicKey(ecPrivateKey.serialize()));
        save(accountContext, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(publicKeyBytes));
        save(accountContext, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(ecPrivateKey.serialize()));

        return new ECKeyPair(Curve.decodePoint(publicKeyBytes, 0), ecPrivateKey);
    }
 
Example 14
Source File: IdentityKeyUtil.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static @NonNull
IdentityKeyPair getIdentityKeyPair(@NonNull AccountContext accountContext) {
    if (!hasIdentityKey(accountContext)) {
        throw new IllegalStateException("There isn't one!");
    }

    try {
        IdentityKey publicKey = getIdentityKey(accountContext);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(retrieve(accountContext, IDENTITY_PRIVATE_KEY_PREF)));

        return new IdentityKeyPair(publicKey, privateKey);
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
 
Example 15
Source File: MasterCipher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public ECPrivateKey decryptPrivateKey(byte[] key) throws InvalidKeyException {
  try {
    return Curve.decodePrivatePoint(decrypt(key, "ECPrivateKey".getBytes()));
  } catch (GeneralSecurityException ge) {
    throw new InvalidKeyException(ge);
  }
}
 
Example 16
Source File: IdentityKeyUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static @NonNull IdentityKeyPair getIdentityKeyPair(@NonNull Context context) {
  if (!hasIdentityKey(context)) throw new AssertionError("There isn't one!");

  try {
    IdentityKey  publicKey  = getIdentityKey(context);
    ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_PREF)));

    return new IdentityKeyPair(publicKey, privateKey);
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}
 
Example 17
Source File: RootKeyTest.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public void testRootKeyDerivationV2() throws NoSuchAlgorithmException, InvalidKeyException {
  byte[] rootKeySeed  = {(byte) 0x7b, (byte) 0xa6, (byte) 0xde, (byte) 0xbc, (byte) 0x2b,
                         (byte) 0xc1, (byte) 0xbb, (byte) 0xf9, (byte) 0x1a, (byte) 0xbb,
                         (byte) 0xc1, (byte) 0x36, (byte) 0x74, (byte) 0x04, (byte) 0x17,
                         (byte) 0x6c, (byte) 0xa6, (byte) 0x23, (byte) 0x09, (byte) 0x5b,
                         (byte) 0x7e, (byte) 0xc6, (byte) 0x6b, (byte) 0x45, (byte) 0xf6,
                         (byte) 0x02, (byte) 0xd9, (byte) 0x35, (byte) 0x38, (byte) 0x94,
                         (byte) 0x2d, (byte) 0xcc};

  byte[] alicePublic  = {(byte) 0x05, (byte) 0xee, (byte) 0x4f, (byte) 0xa6, (byte) 0xcd,
                         (byte) 0xc0, (byte) 0x30, (byte) 0xdf, (byte) 0x49, (byte) 0xec,
                         (byte) 0xd0, (byte) 0xba, (byte) 0x6c, (byte) 0xfc, (byte) 0xff,
                         (byte) 0xb2, (byte) 0x33, (byte) 0xd3, (byte) 0x65, (byte) 0xa2,
                         (byte) 0x7f, (byte) 0xad, (byte) 0xbe, (byte) 0xff, (byte) 0x77,
                         (byte) 0xe9, (byte) 0x63, (byte) 0xfc, (byte) 0xb1, (byte) 0x62,
                         (byte) 0x22, (byte) 0xe1, (byte) 0x3a};

  byte[] alicePrivate = {(byte) 0x21, (byte) 0x68, (byte) 0x22, (byte) 0xec, (byte) 0x67,
                         (byte) 0xeb, (byte) 0x38, (byte) 0x04, (byte) 0x9e, (byte) 0xba,
                         (byte) 0xe7, (byte) 0xb9, (byte) 0x39, (byte) 0xba, (byte) 0xea,
                         (byte) 0xeb, (byte) 0xb1, (byte) 0x51, (byte) 0xbb, (byte) 0xb3,
                         (byte) 0x2d, (byte) 0xb8, (byte) 0x0f, (byte) 0xd3, (byte) 0x89,
                         (byte) 0x24, (byte) 0x5a, (byte) 0xc3, (byte) 0x7a, (byte) 0x94,
                         (byte) 0x8e, (byte) 0x50};

  byte[] bobPublic    = {(byte) 0x05, (byte) 0xab, (byte) 0xb8, (byte) 0xeb, (byte) 0x29,
                         (byte) 0xcc, (byte) 0x80, (byte) 0xb4, (byte) 0x71, (byte) 0x09,
                         (byte) 0xa2, (byte) 0x26, (byte) 0x5a, (byte) 0xbe, (byte) 0x97,
                         (byte) 0x98, (byte) 0x48, (byte) 0x54, (byte) 0x06, (byte) 0xe3,
                         (byte) 0x2d, (byte) 0xa2, (byte) 0x68, (byte) 0x93, (byte) 0x4a,
                         (byte) 0x95, (byte) 0x55, (byte) 0xe8, (byte) 0x47, (byte) 0x57,
                         (byte) 0x70, (byte) 0x8a, (byte) 0x30};

  byte[] nextRoot     = {(byte) 0xb1, (byte) 0x14, (byte) 0xf5, (byte) 0xde, (byte) 0x28,
                         (byte) 0x01, (byte) 0x19, (byte) 0x85, (byte) 0xe6, (byte) 0xeb,
                         (byte) 0xa2, (byte) 0x5d, (byte) 0x50, (byte) 0xe7, (byte) 0xec,
                         (byte) 0x41, (byte) 0xa9, (byte) 0xb0, (byte) 0x2f, (byte) 0x56,
                         (byte) 0x93, (byte) 0xc5, (byte) 0xc7, (byte) 0x88, (byte) 0xa6,
                         (byte) 0x3a, (byte) 0x06, (byte) 0xd2, (byte) 0x12, (byte) 0xa2,
                         (byte) 0xf7, (byte) 0x31};

  byte[] nextChain    = {(byte) 0x9d, (byte) 0x7d, (byte) 0x24, (byte) 0x69, (byte) 0xbc,
                         (byte) 0x9a, (byte) 0xe5, (byte) 0x3e, (byte) 0xe9, (byte) 0x80,
                         (byte) 0x5a, (byte) 0xa3, (byte) 0x26, (byte) 0x4d, (byte) 0x24,
                         (byte) 0x99, (byte) 0xa3, (byte) 0xac, (byte) 0xe8, (byte) 0x0f,
                         (byte) 0x4c, (byte) 0xca, (byte) 0xe2, (byte) 0xda, (byte) 0x13,
                         (byte) 0x43, (byte) 0x0c, (byte) 0x5c, (byte) 0x55, (byte) 0xb5,
                         (byte) 0xca, (byte) 0x5f};

  ECPublicKey  alicePublicKey  = Curve.decodePoint(alicePublic, 0);
  ECPrivateKey alicePrivateKey = Curve.decodePrivatePoint(alicePrivate);
  ECKeyPair    aliceKeyPair    = new ECKeyPair(alicePublicKey, alicePrivateKey);

  ECPublicKey bobPublicKey = Curve.decodePoint(bobPublic, 0);
  RootKey     rootKey      = new RootKey(HKDF.createFor(2), rootKeySeed);

  Pair<RootKey, ChainKey> rootKeyChainKeyPair = rootKey.createChain(bobPublicKey, aliceKeyPair);
  RootKey                 nextRootKey         = rootKeyChainKeyPair.first();
  ChainKey                nextChainKey        = rootKeyChainKeyPair.second();

  assertTrue(Arrays.equals(rootKey.getKeyBytes(), rootKeySeed));
  assertTrue(Arrays.equals(nextRootKey.getKeyBytes(), nextRoot));
  assertTrue(Arrays.equals(nextChainKey.getKey(), nextChain));
}
 
Example 18
Source File: SenderKeyState.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public ECPrivateKey getSigningKeyPrivate() {
  return Curve.decodePrivatePoint(senderKeyStateStructure.getSenderSigningKey()
                                                         .getPrivate().toByteArray());
}