react-native-quick-crypto 1.0.0-beta.1 → 1.0.0-beta.10

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 (173) hide show
  1. package/QuickCrypto.podspec +17 -4
  2. package/README.md +172 -0
  3. package/android/CMakeLists.txt +28 -17
  4. package/android/build.gradle +36 -3
  5. package/android/src/main/cpp/cpp-adapter.cpp +3 -10
  6. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +15 -10
  7. package/cpp/ed25519/HybridEdKeyPair.cpp +268 -0
  8. package/cpp/ed25519/HybridEdKeyPair.hpp +85 -0
  9. package/cpp/pbkdf2/HybridPbkdf2.cpp +72 -0
  10. package/cpp/pbkdf2/HybridPbkdf2.hpp +35 -0
  11. package/cpp/random/HybridRandom.cpp +59 -0
  12. package/cpp/random/HybridRandom.hpp +33 -0
  13. package/cpp/utils/Utils.hpp +20 -0
  14. package/deps/fastpbkdf2/fastpbkdf2.c +352 -0
  15. package/deps/fastpbkdf2/fastpbkdf2.h +68 -0
  16. package/lib/commonjs/ed.js +42 -0
  17. package/lib/commonjs/ed.js.map +1 -0
  18. package/lib/commonjs/index.js +61 -10
  19. package/lib/commonjs/index.js.map +1 -1
  20. package/lib/commonjs/keys/classes.js +191 -0
  21. package/lib/commonjs/keys/classes.js.map +1 -0
  22. package/lib/commonjs/keys/generateKeyPair.js +148 -0
  23. package/lib/commonjs/keys/generateKeyPair.js.map +1 -0
  24. package/lib/commonjs/keys/index.js +62 -0
  25. package/lib/commonjs/keys/index.js.map +1 -0
  26. package/lib/commonjs/keys/signVerify.js +41 -0
  27. package/lib/commonjs/keys/signVerify.js.map +1 -0
  28. package/lib/commonjs/keys/utils.js +118 -0
  29. package/lib/commonjs/keys/utils.js.map +1 -0
  30. package/lib/commonjs/pbkdf2.js +89 -0
  31. package/lib/commonjs/pbkdf2.js.map +1 -0
  32. package/lib/commonjs/random.js +3 -3
  33. package/lib/commonjs/random.js.map +1 -1
  34. package/lib/commonjs/specs/edKeyPair.nitro.js +6 -0
  35. package/lib/commonjs/specs/edKeyPair.nitro.js.map +1 -0
  36. package/lib/commonjs/specs/keyObjectHandle.nitro.js +6 -0
  37. package/lib/commonjs/specs/keyObjectHandle.nitro.js.map +1 -0
  38. package/lib/commonjs/specs/pbkdf2.nitro.js +6 -0
  39. package/lib/commonjs/specs/pbkdf2.nitro.js.map +1 -0
  40. package/lib/commonjs/utils/conversion.js +101 -6
  41. package/lib/commonjs/utils/conversion.js.map +1 -1
  42. package/lib/commonjs/utils/errors.js +14 -0
  43. package/lib/commonjs/utils/errors.js.map +1 -0
  44. package/lib/commonjs/utils/hashnames.js +90 -0
  45. package/lib/commonjs/utils/hashnames.js.map +1 -0
  46. package/lib/commonjs/utils/index.js +54 -5
  47. package/lib/commonjs/utils/index.js.map +1 -1
  48. package/lib/commonjs/utils/types.js +38 -0
  49. package/lib/commonjs/utils/types.js.map +1 -1
  50. package/lib/commonjs/utils/validation.js +25 -0
  51. package/lib/commonjs/utils/validation.js.map +1 -0
  52. package/lib/module/ed.js +37 -0
  53. package/lib/module/ed.js.map +1 -0
  54. package/lib/module/index.js +16 -9
  55. package/lib/module/index.js.map +1 -1
  56. package/lib/module/keys/classes.js +182 -0
  57. package/lib/module/keys/classes.js.map +1 -0
  58. package/lib/module/keys/generateKeyPair.js +148 -0
  59. package/lib/module/keys/generateKeyPair.js.map +1 -0
  60. package/lib/module/keys/index.js +29 -0
  61. package/lib/module/keys/index.js.map +1 -0
  62. package/lib/module/keys/signVerify.js +41 -0
  63. package/lib/module/keys/signVerify.js.map +1 -0
  64. package/lib/module/keys/utils.js +110 -0
  65. package/lib/module/keys/utils.js.map +1 -0
  66. package/lib/module/pbkdf2.js +83 -0
  67. package/lib/module/pbkdf2.js.map +1 -0
  68. package/lib/module/random.js +1 -1
  69. package/lib/module/random.js.map +1 -1
  70. package/lib/module/specs/edKeyPair.nitro.js +4 -0
  71. package/lib/module/specs/edKeyPair.nitro.js.map +1 -0
  72. package/lib/module/specs/keyObjectHandle.nitro.js +4 -0
  73. package/lib/module/specs/keyObjectHandle.nitro.js.map +1 -0
  74. package/lib/module/specs/pbkdf2.nitro.js +4 -0
  75. package/lib/module/specs/pbkdf2.nitro.js.map +1 -0
  76. package/lib/module/utils/conversion.js +99 -8
  77. package/lib/module/utils/conversion.js.map +1 -1
  78. package/lib/module/utils/errors.js +10 -0
  79. package/lib/module/utils/errors.js.map +1 -0
  80. package/lib/module/utils/hashnames.js +88 -0
  81. package/lib/module/utils/hashnames.js.map +1 -0
  82. package/lib/module/utils/index.js +5 -5
  83. package/lib/module/utils/index.js.map +1 -1
  84. package/lib/module/utils/types.js +40 -0
  85. package/lib/module/utils/types.js.map +1 -1
  86. package/lib/module/utils/validation.js +19 -0
  87. package/lib/module/utils/validation.js.map +1 -0
  88. package/lib/tsconfig.tsbuildinfo +1 -1
  89. package/lib/typescript/ed.d.ts +17 -0
  90. package/lib/typescript/ed.d.ts.map +1 -0
  91. package/lib/typescript/index.d.ts +50 -9
  92. package/lib/typescript/index.d.ts.map +1 -1
  93. package/lib/typescript/keys/classes.d.ts +38 -0
  94. package/lib/typescript/keys/classes.d.ts.map +1 -0
  95. package/lib/typescript/keys/generateKeyPair.d.ts +1 -0
  96. package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -0
  97. package/lib/typescript/keys/index.d.ts +4 -0
  98. package/lib/typescript/keys/index.d.ts.map +1 -0
  99. package/lib/typescript/keys/signVerify.d.ts +1 -0
  100. package/lib/typescript/keys/signVerify.d.ts.map +1 -0
  101. package/lib/typescript/keys/utils.d.ts +32 -0
  102. package/lib/typescript/keys/utils.d.ts.map +1 -0
  103. package/lib/typescript/pbkdf2.d.ts +12 -0
  104. package/lib/typescript/pbkdf2.d.ts.map +1 -0
  105. package/lib/typescript/random.d.ts +5 -5
  106. package/lib/typescript/random.d.ts.map +1 -1
  107. package/lib/typescript/specs/edKeyPair.nitro.d.ts +16 -0
  108. package/lib/typescript/specs/edKeyPair.nitro.d.ts.map +1 -0
  109. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +15 -0
  110. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -0
  111. package/lib/typescript/specs/pbkdf2.nitro.d.ts +9 -0
  112. package/lib/typescript/specs/pbkdf2.nitro.d.ts.map +1 -0
  113. package/lib/typescript/utils/conversion.d.ts +23 -2
  114. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  115. package/lib/typescript/utils/errors.d.ts +7 -0
  116. package/lib/typescript/utils/errors.d.ts.map +1 -0
  117. package/lib/typescript/utils/hashnames.d.ts +11 -0
  118. package/lib/typescript/utils/hashnames.d.ts.map +1 -0
  119. package/lib/typescript/utils/index.d.ts +5 -5
  120. package/lib/typescript/utils/index.d.ts.map +1 -1
  121. package/lib/typescript/utils/types.d.ts +158 -1
  122. package/lib/typescript/utils/types.d.ts.map +1 -1
  123. package/lib/typescript/utils/validation.d.ts +8 -0
  124. package/lib/typescript/utils/validation.d.ts.map +1 -0
  125. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +65 -0
  126. package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +27 -0
  127. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +64 -0
  128. package/nitrogen/generated/android/QuickCryptoOnLoad.hpp +25 -0
  129. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +58 -0
  130. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +17 -0
  131. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +27 -0
  132. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +38 -0
  133. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +55 -0
  134. package/nitrogen/generated/ios/QuickCryptoAutolinking.swift +12 -0
  135. package/nitrogen/generated/shared/c++/CFRGKeyPairType.hpp +86 -0
  136. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.cpp +29 -0
  137. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.hpp +74 -0
  138. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +27 -0
  139. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +93 -0
  140. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.cpp +22 -0
  141. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +66 -0
  142. package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +22 -0
  143. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +65 -0
  144. package/nitrogen/generated/shared/c++/JWK.hpp +162 -0
  145. package/nitrogen/generated/shared/c++/JWKkty.hpp +86 -0
  146. package/nitrogen/generated/shared/c++/JWKuse.hpp +78 -0
  147. package/nitrogen/generated/shared/c++/KFormatType.hpp +65 -0
  148. package/nitrogen/generated/shared/c++/KeyDetail.hpp +93 -0
  149. package/nitrogen/generated/shared/c++/KeyEncoding.hpp +66 -0
  150. package/nitrogen/generated/shared/c++/KeyType.hpp +65 -0
  151. package/nitrogen/generated/shared/c++/KeyUsage.hpp +102 -0
  152. package/nitrogen/generated/shared/c++/NamedCurve.hpp +82 -0
  153. package/package.json +55 -34
  154. package/src/ed.ts +79 -0
  155. package/src/index.ts +15 -9
  156. package/src/keys/classes.ts +211 -0
  157. package/src/keys/generateKeyPair.ts +146 -0
  158. package/src/keys/index.ts +42 -0
  159. package/src/keys/signVerify.ts +39 -0
  160. package/src/keys/utils.ts +184 -0
  161. package/src/pbkdf2.ts +154 -0
  162. package/src/random.ts +19 -23
  163. package/src/specs/edKeyPair.nitro.ts +41 -0
  164. package/src/specs/keyObjectHandle.nitro.ts +32 -0
  165. package/src/specs/pbkdf2.nitro.ts +18 -0
  166. package/src/specs/random.nitro.ts +2 -2
  167. package/src/utils/conversion.ts +116 -9
  168. package/src/utils/errors.ts +15 -0
  169. package/src/utils/hashnames.ts +96 -0
  170. package/src/utils/index.ts +5 -6
  171. package/src/utils/types.ts +263 -3
  172. package/src/utils/validation.ts +35 -0
  173. package/ios/QuickCryptoOnLoad.mm +0 -19
