SubtleCrypto.wrapKey()

The SubtleCrypto.wrapKey() method "wraps" a key. This means that it exports the key in an external, portable format, then encrypts the exported key. Wrapping a key helps protect it in untrusted environments, such as an otherwise unprotected data store or in transmission over an unprotected network.

As with SubtleCrypto.exportKey(), you specify an export format for the key. To export a key, the key must have CryptoKey.extractable set to true.

But because wrapKey() also encrypts the key to be imported, you also need to pass in the key that must be used to encrypt it. This is sometimes called the "wrapping key".

The inverse of wrapKey() is SubtleCrypto.unwrapKey(): while wrapKey is composed of export+encrypt, unwrapKey is composed of decrypt+import.

Syntax

var result = crypto.subtle.wrapKey(format, key, wrappingKey, wrapAlgo);

Parameters

Return value

  • result is a Promise that on success resolves with an ArrayBuffer containing the encrypted exported key.

Exceptions

The promise is rejected when one of the following exceptions is encountered:

  • InvalidAccessError when the wrapping key is not a key for the requested wrap algorithm.
  • NotSupported when trying to use an algorithm that is either unknown or isn't suitable for encryption or wrapping.
  • TypeError when trying to use an invalid format.

Supported algorithms

All algorithms that are usable for encryption are also usable for key wrapping, as long as the key has the "wrapKey" usage set. For key wrapping you have the additional option of AES-KW.

AES-KW

AES-KW is a way to use the AES cipher for key wrapping.

One advantage of using AES-KW over another AES mode such as AES-GCM is that AES-KW does not require an initialization vector. To use AES-KW, the input must be a multiple of 64 bits.

AES-KW is specified in RFC 3394.

Specifications

Specification Status Comment
Web Cryptography API
The definition of 'SubtleCrypto.wrapKey()' in that specification.
Recommendation Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 37Edge Partial support 12
Notes
Partial support 12
Notes
Notes Not supported: AES-CTR.
Firefox Full support 34
Full support 34
No support 32 — 34
Disabled
Disabled From version 32 until version 34 (exclusive): this feature is behind the dom.webcrypto.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns KeyOperation instead of Promise
Opera Full support 24Safari Full support 7WebView Android Full support 37Chrome Android Full support 37Edge Mobile Full support 12Firefox Android Full support 34
Full support 34
No support 32 — 34
Disabled
Disabled From version 32 until version 34 (exclusive): this feature is behind the dom.webcrypto.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 24Safari iOS Full support 7Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
Partial support  
Partial support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

Document Tags and Contributors

Contributors to this page: wbamberg, kgeorgiou, fscholz, Dans24, abbycar, teoli
Last updated by: wbamberg,