@li0ard/dstu 0.1.7 → 0.1.9

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 (45) hide show
  1. package/README.md +6 -2
  2. package/dstu4145/const.d.ts +2 -0
  3. package/dstu4145/const.js +42 -15
  4. package/dstu4145/ec/expand.d.ts +4 -3
  5. package/dstu4145/ec/expand.js +1 -2
  6. package/dstu4145/ec/index.js +1 -2
  7. package/dstu4145/ec/wnaf.d.ts +2 -1
  8. package/dstu4145/ecies.d.ts +82 -0
  9. package/dstu4145/ecies.js +62 -0
  10. package/dstu4145/{ec/math.d.ts → gf2m/index.d.ts} +1 -0
  11. package/dstu4145/gf2m/index.js +78 -0
  12. package/dstu4145/{ec → gf2m}/onb.js +31 -17
  13. package/dstu4145/gf2m/utils.d.ts +10 -0
  14. package/dstu4145/gf2m/utils.js +262 -0
  15. package/dstu4145/index.d.ts +23 -177
  16. package/dstu4145/index.js +37 -20
  17. package/dstu9041/curve.d.ts +4 -12
  18. package/dstu9041/curve.js +4 -3
  19. package/dstu9041/index.d.ts +2 -8
  20. package/dstu9041/index.js +2 -3
  21. package/dstu9311/hash.d.ts +10 -3
  22. package/dstu9311/hash.js +10 -3
  23. package/iso15946.d.ts +3 -0
  24. package/iso15946.js +49 -0
  25. package/kalyna/index.d.ts +2 -1
  26. package/kalyna/index.js +23 -28
  27. package/keywrap.d.ts +3 -3
  28. package/keywrap.js +21 -16
  29. package/modes/ccm.d.ts +4 -0
  30. package/modes/ccm.js +47 -0
  31. package/modes/gcm.d.ts +1 -1
  32. package/modes/gcm.js +1 -2
  33. package/modes/gmac.js +1 -2
  34. package/modes/index.d.ts +1 -0
  35. package/modes/index.js +1 -0
  36. package/modes/kw.d.ts +2 -6
  37. package/modes/xts.d.ts +2 -6
  38. package/modes/xts.js +2 -2
  39. package/package.json +6 -6
  40. package/strumok/index.d.ts +2 -1
  41. package/types.d.ts +77 -0
  42. package/utils.d.ts +1 -0
  43. package/utils.js +2 -2
  44. package/dstu4145/ec/math.js +0 -206
  45. /package/dstu4145/{ec → gf2m}/onb.d.ts +0 -0
package/README.md CHANGED
@@ -20,7 +20,9 @@ npm i @li0ard/dstu
20
20
 
21
21
  ## Supported algorithms
22
22
 
23
- - Curves and ECDSA (DSTU 4145-2002, polynomial and optimal normal basis)
23
+ - Curves and ECDSA (DSTU 4145-2002)
24
+ - Supports polynomial and optimal normal basis
25
+ - Supports ECDH and encryption
24
26
  - Kalyna cipher (DSTU 7624:2014)
25
27
  - Kupyna hash function (DSTU 7564:2014)
26
28
  - Supports KMAC
@@ -28,10 +30,12 @@ npm i @li0ard/dstu
28
30
  - ECIES encryption schema over Twisted Edwards curves (DSTU 9041:2020)
29
31
  - DSTU 9311:2024 cipher (republished DSTU GOST 28147:2009)
30
32
  - GOST 34.311-95 hash function over DSTU 9311:2024
33
+ - Supports HMAC
31
34
 
32
35
  ## Supported cipher modes
33
36
 
34
37
  - Cipher Block Chaining mode (CBC)
38
+ - Counter with CBC-MAC (CCM) mode
35
39
  - Cipher Feedback mode (CFB)
36
40
  - Counter mode (CTR)
37
41
  - Electronic Codebook mode (ECB)
@@ -39,7 +43,7 @@ npm i @li0ard/dstu
39
43
  - Message Authentication Code mode (MAC and GMAC)
40
44
  - Output Feedback mode (OFB)
41
45
  - Key wrapping (KW)
