All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whispersystems.signalservice.api.kbs.MasterKey Maven / Gradle / Ivy

There is a newer version: 2.15.3_unofficial_107
Show newest version
package org.whispersystems.signalservice.api.kbs;

import org.signal.libsignal.protocol.kdf.HKDF;
import org.whispersystems.signalservice.api.backup.BackupKey;
import org.whispersystems.signalservice.api.storage.StorageKey;
import org.whispersystems.signalservice.internal.util.Hex;
import org.signal.core.util.Base64;
import org.whispersystems.util.StringUtil;

import java.security.SecureRandom;
import java.util.Arrays;

import static org.signal.core.util.CryptoUtil.hmacSha256;

public final class MasterKey {

  private static final int LENGTH = 32;

  private final byte[] masterKey;

  public MasterKey(byte[] masterKey) {
    if (masterKey.length != LENGTH) throw new AssertionError();

    this.masterKey = masterKey;
  }

  public static MasterKey createNew(SecureRandom secureRandom) {
    byte[] key = new byte[LENGTH];
    secureRandom.nextBytes(key);
    return new MasterKey(key);
  }

  public String deriveRegistrationLock() {
    return Hex.toStringCondensed(derive("Registration Lock"));
  }

  public String deriveRegistrationRecoveryPassword() {
    return Base64.encodeWithPadding(derive("Registration Recovery"));
  }

  public StorageKey deriveStorageServiceKey() {
    return new StorageKey(derive("Storage Service Encryption"));
  }

  public byte[] deriveLoggingKey() {
    return derive("Logging Key");
  }

  public BackupKey deriveBackupKey() {
    return new BackupKey(HKDF.deriveSecrets(masterKey, "20231003_Signal_Backups_GenerateBackupKey".getBytes(), 32));
  }

  private byte[] derive(String keyName) {
    return hmacSha256(masterKey, StringUtil.utf8(keyName));
  }

  public byte[] serialize() {
    return masterKey.clone();
  }

  @Override
  public boolean equals(Object o) {
    if (o == null || o.getClass() != getClass()) return false;

    return Arrays.equals(((MasterKey) o).masterKey, masterKey);
  }

  @Override
  public int hashCode() {
    return Arrays.hashCode(masterKey);
  }

  @Override
  public String toString() {
    return "MasterKey(xxx)";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy