@icure/api 8.0.24 → 8.0.25

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 (67) hide show
  1. package/icc-x-api/crypto/AES.d.ts +54 -2
  2. package/icc-x-api/crypto/AES.js +5 -5
  3. package/icc-x-api/crypto/AES.js.map +1 -1
  4. package/icc-x-api/crypto/CryptoPrimitives.d.ts +21 -12
  5. package/icc-x-api/crypto/CryptoPrimitives.js +8 -19
  6. package/icc-x-api/crypto/CryptoPrimitives.js.map +1 -1
  7. package/icc-x-api/crypto/DelegationsDeAnonymization.js +1 -1
  8. package/icc-x-api/crypto/DelegationsDeAnonymization.js.map +1 -1
  9. package/icc-x-api/crypto/HMACUtils.d.ts +8 -1
  10. package/icc-x-api/crypto/HMACUtils.js +3 -3
  11. package/icc-x-api/crypto/HMACUtils.js.map +1 -1
  12. package/icc-x-api/crypto/NativeCryptoPrimitivesBridge.d.ts +113 -0
  13. package/icc-x-api/crypto/NativeCryptoPrimitivesBridge.js +477 -0
  14. package/icc-x-api/crypto/NativeCryptoPrimitivesBridge.js.map +1 -0
  15. package/icc-x-api/crypto/RSA.d.ts +107 -4
  16. package/icc-x-api/crypto/RSA.js +4 -4
  17. package/icc-x-api/crypto/RSA.js.map +1 -1
  18. package/icc-x-api/crypto/shamir.d.ts +7 -3
  19. package/icc-x-api/crypto/shamir.js +14 -5
  20. package/icc-x-api/crypto/shamir.js.map +1 -1
  21. package/icc-x-api/index.d.ts +1 -0
  22. package/icc-x-api/index.js +5 -3
  23. package/icc-x-api/index.js.map +1 -1
  24. package/package.json +1 -1
  25. package/test/icc-api/api/IccMedicalLocationApi.js +6 -6
  26. package/test/icc-api/api/IccMedicalLocationApi.js.map +1 -1
  27. package/test/icc-x-api/crud/comprehensive-crud-test.js +1 -1
  28. package/test/icc-x-api/crud/comprehensive-crud-test.js.map +1 -1
  29. package/test/icc-x-api/crud/entities-crud-test-interface.js +1 -0
  30. package/test/icc-x-api/crud/entities-crud-test-interface.js.map +1 -1
  31. package/test/icc-x-api/crypto/anonymous-delegations-test.js +1 -2
  32. package/test/icc-x-api/crypto/anonymous-delegations-test.js.map +1 -1
  33. package/test/icc-x-api/crypto/crypto-utils.js +1 -2
  34. package/test/icc-x-api/crypto/crypto-utils.js.map +1 -1
  35. package/test/icc-x-api/crypto/exchange-data-manager-test.js +1 -1
  36. package/test/icc-x-api/crypto/exchange-data-manager-test.js.map +1 -1
  37. package/test/icc-x-api/crypto/full-crypto-test.js +1 -2
  38. package/test/icc-x-api/crypto/full-crypto-test.js.map +1 -1
  39. package/test/icc-x-api/crypto/secure-delegations-manager-test.js +1 -1
  40. package/test/icc-x-api/crypto/secure-delegations-manager-test.js.map +1 -1
  41. package/test/icc-x-api/crypto/secure-delegations-metadata-decryptor-test.js +2 -2
  42. package/test/icc-x-api/crypto/secure-delegations-metadata-decryptor-test.js.map +1 -1
  43. package/test/icc-x-api/crypto/shamir.js +6 -7
  44. package/test/icc-x-api/crypto/shamir.js.map +1 -1
  45. package/test/icc-x-api/crypto/signature-keys-manager-test.js +1 -1
  46. package/test/icc-x-api/crypto/signature-keys-manager-test.js.map +1 -1
  47. package/test/icc-x-api/icc-maintenance-task-x-api-test.js +3 -2
  48. package/test/icc-x-api/icc-maintenance-task-x-api-test.js.map +1 -1
  49. package/test/icc-x-api/icc-user-x-api-test.js +1 -1
  50. package/test/icc-x-api/icc-user-x-api-test.js.map +1 -1
  51. package/test/icc-x-api/patient-user.js +1 -2
  52. package/test/icc-x-api/patient-user.js.map +1 -1
  53. package/test/icc-x-api/test-legacy-data-support.js +2 -3
  54. package/test/icc-x-api/test-legacy-data-support.js.map +1 -1
  55. package/test/utils/FakeDataOwnerApi.js +1 -1
  56. package/test/utils/FakeDataOwnerApi.js.map +1 -1
  57. package/test/utils/FakeGenericApi.js +1 -1
  58. package/test/utils/FakeGenericApi.js.map +1 -1
  59. package/test/utils/TestApi.js +1 -1
  60. package/test/utils/TestApi.js.map +1 -1
  61. package/test/utils/TestCryptoStrategies.js +1 -1
  62. package/test/utils/TestCryptoStrategies.js.map +1 -1
  63. package/test/utils/test_utils.js +6 -6
  64. package/test/utils/test_utils.js.map +1 -1
  65. package/test/icc-api/api/IccArticleApi.d.ts +0 -1
  66. package/test/icc-api/api/IccArticleApi.js +0 -64
  67. package/test/icc-api/api/IccArticleApi.js.map +0 -1
@@ -9,7 +9,110 @@ export declare enum ShaVersion {
9
9
  Sha1 = "sha-1",
10
10
  Sha256 = "sha-256"
11
11
  }
