@li0ard/dstu 0.1.8 → 0.2.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 (53) hide show
  1. package/README.md +4 -2
  2. package/dstu4145/ec/index.d.ts +4 -0
  3. package/dstu4145/ec/index.js +19 -0
  4. package/dstu4145/ec/wnaf.d.ts +2 -1
  5. package/dstu4145/ecies.d.ts +82 -0
  6. package/dstu4145/ecies.js +72 -0
  7. package/dstu4145/gf2m/index.js +2 -11
  8. package/dstu4145/gf2m/onb.js +30 -16
  9. package/dstu4145/gf2m/utils.d.ts +1 -1
  10. package/dstu4145/gf2m/utils.js +12 -8
  11. package/dstu4145/index.d.ts +16 -225
  12. package/dstu4145/index.js +20 -26
  13. package/dstu9041/curve.d.ts +4 -12
  14. package/dstu9041/curve.js +5 -3
  15. package/dstu9041/index.d.ts +2 -8
  16. package/dstu9041/index.js +10 -6
  17. package/dstu9311/hash.d.ts +46 -12
  18. package/dstu9311/hash.js +89 -28
  19. package/dstu9311/index.js +4 -5
  20. package/iso15946.d.ts +3 -0
  21. package/iso15946.js +56 -0
  22. package/kalyna/index.d.ts +1 -1
  23. package/kalyna/index.js +6 -6
  24. package/keywrap.js +6 -3
  25. package/kupyna/index.d.ts +116 -67
  26. package/kupyna/index.js +122 -61
  27. package/kupyna/kmac.d.ts +6 -6
  28. package/kupyna/kmac.js +20 -15
  29. package/modes/cbc.d.ts +1 -1
  30. package/modes/cbc.js +5 -7
  31. package/modes/ccm.js +12 -3
  32. package/modes/cfb.js +42 -51
  33. package/modes/cmac.js +4 -1
  34. package/modes/ctr.js +4 -3
  35. package/modes/ecb.js +2 -2
  36. package/modes/gcm.d.ts +4 -1
  37. package/modes/gcm.js +32 -5
  38. package/modes/gmac.js +4 -1
  39. package/modes/index.d.ts +1 -1
  40. package/modes/index.js +1 -1
  41. package/modes/kw.d.ts +2 -6
  42. package/modes/kw.js +9 -16
  43. package/modes/ofb.js +3 -3
  44. package/modes/xts.d.ts +2 -6
  45. package/modes/xts.js +7 -3
  46. package/package.json +4 -4
  47. package/padding.d.ts +1 -1
  48. package/padding.js +5 -0
  49. package/strumok/index.d.ts +3 -2
  50. package/strumok/index.js +4 -2
  51. package/types.d.ts +77 -0
  52. package/utils.d.ts +1 -0
  53. package/utils.js +14 -1
package/types.d.ts CHANGED
@@ -12,6 +12,16 @@ export type Cipher = {
12
12
  /** Decrypt block */
13
13
  decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
14
14
  };
15
+ export type StreamCipher = {
16
+ /** Block size */
17
+ readonly blockSize: number;
18
+ /** Key size */
19
+ readonly keySize: number;
20
+ /** Generate keystream word */
21
+ next_stream(): TRet<Uint8Array>;
22
+ /** Perform encryption/decryption */
23
+ crypt(msg: TArg<Uint8Array>): TRet<Uint8Array>;
24
+ };
15
25
  /** Block mode for {@link Cipher} */
