@@ -14,11 +14,13 @@ import { uInt32BE } from '@aws-crypto/serialize'
14
14
const SEPARATION_INDICATOR = Buffer . from ( [ 0x00 ] )
15
15
const COUNTER_START_VALUE = 1
16
16
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 ]
18
20
const SUPPORTED_DIGEST_ALGORITHMS = [ 'sha256' , 'sha384' ]
19
21
20
22
export type SupportedDigestAlgorithms = 'sha256' | 'sha384'
21
- export type SupportedDerivedKeyLengths = 32
23
+ export type SupportedDerivedKeyLengths = 32 | 64
22
24
23
25
interface KdfCtrInput {
24
26
digestAlgorithm : SupportedDigestAlgorithms
@@ -35,9 +37,20 @@ export function kdfCounterMode({
35
37
purpose,
36
38
expectedLength,
37
39
} : 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
+ )
38
46
/* Precondition: the nonce is required */
39
47
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 */
41
54
/* Precondition: the expected length * 8 must be under the max 32-bit signed integer */
42
55
needs (
43
56
SUPPORTED_DERIVED_KEY_LENGTHS . includes ( expectedLength ) &&
@@ -47,7 +60,7 @@ export function kdfCounterMode({
47
60
)
48
61
49
62
const label = purpose || Buffer . alloc ( 0 )
50
- const info = nonce || Buffer . alloc ( 0 )
63
+ const info = nonce
51
64
const internalLength = 8 + SEPARATION_INDICATOR . length
52
65
53
66
/* Precondition: the input length must be under the max 32-bit signed integer */
@@ -102,11 +115,12 @@ export function rawDerive(
102
115
)
103
116
104
117
// 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
+
106
120
let buffer = Buffer . alloc ( 0 )
107
121
let i = Buffer . from ( uInt32BE ( COUNTER_START_VALUE ) )
108
122
109
- for ( let iteration = 1 ; iteration <= iterations + 1 ; iteration ++ ) {
123
+ for ( let iteration = 1 ; iteration <= iterations ; iteration ++ ) {
110
124
const digest = createHmac ( digestAlgorithm , ikm )
111
125
. update ( i )
112
126
. update ( explicitInfo )
0 commit comments