I'm trying to use LibSodium to implement Key Wrapping using SIV mode (Synthetic Initialization Vector). I'm using this answer as a guide, but I want be sure I understand it correctly. My biggest doubt was whether to use masterKey or authenticationKey as the message param when generating the auth tag.
Similarly with crypto_stream_xchacha20_xor, it wasn't clear to me whether the message param should be the masterKey, or the encryptionKey.
Does the following pseudocode look sound?
Wrap
argonBytes = crypto_pwhash({length: 32, password: password, …})
masterKey = randombytes_buf(32)
authenticationKey = crypto_kdf_derive_from_key({length: 32, id: 0, context: “authKey”,
ikm:argonBytes})
encryptionKey = crypto_kdf_derive_from_key({length: 32, id: 0, context: “encKey”, ikm: argonBytes})
authTag = crypto_generichash({length: 24, message: masterKey, key: authenticationKey})
wrappedMasterKey = authTag + crypto_stream_xchacha20_xor({message: masterKey, nonce: authTag, key: encryptionKey})
Unwrap
argonBytes = crypto_pwhash({length: 32, password: password, …})
authenticationKey = crypto_kdf_derive_from_key({length: 32, id: 0, context: “authKey”, ikm:argonBytes})
encryptionKey = crypto_kdf_derive_from_key({length: 32, id: 0, context: “encKey”, ikm: argonBytes})
nonce = wrappedMasterKey[0,…, 24]
unwrappedMasterKey = crypto_stream_xchacha20_xor({message: wrappedMasterKey[24,…], nonce: nonce, key: encryptionKey})
authTag = crypto_generichash({length: 24, message: unwrappedMasterKey, key: authenticationKey})
if (!memcmp(authTag, nonce)) throw Error
return unwrappedMasterKey