Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/impl_ffi/impl_ffi.hmac.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Future<HmacSecretKeyImpl> hmacSecretKey_importRawKey(
HashImpl hash, {
int? length,
}) async {
_checkData(keyData.isNotEmpty, message: 'HMAC key data must not be empty');
return _HmacSecretKeyImpl(
_asUint8ListZeroedToBitLength(keyData, length),
_HashImpl.fromHash(hash),
Expand Down
49 changes: 49 additions & 0 deletions lib/src/testing/regression/hmac_empty_key.dart
Comment thread
harrshita123 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:webcrypto/webcrypto.dart';

import '../utils/utils.dart';

List<({String name, Future<void> Function() test})> tests() => [
(
name: 'HMAC rejects empty raw keys',
test: () => _expectEmptyKeyRejected(
HmacSecretKey.importRawKey(const [], Hash.sha256),
),
),
(
name: 'HMAC rejects empty JWK keys',
test: () => _expectEmptyKeyRejected(
HmacSecretKey.importJsonWebKey(const {
'kty': 'oct',
'alg': 'HS256',
'k': '',
}, Hash.sha256),
),
),
];

Future<void> _expectEmptyKeyRejected(Future<HmacSecretKey> import) async {
try {
await import;
} on FormatException catch (error) {
check(
error.message == 'HMAC key data must not be empty',
'Expected an empty HMAC key error',
);
return;
}
check(false, 'Expected an empty HMAC key to be rejected');
}
2 changes: 2 additions & 0 deletions lib/src/testing/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import 'regression/aes_gcm_invalid_tag_length.dart'
as aes_gcm_invalid_tag_length;
import 'regression/derive_bits_zero_length.dart' as derive_bits_zero_length;
import 'regression/ecdh_invalid_length.dart' as ecdh_invalid_length;
import 'regression/hmac_empty_key.dart' as hmac_empty_key;
import 'regression/issue_60_trailing_bytes.dart' as issue_60_trailing_bytes;
import 'regression/jwk_base64url.dart' as jwk_base64url;
import 'regression/rsa_oaep_sha1_jwk_alg.dart' as rsa_oaep_sha1_jwk_alg;
Expand Down Expand Up @@ -71,6 +72,7 @@ void runAllTests(
...jwk_base64url.tests(),
...derive_bits_zero_length.tests(),
...ecdh_invalid_length.tests(),
...hmac_empty_key.tests(),
...rsa_oaep_sha1_jwk_alg.tests(),
...rsa_modulus_length.tests(),
];
Expand Down