Aes

AES-CTR 認證 salt+HMAC(密文) OR HMAC(salt+密文)

  • September 22, 2017

這些函式中的哪一個更適合驗證 AES-CTR?

function encrypt(msg, key) {
   encryptionKey, encryptionSalt = newPBKDF2(key)
   hmacKey, hmacSalt = newPBKDF2(key)
   ciphertext = aesCTR(msg, encryptionKey)
   hmac = generateHMAC(ciphertext, hmacKey)
   salt = encryptionSalt + hmacSalt
   return hmac + salt + ciphertext
}

VS

function encrypt(msg, key) {
   encryptionKey, encryptionSalt = newPBKDF2(key)
   hmacKey, hmacSalt = newPBKDF2(key)
   ciphertext = aesCTR(msg, encryptionKey)
   salt = encryptionSalt + hmacSalt
   hmac = generateHMAC(salt + ciphertext, hmacKey)
   return hmac + salt + ciphertext
}

他們都沒有。PBKDF2 不應多次使用或用於大輸出。我建議使用額外的 KBKDF,例如 HKDF。

function encrypt(msg, passphrase, iterationCount) {
   sessionKey, salt = newPBKDF2(passphrase, iterationCount)
   encryptionKey = HKDF(sessionKey, 'ENC')
   encryptionIV = HKDF(sessionKey, 'IV')
   hmacKey = HKDF(sessionKey, 'MAC')
   ciphertext = aesCTR(msg, encryptionKey, encryptionIV)
   authTag = generateHMAC(ciphertext, hmacKey)
   return salt + ciphertext + authTag
}

密碼片語的迭代計數應設置為高。仍然需要一個好的密碼。

由於加密已經應該隨機化以實現 CPA 安全性,因此不需要向 HMAC 輸入鹽。請注意,如果您重用密碼,重放攻擊可能仍然有效,當然。您需要在協議定義中處理好這一點。

引用自:https://crypto.stackexchange.com/questions/51717