Java Code Examples for org.whispersystems.signalservice.internal.util.Util#getSecretBytes()

The following examples show how to use org.whispersystems.signalservice.internal.util.Util#getSecretBytes() . 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: ContactDiscoveryCipher.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public DiscoveryRequest createDiscoveryRequest(List<String> addressBook, RemoteAttestation remoteAttestation) {
  try {
    ByteArrayOutputStream requestDataStream = new ByteArrayOutputStream();

    for (String address : addressBook) {
      requestDataStream.write(ByteUtil.longToByteArray(Long.parseLong(address)));
    }

    byte[]         requestData = requestDataStream.toByteArray();
    byte[]         nonce       = Util.getSecretBytes(12);
    Cipher         cipher      = Cipher.getInstance("AES/GCM/NoPadding");

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(remoteAttestation.getKeys().getClientKey(), "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, nonce));
    cipher.updateAAD(remoteAttestation.getRequestId());

    byte[]   cipherText = cipher.doFinal(requestData);
    byte[][] parts      = ByteUtil.split(cipherText, cipherText.length - TAG_LENGTH_BYTES, TAG_LENGTH_BYTES);

    return new DiscoveryRequest(addressBook.size(), remoteAttestation.getRequestId(), nonce, parts[0], parts[1]);
  } catch (IOException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
    throw new AssertionError(e);
  }
}
 
Example 2
Source File: AttachmentCipherTest.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void test_attachment_decryptFailOnBadKey() throws IOException{
  File    cipherFile          = null;
  boolean hitCorrectException = false;

  try {
    byte[]        key             = Util.getSecretBytes(64);
    byte[]        plaintextInput  = "Gwen Stacy".getBytes();
    EncryptResult encryptResult   = encryptData(plaintextInput, key);
    byte[]        badKey          = new byte[64];

    cipherFile = writeToFile(encryptResult.ciphertext);

    AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, badKey, encryptResult.digest);
  } catch (InvalidMessageException e) {
    hitCorrectException = true;
  } finally {
    if (cipherFile != null) {
      cipherFile.delete();
    }
  }

  assertTrue(hitCorrectException);
}
 
Example 3
Source File: AttachmentCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public void test_attachment_decryptFailOnBadKey() throws IOException{
  File    cipherFile          = null;
  boolean hitCorrectException = false;

  try {
    byte[]        key             = Util.getSecretBytes(64);
    byte[]        plaintextInput  = "Gwen Stacy".getBytes();
    EncryptResult encryptResult   = encryptData(plaintextInput, key);
    byte[]        badKey          = new byte[64];

    cipherFile = writeToFile(encryptResult.ciphertext);

    AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, badKey, encryptResult.digest);
  } catch (InvalidMessageException e) {
    hitCorrectException = true;
  } finally {
    if (cipherFile != null) {
      cipherFile.delete();
    }
  }

  assertTrue(hitCorrectException);
}
 
Example 4
Source File: AttachmentCipherTest.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void test_attachment_decryptFailOnBadMac() throws IOException {
  File    cipherFile          = null;
  boolean hitCorrectException = false;

  try {
    byte[]        key              = Util.getSecretBytes(64);
    byte[]        plaintextInput   = "Uncle Ben".getBytes();
    EncryptResult encryptResult    = encryptData(plaintextInput, key);
    byte[]        badMacCiphertext = Arrays.copyOf(encryptResult.ciphertext, encryptResult.ciphertext.length);

    badMacCiphertext[badMacCiphertext.length - 1] += 1;

    cipherFile = writeToFile(badMacCiphertext);

    AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, key, encryptResult.digest);
  } catch (InvalidMessageException e) {
    hitCorrectException = true;
  } finally {
    if (cipherFile != null) {
      cipherFile.delete();
    }
  }

  assertTrue(hitCorrectException);
}
 
Example 5
Source File: ProfileCipher.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public byte[] encryptName(byte[] input, int paddedLength) {
  try {
    byte[] inputPadded = new byte[paddedLength];

    if (input.length > inputPadded.length) {
      throw new IllegalArgumentException("Input is too long: " + new String(input));
    }

    System.arraycopy(input, 0, inputPadded, 0, input.length);

    byte[] nonce = Util.getSecretBytes(12);

    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, nonce));

    return ByteUtil.combine(nonce, cipher.doFinal(inputPadded));
  } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
