@tideorg/js 0.0.1

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 (234) hide show
  1. package/LICENSE +333 -0
  2. package/README.md +12 -0
  3. package/dist/Clients/ClientBase.d.ts +24 -0
  4. package/dist/Clients/ClientBase.d.ts.map +1 -0
  5. package/dist/Clients/ClientBase.js +160 -0
  6. package/dist/Clients/NetworkClient.d.ts +11 -0
  7. package/dist/Clients/NetworkClient.d.ts.map +1 -0
  8. package/dist/Clients/NetworkClient.js +68 -0
  9. package/dist/Clients/NodeClient.d.ts +34 -0
  10. package/dist/Clients/NodeClient.d.ts.map +1 -0
  11. package/dist/Clients/NodeClient.js +191 -0
  12. package/dist/Clients/VoucherClient.d.ts +8 -0
  13. package/dist/Clients/VoucherClient.d.ts.map +1 -0
  14. package/dist/Clients/VoucherClient.js +36 -0
  15. package/dist/Clients/index.d.ts +5 -0
  16. package/dist/Clients/index.d.ts.map +1 -0
  17. package/dist/Clients/index.js +20 -0
  18. package/dist/Contracts/BaseContract.d.ts +47 -0
  19. package/dist/Contracts/BaseContract.d.ts.map +1 -0
  20. package/dist/Contracts/BaseContract.js +153 -0
  21. package/dist/Contracts/GenericRealmAccessThresholdRoleContract.d.ts +8 -0
  22. package/dist/Contracts/GenericRealmAccessThresholdRoleContract.d.ts.map +1 -0
  23. package/dist/Contracts/GenericRealmAccessThresholdRoleContract.js +21 -0
  24. package/dist/Contracts/GenericResourceAccessThresholdRoleContract.d.ts +8 -0
  25. package/dist/Contracts/GenericResourceAccessThresholdRoleContract.d.ts.map +1 -0
  26. package/dist/Contracts/GenericResourceAccessThresholdRoleContract.js +21 -0
  27. package/dist/Contracts/index.d.ts +4 -0
  28. package/dist/Contracts/index.d.ts.map +1 -0
  29. package/dist/Contracts/index.js +19 -0
  30. package/dist/Cryptide/Components/BaseComponent.d.ts +56 -0
  31. package/dist/Cryptide/Components/BaseComponent.d.ts.map +1 -0
  32. package/dist/Cryptide/Components/BaseComponent.js +176 -0
  33. package/dist/Cryptide/Components/ComponentRegistry.d.ts +18 -0
  34. package/dist/Cryptide/Components/ComponentRegistry.d.ts.map +1 -0
  35. package/dist/Cryptide/Components/ComponentRegistry.js +39 -0
  36. package/dist/Cryptide/Components/Schemes/AES/AESScheme.d.ts +5 -0
  37. package/dist/Cryptide/Components/Schemes/AES/AESScheme.d.ts.map +1 -0
  38. package/dist/Cryptide/Components/Schemes/AES/AESScheme.js +20 -0
  39. package/dist/Cryptide/Components/Schemes/AES/index.d.ts +2 -0
  40. package/dist/Cryptide/Components/Schemes/AES/index.d.ts.map +1 -0
  41. package/dist/Cryptide/Components/Schemes/AES/index.js +17 -0
  42. package/dist/Cryptide/Components/Schemes/BaseScheme.d.ts +7 -0
  43. package/dist/Cryptide/Components/Schemes/BaseScheme.d.ts.map +1 -0
  44. package/dist/Cryptide/Components/Schemes/BaseScheme.js +23 -0
  45. package/dist/Cryptide/Components/Schemes/Ed25519/Ed25519Components.d.ts +52 -0
  46. package/dist/Cryptide/Components/Schemes/Ed25519/Ed25519Components.d.ts.map +1 -0
  47. package/dist/Cryptide/Components/Schemes/Ed25519/Ed25519Components.js +171 -0
  48. package/dist/Cryptide/Components/Schemes/Ed25519/Ed25519Scheme.d.ts +13 -0
  49. package/dist/Cryptide/Components/Schemes/Ed25519/Ed25519Scheme.d.ts.map +1 -0
  50. package/dist/Cryptide/Components/Schemes/Ed25519/Ed25519Scheme.js +71 -0
  51. package/dist/Cryptide/Components/Schemes/Ed25519/index.d.ts +3 -0
  52. package/dist/Cryptide/Components/Schemes/Ed25519/index.d.ts.map +1 -0
  53. package/dist/Cryptide/Components/Schemes/Ed25519/index.js +18 -0
  54. package/dist/Cryptide/Components/Schemes/SchemeRegistry.d.ts +3 -0
  55. package/dist/Cryptide/Components/Schemes/SchemeRegistry.d.ts.map +1 -0
  56. package/dist/Cryptide/Components/Schemes/SchemeRegistry.js +22 -0
  57. package/dist/Cryptide/Components/Schemes/index.d.ts +5 -0
  58. package/dist/Cryptide/Components/Schemes/index.d.ts.map +1 -0
  59. package/dist/Cryptide/Components/Schemes/index.js +20 -0
  60. package/dist/Cryptide/Components/index.d.ts +4 -0
  61. package/dist/Cryptide/Components/index.d.ts.map +1 -0
  62. package/dist/Cryptide/Components/index.js +19 -0
  63. package/dist/Cryptide/Ed25519.d.ts +106 -0
  64. package/dist/Cryptide/Ed25519.d.ts.map +1 -0
  65. package/dist/Cryptide/Ed25519.js +522 -0
  66. package/dist/Cryptide/Encryption/AES.d.ts +6 -0
  67. package/dist/Cryptide/Encryption/AES.d.ts.map +1 -0
  68. package/dist/Cryptide/Encryption/AES.js +76 -0
  69. package/dist/Cryptide/Encryption/DH.d.ts +4 -0
  70. package/dist/Cryptide/Encryption/DH.d.ts.map +1 -0
  71. package/dist/Cryptide/Encryption/DH.js +38 -0
  72. package/dist/Cryptide/Encryption/ElGamal.d.ts +12 -0
  73. package/dist/Cryptide/Encryption/ElGamal.d.ts.map +1 -0
  74. package/dist/Cryptide/Encryption/ElGamal.js +61 -0
  75. package/dist/Cryptide/Encryption/index.d.ts +4 -0
  76. package/dist/Cryptide/Encryption/index.d.ts.map +1 -0
  77. package/dist/Cryptide/Encryption/index.js +19 -0
  78. package/dist/Cryptide/Hashing/H2P.d.ts +6 -0
  79. package/dist/Cryptide/Hashing/H2P.d.ts.map +1 -0
  80. package/dist/Cryptide/Hashing/H2P.js +235 -0
  81. package/dist/Cryptide/Hashing/Hash.d.ts +8 -0
  82. package/dist/Cryptide/Hashing/Hash.d.ts.map +1 -0
  83. package/dist/Cryptide/Hashing/Hash.js +43 -0
  84. package/dist/Cryptide/Hashing/index.d.ts +3 -0
  85. package/dist/Cryptide/Hashing/index.d.ts.map +1 -0
  86. package/dist/Cryptide/Hashing/index.js +18 -0
  87. package/dist/Cryptide/Interpolation.d.ts +20 -0
  88. package/dist/Cryptide/Interpolation.d.ts.map +1 -0
  89. package/dist/Cryptide/Interpolation.js +68 -0
  90. package/dist/Cryptide/Math.d.ts +11 -0
  91. package/dist/Cryptide/Math.d.ts.map +1 -0
  92. package/dist/Cryptide/Math.js +81 -0
  93. package/dist/Cryptide/Serialization.d.ts +66 -0
  94. package/dist/Cryptide/Serialization.d.ts.map +1 -0
  95. package/dist/Cryptide/Serialization.js +517 -0
  96. package/dist/Cryptide/Signing/BlindSig.d.ts +10 -0
  97. package/dist/Cryptide/Signing/BlindSig.d.ts.map +1 -0
  98. package/dist/Cryptide/Signing/BlindSig.js +41 -0
  99. package/dist/Cryptide/Signing/EdDSA.d.ts +14 -0
  100. package/dist/Cryptide/Signing/EdDSA.d.ts.map +1 -0
  101. package/dist/Cryptide/Signing/EdDSA.js +67 -0
  102. package/dist/Cryptide/Signing/TideSignature.d.ts +59 -0
  103. package/dist/Cryptide/Signing/TideSignature.d.ts.map +1 -0
  104. package/dist/Cryptide/Signing/TideSignature.js +118 -0
  105. package/dist/Cryptide/Signing/index.d.ts +4 -0
  106. package/dist/Cryptide/Signing/index.d.ts.map +1 -0
  107. package/dist/Cryptide/Signing/index.js +19 -0
  108. package/dist/Cryptide/TideKey.d.ts +17 -0
  109. package/dist/Cryptide/TideKey.d.ts.map +1 -0
  110. package/dist/Cryptide/TideKey.js +84 -0
  111. package/dist/Cryptide/TideMemoryObjects.d.ts +4 -0
  112. package/dist/Cryptide/TideMemoryObjects.d.ts.map +1 -0
  113. package/dist/Cryptide/TideMemoryObjects.js +32 -0
  114. package/dist/Cryptide/index.d.ts +11 -0
  115. package/dist/Cryptide/index.d.ts.map +1 -0
  116. package/dist/Cryptide/index.js +28 -0
  117. package/dist/Flow/DecryptionFlows/dVVKDecryptionFlow.d.ts +16 -0
  118. package/dist/Flow/DecryptionFlows/dVVKDecryptionFlow.d.ts.map +1 -0
  119. package/dist/Flow/DecryptionFlows/dVVKDecryptionFlow.js +47 -0
  120. package/dist/Flow/DecryptionFlows/index.d.ts +2 -0
  121. package/dist/Flow/DecryptionFlows/index.d.ts.map +1 -0
  122. package/dist/Flow/DecryptionFlows/index.js +17 -0
  123. package/dist/Flow/EncryptionFlows/AuthorizedEncryptionFlow.d.ts +13 -0
  124. package/dist/Flow/EncryptionFlows/AuthorizedEncryptionFlow.d.ts.map +1 -0
  125. package/dist/Flow/EncryptionFlows/AuthorizedEncryptionFlow.js +183 -0
  126. package/dist/Flow/EncryptionFlows/PolicyAuthorizedEncryptionFlow.d.ts +57 -0
  127. package/dist/Flow/EncryptionFlows/PolicyAuthorizedEncryptionFlow.d.ts.map +1 -0
  128. package/dist/Flow/EncryptionFlows/PolicyAuthorizedEncryptionFlow.js +220 -0
  129. package/dist/Flow/EncryptionFlows/index.d.ts +3 -0
  130. package/dist/Flow/EncryptionFlows/index.d.ts.map +1 -0
  131. package/dist/Flow/EncryptionFlows/index.js +18 -0
  132. package/dist/Flow/SigningFlows/AuthorizedSigningFlow.d.ts +12 -0
  133. package/dist/Flow/SigningFlows/AuthorizedSigningFlow.d.ts.map +1 -0
  134. package/dist/Flow/SigningFlows/AuthorizedSigningFlow.js +50 -0
  135. package/dist/Flow/SigningFlows/dTestVVkSigningFlow.d.ts +15 -0
  136. package/dist/Flow/SigningFlows/dTestVVkSigningFlow.d.ts.map +1 -0
  137. package/dist/Flow/SigningFlows/dTestVVkSigningFlow.js +67 -0
  138. package/dist/Flow/SigningFlows/dVVKSigningFlow.d.ts +17 -0
  139. package/dist/Flow/SigningFlows/dVVKSigningFlow.d.ts.map +1 -0
  140. package/dist/Flow/SigningFlows/dVVKSigningFlow.js +61 -0
  141. package/dist/Flow/SigningFlows/dVVKSigningFlow2Step.d.ts +28 -0
  142. package/dist/Flow/SigningFlows/dVVKSigningFlow2Step.d.ts.map +1 -0
  143. package/dist/Flow/SigningFlows/dVVKSigningFlow2Step.js +118 -0
  144. package/dist/Flow/SigningFlows/index.d.ts +5 -0
  145. package/dist/Flow/SigningFlows/index.d.ts.map +1 -0
  146. package/dist/Flow/SigningFlows/index.js +20 -0
  147. package/dist/Flow/VoucherFlows/VoucherFlow.d.ts +16 -0
  148. package/dist/Flow/VoucherFlows/VoucherFlow.d.ts.map +1 -0
  149. package/dist/Flow/VoucherFlows/VoucherFlow.js +34 -0
  150. package/dist/Flow/VoucherFlows/index.d.ts +2 -0
  151. package/dist/Flow/VoucherFlows/index.d.ts.map +1 -0
  152. package/dist/Flow/VoucherFlows/index.js +17 -0
  153. package/dist/Flow/index.d.ts +5 -0
  154. package/dist/Flow/index.d.ts.map +1 -0
  155. package/dist/Flow/index.js +20 -0
  156. package/dist/Math/KeyDecryption.d.ts +3 -0
  157. package/dist/Math/KeyDecryption.d.ts.map +1 -0
  158. package/dist/Math/KeyDecryption.js +24 -0
  159. package/dist/Math/KeySigning.d.ts +4 -0
  160. package/dist/Math/KeySigning.d.ts.map +1 -0
  161. package/dist/Math/KeySigning.js +28 -0
  162. package/dist/Math/index.d.ts +3 -0
  163. package/dist/Math/index.d.ts.map +1 -0
  164. package/dist/Math/index.js +18 -0
  165. package/dist/Models/AuthRequest.d.ts +14 -0
  166. package/dist/Models/AuthRequest.d.ts.map +1 -0
  167. package/dist/Models/AuthRequest.js +48 -0
  168. package/dist/Models/BaseTideRequest.d.ts +55 -0
  169. package/dist/Models/BaseTideRequest.d.ts.map +1 -0
  170. package/dist/Models/BaseTideRequest.js +299 -0
  171. package/dist/Models/CustomTideRequest.d.ts +19 -0
  172. package/dist/Models/CustomTideRequest.d.ts.map +1 -0
  173. package/dist/Models/CustomTideRequest.js +30 -0
  174. package/dist/Models/Datum.d.ts +11 -0
  175. package/dist/Models/Datum.d.ts.map +1 -0
  176. package/dist/Models/Datum.js +33 -0
  177. package/dist/Models/Doken.d.ts +33 -0
  178. package/dist/Models/Doken.d.ts.map +1 -0
  179. package/dist/Models/Doken.js +133 -0
  180. package/dist/Models/EnclaveEntry.d.ts +14 -0
  181. package/dist/Models/EnclaveEntry.d.ts.map +1 -0
  182. package/dist/Models/EnclaveEntry.js +49 -0
  183. package/dist/Models/Infos/KeyInfo.d.ts +24 -0
  184. package/dist/Models/Infos/KeyInfo.d.ts.map +1 -0
  185. package/dist/Models/Infos/KeyInfo.js +51 -0
  186. package/dist/Models/Infos/OrkInfo.d.ts +18 -0
  187. package/dist/Models/Infos/OrkInfo.d.ts.map +1 -0
  188. package/dist/Models/Infos/OrkInfo.js +58 -0
  189. package/dist/Models/Infos/index.d.ts +3 -0
  190. package/dist/Models/Infos/index.d.ts.map +1 -0
  191. package/dist/Models/Infos/index.js +18 -0
  192. package/dist/Models/ModelRegistry.d.ts +29 -0
  193. package/dist/Models/ModelRegistry.d.ts.map +1 -0
  194. package/dist/Models/ModelRegistry.js +321 -0
  195. package/dist/Models/Policy.d.ts +41 -0
  196. package/dist/Models/Policy.d.ts.map +1 -0
  197. package/dist/Models/Policy.js +317 -0
  198. package/dist/Models/PolicyProtectedSerializedField.d.ts +12 -0
  199. package/dist/Models/PolicyProtectedSerializedField.d.ts.map +1 -0
  200. package/dist/Models/PolicyProtectedSerializedField.js +50 -0
  201. package/dist/Models/Responses/Vendor/VoucherResponse.d.ts +12 -0
  202. package/dist/Models/Responses/Vendor/VoucherResponse.d.ts.map +1 -0
  203. package/dist/Models/Responses/Vendor/VoucherResponse.js +39 -0
  204. package/dist/Models/Responses/Vendor/index.d.ts +2 -0
  205. package/dist/Models/Responses/Vendor/index.d.ts.map +1 -0
  206. package/dist/Models/Responses/Vendor/index.js +17 -0
  207. package/dist/Models/Responses/index.d.ts +2 -0
  208. package/dist/Models/Responses/index.d.ts.map +1 -0
  209. package/dist/Models/Responses/index.js +17 -0
  210. package/dist/Models/SerializedField.d.ts +12 -0
  211. package/dist/Models/SerializedField.d.ts.map +1 -0
  212. package/dist/Models/SerializedField.js +50 -0
  213. package/dist/Models/VendorData.d.ts +12 -0
  214. package/dist/Models/VendorData.d.ts.map +1 -0
  215. package/dist/Models/VendorData.js +38 -0
  216. package/dist/Models/VendorSettings.d.ts +9 -0
  217. package/dist/Models/VendorSettings.d.ts.map +1 -0
  218. package/dist/Models/VendorSettings.js +32 -0
  219. package/dist/Models/index.d.ts +13 -0
  220. package/dist/Models/index.d.ts.map +1 -0
  221. package/dist/Models/index.js +30 -0
  222. package/dist/Tools/TideMemory.d.ts +10 -0
  223. package/dist/Tools/TideMemory.d.ts.map +1 -0
  224. package/dist/Tools/TideMemory.js +105 -0
  225. package/dist/Tools/Utils.d.ts +13 -0
  226. package/dist/Tools/Utils.d.ts.map +1 -0
  227. package/dist/Tools/Utils.js +244 -0
  228. package/dist/Tools/index.d.ts +3 -0
  229. package/dist/Tools/index.d.ts.map +1 -0
  230. package/dist/Tools/index.js +18 -0
  231. package/dist/index.d.ts +10 -0
  232. package/dist/index.d.ts.map +1 -0
  233. package/dist/index.js +26 -0
  234. package/package.json +27 -0
