Skip to content

Commit

Permalink
deriveBits: document deriveBits limitation, and explicitly return an
Browse files Browse the repository at this point in the history
error
  • Loading branch information
olegbespalov committed Apr 25, 2024
1 parent cf71dba commit 3f7b1d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ The current state of the project is that it is an experimental module of the Web
| `crypto.subtle.deriveKey()` ||||
| `crypto.subtle.deriveBits()` ||||

Note: `deriveBits` currently doesn't support length parameter non-multiple of 8.

##### Key wrapping

| API | AES-CBC | AES-GCM | AES-CTR | AES-KW | RSA-OAEP |
Expand Down
6 changes: 6 additions & 0 deletions webcrypto/subtle_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,12 @@ func (sc *SubtleCrypto) DeriveBits( //nolint:funlen,gocognit // we have a lot of
return NewError(InvalidAccessError, err.Error())
}

// currently we don't support lengths that are not multiples of 8
// https://github.com/grafana/xk6-webcrypto/issues/80
if length%8 != 0 {
return NewError(NotSupportedError, "currently only multiples of 8 are supported for length")
}

deriver, err = newBitsDeriver(normalizeAlgorithmName)
if err != nil {
return err
Expand Down
20 changes: 11 additions & 9 deletions webcrypto/tests/derive_bits_keys/ecdh_bits.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ function define_tests() {
});
}, namedCurve + " short result");

// Non-multiple of 8
promise_test(function(test) {
return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve] - 11)
.then(function(derivation) {
assert_true(equalBuffers(derivation, derivations[namedCurve], 8 * sizes[namedCurve] - 11), "Derived correct bits " + namedCurve + " size: " + 8 * sizes[namedCurve] + " derivation: " + JSON.stringify(derivation) + " expected: " + JSON.stringify(derivations[namedCurve]) );
}, function(err) {
assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
});
}, namedCurve + " non-multiple of 8 bits");
// TODO: once we have support of lengths that are not a multiple of 8 bits, uncomment this test
// https://github.com/grafana/xk6-webcrypto/issues/80
// // Non-multiple of 8
// promise_test(function(test) {
// return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve] - 11)
// .then(function(derivation) {
// assert_true(equalBuffers(derivation, derivations[namedCurve], 8 * sizes[namedCurve] - 11), "Derived correct bits " + namedCurve + " size: " + 8 * sizes[namedCurve] + " derivation: " + JSON.stringify(derivation) + " expected: " + JSON.stringify(derivations[namedCurve]) );
// }, function(err) {
// assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
// });
// }, namedCurve + " non-multiple of 8 bits");

// Errors to test:

Expand Down

0 comments on commit 3f7b1d5

Please sign in to comment.