org.bouncycastle.crypto.params.HKDFParameters Java Examples

The following examples show how to use org.bouncycastle.crypto.params.HKDFParameters. 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: LibraKeyFactory.java    From jlibra with Apache License 2.0 6 votes vote down vote up
public ExtendedPrivKey privateChild(ChildNumber childNumber) {
    byte[] secretKey = new byte[32];
    byte[] info = createInfo(childNumber);
    SHA3Digest sha3 = new SHA3Digest(256);
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(sha3);
    hkdf.init(HKDFParameters.skipExtractParameters(master.getData(), info));
    hkdf.generateBytes(secretKey, 0, 32);

    return new ExtendedPrivKey(new SecretKey(ByteArray.from(secretKey)));
}
 
Example #2
Source File: HttpEce.java    From webpush-java with MIT License 6 votes vote down vote up
/**
 * Convenience method for computing the HMAC Key Derivation Function. The real work is offloaded to BouncyCastle.
 */
protected static byte[] hkdfExpand(byte[] ikm, byte[] salt, byte[] info, int length) {
    log("salt", salt);
    log("ikm", ikm);
    log("info", info);

    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
    hkdf.init(new HKDFParameters(ikm, salt, info));

    byte[] okm = new byte[length];
    hkdf.generateBytes(okm, 0, length);

    log("expand", okm);

    return okm;
}
 
Example #3
Source File: RFC5869KDF.java    From InflatableDonkey with MIT License 6 votes vote down vote up
public static final byte[]
        apply(byte[] ikm, byte[] salt, byte[] info, Supplier<Digest> digestSupplier, int keyLengthBytes) {
            
    logger.trace("<< apply() - ikm: 0x{} salt: 0x{} info: 0x{} digestSupplier: {} keyLengthBytes: {}",
            Hex.toHexString(ikm), Hex.toHexString(salt), Hex.toHexString(info), digestSupplier, keyLengthBytes);

    Digest hash = digestSupplier.get();
    byte[] okm = new byte[keyLengthBytes];

    HKDFParameters params = new HKDFParameters(ikm, salt, info);
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(hash);
    hkdf.init(params);
    hkdf.generateBytes(okm, 0, keyLengthBytes);

    logger.trace(">> apply() - output keying material: 0x{}", Hex.toHexString(okm));
    return okm;
}
 
Example #4
Source File: FinalPairHandler.java    From HAP-Java with MIT License 6 votes vote down vote up
private HttpResponse createUser(byte[] username, byte[] ltpk, byte[] proof) throws Exception {
  HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA512Digest());
  hkdf.init(
      new HKDFParameters(
          k,
          "Pair-Setup-Controller-Sign-Salt".getBytes(StandardCharsets.UTF_8),
          "Pair-Setup-Controller-Sign-Info".getBytes(StandardCharsets.UTF_8)));
  byte[] okm = new byte[32];
  hkdf.generateBytes(okm, 0, 32);

  byte[] completeData = ByteUtils.joinBytes(okm, username, ltpk);

  if (!new EdsaVerifier(ltpk).verify(completeData, proof)) {
    throw new Exception("Invalid signature");
  }
  authInfo.createUser(authInfo.getMac() + new String(username, StandardCharsets.UTF_8), ltpk);
  return createResponse();
}
 
Example #5
Source File: BouncyCastleV1CryptoProvider.java    From paseto with MIT License 5 votes vote down vote up
@Override
public byte[] hkdfExtractAndExpand(byte[] salt, byte[] inputKeyingMaterial, byte[] info) {
	validateHkdfExtractAndExpand(salt, inputKeyingMaterial, info);

	Digest digest = new SHA384Digest();
	HKDFBytesGenerator hkdf = new HKDFBytesGenerator(digest);
	hkdf.init(new HKDFParameters(inputKeyingMaterial, salt, info));

	byte[] out = new byte[HKDF_LEN];
	hkdf.generateBytes(out, 0, out.length);
	return out;
}
 
Example #6
Source File: Hashes.java    From symbol-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Hasher used for shared keys
 *
 * @param sharedSecret the shared secret
 * @return the shared key hash.
 */
public static byte[] sha256ForSharedKey(byte[] sharedSecret) {
    Digest hash = new SHA256Digest();
    byte[] info = "catapult".getBytes();
    int length = 32;
    byte[] sharedKey = new byte[length];
    HKDFParameters params = new HKDFParameters(sharedSecret, null, info);
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(hash);
    hkdf.init(params);
    hkdf.generateBytes(sharedKey, 0, length);
    return sharedKey;
}
 
Example #7
Source File: CryptAeadBase.java    From shadowsocks-java with MIT License 5 votes vote down vote up
private byte[] genSubkey(byte[] salt) {
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA1Digest());
    hkdf.init(new HKDFParameters(_ssKey.getEncoded(), salt, info));
    byte[] okm = new byte[getKeyLength()];
    hkdf.generateBytes(okm, 0, getKeyLength());
    return okm;
}
 
Example #8
Source File: HttpEce.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Convenience method for computing the HMAC Key Derivation Function. The
 * real work is offloaded to BouncyCastle.
 */
protected static byte[] hkdfExpand(byte[] ikm, byte[] salt, byte[] info, int length)
        throws InvalidKeyException, NoSuchAlgorithmException {
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
    hkdf.init(new HKDFParameters(ikm, salt, info));

    byte[] okm = new byte[length];
    hkdf.generateBytes(okm, 0, length);

    return okm;
}
 
