protected static Cipher initCipherForBlock(Cipher cipher, int block, EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode) throws GeneralSecurityException { EncryptionVerifier ver = encryptionInfo.getVerifier(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte blockKey[] = new byte[4]; LittleEndian.putUInt(blockKey, 0, block); MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo); hashAlg.update(skey.getEncoded()); byte encKey[] = hashAlg.digest(blockKey); EncryptionHeader header = encryptionInfo.getHeader(); int keyBits = header.getKeySize(); encKey = CryptoFunctions.getBlock0(encKey, keyBits / 8); if (keyBits == 40) { encKey = CryptoFunctions.getBlock0(encKey, 16); } SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm()); if (cipher == null) { cipher = CryptoFunctions.getCipher(key, header.getCipherAlgorithm(), null, null, encryptMode); } else { cipher.init(encryptMode, key); } return cipher; }
HashAlgorithm hashAlgo = header.getHashAlgorithm(); Mac integrityMD = CryptoFunctions.getMac(hashAlgo); byte hmacKey[] = getBlock0(this.integritySalt, getNextBlockSize(this.integritySalt.length, blockSize)); integrityMD.init(new SecretKeySpec(hmacKey, hashAlgo.jceHmacId)); byte hmacValueFilled[] = getBlock0(hmacValue, getNextBlockSize(hmacValue.length, blockSize));
/* package */ static byte[] hashInput(AgileEncryptionVerifier ver, byte pwHash[], byte blockKey[], byte inputKey[], int cipherMode) { CipherAlgorithm cipherAlgo = ver.getCipherAlgorithm(); ChainingMode chainMode = ver.getChainingMode(); int keySize = ver.getKeySize()/8; int blockSize = ver.getBlockSize(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte intermedKey[] = generateKey(pwHash, hashAlgo, blockKey, keySize); SecretKey skey = new SecretKeySpec(intermedKey, cipherAlgo.jceId); byte[] iv = generateIv(hashAlgo, ver.getSalt(), null, blockSize); Cipher cipher = getCipher(skey, cipherAlgo, chainMode, iv, cipherMode); byte[] hashFinal; try { inputKey = getBlock0(inputKey, getNextBlockSize(inputKey.length, blockSize)); hashFinal = cipher.doFinal(inputKey); return hashFinal; } catch (GeneralSecurityException e) { throw new EncryptedDocumentException(e); } }
verifierHashDec = getBlock0(verifierHashDec, ver.getHashAlgorithm().hashSize); keyspec = getBlock0(keyspec, header.getKeySize()/8); SecretKeySpec secretKey = new SecretKeySpec(keyspec, header.getCipherAlgorithm().jceId); Cipher cipher = getCipher(secretKey, cipherAlgo, header.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacKey[] = cipher.doFinal(header.getEncryptedHmacKey()); hmacKey = getBlock0(hmacKey, header.getHashAlgorithm().hashSize); cipher = getCipher(secretKey, cipherAlgo, ver.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacValue[] = cipher.doFinal(header.getEncryptedHmacValue()); hmacValue = getBlock0(hmacValue, header.getHashAlgorithm().hashSize);
byte vec[] = CryptoFunctions.generateIv(header.getHashAlgorithm(), header.getKeySalt(), kIntegrityKeyBlock, header.getBlockSize()); Cipher cipher = getCipher(secretKey, header.getCipherAlgorithm(), header.getChainingMode(), vec, Cipher.ENCRYPT_MODE); byte hmacKey[] = getBlock0(this.integritySalt, getNextBlockSize(this.integritySalt.length, blockSize)); byte encryptedHmacKey[] = cipher.doFinal(hmacKey); header.setEncryptedHmacKey(encryptedHmacKey);
protected static Cipher initCipherForBlock(Cipher cipher, int block, EncryptionInfo encryptionInfo, SecretKey skey, int encryptMode) throws GeneralSecurityException { EncryptionVerifier ver = encryptionInfo.getVerifier(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte blockKey[] = new byte[4]; LittleEndian.putUInt(blockKey, 0, block); MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo); hashAlg.update(skey.getEncoded()); byte encKey[] = hashAlg.digest(blockKey); EncryptionHeader header = encryptionInfo.getHeader(); int keyBits = header.getKeySize(); encKey = CryptoFunctions.getBlock0(encKey, keyBits / 8); if (keyBits == 40) { encKey = CryptoFunctions.getBlock0(encKey, 16); } SecretKey key = new SecretKeySpec(encKey, skey.getAlgorithm()); if (cipher == null) { cipher = CryptoFunctions.getCipher(key, header.getCipherAlgorithm(), null, null, encryptMode); } else { cipher.init(encryptMode, key); } return cipher; }
HashAlgorithm hashAlgo = header.getHashAlgorithm(); Mac integrityMD = CryptoFunctions.getMac(hashAlgo); byte hmacKey[] = getBlock0(this.integritySalt, getNextBlockSize(this.integritySalt.length, blockSize)); integrityMD.init(new SecretKeySpec(hmacKey, hashAlgo.jceHmacId)); byte hmacValueFilled[] = getBlock0(hmacValue, getNextBlockSize(hmacValue.length, blockSize));
/* package */ static byte[] hashInput(AgileEncryptionVerifier ver, byte pwHash[], byte blockKey[], byte inputKey[], int cipherMode) { CipherAlgorithm cipherAlgo = ver.getCipherAlgorithm(); ChainingMode chainMode = ver.getChainingMode(); int keySize = ver.getKeySize()/8; int blockSize = ver.getBlockSize(); HashAlgorithm hashAlgo = ver.getHashAlgorithm(); byte intermedKey[] = generateKey(pwHash, hashAlgo, blockKey, keySize); SecretKey skey = new SecretKeySpec(intermedKey, cipherAlgo.jceId); byte[] iv = generateIv(hashAlgo, ver.getSalt(), null, blockSize); Cipher cipher = getCipher(skey, cipherAlgo, chainMode, iv, cipherMode); byte[] hashFinal; try { inputKey = getBlock0(inputKey, getNextBlockSize(inputKey.length, blockSize)); hashFinal = cipher.doFinal(inputKey); return hashFinal; } catch (GeneralSecurityException e) { throw new EncryptedDocumentException(e); } }
verifierHashDec = getBlock0(verifierHashDec, ver.getHashAlgorithm().hashSize); keyspec = getBlock0(keyspec, header.getKeySize()/8); SecretKeySpec secretKey = new SecretKeySpec(keyspec, header.getCipherAlgorithm().jceId); Cipher cipher = getCipher(secretKey, cipherAlgo, header.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacKey[] = cipher.doFinal(header.getEncryptedHmacKey()); hmacKey = getBlock0(hmacKey, header.getHashAlgorithm().hashSize); cipher = getCipher(secretKey, cipherAlgo, ver.getChainingMode(), vec, Cipher.DECRYPT_MODE); byte hmacValue[] = cipher.doFinal(header.getEncryptedHmacValue()); hmacValue = getBlock0(hmacValue, header.getHashAlgorithm().hashSize);
byte vec[] = CryptoFunctions.generateIv(header.getHashAlgorithm(), header.getKeySalt(), kIntegrityKeyBlock, header.getBlockSize()); Cipher cipher = getCipher(secretKey, header.getCipherAlgorithm(), header.getChainingMode(), vec, Cipher.ENCRYPT_MODE); byte hmacKey[] = getBlock0(this.integritySalt, getNextBlockSize(this.integritySalt.length, blockSize)); byte encryptedHmacKey[] = cipher.doFinal(hmacKey); header.setEncryptedHmacKey(encryptedHmacKey);