I'm getting the following error when trying to wrap an RSA-PSS key using AES-KW:
The AES-KW input data length is invalid: not a multiple of 8 bytes
It works sometimes though, but only when the length of the output of the key in 'pkcs8' format is divisible by 8. That is perhaps also stated in https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey.
My question then is: Is it not possible to wrap an RSA-PSS key using AES-KW? I can't find any padding option. If not, is my best option to go the IV route?
See below code:
export async function wrapKeyAsync(key: CryptoKey, password: string) {
let keyMaterial = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(password),
{ name: "PBKDF2" },
false,
["deriveBits", "deriveKey"]
);
let salt = crypto.getRandomValues(new Uint8Array(16));
let wrappingKey = await crypto.subtle.deriveKey(
{
"name": "PBKDF2",
salt: salt,
"iterations": 100_000,
"hash": "SHA-256"
},
keyMaterial,
{ "name": "AES-KW", "length": 256 },
true,
["wrapKey", "unwrapKey"]
);
console.log((await crypto.subtle.exportKey("pkcs8", key)).byteLength / 8); // Only works when input is divisible by 8
return {
wrappedKey: new Uint8Array(await crypto.subtle.wrapKey(
"pkcs8",
key,
wrappingKey,
"AES-KW"
)),
salt
};
}