42
- - Supports [`Dstu7624Wrap`](https://zakon.rada.gov.ua/laws/show/z0108-13)
46
+ - Supports [`Dstu7624Wrap`/`GOST28147Wrap`](https://zakon.rada.gov.ua/laws/show/z0108-13)
43
47
  - XEX Tweakable Block Ciphertext Stealing (XTS)
44
48
 
45
49
  ## Supported curves
@@ -28,6 +28,8 @@ export interface DSTUParameters extends DSTUShortParameters {
28
28
  Gx: string;
29
29
  /** Base point `Y` coordinate */
30
30
  Gy: string;
31
+ /** Curve OID's */
32
+ oids?: string[];
31
33
  }
32
34
  /** DSTU 4145-2002 163 bit curve */
33
35
  export declare const DSTU_163: DSTUParameters;
package/dstu4145/const.js CHANGED
@@ -1,3 +1,15 @@
1
+ const generateOids_pb = (index) => [
2
+ `1.2.804.2.1.1.1.1.3.1.1.2.${index}`, // DSTU 4145 with GOST 34.311-95
3
+ `1.2.804.2.1.1.1.1.3.6.1.1.2.${index}`, // DSTU 4145 with Kupyna-256
4
+ `1.2.804.2.1.1.1.1.3.6.2.1.2.${index}`, // DSTU 4145 with Kupyna-384
5
+ `1.2.804.2.1.1.1.1.3.6.3.1.2.${index}`, // DSTU 4145 with Kupyna-512
6
+ ];
7
+ const generateOids_onb = (index) => [
8
+ `1.2.804.2.1.1.1.1.3.1.2.2.${index}`, // DSTU 4145 with GOST 34.311-95
9
+ `1.2.804.2.1.1.1.1.3.6.1.2.2.${index}`, // DSTU 4145 with Kupyna-256
10
+ `1.2.804.2.1.1.1.1.3.6.2.2.2.${index}`, // DSTU 4145 with Kupyna-384
11
+ `1.2.804.2.1.1.1.1.3.6.3.2.2.${index}` // DSTU 4145 with Kupyna-512
12
+ ];
1
13
  /** DSTU 4145-2002 163 bit curve */
2
14
  export const DSTU_163 = {
3
15
  a: 1,
@@ -7,7 +19,8 @@ export const DSTU_163 = {
7
19
  Gx: "02E2F85F5DD74CE983A5C4237229DAF8A3F35823BE",
8
20
  Gy: "03826F008A8C51D7B95284D9D03FF0E00CE2CD723A",
9
21
  m: 163,
10
- ks: [7, 6, 3]
22
+ ks: [7, 6, 3],
23
+ oids: generateOids_pb("0")
11
24
  };
12
25
  /** DSTU 4145-2002 163 bit curve (for testing) */
13
26
  export const DSTU_163_TEST = {
@@ -29,7 +42,8 @@ export const DSTU_167 = {
29
42
  Gx: "7A1F6653786A68192803910A3D30B2A2018B21CD54",
30
43
  Gy: "5F49EB26781C0EC6B8909156D98ED435E45FD59918",
31
44
  m: 167,
32
- ks: [6]
45
+ ks: [6],
46
+ oids: generateOids_pb("1")
33
47
  };
34
48
  /** DSTU 4145-2002 173 bit curve */
35
49
  export const DSTU_173 = {
@@ -40,7 +54,8 @@ export const DSTU_173 = {
40
54
  Gx: "04D41A619BCC6EADF0448FA22FAD567A9181D37389CA",
41
55
  Gy: "10B51CC12849B234C75E6DD2028BF7FF5C1CE0D991A1",
42
56
  m: 173,
43
- ks: [10, 2, 1]
57
+ ks: [10, 2, 1],
58
+ oids: generateOids_pb("2")
44
59
  };
45
60
  const ONB_173 = {
46
61
  matrix: "005659AA54E60F1C270590C01035040F062A360C21319547C8881C2536348844A8858A454B743CD022420C9039670200586546551C325F18F148D1C0D38A4169BA20C00245CD97492A38ED5072E38E873E16A84C18A5E7C8064F2E20254023404E98323E521978E30151950B15EC4C30C44114895237125DD8D3A18D04294030090864A61298544FFC5831D1A14E901011B0CC68B402921741623005B0244715204A3DBC68C98426D08F385028354A33E6511610077A3CD034A64B08476B9EC87873A20F0652876A48A033E007024760E05C41154301011A2D384150F4C1468153A30ABD20A2E1C50223304C650865A28D884C74C244017480CC96490E16E1D8340294831D2414E12195640A012611D6B0B882C2C98253448E1421A4A01393192922A9E9C448120B190CAC6470230414873C3842E9D014844D162E10941848E4C78E1B3D253E1532C260C8895255F8DC4144810F050B150EEDA933850D8D2520366DE191E39324141A8AC468E262D0804751B8F8F8F30155922635A4241034E7D2195697000000000000000000000000",
@@ -57,7 +72,8 @@ export const DSTU_173_ONB = {
57
72
  Gy: "005A43D5EE1F400349CF35150C74F119A39145F0CB08",
58
73
  m: 173,
59
74
  ks: [10, 2, 1],
60
- onb: ONB_173
75
+ onb: ONB_173,
76
+ oids: generateOids_onb("0")
61
77
  };
62
78
  /** DSTU 4145-2002 173 bit curve (ONB, for testing) */
63
79
  export const DSTU_173_ONB_TEST = {
@@ -80,7 +96,8 @@ export const DSTU_179 = {
80
96
  Gx: "06BA06FE51464B2BD26DC57F48819BA9954667022C7D03",
81
97
  Gy: "025FBC363582DCEC065080CA8287AAFF09788A66DC3A9E",
82
98
  m: 179,
83
- ks: [4, 2, 1]
99
+ ks: [4, 2, 1],
100
+ oids: generateOids_pb("3")
84
101
  };
85
102
  /** DSTU 4145-2002 179 bit curve (ONB) */
86
103
  export const DSTU_179_ONB = {
@@ -96,7 +113,8 @@ export const DSTU_179_ONB = {
96
113
  matrix: "00620D1132C2500E332DEE346960A0D4274A6FD84C89248193020A0656C1F124698D8F281AF424F1D3605525523F22713975C04E82251B8CF44863E3D00B3F223E015145058E1B526C5075DA12618E0746633C5870F4410C842C314C08D861E3D288572E8EE000A4E44A86334F709C208425C90B509642309041204C94500E9820A9D3A2D5814D4CF020D8C2E211192F1DA850283549559B496DDEB849256994941E0330C1B85243500E574E58A498F3C5D1854532F63C8092820C0639557E70E88400129D1510149D000463101A1A18BC00A11427D584350444ED019341100A332A5A39C86347D311421A42FDC9E3A2D0924C3778940800201609583A6888A854C55397511B32A9292200110921355E9D2914C888094C88D0E8B01224C4053E62140D4883E5D21F43485CE1A153460B0A270FCA8079B222509644378A0418B203D6214B41FACCB0B0805184478DE454E89240930F514A0641F96265CC8C46203A283015674A01540B207D58A4258C0A3D4D5881EA5300848453153671A8C385D31D56A62630E023454D8C443C8894904504000000000000",
97
114
  root1: "61459B574DD5042A1F24458913913559A0E436E811FA0200",
98
115
  root2: "2DABB4A422BFA53E33FB9276C3E05A0E4F6F7A373CBE0600"
99
- }
116
+ },
117
+ oids: generateOids_onb("1")
100
118
  };
101
119
  /** DSTU 4145-2002 191 bit curve */
102
120
  export const DSTU_191 = {
@@ -107,7 +125,8 @@ export const DSTU_191 = {
107
125
  Gx: "714114B762F2FF4A7912A6D2AC58B9B5C2FCFE76DAEB7129",
108
126
  Gy: "29C41E568B77C617EFE5902F11DB96FA9613CD8D03DB08DA",
109
127
  m: 191,
110
- ks: [9]
128
+ ks: [9],
129
+ oids: generateOids_pb("4")
111
130
  };
112
131
  /** DSTU 4145-2002 191 bit curve (ONB) */
113
132
  export const DSTU_191_ONB = {
@@ -123,7 +142,8 @@ export const DSTU_191_ONB = {
123
142
  matrix: "007AD9AA05A4D59442374A64B0222400805E417851F921C24D854C9412C91922A00A09201776190A24058C0C3462B068E844A8060A4B6F422CF8D4C3911A3346FED4D1B380D41A2C3C50A518C4211213181564299AD4010504530CFCB89135075384311C36C5F0B2A4C78A5147D268F9D2020F8C4C1E620D88A3A3C985568274694065835313431B588531132514834A74F6B4A133A14F1D506C5A2858F461CB0B231342EDF1C387928458441039C951C394885995FA50D0E3E2539651671A815195454D185180A01489E040920C551B029D599586510C482C9420F850E0CE91462ED6A049E56947065C025E05A93562170E5633003528F380888938360E0DDA6401928F2E20121C5851C0D1165176CA4890F30456095C016C55E1E58A96852521483DF2A4E2CA004418E08C9842439709424E4EC1C171428A835563227DD1B4E80E05365A18E5F192E011121E043A75387589C6832737720D7985814A0E5A6F4AF950A2000F17450430A5E0244990985485725DD141C2D521263F66696AF3828F9D39452691B9D400CA093233A240B0F4A714A83C2092B0E8622089010407B82051E121D10E58566E09F1750B000000",
124
143
  root1: "2B54ACCD849AD972B18FA0B5162FA9DBBF3FA51AF512FE58",
125
144
  root2: "1FCF56EBC3149F0107368DC9BE706078A534C0521E141632"
126
- }
145
+ },
146
+ oids: generateOids_onb("2")
127
147
  };
128
148
  /** DSTU 4145-2002 233 bit curve */
129
149
  export const DSTU_233 = {
@@ -134,7 +154,8 @@ export const DSTU_233 = {
134
154
  Gx: "3FCDA526B6CDF83BA1118DF35B3C31761D3545F32728D003EEB25EFE96",
135
155
  Gy: "9CA8B57A934C54DEEDA9E54A7BBAD95E3B2E91C54D32BE0B9DF96D8D35",
136
156
  m: 233,
137
- ks: [9, 4, 1]
157
+ ks: [9, 4, 1],
158
+ oids: generateOids_pb("5")
138
159
  };
139
160
  /** DSTU 4145-2002 233 bit curve (ONB) */
140
161
  export const DSTU_233_ONB = {
@@ -150,7 +171,8 @@ export const DSTU_233_ONB = {
150
171
  matrix: "00CE5DB366A21824171496DD6903C44A8D52430A8139A5451828573ED04461D461128A0000CE9D30172951894A199EDDD2F304D09C0C04A6EDD165AB8C025514C6B9E16527938C1402242D32B1A1DC8B474DC668E9F6EAD7A7576A82C1F224C60D166533F6D471B6A056924A1EA6417166269725444846805805474D825F638401E396A6C7012520642960F6A6D304302CBC85CA52035C184E42724460E1E0948D6E3F82ADF1F5265518521282DCB0D320990A4131CC58A895881AAE0E18B2093876C29AA272158891618260101A6D298E718864A19A255A24FC1C19B2E3DB962F4C1AA9C074E6098C681AE4903844A48A9119049EED9246878B131D2EC60551A66917033D5DA215F85364CA08596B54795AD64BDC95293F3EC10116CB15826D8D0441B175A95985470986C1F295A554A76FA808AD58C2808E813C3B0A35F136CC94985E34DA500947A40F8D2B45E2B0F121629B875B67A405CAF382D8164784BC1C60B520D7075A210EF1E853400E153F56C01922A563D31D713640B1E126A5558823060C71C12364540B091036390805249C292C5578F1493600D385401B8A39218727C70454A2BE34C8340819A36C01AE493267AC9A06447F66C591F201498F59AC9288C015451BA545178CC10054821A344A036270B8A408521972CC52B5E08463D4031C0F2240107543D38A3A5E742969B62A161947695A2DE9C2638E11687C64604036091B0A74D80000000000",
151
172
  root1: "ABBFC55B841C74F461EC5DC0D81A2013C2A69BF19F97AAB01B70D2498F010000",
152
173
  root2: "214C08346A28283EB5F281C5D8604E518202C240A9121EFA89FD28E6CD000000"
153
- }
174
+ },
175
+ oids: generateOids_onb("3")
154
176
  };
155
177
  /** DSTU 4145-2002 257 bit curve */
156
178
  export const DSTU_257 = {
@@ -161,7 +183,8 @@ export const DSTU_257 = {
161
183
  Gx: "002A29EF207D0E9B6C55CD260B306C7E007AC491CA1B10C62334A9E8DCD8D20FB7",
162
184
  Gy: "010686D41FF744D4449FCCF6D8EEA03102E6812C93A9D60B978B702CF156D814EF",
163
185
  m: 257,
164
- ks: [12]
186
+ ks: [12],
187
+ oids: generateOids_pb("6")
165
188
  };
166
189
  /** DSTU 4145-2002 307 bit curve */
167
190
  export const DSTU_307 = {
@@ -172,7 +195,8 @@ export const DSTU_307 = {
172
195
  Gx: "0216EE8B189D291A0224984C1E92F1D16BF75CCD825A087A239B276D3167743C52C02D6E7232AA",
173
196
  Gy: "05D9306BACD22B7FAEB09D2E049C6E2866C5D1677762A8F2F2DC9A11C7F7BE8340AB2237C7F2A0",
174
197
  m: 307,
175
- ks: [8, 4, 2]
198
+ ks: [8, 4, 2],
199
+ oids: generateOids_pb("7")
176
200
  };
177
201
  /** DSTU 4145-2002 367 bit curve */
178
202
  export const DSTU_367 = {
@@ -183,7 +207,8 @@ export const DSTU_367 = {
183
207
  Gx: "324A6EDDD512F08C49A99AE0D3F961197A76413E7BE81A400CA681E09639B5FE12E59A109F78BF4A373541B3B9A1",
184
208
  Gy: "01AB597A5B4477F59E39539007C7F977D1A567B92B043A49C6B61984C3FE3481AAF454CD41BA1F051626442B3C10",
185
209
  m: 367,
186
- ks: [21]
210
+ ks: [21],
211
+ oids: generateOids_pb("8")
187
212
  };
188
213
  /** DSTU 4145-2002 431 bit curve */
189
214
  export const DSTU_431 = {
@@ -194,7 +219,8 @@ export const DSTU_431 = {
194
219
  Gx: "1A62BA79D98133A16BBAE7ED9A8E03C32E0824D57AEF72F88986874E5AAE49C27BED49A2A95058068426C2171E99FD3B43C5947C857D",
195
220
  Gy: "70B5E1E14031C1F70BBEFE96BDDE66F451754B4CA5F48DA241F331AA396B8D1839A855C1769B1EA14BA53308B5E2723724E090E02DB9",
196
221
  m: 431,
197
- ks: [5, 3, 1]
222
+ ks: [5, 3, 1],
223
+ oids: generateOids_pb("9")
198
224
  };
199
225
  /** DSTU 4145-2002 431 bit curve (ONB) */
200
226
  export const DSTU_431_ONB = {
@@ -210,5 +236,6 @@ export const DSTU_431_ONB = {
210
236
  matrix: "005A33F2B708E339C564D9394018655904A0396A7ED9F747AD3CAD0D0157E91648F310BD37721DAA8CA25EB7CF7B8568E8E94AA7995CA638DB9374C22FA5AF9F8E9122F422E1847503CE32081AE69C32D50FC61A2101420D0BB657E7D61AD9255B1C8F3DB4AE0CED8DCA89C9602D4DAA474EF306CFBA72DA1048462833B3500C2A60CBAFB09799B4588223DA51D38E77340CAA314CD7EABED15BE3F1E038A0B150BB0B303E51DD2E71084145A820F17B4932B28A9E4ADAB2AA476D0FD18155325CCAC2B0ADB55CF5E5B9D6074B8568C38A5930C98762B5A29CBEA5506908EBAD4E2B26A299760026906AAA9C8283BB89A3C0BB67192F330D26110E2108309F89D80A96ABCBBAF296615323531222394A03DAE6456EB859A9A665396A6DCF0F9273686D89EB31F0B7AC4C17161AE5A1580CA815BBEE71598BB01C905C40CA111932CE16C12CE1F2403B34A0807462FC2D585DD69C136431BC9A7CDA292935546778C66265E717067905445BC9DA22E50742221EB3432DE11D94BA6DD32440A462930DBC43378E7114201D23D6191529A0FB85F4DAAA7E7AE179E6C614A8CA9336F31DC7EDD79FD10ED312D821C1EDC12733C88A489DD07103470234FE5B1466F5E28BC92C35E1CB811EB06D7566AA8A5BB4E94AC028406FA60788A6383A5D06FBC803876A16987C9E1A9D980A694C2C06A45E10D60B5FA8364B2091B91BE574D0C510CC5DFA6A31328C61A3A29E845141AA3168A278711867E7E13C3F17F2410B7607D606BD3A3B5B2E6568A9A7AE570B9394DB52A616546F22DED9B9AB9506CC3B79BE8298A10E10A3E164529328CB8D19CBF7707D8A5C08A531611D9E31B947497229AFB7945274AA82E8C0B7356EEA4002016CB2BEE52431990CAAAF3DCA9596986826654C0380EA020068FD8F35BA7E607E262078A3730AA6B8529282ACD04508B1554013A9C98DF43C7DBEA85A806AED6549AE3EAA62CBF786572C88C0C4ADD2F6C325979121077FC4A645161E7CA79C95C1A9840689C36D0BAE488AA0A7C4CBA772B6C1DB2CE0238D0C18A303AAA834BFB934096B1339A7E5C75970625E9B2A8D4281862E420BFA1BF68809480E5033B37BB0ACBB8B32CA750855C9592436256E16098B69CA83C425C426B119E8338ECC3E331C38BAEA259D6C7A046F4DDBF12DBC4C114CDBFC39C433A2BC03716A72BBC6DD08271E4E5A3C460C328541561882388C55691A829C9A1EE2EC8DF2DF4853941841BC66C60785EDC60E42DC62E1089432443521176CEFAECCAF58057968209AAFA87DA612BBF2E2310184F04EEB508649F191D452D63D6A2CC66CC6BAFD0A26F28906691069181C5E8937C131DA8DEA003672DD0800000000000000",
211
237
  root1: "595A5822496675B56C7EAE7E537D60F655C784A9B84DDEADE42431B1C324AE818AC7DCBC0E7370E69A16C3268FF66E2F332DD2A013450000",
212
238
  root2: "27FA953C557581AD37A991EB2FB46B7C65DBE2B2CA021FEFAD6AD3EAFB73C14E1A528B8E5C778705B9DFEA18D6F76D125175130A020A0000"
213
- }
239
+ },
240
+ oids: generateOids_onb("4")
214
241
  };
@@ -1,6 +1,7 @@
1
1
  import type { DSTUShortParameters } from "../const.js";
2
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
3
  /** Uncompress point without creating full API curve */
3
- export declare const expandPoint: (xBytes: Uint8Array, params: DSTUShortParameters) => {
4
- x: Uint8Array;
5
- y: Uint8Array;
4
+ export declare const expandPoint: (xBytes: TArg<Uint8Array>, params: DSTUShortParameters) => {
5
+ x: TRet<Uint8Array>;
6
+ y: TRet<Uint8Array>;
6
7
  };
@@ -1,5 +1,4 @@
1
- import { init_onb_parameters } from "./onb.js";
2
- import { createField } from "./math.js";
1
+ import { createField, init_onb_parameters } from "../gf2m/index.js";
3
2
  /** Uncompress point without creating full API curve */
4
3
  export const expandPoint = (xBytes, params) => {
5
4
  const { m, ks, a, onb } = params;
@@ -1,8 +1,7 @@
1
1
  import { concatBytes } from "@noble/hashes/utils.js";
2
2
  import BN from "bn.js";
3
3
  import { bitLength, getWindowSize, windowNaf } from "./wnaf.js";
4
- import { init_onb_parameters } from "./onb.js";
5
- import { createField } from "./math.js";
4
+ import { createField, init_onb_parameters } from "../gf2m/index.js";
6
5
  export const binaryWeierstrass = (parameters) => {
7
6
  const field = createField(parameters.m, parameters.ks);
8
7
  const onb = parameters.onb ? init_onb_parameters(parameters) : undefined;
@@ -1,4 +1,5 @@
1
+ import type { TRet } from "@noble/hashes/utils.js";
1
2
  import BN from "bn.js";
2
3
  export declare const bitLength: (n: number) => number;
3
- export declare const windowNaf: (width: number, k: BN) => Int32Array;
4
+ export declare const windowNaf: (width: number, k: BN) => TRet<Int32Array>;
4
5
  export declare const getWindowSize: (bits: number) => number;
@@ -0,0 +1,82 @@
1
+ import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ import type { ECDSA } from "../types.js";
3
+ /** Encrypted message */
4
+ export type EncryptedMessage = {
5
+ /** User keying material (UKM) for KDF */
6
+ ukm?: TArg<Uint8Array>;
7
+ /** Encryption IV */
8
+ iv: TArg<Uint8Array>;
9
+ /** Wrapped encryption key */
10
+ wcek: TArg<Uint8Array>;
11
+ /** Encrypted message */
12
+ data: TArg<Uint8Array>;
13
+ };
14
+ /**
15
+ * ECIES schema over DSTU 4145-2002.
16
+ * @param curve - Curve to use.
17
+ * @param hash - Hash function to use. (Output length must be 32 bytes)
18
+ * @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
19
+ * @param useDstu9311 - Whether to use DSTU 9311:2024 instead of Kalyna-256?
20
+ */
21
+ export declare const dstu4145Encrypter: (curve: ECDSA, hash: CHash<Hash<any>>, withCofactor?: boolean, useDstu9311?: boolean) => Readonly<{
22
+ /**
23
+ * Encrypt message
24
+ * @param secretKeyA - Sender secret key.
25
+ * @param publicKeyB - Recipient public key.
26
+ * @param plaintext - Message to encrypt.
27
+ */
28
+ encrypt: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, plaintext: TArg<Uint8Array>) => EncryptedMessage;
29
+ /**
30
+ * Decrypt message
31
+ * @param secretKeyB - Recipient secret key.
32
+ * @param publicKeyA - Sender public key.
33
+ * @param encryptedMessage - Encrypted message object.
34
+ */
35
+ decrypt: (secretKeyB: TArg<Uint8Array>, publicKeyA: TArg<Uint8Array>, encryptedMessage: EncryptedMessage) => TRet<Uint8Array>;
36
+ }>;
37
+ /**
38
+ * ECIES schema over DSTU 4145-2002.
39
+ *
40
+ * Uses GOST 34.311-95 as hash function.
41
+ * @param curve - Curve to use.
42
+ * @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
43
+ */
44
+ export declare const dstu4145EncrypterWithGost: (curve: ECDSA, withCofactor?: boolean) => Readonly<{
45
+ /**
46
+ * Encrypt message
47
+ * @param secretKeyA - Sender secret key.
48
+ * @param publicKeyB - Recipient public key.
49
+ * @param plaintext - Message to encrypt.
50
+ */
51
+ encrypt: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, plaintext: TArg<Uint8Array>) => EncryptedMessage;
52
+ /**
53
+ * Decrypt message
54
+ * @param secretKeyB - Recipient secret key.
55
+ * @param publicKeyA - Sender public key.
56
+ * @param encryptedMessage - Encrypted message object.
57
+ */
58
+ decrypt: (secretKeyB: TArg<Uint8Array>, publicKeyA: TArg<Uint8Array>, encryptedMessage: EncryptedMessage) => TRet<Uint8Array>;
59
+ }>;
60
+ /**
61
+ * ECIES schema over DSTU 4145-2002.
62
+ *
63
+ * Uses Kupyna-256 as hash function.
64
+ * @param curve - Curve to use.
65
+ * @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
66
+ */
67
+ export declare const dstu4145EncrypterWithKupyna: (curve: ECDSA, withCofactor?: boolean) => Readonly<{
68
+ /**
69
+ * Encrypt message
70
+ * @param secretKeyA - Sender secret key.
71
+ * @param publicKeyB - Recipient public key.
72
+ * @param plaintext - Message to encrypt.
73
+ */
74
+ encrypt: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, plaintext: TArg<Uint8Array>) => EncryptedMessage;
75
+ /**
76
+ * Decrypt message
77
+ * @param secretKeyB - Recipient secret key.
78
+ * @param publicKeyA - Sender public key.
79
+ * @param encryptedMessage - Encrypted message object.
80
+ */
81
+ decrypt: (secretKeyB: TArg<Uint8Array>, publicKeyA: TArg<Uint8Array>, encryptedMessage: EncryptedMessage) => TRet<Uint8Array>;
82
+ }>;
@@ -0,0 +1,62 @@
1
+ import { randomBytes } from "@noble/hashes/utils.js";
2
+ import { Dstu9311, gost3431195 } from "../dstu9311/index.js";
3
+ import { Kalyna256 } from "../kalyna/index.js";
4
+ import { keyWrap } from "../keywrap.js";
5
+ import { cfb } from "../modes/cfb.js";
6
+ import { kupyna256 } from "../kupyna/index.js";
7
+ import { iso15946_kdf } from "../iso15946.js";
8
+ /**
9
+ * ECIES schema over DSTU 4145-2002.
10
+ * @param curve - Curve to use.
11
+ * @param hash - Hash function to use. (Output length must be 32 bytes)
12
+ * @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
13
+ * @param useDstu9311 - Whether to use DSTU 9311:2024 instead of Kalyna-256?
14
+ */
15
+ export const dstu4145Encrypter = (curve, hash, withCofactor = true, useDstu9311 = false) => {
16
+ const oid = useDstu9311 ? "1.2.804.2.1.1.1.1.1.1.5" : "1.2.804.2.1.1.1.1.1.3.11";
17
+ const Cipher = useDstu9311 ? Dstu9311 : Kalyna256;
18
+ const macLength = useDstu9311 ? 8 : 32;
19
+ const getSharedSecret = (sharedKey, ukm) => iso15946_kdf(hash, oid, sharedKey, ukm);
20
+ return Object.freeze({
21
+ /**
22
+ * Encrypt message
23
+ * @param secretKeyA - Sender secret key.
24
+ * @param publicKeyB - Recipient public key.
25
+ * @param plaintext - Message to encrypt.
26
+ */
27
+ encrypt: (secretKeyA, publicKeyB, plaintext) => {
28
+ const cek = randomBytes(32), ukm = randomBytes(64), iv = randomBytes(macLength);
29
+ const sharedKey = getSharedSecret(curve.getSharedSecret(secretKeyA, publicKeyB, withCofactor), ukm);
30
+ const wcek = keyWrap(sharedKey, useDstu9311).wrap(cek, iv);
31
+ const data = cfb(new Cipher(cek), iv).encrypt(plaintext);
32
+ return { ukm, iv, wcek, data };
33
+ },
34
+ /**
35
+ * Decrypt message
36
+ * @param secretKeyB - Recipient secret key.
37
+ * @param publicKeyA - Sender public key.
38
+ * @param encryptedMessage - Encrypted message object.
39
+ */
40
+ decrypt: (secretKeyB, publicKeyA, encryptedMessage) => {
41
+ const sharedKey = getSharedSecret(curve.getSharedSecret(secretKeyB, publicKeyA, withCofactor), encryptedMessage.ukm);
42
+ const cek = keyWrap(sharedKey, useDstu9311).unwrap(encryptedMessage.wcek);
43
+ return cfb(new Cipher(cek), encryptedMessage.iv).decrypt(encryptedMessage.data);
44
+ }
45
+ });
46
+ };
47
+ /**
48
+ * ECIES schema over DSTU 4145-2002.
49
+ *
50
+ * Uses GOST 34.311-95 as hash function.
51
+ * @param curve - Curve to use.
52
+ * @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
53
+ */
54
+ export const dstu4145EncrypterWithGost = (curve, withCofactor) => dstu4145Encrypter(curve, gost3431195, withCofactor, true);
55
+ /**
56
+ * ECIES schema over DSTU 4145-2002.
57
+ *
58
+ * Uses Kupyna-256 as hash function.
59
+ * @param curve - Curve to use.
60
+ * @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
61
+ */
62
+ export const dstu4145EncrypterWithKupyna = (curve, withCofactor) => dstu4145Encrypter(curve, kupyna256, withCofactor);
@@ -20,3 +20,4 @@ export declare const createField: (m: number, ks: number[]) => Readonly<{
20
20
  fromHexStringOrBytes: (v: string | Uint8Array) => BN;
21
21
  toBytes: (x: BN, length?: number) => TRet<Uint8Array>;
22
22
  }>;
23
+ export * from "./onb.js";
@@ -0,0 +1,78 @@
1
+ import BN from "bn.js";
2
+ import { bn2LE, invWords, le2BN, modWords, mulWords, sqrWords, WORD_BITS } from "./utils.js";
3
+ /** Compute curve modulo */
4
+ export const computeMod = (m, ks) => {
5
+ const modulo = new BN(0);
6
+ modulo.setn(m, 1);
7
+ modulo.setn(0, 1);
8
+ for (const i of ks)
9
+ modulo.setn(i, 1);
10
+ return modulo;
11
+ };
12
+ /** Create `GF(2^m)` field */
13
+ export const createField = (m, ks) => {
14
+ const modulo = computeMod(m, ks), wordsPerElement = Math.ceil(m / WORD_BITS), wordsForModWord = Math.floor(m / WORD_BITS) + 1;
15
+ const polyBits = Int32Array.from([m, ...[...ks].sort((a, b) => b - a), 0]), polyWordsForInv = bn2LE(modulo, wordsForModWord);
16
+ const modw = (a) => le2BN(modWords(a, polyBits).subarray(0, wordsPerElement));
17
+ const mod = (f) => {
18
+ const bl = f.bitLength();
19
+ if (bl <= m)
20
+ return f.clone();
21
+ return modw(bn2LE(f, Math.ceil(bl / WORD_BITS)));
22
+ };
23
+ const mul = (x, v) => x.eq(v)
24
+ ? sqr(x)
25
+ : modw(mulWords(bn2LE(x), bn2LE(v)));
26
+ const div = (x, v) => mul(x, invert(v));
27
+ const sqr = (x) => modw(sqrWords(bn2LE(x)));
28
+ const testBit = (x, i) => x.testn(i) ? 1 : 0;
29
+ const invBit = (x, i) => x.setn(i, !x.testn(i));
30
+ const sqrt = (x) => {
31
+ let r = mod(x);
32
+ for (let i = 0; i < m - 1; i++)
33
+ r = sqr(r);
34
+ return r;
35
+ };
36
+ const trace = (x) => {
37
+ let t = x;
38
+ for (let i = 1; i < m; i++)
39
+ t = sqr(t).ixor(x);
40
+ return testBit(t, 0);
41
+ };
42
+ const traceOnb = (x) => {
43
+ let t = 0;
44
+ for (let i = 0; i < m; i++)
45
+ t ^= testBit(x, i);
46
+ return t;
47
+ };
48
+ const htrace = (x) => {
49
+ let ht = x;
50
+ for (let i = 1; i <= Math.floor((m - 1) / 2); i++)
51
+ ht = sqr(sqr(ht)).ixor(x);
52
+ return ht;
53
+ };
54
+ const invert = (f) => {
55
+ const reducedWords = modWords(bn2LE(f), polyBits), words = new Uint32Array(wordsForModWord);
56
+ words.set(reducedWords.subarray(0, Math.min(reducedWords.length, wordsForModWord)));
57
+ return le2BN(invWords(words, polyWordsForInv).subarray(0, wordsPerElement));
58
+ };
59
+ const solve_quad = (v) => {
60
+ const a = mod(v), z = htrace(a);
61
+ if (sqr(z).ixor(z).cmp(a) == 0)
62
+ return mod(z);
63
+ throw new Error("squad eq fail: no square root exists");
64
+ };
65
+ const hashToField = (hash) => {
66
+ const h = new BN(hash);
67
+ return h.imaskn(Math.min(m, h.bitLength()));
68
+ };
69
+ const fromHexStringOrBytes = (v) => new BN(v, 16);
70
+ const toBytes = (x, length) => new Uint8Array(x.toArray("be", length));
71
+ return Object.freeze({
72
+ MODULO: modulo, LENGTH: Math.ceil(m / 8),
73
+ mod, mul, div, sqr, sqrt, invert,
74
+ solve_quad, trace, traceOnb, hashToField, invBit,
75
+ fromHexStringOrBytes, toBytes
76
+ });
77
+ };
78
+ export * from "./onb.js";
@@ -1,6 +1,6 @@
1
1
  import BN from "bn.js";
2
2
  import { hexToBytes } from "@noble/hashes/utils.js";
3
- import { createField } from "./math.js";
3
+ import { createField } from "./index.js";
4
4
  const decompress_matrix = (compress_mulp, mulp) => {
5
5
  const mlen = Math.ceil(compress_mulp.bitLength() / 9);
6
6
  if (mlen === 0)
@@ -12,38 +12,52 @@ const decompress_matrix = (compress_mulp, mulp) => {
12
12
  temp.ishrn(9);
13
13
  }
14
14
  };
15
+ const bnToBits = (x, m) => {
16
+ const bits = new Uint8Array(m);
17
+ for (let i = 0; i < m; i++)
18
+ bits[i] = x.testn(i) ? 1 : 0;
19
+ return bits;
20
+ };
21
+ const bitsToBN = (bits) => {
22
+ const r = new BN(0);
23
+ for (let j = 0; j < bits.length; j++)
24
+ if (bits[j])
25
+ r.setn(j, 1);
26
+ return r;
27
+ };
15
28
  export const init_onb_parameters = (parameters) => {
16
29
  if (!parameters.onb)
17
30
  throw new Error("Invalid curve: Curve doesn't support ONB");
18
31
  const { m, onb, ks } = parameters;
32
+ const m_sub_1 = m - 1;
19
33
  const mulp = new Uint16Array(2 * m - 1);
20
34
  const compress_mulp = new BN(hexToBytes(onb.matrix), "le");
21
35
  decompress_matrix(compress_mulp, mulp);
22
36
  const root1 = new BN(hexToBytes(onb.root1), "le"), root2 = new BN(hexToBytes(onb.root2), "le");
37
+ const root2Bits = bnToBits(root2, m);
38
+ const bitAt = (bits, pos) => bits[pos % m];
23
39
  const multiplyOnb = (x) => {
24
- const r = new BN(0);
25
- const xb = (pos) => (x.testn(pos % m) ? 1 : 0);
26
- const yb = (pos) => (root2.testn(pos % m) ? 1 : 0);
40
+ const xBits = bnToBits(x, m);
41
+ const out = new Uint8Array(m);
27
42
  for (let j = 0; j < m; j++) {
28
43
  let bit = 0;
29
- for (let i = 0; i < m - 1; i++) {
30
- const t1 = yb(mulp[2 * i] + j + 1);
31
- const t2 = yb(mulp[2 * i + 1] + j + 1);
32
- const t3 = xb(i + j + 1);
44
+ const j1 = j + 1;
45
+ for (let i = 0; i < m_sub_1; i++) {
46
+ const i2 = i * 2;
47
+ const t1 = bitAt(root2Bits, mulp[i2] + j1), t2 = bitAt(root2Bits, mulp[i2 + 1] + j1), t3 = bitAt(xBits, i + j1);
33
48
  bit ^= (t1 ^ t2) & t3;
34
49
  }
35
- bit ^= yb(mulp[2 * m - 2] + j + 1) & xb(m - 1 + j + 1);
36
- if (bit)
37
- r.setn(j, 1);
50
+ bit ^= bitAt(root2Bits, mulp[2 * m - 2] + j1) & bitAt(xBits, m_sub_1 + j1);
51
+ out[j] = bit;
38
52
  }
39
- return r;
53
+ return bitsToBN(out);
40
54
  };
41
55
  const reverseBits = (x) => {
42
- const r = new BN(0);
56
+ const xBits = bnToBits(x, m);
57
+ const out = new Uint8Array(m);
43
58
  for (let i = 0; i < m; i++)
44
- if (x.testn(i))
45
- r.setn(m - 1 - i, 1);
46
- return r;
59
+ out[m_sub_1 - i] = xBits[i];
60
+ return bitsToBN(out);
47
61
  };
48
62
  const toPb = new Array(m);
49
63
  toPb[0] = root1.clone();
@@ -60,7 +74,7 @@ export const init_onb_parameters = (parameters) => {
60
74
  const r = new BN(0);
61
75
  for (let p = 0; p < m; p++)
62
76
  if (x.testn(p))
63
- r.ixor(toPb[m - 1 - p]);
77
+ r.ixor(toPb[m_sub_1 - p]);
64
78
  return r;
65
79
  },
66
80
  pbToOnb: (x) => {
@@ -0,0 +1,10 @@
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ import BN from "bn.js";
3
+ export declare const SQR_PRECOMP: Uint16Array<ArrayBuffer>;
4
+ export declare const WORD_BITS = 32;
5
+ export declare const bn2LE: (x: BN, numWords?: number) => TRet<Uint32Array>;
6
+ export declare const le2BN: (arr: TArg<Uint32Array>) => BN;
7
+ export declare const mulWords: (a: TArg<Uint32Array>, b: TArg<Uint32Array>) => TRet<Uint32Array>;
8
+ export declare const sqrWords: (xw: TArg<Uint32Array>) => TRet<Uint32Array>;
9
+ export declare const modWords: (a: TArg<Uint32Array>, poly: TArg<Int32Array>) => TRet<Uint32Array>;
10
+ export declare const invWords: (a: TArg<Uint32Array>, poly: TArg<Uint32Array>) => TRet<Uint32Array>;