@li0ard/gost 0.0.1 → 0.1.3

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 (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +47 -45
  3. package/gost3410/const.d.ts +47 -0
  4. package/gost3410/const.js +126 -0
  5. package/gost3410/conversion.d.ts +19 -0
  6. package/gost3410/conversion.js +35 -0
  7. package/gost3410/index.d.ts +29 -0
  8. package/gost3410/index.js +84 -0
  9. package/gost3410/vko.d.ts +38 -0
  10. package/gost3410/vko.js +47 -0
  11. package/gost341194/index.d.ts +23 -0
  12. package/gost341194/index.js +193 -0
  13. package/hmac.d.ts +15 -0
  14. package/hmac.js +22 -0
  15. package/index.d.ts +9 -0
  16. package/index.js +9 -0
  17. package/kdf.d.ts +7 -0
  18. package/kdf.js +59 -0
  19. package/kuznyechik/const.d.ts +4 -0
  20. package/kuznyechik/const.js +78 -0
  21. package/kuznyechik/index.d.ts +12 -0
  22. package/kuznyechik/index.js +207 -0
  23. package/magma/const.d.ts +62 -0
  24. package/magma/const.js +244 -0
  25. package/magma/index.d.ts +24 -0
  26. package/magma/index.js +86 -0
  27. package/modes/_keytransform.d.ts +5 -0
  28. package/modes/_keytransform.js +35 -0
  29. package/modes/cbc.d.ts +8 -0
  30. package/modes/cbc.js +42 -0
  31. package/modes/cfb.d.ts +8 -0
  32. package/modes/cfb.js +37 -0
  33. package/modes/ctr.d.ts +15 -0
  34. package/modes/ctr.js +62 -0
  35. package/modes/ecb.d.ts +7 -0
  36. package/modes/ecb.js +21 -0
  37. package/modes/index.d.ts +8 -0
  38. package/modes/index.js +8 -0
  39. package/modes/mac.d.ts +21 -0
  40. package/modes/mac.js +119 -0
  41. package/modes/mgm.d.ts +8 -0
  42. package/modes/mgm.js +90 -0
  43. package/modes/ofb.d.ts +8 -0
  44. package/modes/ofb.js +25 -0
  45. package/modes/wrap.d.ts +14 -0
  46. package/modes/wrap.js +57 -0
  47. package/package.json +48 -7
  48. package/streebog/const.d.ts +4 -0
  49. package/streebog/const.js +102 -0
  50. package/streebog/index.d.ts +66 -0
  51. package/streebog/index.js +295 -0
  52. package/types.d.ts +50 -0
  53. package/types.js +1 -0
  54. package/utils.d.ts +7 -0
  55. package/utils.js +47 -0
@@ -0,0 +1,62 @@
1
+ /** S-Box from RFC 7836 */
2
+ export declare const ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>[];
3
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset A` */
4
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET: Uint8Array<ArrayBuffer>[];
5
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset B` */
6
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET: Uint8Array<ArrayBuffer>[];
7
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset C` */
8
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET: Uint8Array<ArrayBuffer>[];
9
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset D` */
10
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET: Uint8Array<ArrayBuffer>[];
11
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
12
+ export declare const DSSZZI_UA_DKE_1: Uint8Array<ArrayBuffer>[];
13
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
14
+ export declare const DSSZZI_UA_DKE_2: Uint8Array<ArrayBuffer>[];
15
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
16
+ export declare const DSSZZI_UA_DKE_3: Uint8Array<ArrayBuffer>[];
17
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
18
+ export declare const DSSZZI_UA_DKE_4: Uint8Array<ArrayBuffer>[];
19
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
20
+ export declare const DSSZZI_UA_DKE_5: Uint8Array<ArrayBuffer>[];
21
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
22
+ export declare const DSSZZI_UA_DKE_6: Uint8Array<ArrayBuffer>[];
23
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
24
+ export declare const DSSZZI_UA_DKE_7: Uint8Array<ArrayBuffer>[];
25
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
26
+ export declare const DSSZZI_UA_DKE_8: Uint8Array<ArrayBuffer>[];
27
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
28
+ export declare const DSSZZI_UA_DKE_9: Uint8Array<ArrayBuffer>[];
29
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
30
+ export declare const DSSZZI_UA_DKE_10: Uint8Array<ArrayBuffer>[];
31
+ export declare const ID_GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
32
+ export declare const ID_GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
33
+ export declare const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET: Uint8Array<ArrayBuffer>[];
34
+ export declare const EAC_PARAM_SET: Uint8Array<ArrayBuffer>[];
35
+ /** Implemented S-Boxes */
36
+ export declare const magmaSboxes: {
37
+ ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>[];
38
+ ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET: Uint8Array<ArrayBuffer>[];
39
+ ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET: Uint8Array<ArrayBuffer>[];
40
+ ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET: Uint8Array<ArrayBuffer>[];
41
+ ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET: Uint8Array<ArrayBuffer>[];
42
+ ID_GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
43
+ ID_GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
44
+ ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET: Uint8Array<ArrayBuffer>[];
45
+ EAC_PARAM_SET: Uint8Array<ArrayBuffer>[];
46
+ DSSZZI_UA_DKE_1: Uint8Array<ArrayBuffer>[];
47
+ DSSZZI_UA_DKE_2: Uint8Array<ArrayBuffer>[];
48
+ DSSZZI_UA_DKE_3: Uint8Array<ArrayBuffer>[];
49
+ DSSZZI_UA_DKE_4: Uint8Array<ArrayBuffer>[];
50
+ DSSZZI_UA_DKE_5: Uint8Array<ArrayBuffer>[];
51
+ DSSZZI_UA_DKE_6: Uint8Array<ArrayBuffer>[];
52
+ DSSZZI_UA_DKE_7: Uint8Array<ArrayBuffer>[];
53
+ DSSZZI_UA_DKE_8: Uint8Array<ArrayBuffer>[];
54
+ DSSZZI_UA_DKE_9: Uint8Array<ArrayBuffer>[];
55
+ DSSZZI_UA_DKE_10: Uint8Array<ArrayBuffer>[];
56
+ };
57
+ /** Sequences of `K_i` S-Box applying */
58
+ export declare const magmaKeySequences: {
59
+ ENCRYPT: number[];
60
+ DECRYPT: number[];
61
+ MAC: number[];
62
+ };
package/magma/const.js ADDED
@@ -0,0 +1,244 @@
1
+ /** S-Box from RFC 7836 */
2
+ export const ID_TC26_GOST_28147_PARAM_Z = [
3
+ new Uint8Array([0x0c, 0x04, 0x06, 0x02, 0x0a, 0x05, 0x0b, 0x09, 0x0e, 0x08, 0x0d, 0x07, 0x00, 0x03, 0x0f, 0x01]),
4
+ new Uint8Array([0x06, 0x08, 0x02, 0x03, 0x09, 0x0a, 0x05, 0x0c, 0x01, 0x0e, 0x04, 0x07, 0x0b, 0x0d, 0x00, 0x0f]),
5
+ new Uint8Array([0x0b, 0x03, 0x05, 0x08, 0x02, 0x0f, 0x0a, 0x0d, 0x0e, 0x01, 0x07, 0x04, 0x0c, 0x09, 0x06, 0x00]),
6
+ new Uint8Array([0x0c, 0x08, 0x02, 0x01, 0x0d, 0x04, 0x0f, 0x06, 0x07, 0x00, 0x0a, 0x05, 0x03, 0x0e, 0x09, 0x0b]),
7
+ new Uint8Array([0x07, 0x0f, 0x05, 0x0a, 0x08, 0x01, 0x06, 0x0d, 0x00, 0x09, 0x03, 0x0e, 0x0b, 0x04, 0x02, 0x0c]),
8
+ new Uint8Array([0x05, 0x0d, 0x0f, 0x06, 0x09, 0x02, 0x0c, 0x0a, 0x0b, 0x07, 0x08, 0x01, 0x04, 0x03, 0x0e, 0x00]),
9
+ new Uint8Array([0x08, 0x0e, 0x02, 0x05, 0x06, 0x09, 0x01, 0x0c, 0x0f, 0x04, 0x0b, 0x00, 0x0d, 0x0a, 0x03, 0x07]),
10
+ new Uint8Array([0x01, 0x07, 0x0e, 0x0d, 0x00, 0x05, 0x08, 0x03, 0x04, 0x0f, 0x0a, 0x06, 0x09, 0x0c, 0x0b, 0x02]),
11
+ ];
12
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset A` */
13
+ export const ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET = [
14
+ new Uint8Array([0x09, 0x06, 0x03, 0x02, 0x08, 0x0b, 0x01, 0x07, 0x0a, 0x04, 0x0e, 0x0f, 0x0c, 0x00, 0x0d, 0x05]),
15
+ new Uint8Array([0x03, 0x07, 0x0e, 0x09, 0x08, 0x0a, 0x0f, 0x00, 0x05, 0x02, 0x06, 0x0c, 0x0b, 0x04, 0x0d, 0x01]),
16
+ new Uint8Array([0x0e, 0x04, 0x06, 0x02, 0x0b, 0x03, 0x0d, 0x08, 0x0c, 0x0f, 0x05, 0x0a, 0x00, 0x07, 0x01, 0x09]),
17
+ new Uint8Array([0x0e, 0x07, 0x0a, 0x0c, 0x0d, 0x01, 0x03, 0x09, 0x00, 0x02, 0x0b, 0x04, 0x0f, 0x08, 0x05, 0x06]),
18
+ new Uint8Array([0x0b, 0x05, 0x01, 0x09, 0x08, 0x0d, 0x0f, 0x00, 0x0e, 0x04, 0x02, 0x03, 0x0c, 0x07, 0x0a, 0x06]),
19
+ new Uint8Array([0x03, 0x0a, 0x0d, 0x0c, 0x01, 0x02, 0x00, 0x0b, 0x07, 0x05, 0x09, 0x04, 0x08, 0x0f, 0x0e, 0x06]),
20
+ new Uint8Array([0x01, 0x0d, 0x02, 0x09, 0x07, 0x0a, 0x06, 0x00, 0x08, 0x0c, 0x04, 0x05, 0x0f, 0x03, 0x0b, 0x0e]),
21
+ new Uint8Array([0x0b, 0x0a, 0x0f, 0x05, 0x00, 0x0c, 0x0e, 0x08, 0x06, 0x02, 0x03, 0x09, 0x01, 0x07, 0x0d, 0x04]),
22
+ ];
23
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset B` */
24
+ export const ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET = [
25
+ new Uint8Array([0x08, 0x04, 0x0b, 0x01, 0x03, 0x05, 0x00, 0x09, 0x02, 0x0e, 0x0a, 0x0c, 0x0d, 0x06, 0x07, 0x0f]),
26
+ new Uint8Array([0x00, 0x01, 0x02, 0x0a, 0x04, 0x0d, 0x05, 0x0c, 0x09, 0x07, 0x03, 0x0f, 0x0b, 0x08, 0x06, 0x0e]),
27
+ new Uint8Array([0x0e, 0x0c, 0x00, 0x0a, 0x09, 0x02, 0x0d, 0x0b, 0x07, 0x05, 0x08, 0x0f, 0x03, 0x06, 0x01, 0x04]),
28
+ new Uint8Array([0x07, 0x05, 0x00, 0x0d, 0x0b, 0x06, 0x01, 0x02, 0x03, 0x0a, 0x0c, 0x0f, 0x04, 0x0e, 0x09, 0x08]),
29
+ new Uint8Array([0x02, 0x07, 0x0c, 0x0f, 0x09, 0x05, 0x0a, 0x0b, 0x01, 0x04, 0x00, 0x0d, 0x06, 0x08, 0x0e, 0x03]),
30
+ new Uint8Array([0x08, 0x03, 0x02, 0x06, 0x04, 0x0d, 0x0e, 0x0b, 0x0c, 0x01, 0x07, 0x0f, 0x0a, 0x00, 0x09, 0x05]),
31
+ new Uint8Array([0x05, 0x02, 0x0a, 0x0b, 0x09, 0x01, 0x0c, 0x03, 0x07, 0x04, 0x0d, 0x00, 0x06, 0x0f, 0x08, 0x0e]),
32
+ new Uint8Array([0x00, 0x04, 0x0b, 0x0e, 0x08, 0x03, 0x07, 0x01, 0x0a, 0x02, 0x09, 0x06, 0x0f, 0x0d, 0x05, 0x0c]),
33
+ ];
34
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset C` */
35
+ export const ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET = [
36
+ new Uint8Array([0x01, 0x0b, 0x0c, 0x02, 0x09, 0x0d, 0x00, 0x0f, 0x04, 0x05, 0x08, 0x0e, 0x0a, 0x07, 0x06, 0x03]),
37
+ new Uint8Array([0x00, 0x01, 0x07, 0x0d, 0x0b, 0x04, 0x05, 0x02, 0x08, 0x0e, 0x0f, 0x0c, 0x09, 0x0a, 0x06, 0x03]),
38
+ new Uint8Array([0x08, 0x02, 0x05, 0x00, 0x04, 0x09, 0x0f, 0x0a, 0x03, 0x07, 0x0c, 0x0d, 0x06, 0x0e, 0x01, 0x0b]),
39
+ new Uint8Array([0x03, 0x06, 0x00, 0x01, 0x05, 0x0d, 0x0a, 0x08, 0x0b, 0x02, 0x09, 0x07, 0x0e, 0x0f, 0x0c, 0x04]),
40
+ new Uint8Array([0x08, 0x0d, 0x0b, 0x00, 0x04, 0x05, 0x01, 0x02, 0x09, 0x03, 0x0c, 0x0e, 0x06, 0x0f, 0x0a, 0x07]),
41
+ new Uint8Array([0x0c, 0x09, 0x0b, 0x01, 0x08, 0x0e, 0x02, 0x04, 0x07, 0x03, 0x06, 0x05, 0x0a, 0x00, 0x0f, 0x0d]),
42
+ new Uint8Array([0x0a, 0x09, 0x06, 0x08, 0x0d, 0x0e, 0x02, 0x00, 0x0f, 0x03, 0x05, 0x0b, 0x04, 0x01, 0x0c, 0x07]),
43
+ new Uint8Array([0x07, 0x04, 0x00, 0x05, 0x0a, 0x02, 0x0f, 0x0e, 0x0c, 0x06, 0x01, 0x0b, 0x0d, 0x09, 0x03, 0x08]),
44
+ ];
45
+ /** S-Box from RFC 4357 aka `CryptoPro Paramset D` */
46
+ export const ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET = [
47
+ new Uint8Array([0x0f, 0x0c, 0x02, 0x0a, 0x06, 0x04, 0x05, 0x00, 0x07, 0x09, 0x0e, 0x0d, 0x01, 0x0b, 0x08, 0x03]),
48
+ new Uint8Array([0x0b, 0x06, 0x03, 0x04, 0x0c, 0x0f, 0x0e, 0x02, 0x07, 0x0d, 0x08, 0x00, 0x05, 0x0a, 0x09, 0x01]),
49
+ new Uint8Array([0x01, 0x0c, 0x0b, 0x00, 0x0f, 0x0e, 0x06, 0x05, 0x0a, 0x0d, 0x04, 0x08, 0x09, 0x03, 0x07, 0x02]),
50
+ new Uint8Array([0x01, 0x05, 0x0e, 0x0c, 0x0a, 0x07, 0x00, 0x0d, 0x06, 0x02, 0x0b, 0x04, 0x09, 0x03, 0x0f, 0x08]),
51
+ new Uint8Array([0x00, 0x0c, 0x08, 0x09, 0x0d, 0x02, 0x0a, 0x0b, 0x07, 0x03, 0x06, 0x05, 0x04, 0x0e, 0x0f, 0x01]),
52
+ new Uint8Array([0x08, 0x00, 0x0f, 0x03, 0x02, 0x05, 0x0e, 0x0b, 0x01, 0x0a, 0x04, 0x07, 0x0c, 0x09, 0x0d, 0x06]),
53
+ new Uint8Array([0x03, 0x00, 0x06, 0x0f, 0x01, 0x0e, 0x09, 0x02, 0x0d, 0x08, 0x0c, 0x04, 0x0b, 0x0a, 0x05, 0x07]),
54
+ new Uint8Array([0x01, 0x0a, 0x06, 0x08, 0x0f, 0x0b, 0x00, 0x04, 0x0c, 0x03, 0x05, 0x09, 0x07, 0x0d, 0x02, 0x0e]),
55
+ ];
56
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
57
+ export const DSSZZI_UA_DKE_1 = [
58
+ new Uint8Array([0x0a, 0x09, 0x0d, 0x06, 0x0e, 0x0b, 0x04, 0x05, 0x0f, 0x01, 0x03, 0x0c, 0x07, 0x00, 0x08, 0x02]),
59
+ new Uint8Array([0x08, 0x00, 0x0c, 0x04, 0x09, 0x06, 0x07, 0x0b, 0x02, 0x03, 0x01, 0x0f, 0x05, 0x0e, 0x0a, 0x0d]),
60
+ new Uint8Array([0x0f, 0x06, 0x05, 0x08, 0x0e, 0x0b, 0x0a, 0x04, 0x0c, 0x00, 0x03, 0x07, 0x02, 0x09, 0x01, 0x0d]),
61
+ new Uint8Array([0x03, 0x08, 0x0d, 0x09, 0x06, 0x0b, 0x0f, 0x00, 0x02, 0x05, 0x0c, 0x0a, 0x04, 0x0e, 0x01, 0x07]),
62
+ new Uint8Array([0x0f, 0x08, 0x0e, 0x09, 0x07, 0x02, 0x00, 0x0d, 0x0c, 0x06, 0x01, 0x05, 0x0b, 0x04, 0x03, 0x0a]),
63
+ new Uint8Array([0x02, 0x08, 0x09, 0x07, 0x05, 0x0f, 0x00, 0x0b, 0x0c, 0x01, 0x0d, 0x0e, 0x0a, 0x03, 0x06, 0x04]),
64
+ new Uint8Array([0x03, 0x08, 0x0b, 0x05, 0x06, 0x04, 0x0e, 0x0a, 0x02, 0x0c, 0x01, 0x07, 0x09, 0x0f, 0x0d, 0x00]),
65
+ new Uint8Array([0x01, 0x02, 0x03, 0x0e, 0x06, 0x0d, 0x0b, 0x08, 0x0f, 0x0a, 0x0c, 0x05, 0x07, 0x09, 0x00, 0x04]),
66
+ ];
67
+ // Converted from tables by script, accuracy not guaranteed
68
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
69
+ export const DSSZZI_UA_DKE_2 = [
70
+ new Uint8Array([0x0e, 0x09, 0x03, 0x07, 0x0f, 0x04, 0x0c, 0x0b, 0x06, 0x0a, 0x0d, 0x01, 0x00, 0x05, 0x08, 0x02]),
71
+ new Uint8Array([0x0a, 0x0d, 0x0c, 0x07, 0x06, 0x0e, 0x08, 0x01, 0x0f, 0x03, 0x0b, 0x04, 0x00, 0x09, 0x05, 0x02]),
72
+ new Uint8Array([0x04, 0x0b, 0x01, 0x0f, 0x09, 0x02, 0x0e, 0x0c, 0x06, 0x0a, 0x08, 0x07, 0x03, 0x05, 0x00, 0x0d]),
73
+ new Uint8Array([0x04, 0x05, 0x01, 0x0c, 0x07, 0x0e, 0x09, 0x02, 0x0a, 0x0f, 0x0b, 0x0d, 0x00, 0x08, 0x06, 0x03]),
74
+ new Uint8Array([0x0c, 0x0b, 0x03, 0x09, 0x0f, 0x00, 0x04, 0x05, 0x07, 0x02, 0x0e, 0x0d, 0x01, 0x0a, 0x08, 0x06]),
75
+ new Uint8Array([0x08, 0x07, 0x03, 0x0a, 0x09, 0x06, 0x0e, 0x05, 0x0d, 0x00, 0x04, 0x0c, 0x01, 0x02, 0x0f, 0x0b]),
76
+ new Uint8Array([0x0f, 0x00, 0x0e, 0x06, 0x08, 0x0d, 0x05, 0x09, 0x0a, 0x03, 0x01, 0x0c, 0x04, 0x0b, 0x07, 0x02]),
77
+ new Uint8Array([0x04, 0x03, 0x0e, 0x0d, 0x05, 0x00, 0x02, 0x0b, 0x01, 0x0a, 0x07, 0x06, 0x09, 0x0f, 0x08, 0x0c]),
78
+ ];
79
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
80
+ export const DSSZZI_UA_DKE_3 = [
81
+ new Uint8Array([0x0d, 0x09, 0x01, 0x0e, 0x07, 0x02, 0x0c, 0x05, 0x04, 0x0b, 0x06, 0x0f, 0x03, 0x08, 0x0a, 0x00]),
82
+ new Uint8Array([0x07, 0x08, 0x06, 0x0b, 0x00, 0x03, 0x04, 0x0d, 0x09, 0x05, 0x0f, 0x0e, 0x0a, 0x0c, 0x02, 0x01]),
83
+ new Uint8Array([0x0a, 0x05, 0x03, 0x0c, 0x09, 0x08, 0x0d, 0x06, 0x04, 0x0f, 0x0e, 0x00, 0x02, 0x0b, 0x01, 0x07]),
84
+ new Uint8Array([0x0b, 0x0a, 0x0c, 0x01, 0x05, 0x06, 0x09, 0x0e, 0x02, 0x0d, 0x0f, 0x07, 0x00, 0x04, 0x03, 0x08]),
85
+ new Uint8Array([0x05, 0x0b, 0x03, 0x00, 0x0f, 0x09, 0x0e, 0x04, 0x01, 0x0c, 0x08, 0x06, 0x02, 0x0a, 0x07, 0x0d]),
86
+ new Uint8Array([0x04, 0x03, 0x0b, 0x0d, 0x01, 0x0f, 0x08, 0x02, 0x07, 0x0e, 0x0c, 0x09, 0x0a, 0x00, 0x06, 0x05]),
87
+ new Uint8Array([0x03, 0x07, 0x08, 0x0b, 0x01, 0x0e, 0x05, 0x00, 0x0d, 0x04, 0x0c, 0x0a, 0x02, 0x09, 0x0f, 0x06]),
88
+ new Uint8Array([0x06, 0x0d, 0x0c, 0x0a, 0x0b, 0x07, 0x09, 0x03, 0x0f, 0x0e, 0x01, 0x02, 0x00, 0x08, 0x04, 0x05]),
89
+ ];
90
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
91
+ export const DSSZZI_UA_DKE_4 = [
92
+ new Uint8Array([0x09, 0x0c, 0x03, 0x0d, 0x07, 0x06, 0x0e, 0x01, 0x0a, 0x02, 0x00, 0x04, 0x08, 0x0f, 0x05, 0x0b]),
93
+ new Uint8Array([0x0a, 0x05, 0x0b, 0x0e, 0x07, 0x06, 0x00, 0x0c, 0x02, 0x08, 0x0f, 0x04, 0x0d, 0x03, 0x09, 0x01]),
94
+ new Uint8Array([0x04, 0x0c, 0x03, 0x00, 0x0d, 0x02, 0x0e, 0x0b, 0x07, 0x0f, 0x05, 0x09, 0x01, 0x08, 0x0a, 0x06]),
95
+ new Uint8Array([0x03, 0x09, 0x04, 0x05, 0x0e, 0x07, 0x08, 0x06, 0x0d, 0x00, 0x02, 0x0f, 0x0b, 0x0c, 0x0a, 0x01]),
96
+ new Uint8Array([0x02, 0x09, 0x0c, 0x0f, 0x0d, 0x0b, 0x04, 0x01, 0x07, 0x05, 0x03, 0x0e, 0x06, 0x08, 0x0a, 0x00]),
97
+ new Uint8Array([0x0e, 0x05, 0x0d, 0x0b, 0x01, 0x09, 0x04, 0x02, 0x0f, 0x08, 0x07, 0x00, 0x03, 0x0c, 0x0a, 0x06]),
98
+ new Uint8Array([0x0e, 0x06, 0x05, 0x0a, 0x09, 0x0d, 0x04, 0x08, 0x0b, 0x0c, 0x00, 0x03, 0x07, 0x01, 0x0f, 0x02]),
99
+ new Uint8Array([0x01, 0x09, 0x0c, 0x0b, 0x07, 0x06, 0x08, 0x03, 0x02, 0x0f, 0x0e, 0x00, 0x05, 0x0a, 0x04, 0x0d]),
100
+ ];
101
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
102
+ export const DSSZZI_UA_DKE_5 = [
103
+ new Uint8Array([0x03, 0x04, 0x0d, 0x08, 0x0c, 0x07, 0x0a, 0x02, 0x00, 0x0e, 0x09, 0x0f, 0x0b, 0x01, 0x05, 0x06]),
104
+ new Uint8Array([0x0c, 0x07, 0x06, 0x09, 0x03, 0x08, 0x0b, 0x05, 0x0f, 0x0a, 0x00, 0x0d, 0x04, 0x02, 0x01, 0x0e]),
105
+ new Uint8Array([0x0e, 0x04, 0x08, 0x07, 0x0b, 0x03, 0x0a, 0x0c, 0x01, 0x02, 0x06, 0x09, 0x0d, 0x0f, 0x00, 0x05]),
106
+ new Uint8Array([0x03, 0x09, 0x06, 0x0d, 0x08, 0x0f, 0x0a, 0x02, 0x07, 0x0e, 0x0c, 0x00, 0x0b, 0x04, 0x01, 0x05]),
107
+ new Uint8Array([0x05, 0x0c, 0x0a, 0x07, 0x02, 0x01, 0x0f, 0x0d, 0x0e, 0x03, 0x0b, 0x04, 0x00, 0x08, 0x09, 0x06]),
108
+ new Uint8Array([0x01, 0x08, 0x0b, 0x0e, 0x07, 0x04, 0x0a, 0x00, 0x0c, 0x03, 0x05, 0x0d, 0x09, 0x0f, 0x06, 0x02]),
109
+ new Uint8Array([0x09, 0x0b, 0x0a, 0x0d, 0x05, 0x0e, 0x02, 0x03, 0x00, 0x06, 0x04, 0x0c, 0x0f, 0x01, 0x07, 0x08]),
110
+ new Uint8Array([0x0e, 0x09, 0x01, 0x08, 0x05, 0x0f, 0x0b, 0x00, 0x06, 0x02, 0x0c, 0x07, 0x0a, 0x04, 0x0d, 0x03]),
111
+ ];
112
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
113
+ export const DSSZZI_UA_DKE_6 = [
114
+ new Uint8Array([0x0f, 0x0c, 0x09, 0x06, 0x0e, 0x02, 0x01, 0x0b, 0x00, 0x0d, 0x04, 0x0a, 0x07, 0x08, 0x03, 0x05]),
115
+ new Uint8Array([0x0e, 0x0c, 0x05, 0x00, 0x07, 0x04, 0x0a, 0x03, 0x02, 0x06, 0x01, 0x0d, 0x09, 0x0b, 0x0f, 0x08]),
116
+ new Uint8Array([0x05, 0x06, 0x0d, 0x09, 0x0b, 0x0e, 0x0a, 0x03, 0x0f, 0x02, 0x08, 0x01, 0x04, 0x00, 0x07, 0x0c]),
117
+ new Uint8Array([0x01, 0x0f, 0x07, 0x04, 0x02, 0x0e, 0x0c, 0x03, 0x06, 0x0b, 0x09, 0x08, 0x00, 0x05, 0x0a, 0x0d]),
118
+ new Uint8Array([0x0f, 0x09, 0x0e, 0x06, 0x0d, 0x01, 0x05, 0x08, 0x04, 0x02, 0x03, 0x0c, 0x0a, 0x0b, 0x00, 0x07]),
119
+ new Uint8Array([0x0b, 0x00, 0x0d, 0x07, 0x0c, 0x0e, 0x01, 0x04, 0x02, 0x03, 0x06, 0x08, 0x0a, 0x05, 0x0f, 0x09]),
120
+ new Uint8Array([0x07, 0x0e, 0x0f, 0x08, 0x0d, 0x00, 0x0b, 0x03, 0x0a, 0x01, 0x04, 0x02, 0x09, 0x0c, 0x06, 0x05]),
121
+ new Uint8Array([0x01, 0x05, 0x0e, 0x0b, 0x02, 0x0c, 0x03, 0x08, 0x0a, 0x00, 0x09, 0x07, 0x0f, 0x06, 0x04, 0x0d]),
122
+ ];
123
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
124
+ export const DSSZZI_UA_DKE_7 = [
125
+ new Uint8Array([0x0f, 0x0d, 0x0a, 0x05, 0x0c, 0x00, 0x01, 0x06, 0x09, 0x02, 0x0e, 0x07, 0x03, 0x0b, 0x04, 0x08]),
126
+ new Uint8Array([0x02, 0x05, 0x0a, 0x00, 0x06, 0x09, 0x01, 0x0f, 0x0d, 0x04, 0x07, 0x0e, 0x0b, 0x03, 0x08, 0x0c]),
127
+ new Uint8Array([0x03, 0x0e, 0x04, 0x0b, 0x05, 0x09, 0x01, 0x02, 0x0f, 0x06, 0x08, 0x0d, 0x07, 0x00, 0x0a, 0x0c]),
128
+ new Uint8Array([0x04, 0x0a, 0x0b, 0x09, 0x0f, 0x02, 0x0e, 0x05, 0x0d, 0x01, 0x03, 0x06, 0x00, 0x07, 0x0c, 0x08]),
129
+ new Uint8Array([0x0f, 0x06, 0x05, 0x08, 0x09, 0x07, 0x0c, 0x0b, 0x00, 0x0a, 0x03, 0x01, 0x02, 0x04, 0x0d, 0x0e]),
130
+ new Uint8Array([0x0c, 0x0b, 0x0f, 0x04, 0x05, 0x01, 0x0e, 0x09, 0x00, 0x08, 0x0d, 0x02, 0x0a, 0x07, 0x03, 0x06]),
131
+ new Uint8Array([0x0d, 0x02, 0x04, 0x08, 0x0b, 0x0c, 0x01, 0x03, 0x0a, 0x05, 0x09, 0x0e, 0x07, 0x0f, 0x00, 0x06]),
132
+ new Uint8Array([0x01, 0x05, 0x00, 0x0f, 0x06, 0x0a, 0x03, 0x0e, 0x07, 0x02, 0x0c, 0x0d, 0x0b, 0x08, 0x09, 0x04]),
133
+ ];
134
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
135
+ export const DSSZZI_UA_DKE_8 = [
136
+ new Uint8Array([0x0e, 0x04, 0x0b, 0x02, 0x08, 0x07, 0x05, 0x0c, 0x09, 0x0d, 0x00, 0x03, 0x01, 0x0f, 0x06, 0x0a]),
137
+ new Uint8Array([0x03, 0x0e, 0x0c, 0x0a, 0x06, 0x02, 0x0d, 0x01, 0x09, 0x08, 0x07, 0x04, 0x00, 0x0f, 0x05, 0x0b]),
138
+ new Uint8Array([0x05, 0x02, 0x08, 0x07, 0x01, 0x0f, 0x0e, 0x06, 0x04, 0x0d, 0x0b, 0x00, 0x0a, 0x03, 0x0c, 0x09]),
139
+ new Uint8Array([0x0c, 0x0a, 0x07, 0x0d, 0x0e, 0x03, 0x00, 0x02, 0x09, 0x05, 0x01, 0x06, 0x0b, 0x04, 0x0f, 0x08]),
140
+ new Uint8Array([0x06, 0x03, 0x0f, 0x07, 0x00, 0x09, 0x0a, 0x08, 0x0b, 0x0c, 0x04, 0x01, 0x05, 0x02, 0x0d, 0x0e]),
141
+ new Uint8Array([0x06, 0x0d, 0x0f, 0x01, 0x05, 0x03, 0x08, 0x00, 0x0b, 0x0a, 0x0e, 0x04, 0x09, 0x0c, 0x02, 0x07]),
142
+ new Uint8Array([0x02, 0x0f, 0x0c, 0x05, 0x0b, 0x01, 0x03, 0x0e, 0x00, 0x06, 0x0d, 0x0a, 0x07, 0x09, 0x04, 0x08]),
143
+ new Uint8Array([0x03, 0x00, 0x05, 0x0c, 0x08, 0x0f, 0x0d, 0x0e, 0x0b, 0x06, 0x02, 0x09, 0x07, 0x01, 0x04, 0x0a]),
144
+ ];
145
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
146
+ export const DSSZZI_UA_DKE_9 = [
147
+ new Uint8Array([0x09, 0x00, 0x0b, 0x0c, 0x02, 0x04, 0x03, 0x0f, 0x0d, 0x06, 0x0e, 0x01, 0x0a, 0x07, 0x05, 0x08]),
148
+ new Uint8Array([0x03, 0x05, 0x00, 0x0f, 0x08, 0x07, 0x0e, 0x0c, 0x0d, 0x0a, 0x01, 0x06, 0x0b, 0x02, 0x04, 0x09]),
149
+ new Uint8Array([0x08, 0x04, 0x05, 0x0a, 0x0e, 0x0b, 0x0d, 0x06, 0x0c, 0x0f, 0x07, 0x09, 0x03, 0x01, 0x02, 0x00]),
150
+ new Uint8Array([0x05, 0x04, 0x0f, 0x00, 0x0c, 0x0b, 0x0a, 0x09, 0x01, 0x0e, 0x08, 0x06, 0x03, 0x02, 0x0d, 0x07]),
151
+ new Uint8Array([0x07, 0x0c, 0x03, 0x00, 0x06, 0x08, 0x0e, 0x0b, 0x01, 0x0f, 0x0d, 0x0a, 0x09, 0x05, 0x02, 0x04]),
152
+ new Uint8Array([0x07, 0x04, 0x03, 0x0b, 0x06, 0x0a, 0x08, 0x01, 0x09, 0x0c, 0x0e, 0x0d, 0x00, 0x0f, 0x02, 0x05]),
153
+ new Uint8Array([0x07, 0x0e, 0x09, 0x0f, 0x01, 0x04, 0x08, 0x03, 0x0b, 0x0d, 0x00, 0x02, 0x06, 0x0a, 0x05, 0x0c]),
154
+ new Uint8Array([0x0e, 0x02, 0x08, 0x0f, 0x03, 0x00, 0x07, 0x0c, 0x0b, 0x0d, 0x01, 0x05, 0x06, 0x04, 0x09, 0x0a]),
155
+ ];
156
+ /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
157
+ export const DSSZZI_UA_DKE_10 = [
158
+ new Uint8Array([0x08, 0x04, 0x06, 0x09, 0x0b, 0x0c, 0x01, 0x02, 0x03, 0x07, 0x0e, 0x00, 0x0d, 0x0a, 0x0f, 0x05]),
159
+ new Uint8Array([0x07, 0x0d, 0x01, 0x08, 0x0a, 0x0e, 0x04, 0x0f, 0x09, 0x00, 0x06, 0x03, 0x02, 0x0c, 0x0b, 0x05]),
160
+ new Uint8Array([0x0c, 0x08, 0x0d, 0x01, 0x0a, 0x02, 0x09, 0x06, 0x03, 0x04, 0x0e, 0x07, 0x05, 0x0f, 0x00, 0x0b]),
161
+ new Uint8Array([0x02, 0x0b, 0x03, 0x04, 0x0c, 0x07, 0x09, 0x0d, 0x0f, 0x08, 0x05, 0x00, 0x01, 0x0e, 0x0a, 0x06]),
162
+ new Uint8Array([0x08, 0x03, 0x0d, 0x0a, 0x0e, 0x0f, 0x05, 0x01, 0x04, 0x07, 0x0b, 0x0c, 0x02, 0x00, 0x06, 0x09]),
163
+ new Uint8Array([0x04, 0x0c, 0x09, 0x0b, 0x0e, 0x0a, 0x07, 0x06, 0x03, 0x05, 0x00, 0x0f, 0x01, 0x02, 0x08, 0x0d]),
164
+ new Uint8Array([0x05, 0x08, 0x0e, 0x07, 0x03, 0x00, 0x01, 0x0d, 0x0a, 0x06, 0x09, 0x02, 0x0f, 0x0b, 0x0c, 0x04]),
165
+ new Uint8Array([0x0a, 0x03, 0x05, 0x09, 0x00, 0x0d, 0x07, 0x08, 0x0c, 0x04, 0x01, 0x06, 0x0b, 0x0f, 0x02, 0x0e]),
166
+ ];
167
+ export const ID_GOST_28147_89_TEST_PARAM_SET = [
168
+ new Uint8Array([4, 2, 15, 5, 9, 1, 0, 8, 14, 3, 11, 12, 13, 7, 10, 6]),
169
+ new Uint8Array([12, 9, 15, 14, 8, 1, 3, 10, 2, 7, 4, 13, 6, 0, 11, 5]),
170
+ new Uint8Array([13, 8, 14, 12, 7, 3, 9, 10, 1, 5, 2, 4, 6, 15, 0, 11]),
171
+ new Uint8Array([14, 9, 11, 2, 5, 15, 7, 1, 0, 13, 12, 6, 10, 4, 3, 8]),
172
+ new Uint8Array([3, 14, 5, 9, 6, 8, 0, 13, 10, 11, 7, 12, 2, 1, 15, 4]),
173
+ new Uint8Array([8, 15, 6, 11, 1, 9, 12, 5, 13, 3, 7, 10, 0, 14, 2, 4]),
174
+ new Uint8Array([9, 11, 12, 0, 3, 6, 7, 5, 4, 8, 14, 15, 1, 10, 2, 13]),
175
+ new Uint8Array([12, 6, 5, 2, 11, 0, 9, 13, 3, 14, 7, 10, 15, 4, 1, 8]),
176
+ ];
177
+ export const ID_GOSTR_3411_94_TEST_PARAM_SET = [
178
+ new Uint8Array([4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3]),
179
+ new Uint8Array([14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9]),
180
+ new Uint8Array([5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11]),
181
+ new Uint8Array([7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3]),
182
+ new Uint8Array([6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2]),
183
+ new Uint8Array([4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14]),
184
+ new Uint8Array([13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12]),
185
+ new Uint8Array([1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12]),
186
+ ];
187
+ export const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET = [
188
+ new Uint8Array([10, 4, 5, 6, 8, 1, 3, 7, 13, 12, 14, 0, 9, 2, 11, 15]),
189
+ new Uint8Array([5, 15, 4, 0, 2, 13, 11, 9, 1, 7, 6, 3, 12, 14, 10, 8]),
190
+ new Uint8Array([7, 15, 12, 14, 9, 4, 1, 0, 3, 11, 5, 2, 6, 10, 8, 13]),
191
+ new Uint8Array([4, 10, 7, 12, 0, 15, 2, 8, 14, 1, 6, 5, 13, 11, 9, 3]),
192
+ new Uint8Array([7, 6, 4, 11, 9, 12, 2, 10, 1, 8, 0, 14, 15, 13, 3, 5]),
193
+ new Uint8Array([7, 6, 2, 4, 13, 9, 15, 0, 10, 1, 5, 11, 8, 14, 12, 3]),
194
+ new Uint8Array([13, 14, 4, 1, 7, 0, 5, 10, 3, 12, 8, 15, 6, 2, 9, 11]),
195
+ new Uint8Array([1, 3, 10, 9, 5, 11, 4, 15, 8, 6, 7, 14, 13, 0, 2, 12]),
196
+ ];
197
+ export const EAC_PARAM_SET = [
198
+ new Uint8Array([11, 4, 8, 10, 9, 7, 0, 3, 1, 6, 2, 15, 14, 5, 12, 13]),
199
+ new Uint8Array([1, 7, 14, 9, 11, 3, 15, 12, 0, 5, 4, 6, 13, 10, 8, 2]),
200
+ new Uint8Array([7, 3, 1, 9, 2, 4, 13, 15, 8, 10, 12, 6, 5, 0, 11, 14]),
201
+ new Uint8Array([10, 5, 15, 7, 14, 11, 3, 9, 2, 8, 1, 12, 0, 4, 6, 13]),
202
+ new Uint8Array([0, 14, 6, 11, 9, 3, 8, 4, 12, 15, 10, 5, 13, 7, 1, 2]),
203
+ new Uint8Array([9, 2, 11, 12, 0, 4, 5, 6, 3, 15, 13, 8, 1, 7, 14, 10]),
204
+ new Uint8Array([4, 0, 14, 1, 5, 11, 8, 3, 12, 2, 9, 7, 6, 10, 13, 15]),
205
+ new Uint8Array([7, 14, 12, 13, 9, 4, 8, 15, 10, 2, 6, 0, 3, 11, 5, 1]),
206
+ ];
207
+ /** Implemented S-Boxes */
208
+ export const magmaSboxes = {
209
+ ID_TC26_GOST_28147_PARAM_Z,
210
+ ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET,
211
+ ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET,
212
+ ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET,
213
+ ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET,
214
+ ID_GOST_28147_89_TEST_PARAM_SET,
215
+ ID_GOSTR_3411_94_TEST_PARAM_SET,
216
+ ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET,
217
+ EAC_PARAM_SET,
218
+ DSSZZI_UA_DKE_1,
219
+ DSSZZI_UA_DKE_2,
220
+ DSSZZI_UA_DKE_3,
221
+ DSSZZI_UA_DKE_4,
222
+ DSSZZI_UA_DKE_5,
223
+ DSSZZI_UA_DKE_6,
224
+ DSSZZI_UA_DKE_7,
225
+ DSSZZI_UA_DKE_8,
226
+ DSSZZI_UA_DKE_9,
227
+ DSSZZI_UA_DKE_10
228
+ };
229
+ /** Sequences of `K_i` S-Box applying */
230
+ export const magmaKeySequences = {
231
+ ENCRYPT: [
232
+ 0, 1, 2, 3, 4, 5, 6, 7,
233
+ 0, 1, 2, 3, 4, 5, 6, 7,
234
+ 0, 1, 2, 3, 4, 5, 6, 7,
235
+ 7, 6, 5, 4, 3, 2, 1, 0
236
+ ],
237
+ DECRYPT: [
238
+ 0, 1, 2, 3, 4, 5, 6, 7,
239
+ 7, 6, 5, 4, 3, 2, 1, 0,
240
+ 7, 6, 5, 4, 3, 2, 1, 0,
241
+ 7, 6, 5, 4, 3, 2, 1, 0
242
+ ],
243
+ MAC: [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7]
244
+ };
@@ -0,0 +1,24 @@
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ import type { Cipher } from "../types.js";
3
+ /** Magma (GOST R 34.12-2015 and GOST 28147-89) cipher */
4
+ export declare class Magma implements Cipher {
5
+ private sbox;
6
+ isLegacy: boolean;
7
+ readonly keySize = 32;
8
+ readonly blockSize = 8;
9
+ private key;
10
+ /**
11
+ * Magma (GOST R 34.12-2015 and GOST 28147-89) cipher
12
+ * @param key Encryption key
13
+ * @param sbox S-Box
14
+ * @param isLegacy Use GOST 28147-89 instead of GOST R 34.12-2015?
15
+ */
16
+ constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array>[], isLegacy?: boolean);
17
+ private regenerateRoundKeys;
18
+ proceedBlock(block: TArg<Uint8Array>, sequence: number[]): TRet<Uint8Array>;
19
+ encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
20
+ decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
21
+ static reverseKey(key: TArg<Uint8Array>): TRet<Uint8Array>;
22
+ static reverseChunks(data: TArg<Uint8Array>): TRet<Uint8Array>;
23
+ }
24
+ export { magmaSboxes, magmaKeySequences } from "./const.js";
package/magma/index.js ADDED
@@ -0,0 +1,86 @@
1
+ import { bytesToNumberBE, concatBytes, copyBytes, numberToBytesBE } from "@noble/curves/utils.js";
2
+ import { ID_TC26_GOST_28147_PARAM_Z, magmaKeySequences } from "./const.js";
3
+ const BLOCKSIZE = 8, KEYSIZE = 32;
4
+ const T = (value, sbox) => {
5
+ //let result = 0;
6
+ //for (let i = 0; i < 8; i++) result |= sbox[i][(value >> (4 * i)) & 0x0f] << (4 * i);
7
+ let result = sbox[0][(value >> 0) & 0x0f] << 0;
8
+ result |= sbox[1][(value >> 4) & 0x0f] << 4;
9
+ result |= sbox[2][(value >> 8) & 0x0f] << 8;
10
+ result |= sbox[3][(value >> 12) & 0x0f] << 12;
11
+ result |= sbox[4][(value >> 16) & 0x0f] << 16;
12
+ result |= sbox[5][(value >> 20) & 0x0f] << 20;
13
+ result |= sbox[6][(value >> 24) & 0x0f] << 24;
14
+ result |= sbox[7][(value >> 28) & 0x0f] << 28;
15
+ return result >>> 0;
16
+ };
17
+ const G = (a, k, sbox) => {
18
+ const substituted = T((a + k) >>> 0, sbox);
19
+ return ((substituted << 11) | (substituted >>> 21)) >>> 0;
20
+ };
21
+ /** Magma (GOST R 34.12-2015 and GOST 28147-89) cipher */
22
+ export class Magma {
23
+ sbox;
24
+ isLegacy;
25
+ keySize = KEYSIZE;
26
+ blockSize = BLOCKSIZE;
27
+ key;
28
+ /**
29
+ * Magma (GOST R 34.12-2015 and GOST 28147-89) cipher
30
+ * @param key Encryption key
31
+ * @param sbox S-Box
32
+ * @param isLegacy Use GOST 28147-89 instead of GOST R 34.12-2015?
33
+ */
34
+ constructor(key, sbox = ID_TC26_GOST_28147_PARAM_Z, isLegacy = false) {
35
+ this.sbox = sbox;
36
+ this.isLegacy = isLegacy;
37
+ if (key.length !== this.keySize)
38
+ throw new Error("Invalid key length");
39
+ this.key = isLegacy ? Magma.reverseKey(key) : key;
40
+ }
41
+ regenerateRoundKeys(sequence) {
42
+ const keyChunks = [];
43
+ for (let j = 0; j < 8; j++)
44
+ keyChunks.push(Number(bytesToNumberBE(this.key.subarray(j * 4, j * 4 + 4))));
45
+ const roundKeys = new Array(sequence.length);
46
+ for (let i = 0; i < sequence.length; i++)
47
+ roundKeys[i] = keyChunks[sequence[i]];
48
+ return roundKeys;
49
+ }
50
+ proceedBlock(block, sequence) {
51
+ if (block.length !== this.blockSize)
52
+ throw new Error("Invalid block size");
53
+ const roundKeys = this.regenerateRoundKeys(sequence);
54
+ let a0 = Number(bytesToNumberBE(block.subarray(0, 4)));
55
+ let a1 = Number(bytesToNumberBE(block.subarray(4, 8)));
56
+ for (let i = 0; i < roundKeys.length; i++) {
57
+ const temp = a1;
58
+ a1 = (a0 ^ G(a1, roundKeys[i], this.sbox)) >>> 0;
59
+ a0 = temp;
60
+ }
61
+ return concatBytes(numberToBytesBE(a1, 4), numberToBytesBE(a0, 4));
62
+ }
63
+ encrypt(plaintext) {
64
+ if (this.isLegacy)
65
+ return Magma.reverseChunks(this.proceedBlock(Magma.reverseChunks(plaintext), magmaKeySequences.ENCRYPT));
66
+ return this.proceedBlock(plaintext, magmaKeySequences.ENCRYPT);
67
+ }
68
+ decrypt(ciphertext) {
69
+ if (this.isLegacy)
70
+ return Magma.reverseChunks(this.proceedBlock(Magma.reverseChunks(ciphertext), magmaKeySequences.DECRYPT));
71
+ return this.proceedBlock(ciphertext, magmaKeySequences.DECRYPT);
72
+ }
73
+ static reverseKey(key) {
74
+ const result = new Uint8Array(KEYSIZE);
75
+ for (let i = 0; i < BLOCKSIZE; i++)
76
+ result.set(copyBytes(key.subarray(i * 4, i * 4 + 4)).reverse(), i * 4);
77
+ return result;
78
+ }
79
+ static reverseChunks(data) {
80
+ const chunks = [];
81
+ for (let i = 0; i < data.length; i += BLOCKSIZE)
82
+ chunks.push(copyBytes(data.subarray(i, i + BLOCKSIZE)).reverse());
83
+ return concatBytes(...chunks);
84
+ }
85
+ }
86
+ export { magmaSboxes, magmaKeySequences } from "./const.js";
@@ -0,0 +1,5 @@
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ import type { Cipher } from "../types.js";
3
+ export declare const cp_kek_diversify: (kek: TArg<Uint8Array>, ukm: TArg<Uint8Array>, sbox?: TArg<Uint8Array>[]) => TRet<Uint8Array>;
4
+ export declare const acpkm: (encrypter: (msg: TArg<Uint8Array>) => TRet<Uint8Array>, bs: number) => TRet<Uint8Array>;
5
+ export declare const acpkm_master: (cipher: Cipher, length: number) => TRet<Uint8Array>;
@@ -0,0 +1,35 @@
1
+ import { bytesToNumberLE, concatBytes, copyBytes, numberToBytesLE } from "@noble/curves/utils.js";
2
+ import { Magma } from "../magma/index.js";
3
+ import { ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET } from "../magma/const.js";
4
+ import { cfb } from "./cfb.js";
5
+ import { ctr } from "./ctr.js";
6
+ import { hexToBytes } from "@noble/hashes/utils.js";
7
+ import { ecb } from "./ecb.js";
8
+ export const cp_kek_diversify = (kek, ukm, sbox = ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET) => {
9
+ let out = copyBytes(kek);
10
+ for (let i = 0; i < 8; i++) {
11
+ let s1 = 0, s2 = 0;
12
+ for (let j = 0; j < 8; j++) {
13
+ const k = Number(bytesToNumberLE(out.subarray(j * 4, j * 4 + 4))); //((out[j * 4]) | (out[j * 4 + 1] << 8) | (out[j * 4 + 2] << 16) | (out[j * 4 + 3] << 24)) >>> 0;
14
+ if ((ukm[i] >> j) & 1)
15
+ s1 += k;
16
+ else
17
+ s2 += k;
18
+ }
19
+ const iv = concatBytes(numberToBytesLE(s1 >>> 0, 4), numberToBytesLE(s2 >>> 0, 4));
20
+ const cipher = new Magma(out, sbox, true);
21
+ out = cfb(cipher, iv).encrypt(out);
22
+ }
23
+ return out;
24
+ };
25
+ export const acpkm = (encrypter, bs) => {
26
+ const result = [];
27
+ for (let d = 0x80; d < (0x80 + bs * (32 / bs)); d += bs) {
28
+ const block = new Uint8Array(bs);
29
+ for (let i = 0; i < bs; i++)
30
+ block[i] = d + i;
31
+ result.push(encrypter(block));
32
+ }
33
+ return concatBytes(...result);
34
+ };
35
+ export const acpkm_master = (cipher, length) => ctr(cipher, new Uint8Array(cipher.blockSize / 2).fill(0xFF), true, true).crypt(new Uint8Array(length));
package/modes/cbc.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { type TArg } from "@noble/hashes/utils.js";
2
+ import type { BlockMode, Cipher } from "../types.js";
3
+ /**
4
+ * **EN:** Cipher Block Chaining (CBC) mode
5
+ *
6
+ * **RU:** Режим простой замены с зацеплением
7
+ */
8
+ export declare const cbc: (cipher: Cipher, iv: TArg<Uint8Array>) => BlockMode;
package/modes/cbc.js ADDED
@@ -0,0 +1,42 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ /**
4
+ * **EN:** Cipher Block Chaining (CBC) mode
5
+ *
6
+ * **RU:** Режим простой замены с зацеплением
7
+ */
8
+ export const cbc = (cipher, iv) => {
9
+ if (iv.length == 0 || iv.length % cipher.blockSize !== 0)
10
+ throw new Error("Invalid IV size");
11
+ const encrypter = cipher.encrypt.bind(cipher);
12
+ const decrypter = cipher.decrypt.bind(cipher);
13
+ return {
14
+ encrypt: (plaintext) => {
15
+ if (plaintext.length == 0 || plaintext.length % cipher.blockSize !== 0)
16
+ throw new Error("Data not aligned");
17
+ let r = [];
18
+ for (let i = 0; i < iv.length; i += cipher.blockSize)
19
+ r.push(iv.slice(i, i + cipher.blockSize));
20
+ const result = [];
21
+ for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
22
+ result.push(encrypter(xorBytes(r[0], plaintext.subarray(i, i + cipher.blockSize))));
23
+ r = r.slice(1).concat(result[result.length - 1]);
24
+ }
25
+ return concatBytes(...result);
26
+ },
27
+ decrypt: (ciphertext) => {
28
+ if (ciphertext.length == 0 || ciphertext.length % cipher.blockSize !== 0)
29
+ throw new Error("Data not aligned");
30
+ let r = [];
31
+ for (let i = 0; i < iv.length; i += cipher.blockSize)
32
+ r.push(iv.slice(i, i + cipher.blockSize));
33
+ const result = [];
34
+ for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
35
+ const blk = ciphertext.slice(i, i + cipher.blockSize);
36
+ result.push(xorBytes(r[0], decrypter(blk)));
37
+ r = r.slice(1).concat(blk);
38
+ }
39
+ return concatBytes(...result);
40
+ }
41
+ };
42
+ };
package/modes/cfb.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { type TArg } from "@noble/hashes/utils.js";
2
+ import type { BlockMode, Cipher } from "../types.js";
3
+ /**
4
+ * **EN:** Cipher Feedback (CFB) mode
5
+ *
6
+ * **RU:** Режим гаммирования с обратной связью по шифртексту
7
+ */
8
+ export declare const cfb: (cipher: Cipher, iv: TArg<Uint8Array>) => BlockMode;
package/modes/cfb.js ADDED
@@ -0,0 +1,37 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { getPadLength, xorBytes } from "../utils.js";
3
+ /**
4
+ * **EN:** Cipher Feedback (CFB) mode
5
+ *
6
+ * **RU:** Режим гаммирования с обратной связью по шифртексту
7
+ */
8
+ export const cfb = (cipher, iv) => {
9
+ if (iv.length == 0 || iv.length % cipher.blockSize !== 0)
10
+ throw new Error("Invalid IV size");
11
+ const encrypter = cipher.encrypt.bind(cipher);
12
+ return {
13
+ encrypt: (plaintext) => {
14
+ let r = [];
15
+ for (let i = 0; i < iv.length; i += cipher.blockSize)
16
+ r.push(iv.slice(i, i + cipher.blockSize));
17
+ const result = [];
18
+ for (let i = 0; i < (plaintext.length + getPadLength(plaintext.length, cipher.blockSize)); i += cipher.blockSize) {
19
+ result.push(xorBytes(encrypter(r[0]), plaintext.subarray(i, i + cipher.blockSize)));
20
+ r = r.slice(1).concat(result[result.length - 1]);
21
+ }
22
+ return concatBytes(...result);
23
+ },
24
+ decrypt: (ciphertext) => {
25
+ let r = [];
26
+ for (let i = 0; i < iv.length; i += cipher.blockSize)
27
+ r.push(iv.slice(i, i + cipher.blockSize));
28
+ const result = [];
29
+ for (let i = 0; i < (ciphertext.length + getPadLength(ciphertext.length, cipher.blockSize)); i += cipher.blockSize) {
30
+ const blk = ciphertext.slice(i, i + cipher.blockSize);
31
+ result.push(xorBytes(encrypter(r[0]), blk));
32
+ r = r.slice(1).concat(blk);
33
+ }
34
+ return concatBytes(...result);
35
+ }
36
+ };
37
+ };
package/modes/ctr.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { type TArg } from "@noble/hashes/utils.js";
2
+ import type { Cipher, StreamMode } from "../types.js";
3
+ import type { Magma } from "../magma/index.js";
4
+ /**
5
+ * **EN:** Counter (CTR) mode
6
+ *
7
+ * **RU:** Режим гаммирования
8
+ */
9
+ export declare const ctr: (cipher: Cipher, iv: TArg<Uint8Array>, isAcpkm?: boolean, _isAcpkmOmac?: boolean) => StreamMode;
10
+ /**
11
+ * **EN:** Counter (CTR) mode (GOST 28147-89)
12
+ *
13
+ * **RU:** Режим гаммирования (ГОСТ 28147-89)
14
+ */
15
+ export declare const cnt: (cipher: Magma, iv: TArg<Uint8Array>) => StreamMode;
package/modes/ctr.js ADDED
@@ -0,0 +1,62 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { getPadLength, xorBytes } from "../utils.js";
3
+ import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
4
+ import { acpkm } from "./_keytransform.js";
5
+ /**
6
+ * **EN:** Counter (CTR) mode
7
+ *
8
+ * **RU:** Режим гаммирования
9
+ */
10
+ export const ctr = (cipher, iv, isAcpkm, _isAcpkmOmac) => {
11
+ const halfBlockSize = cipher.blockSize / 2;
12
+ if (iv.length !== halfBlockSize)
13
+ throw new Error("Invalid IV size");
14
+ return {
15
+ crypt: (msg) => {
16
+ let encrypter = cipher.encrypt.bind(cipher);
17
+ const ctrMax = 1n << (8n * BigInt(halfBlockSize));
18
+ const maxSize = ctrMax * BigInt(cipher.blockSize);
19
+ if (BigInt(msg.length) > maxSize)
20
+ throw new Error("Too big data");
21
+ let acpkmSectionSize = 0;
22
+ if (isAcpkm)
23
+ acpkmSectionSize = _isAcpkmOmac ? (cipher.blockSize == 16 ? 6 : 10) : 2;
24
+ const keystreamBlocks = [];
25
+ for (let ctr = 0; ctr < Math.ceil(msg.length / cipher.blockSize); ctr++) {
26
+ if (isAcpkm && ctr != 0 && (ctr % acpkmSectionSize) == 0) {
27
+ // @ts-ignore
28
+ const cipher2 = new cipher.constructor(acpkm(encrypter, cipher.blockSize));
29
+ encrypter = cipher2.encrypt.bind(cipher2);
30
+ }
31
+ keystreamBlocks.push(encrypter(concatBytes(iv, numberToBytesBE(ctr, halfBlockSize))));
32
+ }
33
+ return xorBytes(concatBytes(...keystreamBlocks), msg);
34
+ }
35
+ };
36
+ };
37
+ /**
38
+ * **EN:** Counter (CTR) mode (GOST 28147-89)
39
+ *
40
+ * **RU:** Режим гаммирования (ГОСТ 28147-89)
41
+ */
42
+ export const cnt = (cipher, iv) => {
43
+ if (iv.length !== cipher.blockSize)
44
+ throw new Error("Invalid IV size");
45
+ const C1 = 0x01010104n;
46
+ const C2 = 0x01010101n;
47
+ const encrypter = cipher.encrypt.bind(cipher);
48
+ return {
49
+ crypt: (msg) => {
50
+ const encryptedIv = encrypter(iv).reverse();
51
+ let n2 = (bytesToNumberBE(encryptedIv.subarray(0, 4)));
52
+ let n1 = (bytesToNumberBE(encryptedIv.subarray(4)));
53
+ const gamma = [];
54
+ for (let i = 0; i < (msg.length + getPadLength(msg.length, cipher.blockSize)); i += cipher.blockSize) {
55
+ n1 = (n1 + C2) % 0x100000000n;
56
+ n2 = (n2 + C1) % 0xffffffffn;
57
+ gamma.push(encrypter(concatBytes(numberToBytesBE(n2, 4), numberToBytesBE(n1, 4)).reverse()));
58
+ }
59
+ return xorBytes(concatBytes(...gamma), msg);
60
+ }
61
+ };
62
+ };
package/modes/ecb.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { BlockMode, Cipher } from "../types.js";
2
+ /**
3
+ * **EN:** Electronic Codebook (ECB) mode
4
+ *
5
+ * **RU:** Режим простой замены
6
+ */
7
+ export declare const ecb: (cipher: Cipher) => BlockMode;