Example #9
Source File: Crypto.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
public static byte[] hkdfSha256(byte[] ikm, byte[] salt, byte[] info, int outputLength) {
  byte[] output = new byte[outputLength];
  HKDFParameters params = new HKDFParameters(ikm, salt, info);
  HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
  hkdf.init(params);
  hkdf.generateBytes(output, 0, outputLength);
  return output;
}
 
Example #10
Source File: FinalPairHandler.java    From HAP-Java with MIT License 5 votes vote down vote up
public HttpResponse handle(PairSetupRequest req) throws Exception {
  HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA512Digest());
  hkdf.init(
      new HKDFParameters(
          k,
          "Pair-Setup-Encrypt-Salt".getBytes(StandardCharsets.UTF_8),
          "Pair-Setup-Encrypt-Info".getBytes(StandardCharsets.UTF_8)));
  byte[] okm = hkdf_enc_key = new byte[32];
  hkdf.generateBytes(okm, 0, 32);

  return decrypt((Stage3Request) req, okm);
}
 
Example #11
Source File: FinalPairHandler.java    From HAP-Java with MIT License 5 votes vote down vote up
private HttpResponse createResponse() throws Exception {
  HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA512Digest());
  hkdf.init(
      new HKDFParameters(
          k,
          "Pair-Setup-Accessory-Sign-Salt".getBytes(StandardCharsets.UTF_8),
          "Pair-Setup-Accessory-Sign-Info".getBytes(StandardCharsets.UTF_8)));
  byte[] okm = new byte[32];
  hkdf.generateBytes(okm, 0, 32);

  EdsaSigner signer = new EdsaSigner(authInfo.getPrivateKey());

  byte[] material =
      ByteUtils.joinBytes(
          okm, authInfo.getMac().getBytes(StandardCharsets.UTF_8), signer.getPublicKey());

  byte[] proof = signer.sign(material);

  Encoder encoder = TypeLengthValueUtils.getEncoder();
  encoder.add(MessageType.USERNAME, authInfo.getMac().getBytes(StandardCharsets.UTF_8));
  encoder.add(MessageType.PUBLIC_KEY, signer.getPublicKey());
  encoder.add(MessageType.SIGNATURE, proof);
  byte[] plaintext = encoder.toByteArray();

  ChachaEncoder chacha =
      new ChachaEncoder(hkdf_enc_key, "PS-Msg06".getBytes(StandardCharsets.UTF_8));
  byte[] ciphertext = chacha.encodeCiphertext(plaintext);

  encoder = TypeLengthValueUtils.getEncoder();
  encoder.add(MessageType.STATE, (short) 6);
  encoder.add(MessageType.ENCRYPTED_DATA, ciphertext);

  return new PairingResponse(encoder.toByteArray());
}
 
Example #12
Source File: PairVerificationManager.java    From HAP-Java with MIT License 5 votes vote down vote up
private HttpResponse stage1(Stage1Request request) throws Exception {
  logger.trace("Starting pair verification for " + registry.getLabel());
  clientPublicKey = request.getClientPublicKey();
  publicKey = new byte[32];
  byte[] privateKey = new byte[32];
  getSecureRandom().nextBytes(privateKey);
  Curve25519.keygen(publicKey, null, privateKey);

  sharedSecret = new byte[32];
  Curve25519.curve(sharedSecret, privateKey, clientPublicKey);

  byte[] material =
      ByteUtils.joinBytes(
          publicKey, authInfo.getMac().getBytes(StandardCharsets.UTF_8), clientPublicKey);

  byte[] proof = new EdsaSigner(authInfo.getPrivateKey()).sign(material);

  HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA512Digest());
  hkdf.init(
      new HKDFParameters(
          sharedSecret,
          "Pair-Verify-Encrypt-Salt".getBytes(StandardCharsets.UTF_8),
          "Pair-Verify-Encrypt-Info".getBytes(StandardCharsets.UTF_8)));
  hkdfKey = new byte[32];
  hkdf.generateBytes(hkdfKey, 0, 32);

  Encoder encoder = TypeLengthValueUtils.getEncoder();
  encoder.add(MessageType.USERNAME, authInfo.getMac().getBytes(StandardCharsets.UTF_8));
  encoder.add(MessageType.SIGNATURE, proof);
  byte[] plaintext = encoder.toByteArray();

  ChachaEncoder chacha = new ChachaEncoder(hkdfKey, "PV-Msg02".getBytes(StandardCharsets.UTF_8));
  byte[] ciphertext = chacha.encodeCiphertext(plaintext);

  encoder = TypeLengthValueUtils.getEncoder();
  encoder.add(MessageType.STATE, (short) 2);
  encoder.add(MessageType.ENCRYPTED_DATA, ciphertext);
  encoder.add(MessageType.PUBLIC_KEY, publicKey);
  return new PairingResponse(encoder.toByteArray());
}
 
Example #13
Source File: PairVerificationManager.java    From HAP-Java with MIT License 5 votes vote down vote up
private byte[] createKey(String info) {
  HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA512Digest());
  hkdf.init(
      new HKDFParameters(
          sharedSecret,
          "Control-Salt".getBytes(StandardCharsets.UTF_8),
          info.getBytes(StandardCharsets.UTF_8)));
  byte[] key = new byte[32];
  hkdf.generateBytes(key, 0, 32);
  return key;
}