Example 6
Source File: AttachmentCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public void test_sticker_decryptFailOnBadKey() throws IOException{
  boolean hitCorrectException = false;

  try {
    byte[]        packKey         = Util.getSecretBytes(32);
    byte[]        plaintextInput  = "Gwen Stacy".getBytes();
    EncryptResult encryptResult   = encryptData(plaintextInput, expandPackKey(packKey));
    byte[]        badPackKey      = new byte[32];

    AttachmentCipherInputStream.createForStickerData(encryptResult.ciphertext, badPackKey);
  } catch (InvalidMessageException e) {
    hitCorrectException = true;
  }

  assertTrue(hitCorrectException);
}
 
Example 7
Source File: ProfileCipher.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public byte[] encryptName(byte[] input, int paddedLength) {
  try {
    byte[] inputPadded = new byte[paddedLength];

    if (input.length > inputPadded.length) {
      throw new IllegalArgumentException("Input is too long: " + new String(input));
    }

    System.arraycopy(input, 0, inputPadded, 0, input.length);

    byte[] nonce = Util.getSecretBytes(12);

    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(128, nonce));

    return ByteUtil.combine(nonce, cipher.doFinal(inputPadded));
  } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
Example 8
Source File: AttachmentCipherTest.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void test_sticker_encryptDecrypt() throws IOException, InvalidMessageException {
  byte[]        packKey         = Util.getSecretBytes(32);
  byte[]        plaintextInput  = "Peter Parker".getBytes();
  EncryptResult encryptResult   = encryptData(plaintextInput, expandPackKey(packKey));
  InputStream   inputStream     = AttachmentCipherInputStream.createForStickerData(encryptResult.ciphertext, packKey);
  byte[]        plaintextOutput = readInputStreamFully(inputStream);

  assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
}
 
Example 9
Source File: SignalServiceAttachmentStream.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public SignalServiceAttachmentStream(InputStream inputStream, String contentType, long length, Optional<String> fileName, boolean voiceNote, Optional<byte[]> preview, String index,ProgressListener listener) {
  super(contentType, index);
  this.inputStream = inputStream;
  this.length      = length;
  this.fileName    = fileName;
  this.listener    = listener;
  this.voiceNote   = voiceNote;
  this.preview     = preview;
  this.attachmentKey    = Util.getSecretBytes(64);
}
 
Example 10
Source File: AttachmentCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void test_sticker_encryptDecryptEmpty() throws IOException, InvalidMessageException {
  byte[]        packKey         = Util.getSecretBytes(32);
  byte[]        plaintextInput  = "".getBytes();
  EncryptResult encryptResult   = encryptData(plaintextInput, expandPackKey(packKey));
  InputStream   inputStream     = AttachmentCipherInputStream.createForStickerData(encryptResult.ciphertext, packKey);
  byte[]        plaintextOutput = readInputStreamFully(inputStream);

  assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
}
 
Example 11
Source File: SignalStorageCipherTest.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void symmetry() throws InvalidKeyException {
  StorageItemKey key  = new StorageItemKey(Util.getSecretBytes(32));
  byte[]         data = Util.getSecretBytes(1337);

  byte[] ciphertext = SignalStorageCipher.encrypt(key, data);
  byte[] plaintext  = SignalStorageCipher.decrypt(key, ciphertext);

  assertArrayEquals(data, plaintext);
}
 
Example 12
Source File: ProfileCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void testEmpty() throws Exception {
  byte[]        key       = Util.getSecretBytes(32);
  ProfileCipher cipher    = new ProfileCipher(key);
  byte[]        name      = cipher.encryptName("".getBytes(), 26);
  byte[]        plaintext = cipher.decryptName(name);

  assertEquals(plaintext.length, 0);
}
 
Example 13
Source File: AttachmentCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void test_attachment_encryptDecrypt() throws IOException, InvalidMessageException {
  byte[]        key             = Util.getSecretBytes(64);
  byte[]        plaintextInput  = "Peter Parker".getBytes();
  EncryptResult encryptResult   = encryptData(plaintextInput, key);
  File          cipherFile      = writeToFile(encryptResult.ciphertext);
  InputStream   inputStream     = AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, key, encryptResult.digest);
  byte[]        plaintextOutput = readInputStreamFully(inputStream);

  assertTrue(Arrays.equals(plaintextInput, plaintextOutput));

  cipherFile.delete();
}
 
