/** * Returns the byte length of the specified byte array. * * @param byteArray The byte array. May be {@code null}. * * @return The bite length, zero if the array is {@code null}. */ public static int bitLength(final byte[] byteArray) { if (byteArray == null) { return 0; } else { return bitLength(byteArray.length); } }
/** * Generates a random 128 bit (16 byte) Initialisation Vector(IV) for * use in AES-CBC encryption. * * @param randomGen The secure random generator to use. Must be * correctly initialised and not {@code null}. * * @return The random 128 bit IV, as 16 byte array. */ public static byte[] generateIV(final SecureRandom randomGen) { byte[] bytes = new byte[ByteUtils.byteLength(IV_BIT_LENGTH)]; randomGen.nextBytes(bytes); return bytes; }
/** * Composes the other info as {@code algID || partyUInfo || partyVInfo * || suppPubInfo || suppPrivInfo}. * * @param algID The algorithm identifier, {@code null} if not * specified. * @param partyUInfo The partyUInfo, {@code null} if not specified. * @param partyVInfo The partyVInfo {@code null} if not specified. * @param suppPubInfo The suppPubInfo, {@code null} if not specified. * @param suppPrivInfo The suppPrivInfo, {@code null} if not specified. * * @return The resulting other info. */ public static byte[] composeOtherInfo(final byte[] algID, final byte[] partyUInfo, final byte[] partyVInfo, final byte[] suppPubInfo, final byte[] suppPrivInfo) { return ByteUtils.concat(algID, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo); }
for (int i=1; i <= computeDigestCycles(ByteUtils.safeBitLength(md.getDigestLength()), keyLengthBits); i++) { final int keyLengthBytes = ByteUtils.byteLength(keyLengthBits); return new SecretKeySpec(ByteUtils.subArray(derivedKeyMaterial, 0, keyLengthBytes), "AES");
/** * Returns the byte length of the specified byte array, preventing * integer overflow. * * @param byteArray The byte array. May be {@code null}. * * @return The bite length, zero if the array is {@code null}. * * @throws IntegerOverflowException On a integer overflow. */ public static int safeBitLength(final byte[] byteArray) throws IntegerOverflowException { if (byteArray == null) { return 0; } else { return safeBitLength(byteArray.length); } }
final int tagPos = cipherOutput.length - ByteUtils.byteLength(AUTH_TAG_BIT_LENGTH); byte[] cipherText = ByteUtils.subArray(cipherOutput, 0, tagPos); byte[] authTag = ByteUtils.subArray(cipherOutput, tagPos, ByteUtils.byteLength(AUTH_TAG_BIT_LENGTH));
byte[] block = extractBlock(formattedSalt, iterationCount, i + 1, prf); if (i == (l - 1)) { block = ByteUtils.subArray(block, 0, r);
/** * Computes the bit length of the specified Additional Authenticated * Data (AAD). Used in AES/CBC/PKCS5Padding/HMAC-SHA2 encryption. * * @param aad The Additional Authenticated Data (AAD). Must not be * {@code null}. * * @return The computed AAD bit length, as a 64 bit big-endian * representation (8 byte array). * * @throws IntegerOverflowException On a integer overflow. */ public static byte[] computeLength(final byte[] aad) throws IntegerOverflowException { final int bitLength = ByteUtils.safeBitLength(aad); return ByteBuffer.allocate(8).putLong(bitLength).array(); } }
@Override public int size() { try { return ByteUtils.safeBitLength(k.decode()); } catch (IntegerOverflowException e) { throw new ArithmeticException(e.getMessage()); } }
/** * Creates a new Message Authentication (MAC) signer. * * @param secret The secret. Must be at least 256 bits long and not * {@code null}. * * @throws KeyLengthException If the secret length is shorter than the * minimum 256-bit requirement. */ public MACSigner(final byte[] secret) throws KeyLengthException { super(secret, getCompatibleAlgorithms(ByteUtils.bitLength(secret.length))); }
/** * Encodes the specified data as {@code data.length || data}. * * @param data The data to encode, may be {@code null}. * * @return The encoded data. */ public static byte[] encodeDataWithLength(final byte[] data) { byte[] bytes = data != null ? data : new byte[0]; byte[] length = IntegerUtils.toBytes(bytes.length); return ByteUtils.concat(length, bytes); }
/** * Generates a Content Encryption Key (CEK) for the specified JOSE * encryption method. * * @param enc The encryption method. Must not be {@code null}. * @param randomGen The secure random generator to use. Must not be * {@code null}. * * @return The generated CEK (with algorithm "AES"). * * @throws JOSEException If the encryption method is not supported. */ public static SecretKey generateCEK(final EncryptionMethod enc, final SecureRandom randomGen) throws JOSEException { if (! SUPPORTED_ENCRYPTION_METHODS.contains(enc)) { throw new JOSEException(AlgorithmSupportMessage.unsupportedEncryptionMethod(enc, SUPPORTED_ENCRYPTION_METHODS)); } final byte[] cekMaterial = new byte[ByteUtils.byteLength(enc.cekBitLength())]; randomGen.nextBytes(cekMaterial); return new SecretKeySpec(cekMaterial, "AES"); }
@Override public int size() { try { return ByteUtils.safeBitLength(n.decode()); } catch (IntegerOverflowException e) { throw new ArithmeticException(e.getMessage()); } }
@Override public int size() { return ByteUtils.bitLength(x.decode()); }
if (i == 1) inputBytes = ByteUtils.concat(salt, IntegerUtils.toBytes(blockIndex)); currentU = prf.doFinal(inputBytes); xorU = currentU;
@Override public Base64URL sign(final JWSHeader header, final byte[] signingInput) throws JOSEException { final int minRequiredLength = getMinRequiredSecretLength(header.getAlgorithm()); if (getSecret().length < ByteUtils.byteLength(minRequiredLength)) { throw new KeyLengthException("The secret length for " + header.getAlgorithm() + " must be at least " + minRequiredLength + " bits"); } String jcaAlg = getJCAAlgorithmName(header.getAlgorithm()); byte[] hmac = HMAC.compute(jcaAlg, getSecret(), signingInput, getJCAContext().getProvider()); return Base64URL.encode(hmac); } }
/** * Validates the specified IV and authentication tag according to the * AES GCM requirements in * <a href="https://tools.ietf.org/html/rfc7518#section-5.3">JWA RFC</a>. * * @param iv The IV to check for compliance. * @param authTagLength The authentication tag length to check for * compliance. * * @throws JOSEException If the parameters don't match the JWA * requirements. * * @see #IV_BIT_LENGTH * @see #AUTH_TAG_BIT_LENGTH */ private static void validate(final byte[] iv, final int authTagLength) throws JOSEException { if (ByteUtils.safeBitLength(iv) != IV_BIT_LENGTH) { throw new JOSEException(String.format("IV length of %d bits is required, got %d", IV_BIT_LENGTH, ByteUtils.safeBitLength(iv))); } if (authTagLength != AUTH_TAG_BIT_LENGTH) { throw new JOSEException(String.format("Authentication tag length of %d bits is required, got %d", AUTH_TAG_BIT_LENGTH, authTagLength)); } }
/** * Creates a new AES encryption / decryption provider. * * @param kek The Key Encryption Key. Must be 128 bits (16 bytes), 192 * bits (24 bytes) or 256 bits (32 bytes). Must not be * {@code null}. * * @throws KeyLengthException If the KEK length is invalid. */ protected AESCryptoProvider(final SecretKey kek) throws KeyLengthException { super(getCompatibleJWEAlgorithms(ByteUtils.bitLength(kek.getEncoded())), ContentCryptoProvider.SUPPORTED_ENCRYPTION_METHODS); this.kek = kek; }
return cipher.doFinal(ByteUtils.concat(cipherText, authTag));
byte[] secretKeyBytes = cipher.doFinal(encryptedCEK); if (ByteUtils.safeBitLength(secretKeyBytes) != keyLength) {