@@ -0,0 +1,183 @@
1
+ //
2
+ // Tide Protocol - Infrastructure for a TRUE Zero-Trust paradigm
3
+ // Copyright (C) 2022 Tide Foundation Ltd
4
+ //
5
+ // This program is free software and is subject to the terms of
6
+ // the Tide Community Open Code License as published by the
7
+ // Tide Foundation Limited. You may modify it and redistribute
8
+ // it in accordance with and subject to the terms of that License.
9
+ // This program is distributed WITHOUT WARRANTY of any kind,
10
+ // including without any implied warranty of MERCHANTABILITY or
11
+ // FITNESS FOR A PARTICULAR PURPOSE.
12
+ // See the Tide Community Open Code License for more details.
13
+ // You should have received a copy of the Tide Community Open
14
+ // Code License along with this program.
15
+ // If not, see https://tide.org/licenses_tcoc2-0-0-en
16
+ //
17
+ import { Encryption, Serialization } from "../../Cryptide/index";
18
+ import { decryptDataRawOutput, encryptDataRawOutput } from "../../Cryptide/Encryption/AES";
19
+ import { numberToUint8Array, StringToUint8Array } from "../../Cryptide/Serialization";
20
+ import { CurrentTime } from "../../Tools/Utils";
21
+ import BaseTideRequest from "../../Models/BaseTideRequest";
22
+ import dVVKSigningFlow from "../SigningFlows/dVVKSigningFlow";
23
+ import SerializedField from "../../Models/SerializedField";
24
+ import dVVKDecryptionFlow from "../DecryptionFlows/dVVKDecryptionFlow";
25
+ /**
26
+ *
27
+ * @param {{
28
+ * vendorId: string,
29
+ * token: Doken,
30
+ * sessionKey: TideKey
31
+ * voucherURL: string,
32
+ * homeOrkUrl: string | null
33
+ * keyInfo: KeyInfo
34
+ * }} config
35
+ */
36
+ export function AuthorizedEncryptionFlow(config) {
37
+ if (!(this instanceof AuthorizedEncryptionFlow)) {
38
+ throw new Error("The 'AuthorizedEncryptionFlow' constructor must be invoked with 'new'.");
39
+ }
40
+ var encryptionFlow = this;
41
+ if (!config.token.payload.sessionKey.Equals(config.sessionKey.get_public_component())) {
42
+ throw Error("Mismatch between session key private and Doken session key public");
43
+ }
44
+ encryptionFlow.vvkId = config.vendorId;
45
+ encryptionFlow.token = config.token;
46
+ encryptionFlow.sessKey = config.sessionKey;
47
+ encryptionFlow.voucherURL = config.voucherURL;
48
+ encryptionFlow.vvkInfo = config.keyInfo;
49
+ /**
50
+ *
51
+ * @param {[
52
+ * {
53
+ * data: Uint8Array,
54
+ * tags: string[]
55
+ * }
56
+ * ]} datasToEncrypt
57
+ * @returns
58
+ */
59
+ encryptionFlow.encrypt = async function (datasToEncrypt) {
60
+ const encReqs = await Promise.all(datasToEncrypt.map(async (d) => {
61
+ const d_b = d.data;
62
+ if (d_b.length < 32) {
63
+ // if data is less than 32B
64
+ // Gr. EncryptedData
65
+ const encryptedData = await Encryption.ElGamal.encryptDataRaw(d_b, encryptionFlow.vvkInfo.UserPublic);
66
+ const tags_b = d.tags.map(t => StringToUint8Array(t));
67
+ return {
68
+ encryptionToSign: encryptedData,
69
+ encryptedData: encryptedData,
70
+ tags: tags_b,
71
+ sizeLessThan32: true
72
+ };
73
+ }
74
+ else {
75
+ // if data is more than 32B
76
+ const largeDataKey = window.crypto.getRandomValues(new Uint8Array(32));
77
+ const encryptedData = await encryptDataRawOutput(d_b, largeDataKey);
78
+ const encryptedKey = await Encryption.ElGamal.encryptDataRaw(largeDataKey, encryptionFlow.vvkInfo.UserPublic);
79
+ const tags_b = d.tags.map(t => StringToUint8Array(t));
80
+ return {
81
+ encryptionToSign: encryptedKey,
82
+ encryptedData: encryptedData,
83
+ tags: tags_b,
84
+ sizeLessThan32: false
85
+ };
86
+ }
87
+ }));
88
+ // Start signing flow to authorize this encryption
89
+ const timestamp = CurrentTime();
90
+ const timestamp_b = numberToUint8Array(timestamp, 8);
91
+ const size = encReqs.reduce((sum, next) => {
92
+ // init 4 + as we'll be creating tide memory within tide memory
93
+ // + 4 again since its another index
94
+ const nsize = 4 + 4 + (4 + next.encryptionToSign.length + next.tags.reduce((sum, next) => sum + 4 + next.length, 0));
95
+ return sum + nsize;
96
+ }, 0) + 4 + timestamp_b.length;
97
+ const draft = Serialization.CreateTideMemory(timestamp_b, size);
98
+ encReqs.forEach((enc, i) => {
99
+ const entry = Serialization.CreateTideMemory(enc.encryptionToSign, 4 + enc.encryptionToSign.length + enc.tags.reduce((sum, next) => sum + 4 + next.length, 0));
100
+ enc.tags.forEach((tag, j) => {
101
+ Serialization.WriteValue(entry, j + 1, tag);
102
+ });
103
+ Serialization.WriteValue(draft, i + 1, entry);
104
+ });
105
+ const encryptionRequest = new BaseTideRequest("TideSelfEncryption", "1", "Doken:1", draft);
106
+ // Deserialize token to retrieve vuid - if it exists
107
+ const vuid = this.token.payload.vuid;
108
+ if (vuid)
109
+ encryptionRequest.setNewDynamicData(StringToUint8Array(vuid));
110
+ // Initiate signing flow
111
+ const encryptingSigningFlow = new dVVKSigningFlow(this.vvkId, encryptionFlow.vvkInfo.UserPublic, encryptionFlow.vvkInfo.OrkInfo, encryptionFlow.sessKey, encryptionFlow.token, this.voucherURL);
112
+ const signatures = await encryptingSigningFlow.start(encryptionRequest);
113
+ // Construct final serialized payloads for client to store
114
+ return signatures.map((sig, i) => SerializedField.create(encReqs[i].encryptedData, timestamp, encReqs[i].sizeLessThan32 ? null : encReqs[i].encryptionToSign, sig));
115
+ };
116
+ /**
117
+ *
118
+ * @param {[
119
+ * {
120
+ * encrypted: Uint8Array,
121
+ * tags: string[]
122
+ * }
123
+ * ]} datasToDecrypt
124
+ */
125
+ encryptionFlow.decrypt = async function (datasToDecrypt) {
126
+ // Deserialize all datasToDecrypt + include tags in object
127
+ const deserializedDatas = datasToDecrypt.map(d => {
128
+ const b = SerializedField.deserialize(d.encrypted);
129
+ if (b.signature == null)
130
+ throw Error("Signature must be provided in Tide Serialized Data to an Authorized Decryption");
131
+ const tags_b = d.tags.map(t => StringToUint8Array(t));
132
+ return {
133
+ ...b,
134
+ tags: tags_b
135
+ };
136
+ });
137
+ // Get orks to apply vvk
138
+ const entries = deserializedDatas.map((data, i) => {
139
+ if (data.encKey) {
140
+ // We must decrypt the encrypted key, not the data itself
141
+ const entry = Serialization.CreateTideMemory(data.encKey, 4 + data.encKey.length + 4 + data.signature.length + 4 + data.timestamp.length + data.tags.reduce((sum, next) => sum + 4 + next.length, 0));
142
+ Serialization.WriteValue(entry, 1, data.signature); // won't be null
143
+ Serialization.WriteValue(entry, 2, data.timestamp);
144
+ data.tags.forEach((tag, j) => {
145
+ Serialization.WriteValue(entry, j + 3, tag); // + 3 as we start at index 3
146
+ });
147
+ return entry;
148
+ }
149
+ else {
150
+ // decrypt data directly
151
+ const entry = Serialization.CreateTideMemory(data.encFieldChk, 4 + data.encFieldChk.length + 4 + data.signature.length + 4 + data.timestamp.length + data.tags.reduce((sum, next) => sum + 4 + next.length, 0));
152
+ Serialization.WriteValue(entry, 1, data.signature); // won't be null
153
+ Serialization.WriteValue(entry, 2, data.timestamp);
154
+ data.tags.forEach((tag, j) => {
155
+ Serialization.WriteValue(entry, j + 3, tag); // + 3 as we start at index 3
156
+ });
157
+ return entry;
158
+ }
159
+ });
160
+ const draft = Serialization.CreateTideMemory(entries[0], entries.reduce((sum, next) => sum + 4 + next.length, 0));
161
+ for (let i = 1; i < entries.length; i++) {
162
+ Serialization.WriteValue(draft, i, entries[i]);
163
+ }
164
+ const decryptionRequest = new BaseTideRequest("SelfDecrypt", "1", "Doken:1", draft);
165
+ const flow = new dVVKDecryptionFlow(this.vvkId, this.vvkInfo.UserPublic, this.vvkInfo.OrkInfo, this.sessKey, this.token, this.voucherURL);
166
+ const dataKeys = await flow.start(decryptionRequest);
167
+ // Decrypt all datas
168
+ const decryptedDatas = await Promise.all(deserializedDatas.map(async (data, i) => {
169
+ // if encKey exists - decrypt with elgamal that
170
+ // then decrypt encField with key
171
+ if (data.encKey) {
172
+ const key = await decryptDataRawOutput(data.encKey.slice(32), dataKeys[i]);
173
+ return await decryptDataRawOutput(data.encFieldChk, key);
174
+ }
175
+ else {
176
+ // else - decrypt encField with elgamal
177
+ return await decryptDataRawOutput(data.encFieldChk.slice(32), dataKeys[i]);
178
+ }
179
+ }));
180
+ // Return as bytes
181
+ return decryptedDatas;
182
+ };
183
+ }
@@ -0,0 +1,57 @@
1
+ import BaseTideRequest from "../../Models/BaseTideRequest";
2
+ import TideKey from "../../Cryptide/TideKey";
3
+ import KeyInfo from "../../Models/Infos/KeyInfo";
4
+ import { Tools } from "../..";
5
+ import { Doken } from "../../Models/Doken";
6
+ interface EncryptionFlowConfig {
7
+ vendorId: string;
8
+ token: Doken;
9
+ sessionKey: TideKey;
10
+ voucherURL: string;
11
+ homeOrkUrl: string | null;
12
+ keyInfo: KeyInfo;
13
+ }
14
+ export interface DataToEncrypt {
15
+ data: Uint8Array;
16
+ tags: string[];
17
+ }
18
+ export interface DataToDecrypt {
19
+ encrypted: Uint8Array;
20
+ tags: string[];
21
+ }
22
+ export declare class PolicyAuthorizedEncryptionFlow {
23
+ vvkId: string;
24
+ token: Doken;
25
+ sessKey: TideKey;
26
+ voucherURL: string;
27
+ policy: Uint8Array;
28
+ vvkInfo: KeyInfo;
29
+ constructor(config: EncryptionFlowConfig);
30
+ createEncryptionRequest(datasToEncrypt: DataToEncrypt[], addHeavyDataToReq?: boolean): Promise<{
31
+ request: BaseTideRequest;
32
+ encReqs: {
33
+ encryptionToSign: Uint8Array<ArrayBuffer>;
34
+ encryptionAuthData: any;
35
+ encryptedData: Uint8Array<ArrayBuffer>;
36
+ tags: Uint8Array<ArrayBuffer>[];
37
+ sizeLessThan32: boolean;
38
+ }[];
39
+ timestamp: number;
40
+ }>;
41
+ encrypt(datasToEncrypt: DataToEncrypt[], policy: Uint8Array): Promise<Uint8Array[]>;
42
+ commitEncrypt(request: Uint8Array, policy: Uint8Array): Promise<Tools.TideMemory[]>;
43
+ createDecryptionRequest(datasToDecrypt: DataToDecrypt[], addHeavyDataToReq?: boolean): {
44
+ request: BaseTideRequest;
45
+ deserializedDatas: {
46
+ tags: Uint8Array<ArrayBuffer>[];
47
+ encFieldChk: Uint8Array<ArrayBufferLike>;
48
+ timestamp: Uint8Array<ArrayBufferLike>;
49
+ encKey: Uint8Array<ArrayBufferLike>;
50
+ signature: Uint8Array<ArrayBufferLike>;
51
+ }[];
52
+ };
53
+ decrypt(datasToDecrypt: DataToDecrypt[], policy: Uint8Array): Promise<Uint8Array[]>;
54
+ commitDecrypt(request: Uint8Array, policy: Uint8Array): Promise<any[]>;
55
+ }
56
+ export {};
57
+ //# sourceMappingURL=PolicyAuthorizedEncryptionFlow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PolicyAuthorizedEncryptionFlow.d.ts","sourceRoot":"","sources":["../../../Flow/EncryptionFlows/PolicyAuthorizedEncryptionFlow.ts"],"names":[],"mappings":"AAqBA,OAAO,eAAe,MAAM,8BAA8B,CAAC;AAI3D,OAAO,OAAO,MAAM,wBAAwB,CAAC;AAC7C,OAAO,OAAO,MAAM,4BAA4B,CAAC;AAEjD,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,UAAU,oBAAoB;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,KAAK,CAAC;IACb,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC1B,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,MAAM,WAAW,aAAa;IAC1B,SAAS,EAAE,UAAU,CAAC;IACtB,IAAI,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,qBAAa,8BAA8B;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,UAAU,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;gBAEL,MAAM,EAAE,oBAAoB;IAYlC,uBAAuB,CAAC,cAAc,EAAE,aAAa,EAAE,EAAE,iBAAiB,UAAQ;;;;;;;;;;;IAsElF,OAAO,CAAC,cAAc,EAAE,aAAa,EAAE,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAkBnF,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU;IAoC3D,uBAAuB,CAAC,cAAc,EAAE,aAAa,EAAE,EAAE,iBAAiB,UAAM;;;;;;;;;;IAgD1E,OAAO,CAAC,cAAc,EAAE,aAAa,EAAE,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAwBnF,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU;CA8B9D"}
@@ -0,0 +1,220 @@
1
+ //
2
+ // Tide Protocol - Infrastructure for a TRUE Zero-Trust paradigm
3
+ // Copyright (C) 2022 Tide Foundation Ltd
4
+ //
5
+ // This program is free software and is subject to the terms of
6
+ // the Tide Community Open Code License as published by the
7
+ // Tide Foundation Limited. You may modify it and redistribute
8
+ // it in accordance with and subject to the terms of that License.
9
+ // This program is distributed WITHOUT WARRANTY of any kind,
10
+ // including without any implied warranty of MERCHANTABILITY or
11
+ // FITNESS FOR A PARTICULAR PURPOSE.
12
+ // See the Tide Community Open Code License for more details.
13
+ // You should have received a copy of the Tide Community Open
14
+ // Code License along with this program.
15
+ // If not, see https://tide.org/licenses_tcoc2-0-0-en
16
+ //
17
+ import { Encryption, Serialization } from "../../Cryptide/index";
18
+ import { decryptDataRawOutput, encryptDataRawOutput } from "../../Cryptide/Encryption/AES";
19
+ import { CreateTideMemoryFromArray, GetValue, numberToUint8Array, StringToUint8Array, TryGetValue } from "../../Cryptide/Serialization";
20
+ import { CurrentTime } from "../../Tools/Utils";
21
+ import BaseTideRequest from "../../Models/BaseTideRequest";
22
+ import dVVKSigningFlow from "../SigningFlows/dVVKSigningFlow";
23
+ import dVVKDecryptionFlow from "../DecryptionFlows/dVVKDecryptionFlow";
24
+ import PolicyProtectedSerializedField from "../../Models/PolicyProtectedSerializedField";
25
+ import { Tools } from "../..";
26
+ import { TideMemory } from "../../Tools";
27
+ export class PolicyAuthorizedEncryptionFlow {
28
+ constructor(config) {
29
+ if (!config.token.payload.sessionKey.Equals(config.sessionKey.get_public_component())) {
30
+ throw Error("Mismatch between session key private and Doken session key public");
31
+ }
32
+ this.vvkId = config.vendorId;
33
+ this.token = config.token;
34
+ this.sessKey = config.sessionKey;
35
+ this.voucherURL = config.voucherURL;
36
+ this.vvkInfo = config.keyInfo;
37
+ }
38
+ async createEncryptionRequest(datasToEncrypt, addHeavyDataToReq = false) {
39
+ const encReqs = await Promise.all(datasToEncrypt.map(async (d) => {
40
+ const d_b = d.data;
41
+ if (d_b.length < 32) {
42
+ const tags_b = d.tags.map(t => StringToUint8Array(t));
43
+ // if data is less than 32B
44
+ // Gr. EncryptedData
45
+ const encryptedData = await Encryption.ElGamal.encryptDataRaw_withAuthentication(d_b, this.vvkInfo.UserPublic, Serialization.ConcatUint8Arrays(tags_b));
46
+ return {
47
+ encryptionToSign: encryptedData.cipher,
48
+ encryptionAuthData: encryptedData.auth,
49
+ encryptedData: encryptedData.cipher,
50
+ tags: tags_b,
51
+ sizeLessThan32: true
52
+ };
53
+ }
54
+ else {
55
+ const tags_b = d.tags.map(t => StringToUint8Array(t));
56
+ // if data is more than 32B
57
+ const largeDataKey = window.crypto.getRandomValues(new Uint8Array(32));
58
+ const encryptedData = await encryptDataRawOutput(d_b, largeDataKey);
59
+ const encryptedKey = await Encryption.ElGamal.encryptDataRaw_withAuthentication(largeDataKey, this.vvkInfo.UserPublic, Serialization.ConcatUint8Arrays(tags_b));
60
+ return {
61
+ encryptionToSign: encryptedKey.cipher,
62
+ encryptionAuthData: encryptedKey.auth,
63
+ encryptedData: encryptedData,
64
+ tags: tags_b,
65
+ sizeLessThan32: false
66
+ };
67
+ }
68
+ }));
69
+ // Start signing flow to authorize this encryption
70
+ const timestamp = CurrentTime();
71
+ const timestamp_b = numberToUint8Array(timestamp, 8);
72
+ let arr = [timestamp_b];
73
+ encReqs.forEach((enc) => {
74
+ const entry = CreateTideMemoryFromArray([
75
+ enc.encryptionToSign.slice(0, 32), // only get C1 point for draft
76
+ enc.encryptionAuthData,
77
+ ...enc.tags
78
+ ]);
79
+ arr.push(entry);
80
+ });
81
+ const draft = CreateTideMemoryFromArray(arr);
82
+ const request = new BaseTideRequest("PolicyEnabledEncryption", "1", "Policy:1", draft);
83
+ if (addHeavyDataToReq) {
84
+ request.setCustomExpiry(604800); // default one week - assuming this req is drafted
85
+ // we need to store the actual encrypted data (if size larger than 32) in request as well
86
+ // this is for when we create the PolicyProtectedSerializedField object after committion
87
+ const dataToStoreLater = Serialization.CreateTideMemoryFromArray(encReqs.map((e) => PolicyProtectedSerializedField.create(e.encryptedData, timestamp, e.sizeLessThan32 ? null : e.encryptionToSign, null))); // no signature for now
88
+ request.addAuthorizerCertificate(dataToStoreLater); // authorizer cert not used by the tide network in this flow but useful for us to serialize the data for local storage
89
+ }
90
+ return { request, encReqs, timestamp };
91
+ }
92
+ async encrypt(datasToEncrypt, policy) {
93
+ const { request: encryptionRequest, encReqs, timestamp } = await this.createEncryptionRequest(datasToEncrypt);
94
+ encryptionRequest.addPolicy(policy);
95
+ // Initiate signing flow
96
+ const encryptingSigningFlow = new dVVKSigningFlow(this.vvkId, this.vvkInfo.UserPublic, this.vvkInfo.OrkInfo, this.sessKey, this.token, this.voucherURL);
97
+ const signatures = await encryptingSigningFlow.start(encryptionRequest);
98
+ // Construct final serialized payloads for client to store
99
+ return signatures.map((sig, i) => PolicyProtectedSerializedField.create(encReqs[i].encryptedData, timestamp, encReqs[i].sizeLessThan32 ? null : encReqs[i].encryptionToSign, sig));
100
+ }
101
+ async commitEncrypt(request, policy) {
102
+ // Remove authorizer cert from request before sending it up to the orks
103
+ const readyEncRequest = BaseTideRequest.decode(request);
104
+ const encryptedData = readyEncRequest.authorizerCert;
105
+ readyEncRequest.authorizerCert = new Tools.TideMemory(); // clear request of the heavy data
106
+ // Deserialize data stored in the request previously
107
+ let encryptedDatas = [];
108
+ let resultObj = { result: undefined };
109
+ for (let i = 0; Serialization.TryGetValue(encryptedData, i, resultObj); i++) {
110
+ encryptedDatas.push(resultObj.result);
111
+ }
112
+ const deserializedDatas = encryptedDatas.map(e => {
113
+ const b = PolicyProtectedSerializedField.deserialize(e);
114
+ if (b.signature != null)
115
+ throw Error("There shouldn't be any signatures in this data");
116
+ return b;
117
+ });
118
+ // Add the policy to the request
119
+ readyEncRequest.addPolicy(policy);
120
+ // Initiate signing flow
121
+ const encryptingSigningFlow = new dVVKSigningFlow(this.vvkId, this.vvkInfo.UserPublic, this.vvkInfo.OrkInfo, this.sessKey, this.token, this.voucherURL);
122
+ const signatures = await encryptingSigningFlow.start(readyEncRequest);
123
+ // Construct final serialized payloads for client to store WITH SIGNATURE - that's the only reason we are doing this again
124
+ return signatures.map((sig, i) => PolicyProtectedSerializedField.create(deserializedDatas[i].encFieldChk, deserializedDatas[i].timestamp, deserializedDatas[i].encKey ? deserializedDatas[i].encKey : null, sig));
125
+ }
126
+ createDecryptionRequest(datasToDecrypt, addHeavyDataToReq = false) {
127
+ // Deserialize all datasToDecrypt + include tags in object
128
+ const deserializedDatas = datasToDecrypt.map(d => {
129
+ const b = PolicyProtectedSerializedField.deserialize(d.encrypted);
130
+ if (b.signature == null)
131
+ throw Error("Signature must be provided in Tide Serialized Data to an Authorized Decryption");
132
+ const tags_b = d.tags.map(t => StringToUint8Array(t));
133
+ return {
134
+ ...b,
135
+ tags: tags_b
136
+ };
137
+ });
138
+ // Get orks to apply vvk
139
+ const entries = deserializedDatas.map((data, i) => {
140
+ if (data.encKey) {
141
+ // We must decrypt the encrypted key, not the data itself
142
+ const entry = CreateTideMemoryFromArray([
143
+ data.encKey.slice(0, 32), // only send c1 (point)
144
+ data.signature,
145
+ data.timestamp,
146
+ ...data.tags
147
+ ]);
148
+ return entry;
149
+ }
150
+ else {
151
+ // decrypt data directly
152
+ const entry = CreateTideMemoryFromArray([
153
+ data.encFieldChk.slice(0, 32), // only send c1 (point)
154
+ data.signature,
155
+ data.timestamp,
156
+ ...data.tags
157
+ ]);
158
+ return entry;
159
+ }
160
+ });
161
+ const draft = CreateTideMemoryFromArray(entries);
162
+ const request = new BaseTideRequest("PolicyEnabledDecryption", "1", "Policy:1", draft);
163
+ if (addHeavyDataToReq) {
164
+ request.setCustomExpiry(604800); // default for now - assuming this req is drafted
165
+ const dynData = TideMemory.CreateFromArray(deserializedDatas.map(d => {
166
+ return TideMemory.CreateFromArray([d.encFieldChk, d.encKey ? d.encKey : new Uint8Array()]);
167
+ })); // efficient serialized of heavy data
168
+ request.addAuthorizerCertificate(dynData); // authorizer cert not used by the tide network in this flow but useful for us to serialize the data for local storage
169
+ }
170
+ return { request, deserializedDatas };
171
+ }
172
+ async decrypt(datasToDecrypt, policy) {
173
+ const { request: decryptionRequest, deserializedDatas } = this.createDecryptionRequest(datasToDecrypt);
174
+ decryptionRequest.addPolicy(policy);
175
+ const flow = new dVVKDecryptionFlow(this.vvkId, this.vvkInfo.UserPublic, this.vvkInfo.OrkInfo, this.sessKey, this.token, this.voucherURL);
176
+ const dataKeys = await flow.start(decryptionRequest);
177
+ // Decrypt all datas
178
+ const decryptedDatas = await Promise.all(deserializedDatas.map(async (data, i) => {
179
+ // if encKey exists - decrypt with elgamal that
180
+ // then decrypt encField with key
181
+ if (data.encKey) {
182
+ const key = await decryptDataRawOutput(data.encKey.slice(32), dataKeys[i]);
183
+ return await decryptDataRawOutput(data.encFieldChk, key);
184
+ }
185
+ else {
186
+ // else - decrypt encField with elgamal
187
+ return await decryptDataRawOutput(data.encFieldChk.slice(32), dataKeys[i]);
188
+ }
189
+ }));
190
+ // Return as bytes
191
+ return decryptedDatas;
192
+ }
193
+ async commitDecrypt(request, policy) {
194
+ const decryptionRequest = BaseTideRequest.decode(request);
195
+ decryptionRequest.addPolicy(policy);
196
+ const heavyData = decryptionRequest.authorizerCert;
197
+ decryptionRequest.authorizerCert = new TideMemory(); // clear decryption request of heavy data
198
+ const flow = new dVVKDecryptionFlow(this.vvkId, this.vvkInfo.UserPublic, this.vvkInfo.OrkInfo, this.sessKey, this.token, this.voucherURL);
199
+ const dataKeys = await flow.start(decryptionRequest);
200
+ // Decrypt all datas
201
+ let resultObj = { result: undefined };
202
+ let decryptedDatas = [];
203
+ for (let i = 0; TryGetValue(heavyData, i, resultObj); i++) {
204
+ const encFieldChk = GetValue(resultObj.result, 0);
205
+ const encKey = GetValue(resultObj.result, 1);
206
+ // if encKey exists - decrypt with elgamal that
207
+ // then decrypt encField with key
208
+ if (encKey.length > 0) {
209
+ const key = await decryptDataRawOutput(encKey.slice(32), dataKeys[i]);
210
+ decryptedDatas.push(await decryptDataRawOutput(encFieldChk, key));
211
+ }
212
+ else {
213
+ // else - decrypt encField with elgamal
214
+ decryptedDatas.push(await decryptDataRawOutput(encFieldChk.slice(32), dataKeys[i]));
215
+ }
216
+ }
217
+ // Return as bytes
218
+ return decryptedDatas;
219
+ }
220
+ }
@@ -0,0 +1,3 @@
1
+ export { AuthorizedEncryptionFlow } from './AuthorizedEncryptionFlow';
2
+ export { PolicyAuthorizedEncryptionFlow, DataToDecrypt, DataToEncrypt } from './PolicyAuthorizedEncryptionFlow';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../Flow/EncryptionFlows/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,8BAA8B,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAA"}
@@ -0,0 +1,18 @@
1
+ //
2
+ // Tide Protocol - Infrastructure for a TRUE Zero-Trust paradigm
3
+ // Copyright (C) 2022 Tide Foundation Ltd
4
+ //
5
+ // This program is free software and is subject to the terms of
6
+ // the Tide Community Open Code License as published by the
7
+ // Tide Foundation Limited. You may modify it and redistribute
8
+ // it in accordance with and subject to the terms of that License.
9
+ // This program is distributed WITHOUT WARRANTY of any kind,
10
+ // including without any implied warranty of MERCHANTABILITY or
11
+ // FITNESS FOR A PARTICULAR PURPOSE.
12
+ // See the Tide Community Open Code License for more details.
13
+ // You should have received a copy of the Tide Community Open
14
+ // Code License along with this program.
15
+ // If not, see https://tide.org/licenses_tcoc2-0-0-en
16
+ //
17
+ export { AuthorizedEncryptionFlow } from './AuthorizedEncryptionFlow';
18
+ export { PolicyAuthorizedEncryptionFlow } from './PolicyAuthorizedEncryptionFlow';
@@ -0,0 +1,12 @@
1
+ import TideKey from "../../Cryptide/TideKey";
2
+ import KeyInfo from "../../Models/Infos/KeyInfo";
3
+ import { Doken } from "../../Models/Doken";
4
+ export declare function AuthorizedSigningFlow(config: {
5
+ vendorId: string;
6
+ token: Doken;
7
+ sessionKey: TideKey;
8
+ voucherURL: string;
9
+ homeOrkUrl: string | null;
10
+ keyInfo: KeyInfo;
11
+ }): void;
12
+ //# sourceMappingURL=AuthorizedSigningFlow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthorizedSigningFlow.d.ts","sourceRoot":"","sources":["../../../Flow/SigningFlows/AuthorizedSigningFlow.ts"],"names":[],"mappings":"AAmBA,OAAO,OAAO,MAAM,wBAAwB,CAAC;AAC7C,OAAO,OAAO,MAAM,4BAA4B,CAAC;AAEjD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,UAAU,EAAE,OAAO,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,QAyCrK"}
@@ -0,0 +1,50 @@
1
+ //
2
+ // Tide Protocol - Infrastructure for a TRUE Zero-Trust paradigm
3
+ // Copyright (C) 2022 Tide Foundation Ltd
4
+ //
5
+ // This program is free software and is subject to the terms of
6
+ // the Tide Community Open Code License as published by the
7
+ // Tide Foundation Limited. You may modify it and redistribute
8
+ // it in accordance with and subject to the terms of that License.
9
+ // This program is distributed WITHOUT WARRANTY of any kind,
10
+ // including without any implied warranty of MERCHANTABILITY or
11
+ // FITNESS FOR A PARTICULAR PURPOSE.
12
+ // See the Tide Community Open Code License for more details.
13
+ // You should have received a copy of the Tide Community Open
14
+ // Code License along with this program.
15
+ // If not, see https://tide.org/licenses_tcoc2-0-0-en
16
+ //
17
+ import BaseTideRequest from "../../Models/BaseTideRequest";
18
+ import dVVKSigningFlow from "../SigningFlows/dVVKSigningFlow";
19
+ import { Tools } from "../..";
20
+ export function AuthorizedSigningFlow(config) {
21
+ if (!(this instanceof AuthorizedSigningFlow)) {
22
+ throw new Error("The 'AuthorizedSigningFlow' constructor must be invoked with 'new'.");
23
+ }
24
+ if (config.token) {
25
+ if (!config.token.payload.sessionKey.Equals(config.sessionKey.get_public_component()))
26
+ throw Error("Mismatch between session key private and Doken session key public");
27
+ }
28
+ var signingFlow = this;
29
+ signingFlow.vvkId = config.vendorId;
30
+ signingFlow.token = config.token;
31
+ signingFlow.voucherURL = config.voucherURL;
32
+ signingFlow.sessKey = config.sessionKey;
33
+ signingFlow.vvkInfo = config.keyInfo;
34
+ signingFlow.signv2 = async function (tideSerializedRequest, waitForAll) {
35
+ const flow = new dVVKSigningFlow(this.vvkId, signingFlow.vvkInfo.UserPublic, signingFlow.vvkInfo.OrkInfo, signingFlow.sessKey, signingFlow.token, this.voucherURL);
36
+ return flow.start(BaseTideRequest.decode(tideSerializedRequest), waitForAll);
37
+ };
38
+ signingFlow.initializeRequest = async function (tideReqToInitialize, waitForAll) {
39
+ const requestToInitializeDetails = await tideReqToInitialize.getRequestInitDetails();
40
+ const initRequest = new BaseTideRequest("TideRequestInitialization", "1", "Doken:1", Tools.TideMemory.CreateFromArray([
41
+ requestToInitializeDetails.creationTime,
42
+ requestToInitializeDetails.expireTime,
43
+ requestToInitializeDetails.modelId,
44
+ requestToInitializeDetails.draftHash
45
+ ]), new Tools.TideMemory());
46
+ const flow = new dVVKSigningFlow(this.vvkId, signingFlow.vvkInfo.UserPublic, signingFlow.vvkInfo.OrkInfo, signingFlow.sessKey, signingFlow.token, this.voucherURL);
47
+ const sig = (await flow.start(initRequest, waitForAll))[0];
48
+ tideReqToInitialize.addCreationSignature(requestToInitializeDetails.creationTime, sig);
49
+ };
50
+ }
@@ -0,0 +1,15 @@
1
+ import OrkInfo from "../../Models/Infos/OrkInfo";
2
+ export default class dTestVVKSigningFlow {
3
+ vvkid: string;
4
+ vvkPublic: any;
5
+ orks: OrkInfo[];
6
+ sessKey: Uint8Array;
7
+ gSessKey: any;
8
+ vrk: bigint;
9
+ authorizer: Uint8Array;
10
+ authorizerCert: Uint8Array;
11
+ voucherURL: string;
12
+ constructor(vvkid: string, vvkPublic: any, orks: OrkInfo[], sessKey: Uint8Array, gSessKey: any, vrk: bigint, authorizer: Uint8Array, authorizerCert: Uint8Array, voucherURL: string);
13
+ start(): Promise<void>;
14
+ }
15
+ //# sourceMappingURL=dTestVVkSigningFlow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dTestVVkSigningFlow.d.ts","sourceRoot":"","sources":["../../../Flow/SigningFlows/dTestVVkSigningFlow.ts"],"names":[],"mappings":"AAqBA,OAAO,OAAO,MAAM,4BAA4B,CAAC;AAMjD,MAAM,CAAC,OAAO,OAAO,mBAAmB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,GAAG,CAAC;IACf,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,GAAG,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,UAAU,CAAC;IACvB,cAAc,EAAE,UAAU,CAAC;IAC3B,UAAU,EAAE,MAAM,CAAC;gBAEP,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM;IAa7K,KAAK;CAoCd"}
@@ -0,0 +1,67 @@
1
+ //
2
+ // Tide Protocol - Infrastructure for a TRUE Zero-Trust paradigm
3
+ // Copyright (C) 2022 Tide Foundation Ltd
4
+ //
5
+ // This program is free software and is subject to the terms of
6
+ // the Tide Community Open Code License as published by the
7
+ // Tide Foundation Limited. You may modify it and redistribute
8
+ // it in accordance with and subject to the terms of that License.
9
+ // This program is distributed WITHOUT WARRANTY of any kind,
10
+ // including without any implied warranty of MERCHANTABILITY or
11
+ // FITNESS FOR A PARTICULAR PURPOSE.
12
+ // See the Tide Community Open Code License for more details.
13
+ // You should have received a copy of the Tide Community Open
14
+ // Code License along with this program.
15
+ // If not, see https://tide.org/licenses_tcoc2-0-0-en
16
+ //
17
+ import { Signing } from "../../Cryptide/index";
18
+ import BaseTideRequest from "../../Models/BaseTideRequest";
19
+ import { Threshold, WaitForNumberofORKs, sortORKs } from "../../Tools/Utils";
20
+ import NodeClient from "../../Clients/NodeClient";
21
+ import { PreSign, Sign as SumS } from "../../Math/KeySigning";
22
+ import { BigIntToByteArray, ConcatUint8Arrays, StringToUint8Array, base64ToBytes, bytesToBase64, serializeBitArray } from "../../Cryptide/Serialization";
23
+ import VoucherFlow from "../VoucherFlows/VoucherFlow";
24
+ import { TestSignatureFormat } from "../../Cryptide/Signing/TideSignature";
25
+ export default class dTestVVKSigningFlow {
26
+ constructor(vvkid, vvkPublic, orks, sessKey, gSessKey, vrk, authorizer, authorizerCert, voucherURL) {
27
+ this.vvkid = vvkid;
28
+ this.vvkPublic = vvkPublic;
29
+ this.orks = orks;
30
+ this.orks = sortORKs(this.orks); // sort for bitwise!
31
+ this.sessKey = sessKey;
32
+ this.gSessKey = gSessKey;
33
+ this.vrk = vrk;
34
+ this.authorizer = authorizer;
35
+ this.authorizerCert = authorizerCert;
36
+ this.voucherURL = voucherURL;
37
+ }
38
+ async start() {
39
+ const startTime = performance.now();
40
+ const draft = `{"SomeStaticData":"This msg was previously authorized"}`;
41
+ const dynamicData = `{"SomeDynamicData":"New log in"}`;
42
+ const request = new BaseTideRequest("TestInit", "1", "VRK:1", StringToUint8Array(draft), StringToUint8Array(dynamicData));
43
+ const proof = base64ToBytes(await Signing.EdDSA.sign(await request.dataToAuthorize(), this.vrk));
44
+ var x = await request.dataToAuthorize();
45
+ request.addAuthorization(proof);
46
+ request.addAuthorizer(this.authorizer);
47
+ request.addAuthorizerCertificate(this.authorizerCert);
48
+ const clients = await Promise.all(this.orks.map(async (info) => await new NodeClient(info.orkURL).EnableTideDH(this.gSessKey, this.sessKey, info.orkPublic)));
49
+ const voucherFlow = new VoucherFlow(this.orks.map(o => o.orkPaymentPublic), this.voucherURL, "vendorsign");
50
+ const { vouchers } = await voucherFlow.GetVouchers();
51
+ const pre_PreSignResponses = clients.map((client, i) => client.PreSign(i, this.vvkid, request, vouchers.toORK(i)));
52
+ const { fulfilledResponses, bitwise } = await WaitForNumberofORKs(this.orks, pre_PreSignResponses, "VVK", Threshold, null, clients);
53
+ const GRj = PreSign(fulfilledResponses);
54
+ const pre_SignResponses = clients.map(client => client.Sign(this.vvkid, request, GRj, serializeBitArray(bitwise)));
55
+ const SignResponses = await Promise.all(pre_SignResponses);
56
+ const Sj = SumS(SignResponses.map(s => s.Sij));
57
+ if (GRj.length != Sj.length)
58
+ throw Error("Weird amount of GRjs and Sjs");
59
+ const testSig = bytesToBase64(ConcatUint8Arrays([GRj[0].toRawBytes(), BigIntToByteArray(Sj[0])]));
60
+ const toVerify = "This msg was previously authorized <-mix-> New log in";
61
+ const valid = await Signing.EdDSA.verify(testSig, this.vvkPublic, new TestSignatureFormat(toVerify).format());
62
+ if (!valid)
63
+ throw Error("Test VVK Signing failed");
64
+ const endTime = performance.now();
65
+ console.log(`Test VVK Signing took ${endTime - startTime} milliseconds.`);
66
+ }
67
+ }