Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/pkcs7-padding.js
Show All 10 Lines | |||||
* to the standard PKCS#7 padding, but operates on the block basis instead of | * to the standard PKCS#7 padding, but operates on the block basis instead of | ||||
* the byte basis. The fill value of the padding is the number of blocks added. | * the byte basis. The fill value of the padding is the number of blocks added. | ||||
* | * | ||||
* The PKCS#7 padding is described in RFC 5652, section 6.3. | * The PKCS#7 padding is described in RFC 5652, section 6.3. | ||||
*/ | */ | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
export type PaddingConfiguration = { | |||||
+blockSizeBytes: number, | |||||
+superblockSizeBlocks: number, | |||||
}; | |||||
/** | |||||
* The padding configuration for 10KB superblocks. | |||||
* The block size is 250 bytes, and the superblock size is 40 blocks. | |||||
*/ | |||||
const PKCS7_10KB: PaddingConfiguration = { | |||||
blockSizeBytes: 250, | |||||
superblockSizeBlocks: 40, | |||||
}; | |||||
/** | |||||
* Pads the input using the extended PKCS#7 padding (superblock padding). | |||||
* The input is first padded using the standard PKCS#7 padding, and then | |||||
* padded to the nearest multiple of superblocks (blocks of blocks). | |||||
* | |||||
* @param {Uint8Array} data - The input to be padded. | |||||
* @param {PaddingConfiguration} paddingConfiguration - The padding | |||||
* configuration. Defaults to multiple of 10KB. See {@link PKCS7_10KB}. | |||||
* @returns {Uint8Array} The padded input. | |||||
*/ | |||||
function pad( | |||||
data: Uint8Array, | |||||
{ blockSizeBytes, superblockSizeBlocks }: PaddingConfiguration = PKCS7_10KB, | |||||
): Uint8Array { | |||||
const pkcs7Padded = pkcs7pad(data, blockSizeBytes); | |||||
return superblockPad(pkcs7Padded, blockSizeBytes, superblockSizeBlocks); | |||||
} | |||||
/** | |||||
* Unpads the input using the extended PKCS#7 padding (superblock padding). | |||||
* The input is first unpadded on the block basis, and then unpadded using | |||||
* the standard PKCS#7 padding. | |||||
* | |||||
* @param {Uint8Array} data - The input to be unpadded. | |||||
* @param {PaddingConfiguration} paddingConfiguration - The padding | |||||
* configuration. Defaults to multiple of 10KB. See {@link PKCS7_10KB}. | |||||
* @returns {Uint8Array} The unpadded input. | |||||
*/ | |||||
function unpad( | |||||
data: Uint8Array, | |||||
{ blockSizeBytes }: PaddingConfiguration = PKCS7_10KB, | |||||
): Uint8Array { | |||||
const blockUnpadded = superblockUnpad(data, blockSizeBytes); | |||||
return pkcs7unpad(blockUnpadded); | |||||
} | |||||
/** | /** | ||||
* PKCS#7 padding function for `Uint8Array` data. | * PKCS#7 padding function for `Uint8Array` data. | ||||
* | * | ||||
* @param {Uint8Array} data - The data to be padded. | * @param {Uint8Array} data - The data to be padded. | ||||
* @param {number} blockSizeBytes - The block size in bytes. | * @param {number} blockSizeBytes - The block size in bytes. | ||||
* @returns {Uint8Array} The padded data as a new Uint8Array. | * @returns {Uint8Array} The padded data as a new Uint8Array. | ||||
*/ | */ | ||||
function pkcs7pad(data: Uint8Array, blockSizeBytes: number): Uint8Array { | function pkcs7pad(data: Uint8Array, blockSizeBytes: number): Uint8Array { | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | invariant( | ||||
data.subarray(unpaddedLengthBytes).every(x => x === paddingBlocks), | data.subarray(unpaddedLengthBytes).every(x => x === paddingBlocks), | ||||
'invalid padding', | 'invalid padding', | ||||
); | ); | ||||
const unpaddedData = data.subarray(0, unpaddedLengthBytes); | const unpaddedData = data.subarray(0, unpaddedLengthBytes); | ||||
return unpaddedData; | return unpaddedData; | ||||
} | } | ||||
export { pkcs7pad, pkcs7unpad, superblockPad, superblockUnpad }; | export { PKCS7_10KB, pad, unpad }; | ||||
// exported for testing purposes only | |||||
export const testing = { | |||||
pkcs7pad, | |||||
pkcs7unpad, | |||||
superblockPad, | |||||
superblockUnpad, | |||||
}; |