@@ -0,0 +1,211 @@
1
+ import type { KeyObjectHandle } from '../specs/keyObjectHandle.nitro';
2
+ import type {
3
+ AsymmetricKeyType,
4
+ EncodingOptions,
5
+ KeyUsage,
6
+ SubtleAlgorithm,
7
+ } from '../utils';
8
+ import { parsePrivateKeyEncoding, parsePublicKeyEncoding } from './utils';
9
+
10
+ export class CryptoKey {
11
+ keyObject: KeyObject;
12
+ keyAlgorithm: SubtleAlgorithm;
13
+ keyUsages: KeyUsage[];
14
+ keyExtractable: boolean;
15
+
16
+ constructor(
17
+ keyObject: KeyObject,
18
+ keyAlgorithm: SubtleAlgorithm,
19
+ keyUsages: KeyUsage[],
20
+ keyExtractable: boolean,
21
+ ) {
22
+ this.keyObject = keyObject;
23
+ this.keyAlgorithm = keyAlgorithm;
24
+ this.keyUsages = keyUsages;
25
+ this.keyExtractable = keyExtractable;
26
+ }
27
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
+ inspect(_depth: number, _options: unknown): unknown {
29
+ throw new Error('CryptoKey.inspect is not implemented');
30
+ // if (depth < 0) return this;
31
+
32
+ // const opts = {
33
+ // ...options,
34
+ // depth: options.depth == null ? null : options.depth - 1,
35
+ // };
36
+
37
+ // return `CryptoKey ${inspect(
38
+ // {
39
+ // type: this.type,
40
+ // extractable: this.extractable,
41
+ // algorithm: this.algorithm,
42
+ // usages: this.usages,
43
+ // },
44
+ // opts
45
+ // )}`;
46
+ }
47
+
48
+ get type() {
49
+ // if (!(this instanceof CryptoKey)) throw new Error('Invalid CryptoKey');
50
+ return this.keyObject.type;
51
+ }
52
+
53
+ get extractable() {
54
+ return this.keyExtractable;
55
+ }
56
+
57
+ get algorithm() {
58
+ return this.keyAlgorithm;
59
+ }
60
+
61
+ get usages() {
62
+ return this.keyUsages;
63
+ }
64
+ }
65
+
66
+ export class KeyObject {
67
+ handle: KeyObjectHandle;
68
+ type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
69
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
70
+ export(_options?: EncodingOptions): ArrayBuffer {
71
+ return new ArrayBuffer(0);
72
+ }
73
+
74
+ constructor(type: string, handle: KeyObjectHandle) {
75
+ if (type !== 'secret' && type !== 'public' && type !== 'private')
76
+ throw new Error(`invalid KeyObject type: ${type}`);
77
+ this.handle = handle;
78
+ this.type = type;
79
+ }
80
+
81
+ // get type(): string {
82
+ // return this.type;
83
+ // }
84
+
85
+ // static from(key) {
86
+ // if (!isCryptoKey(key))
87
+ // throw new ERR_INVALID_ARG_TYPE('key', 'CryptoKey', key);
88
+ // return key[kKeyObject];
89
+ // }
90
+
91
+ // equals(otherKeyObject) {
92
+ // if (!isKeyObject(otherKeyObject)) {
93
+ // throw new ERR_INVALID_ARG_TYPE(
94
+ // 'otherKeyObject',
95
+ // 'KeyObject',
96
+ // otherKeyObject
97
+ // );
98
+ // }
99
+
100
+ // return (
101
+ // otherKeyObject.type === this.type &&
102
+ // this[kHandle].equals(otherKeyObject[kHandle])
103
+ // );
104
+ // }
105
+ }
106
+
107
+ export class SecretKeyObject extends KeyObject {
108
+ constructor(handle: KeyObjectHandle) {
109
+ super('secret', handle);
110
+ }
111
+
112
+ // get symmetricKeySize() {
113
+ // return this[kHandle].getSymmetricKeySize();
114
+ // }
115
+
116
+ export(options?: EncodingOptions) {
117
+ if (options !== undefined) {
118
+ if (options.format === 'jwk') {
119
+ throw new Error('SecretKey export for jwk is not implemented');
120
+ // return this.handle.exportJwk({}, false);
121
+ }
122
+ }
123
+ return this.handle.exportKey();
124
+ }
125
+ }
126
+
127
+ // const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
128
+ // const kAsymmetricKeyDetails = Symbol('kAsymmetricKeyDetails');
129
+
130
+ // function normalizeKeyDetails(details = {}) {
131
+ // if (details.publicExponent !== undefined) {
132
+ // return {
133
+ // ...details,
134
+ // publicExponent: bigIntArrayToUnsignedBigInt(
135
+ // new Uint8Array(details.publicExponent)
136
+ // ),
137
+ // };
138
+ // }
139
+ // return details;
140
+ // }
141
+
142
+ export class AsymmetricKeyObject extends KeyObject {
143
+ constructor(type: string, handle: KeyObjectHandle) {
144
+ super(type, handle);
145
+ }
146
+
147
+ private _asymmetricKeyType?: AsymmetricKeyType;
148
+
149
+ get asymmetricKeyType(): AsymmetricKeyType {
150
+ if (!this._asymmetricKeyType) {
151
+ this._asymmetricKeyType = this.handle.getAsymmetricKeyType();
152
+ }
153
+ return this._asymmetricKeyType;
154
+ }
155
+
156
+ // get asymmetricKeyDetails() {
157
+ // switch (this._asymmetricKeyType) {
158
+ // case 'rsa':
159
+ // case 'rsa-pss':
160
+ // case 'dsa':
161
+ // case 'ec':
162
+ // return (
163
+ // this[kAsymmetricKeyDetails] ||
164
+ // (this[kAsymmetricKeyDetails] = normalizeKeyDetails(
165
+ // this[kHandle].keyDetail({})
166
+ // ))
167
+ // );
168
+ // default:
169
+ // return {};
170
+ // }
171
+ // }
172
+ }
173
+
174
+ export class PublicKeyObject extends AsymmetricKeyObject {
175
+ constructor(handle: KeyObjectHandle) {
176
+ super('public', handle);
177
+ }
178
+
179
+ export(options: EncodingOptions) {
180
+ if (options?.format === 'jwk') {
181
+ throw new Error('PublicKey export for jwk is not implemented');
182
+ // return this.handle.exportJwk({}, false);
183
+ }
184
+ const { format, type } = parsePublicKeyEncoding(
185
+ options,
186
+ this.asymmetricKeyType,
187
+ );
188
+ return this.handle.exportKey(format, type);
189
+ }
190
+ }
191
+
192
+ export class PrivateKeyObject extends AsymmetricKeyObject {
193
+ constructor(handle: KeyObjectHandle) {
194
+ super('private', handle);
195
+ }
196
+
197
+ export(options: EncodingOptions) {
198
+ if (options?.format === 'jwk') {
199
+ if (options.passphrase !== undefined) {
200
+ throw new Error('jwk does not support encryption');
201
+ }
202
+ throw new Error('PrivateKey export for jwk is not implemented');
203
+ // return this.handle.exportJwk({}, false);
204
+ }
205
+ const { format, type, cipher, passphrase } = parsePrivateKeyEncoding(
206
+ options,
207
+ this.asymmetricKeyType,
208
+ );
209
+ return this.handle.exportKey(format, type, cipher, passphrase);
210
+ }
211
+ }
@@ -0,0 +1,146 @@
1
+ // import { ed25519 } from '../ed25519';
2
+ // import {
3
+ // kEmptyObject,
4
+ // validateFunction,
5
+ // type CryptoKeyPair,
6
+ // type GenerateKeyPairCallback,
7
+ // type GenerateKeyPairOptions,
8
+ // type GenerateKeyPairPromiseReturn,
9
+ // type GenerateKeyPairReturn,
10
+ // type KeyPairGenConfig,
11
+ // type KeyPairType,
12
+ // } from '../utils';
13
+ // import { parsePrivateKeyEncoding, parsePublicKeyEncoding } from './utils';
14
+
15
+ // export const generateKeyPair = (
16
+ // type: KeyPairType,
17
+ // options: GenerateKeyPairOptions,
18
+ // callback: GenerateKeyPairCallback,
19
+ // ): void => {
20
+ // validateFunction(callback);
21
+ // internalGenerateKeyPair(true, type, options, callback);
22
+ // };
23
+
24
+ // // Promisify generateKeyPair
25
+ // // (attempted to use util.promisify, to no avail)
26
+ // export const generateKeyPairPromise = (
27
+ // type: KeyPairType,
28
+ // options: GenerateKeyPairOptions,
29
+ // ): Promise<GenerateKeyPairPromiseReturn> => {
30
+ // return new Promise((resolve, reject) => {
31
+ // generateKeyPair(type, options, (err, publicKey, privateKey) => {
32
+ // if (err) {
33
+ // reject([err, undefined]);
34
+ // } else {
35
+ // resolve([undefined, { publicKey, privateKey }]);
36
+ // }
37
+ // });
38
+ // });
39
+ // };
40
+
41
+ // // generateKeyPairSync
42
+ // export function generateKeyPairSync(type: KeyPairType): CryptoKeyPair;
43
+ // export function generateKeyPairSync(
44
+ // type: KeyPairType,
45
+ // options: GenerateKeyPairOptions,
46
+ // ): CryptoKeyPair;
47
+ // export function generateKeyPairSync(
48
+ // type: KeyPairType,
49
+ // options?: GenerateKeyPairOptions,
50
+ // ): CryptoKeyPair {
51
+ // const [err, publicKey, privateKey] = internalGenerateKeyPair(
52
+ // false,
53
+ // type,
54
+ // options,
55
+ // undefined,
56
+ // )!;
57
+
58
+ // if (err) {
59
+ // throw err;
60
+ // }
61
+
62
+ // return {
63
+ // publicKey,
64
+ // privateKey,
65
+ // };
66
+ // }
67
+
68
+ // function parseKeyPairEncoding(
69
+ // keyType: string,
70
+ // options: GenerateKeyPairOptions = kEmptyObject,
71
+ // ): KeyPairGenConfig {
72
+ // const { publicKeyEncoding, privateKeyEncoding } = options;
73
+
74
+ // let publicFormat, publicType;
75
+ // if (publicKeyEncoding == null) {
76
+ // publicFormat = publicType = -1;
77
+ // } else if (typeof publicKeyEncoding === 'object') {
78
+ // ({ format: publicFormat, type: publicType } = parsePublicKeyEncoding(
79
+ // publicKeyEncoding,
80
+ // keyType,
81
+ // 'publicKeyEncoding',
82
+ // ));
83
+ // } else {
84
+ // throw new Error(
85
+ // 'Invalid argument options.publicKeyEncoding',
86
+ // publicKeyEncoding,
87
+ // );
88
+ // }
89
+
90
+ // let privateFormat, privateType, cipher, passphrase;
91
+ // if (privateKeyEncoding == null) {
92
+ // privateFormat = privateType = -1;
93
+ // } else if (typeof privateKeyEncoding === 'object') {
94
+ // ({
95
+ // format: privateFormat,
96
+ // type: privateType,
97
+ // cipher,
98
+ // passphrase,
99
+ // } = parsePrivateKeyEncoding(
100
+ // privateKeyEncoding,
101
+ // keyType,
102
+ // 'privateKeyEncoding',
103
+ // ));
104
+ // } else {
105
+ // throw new Error(
106
+ // 'Invalid argument options.privateKeyEncoding',
107
+ // publicKeyEncoding as ErrorOptions,
108
+ // );
109
+ // }
110
+
111
+ // return {
112
+ // publicFormat,
113
+ // publicType,
114
+ // privateFormat,
115
+ // privateType,
116
+ // cipher,
117
+ // passphrase,
118
+ // };
119
+ // }
120
+
121
+ // function internalGenerateKeyPair(
122
+ // isAsync: boolean,
123
+ // type: KeyPairType,
124
+ // options: GenerateKeyPairOptions | undefined,
125
+ // callback: GenerateKeyPairCallback | undefined,
126
+ // ): GenerateKeyPairReturn | void {
127
+ // const encoding = parseKeyPairEncoding(type, options);
128
+
129
+ // switch (type) {
130
+ // case 'ed25519':
131
+ // case 'ed448':
132
+ // case 'x25519':
133
+ // case 'x448': {
134
+ // return ed25519.utils.generateKeyPair(isAsync, type, encoding, callback);
135
+ // }
136
+ // default:
137
+ // // Fall through
138
+ // }
139
+
140
+ // const err = new Error(`
141
+ // Invalid Argument options: '${type}' scheme not supported for
142
+ // generateKeyPair(). Currently not all encryption methods are supported in
143
+ // this library. Check docs/implementation_coverage.md for status.
144
+ // `);
145
+ // return [err, undefined, undefined];
146
+ // }
@@ -0,0 +1,42 @@
1
+ import {
2
+ CryptoKey,
3
+ KeyObject,
4
+ SecretKeyObject,
5
+ PublicKeyObject,
6
+ PrivateKeyObject,
7
+ } from './classes';
8
+ // import { generateKeyPair } from './generateKeyPair';
9
+ // import { sign, verify } from './signVerify';
10
+ import {
11
+ isCryptoKey,
12
+ parseKeyEncoding,
13
+ parsePrivateKeyEncoding,
14
+ parsePublicKeyEncoding,
15
+ } from './utils';
16
+
17
+ export {
18
+ // Node Public API
19
+ // createSecretKey,
20
+ // createPublicKey,
21
+ // createPrivateKey,
22
+ CryptoKey,
23
+ // generateKeyPair,
24
+ KeyObject,
25
+ // InternalCryptoKey,
26
+ // sign,
27
+ // verify,
28
+
29
+ // Node Internal API
30
+ parsePublicKeyEncoding,
31
+ parsePrivateKeyEncoding,
32
+ parseKeyEncoding,
33
+ // preparePrivateKey,
34
+ // preparePublicOrPrivateKey,
35
+ // prepareSecretKey,
36
+ SecretKeyObject,
37
+ PublicKeyObject,
38
+ PrivateKeyObject,
39
+ // isKeyObject,
40
+ isCryptoKey,
41
+ // importGenericSecretKey,
42
+ };
@@ -0,0 +1,39 @@
1
+ // import { KeyObject, PublicKeyObject, PrivateKeyObject } from '.';
2
+ // import { ed25519 } from '../ed25519';
3
+ // import type {
4
+ // BinaryLike,
5
+ // BinaryLikeNode,
6
+ // SignCallback,
7
+ // VerifyCallback,
8
+ // } from '../utils';
9
+
10
+ // export function sign(
11
+ // algorithm: string | null | undefined,
12
+ // data: BinaryLike,
13
+ // key: BinaryLikeNode | KeyObject,
14
+ // callback: SignCallback,
15
+ // ): ArrayBuffer {
16
+ // console.log('sign ', algorithm, data, key, callback);
17
+ // return new ArrayBuffer(32);
18
+ // }
19
+
20
+ // export function verify(
21
+ // algorithm: string | null | undefined,
22
+ // data: BinaryLike,
23
+ // key: BinaryLikeNode | KeyObject,
24
+ // signature: ArrayBuffer,
25
+ // callback: VerifyCallback,
26
+ // ): boolean {
27
+ // if (!algorithm) {
28
+ // if (key instanceof PublicKeyObject) {
29
+ // switch (key.asymmetricKeyType) {
30
+ // case 'ed25519':
31
+ // case 'ed448':
32
+ // case 'x25519':
33
+ // case 'x448': {
34
+ // return ed25519.verify(signature, data, key);
35
+ // }
36
+ // }
37
+ // }
38
+ // throw new Error('Verify not implemented', algorithm, data, key, signature, callback);
39
+ // }
@@ -0,0 +1,184 @@
1
+ import {
2
+ binaryLikeToArrayBuffer,
3
+ isStringOrBuffer,
4
+ KeyEncoding,
5
+ KFormatType,
6
+ } from '../utils';
7
+ import type { EncodingOptions } from '../utils';
8
+
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ export const isCryptoKey = (obj: any): boolean => {
11
+ return obj !== null && obj?.keyObject !== undefined;
12
+ };
13
+
14
+ /**
15
+ * Parses the public key encoding based on an object. keyType must be undefined
16
+ * when this is used to parse an input encoding and must be a valid key type if
17
+ * used to parse an output encoding.
18
+ */
19
+ export function parsePublicKeyEncoding(
20
+ enc: EncodingOptions,
21
+ keyType: string | undefined,
22
+ objName?: string,
23
+ ) {
24
+ return parseKeyEncoding(enc, keyType, keyType ? true : undefined, objName);
25
+ }
26
+
27
+ /**
28
+ * Parses the private key encoding based on an object. keyType must be undefined
29
+ * when this is used to parse an input encoding and must be a valid key type if
30
+ * used to parse an output encoding.
31
+ */
32
+ export function parsePrivateKeyEncoding(
33
+ enc: EncodingOptions,
34
+ keyType: string | undefined,
35
+ objName?: string,
36
+ ) {
37
+ return parseKeyEncoding(enc, keyType, false, objName);
38
+ }
39
+
40
+ export function parseKeyEncoding(
41
+ enc: EncodingOptions,
42
+ keyType?: string,
43
+ isPublic?: boolean,
44
+ objName?: string,
45
+ ) {
46
+ // validateObject(enc, 'options');
47
+
48
+ const isInput = keyType === undefined;
49
+
50
+ const { format, type } = parseKeyFormatAndType(
51
+ enc,
52
+ keyType,
53
+ isPublic,
54
+ objName,
55
+ );
56
+
57
+ let cipher, passphrase, encoding;
58
+ if (isPublic !== true) {
59
+ ({ cipher, passphrase, encoding } = enc);
60
+
61
+ if (!isInput) {
62
+ if (cipher != null) {
63
+ if (typeof cipher !== 'string')
64
+ throw new Error(
65
+ `Invalid argument ${option('cipher', objName)}: ${cipher}`,
66
+ );
67
+ if (
68
+ format === KFormatType.kKeyFormatDER &&
69
+ (type === KeyEncoding.kKeyEncodingPKCS1 ||
70
+ type === KeyEncoding.kKeyEncodingSEC1)
71
+ ) {
72
+ throw new Error(
73
+ `Incompatible key options ${encodingNames[type]} does not support encryption`,
74
+ );
75
+ }
76
+ } else if (passphrase !== undefined) {
77
+ throw new Error(
78
+ `invalid argument ${option('cipher', objName)}: ${cipher}`,
79
+ );
80
+ }
81
+ }
82
+
83
+ if (
84
+ (isInput && passphrase !== undefined && !isStringOrBuffer(passphrase)) ||
85
+ (!isInput && cipher != null && !isStringOrBuffer(passphrase))
86
+ ) {
87
+ throw new Error(
88
+ `Invalid argument value ${option('passphrase', objName)}: ${passphrase}`,
89
+ );
90
+ }
91
+ }
92
+
93
+ if (passphrase !== undefined)
94
+ passphrase = binaryLikeToArrayBuffer(passphrase, encoding);
95
+
96
+ return { format, type, cipher, passphrase };
97
+ }
98
+
99
+ const encodingNames = {
100
+ [KeyEncoding.kKeyEncodingPKCS1]: 'pkcs1',
101
+ [KeyEncoding.kKeyEncodingPKCS8]: 'pkcs8',
102
+ [KeyEncoding.kKeyEncodingSPKI]: 'spki',
103
+ [KeyEncoding.kKeyEncodingSEC1]: 'sec1',
104
+ };
105
+
106
+ function option(name: string, objName?: string) {
107
+ return objName === undefined
108
+ ? `options.${name}`
109
+ : `options.${objName}.${name}`;
110
+ }
111
+
112
+ function parseKeyFormat(
113
+ formatStr?: string,
114
+ defaultFormat?: KFormatType,
115
+ optionName?: string,
116
+ ) {
117
+ if (formatStr === undefined && defaultFormat !== undefined)
118
+ return defaultFormat;
119
+ else if (formatStr === 'pem') return KFormatType.kKeyFormatPEM;
120
+ else if (formatStr === 'der') return KFormatType.kKeyFormatDER;
121
+ else if (formatStr === 'jwk') return KFormatType.kKeyFormatJWK;
122
+ throw new Error(`Invalid key format str: ${optionName}`);
123
+ }
124
+
125
+ function parseKeyType(
126
+ typeStr: string | undefined,
127
+ required: boolean,
128
+ keyType: string | undefined,
129
+ isPublic: boolean | undefined,
130
+ optionName: string,
131
+ ): KeyEncoding | undefined {
132
+ if (typeStr === undefined && !required) {
133
+ return undefined;
134
+ } else if (typeStr === 'pkcs1') {
135
+ if (keyType !== undefined && keyType !== 'rsa') {
136
+ throw new Error(
137
+ `Crypto incompatible key options: ${typeStr} can only be used for RSA keys`,
138
+ );
139
+ }
140
+ return KeyEncoding.kKeyEncodingPKCS1;
141
+ } else if (typeStr === 'spki' && isPublic !== false) {
142
+ return KeyEncoding.kKeyEncodingSPKI;
143
+ } else if (typeStr === 'pkcs8' && isPublic !== true) {
144
+ return KeyEncoding.kKeyEncodingPKCS8;
145
+ } else if (typeStr === 'sec1' && isPublic !== true) {
146
+ if (keyType !== undefined && keyType !== 'ec') {
147
+ throw new Error(
148
+ `Incompatible key options ${typeStr} can only be used for EC keys`,
149
+ );
150
+ }
151
+ return KeyEncoding.kKeyEncodingSEC1;
152
+ }
153
+
154
+ throw new Error(`Invalid option ${optionName} - ${typeStr}`);
155
+ }
156
+
157
+ function parseKeyFormatAndType(
158
+ enc: EncodingOptions,
159
+ keyType?: string,
160
+ isPublic?: boolean,
161
+ objName?: string,
162
+ ) {
163
+ const { format: formatStr, type: typeStr } = enc;
164
+
165
+ const isInput = keyType === undefined;
166
+ const format = parseKeyFormat(
167
+ formatStr,
168
+ isInput ? KFormatType.kKeyFormatPEM : undefined,
169
+ option('format', objName),
170
+ );
171
+
172
+ const isRequired =
173
+ (!isInput || format === KFormatType.kKeyFormatDER) &&
174
+ format !== KFormatType.kKeyFormatJWK;
175
+
176
+ const type = parseKeyType(
177
+ typeStr,
178
+ isRequired,
179
+ keyType,
180
+ isPublic,
181
+ option('type', objName),
182
+ );
183
+ return { format, type };
184
+ }