12
- export declare class RSAUtils {
12
+ export interface RSAUtils {
13
+ /**
14
+ * Generates a key pair for encryption/decryption of data.
15
+ * @param shaVersion the version of the SHA algorithm to use.
16
+ */
17
+ generateKeyPair(shaVersion: ShaVersion): Promise<KeyPair<CryptoKey>>;
18
+ /**
19
+ * Generates a key pair for signing data and signature verification.
20
+ */
21
+ generateSignatureKeyPair(): Promise<KeyPair<CryptoKey>>;
22
+ /**
23
+ *
24
+ * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)
25
+ * 'spki': for private key
26
+ * 'pkcs8': for private Key
27
+ *
28
+ * @param keyPair is {publicKey: CryptoKey, privateKey: CryptoKey}
29
+ * @param privKeyFormat will be 'pkcs8' or 'jwk'
30
+ * @param pubKeyFormat will be 'spki' or 'jwk'
31
+ * @returns {Promise} will the AES Key
32
+ */
33
+ exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'jwk', pubKeyFormat: 'jwk'): Promise<KeyPair<JsonWebKey>>;
34
+ exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'pkcs8', pubKeyFormat: 'spki'): Promise<KeyPair<ArrayBuffer>>;
35
+ exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: string, pubKeyFormat: string): Promise<KeyPair<JsonWebKey | ArrayBuffer>>;
36
+ /**
37
+ * Format:
38
+ *
39
+ * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)
40
+ * 'spki': for private key
41
+ * 'pkcs8': for private Key
42
+ *
43
+ * @param cryptoKey public or private
44
+ * @param format either 'jwk' or 'spki' or 'pkcs8'
45
+ * @returns {Promise|*} will be RSA key (public or private)
46
+ */
47
+ exportKey(cryptoKey: CryptoKey, format: 'jwk'): Promise<JsonWebKey>;
48
+ exportKey(cryptoKey: CryptoKey, format: 'spki'): Promise<ArrayBuffer>;
49
+ exportKey(cryptoKey: CryptoKey, format: 'pkcs8'): Promise<ArrayBuffer>;
50
+ exportKey(cryptoKey: CryptoKey, format: string): Promise<JsonWebKey | ArrayBuffer>;
51
+ /**
52
+ *
53
+ * @param publicKey (CryptoKey)
54
+ * @param plainData (Uint8Array)
55
+ */
56
+ encrypt(publicKey: CryptoKey, plainData: Uint8Array): Promise<ArrayBuffer>;
57
+ /**
58
+ *
59
+ * @param privateKey (CryptoKey)
60
+ * @param encryptedData (Uint8Array)
61
+ */
62
+ decrypt(privateKey: CryptoKey, encryptedData: Uint8Array): Promise<ArrayBuffer>;
63
+ /**
64
+ *
65
+ * @param format 'jwk', 'spki', or 'pkcs8'
66
+ * @param keydata should be the key data based on the format.
67
+ * @param keyUsages Array of usages. For example, ['encrypt'] for public key.
68
+ * @param hashAlgorithm 'sha-1' or 'sha-256'
69
+ * @returns {*}
70
+ */
71
+ importKey(format: 'jwk' | 'spki' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, keyUsages: KeyUsage[], hashAlgorithm: ShaVersion): Promise<CryptoKey>;
72
+ importSignatureKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey>;
73
+ importVerificationKey(format: 'jwk' | 'spki', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey>;
74
+ /**
75
+ *
76
+ * @param format 'jwk' or 'pkcs8'
77
+ * @param keydata should be the key data based on the format.
78
+ * @param hashAlgorithm 'sha-1' or 'sha-256'
79
+ * @returns {*}
80
+ */
81
+ importPrivateKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<CryptoKey>;
82
+ /**
83
+ *
84
+ * @param privateKeyFormat 'jwk' or 'pkcs8'
85
+ * @param privateKeydata should be the key data based on the format.
86
+ * @param publicKeyFormat 'jwk' or 'spki'
87
+ * @param publicKeyData should be the key data based on the format.
88
+ * @param hashAlgorithm 'sha-1' or 'sha-256'
89
+ * @returns {Promise|*}
90
+ */
91
+ importKeyPair(privateKeyFormat: 'jwk' | 'pkcs8', privateKeydata: JsonWebKey | ArrayBuffer, publicKeyFormat: 'jwk' | 'spki', publicKeyData: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<KeyPair<CryptoKey>>;
92
+ /**
93
+ * Tries to encrypt then decrypt data using a keypair. If both operations succeed without throwing an error and the decrypted data matches the
94
+ * original data returns true, else false.
95
+ * @param keyPair a key pair.
96
+ * @return if the key pair could be successfully used to encrypt then decrypt data.
97
+ */
98
+ checkKeyPairValidity(keyPair: KeyPair<CryptoKey>): Promise<boolean>;
99
+ /**
100
+ * Generates a signature for some data. The signature algorithm used is RSA-PSS with SHA-256.
101
+ * @param privateKey private key to use for signature
102
+ * @param data the data to sign
103
+ * @return the signature.
104
+ */
105
+ sign(privateKey: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>;
106
+ /**
107
+ * Verifies if a signature matches the data. The signature algorithm used is RSA-PSS with sha-256.
108
+ * @param publicKey public key to use for signature verification.
109
+ * @param signature the signature to verify
110
+ * @param data the data that was signed
111
+ * @return if the signature matches the data and key.
112
+ */
113
+ verifySignature(publicKey: CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean>;
114
+ }
115
+ export declare class RSAUtilsImpl implements RSAUtils {
13
116
  /********* RSA Config **********/
14
117
  rsaParams: any;
15
118
  rsaHashedParams: any;
@@ -73,10 +176,10 @@ export declare class RSAUtils {
73
176
  * @param hashAlgorithm 'sha-1' or 'sha-256'
74
177
  * @returns {*}
75
178
  */
76
- importKey(format: string, keydata: JsonWebKey | ArrayBuffer, keyUsages: KeyUsage[], hashAlgorithm: ShaVersion): Promise<CryptoKey>;
179
+ importKey(format: 'jwk' | 'spki' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, keyUsages: KeyUsage[], hashAlgorithm: ShaVersion): Promise<CryptoKey>;
77
180
  importSignatureKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey>;
78
181
  importVerificationKey(format: 'jwk' | 'spki', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey>;
79
- private paramsForCreationOrImport;
182
+ paramsForCreationOrImport(shaVersion: ShaVersion): any;
80
183
  /**
81
184
  *
82
185
  * @param format 'jwk' or 'pkcs8'
@@ -84,7 +187,7 @@ export declare class RSAUtils {
84
187
  * @param hashAlgorithm 'sha-1' or 'sha-256'
85
188
  * @returns {*}
86
189
  */
87
- importPrivateKey(format: string, keydata: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<CryptoKey>;
190
+ importPrivateKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<CryptoKey>;
88
191
  /**
89
192
  *
90
193
  * @param privateKeyFormat 'jwk' or 'pkcs8'
@@ -9,14 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.RSA = exports.RSAUtils = exports.ShaVersion = void 0;
12
+ exports.RSA = exports.RSAUtilsImpl = exports.ShaVersion = void 0;
13
13
  const utils_1 = require("../utils");
14
14
  var ShaVersion;
15
15
  (function (ShaVersion) {
16
16
  ShaVersion["Sha1"] = "sha-1";
17
17
  ShaVersion["Sha256"] = "sha-256";
18
18
  })(ShaVersion = exports.ShaVersion || (exports.ShaVersion = {}));
19
- class RSAUtils {
19
+ class RSAUtilsImpl {
20
20
  constructor(crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : {}) {
21
21
  /********* RSA Config **********/
22
22
  //TODO bigger modulus
@@ -210,6 +210,6 @@ class RSAUtils {
210
210
  });
211
211
  }
212
212
  }
213
- exports.RSAUtils = RSAUtils;
214
- exports.RSA = new RSAUtils();
213
+ exports.RSAUtilsImpl = RSAUtilsImpl;
214
+ exports.RSA = new RSAUtilsImpl();
215
215
  //# sourceMappingURL=RSA.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"RSA.js","sourceRoot":"","sources":["../../../icc-x-api/crypto/RSA.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oCAA4C;AAO5C,IAAY,UAGX;AAHD,WAAY,UAAU;IACpB,4BAAc,CAAA;IACd,gCAAkB,CAAA;AACpB,CAAC,EAHW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QAGrB;AAED,MAAa,QAAQ;IA2BnB,YAAY,SAAiB,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,EAAa;QA1BvI,iCAAiC;QACjC,qBAAqB;QACrB,sBAAsB;QACtB,cAAS,GAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,CAAA;QACrC,mDAAmD;QACnD,oBAAe,GAAQ;YACrB,IAAI,EAAE,UAAU;YAChB,aAAa,EAAE,IAAI;YACnB,cAAc,EAAE,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAClD,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;SACxB,CAAA;QACD,8BAAyB,GAAQ;YAC/B,IAAI,EAAE,UAAU;YAChB,aAAa,EAAE,IAAI;YACnB,cAAc,EAAE,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAClD,IAAI,EAAE,EAAE,IAAI,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AAG,YAAY,CAAA;gBACzB,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,IAAA,gBAAQ,EAAC,IAAI,CAAC,CAAC,CAAA;gBAC3E,MAAM,aAAa,GAAG,IAAA,eAAO,EAAC,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAA;gBACpG,OAAO,aAAa,KAAK,IAAI,CAAA;aAC9B;YAAC,OAAO,CAAC,EAAE;gBACV,OAAO,KAAK,CAAA;aACb;QACH,CAAC;KAAA;IAED;;;;;OAKG;IACG,IAAI,CAAC,UAAqB,EAAE,IAAiB;;YACjD,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;QAC7F,CAAC;KAAA;IAED;;;;;;OAMG;IACG,eAAe,CAAC,SAAoB,EAAE,SAAsB,EAAE,IAAiB;;YACnF,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAA;QACzG,CAAC;KAAA;CACF;AA5OD,4BA4OC;AAEY,QAAA,GAAG,GAAG,IAAI,QAAQ,EAAE,CAAA","sourcesContent":["import { ua2utf8, utf8_2ua } from '../utils'\n\n/**\n * Represents an RSA KeyPair in a generic format.\n */\nexport type KeyPair<T> = { publicKey: T; privateKey: T }\n\nexport enum ShaVersion {\n Sha1 = 'sha-1',\n Sha256 = 'sha-256',\n}\n\nexport class RSAUtils {\n /********* RSA Config **********/\n //TODO bigger modulus\n //TODO PSS for signing\n rsaParams: any = { name: 'RSA-OAEP' }\n // RSA params for 'import' and 'generate' function.\n rsaHashedParams: any = {\n name: 'RSA-OAEP',\n modulusLength: 2048,\n publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 (Fermat F4), read http://en.wikipedia.org/wiki/65537_(number)\n hash: { name: 'sha-1' },\n }\n rsaWithSha256HashedParams: any = {\n name: 'RSA-OAEP',\n modulusLength: 2048,\n publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 (Fermat F4), read http://en.wikipedia.org/wiki/65537_(number)\n hash: { name: 'SHA-256' },\n }\n private readonly signatureKeysGenerationParams = {\n name: 'RSA-PSS',\n modulusLength: 2048,\n publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 (Fermat F4), read http://en.wikipedia.org/wiki/65537_(number)\n hash: { name: 'SHA-256' },\n }\n\n private crypto: Crypto\n\n constructor(crypto: Crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : ({} as Crypto)) {\n this.crypto = crypto\n }\n\n /**\n * Generates a key pair for encryption/decryption of data.\n * @param shaVersion the version of the SHA algorithm to use.\n */\n generateKeyPair(shaVersion: ShaVersion): Promise<KeyPair<CryptoKey>> {\n const extractable = true\n const keyUsages: KeyUsage[] = ['decrypt', 'encrypt']\n const rsaParams = this.paramsForCreationOrImport(shaVersion)\n\n return new Promise<KeyPair<CryptoKey>>((resolve, reject) => {\n this.crypto.subtle.generateKey(rsaParams, extractable, keyUsages).then(resolve, reject)\n })\n }\n\n /**\n * Generates a key pair for signing data and signature verification.\n */\n async generateSignatureKeyPair(): Promise<KeyPair<CryptoKey>> {\n return await this.crypto.subtle.generateKey(this.signatureKeysGenerationParams, true, ['sign', 'verify'])\n }\n\n /**\n *\n * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)\n * 'spki': for private key\n * 'pkcs8': for private Key\n *\n * @param keyPair is {publicKey: CryptoKey, privateKey: CryptoKey}\n * @param privKeyFormat will be 'pkcs8' or 'jwk'\n * @param pubKeyFormat will be 'spki' or 'jwk'\n * @returns {Promise} will the AES Key\n */\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'jwk', pubKeyFormat: 'jwk'): Promise<KeyPair<JsonWebKey>>\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'pkcs8', pubKeyFormat: 'spki'): Promise<KeyPair<ArrayBuffer>>\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: string, pubKeyFormat: string): Promise<KeyPair<JsonWebKey | ArrayBuffer>> {\n const pubPromise = this.crypto.subtle.exportKey(pubKeyFormat as any, keyPair.publicKey)\n const privPromise = this.crypto.subtle.exportKey(privKeyFormat as any, keyPair.privateKey)\n\n return Promise.all([pubPromise, privPromise]).then(function (results) {\n return {\n publicKey: results[0],\n privateKey: results[1],\n }\n })\n }\n\n /**\n * Format:\n *\n * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)\n * 'spki': for private key\n * 'pkcs8': for private Key\n *\n * @param cryptoKey public or private\n * @param format either 'jwk' or 'spki' or 'pkcs8'\n * @returns {Promise|*} will be RSA key (public or private)\n */\n exportKey(cryptoKey: CryptoKey, format: 'jwk'): Promise<JsonWebKey>\n exportKey(cryptoKey: CryptoKey, format: 'spki'): Promise<ArrayBuffer>\n exportKey(cryptoKey: CryptoKey, format: 'pkcs8'): Promise<ArrayBuffer>\n exportKey(cryptoKey: CryptoKey, format: string): Promise<JsonWebKey | ArrayBuffer> {\n return new Promise((resolve: (value: JsonWebKey | ArrayBuffer) => any, reject) => {\n this.crypto.subtle.exportKey(format as any, cryptoKey).then(resolve, reject)\n })\n }\n\n /**\n *\n * @param publicKey (CryptoKey)\n * @param plainData (Uint8Array)\n */\n encrypt(publicKey: CryptoKey, plainData: Uint8Array): Promise<ArrayBuffer> {\n return new Promise((resolve: (value: ArrayBuffer) => any, reject) => {\n this.crypto.subtle.encrypt(this.rsaParams, publicKey, plainData.buffer ? plainData.buffer : plainData).then(resolve, reject) //Node prefers arrayBuffer\n })\n }\n\n /**\n *\n * @param privateKey (CryptoKey)\n * @param encryptedData (Uint8Array)\n */\n decrypt(privateKey: CryptoKey, encryptedData: Uint8Array): Promise<ArrayBuffer> {\n return new Promise((resolve: (value: ArrayBuffer) => any, reject) => {\n this.crypto.subtle.decrypt(this.rsaParams, privateKey, encryptedData).then(resolve, reject)\n })\n }\n\n /**\n *\n * @param format 'jwk', 'spki', or 'pkcs8'\n * @param keydata should be the key data based on the format.\n * @param keyUsages Array of usages. For example, ['encrypt'] for public key.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {*}\n */\n importKey(format: string, keydata: JsonWebKey | ArrayBuffer, keyUsages: KeyUsage[], hashAlgorithm: ShaVersion): Promise<CryptoKey> {\n const extractable = true\n return new Promise((resolve: (value: CryptoKey) => any, reject) => {\n const rsaParams = this.paramsForCreationOrImport(hashAlgorithm)\n this.crypto.subtle.importKey(format as any, keydata as any, rsaParams, extractable, keyUsages).then(resolve, reject)\n })\n }\n\n importSignatureKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey> {\n const extractable = true\n return this.crypto.subtle.importKey(format as any, keydata as any, this.signatureKeysGenerationParams, extractable, ['sign'])\n }\n\n importVerificationKey(format: 'jwk' | 'spki', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey> {\n const extractable = true\n return this.crypto.subtle.importKey(format as any, keydata as any, this.signatureKeysGenerationParams, extractable, ['verify'])\n }\n\n private paramsForCreationOrImport(shaVersion: ShaVersion) {\n if (shaVersion === 'sha-1') {\n return this.rsaHashedParams\n } else if (shaVersion === 'sha-256') {\n return this.rsaWithSha256HashedParams\n } else {\n throw new Error('Unexpected error, invalid SHA version')\n }\n }\n\n /**\n *\n * @param format 'jwk' or 'pkcs8'\n * @param keydata should be the key data based on the format.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {*}\n */\n importPrivateKey(format: string, keydata: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<CryptoKey> {\n const extractable = true\n return new Promise((resolve: (value: CryptoKey) => any, reject) => {\n const rsaParams = this.paramsForCreationOrImport(hashAlgorithm)\n this.crypto.subtle.importKey(format as any, keydata as any, rsaParams, extractable, ['decrypt']).then(resolve, reject)\n })\n }\n\n /**\n *\n * @param privateKeyFormat 'jwk' or 'pkcs8'\n * @param privateKeydata should be the key data based on the format.\n * @param publicKeyFormat 'jwk' or 'spki'\n * @param publicKeyData should be the key data based on the format.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {Promise|*}\n */\n importKeyPair(\n privateKeyFormat: string,\n privateKeydata: JsonWebKey | ArrayBuffer,\n publicKeyFormat: string,\n publicKeyData: JsonWebKey | ArrayBuffer,\n hashAlgorithm: ShaVersion\n ): Promise<KeyPair<CryptoKey>> {\n const extractable = true\n const rsaParams = this.paramsForCreationOrImport(hashAlgorithm)\n const privPromise = this.crypto.subtle.importKey(privateKeyFormat as any, privateKeydata as any, rsaParams, extractable, ['decrypt'])\n const pubPromise = this.crypto.subtle.importKey(publicKeyFormat as any, publicKeyData as any, rsaParams, extractable, ['encrypt'])\n\n return Promise.all([pubPromise, privPromise]).then(function (results) {\n return {\n publicKey: results[0],\n privateKey: results[1],\n }\n })\n }\n\n /**\n * Tries to encrypt then decrypt data using a keypair. If both operations succeed without throwing an error and the decrypted data matches the\n * original data returns true, else false.\n * @param keyPair a key pair.\n * @return if the key pair could be successfully used to encrypt then decrypt data.\n */\n async checkKeyPairValidity(keyPair: KeyPair<CryptoKey>): Promise<boolean> {\n try {\n const text = 'shibboleth'\n const encryptedText = await this.encrypt(keyPair.publicKey, utf8_2ua(text))\n const decryptedText = ua2utf8(await this.decrypt(keyPair.privateKey, new Uint8Array(encryptedText)))\n return decryptedText === text\n } catch (e) {\n return false\n }\n }\n\n /**\n * Generates a signature for some data. The signature algorithm used is RSA-PSS with SHA-256.\n * @param privateKey private key to use for signature\n * @param data the data to sign\n * @return the signature.\n */\n async sign(privateKey: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {\n return await this.crypto.subtle.sign({ name: 'RSA-PSS', saltLength: 32 }, privateKey, data)\n }\n\n /**\n * Verifies if a signature matches the data. The signature algorithm used is RSA-PSS with sha-256.\n * @param publicKey public key to use for signature verification.\n * @param signature the signature to verify\n * @param data the data that was signed\n * @return if the signature matches the data and key.\n */\n async verifySignature(publicKey: CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {\n return await this.crypto.subtle.verify({ name: 'RSA-PSS', saltLength: 32 }, publicKey, signature, data)\n }\n}\n\nexport const RSA = new RSAUtils()\n"]}
1
+ {"version":3,"file":"RSA.js","sourceRoot":"","sources":["../../../icc-x-api/crypto/RSA.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oCAA4C;AAO5C,IAAY,UAGX;AAHD,WAAY,UAAU;IACpB,4BAAc,CAAA;IACd,gCAAkB,CAAA;AACpB,CAAC,EAHW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QAGrB;AAkID,MAAa,YAAY;IA2BvB,YAAY,SAAiB,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,EAAa;QA1BvI,iCAAiC;QACjC,qBAAqB;QACrB,sBAAsB;QACtB,cAAS,GAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,CAAA;QACrC,mDAAmD;QACnD,oBAAe,GAAQ;YACrB,IAAI,EAAE,UAAU;YAChB,aAAa,EAAE,IAAI;YACnB,cAAc,EAAE,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAClD,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;SACxB,CAAA;QACD,8BAAyB,GAAQ;YAC/B,IAAI,EAAE,UAAU;YAChB,a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AC,CAAC;aACvB,CAAA;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;IAED;;;;;OAKG;IACG,oBAAoB,CAAC,OAA2B;;YACpD,IAAI;gBACF,MAAM,IAAI,GAAG,YAAY,CAAA;gBACzB,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,IAAA,gBAAQ,EAAC,IAAI,CAAC,CAAC,CAAA;gBAC3E,MAAM,aAAa,GAAG,IAAA,eAAO,EAAC,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAA;gBACpG,OAAO,aAAa,KAAK,IAAI,CAAA;aAC9B;YAAC,OAAO,CAAC,EAAE;gBACV,OAAO,KAAK,CAAA;aACb;QACH,CAAC;KAAA;IAED;;;;;OAKG;IACG,IAAI,CAAC,UAAqB,EAAE,IAAiB;;YACjD,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;QAC7F,CAAC;KAAA;IAED;;;;;;OAMG;IACG,eAAe,CAAC,SAAoB,EAAE,SAAsB,EAAE,IAAiB;;YACnF,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAA;QACzG,CAAC;KAAA;CACF;AAjPD,oCAiPC;AAEY,QAAA,GAAG,GAAa,IAAI,YAAY,EAAE,CAAA","sourcesContent":["import { ua2utf8, utf8_2ua } from '../utils'\n\n/**\n * Represents an RSA KeyPair in a generic format.\n */\nexport type KeyPair<T> = { publicKey: T; privateKey: T }\n\nexport enum ShaVersion {\n Sha1 = 'sha-1',\n Sha256 = 'sha-256',\n}\n\nexport interface RSAUtils {\n /**\n * Generates a key pair for encryption/decryption of data.\n * @param shaVersion the version of the SHA algorithm to use.\n */\n generateKeyPair(shaVersion: ShaVersion): Promise<KeyPair<CryptoKey>>\n\n /**\n * Generates a key pair for signing data and signature verification.\n */\n generateSignatureKeyPair(): Promise<KeyPair<CryptoKey>>\n\n /**\n *\n * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)\n * 'spki': for private key\n * 'pkcs8': for private Key\n *\n * @param keyPair is {publicKey: CryptoKey, privateKey: CryptoKey}\n * @param privKeyFormat will be 'pkcs8' or 'jwk'\n * @param pubKeyFormat will be 'spki' or 'jwk'\n * @returns {Promise} will the AES Key\n */\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'jwk', pubKeyFormat: 'jwk'): Promise<KeyPair<JsonWebKey>>\n\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'pkcs8', pubKeyFormat: 'spki'): Promise<KeyPair<ArrayBuffer>>\n\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: string, pubKeyFormat: string): Promise<KeyPair<JsonWebKey | ArrayBuffer>>\n\n /**\n * Format:\n *\n * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)\n * 'spki': for private key\n * 'pkcs8': for private Key\n *\n * @param cryptoKey public or private\n * @param format either 'jwk' or 'spki' or 'pkcs8'\n * @returns {Promise|*} will be RSA key (public or private)\n */\n exportKey(cryptoKey: CryptoKey, format: 'jwk'): Promise<JsonWebKey>\n\n exportKey(cryptoKey: CryptoKey, format: 'spki'): Promise<ArrayBuffer>\n\n exportKey(cryptoKey: CryptoKey, format: 'pkcs8'): Promise<ArrayBuffer>\n\n exportKey(cryptoKey: CryptoKey, format: string): Promise<JsonWebKey | ArrayBuffer>\n\n /**\n *\n * @param publicKey (CryptoKey)\n * @param plainData (Uint8Array)\n */\n encrypt(publicKey: CryptoKey, plainData: Uint8Array): Promise<ArrayBuffer>\n\n /**\n *\n * @param privateKey (CryptoKey)\n * @param encryptedData (Uint8Array)\n */\n decrypt(privateKey: CryptoKey, encryptedData: Uint8Array): Promise<ArrayBuffer>\n\n /**\n *\n * @param format 'jwk', 'spki', or 'pkcs8'\n * @param keydata should be the key data based on the format.\n * @param keyUsages Array of usages. For example, ['encrypt'] for public key.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {*}\n */\n importKey(format: 'jwk' | 'spki' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, keyUsages: KeyUsage[], hashAlgorithm: ShaVersion): Promise<CryptoKey>\n\n importSignatureKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey>\n\n importVerificationKey(format: 'jwk' | 'spki', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey>\n\n /**\n *\n * @param format 'jwk' or 'pkcs8'\n * @param keydata should be the key data based on the format.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {*}\n */\n importPrivateKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<CryptoKey>\n\n /**\n *\n * @param privateKeyFormat 'jwk' or 'pkcs8'\n * @param privateKeydata should be the key data based on the format.\n * @param publicKeyFormat 'jwk' or 'spki'\n * @param publicKeyData should be the key data based on the format.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {Promise|*}\n */\n importKeyPair(\n privateKeyFormat: 'jwk' | 'pkcs8',\n privateKeydata: JsonWebKey | ArrayBuffer,\n publicKeyFormat: 'jwk' | 'spki',\n publicKeyData: JsonWebKey | ArrayBuffer,\n hashAlgorithm: ShaVersion\n ): Promise<KeyPair<CryptoKey>>\n\n /**\n * Tries to encrypt then decrypt data using a keypair. If both operations succeed without throwing an error and the decrypted data matches the\n * original data returns true, else false.\n * @param keyPair a key pair.\n * @return if the key pair could be successfully used to encrypt then decrypt data.\n */\n checkKeyPairValidity(keyPair: KeyPair<CryptoKey>): Promise<boolean>\n\n /**\n * Generates a signature for some data. The signature algorithm used is RSA-PSS with SHA-256.\n * @param privateKey private key to use for signature\n * @param data the data to sign\n * @return the signature.\n */\n sign(privateKey: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer>\n\n /**\n * Verifies if a signature matches the data. The signature algorithm used is RSA-PSS with sha-256.\n * @param publicKey public key to use for signature verification.\n * @param signature the signature to verify\n * @param data the data that was signed\n * @return if the signature matches the data and key.\n */\n verifySignature(publicKey: CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean>\n}\n\nexport class RSAUtilsImpl implements RSAUtils {\n /********* RSA Config **********/\n //TODO bigger modulus\n //TODO PSS for signing\n rsaParams: any = { name: 'RSA-OAEP' }\n // RSA params for 'import' and 'generate' function.\n rsaHashedParams: any = {\n name: 'RSA-OAEP',\n modulusLength: 2048,\n publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 (Fermat F4), read http://en.wikipedia.org/wiki/65537_(number)\n hash: { name: 'sha-1' },\n }\n rsaWithSha256HashedParams: any = {\n name: 'RSA-OAEP',\n modulusLength: 2048,\n publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 (Fermat F4), read http://en.wikipedia.org/wiki/65537_(number)\n hash: { name: 'SHA-256' },\n }\n private readonly signatureKeysGenerationParams = {\n name: 'RSA-PSS',\n modulusLength: 2048,\n publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 (Fermat F4), read http://en.wikipedia.org/wiki/65537_(number)\n hash: { name: 'SHA-256' },\n }\n\n private crypto: Crypto\n\n constructor(crypto: Crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : ({} as Crypto)) {\n this.crypto = crypto\n }\n\n /**\n * Generates a key pair for encryption/decryption of data.\n * @param shaVersion the version of the SHA algorithm to use.\n */\n generateKeyPair(shaVersion: ShaVersion): Promise<KeyPair<CryptoKey>> {\n const extractable = true\n const keyUsages: KeyUsage[] = ['decrypt', 'encrypt']\n const rsaParams = this.paramsForCreationOrImport(shaVersion)\n\n return new Promise<KeyPair<CryptoKey>>((resolve, reject) => {\n this.crypto.subtle.generateKey(rsaParams, extractable, keyUsages).then(resolve, reject)\n })\n }\n\n /**\n * Generates a key pair for signing data and signature verification.\n */\n async generateSignatureKeyPair(): Promise<KeyPair<CryptoKey>> {\n return await this.crypto.subtle.generateKey(this.signatureKeysGenerationParams, true, ['sign', 'verify'])\n }\n\n /**\n *\n * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)\n * 'spki': for private key\n * 'pkcs8': for private Key\n *\n * @param keyPair is {publicKey: CryptoKey, privateKey: CryptoKey}\n * @param privKeyFormat will be 'pkcs8' or 'jwk'\n * @param pubKeyFormat will be 'spki' or 'jwk'\n * @returns {Promise} will the AES Key\n */\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'jwk', pubKeyFormat: 'jwk'): Promise<KeyPair<JsonWebKey>>\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: 'pkcs8', pubKeyFormat: 'spki'): Promise<KeyPair<ArrayBuffer>>\n exportKeys(keyPair: KeyPair<CryptoKey>, privKeyFormat: string, pubKeyFormat: string): Promise<KeyPair<JsonWebKey | ArrayBuffer>> {\n const pubPromise = this.crypto.subtle.exportKey(pubKeyFormat as any, keyPair.publicKey)\n const privPromise = this.crypto.subtle.exportKey(privKeyFormat as any, keyPair.privateKey)\n\n return Promise.all([pubPromise, privPromise]).then(function (results) {\n return {\n publicKey: results[0],\n privateKey: results[1],\n }\n })\n }\n\n /**\n * Format:\n *\n * 'JWK': Json Web key (ref. http://tools.ietf.org/html/draft-ietf-jose-json-web-key-11)\n * 'spki': for private key\n * 'pkcs8': for private Key\n *\n * @param cryptoKey public or private\n * @param format either 'jwk' or 'spki' or 'pkcs8'\n * @returns {Promise|*} will be RSA key (public or private)\n */\n exportKey(cryptoKey: CryptoKey, format: 'jwk'): Promise<JsonWebKey>\n exportKey(cryptoKey: CryptoKey, format: 'spki'): Promise<ArrayBuffer>\n exportKey(cryptoKey: CryptoKey, format: 'pkcs8'): Promise<ArrayBuffer>\n exportKey(cryptoKey: CryptoKey, format: string): Promise<JsonWebKey | ArrayBuffer> {\n return new Promise((resolve: (value: JsonWebKey | ArrayBuffer) => any, reject) => {\n this.crypto.subtle.exportKey(format as any, cryptoKey).then(resolve, reject)\n })\n }\n\n /**\n *\n * @param publicKey (CryptoKey)\n * @param plainData (Uint8Array)\n */\n encrypt(publicKey: CryptoKey, plainData: Uint8Array): Promise<ArrayBuffer> {\n return new Promise((resolve: (value: ArrayBuffer) => any, reject) => {\n this.crypto.subtle.encrypt(this.rsaParams, publicKey, plainData.buffer ? plainData.buffer : plainData).then(resolve, reject) //Node prefers arrayBuffer\n })\n }\n\n /**\n *\n * @param privateKey (CryptoKey)\n * @param encryptedData (Uint8Array)\n */\n decrypt(privateKey: CryptoKey, encryptedData: Uint8Array): Promise<ArrayBuffer> {\n return new Promise((resolve: (value: ArrayBuffer) => any, reject) => {\n this.crypto.subtle.decrypt(this.rsaParams, privateKey, encryptedData).then(resolve, reject)\n })\n }\n\n /**\n *\n * @param format 'jwk', 'spki', or 'pkcs8'\n * @param keydata should be the key data based on the format.\n * @param keyUsages Array of usages. For example, ['encrypt'] for public key.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {*}\n */\n importKey(\n format: 'jwk' | 'spki' | 'pkcs8',\n keydata: JsonWebKey | ArrayBuffer,\n keyUsages: KeyUsage[],\n hashAlgorithm: ShaVersion\n ): Promise<CryptoKey> {\n const extractable = true\n return new Promise((resolve: (value: CryptoKey) => any, reject) => {\n const rsaParams = this.paramsForCreationOrImport(hashAlgorithm)\n this.crypto.subtle.importKey(format as any, keydata as any, rsaParams, extractable, keyUsages).then(resolve, reject)\n })\n }\n\n importSignatureKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey> {\n const extractable = true\n return this.crypto.subtle.importKey(format as any, keydata as any, this.signatureKeysGenerationParams, extractable, ['sign'])\n }\n\n importVerificationKey(format: 'jwk' | 'spki', keydata: JsonWebKey | ArrayBuffer): Promise<CryptoKey> {\n const extractable = true\n return this.crypto.subtle.importKey(format as any, keydata as any, this.signatureKeysGenerationParams, extractable, ['verify'])\n }\n\n paramsForCreationOrImport(shaVersion: ShaVersion) {\n if (shaVersion === 'sha-1') {\n return this.rsaHashedParams\n } else if (shaVersion === 'sha-256') {\n return this.rsaWithSha256HashedParams\n } else {\n throw new Error('Unexpected error, invalid SHA version')\n }\n }\n\n /**\n *\n * @param format 'jwk' or 'pkcs8'\n * @param keydata should be the key data based on the format.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {*}\n */\n importPrivateKey(format: 'jwk' | 'pkcs8', keydata: JsonWebKey | ArrayBuffer, hashAlgorithm: ShaVersion): Promise<CryptoKey> {\n const extractable = true\n return new Promise((resolve: (value: CryptoKey) => any, reject) => {\n const rsaParams = this.paramsForCreationOrImport(hashAlgorithm)\n this.crypto.subtle.importKey(format as any, keydata as any, rsaParams, extractable, ['decrypt']).then(resolve, reject)\n })\n }\n\n /**\n *\n * @param privateKeyFormat 'jwk' or 'pkcs8'\n * @param privateKeydata should be the key data based on the format.\n * @param publicKeyFormat 'jwk' or 'spki'\n * @param publicKeyData should be the key data based on the format.\n * @param hashAlgorithm 'sha-1' or 'sha-256'\n * @returns {Promise|*}\n */\n importKeyPair(\n privateKeyFormat: string,\n privateKeydata: JsonWebKey | ArrayBuffer,\n publicKeyFormat: string,\n publicKeyData: JsonWebKey | ArrayBuffer,\n hashAlgorithm: ShaVersion\n ): Promise<KeyPair<CryptoKey>> {\n const extractable = true\n const rsaParams = this.paramsForCreationOrImport(hashAlgorithm)\n const privPromise = this.crypto.subtle.importKey(privateKeyFormat as any, privateKeydata as any, rsaParams, extractable, ['decrypt'])\n const pubPromise = this.crypto.subtle.importKey(publicKeyFormat as any, publicKeyData as any, rsaParams, extractable, ['encrypt'])\n\n return Promise.all([pubPromise, privPromise]).then(function (results) {\n return {\n publicKey: results[0],\n privateKey: results[1],\n }\n })\n }\n\n /**\n * Tries to encrypt then decrypt data using a keypair. If both operations succeed without throwing an error and the decrypted data matches the\n * original data returns true, else false.\n * @param keyPair a key pair.\n * @return if the key pair could be successfully used to encrypt then decrypt data.\n */\n async checkKeyPairValidity(keyPair: KeyPair<CryptoKey>): Promise<boolean> {\n try {\n const text = 'shibboleth'\n const encryptedText = await this.encrypt(keyPair.publicKey, utf8_2ua(text))\n const decryptedText = ua2utf8(await this.decrypt(keyPair.privateKey, new Uint8Array(encryptedText)))\n return decryptedText === text\n } catch (e) {\n return false\n }\n }\n\n /**\n * Generates a signature for some data. The signature algorithm used is RSA-PSS with SHA-256.\n * @param privateKey private key to use for signature\n * @param data the data to sign\n * @return the signature.\n */\n async sign(privateKey: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {\n return await this.crypto.subtle.sign({ name: 'RSA-PSS', saltLength: 32 }, privateKey, data)\n }\n\n /**\n * Verifies if a signature matches the data. The signature algorithm used is RSA-PSS with sha-256.\n * @param publicKey public key to use for signature verification.\n * @param signature the signature to verify\n * @param data the data that was signed\n * @return if the signature matches the data and key.\n */\n async verifySignature(publicKey: CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {\n return await this.crypto.subtle.verify({ name: 'RSA-PSS', saltLength: 32 }, publicKey, signature, data)\n }\n}\n\nexport const RSA: RSAUtils = new RSAUtilsImpl()\n"]}
@@ -1,4 +1,4 @@
1
- export declare class ShamirClass {
1
+ export declare abstract class ShamirClass {
2
2
  config: {
3
3
  bits: number;
4
4
  radix: number;
@@ -13,8 +13,7 @@ export declare class ShamirClass {
13
13
  logs: number[];
14
14
  exps: number[];
15
15
  };
16
- private crypto;
17
- constructor(crypto?: Crypto);
16
+ protected abstract fillRandom(arr: Uint32Array): void;
18
17
  init(): void;
19
18
  split(str: string, padLength?: number): number[];
20
19
  bin2hex(str: string): string;
@@ -37,4 +36,9 @@ export declare class ShamirClass {
37
36
  newShare(id: number | string, shares: Array<string>): string;
38
37
  lagrange(at: number, x: Array<number>, y: Array<number>): number;
39
38
  }
39
+ export declare class WebcryptoShamir extends ShamirClass {
40
+ private crypto;
41
+ constructor(crypto?: Crypto);
42
+ protected fillRandom(arr: Uint32Array): void;
43
+ }
40
44
  export declare const shamir: ShamirClass;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.shamir = exports.ShamirClass = void 0;
3
+ exports.shamir = exports.WebcryptoShamir = exports.ShamirClass = void 0;
4
4
  class ShamirClass {
5
- constructor(crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : {}) {
5
+ constructor() {
6
6
  // Protected settings object
7
7
  this.config = {
8
8
  bits: 8,
@@ -22,7 +22,6 @@ class ShamirClass {
22
22
  logs: [],
23
23
  exps: [],
24
24
  };
25
- this.crypto = crypto;
26
25
  }
27
26
  init() {
28
27
  const primitive = this.config.primitivePolynomials[this.config.bits];
@@ -93,7 +92,7 @@ class ShamirClass {
93
92
  };
94
93
  let elems = Math.ceil(bits / 32), str = null, arr = new Uint32Array(elems);
95
94
  while (str === null) {
96
- this.crypto.getRandomValues(arr);
95
+ this.fillRandom(arr);
97
96
  str = construct(bits, arr, 8);
98
97
  }
99
98
  return str;
@@ -276,5 +275,15 @@ class ShamirClass {
276
275
  }
277
276
  }
278
277
  exports.ShamirClass = ShamirClass;
279
- exports.shamir = new ShamirClass();
278
+ class WebcryptoShamir extends ShamirClass {
279
+ constructor(crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : {}) {
280
+ super();
281
+ this.crypto = crypto;
282
+ }
283
+ fillRandom(arr) {
284
+ this.crypto.getRandomValues(arr);
285
+ }
286
+ }
287
+ exports.WebcryptoShamir = WebcryptoShamir;
288
+ exports.shamir = new WebcryptoShamir();
280
289
  //# sourceMappingURL=shamir.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shamir.js","sourceRoot":"","sources":["../../../icc-x-api/crypto/shamir.ts"],"names":[],"mappings":";;;AAAA,MAAa,WAAW;IAsBtB,YAAY,SAAiB,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,EAAa;QArBvI,4BAA4B;QAC5B,WAAM,GAAG;YACP,IAAI,EAAE,CAAC;YACP,KAAK,EAAE,EAAE;YACT,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,iBAAiB,CAAC;YACtC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC;YACzC,OAAO,EAAE,CAAC;YACV,OAAO,EAAE,EAAE;YACX,YAAY,EAAE,CAAC;YACf,eAAe,EAAE,CAAC;YAClB,sFAAsF;YACtF,+EAA+E;YAC/E,oEAAoE;YACpE,oBAAoB,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACtI,4BAA4B;YAC5B,OAAO,EAAE,wHAAwH;YACjI,IAAI,EAAE,EAAmB;YACzB,IAAI,EAAE,EAAmB;SAC1B,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AChC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;gBACxB,IAAI,CAAC,KAAK,CAAC,EAAE;oBACX,SAAQ;iBACT;gBACD,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;oBACf,kFAAkF;oBAClF,OAAO,GAAG,CAAC,CAAC,CAAA,CAAC,oFAAoF;oBACjG,MAAK;iBACN;gBACD,OAAO;oBACL,CAAC,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,oCAAoC;wBAC9H,IAAI,CAAC,MAAM,CAAC,GAAG,CAAA;aAClB;YAED,GAAG,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA,CAAC,mIAAmI;SACjM;QACD,OAAO,GAAG,CAAA;IACZ,CAAC;CACF;AAnUD,kCAmUC;AAEY,QAAA,MAAM,GAAG,IAAI,WAAW,EAAE,CAAA","sourcesContent":["export class ShamirClass {\n // Protected settings object\n config = {\n bits: 8, // default number of bits\n radix: 16, // work with HEX by default\n size: Math.pow(2, 8 /* config.bits */),\n max: Math.pow(2, 8 /* config.bits */) - 1,\n minBits: 3,\n maxBits: 16, // this permits 65,535 shares, though going this high is NOT recommended in JS!\n bytesPerChar: 2,\n maxBytesPerChar: 6, // Math.pow(256,7) > Math.pow(2,53)\n // Primitive polynomials (in decimal form) for Galois Fields GF(2^n), for 2 <= n <= 30\n // The index of each term in the array corresponds to the n for that polynomial\n // i.e. to get the polynomial for n=16, use primitivePolynomials[16]\n primitivePolynomials: [null, null, 1, 3, 3, 5, 3, 3, 29, 17, 9, 5, 83, 27, 43, 3, 45, 9, 39, 39, 9, 5, 3, 33, 27, 9, 71, 39, 9, 5, 83],\n // warning for insecure PRNG\n warning: 'WARNING:\\nA secure random number generator was not found.\\nUsing Math.random(), which is NOT cryptographically strong!',\n logs: [] as Array<number>,\n exps: [] as Array<number>,\n }\n private crypto: Crypto\n\n constructor(crypto: Crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : ({} as Crypto)) {\n this.crypto = crypto\n }\n\n init() {\n const primitive = this.config.primitivePolynomials[this.config.bits]!\n let x = 1\n\n for (let i = 0; i < this.config.size; i++) {\n this.config.exps[i] = x\n this.config.logs[x] = i\n x <<= 1\n if (x >= this.config.size) {\n x ^= primitive\n x &= this.config.max\n }\n }\n }\n\n // Splits a number string `bits`-length segments, after first\n // optionally zero-padding it to a length that is a multiple of `padLength.\n // Returns array of integers (each less than 2^bits-1), with each element\n // representing a `bits`-length segment of the input string from right to left,\n // i.e. parts[0] represents the right-most `bits`-length segment of the input string.\n split(str: string, padLength = 0) {\n if (padLength) {\n str = this.padLeft(str, padLength)\n }\n const parts = []\n for (var i = str.length; i > this.config.bits; i -= this.config.bits) {\n parts.push(parseInt(str.slice(i - this.config.bits, i), 2))\n }\n parts.push(parseInt(str.slice(0, i), 2))\n return parts\n }\n\n bin2hex(str: string) {\n let hex = '',\n num\n str = this.padLeft(str, 4)\n for (let i = str.length; i >= 4; i -= 4) {\n num = parseInt(str.slice(i - 4, i), 2)\n if (isNaN(num)) {\n throw new Error('Invalid binary character.')\n }\n hex = num.toString(16) + hex\n }\n return hex\n }\n\n hex2bin(str: string) {\n let bin = '',\n num\n for (let i = str.length - 1; i >= 0; i--) {\n num = parseInt(str[i], 16)\n if (isNaN(num)) {\n throw new Error('Invalid hex character.')\n }\n bin = this.padLeft(num.toString(2), 4) + bin\n }\n\n return bin\n }\n\n padLeft(str: string, bits: number = this.config.bits) {\n const missing = str.length % bits\n return (missing ? new Array(bits - missing + 1).join('0') : '') + str\n }\n\n random(bits: number) {\n const construct = (bits: number, arr: Uint32Array, size: number) => {\n let str = '',\n i = 0\n const len = arr.length - 1\n while (i < len || str.length < bits) {\n str += this.padLeft(arr[i].toString(16), size)\n i++\n }\n str = str.substr(-bits / 4)\n return (str.match(/0/g) || []).length === str.length ? null : str\n }\n\n let elems = Math.ceil(bits / 32),\n str = null,\n arr = new Uint32Array(elems)\n\n while (str === null) {\n this.crypto.getRandomValues(arr)\n str = construct(bits, arr, 8)\n }\n\n return str\n }\n\n share(secretString: string, numShares: number, threshold: number) {\n if (!this.config.logs.length) {\n this.init()\n }\n\n if (numShares % 1 !== 0 || numShares < 2) {\n throw new Error('Number of shares must be an integer between 2 and 2^bits-1 (' + this.config.max + '), inclusive.')\n }\n if (numShares > this.config.max) {\n var neededBits = Math.ceil(Math.log(numShares + 1) / Math.LN2)\n throw new Error(\n 'Number of shares must be an integer between 2 and 2^bits-1 (' +\n this.config.max +\n '), inclusive. To create ' +\n numShares +\n ' shares, use at least ' +\n neededBits +\n ' bits.'\n )\n }\n if (threshold % 1 !== 0 || threshold < 2) {\n throw new Error('Threshold number of shares must be an integer between 2 and 2^bits-1 (' + this.config.max + '), inclusive.')\n }\n if (threshold > this.config.max) {\n var neededBits = Math.ceil(Math.log(threshold + 1) / Math.LN2)\n throw new Error(\n 'Threshold number of shares must be an integer between 2 and 2^bits-1 (' +\n this.config.max +\n '), inclusive. To use a threshold of ' +\n threshold +\n ', use at least ' +\n neededBits +\n ' bits.'\n )\n }\n\n // append a 1 so that we can preserve the correct number of leading zeros in our secret\n const secret = this.split('1' + this.hex2bin(secretString), 0)\n const x = new Array(numShares),\n y = new Array(numShares)\n for (let i = 0, len = secret.length; i < len; i++) {\n const subShares = this._getShares(secret[i], numShares, threshold)\n for (let j = 0; j < numShares; j++) {\n x[j] = x[j] || subShares[j].x.toString(this.config.radix)\n y[j] = this.padLeft(subShares[j].y.toString(2)) + (y[j] ? y[j] : '')\n }\n }\n const padding = this.config.max.toString(this.config.radix).length\n return y.map((b, idx) => this.config.bits.toString(16).toUpperCase() + this.padLeft(x[idx], padding) + this.bin2hex(b))\n }\n\n // This is the basic polynomial generation and evaluation function\n // for a `config.bits`-length secret (NOT an arbitrary length)\n // Note: no error-checking at this stage! If `secrets` is NOT\n // a NUMBER less than 2^bits-1, the output will be incorrect!\n _getShares(secret: number, numShares: number, threshold: number) {\n const shares = []\n const coeffs = [secret]\n\n for (var i = 1; i < threshold; i++) {\n coeffs[i] = parseInt(this.random(this.config.bits), 16)\n }\n for (var i = 1, len = numShares + 1; i < len; i++) {\n shares[i - 1] = {\n x: i,\n y: this.horner(i, coeffs),\n }\n }\n return shares\n }\n\n // Polynomial evaluation at `x` using Horner's Method\n // TODO: this can possibly be sped up using other methods\n // NOTE: fx=fx * x + coeff[i] -> exp(log(fx) + log(x)) + coeff[i],\n // so if fx===0, just set fx to coeff[i] because\n // using the exp/log form will result in incorrect value\n horner(x: number, coeffs: Array<number>) {\n const logx = this.config.logs[x]\n let fx = 0\n for (let i = coeffs.length - 1; i >= 0; i--) {\n if (fx === 0) {\n fx = coeffs[i]\n continue\n }\n fx = this.config.exps[(logx + this.config.logs[fx]) % this.config.max] ^ coeffs[i]\n }\n return fx\n }\n\n processShare(share: string) {\n const bits = parseInt(share[0], 16)\n if (bits && (bits % 1 !== 0 || bits < this.config.minBits || bits > this.config.maxBits)) {\n throw new Error('Number of bits must be an integer between ' + this.config.minBits + ' and ' + this.config.maxBits + ', inclusive.')\n }\n\n const max = Math.pow(2, bits) - 1\n const idLength = max.toString(this.config.radix).length\n\n const id = parseInt(share.substr(1, idLength), this.config.radix)\n if (id % 1 !== 0 || id < 1 || id > max) {\n throw new Error('Share id must be an integer between 1 and ' + this.config.max + ', inclusive.')\n }\n share = share.substr(idLength + 1)\n if (!share.length) {\n throw new Error('Invalid share: zero-length share.')\n }\n return {\n bits: bits,\n id: id,\n value: share,\n }\n }\n\n _combine(at: number, shares: Array<string>) {\n if (!this.config.logs.length) {\n this.init()\n }\n\n const x: Array<number> = []\n const y: Array<Array<number>> = []\n let result = ''\n let idx: number\n\n for (var i = 0, len = shares.length; i < len; i++) {\n const share = this.processShare(shares[i])\n\n if (x.includes(share['id'])) {\n // repeated x value?\n continue\n }\n\n idx = x.push(share['id']) - 1\n const shareValues = this.split(this.hex2bin(share['value']))\n for (let j = 0, len2 = shareValues.length; j < len2; j++) {\n y[j] = y[j] || []\n y[j][idx] = shareValues[j]\n }\n }\n\n for (var i = 0, len = y.length; i < len; i++) {\n result = this.padLeft(this.lagrange(at, x, y[i]).toString(2)) + result\n }\n\n if (at === 0) {\n // reconstructing the secret\n return this.bin2hex(result.slice(result.indexOf('1') + 1))\n } else {\n // generating a new share\n return this.bin2hex(result)\n }\n }\n\n combine(shares: Array<string>) {\n return this._combine(0, shares)\n }\n\n // Generate a new share with id `id` (a number between 1 and 2^bits-1)\n // `id` can be a Number or a String in the default radix (16)\n newShare(id: number | string, shares: Array<string>) {\n if (typeof id === 'string') {\n id = parseInt(id, this.config.radix)\n }\n\n const share = this.processShare(shares[0])\n const max = Math.pow(2, share['bits']) - 1\n\n if (id % 1 !== 0 || id < 1 || id > max) {\n throw new Error('Share id must be an integer between 1 and ' + this.config.max + ', inclusive.')\n }\n\n const padding = max.toString(this.config.radix).length\n return this.config.bits.toString(16).toUpperCase() + this.padLeft(id.toString(this.config.radix), padding) + this._combine(id, shares)\n }\n\n // Evaluate the Lagrange interpolation polynomial at x = `at`\n // using x and y Arrays that are of the same length, with\n // corresponding elements constituting points on the polynomial.\n lagrange(at: number, x: Array<number>, y: Array<number>) {\n let sum = 0,\n product,\n i,\n j,\n len\n for (i = 0, len = x.length; i < len; i++) {\n if (!y[i]) {\n continue\n }\n\n product = this.config.logs[y[i]]\n for (j = 0; j < len; j++) {\n if (i === j) {\n continue\n }\n if (at === x[j]) {\n // happens when computing a share that is in the list of shares used to compute it\n product = -1 // fix for a zero product term, after which the sum should be sum^0 = sum, not sum^1\n break\n }\n product =\n (product + this.config.logs[at ^ x[j]] - this.config.logs[x[i] ^ x[j]] + this.config.max) /* to make sure it's not negative */ %\n this.config.max\n }\n\n sum = product === -1 ? sum : sum ^ this.config.exps[product] // though exps[-1]= undefined and undefined ^ anything = anything in chrome, this behavior may not hold everywhere, so do the check\n }\n return sum\n }\n}\n\nexport const shamir = new ShamirClass()\n"]}
1
+ {"version":3,"file":"shamir.js","sourceRoot":"","sources":["../../../icc-x-api/crypto/shamir.ts"],"names":[],"mappings":";;;AAAA,MAAsB,WAAW;IAAjC;QACE,4BAA4B;QAC5B,WAAM,GAAG;YACP,IAAI,EAAE,CAAC;YACP,KAAK,EAAE,EAAE;YACT,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,iBAAiB,CAAC;YACtC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC;YACzC,OAAO,EAAE,CAAC;YACV,OAAO,EAAE,EAAE;YACX,YAAY,EAAE,CAAC;YACf,eAAe,EAAE,CAAC;YAClB,sFAAsF;YACtF,+EAA+E;YAC/E,oEAAoE;YACpE,oBAAoB,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACtI,4BAA4B;YAC5B,OAAO,EAAE,wHAAwH;YACjI,IAAI,EAAE,EAAmB;YACzB,IAA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AAe,EAAE,CAAA","sourcesContent":["export abstract class ShamirClass {\n // Protected settings object\n config = {\n bits: 8, // default number of bits\n radix: 16, // work with HEX by default\n size: Math.pow(2, 8 /* config.bits */),\n max: Math.pow(2, 8 /* config.bits */) - 1,\n minBits: 3,\n maxBits: 16, // this permits 65,535 shares, though going this high is NOT recommended in JS!\n bytesPerChar: 2,\n maxBytesPerChar: 6, // Math.pow(256,7) > Math.pow(2,53)\n // Primitive polynomials (in decimal form) for Galois Fields GF(2^n), for 2 <= n <= 30\n // The index of each term in the array corresponds to the n for that polynomial\n // i.e. to get the polynomial for n=16, use primitivePolynomials[16]\n primitivePolynomials: [null, null, 1, 3, 3, 5, 3, 3, 29, 17, 9, 5, 83, 27, 43, 3, 45, 9, 39, 39, 9, 5, 3, 33, 27, 9, 71, 39, 9, 5, 83],\n // warning for insecure PRNG\n warning: 'WARNING:\\nA secure random number generator was not found.\\nUsing Math.random(), which is NOT cryptographically strong!',\n logs: [] as Array<number>,\n exps: [] as Array<number>,\n }\n\n protected abstract fillRandom(arr: Uint32Array): void\n\n init() {\n const primitive = this.config.primitivePolynomials[this.config.bits]!\n let x = 1\n\n for (let i = 0; i < this.config.size; i++) {\n this.config.exps[i] = x\n this.config.logs[x] = i\n x <<= 1\n if (x >= this.config.size) {\n x ^= primitive\n x &= this.config.max\n }\n }\n }\n\n // Splits a number string `bits`-length segments, after first\n // optionally zero-padding it to a length that is a multiple of `padLength.\n // Returns array of integers (each less than 2^bits-1), with each element\n // representing a `bits`-length segment of the input string from right to left,\n // i.e. parts[0] represents the right-most `bits`-length segment of the input string.\n split(str: string, padLength = 0) {\n if (padLength) {\n str = this.padLeft(str, padLength)\n }\n const parts = []\n for (var i = str.length; i > this.config.bits; i -= this.config.bits) {\n parts.push(parseInt(str.slice(i - this.config.bits, i), 2))\n }\n parts.push(parseInt(str.slice(0, i), 2))\n return parts\n }\n\n bin2hex(str: string) {\n let hex = '',\n num\n str = this.padLeft(str, 4)\n for (let i = str.length; i >= 4; i -= 4) {\n num = parseInt(str.slice(i - 4, i), 2)\n if (isNaN(num)) {\n throw new Error('Invalid binary character.')\n }\n hex = num.toString(16) + hex\n }\n return hex\n }\n\n hex2bin(str: string) {\n let bin = '',\n num\n for (let i = str.length - 1; i >= 0; i--) {\n num = parseInt(str[i], 16)\n if (isNaN(num)) {\n throw new Error('Invalid hex character.')\n }\n bin = this.padLeft(num.toString(2), 4) + bin\n }\n\n return bin\n }\n\n padLeft(str: string, bits: number = this.config.bits) {\n const missing = str.length % bits\n return (missing ? new Array(bits - missing + 1).join('0') : '') + str\n }\n\n random(bits: number) {\n const construct = (bits: number, arr: Uint32Array, size: number) => {\n let str = '',\n i = 0\n const len = arr.length - 1\n while (i < len || str.length < bits) {\n str += this.padLeft(arr[i].toString(16), size)\n i++\n }\n str = str.substr(-bits / 4)\n return (str.match(/0/g) || []).length === str.length ? null : str\n }\n\n let elems = Math.ceil(bits / 32),\n str = null,\n arr = new Uint32Array(elems)\n\n while (str === null) {\n this.fillRandom(arr)\n str = construct(bits, arr, 8)\n }\n\n return str\n }\n\n share(secretString: string, numShares: number, threshold: number) {\n if (!this.config.logs.length) {\n this.init()\n }\n\n if (numShares % 1 !== 0 || numShares < 2) {\n throw new Error('Number of shares must be an integer between 2 and 2^bits-1 (' + this.config.max + '), inclusive.')\n }\n if (numShares > this.config.max) {\n var neededBits = Math.ceil(Math.log(numShares + 1) / Math.LN2)\n throw new Error(\n 'Number of shares must be an integer between 2 and 2^bits-1 (' +\n this.config.max +\n '), inclusive. To create ' +\n numShares +\n ' shares, use at least ' +\n neededBits +\n ' bits.'\n )\n }\n if (threshold % 1 !== 0 || threshold < 2) {\n throw new Error('Threshold number of shares must be an integer between 2 and 2^bits-1 (' + this.config.max + '), inclusive.')\n }\n if (threshold > this.config.max) {\n var neededBits = Math.ceil(Math.log(threshold + 1) / Math.LN2)\n throw new Error(\n 'Threshold number of shares must be an integer between 2 and 2^bits-1 (' +\n this.config.max +\n '), inclusive. To use a threshold of ' +\n threshold +\n ', use at least ' +\n neededBits +\n ' bits.'\n )\n }\n\n // append a 1 so that we can preserve the correct number of leading zeros in our secret\n const secret = this.split('1' + this.hex2bin(secretString), 0)\n const x = new Array(numShares),\n y = new Array(numShares)\n for (let i = 0, len = secret.length; i < len; i++) {\n const subShares = this._getShares(secret[i], numShares, threshold)\n for (let j = 0; j < numShares; j++) {\n x[j] = x[j] || subShares[j].x.toString(this.config.radix)\n y[j] = this.padLeft(subShares[j].y.toString(2)) + (y[j] ? y[j] : '')\n }\n }\n const padding = this.config.max.toString(this.config.radix).length\n return y.map((b, idx) => this.config.bits.toString(16).toUpperCase() + this.padLeft(x[idx], padding) + this.bin2hex(b))\n }\n\n // This is the basic polynomial generation and evaluation function\n // for a `config.bits`-length secret (NOT an arbitrary length)\n // Note: no error-checking at this stage! If `secrets` is NOT\n // a NUMBER less than 2^bits-1, the output will be incorrect!\n _getShares(secret: number, numShares: number, threshold: number) {\n const shares = []\n const coeffs = [secret]\n\n for (var i = 1; i < threshold; i++) {\n coeffs[i] = parseInt(this.random(this.config.bits), 16)\n }\n for (var i = 1, len = numShares + 1; i < len; i++) {\n shares[i - 1] = {\n x: i,\n y: this.horner(i, coeffs),\n }\n }\n return shares\n }\n\n // Polynomial evaluation at `x` using Horner's Method\n // TODO: this can possibly be sped up using other methods\n // NOTE: fx=fx * x + coeff[i] -> exp(log(fx) + log(x)) + coeff[i],\n // so if fx===0, just set fx to coeff[i] because\n // using the exp/log form will result in incorrect value\n horner(x: number, coeffs: Array<number>) {\n const logx = this.config.logs[x]\n let fx = 0\n for (let i = coeffs.length - 1; i >= 0; i--) {\n if (fx === 0) {\n fx = coeffs[i]\n continue\n }\n fx = this.config.exps[(logx + this.config.logs[fx]) % this.config.max] ^ coeffs[i]\n }\n return fx\n }\n\n processShare(share: string) {\n const bits = parseInt(share[0], 16)\n if (bits && (bits % 1 !== 0 || bits < this.config.minBits || bits > this.config.maxBits)) {\n throw new Error('Number of bits must be an integer between ' + this.config.minBits + ' and ' + this.config.maxBits + ', inclusive.')\n }\n\n const max = Math.pow(2, bits) - 1\n const idLength = max.toString(this.config.radix).length\n\n const id = parseInt(share.substr(1, idLength), this.config.radix)\n if (id % 1 !== 0 || id < 1 || id > max) {\n throw new Error('Share id must be an integer between 1 and ' + this.config.max + ', inclusive.')\n }\n share = share.substr(idLength + 1)\n if (!share.length) {\n throw new Error('Invalid share: zero-length share.')\n }\n return {\n bits: bits,\n id: id,\n value: share,\n }\n }\n\n _combine(at: number, shares: Array<string>) {\n if (!this.config.logs.length) {\n this.init()\n }\n\n const x: Array<number> = []\n const y: Array<Array<number>> = []\n let result = ''\n let idx: number\n\n for (var i = 0, len = shares.length; i < len; i++) {\n const share = this.processShare(shares[i])\n\n if (x.includes(share['id'])) {\n // repeated x value?\n continue\n }\n\n idx = x.push(share['id']) - 1\n const shareValues = this.split(this.hex2bin(share['value']))\n for (let j = 0, len2 = shareValues.length; j < len2; j++) {\n y[j] = y[j] || []\n y[j][idx] = shareValues[j]\n }\n }\n\n for (var i = 0, len = y.length; i < len; i++) {\n result = this.padLeft(this.lagrange(at, x, y[i]).toString(2)) + result\n }\n\n if (at === 0) {\n // reconstructing the secret\n return this.bin2hex(result.slice(result.indexOf('1') + 1))\n } else {\n // generating a new share\n return this.bin2hex(result)\n }\n }\n\n combine(shares: Array<string>) {\n return this._combine(0, shares)\n }\n\n // Generate a new share with id `id` (a number between 1 and 2^bits-1)\n // `id` can be a Number or a String in the default radix (16)\n newShare(id: number | string, shares: Array<string>) {\n if (typeof id === 'string') {\n id = parseInt(id, this.config.radix)\n }\n\n const share = this.processShare(shares[0])\n const max = Math.pow(2, share['bits']) - 1\n\n if (id % 1 !== 0 || id < 1 || id > max) {\n throw new Error('Share id must be an integer between 1 and ' + this.config.max + ', inclusive.')\n }\n\n const padding = max.toString(this.config.radix).length\n return this.config.bits.toString(16).toUpperCase() + this.padLeft(id.toString(this.config.radix), padding) + this._combine(id, shares)\n }\n\n // Evaluate the Lagrange interpolation polynomial at x = `at`\n // using x and y Arrays that are of the same length, with\n // corresponding elements constituting points on the polynomial.\n lagrange(at: number, x: Array<number>, y: Array<number>) {\n let sum = 0,\n product,\n i,\n j,\n len\n for (i = 0, len = x.length; i < len; i++) {\n if (!y[i]) {\n continue\n }\n\n product = this.config.logs[y[i]]\n for (j = 0; j < len; j++) {\n if (i === j) {\n continue\n }\n if (at === x[j]) {\n // happens when computing a share that is in the list of shares used to compute it\n product = -1 // fix for a zero product term, after which the sum should be sum^0 = sum, not sum^1\n break\n }\n product =\n (product + this.config.logs[at ^ x[j]] - this.config.logs[x[i] ^ x[j]] + this.config.max) /* to make sure it's not negative */ %\n this.config.max\n }\n\n sum = product === -1 ? sum : sum ^ this.config.exps[product] // though exps[-1]= undefined and undefined ^ anything = anything in chrome, this behavior may not hold everywhere, so do the check\n }\n return sum\n }\n}\n\nexport class WebcryptoShamir extends ShamirClass {\n private crypto: Crypto\n\n constructor(crypto: Crypto = typeof window !== 'undefined' ? window.crypto : typeof self !== 'undefined' ? self.crypto : ({} as Crypto)) {\n super()\n this.crypto = crypto\n }\n\n protected fillRandom(arr: Uint32Array): void {\n this.crypto.getRandomValues(arr)\n }\n}\n\nexport const shamir: ShamirClass = new WebcryptoShamir()\n"]}
@@ -62,6 +62,7 @@ export { LocalStorageImpl } from './storage/LocalStorageImpl';
62
62
  export { StorageFacade } from './storage/StorageFacade';
63
63
  export { KeyStorageImpl } from './storage/KeyStorageImpl';
64
64
  export { CryptoStrategies } from './crypto/CryptoStrategies';
65
+ export { NativeCryptoPrimitivesBridge } from './crypto/NativeCryptoPrimitivesBridge';
65
66
  export { getShaVersionForKey } from './crypto/utils';
66
67
  export interface BasicApis {
67
68
  readonly authApi: IccAuthApi;
@@ -23,7 +23,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23
23
  });
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.BasicApis = exports.IcureApi = exports.EncryptedFieldsConfig = exports.getShaVersionForKey = exports.KeyStorageImpl = exports.LocalStorageImpl = void 0;
26
+ exports.BasicApis = exports.IcureApi = exports.EncryptedFieldsConfig = exports.getShaVersionForKey = exports.NativeCryptoPrimitivesBridge = exports.KeyStorageImpl = exports.LocalStorageImpl = void 0;
27
27
  const icc_api_1 = require("../icc-api");
28
28
  const icc_user_x_api_1 = require("./icc-user-x-api");
29
29
  const icc_crypto_x_api_1 = require("./icc-crypto-x-api");
@@ -115,6 +115,8 @@ var LocalStorageImpl_2 = require("./storage/LocalStorageImpl");
115
115
  Object.defineProperty(exports, "LocalStorageImpl", { enumerable: true, get: function () { return LocalStorageImpl_2.LocalStorageImpl; } });
116
116
  var KeyStorageImpl_2 = require("./storage/KeyStorageImpl");
117
117
  Object.defineProperty(exports, "KeyStorageImpl", { enumerable: true, get: function () { return KeyStorageImpl_2.KeyStorageImpl; } });
118
+ var NativeCryptoPrimitivesBridge_1 = require("./crypto/NativeCryptoPrimitivesBridge");
119
+ Object.defineProperty(exports, "NativeCryptoPrimitivesBridge", { enumerable: true, get: function () { return NativeCryptoPrimitivesBridge_1.NativeCryptoPrimitivesBridge; } });
118
120
  var utils_2 = require("./crypto/utils");
119
121
  Object.defineProperty(exports, "getShaVersionForKey", { enumerable: true, get: function () { return utils_2.getShaVersionForKey; } });
120
122
  var IcureApiOptions;
@@ -248,7 +250,7 @@ function initialiseCryptoWithProvider(host, fetchImpl, groupSpecificAuthenticati
248
250
  const baseRecoveryDataApi = new IccRecoveryDataApi_1.IccRecoveryDataApi(host, updatedHeaders, groupSpecificAuthenticationProvider, fetchImpl);
249
251
  // Crypto initialisation
250
252
  const icureStorage = new IcureStorageFacade_1.IcureStorageFacade(params.keyStorage, params.storage, params.entryKeysFactory);
251
- const cryptoPrimitives = new CryptoPrimitives_1.CryptoPrimitives(crypto);
253
+ const cryptoPrimitives = 'AES' in crypto && 'RSA' in crypto && 'HMAC' in crypto ? crypto : new CryptoPrimitives_1.CryptoPrimitivesImpl(crypto);
252
254
  const baseExchangeKeysManager = new BaseExchangeKeysManager_1.BaseExchangeKeysManager(cryptoPrimitives, dataOwnerApi, healthcarePartyApi, basePatientApi, deviceApi);
253
255
  const baseExchangeDataManager = new BaseExchangeDataManager_1.BaseExchangeDataManager(exchangeDataApi, dataOwnerApi, cryptoPrimitives, dataOwnerRequiresAnonymousDelegation);
254
256
  const keyRecovery = new KeyRecovery_1.KeyRecovery(cryptoPrimitives, dataOwnerApi, baseExchangeKeysManager, baseExchangeDataManager);
@@ -509,7 +511,7 @@ class IcureApiImpl {
509
511
  return __awaiter(this, void 0, void 0, function* () {
510
512
  const availableGroups = yield this.latestGroupsRequest;
511
513
  const switchedProvider = yield this.groupSpecificAuthenticationProvider.switchGroup(newGroupId, availableGroups);
512
- const cryptoInitInfos = yield initialiseCryptoWithProvider(this.host, this.fetch, switchedProvider, this.params, this.cryptoStrategies, this.cryptoApi.primitives.crypto);
514
+ const cryptoInitInfos = yield initialiseCryptoWithProvider(this.host, this.fetch, switchedProvider, this.params, this.cryptoStrategies, this.cryptoApi.primitives);
513
515
  return new IcureApiImpl(cryptoInitInfos, this.host, switchedProvider, this.fetch, this.grouplessUserApi, availableGroups, availableGroups.find((x) => x.groupId === newGroupId), this.params, this.cryptoStrategies);
514
516
  });
515
517
  }