|
| 1 | +// Copyright (c) 2017-2023 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | +// https://opensource.org/licenses/Apache-2.0 |
| 4 | +// |
| 5 | +// Copyright Joyent, Inc. and other Node contributors. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | +// copy of this software and associated documentation files (the |
| 9 | +// "Software"), to deal in the Software without restriction, including |
| 10 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 11 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 12 | +// persons to whom the Software is furnished to do so, subject to the |
| 13 | +// following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included |
| 16 | +// in all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 21 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 22 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + |
| 26 | +/* todo: the following is adopted code, enabling linting one day */ |
| 27 | +/* eslint-disable */ |
| 28 | + |
| 29 | +'use strict'; |
| 30 | + |
| 31 | +import { default as cryptoImpl } from 'node-internal:crypto'; |
| 32 | + |
| 33 | +import { |
| 34 | + validateFunction, |
| 35 | + validateInteger, |
| 36 | + validateString, |
| 37 | +} from 'node-internal:validators'; |
| 38 | + |
| 39 | +import { |
| 40 | + KeyObject, |
| 41 | +} from 'node-internal:crypto_keys'; |
| 42 | + |
| 43 | +type ArrayLike = cryptoImpl.ArrayLike; |
| 44 | + |
| 45 | +import { |
| 46 | + kMaxLength, |
| 47 | +} from 'node-internal:internal_buffer'; |
| 48 | + |
| 49 | +import { |
| 50 | + toBuf, |
| 51 | + validateByteSource, |
| 52 | +} from 'node-internal:crypto_util'; |
| 53 | + |
| 54 | +import { |
| 55 | + isAnyArrayBuffer, |
| 56 | + isArrayBufferView, |
| 57 | +} from 'node-internal:internal_types'; |
| 58 | + |
| 59 | +import { |
| 60 | + NodeError, |
| 61 | + ERR_INVALID_ARG_TYPE, |
| 62 | + ERR_OUT_OF_RANGE, |
| 63 | +} from 'node-internal:internal_errors'; |
| 64 | + |
| 65 | +function validateParameters(hash: string, key: ArrayLike | KeyObject, salt: ArrayLike, |
| 66 | + info: ArrayLike, length: number) { |
| 67 | + // TODO(soon): Add support for KeyObject input. |
| 68 | + if (key instanceof KeyObject) { |
| 69 | + throw new NodeError("ERR_METHOD_NOT_IMPLEMENTED", "KeyObject support for hkdf() and " + |
| 70 | + "hkdfSync() is not yet implemented. Use ArrayBuffer, TypedArray, " + |
| 71 | + "DataView, or Buffer instead."); |
| 72 | + } |
| 73 | + |
| 74 | + validateString(hash, 'digest'); |
| 75 | + key = prepareKey(key as unknown as ArrayLike); |
| 76 | + salt = validateByteSource(salt, 'salt'); |
| 77 | + info = validateByteSource(info, 'info'); |
| 78 | + |
| 79 | + validateInteger(length, 'length', 0, kMaxLength); |
| 80 | + |
| 81 | + if (info.byteLength > 1024) { |
| 82 | + throw new ERR_OUT_OF_RANGE( |
| 83 | + 'info', |
| 84 | + 'must not contain more than 1024 bytes', |
| 85 | + info.byteLength); |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + hash, |
| 90 | + key, |
| 91 | + salt, |
| 92 | + info, |
| 93 | + length, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +function prepareKey(key: ArrayLike): ArrayLike { |
| 98 | + key = toBuf(key); |
| 99 | + |
| 100 | + if (!isAnyArrayBuffer(key) && !isArrayBufferView(key)) { |
| 101 | + throw new ERR_INVALID_ARG_TYPE( |
| 102 | + 'ikm', |
| 103 | + [ |
| 104 | + 'string', |
| 105 | + 'SecretKeyObject', |
| 106 | + 'ArrayBuffer', |
| 107 | + 'TypedArray', |
| 108 | + 'DataView', |
| 109 | + 'Buffer', |
| 110 | + ], |
| 111 | + key); |
| 112 | + } |
| 113 | + |
| 114 | + return key; |
| 115 | +} |
| 116 | + |
| 117 | +export function hkdf(hash: string, key: ArrayLike | KeyObject, salt: ArrayLike, info: ArrayLike, |
| 118 | + length: number, |
| 119 | + callback: (err: Error|null, derivedKey?: ArrayBuffer) => void): void { |
| 120 | + ({ |
| 121 | + hash, |
| 122 | + key, |
| 123 | + salt, |
| 124 | + info, |
| 125 | + length, |
| 126 | + } = validateParameters(hash, key, salt, info, length)); |
| 127 | + |
| 128 | + validateFunction(callback, 'callback'); |
| 129 | + |
| 130 | + new Promise<ArrayBuffer>((res, rej) => { |
| 131 | + try { |
| 132 | + res(cryptoImpl.getHkdf(hash, key as ArrayLike, salt, info, length)); |
| 133 | + } catch(err) { |
| 134 | + rej(err); |
| 135 | + } |
| 136 | + }).then((val: ArrayBuffer) => callback(null, val), (err) => callback(err)); |
| 137 | +} |
| 138 | + |
| 139 | +export function hkdfSync(hash: string, key: ArrayLike | KeyObject, salt: ArrayLike, |
| 140 | + info: ArrayLike, length: number): ArrayBuffer { |
| 141 | + ({ |
| 142 | + hash, |
| 143 | + key, |
| 144 | + salt, |
| 145 | + info, |
| 146 | + length, |
| 147 | + } = validateParameters(hash, key, salt, info, length)); |
| 148 | + |
| 149 | + return cryptoImpl.getHkdf(hash, key, salt, info, length); |
| 150 | +} |
0 commit comments