Skip to content

Commit 7fb3bba

Browse files
committed
adding complete test vectors
1 parent b7e9745 commit 7fb3bba

File tree

3 files changed

+993
-123
lines changed

3 files changed

+993
-123
lines changed

modules/kdf-ctr-mode-node/src/kdfctr.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import { uInt32BE } from '@aws-crypto/serialize'
1414
const SEPARATION_INDICATOR = Buffer.from([0x00])
1515
const COUNTER_START_VALUE = 1
1616
export const INT32_MAX_LIMIT = 2147483647
17-
const SUPPORTED_DERIVED_KEY_LENGTHS = [32]
17+
const SUPPORTED_IKM_LENGTHS = [32, 48, 66]
18+
const SUPPORTED_NONCE_LENGTHS = [16, 32]
19+
const SUPPORTED_DERIVED_KEY_LENGTHS = [32, 64]
1820
const SUPPORTED_DIGEST_ALGORITHMS = ['sha256', 'sha384']
1921

2022
export type SupportedDigestAlgorithms = 'sha256' | 'sha384'
21-
export type SupportedDerivedKeyLengths = 32
23+
export type SupportedDerivedKeyLengths = 32 | 64
2224

2325
interface KdfCtrInput {
2426
digestAlgorithm: SupportedDigestAlgorithms
@@ -35,9 +37,20 @@ export function kdfCounterMode({
3537
purpose,
3638
expectedLength,
3739
}: KdfCtrInput): Buffer {
40+
41+
/* Precondition: the ikm must be 32, 48, 66 bytes long */
42+
needs(
43+
SUPPORTED_IKM_LENGTHS.includes(ikm.length),
44+
`Unsupported IKM length ${ikm.length}`
45+
)
3846
/* Precondition: the nonce is required */
3947
needs(nonce, 'The nonce must be provided')
40-
/* Precondition: the expected length must be 32 bytes */
48+
/* Precondition: the nonce must be 16, 32 bytes long */
49+
needs(
50+
SUPPORTED_NONCE_LENGTHS.includes(nonce.length),
51+
`Unsupported nonce length ${nonce.length}`
52+
)
53+
/* Precondition: the expected length must be 32, 64 bytes */
4154
/* Precondition: the expected length * 8 must be under the max 32-bit signed integer */
4255
needs(
4356
SUPPORTED_DERIVED_KEY_LENGTHS.includes(expectedLength) &&
@@ -47,7 +60,7 @@ export function kdfCounterMode({
4760
)
4861

4962
const label = purpose || Buffer.alloc(0)
50-
const info = nonce || Buffer.alloc(0)
63+
const info = nonce
5164
const internalLength = 8 + SEPARATION_INDICATOR.length
5265

5366
/* Precondition: the input length must be under the max 32-bit signed integer */
@@ -102,11 +115,12 @@ export function rawDerive(
102115
)
103116

104117
// number of iterations calculated in accordance with SP800-108
105-
const iterations = Math.ceil(length / h)
118+
const iterations = Math.floor((length + h - 1) / h)
119+
106120
let buffer = Buffer.alloc(0)
107121
let i = Buffer.from(uInt32BE(COUNTER_START_VALUE))
108122

109-
for (let iteration = 1; iteration <= iterations + 1; iteration++) {
123+
for (let iteration = 1; iteration <= iterations; iteration++) {
110124
const digest = createHmac(digestAlgorithm, ikm)
111125
.update(i)
112126
.update(explicitInfo)

0 commit comments

Comments
 (0)