Example 14
Source File: ProfileCipherTest.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void testEncryptDecrypt() throws InvalidCiphertextException, InvalidInputException {
  ProfileKey    key       = new ProfileKey(Util.getSecretBytes(32));
  ProfileCipher cipher    = new ProfileCipher(key);
  byte[]        name      = cipher.encryptName("Clement\0Duval".getBytes(), ProfileCipher.NAME_PADDED_LENGTH);
  byte[]        plaintext = cipher.decryptName(name);
  assertEquals(new String(plaintext), "Clement\0Duval");
}
 
Example 15
Source File: AttachmentCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void test_attachment_encryptDecryptEmpty() throws IOException, InvalidMessageException {
  byte[]        key             = Util.getSecretBytes(64);
  byte[]        plaintextInput  = "".getBytes();
  EncryptResult encryptResult   = encryptData(plaintextInput, key);
  File          cipherFile      = writeToFile(encryptResult.ciphertext);
  InputStream   inputStream     = AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, key, encryptResult.digest);
  byte[]        plaintextOutput = readInputStreamFully(inputStream);

  assertTrue(Arrays.equals(plaintextInput, plaintextOutput));

  cipherFile.delete();
}
 
Example 16
Source File: SignalBot.java    From signal-bot with GNU General Public License v3.0 5 votes vote down vote up
public void verify(String verificationCode) throws IOException {
    String username = prefs.get("LOCAL_USERNAME", null);
    String password = prefs.get("LOCAL_PASSWORD", null);
    logger.info("Verifying user " + username + " with code " + verificationCode + "...");
    String code = verificationCode.replace("-", "");
    int registrationId = KeyHelper.generateRegistrationId(false);
    prefs.putInt("REGISTRATION_ID", registrationId);
    byte[] profileKey = Util.getSecretBytes(32);
    byte[] unidentifiedAccessKey = UnidentifiedAccess.deriveAccessKeyFrom(profileKey);
    accountManager = new SignalServiceAccountManager(config, username, password, USER_AGENT);
    accountManager.verifyAccountWithCode(code, null, registrationId, true, null, unidentifiedAccessKey, false);
}
 
Example 17
Source File: AttachmentCipherTest.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void test_attachment_encryptDecrypt() throws IOException, InvalidMessageException {
  byte[]        key             = Util.getSecretBytes(64);
  byte[]        plaintextInput  = "Peter Parker".getBytes();
  EncryptResult encryptResult   = encryptData(plaintextInput, key);
  File          cipherFile      = writeToFile(encryptResult.ciphertext);
  InputStream   inputStream     = AttachmentCipherInputStream.createForAttachment(cipherFile, plaintextInput.length, key, encryptResult.digest);
  byte[]        plaintextOutput = readInputStreamFully(inputStream);

  assertTrue(Arrays.equals(plaintextInput, plaintextOutput));

  cipherFile.delete();
}
 
Example 18
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public ResumableUploadSpec getResumableUploadSpec(AttachmentV3UploadAttributes uploadAttributes) throws IOException {
  return new ResumableUploadSpec(Util.getSecretBytes(64),
                                 Util.getSecretBytes(16),
                                 uploadAttributes.getKey(),
                                 uploadAttributes.getCdn(),
                                 getResumableUploadUrl(uploadAttributes.getSignedUploadLocation(), uploadAttributes.getHeaders()),
                                 System.currentTimeMillis() + CDN2_RESUMABLE_LINK_LIFETIME_MILLIS);
}
 
Example 19
Source File: AttachmentCipherTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void test_sticker_encryptDecrypt() throws IOException, InvalidMessageException {
  byte[]        packKey         = Util.getSecretBytes(32);
  byte[]        plaintextInput  = "Peter Parker".getBytes();
  EncryptResult encryptResult   = encryptData(plaintextInput, expandPackKey(packKey));
  InputStream   inputStream     = AttachmentCipherInputStream.createForStickerData(encryptResult.ciphertext, packKey);
  byte[]        plaintextOutput = readInputStreamFully(inputStream);

  assertTrue(Arrays.equals(plaintextInput, plaintextOutput));
}
 
Example 20
Source File: SignalStorageCipher.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] encrypt(StorageCipherKey key, byte[] data) {
  try {
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    byte[] iv     = Util.getSecretBytes(IV_LENGTH);

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, iv));
    byte[] ciphertext = cipher.doFinal(data);

    return Util.join(iv, ciphertext);
  } catch (NoSuchAlgorithmException | java.security.InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException  e) {
    throw new AssertionError(e);
  }
}