salty-crypto 0.3.0 → 1.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +3 -3
  2. package/browser-demo.html +1 -1
  3. package/dist/salty-crypto.d.mts +348 -0
  4. package/dist/salty-crypto.d.ts +36 -87
  5. package/dist/salty-crypto.global.js +2 -0
  6. package/dist/salty-crypto.global.js.map +1 -0
  7. package/dist/salty-crypto.js +2 -1
  8. package/dist/salty-crypto.js.map +1 -0
  9. package/dist/salty-crypto.mjs +2 -0
  10. package/dist/salty-crypto.mjs.map +1 -0
  11. package/lib/aead/chacha20poly1305.d.ts +2 -0
  12. package/lib/aead/chacha20poly1305.js +53 -0
  13. package/lib/aead/chacha20poly1305.js.map +1 -0
  14. package/lib/aead.d.ts +16 -0
  15. package/lib/aead.js +18 -0
  16. package/lib/aead.js.map +1 -0
  17. package/lib/bytes.d.ts +4 -0
  18. package/lib/bytes.js +27 -0
  19. package/lib/bytes.js.map +1 -0
  20. package/lib/cipher/chacha20.d.ts +4 -0
  21. package/lib/cipher/chacha20.js +84 -0
  22. package/lib/cipher/chacha20.js.map +1 -0
  23. package/lib/cipher.d.ts +9 -0
  24. package/lib/cipher.js +4 -0
  25. package/lib/cipher.js.map +1 -0
  26. package/lib/dh/x25519.d.ts +10 -0
  27. package/lib/dh/x25519.js +624 -0
  28. package/lib/dh/x25519.js.map +1 -0
  29. package/lib/dh.d.ts +11 -0
  30. package/lib/dh.js +17 -0
  31. package/lib/dh.js.map +1 -0
  32. package/lib/hash/blake2s.d.ts +18 -0
  33. package/lib/hash/blake2s.js +120 -0
  34. package/lib/hash/blake2s.js.map +1 -0
  35. package/lib/hash/poly1305.d.ts +18 -0
  36. package/lib/hash/poly1305.js +376 -0
  37. package/lib/hash/poly1305.js.map +1 -0
  38. package/lib/hash.d.ts +14 -0
  39. package/lib/hash.js +5 -0
  40. package/lib/hash.js.map +1 -0
  41. package/lib/hkdf.d.ts +6 -0
  42. package/lib/hkdf.js +17 -0
  43. package/lib/hkdf.js.map +1 -0
  44. package/lib/hmac.d.ts +6 -0
  45. package/lib/hmac.js +17 -0
  46. package/lib/hmac.js.map +1 -0
  47. package/lib/index.d.ts +45 -0
  48. package/lib/index.js +48 -0
  49. package/lib/index.js.map +1 -0
  50. package/lib/io.d.ts +4 -0
  51. package/lib/io.js +37 -0
  52. package/lib/io.js.map +1 -0
  53. package/lib/noise/algorithms.d.ts +15 -0
  54. package/lib/noise/algorithms.js +10 -0
  55. package/lib/noise/algorithms.js.map +1 -0
  56. package/lib/noise/cipherstate.d.ts +14 -0
  57. package/lib/noise/cipherstate.js +62 -0
  58. package/lib/noise/cipherstate.js.map +1 -0
  59. package/lib/noise/handshake.d.ts +52 -0
  60. package/lib/noise/handshake.js +191 -0
  61. package/lib/noise/handshake.js.map +1 -0
  62. package/lib/noise/patterns.d.ts +16 -0
  63. package/lib/noise/patterns.js +73 -0
  64. package/lib/noise/patterns.js.map +1 -0
  65. package/lib/noise/profiles.d.ts +2 -0
  66. package/lib/noise/profiles.js +11 -0
  67. package/lib/noise/profiles.js.map +1 -0
  68. package/lib/noise/rekey.d.ts +3 -0
  69. package/lib/noise/rekey.js +9 -0
  70. package/lib/noise/rekey.js.map +1 -0
  71. package/lib/noise.d.ts +6 -0
  72. package/lib/noise.js +8 -0
  73. package/lib/noise.js.map +1 -0
  74. package/lib/nonce.d.ts +9 -0
  75. package/lib/nonce.js +25 -0
  76. package/lib/nonce.js.map +1 -0
  77. package/lib/random.d.ts +2 -0
  78. package/lib/random.js +34 -0
  79. package/lib/random.js.map +1 -0
  80. package/package.json +38 -22
  81. package/src/.DS_Store +0 -0
  82. package/src/aead/chacha20poly1305.ts +7 -7
  83. package/src/aead.ts +3 -3
  84. package/src/bytes.ts +1 -1
  85. package/src/cipher/chacha20.ts +3 -3
  86. package/src/cipher.ts +3 -3
  87. package/src/dh/x25519.ts +1 -1
  88. package/src/dh.ts +3 -3
  89. package/src/hash/blake2s.ts +2 -2
  90. package/src/hash/poly1305.ts +2 -2
  91. package/src/hash.ts +3 -3
  92. package/src/hkdf.ts +3 -3
  93. package/src/hmac.ts +3 -3
  94. package/src/index.ts +23 -23
  95. package/src/io.ts +3 -0
  96. package/src/noise/algorithms.ts +7 -7
  97. package/src/noise/cipherstate.ts +4 -4
  98. package/src/noise/handshake.ts +8 -8
  99. package/src/noise/patterns.ts +1 -1
  100. package/src/noise/profiles.ts +5 -5
  101. package/src/noise/rekey.ts +3 -3
  102. package/src/noise.ts +1 -1
  103. package/src/nonce.ts +1 -1
  104. package/src/random.ts +2 -3
  105. package/test/{tests/aead.test.ts → aead.test.ts} +2 -2
  106. package/test/{tests/blake2.test.ts → blake2.test.ts} +2 -2
  107. package/test/{tests/chacha20.test.ts → chacha20.test.ts} +2 -2
  108. package/test/{tests/io.test.ts → io.test.ts} +2 -2
  109. package/test/{tests/noise.test.ts → noise.test.ts} +2 -2
  110. package/test/{tests/poly1305.test.ts → poly1305.test.ts} +2 -2
  111. package/tsup.config.ts +14 -0
  112. package/rollup.config.js +0 -19
  113. package/speed.ts +0 -22
  114. package/test/harness.ts +0 -74
  115. package/test/tsconfig.json +0 -18
  116. package/test-vectors/noise-c-basic.txt +0 -19684
  117. package/test-vectors/snow.txt +0 -10348
  118. package/watchall +0 -22