16
26
  export type BlockMode = {
17
27
  /** Encrypt plaintext */
@@ -41,3 +51,70 @@ export type AEADMode = {
41
51
  /** Open ciphertext and AAD */
42
52
  open: (ciphertext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
43
53
  };
54
+ /** Key wrap mode for {@link Cipher} */
55
+ export type WrapMode = {
56
+ /** Wrap encryption key */
57
+ wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
58
+ /** Unwrap encryption key */
59
+ unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
60
+ };
61
+ /** Disk encryption mode for {@link Cipher} */
62
+ export type DiskMode = {
63
+ /** Encrypt disk sector */
64
+ encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
65
+ /** Decrypt disk sector */
66
+ decrypt: (ciphertext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
67
+ };
68
+ /** ECDSA signer */
69
+ export type ECDSA = {
70
+ /**
71
+ * Computes public key for a secret key
72
+ * @param isCompressed - Whether to return compact (default), or full key
73
+ * @returns Public key, full when `isCompressed=false`; short when `isCompressed=true`
74
+ */
75
+ getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
76
+ /**
77
+ * Signs a message hash with a secret key.
78
+ * @param secretKey - Secret key bytes.
79
+ * @param digest - Digest bytes.
80
+ * @param rand - Optional parameter for ephemeral key
81
+ * @returns Encoded signature bytes.
82
+ */
83
+ sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
84
+ /**
85
+ * Verifies a signature against message hash and public key.
86
+ * @param publicKey - Public key
87
+ * @param digest - Digest bytes
88
+ * @param signature - Encoded signature bytes.
89
+ * @returns Whether the signature is valid.
90
+ */
91
+ verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
92
+ /**
93
+ * Compute the shared secret point from a secret key and peer public key.
94
+ * @param secretKeyA - Local secret key bytes.
95
+ * @param publicKeyB - Peer public key bytes.
96
+ * @param withCofactor - Whether to multiply result by curve cofactor? (default - `true`).
97
+ * @returns Encoded shared point.
98
+ */
99
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
100
+ /**
101
+ * Generate a secret/public key pair.
102
+ * @param isCompressed - Whether to return compact (default), or full key
103
+ * @returns Secret/public key pair.
104
+ */
105
+ keygen: (isCompressed?: boolean) => KeyPair;
106
+ /** Byte lengths for keys and signatures exposed by this curve. */
107
+ lengths: Readonly<{
108
+ fieldByteLength: number;
109
+ pointByteLength: number;
110
+ scalarByteLength: number;
111
+ signatureByteLength: number;
112
+ }>;
113
+ };
114
+ /** Keypair */
115
+ export type KeyPair = {
116
+ /** Secret key */
117
+ secretKey: TArg<Uint8Array>;
118
+ /** Public key */
119
+ publicKey: TArg<Uint8Array>;
120
+ };
package/utils.d.ts CHANGED
@@ -12,3 +12,4 @@ export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<U
12
12
  export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
13
13
  export declare const assertKalyna: (cipher: Cipher) => void;
14
14
  export declare const reverseBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
15
+ export declare const abytesAligned: (value: TArg<Uint8Array>, blockSize: number, title?: string) => TRet<Uint8Array>;
package/utils.js CHANGED
@@ -1,6 +1,8 @@
1
- import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
1
+ import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE, abytes, anumber } from "@noble/curves/utils.js";
2
2
  import { Kalyna } from "./kalyna/index.js";
3
3
  export const xorBytes = (a, b) => {
4
+ abytes(a);
5
+ abytes(b);
4
6
  const mlen = Math.min(a.length, b.length);
5
7
  const result = new Uint8Array(mlen);
6
8
  for (let i = 0; i < mlen; i++)
@@ -69,3 +71,14 @@ export const assertKalyna = (cipher) => {
69
71
  throw new Error("Cipher mode only for Kalyna");
70
72
  };
71
73
  export const reverseBytes = (bytes) => copyBytes(bytes).reverse();
74
+ const atitle = (title) => title ? `"${title}" ` : '';
75
+ export const abytesAligned = (value, blockSize, title = "") => {
76
+ abytes(value);
77
+ anumber(blockSize);
78
+ if (value.length !== 0 && value.length % blockSize === 0)
79
+ return value;
80
+ const ofLen = ` of length aligned to ${blockSize}`;
81
+ const got = `length=${value.length}`;
82
+ const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
83
+ throw new RangeError(message);
84
+ };