Import box functionality from tweetnacl (#1785)
* Import box functionality from tweetnacl * Remove box/boxOpen functionality * Additional comment * Import secretbox test vectors * Allow empty base64 input * Combine non-xor crypto_stream usage with xor * Adjust * inline let * Rename tests, extract test vectors * Adjust chnages comments * Remove box keypair * Remove rng set operation * Inline adjust * Verify adjustment
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { naclBoxPairFromSecret } from '../index.js';
|
||||
|
||||
describe('naclBoxPairFromSecret', (): void => {
|
||||
const secretKey = new Uint8Array([
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18
|
||||
]);
|
||||
|
||||
it('generates a valid publicKey/secretKey pair', (): void => {
|
||||
expect(
|
||||
naclBoxPairFromSecret(secretKey)
|
||||
).toEqual({
|
||||
publicKey: new Uint8Array([
|
||||
206, 110, 228, 222, 155, 43, 229, 254,
|
||||
38, 117, 157, 116, 224, 87, 139, 171,
|
||||
17, 195, 144, 167, 106, 36, 161, 28,
|
||||
25, 232, 127, 47, 219, 165, 44, 102
|
||||
]),
|
||||
secretKey
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../../types.js';
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclBoxPairFromSecret
|
||||
* @summary Creates a new public/secret box keypair from a secret.
|
||||
* @description
|
||||
* Returns a object containing a box `publicKey` & `secretKey` generated from the supplied secret.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclBoxPairFromSecret } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclBoxPairFromSecret(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclBoxPairFromSecret (secret: Uint8Array): Keypair {
|
||||
return nacl.box.keyPair.fromSecretKey(secret.slice(0, 32));
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
import { naclSecretboxOpen } from './tweetnacl.js';
|
||||
|
||||
/**
|
||||
* @name naclDecrypt
|
||||
@@ -18,5 +18,5 @@ import nacl from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function naclDecrypt (encrypted: Uint8Array, nonce: Uint8Array, secret: Uint8Array): Uint8Array | null {
|
||||
return nacl.secretbox.open(encrypted, nonce, secret) || null;
|
||||
return naclSecretboxOpen(encrypted, nonce, secret);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a.js';
|
||||
import { naclSecretbox } from './tweetnacl.js';
|
||||
|
||||
interface Encrypted {
|
||||
encrypted: Uint8Array;
|
||||
@@ -26,7 +25,7 @@ interface Encrypted {
|
||||
*/
|
||||
export function naclEncrypt (message: Uint8Array, secret: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Encrypted {
|
||||
return {
|
||||
encrypted: nacl.secretbox(message, nonce, secret),
|
||||
encrypted: naclSecretbox(message, nonce, secret),
|
||||
nonce
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,6 +6,3 @@
|
||||
*/
|
||||
export { naclDecrypt } from './decrypt.js';
|
||||
export { naclEncrypt } from './encrypt.js';
|
||||
export { naclBoxPairFromSecret } from './box/fromSecret.js';
|
||||
export { naclOpen } from './open.js';
|
||||
export { naclSeal } from './seal.js';
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { ed25519PairFromString } from '../ed25519/index.js';
|
||||
import { keyExtractPath, keyFromPath } from '../key/index.js';
|
||||
import { naclBoxPairFromSecret, naclOpen, naclSeal } from './index.js';
|
||||
|
||||
describe('naclOpen', (): void => {
|
||||
it('opens a sealed message by the sender', (): void => {
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const sender = ed25519PairFromString('sender');
|
||||
const receiver = ed25519PairFromString('receiver');
|
||||
const senderBox = naclBoxPairFromSecret(sender.secretKey);
|
||||
const receiverBox = naclBoxPairFromSecret(receiver.secretKey);
|
||||
const { nonce, sealed } = naclSeal(message, senderBox.secretKey, receiverBox.publicKey);
|
||||
|
||||
expect(
|
||||
naclOpen(sealed, nonce, senderBox.publicKey, receiverBox.secretKey)
|
||||
).toEqual(
|
||||
message
|
||||
);
|
||||
});
|
||||
|
||||
it('polkadot does double ratchet', () => {
|
||||
const sender = ed25519PairFromString('sender');
|
||||
const receiver = ed25519PairFromString('receiver');
|
||||
|
||||
// Make id key for identifying each other
|
||||
const senderIdKey = keyFromPath(sender, keyExtractPath('//1//1//1//1').path, 'ed25519');
|
||||
const receiverIdKey = keyFromPath(receiver, keyExtractPath('//2//2//2//2').path, 'ed25519');
|
||||
|
||||
// Receiver sends encrypting public key to receive message to decrypt with his private key
|
||||
const receiverIdBoxKey = naclBoxPairFromSecret(receiverIdKey.secretKey);
|
||||
|
||||
// console.log(`Receiver sends receiver's public key to sender ${receiverIdBoxKey.publicKey.toString()}`);
|
||||
|
||||
// Sender encrypts message to send with the public key the receiver sent and send it to receiver
|
||||
const senderIdBoxKey = naclBoxPairFromSecret(senderIdKey.secretKey);
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const { nonce, sealed } = naclSeal(message, senderIdBoxKey.secretKey, receiverIdBoxKey.publicKey);
|
||||
|
||||
// console.log(`Sender sends encrypted message to receiver ${sealed.toString()}, ${nonce.toString()}`);
|
||||
|
||||
// Receiver opens encrypted message from the sender
|
||||
let opened = naclOpen(sealed, nonce, senderIdBoxKey.publicKey, receiverIdBoxKey.secretKey);
|
||||
|
||||
// console.log(`The sealed message is ${opened?.toString() || 'null'}`);
|
||||
|
||||
// Oh no the Id key is hacked, derive Id key again from another index
|
||||
const senderIdKey2 = keyFromPath(sender, keyExtractPath('//1//1//1//2').path, 'ed25519');
|
||||
const receiverIdKey2 = keyFromPath(receiver, keyExtractPath('//2//2//2//3').path, 'ed25519');
|
||||
|
||||
// Receiver sends encrypting public key to receive message to decrypt with his private key
|
||||
const receiverIdBoxKey2 = naclBoxPairFromSecret(receiverIdKey2.secretKey);
|
||||
|
||||
// console.log(`Receiver sends receiver's public key to sender ${receiverIdBoxKey2.publicKey.toString()}`);
|
||||
|
||||
// Sender encrypts message to send with the public key the receiver sent and send it to receiver
|
||||
const senderIdBoxKey2 = naclBoxPairFromSecret(senderIdKey2.secretKey);
|
||||
const message2 = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
let ctx = naclSeal(message2, senderIdBoxKey2.secretKey, receiverIdBoxKey2.publicKey);
|
||||
const sealed2 = ctx.sealed;
|
||||
const nonce2 = ctx.nonce;
|
||||
|
||||
// console.log(`Sender sends encrypted message to receiver ${sealed.toString()}, ${nonce.toString()}`);
|
||||
|
||||
// Receiver opens encrypted message from the sender
|
||||
opened = naclOpen(sealed2, nonce2, senderIdBoxKey2.publicKey, receiverIdBoxKey2.secretKey);
|
||||
// console.log(`The sealed message is ${opened?.toString() || 'null'}`);
|
||||
|
||||
// This time derive key for each message to send and receive
|
||||
// Receiver sends encrypting public key to receive message to decrypt with his private key
|
||||
const receiverIdBoxKey2One = naclBoxPairFromSecret(receiverIdBoxKey2.secretKey);
|
||||
|
||||
// console.log(`Receiver sends receiver's public key to sender ${receiverIdBoxKey2One.publicKey.toString()}`);
|
||||
|
||||
// Sender encrypts message to send with the public key the receiver sent and send it to receiver
|
||||
const senderIdBoxKey2One = naclBoxPairFromSecret(senderIdBoxKey2.secretKey);
|
||||
const message3 = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
|
||||
ctx = naclSeal(message3, senderIdBoxKey2One.secretKey, receiverIdBoxKey2One.publicKey);
|
||||
|
||||
// const sealed3 = ctx.sealed;
|
||||
// const nonce3 = ctx.nonce;
|
||||
|
||||
// console.log(`Sender sends encrypted message to receiver ${sealed3.toString()}, ${nonce3.toString()}`);
|
||||
|
||||
// Receiver opens encrypted message from the sender
|
||||
opened = naclOpen(sealed2, nonce2, senderIdBoxKey2One.publicKey, receiverIdBoxKey2One.secretKey);
|
||||
|
||||
expect(opened).toEqual(message3);
|
||||
// console.log(`The sealed message is ${opened?.toString() || 'null'}`);
|
||||
});
|
||||
|
||||
it('returns null on invalid', (): void => {
|
||||
expect(
|
||||
naclOpen(new Uint8Array(), new Uint8Array(24), new Uint8Array(32), new Uint8Array(32))
|
||||
).toEqual(null);
|
||||
});
|
||||
});
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclOpen
|
||||
* @summary Opens a message using the receiver's secretKey and nonce
|
||||
* @description
|
||||
* Returns a message sealed by the sender, using the receiver's `secret` and `nonce`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclOpen } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclOpen([...], [...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclOpen (sealed: Uint8Array, nonce: Uint8Array, senderBoxPublic: Uint8Array, receiverBoxSecret: Uint8Array): Uint8Array | null {
|
||||
return nacl.box.open(sealed, nonce, senderBoxPublic, receiverBoxSecret) || null;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { ed25519PairFromString } from '../ed25519/index.js';
|
||||
import { naclBoxPairFromSecret, naclSeal } from './index.js';
|
||||
|
||||
describe('naclSeal', (): void => {
|
||||
it('seals a message', (): void => {
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const sender = ed25519PairFromString('sender');
|
||||
const receiver = ed25519PairFromString('receiver');
|
||||
const senderBox = naclBoxPairFromSecret(sender.secretKey);
|
||||
const receiverBox = naclBoxPairFromSecret(receiver.secretKey);
|
||||
const senderBoxSecret = senderBox.secretKey;
|
||||
const receiverBoxPublic = receiverBox.publicKey;
|
||||
|
||||
expect(
|
||||
naclSeal(message, senderBoxSecret, receiverBoxPublic, new Uint8Array(24))
|
||||
).toEqual({
|
||||
nonce: new Uint8Array(24),
|
||||
sealed: new Uint8Array([137, 195, 104, 255, 67, 199, 40, 235, 148, 177, 180, 73, 239, 131, 215, 80, 61, 191, 167, 213, 152, 138, 5, 47, 135])
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,32 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a.js';
|
||||
|
||||
interface Sealed {
|
||||
sealed: Uint8Array;
|
||||
nonce: Uint8Array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name naclSeal
|
||||
* @summary Seals a message using the sender's encrypting secretKey, receiver's public key, and nonce
|
||||
* @description
|
||||
* Returns an encrypted message which can be open only by receiver's secretKey. If the `nonce` was not supplied, a random value is generated.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclSeal } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclSeal([...], [...], [...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclSeal (message: Uint8Array, senderBoxSecret: Uint8Array, receiverBoxPublic: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Sealed {
|
||||
return {
|
||||
nonce,
|
||||
sealed: nacl.box(message, nonce, receiverBoxPublic, senderBoxSecret)
|
||||
};
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,161 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
// Adapted from https://github.com/dchest/tweetnacl-js/blob/6a9594a35a27f9c723c5f1c107e376d1c65c23b3/test/04-secretbox.quick.js
|
||||
// Adapted from https://github.com/dchest/tweetnacl-js/blob/6a9594a35a27f9c723c5f1c107e376d1c65c23b3/test/04-secretbox.js
|
||||
//
|
||||
// Changes made:
|
||||
// - Jest-like test environment (not tape)
|
||||
// - Combine quite and random tests into single file
|
||||
//
|
||||
// Original headers:
|
||||
//
|
||||
// Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
|
||||
// Public domain.
|
||||
//
|
||||
// Implementation derived from TweetNaCl version 20140427.
|
||||
// See for details: http://tweetnacl.cr.yp.to/
|
||||
|
||||
import { stringToU8a, u8aEq } from '@polkadot/util';
|
||||
|
||||
import { base64Decode } from '../base64/index.js';
|
||||
import { naclSecretbox, naclSecretboxOpen } from './tweetnacl.js';
|
||||
import { TEST_DATA } from './tweetnacl-secretbox-data.spec.js';
|
||||
|
||||
describe('tweetnacl/secretbox', (): void => {
|
||||
describe('tweetnacl/test/04-secretbox.quick.js', (): void => {
|
||||
it('naclSecretbox and naclSecretboxOpen', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
key[i] = i & 0xff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < nonce.length; i++) {
|
||||
nonce[i] = (32 + i) & 0xff;
|
||||
}
|
||||
|
||||
const msg = stringToU8a('message to encrypt');
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const openedMsg = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!openedMsg && u8aEq(openedMsg, msg)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('naclSecretbox.open with invalid box', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(new Uint8Array(0), nonce, key)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(new Uint8Array(10), nonce, key)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(new Uint8Array(100), nonce, key)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('naclSecretbox.open with invalid nonce', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
|
||||
for (let i = 0; i < nonce.length; i++) {
|
||||
nonce[i] = i & 0xff;
|
||||
}
|
||||
|
||||
const msg = stringToU8a('message to encrypt');
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const unbox = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
|
||||
nonce[0] = 255;
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(box, nonce, key)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('naclSecretbox.open with invalid key', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
key[i] = i & 0xff;
|
||||
}
|
||||
|
||||
const nonce = new Uint8Array(24);
|
||||
const msg = stringToU8a('message to encrypt');
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const unbox = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
|
||||
key[0] = 255;
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(box, nonce, key)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('naclSecretbox with message lengths of 0 to 1024', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
const fullMsg = new Uint8Array(1024);
|
||||
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
key[i] = i & 0xff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < fullMsg.length; i++) {
|
||||
fullMsg[i] = i & 0xff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < fullMsg.length; i++) {
|
||||
const msg = fullMsg.subarray(0, i);
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const unbox = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('tweetnacl/test/04-secretbox.js', (): void => {
|
||||
for (let i = 0; i < TEST_DATA.length; i++) {
|
||||
const [keyBase64, nonceBase64, msgBase64, expBase64] = TEST_DATA[i];
|
||||
|
||||
it(`is ok on "${msgBase64}"`, (): void => {
|
||||
const key = base64Decode(keyBase64);
|
||||
const nonce = base64Decode(nonceBase64);
|
||||
const msg = base64Decode(msgBase64);
|
||||
const goodBox = base64Decode(expBase64);
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
|
||||
expect(
|
||||
u8aEq(box, goodBox)
|
||||
).toBe(true);
|
||||
|
||||
const unbox = naclSecretboxOpen(goodBox, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,20 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable */
|
||||
/* eslint-disable brace-style,camelcase,comma-spacing,curly,one-var,padding-line-between-statements,space-infix-ops */
|
||||
|
||||
// Imported from https://github.com/dchest/tweetnacl-js/blob/6a9594a35a27f9c723c5f1c107e376d1c65c23b3/nacl.js
|
||||
// Adapted from https://github.com/dchest/tweetnacl-js/blob/6a9594a35a27f9c723c5f1c107e376d1c65c23b3/nacl.js
|
||||
//
|
||||
// Changes made:
|
||||
//
|
||||
// - Added TS types
|
||||
// - Change var to let/const
|
||||
// - Comment out unused functions
|
||||
// - export the only box/secretbox create/open functions (these are camelCase)
|
||||
// - Linting style is mostly disabled below (apart form the changes above), so should be verifyable against original
|
||||
// - Inline some calls (flatten, reduce call tree)
|
||||
// - Some inline let definitions on loop variables
|
||||
// - It is _messy_ the unused code is commented out, not removed
|
||||
//
|
||||
// Original headers:
|
||||
//
|
||||
@@ -71,9 +82,9 @@ function vn (x: Uint8Array, xi: number, y: Uint8Array, yi: number, n: number): n
|
||||
return (1 & ((d - 1) >>> 8)) - 1;
|
||||
}
|
||||
|
||||
function crypto_verify_16 (x: Uint8Array, xi: number, y: Uint8Array, yi: number): number {
|
||||
return vn(x,xi,y,yi,16);
|
||||
}
|
||||
// function crypto_verify_16 (x: Uint8Array, xi: number, y: Uint8Array, yi: number): number {
|
||||
// return vn(x,xi,y,yi,16);
|
||||
// }
|
||||
|
||||
// function crypto_verify_32(x, xi, y, yi) {
|
||||
// return vn(x,xi,y,yi,32);
|
||||
@@ -119,18 +130,18 @@ function core (out: Uint8Array, inp: Uint8Array, k: Uint8Array, c: Uint8Array, h
|
||||
}
|
||||
}
|
||||
|
||||
function crypto_core_salsa20 (out: Uint8Array, inp: Uint8Array, k: Uint8Array, c: Uint8Array): number {
|
||||
core(out,inp,k,c,false);
|
||||
return 0;
|
||||
}
|
||||
// function crypto_core_salsa20 (out: Uint8Array, inp: Uint8Array, k: Uint8Array, c: Uint8Array): number {
|
||||
// core(out,inp,k,c,false);
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
function crypto_core_hsalsa20 (out: Uint8Array, inp: Uint8Array, k: Uint8Array, c: Uint8Array): number {
|
||||
core(out,inp,k,c,true);
|
||||
return 0;
|
||||
}
|
||||
// function crypto_core_hsalsa20 (out: Uint8Array, inp: Uint8Array, k: Uint8Array, c: Uint8Array): number {
|
||||
// core(out,inp,k,c,true);
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
const sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
|
||||
// "expand 32-byte k"
|
||||
// "expand 32-byte k"
|
||||
|
||||
function crypto_stream_salsa20_xor (c: Uint8Array, cpos: number, m: Uint8Array | null, mpos: number, b: number, n: Uint8Array, k: Uint8Array): number {
|
||||
const z = new Uint8Array(16), x = new Uint8Array(64);
|
||||
@@ -139,7 +150,7 @@ function crypto_stream_salsa20_xor (c: Uint8Array, cpos: number, m: Uint8Array |
|
||||
for (i = 0; i < 16; i++) z[i] = 0;
|
||||
for (i = 0; i < 8; i++) z[i] = n[i];
|
||||
while (b >= 64) {
|
||||
crypto_core_salsa20(x,z,k,sigma);
|
||||
core(x, z, k, sigma, false);
|
||||
for (i = 0; i < 64; i++) c[cpos+i] = (m?m[mpos+i]:0) ^ x[i];
|
||||
u = 1;
|
||||
for (i = 8; i < 16; i++) {
|
||||
@@ -152,25 +163,25 @@ function crypto_stream_salsa20_xor (c: Uint8Array, cpos: number, m: Uint8Array |
|
||||
if (m) mpos += 64;
|
||||
}
|
||||
if (b > 0) {
|
||||
crypto_core_salsa20(x,z,k,sigma);
|
||||
core(x, z, k, sigma, false);
|
||||
for (i = 0; i < b; i++) c[cpos+i] = (m?m[mpos+i]:0) ^ x[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function crypto_stream_salsa20 (c: Uint8Array, cpos: number, d: number, n: Uint8Array, k: Uint8Array): number {
|
||||
return crypto_stream_salsa20_xor(c, cpos, null, 0, d, n, k);
|
||||
}
|
||||
// function crypto_stream_salsa20 (c: Uint8Array, cpos: number, d: number, n: Uint8Array, k: Uint8Array): number {
|
||||
// return crypto_stream_salsa20_xor(c, cpos, null, 0, d, n, k);
|
||||
// }
|
||||
|
||||
function crypto_stream (c: Uint8Array, cpos: number, d: number, n: Uint8Array, k: Uint8Array): number {
|
||||
const s = new Uint8Array(32);
|
||||
crypto_core_hsalsa20(s,n,k,sigma);
|
||||
return crypto_stream_salsa20(c,cpos,d,n.subarray(16),s);
|
||||
}
|
||||
// function crypto_stream (c: Uint8Array, cpos: number, d: number, n: Uint8Array, k: Uint8Array): number {
|
||||
// const s = new Uint8Array(32);
|
||||
// crypto_core_hsalsa20(s,n,k,sigma);
|
||||
// return crypto_stream_salsa20(c,cpos,d,n.subarray(16),s);
|
||||
// }
|
||||
|
||||
function crypto_stream_xor (c: Uint8Array, cpos: number, m: Uint8Array | null, mpos: number, d: number, n: Uint8Array, k: Uint8Array): number {
|
||||
const s = new Uint8Array(32);
|
||||
crypto_core_hsalsa20(s,n,k,sigma);
|
||||
core(s, n, k, sigma, true);
|
||||
return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, n.subarray(16), s);
|
||||
}
|
||||
|
||||
@@ -240,7 +251,7 @@ function crypto_onetimeauth (out: Uint8Array, outpos: number, m: Uint8Array, mpo
|
||||
function crypto_onetimeauth_verify (h: Uint8Array, hpos: number, m: Uint8Array, mpos: number, n: number, k: Uint8Array) {
|
||||
const x = new Uint8Array(16);
|
||||
crypto_onetimeauth(x,0,m,mpos,n,k);
|
||||
return crypto_verify_16(h,hpos,x,0);
|
||||
return vn(h, hpos, x, 0, 16);
|
||||
}
|
||||
|
||||
function crypto_secretbox (c: Uint8Array, m: Uint8Array, d: number, n: Uint8Array, k: Uint8Array) {
|
||||
@@ -254,7 +265,7 @@ function crypto_secretbox (c: Uint8Array, m: Uint8Array, d: number, n: Uint8Arra
|
||||
function crypto_secretbox_open (m: Uint8Array, c: Uint8Array, d: number, n: Uint8Array, k: Uint8Array): number {
|
||||
const x = new Uint8Array(32);
|
||||
if (d < 32) return -1;
|
||||
crypto_stream(x,0,32,n,k);
|
||||
crypto_stream_xor(x, 0, null, 0, 32, n, k);
|
||||
if (crypto_onetimeauth_verify(c, 16,c, 32,d - 32,x) !== 0) return -1;
|
||||
crypto_stream_xor(m,0,c,0,d,n,k);
|
||||
for (let i = 0; i < 32; i++) m[i] = 0;
|
||||
@@ -963,7 +974,7 @@ function checkArrayTypes (...args: unknown[]): void {
|
||||
// return b;
|
||||
// };
|
||||
|
||||
nacl.secretbox = function (msg: Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array {
|
||||
export function naclSecretbox (msg: Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array {
|
||||
checkArrayTypes(msg, nonce, key);
|
||||
checkLengths(key, nonce);
|
||||
const m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
|
||||
@@ -973,7 +984,7 @@ nacl.secretbox = function (msg: Uint8Array, nonce: Uint8Array, key: Uint8Array):
|
||||
return c.subarray(crypto_secretbox_BOXZEROBYTES);
|
||||
}
|
||||
|
||||
nacl.secretbox.open = function (box: Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array | null {
|
||||
export function naclSecretboxOpen (box: Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array | null {
|
||||
checkArrayTypes(box, nonce, key);
|
||||
checkLengths(key, nonce);
|
||||
const c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
|
||||
|
||||
Reference in New Issue
Block a user