package/src/random.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// SPDX-License-Identifier: MIT
2
- /// SPDX-FileCopyrightText: Copyright © 2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
2
+ /// SPDX-FileCopyrightText: Copyright © 2023-2025 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
3
3
 
4
4
  // TypeScript port of the randomness-generation code from nacl-fast.js from tweetnacl.
5
5
  //
@@ -12,7 +12,6 @@
12
12
  // // See for details: http://tweetnacl.cr.yp.to/
13
13
 
14
14
  export const _randomBytes: (out: Uint8Array, n: number) => void = (() => {
15
- var crypto: any = typeof self !== 'undefined' ? (self.crypto || (self as any).msCrypto) : null;
16
15
  if (crypto && crypto.getRandomValues) {
17
16
  const QUOTA = 65536;
18
17
  return (x: Uint8Array, n: number) => {
@@ -21,7 +20,7 @@ export const _randomBytes: (out: Uint8Array, n: number) => void = (() => {
21
20
  }
22
21
  };
23
22
  } else if (typeof require !== 'undefined') {
24
- crypto = require('crypto');
23
+ const crypto = require('crypto');
25
24
  if (crypto && crypto.randomBytes) {
26
25
  return (x: Uint8Array, n: number) => x.set(crypto.randomBytes(n));
27
26
  }
@@ -1,5 +1,5 @@
1
- import { ChaCha20Poly1305_RFC8439, Nonce } from '../../dist/salty-crypto.js';
2
- import { it, expect } from '../harness';
1
+ import { ChaCha20Poly1305_RFC8439, Nonce } from '../dist/salty-crypto.js';
2
+ import { it, expect } from 'vitest';
3
3
 
4
4
  it('section 2.8.2 from rfc 8439', () => {
5
5
  const sunscreen_str = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.";
@@ -1,5 +1,5 @@
1
- import { BLAKE2s } from '../../dist/salty-crypto.js';
2
- import { it, expect } from '../harness';
1
+ import { BLAKE2s } from '../dist/salty-crypto.js';
2
+ import { it, expect } from 'vitest';
3
3
 
4
4
  it('Appendix B of RFC 7693', () => {
5
5
  expect(BLAKE2s.digest(new TextEncoder().encode("abc"))).toEqual(Uint8Array.from([
@@ -1,6 +1,6 @@
1
- import { ChaCha20, INTERNALS, Nonce } from '../../dist/salty-crypto.js';
1
+ import { ChaCha20, INTERNALS, Nonce } from '../dist/salty-crypto.js';
2
2
  const { chacha20_quarter_round, chacha20_block } = INTERNALS.cipher.chacha20;
3
- import { it, expect } from '../harness';
3
+ import { it, expect } from 'vitest';
4
4
 
5
5
  it('chacha20_quarter_round 1', () => {
6
6
  const s = new Uint32Array(4);
@@ -1,5 +1,5 @@
1
- import { IO } from '../../dist/salty-crypto.js';
2
- import { describe, it, expect } from '../harness';
1
+ import { IO } from '../dist/salty-crypto.js';
2
+ import { describe, it, expect } from 'vitest';
3
3
 
4
4
  describe('basic', async () => {
5
5
  const s =
@@ -8,8 +8,8 @@ import {
8
8
  isOneWay,
9
9
  lookupPattern,
10
10
  matchPattern,
11
- } from '../../dist/salty-crypto.js';
12
- import { describe, it, expect } from '../harness';
11
+ } from '../dist/salty-crypto.js';
12
+ import { describe, it, expect } from 'vitest';
13
13
 
14
14
  import fs from 'fs';
15
15
  import path from 'path';
@@ -1,6 +1,6 @@
1
- import { Poly1305 } from '../../dist/salty-crypto.js';
1
+ import { Poly1305 } from '../dist/salty-crypto.js';
2
2
 
3
- import { it, expect } from '../harness';
3
+ import { it, expect } from 'vitest';
4
4
 
5
5
  it('section 2.5.2 from rfc 8439', () => {
6
6
  const key = Uint8Array.from([
package/tsup.config.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: {
5
+ 'salty-crypto': 'src/index.ts',
6
+ },
7
+ format: ['cjs', 'esm', 'iife'],
8
+ globalName: 'SaltyCrypto',
9
+ dts: true,
10
+ sourcemap: true,
11
+ clean: true,
12
+ minify: true,
13
+ target: 'es2017',
14
+ });
package/rollup.config.js DELETED
@@ -1,19 +0,0 @@
1
- module.exports = [
2
- {
3
- input: 'lib/index.js',
4
- plugins: [require('@rollup/plugin-terser')()],
5
- output: {
6
- file: 'dist/salty-crypto.js',
7
- format: 'umd',
8
- name: 'SaltyCrypto',
9
- },
10
- },
11
- {
12
- input: 'lib/index.d.ts',
13
- plugins: [require('rollup-plugin-dts').default()],
14
- output: {
15
- file: 'dist/salty-crypto.d.ts',
16
- format: 'es',
17
- },
18
- },
19
- ];
package/speed.ts DELETED
@@ -1,22 +0,0 @@
1
- import { scalarMult } from './x25519';
2
- import { randomBytes } from './random';
3
-
4
- const N = 1000;
5
- const start = +new Date();
6
-
7
- let sum = 0;
8
- for (let i = 0; i < N; i++) {
9
- const sk = randomBytes(32);
10
- const pk = randomBytes(32);
11
- const dh = scalarMult(sk, pk);
12
- sum += dh[0];
13
- }
14
-
15
- const stop = +new Date();
16
- const delta = (stop - start) / 1000.0;
17
-
18
- console.log({
19
- sum,
20
- delta,
21
- rate: N / delta,
22
- });
package/test/harness.ts DELETED
@@ -1,74 +0,0 @@
1
- export { expect } from 'expect';
2
- import { JestAssertionError } from 'expect';
3
- import { glob } from 'glob';
4
- import path from 'path';
5
-
6
- export class Stats {
7
- total = 0;
8
- passed = 0;
9
- failed = 0;
10
- errors = 0;
11
-
12
- merge(s: Stats) {
13
- this.total += s.total;
14
- this.passed += s.passed;
15
- this.failed += s.failed;
16
- this.errors += s.errors;
17
- }
18
- }
19
-
20
- export let testDepth = 0;
21
- export let testStats = new Stats();
22
-
23
- function indent(msg: string) {
24
- let str = '';
25
- for (let i = 0; i < testDepth; i++) str = str + ' ';
26
- console.log(str + msg);
27
- }
28
-
29
- export async function describe(what: string, f: () => (Promise<void> | void)): Promise<void> {
30
- indent('- ' + what);
31
- testDepth++;
32
- await f();
33
- testDepth--;
34
- }
35
-
36
- export async function it(what: string, f: () => (Promise<void> | void)): Promise<void> {
37
- try {
38
- testStats.total++;
39
- await f();
40
- testStats.passed++;
41
- indent('✓ ' + what);
42
- } catch (exn) {
43
- if (exn instanceof JestAssertionError) {
44
- testStats.failed++;
45
- indent('\x1b[33m✗ ' + what + '\x1b[0m')
46
- console.error(`${'\x1b[31m'}${exn.message}${'\x1b[0m'}`);
47
- } else {
48
- testStats.errors++;
49
- throw exn;
50
- }
51
- }
52
- }
53
-
54
- export async function runTests(patterns: string[]): Promise<void> {
55
- for (const pattern of patterns) {
56
- const files = glob.sync(pattern, { nodir: true });
57
- for (const mod of files) {
58
- await describe(mod, async () => {
59
- await import(path.resolve(process.cwd(), mod));
60
- });
61
- }
62
- }
63
- }
64
-
65
- (async () => {
66
- if (Object.is(require.main, module)) {
67
- console.time('tests');
68
- let patterns = process.argv.slice(2);
69
- if (patterns.length === 0) patterns = ['./lib-test/**/*.test.js'];
70
- await runTests(patterns);
71
- console.timeEnd('tests');
72
- console.log(testStats);
73
- }
74
- })();
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2017",
4
- "lib": ["ES2017", "DOM"],
5
- "declaration": true,
6
- "baseUrl": ".",
7
- "rootDir": ".",
8
- "outDir": "../lib-test",
9
- "declarationDir": "../lib-test",
10
- "esModuleInterop": true,
11
- "moduleResolution": "node",
12
- "module": "esnext",
13
- "sourceMap": true,
14
- "strict": true,
15
- "allowJs": true
16
- },
17
- "include": ["**/*"]
18
- }