crypto: convert to buffer before using input length

This commit is contained in:
Abdullah Atta
2023-06-23 11:28:35 +05:00
committed by Abdullah Atta
parent 9096e7711d
commit 10fafa885a

View File

@@ -155,12 +155,13 @@ export function crypto_aead_xchacha20poly1305_ietf_encrypt(
key: Uint8Array, key: Uint8Array,
outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null
): string | Uint8Array { ): string | Uint8Array {
const m = toBuffer(message);
return wrap( return wrap(
message.length + crypto_aead_xchacha20poly1305_ietf_ABYTES, m.byteLength + crypto_aead_xchacha20poly1305_ietf_ABYTES,
(output) => (output) =>
sodium_native_crypto_aead_xchacha20poly1305_ietf_encrypt( sodium_native_crypto_aead_xchacha20poly1305_ietf_encrypt(
output, output,
toBuffer(message), m,
toBuffer(additional_data) || null, toBuffer(additional_data) || null,
null, null,
toBuffer(public_nonce), toBuffer(public_nonce),
@@ -182,9 +183,7 @@ export function crypto_secretstream_xchacha20poly1305_init_push(
key: Uint8Array, key: Uint8Array,
outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null
): { state: StateAddress; header: string | Uint8Array } { ): { state: StateAddress; header: string | Uint8Array } {
const state = Buffer.allocUnsafe( const state = Buffer.alloc(crypto_secretstream_xchacha20poly1305_STATEBYTES);
crypto_secretstream_xchacha20poly1305_STATEBYTES
);
const header = wrap( const header = wrap(
crypto_secretstream_xchacha20poly1305_HEADERBYTES, crypto_secretstream_xchacha20poly1305_HEADERBYTES,
@@ -220,13 +219,14 @@ export function crypto_secretstream_xchacha20poly1305_push(
tag: number, tag: number,
outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null
): string | Uint8Array { ): string | Uint8Array {
const message = toBuffer(message_chunk);
return wrap( return wrap(
message_chunk.length + crypto_secretstream_xchacha20poly1305_ABYTES, message.byteLength + crypto_secretstream_xchacha20poly1305_ABYTES,
(cipher) => (cipher) =>
sodium_native_crypto_secretstream_xchacha20poly1305_push( sodium_native_crypto_secretstream_xchacha20poly1305_push(
state_address as unknown as Buffer, state_address as unknown as Buffer,
cipher, cipher,
toBuffer(message_chunk), message,
toBuffer(ad) || null, toBuffer(ad) || null,
tag tag
), ),
@@ -258,13 +258,14 @@ export function crypto_aead_xchacha20poly1305_ietf_decrypt(
key: Uint8Array, key: Uint8Array,
outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null
): string | Uint8Array { ): string | Uint8Array {
const cipher = toBuffer(ciphertext);
return wrap( return wrap(
ciphertext.length - crypto_aead_xchacha20poly1305_ietf_ABYTES, cipher.byteLength - crypto_aead_xchacha20poly1305_ietf_ABYTES,
(message) => (message) =>
sodium_native_crypto_aead_xchacha20poly1305_ietf_decrypt( sodium_native_crypto_aead_xchacha20poly1305_ietf_decrypt(
message, message,
null, null,
toBuffer(ciphertext), cipher,
toBuffer(additional_data) || null, toBuffer(additional_data) || null,
toBuffer(public_nonce), toBuffer(public_nonce),
toBuffer(key) toBuffer(key)
@@ -277,9 +278,7 @@ export function crypto_secretstream_xchacha20poly1305_init_pull(
header: Uint8Array, header: Uint8Array,
key: Uint8Array key: Uint8Array
): StateAddress { ): StateAddress {
const state = Buffer.allocUnsafe( const state = Buffer.alloc(crypto_secretstream_xchacha20poly1305_STATEBYTES);
crypto_secretstream_xchacha20poly1305_STATEBYTES
);
sodium_native_crypto_secretstream_xchacha20poly1305_init_pull( sodium_native_crypto_secretstream_xchacha20poly1305_init_pull(
state, state,
toBuffer(header), toBuffer(header),
@@ -302,19 +301,20 @@ export function crypto_secretstream_xchacha20poly1305_pull(
): StringMessageTag; ): StringMessageTag;
export function crypto_secretstream_xchacha20poly1305_pull( export function crypto_secretstream_xchacha20poly1305_pull(
state_address: StateAddress, state_address: StateAddress,
cipher: string | Uint8Array, ciphertext: string | Uint8Array,
ad?: string | Uint8Array | null, ad?: string | Uint8Array | null,
outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null
): MessageTag | StringMessageTag { ): MessageTag | StringMessageTag {
const tag = Buffer.alloc(crypto_secretstream_xchacha20poly1305_TAGBYTES); const tag = Buffer.alloc(crypto_secretstream_xchacha20poly1305_TAGBYTES);
const cipher = toBuffer(ciphertext);
const message = wrap( const message = wrap(
cipher.length - crypto_secretstream_xchacha20poly1305_ABYTES, cipher.byteLength - crypto_secretstream_xchacha20poly1305_ABYTES,
(message) => (message) =>
sodium_native_crypto_secretstream_xchacha20poly1305_pull( sodium_native_crypto_secretstream_xchacha20poly1305_pull(
state_address as unknown as Buffer, state_address as unknown as Buffer,
message, message,
tag, tag,
toBuffer(cipher), cipher,
toBuffer(ad) || null toBuffer(ad) || null
), ),
outputFormat outputFormat
@@ -425,7 +425,7 @@ function wrap(
action: (buffer: Buffer) => void, action: (buffer: Buffer) => void,
outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null outputFormat?: StringOutputFormat | Uint8ArrayOutputFormat | null
): string | Uint8Array { ): string | Uint8Array {
const output = Buffer.allocUnsafe(length); const output = Buffer.alloc(length);
action(output); action(output);