jstink 1.0.0

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.
@@ -0,0 +1,31 @@
1
+ # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ push:
8
+ branches: [ "main" ]
9
+ pull_request:
10
+ branches: [ "main" ]
11
+
12
+ jobs:
13
+ build:
14
+
15
+ runs-on: ubuntu-latest
16
+
17
+ strategy:
18
+ matrix:
19
+ node-version: [14.x, 16.x, 18.x]
20
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
+
22
+ steps:
23
+ - uses: actions/checkout@v3
24
+ - name: Use Node.js ${{ matrix.node-version }}
25
+ uses: actions/setup-node@v3
26
+ with:
27
+ node-version: ${{ matrix.node-version }}
28
+ cache: 'npm'
29
+ - run: npm ci
30
+ - run: npm run build --if-present
31
+ - run: npm test
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 andycaine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # jstink
2
+
3
+ A simple, easy-to-use Javascript crytopgraphy library.
4
+
5
+ ## Description
6
+
7
+ Google's excellent [Tink cryptographic library](https://developers.google.com/tink/what-is) exists to help developers without cryptographic backgrounds safely implement common cryptographic tasks.
8
+
9
+ Unfortunately for Javascript developers, [there is no production-ready Javascript version](https://github.com/tink-crypto/tink/issues/689) available.
10
+
11
+ jstink was built to fill this gap. It provides a very simple API that is hard to misuse, and is compatible with Tink ciphertexts and with [Tinkey](https://developers.google.com/tink/tinkey-overview) encrypted keysets.
12
+
13
+ jstink currently only supports the Authenticated Encryption with Associated Data (AEAD) encryption primitive with AES256_GCM key types envelope encrypted with AWS KMS keys.
14
+
15
+ ## Getting Started
16
+
17
+ Let's walk through setting up a simple project that can encrypt and decrypt data using an AES256_GCM key that has been encrypted with a master key stored in AWS KMS. These steps assume you have Node.js and npm already installed.
18
+
19
+ 1. Create a new Node.js project
20
+
21
+ 2. Inside of the project, install jstink using npm:
22
+ ```
23
+ npm install jstink
24
+ ```
25
+
26
+ 3. Create a new symmetric key in AWS KMS that will be used to encrypt our Tinkey data encryption key. This step assumes you have the [AWS CLI](https://aws.amazon.com/cli/) installed and configured:
27
+ ```
28
+ aws kms create-key --description "Key for jstink encryption"
29
+ ```
30
+ Make a note of the key ARN as you'll need it later.
31
+
32
+ 4. [Install Tinkey](https://developers.google.com/tink/install-tinkey).
33
+
34
+ 5. Create a new Tinkey key, envelope encrypted with your newly created AWS KMS key:
35
+ ```
36
+ tinkey create-keyset --key-template AES256_GCM --out keyset.json --master-key-uri aws-kms://${MASTER_KEY_ARN}
37
+ ```
38
+ Because this key is envelope encrypted with the AWS KMS key you can store it with the data or with the application.
39
+
40
+ 6. Now you can encrypt and decrypt data:
41
+ ```javascript
42
+ const { Aead } = require('jstink');
43
+ const fs = require('fs');
44
+
45
+ const keyset = JSON.parse(fs.readFileSync('./keyset.json'));
46
+ const aead = new Aead(keyset);
47
+
48
+ const ciphertext = await aead.encrypt('Hello World!', 'associatedData');
49
+ const plaintext = await aead.decrypt(ciphertext, 'associatedData');
50
+ ```
51
+
52
+ 7. At some point, as determined by your cryptoperiod, you'll want to rotate your keys. Tinkey makes this nice and easy. First, create a new key:
53
+
54
+ ```
55
+ tinkey add-key --key-template AES256_GCM --in keyset.json --out keysetv2.json --master-key-uri aws-kms://${MASTER_KEY_ARN}
56
+ ```
57
+
58
+ Once you've deployed this keyset, you can make it the default for encryption:
59
+
60
+ ```
61
+ tinkey promote-key --key-id <new-key-id> --key-template AES256_GCM --in keysetv2.json --out keysetv3.json --master-key-uri aws-kms://${MASTER_KEY_ARN}
62
+ ```
63
+
64
+ Decrypt operations will still use the key that was used for encryption (the encryption key ID is stored as part of the Tink wire format). To completely remove the old key (e.g. in the event of a compromise) you'll need to run a process to re-encrypt all data encrypted with the old key, then you can delete the old key:
65
+
66
+ ```
67
+ tinkey delete-key --key-template AES256_GCM --in keysetv3.json --out keysetv4.json --master-key-uri aws-kms://${MASTER_KEY_ARN}
68
+ ```
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "jstink",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "jest",
8
+ "prepublish": "npm test"
9
+ },
10
+ "author": "Andy Caine",
11
+ "license": "MIT",
12
+ "devDependencies": {
13
+ "jest": "^29.7.0"
14
+ },
15
+ "jest": {
16
+ "testEnvironment": "node"
17
+ },
18
+ "dependencies": {
19
+ "@aws-sdk/client-kms": "^3.555.0",
20
+ "google-protobuf": "^3.21.2"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/andycaine/jstink"
25
+ }
26
+ }
package/src/aes_gcm.js ADDED
@@ -0,0 +1,30 @@
1
+ const crypto = require('crypto');
2
+
3
+ const algorithm = 'aes-256-gcm';
4
+
5
+ function encrypt(plaintext, associatedData, key) {
6
+ const iv = crypto.randomBytes(12);
7
+ const cipher = crypto.createCipheriv(algorithm, key, iv);
8
+
9
+ cipher.setAAD(Buffer.from(associatedData));
10
+ const encrypted = cipher.update(plaintext);
11
+ const remaining = cipher.final();
12
+ const tag = cipher.getAuthTag();
13
+ return {
14
+ ciphertext: Buffer.concat([encrypted, remaining]),
15
+ tag,
16
+ iv,
17
+ tag
18
+ };
19
+ }
20
+
21
+ function decrypt(ciphertext, associatedData, key, iv, tag) {
22
+ const decipher = crypto.createDecipheriv(algorithm, key, iv);
23
+ decipher.setAAD(Buffer.from(associatedData));
24
+ decipher.setAuthTag(Buffer.from(tag));
25
+ let decrypted = decipher.update(ciphertext);
26
+ decrypted += decipher.final();
27
+ return decrypted;
28
+ }
29
+
30
+ module.exports = { encrypt, decrypt };
@@ -0,0 +1,67 @@
1
+ // Copyright 2017 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ ////////////////////////////////////////////////////////////////////////////////
16
+
17
+ syntax = "proto3";
18
+
19
+ package google.crypto.tink;
20
+
21
+ option java_package = "com.google.crypto.tink.proto";
22
+ option java_multiple_files = true;
23
+ option go_package = "github.com/tink-crypto/tink-go/v2/proto/aes_gcm_go_proto";
24
+ option objc_class_prefix = "TINKPB";
25
+
26
+ message AesGcmKeyFormat {
27
+ uint32 key_size = 2;
28
+ uint32 version = 3;
29
+ }
30
+
31
+ // key_type: type.googleapis.com/google.crypto.tink.AesGcmKey
32
+ //
33
+ // A AesGcmKey is an AEAD key. Mathematically, it represents the functions
34
+ // Encrypt and Decrypt which we define in the following.
35
+ //
36
+ // First, Tink computes a "output prefix" OP by considering the
37
+ // "OutputPrefixType" message in Keyset.Key and the ID of the key using the
38
+ // Tink function "AEAD-OutputPrefix": (AesGcmKeys must always be stored in a
39
+ // keyset).
40
+ //
41
+ // AEAD-OutputPrefix(output_prefix_type, id):
42
+ // if output_prefix_type == RAW:
43
+ // return "";
44
+ // if output_prefix_type == TINK:
45
+ // return 0x01 + BigEndian(id)
46
+ // if output_prefix_type == CRUNCHY:
47
+ // return 0x00 + BigEndian(id)
48
+ //
49
+ // Then, the function defined by this is defined as:
50
+ // [GCM], Section 5.2.1:
51
+ // * "Encrypt" maps a plaintext P and associated data A to a ciphertext given
52
+ // by the concatenation OP || IV || C || T. In addition to [GCM], Tink
53
+ // has the following restriction: IV is a uniformly random initialization
54
+ // vector of length 12 bytes and T is restricted to 16 bytes.
55
+ //
56
+ // * If OP matches the result of AEAD-OutputPrefix, then "Decrypt" maps the
57
+ // input OP || IV || C || T and A to the the output P in the manner as
58
+ // described in [GCM], Section 5.2.2. If OP does not match, then "Decrypt"
59
+ // returns an error.
60
+ // [GCM]: NIST Special Publication 800-38D: Recommendation for Block Cipher
61
+ // Modes of Operation: Galois/Counter Mode (GCM) and GMAC.
62
+ // http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf.
63
+
64
+ message AesGcmKey {
65
+ uint32 version = 1;
66
+ bytes key_value = 3;
67
+ }
@@ -0,0 +1,412 @@
1
+ // source: aes_gcm.proto
2
+ /**
3
+ * @fileoverview
4
+ * @enhanceable
5
+ * @suppress {missingRequire} reports error on implicit type usages.
6
+ * @suppress {messageConventions} JS Compiler reports an error if a variable or
7
+ * field starts with 'MSG_' and isn't a translatable message.
8
+ * @public
9
+ */
10
+ // GENERATED CODE -- DO NOT EDIT!
11
+ /* eslint-disable */
12
+ // @ts-nocheck
13
+
14
+ var jspb = require('google-protobuf');
15
+ var goog = jspb;
16
+ var global =
17
+ (typeof globalThis !== 'undefined' && globalThis) ||
18
+ (typeof window !== 'undefined' && window) ||
19
+ (typeof global !== 'undefined' && global) ||
20
+ (typeof self !== 'undefined' && self) ||
21
+ (function () { return this; }).call(null) ||
22
+ Function('return this')();
23
+
24
+ goog.exportSymbol('proto.google.crypto.tink.AesGcmKey', null, global);
25
+ goog.exportSymbol('proto.google.crypto.tink.AesGcmKeyFormat', null, global);
26
+ /**
27
+ * Generated by JsPbCodeGenerator.
28
+ * @param {Array=} opt_data Optional initial data array, typically from a
29
+ * server response, or constructed directly in Javascript. The array is used
30
+ * in place and becomes part of the constructed object. It is not cloned.
31
+ * If no data is provided, the constructed object will be empty, but still
32
+ * valid.
33
+ * @extends {jspb.Message}
34
+ * @constructor
35
+ */
36
+ proto.google.crypto.tink.AesGcmKeyFormat = function(opt_data) {
37
+ jspb.Message.initialize(this, opt_data, 0, -1, null, null);
38
+ };
39
+ goog.inherits(proto.google.crypto.tink.AesGcmKeyFormat, jspb.Message);
40
+ if (goog.DEBUG && !COMPILED) {
41
+ /**
42
+ * @public
43
+ * @override
44
+ */
45
+ proto.google.crypto.tink.AesGcmKeyFormat.displayName = 'proto.google.crypto.tink.AesGcmKeyFormat';
46
+ }
47
+ /**
48
+ * Generated by JsPbCodeGenerator.
49
+ * @param {Array=} opt_data Optional initial data array, typically from a
50
+ * server response, or constructed directly in Javascript. The array is used
51
+ * in place and becomes part of the constructed object. It is not cloned.
52
+ * If no data is provided, the constructed object will be empty, but still
53
+ * valid.
54
+ * @extends {jspb.Message}
55
+ * @constructor
56
+ */
57
+ proto.google.crypto.tink.AesGcmKey = function(opt_data) {
58
+ jspb.Message.initialize(this, opt_data, 0, -1, null, null);
59
+ };
60
+ goog.inherits(proto.google.crypto.tink.AesGcmKey, jspb.Message);
61
+ if (goog.DEBUG && !COMPILED) {
62
+ /**
63
+ * @public
64
+ * @override
65
+ */
66
+ proto.google.crypto.tink.AesGcmKey.displayName = 'proto.google.crypto.tink.AesGcmKey';
67
+ }
68
+
69
+
70
+
71
+ if (jspb.Message.GENERATE_TO_OBJECT) {
72
+ /**
73
+ * Creates an object representation of this proto.
74
+ * Field names that are reserved in JavaScript and will be renamed to pb_name.
75
+ * Optional fields that are not set will be set to undefined.
76
+ * To access a reserved field use, foo.pb_<name>, eg, foo.pb_default.
77
+ * For the list of reserved names please see:
78
+ * net/proto2/compiler/js/internal/generator.cc#kKeyword.
79
+ * @param {boolean=} opt_includeInstance Deprecated. whether to include the
80
+ * JSPB instance for transitional soy proto support:
81
+ * http://goto/soy-param-migration
82
+ * @return {!Object}
83
+ */
84
+ proto.google.crypto.tink.AesGcmKeyFormat.prototype.toObject = function(opt_includeInstance) {
85
+ return proto.google.crypto.tink.AesGcmKeyFormat.toObject(opt_includeInstance, this);
86
+ };
87
+
88
+
89
+ /**
90
+ * Static version of the {@see toObject} method.
91
+ * @param {boolean|undefined} includeInstance Deprecated. Whether to include
92
+ * the JSPB instance for transitional soy proto support:
93
+ * http://goto/soy-param-migration
94
+ * @param {!proto.google.crypto.tink.AesGcmKeyFormat} msg The msg instance to transform.
95
+ * @return {!Object}
96
+ * @suppress {unusedLocalVariables} f is only used for nested messages
97
+ */
98
+ proto.google.crypto.tink.AesGcmKeyFormat.toObject = function(includeInstance, msg) {
99
+ var f, obj = {
100
+ keySize: jspb.Message.getFieldWithDefault(msg, 2, 0),
101
+ version: jspb.Message.getFieldWithDefault(msg, 3, 0)
102
+ };
103
+
104
+ if (includeInstance) {
105
+ obj.$jspbMessageInstance = msg;
106
+ }
107
+ return obj;
108
+ };
109
+ }
110
+
111
+
112
+ /**
113
+ * Deserializes binary data (in protobuf wire format).
114
+ * @param {jspb.ByteSource} bytes The bytes to deserialize.
115
+ * @return {!proto.google.crypto.tink.AesGcmKeyFormat}
116
+ */
117
+ proto.google.crypto.tink.AesGcmKeyFormat.deserializeBinary = function(bytes) {
118
+ var reader = new jspb.BinaryReader(bytes);
119
+ var msg = new proto.google.crypto.tink.AesGcmKeyFormat;
120
+ return proto.google.crypto.tink.AesGcmKeyFormat.deserializeBinaryFromReader(msg, reader);
121
+ };
122
+
123
+
124
+ /**
125
+ * Deserializes binary data (in protobuf wire format) from the
126
+ * given reader into the given message object.
127
+ * @param {!proto.google.crypto.tink.AesGcmKeyFormat} msg The message object to deserialize into.
128
+ * @param {!jspb.BinaryReader} reader The BinaryReader to use.
129
+ * @return {!proto.google.crypto.tink.AesGcmKeyFormat}
130
+ */
131
+ proto.google.crypto.tink.AesGcmKeyFormat.deserializeBinaryFromReader = function(msg, reader) {
132
+ while (reader.nextField()) {
133
+ if (reader.isEndGroup()) {
134
+ break;
135
+ }
136
+ var field = reader.getFieldNumber();
137
+ switch (field) {
138
+ case 2:
139
+ var value = /** @type {number} */ (reader.readUint32());
140
+ msg.setKeySize(value);
141
+ break;
142
+ case 3:
143
+ var value = /** @type {number} */ (reader.readUint32());
144
+ msg.setVersion(value);
145
+ break;
146
+ default:
147
+ reader.skipField();
148
+ break;
149
+ }
150
+ }
151
+ return msg;
152
+ };
153
+
154
+
155
+ /**
156
+ * Serializes the message to binary data (in protobuf wire format).
157
+ * @return {!Uint8Array}
158
+ */
159
+ proto.google.crypto.tink.AesGcmKeyFormat.prototype.serializeBinary = function() {
160
+ var writer = new jspb.BinaryWriter();
161
+ proto.google.crypto.tink.AesGcmKeyFormat.serializeBinaryToWriter(this, writer);
162
+ return writer.getResultBuffer();
163
+ };
164
+
165
+
166
+ /**
167
+ * Serializes the given message to binary data (in protobuf wire
168
+ * format), writing to the given BinaryWriter.
169
+ * @param {!proto.google.crypto.tink.AesGcmKeyFormat} message
170
+ * @param {!jspb.BinaryWriter} writer
171
+ * @suppress {unusedLocalVariables} f is only used for nested messages
172
+ */
173
+ proto.google.crypto.tink.AesGcmKeyFormat.serializeBinaryToWriter = function(message, writer) {
174
+ var f = undefined;
175
+ f = message.getKeySize();
176
+ if (f !== 0) {
177
+ writer.writeUint32(
178
+ 2,
179
+ f
180
+ );
181
+ }
182
+ f = message.getVersion();
183
+ if (f !== 0) {
184
+ writer.writeUint32(
185
+ 3,
186
+ f
187
+ );
188
+ }
189
+ };
190
+
191
+
192
+ /**
193
+ * optional uint32 key_size = 2;
194
+ * @return {number}
195
+ */
196
+ proto.google.crypto.tink.AesGcmKeyFormat.prototype.getKeySize = function() {
197
+ return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 2, 0));
198
+ };
199
+
200
+
201
+ /**
202
+ * @param {number} value
203
+ * @return {!proto.google.crypto.tink.AesGcmKeyFormat} returns this
204
+ */
205
+ proto.google.crypto.tink.AesGcmKeyFormat.prototype.setKeySize = function(value) {
206
+ return jspb.Message.setProto3IntField(this, 2, value);
207
+ };
208
+
209
+
210
+ /**
211
+ * optional uint32 version = 3;
212
+ * @return {number}
213
+ */
214
+ proto.google.crypto.tink.AesGcmKeyFormat.prototype.getVersion = function() {
215
+ return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 3, 0));
216
+ };
217
+
218
+
219
+ /**
220
+ * @param {number} value
221
+ * @return {!proto.google.crypto.tink.AesGcmKeyFormat} returns this
222
+ */
223
+ proto.google.crypto.tink.AesGcmKeyFormat.prototype.setVersion = function(value) {
224
+ return jspb.Message.setProto3IntField(this, 3, value);
225
+ };
226
+
227
+
228
+
229
+
230
+
231
+ if (jspb.Message.GENERATE_TO_OBJECT) {
232
+ /**
233
+ * Creates an object representation of this proto.
234
+ * Field names that are reserved in JavaScript and will be renamed to pb_name.
235
+ * Optional fields that are not set will be set to undefined.
236
+ * To access a reserved field use, foo.pb_<name>, eg, foo.pb_default.
237
+ * For the list of reserved names please see:
238
+ * net/proto2/compiler/js/internal/generator.cc#kKeyword.
239
+ * @param {boolean=} opt_includeInstance Deprecated. whether to include the
240
+ * JSPB instance for transitional soy proto support:
241
+ * http://goto/soy-param-migration
242
+ * @return {!Object}
243
+ */
244
+ proto.google.crypto.tink.AesGcmKey.prototype.toObject = function(opt_includeInstance) {
245
+ return proto.google.crypto.tink.AesGcmKey.toObject(opt_includeInstance, this);
246
+ };
247
+
248
+
249
+ /**
250
+ * Static version of the {@see toObject} method.
251
+ * @param {boolean|undefined} includeInstance Deprecated. Whether to include
252
+ * the JSPB instance for transitional soy proto support:
253
+ * http://goto/soy-param-migration
254
+ * @param {!proto.google.crypto.tink.AesGcmKey} msg The msg instance to transform.
255
+ * @return {!Object}
256
+ * @suppress {unusedLocalVariables} f is only used for nested messages
257
+ */
258
+ proto.google.crypto.tink.AesGcmKey.toObject = function(includeInstance, msg) {
259
+ var f, obj = {
260
+ version: jspb.Message.getFieldWithDefault(msg, 1, 0),
261
+ keyValue: msg.getKeyValue_asB64()
262
+ };
263
+
264
+ if (includeInstance) {
265
+ obj.$jspbMessageInstance = msg;
266
+ }
267
+ return obj;
268
+ };
269
+ }
270
+
271
+
272
+ /**
273
+ * Deserializes binary data (in protobuf wire format).
274
+ * @param {jspb.ByteSource} bytes The bytes to deserialize.
275
+ * @return {!proto.google.crypto.tink.AesGcmKey}
276
+ */
277
+ proto.google.crypto.tink.AesGcmKey.deserializeBinary = function(bytes) {
278
+ var reader = new jspb.BinaryReader(bytes);
279
+ var msg = new proto.google.crypto.tink.AesGcmKey;
280
+ return proto.google.crypto.tink.AesGcmKey.deserializeBinaryFromReader(msg, reader);
281
+ };
282
+
283
+
284
+ /**
285
+ * Deserializes binary data (in protobuf wire format) from the
286
+ * given reader into the given message object.
287
+ * @param {!proto.google.crypto.tink.AesGcmKey} msg The message object to deserialize into.
288
+ * @param {!jspb.BinaryReader} reader The BinaryReader to use.
289
+ * @return {!proto.google.crypto.tink.AesGcmKey}
290
+ */
291
+ proto.google.crypto.tink.AesGcmKey.deserializeBinaryFromReader = function(msg, reader) {
292
+ while (reader.nextField()) {
293
+ if (reader.isEndGroup()) {
294
+ break;
295
+ }
296
+ var field = reader.getFieldNumber();
297
+ switch (field) {
298
+ case 1:
299
+ var value = /** @type {number} */ (reader.readUint32());
300
+ msg.setVersion(value);
301
+ break;
302
+ case 3:
303
+ var value = /** @type {!Uint8Array} */ (reader.readBytes());
304
+ msg.setKeyValue(value);
305
+ break;
306
+ default:
307
+ reader.skipField();
308
+ break;
309
+ }
310
+ }
311
+ return msg;
312
+ };
313
+
314
+
315
+ /**
316
+ * Serializes the message to binary data (in protobuf wire format).
317
+ * @return {!Uint8Array}
318
+ */
319
+ proto.google.crypto.tink.AesGcmKey.prototype.serializeBinary = function() {
320
+ var writer = new jspb.BinaryWriter();
321
+ proto.google.crypto.tink.AesGcmKey.serializeBinaryToWriter(this, writer);
322
+ return writer.getResultBuffer();
323
+ };
324
+
325
+
326
+ /**
327
+ * Serializes the given message to binary data (in protobuf wire
328
+ * format), writing to the given BinaryWriter.
329
+ * @param {!proto.google.crypto.tink.AesGcmKey} message
330
+ * @param {!jspb.BinaryWriter} writer
331
+ * @suppress {unusedLocalVariables} f is only used for nested messages
332
+ */
333
+ proto.google.crypto.tink.AesGcmKey.serializeBinaryToWriter = function(message, writer) {
334
+ var f = undefined;
335
+ f = message.getVersion();
336
+ if (f !== 0) {
337
+ writer.writeUint32(
338
+ 1,
339
+ f
340
+ );
341
+ }
342
+ f = message.getKeyValue_asU8();
343
+ if (f.length > 0) {
344
+ writer.writeBytes(
345
+ 3,
346
+ f
347
+ );
348
+ }
349
+ };
350
+
351
+
352
+ /**
353
+ * optional uint32 version = 1;
354
+ * @return {number}
355
+ */
356
+ proto.google.crypto.tink.AesGcmKey.prototype.getVersion = function() {
357
+ return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 1, 0));
358
+ };
359
+
360
+
361
+ /**
362
+ * @param {number} value
363
+ * @return {!proto.google.crypto.tink.AesGcmKey} returns this
364
+ */
365
+ proto.google.crypto.tink.AesGcmKey.prototype.setVersion = function(value) {
366
+ return jspb.Message.setProto3IntField(this, 1, value);
367
+ };
368
+
369
+
370
+ /**
371
+ * optional bytes key_value = 3;
372
+ * @return {!(string|Uint8Array)}
373
+ */
374
+ proto.google.crypto.tink.AesGcmKey.prototype.getKeyValue = function() {
375
+ return /** @type {!(string|Uint8Array)} */ (jspb.Message.getFieldWithDefault(this, 3, ""));
376
+ };
377
+
378
+
379
+ /**
380
+ * optional bytes key_value = 3;
381
+ * This is a type-conversion wrapper around `getKeyValue()`
382
+ * @return {string}
383
+ */
384
+ proto.google.crypto.tink.AesGcmKey.prototype.getKeyValue_asB64 = function() {
385
+ return /** @type {string} */ (jspb.Message.bytesAsB64(
386
+ this.getKeyValue()));
387
+ };
388
+
389
+
390
+ /**
391
+ * optional bytes key_value = 3;
392
+ * Note that Uint8Array is not supported on all browsers.
393
+ * @see http://caniuse.com/Uint8Array
394
+ * This is a type-conversion wrapper around `getKeyValue()`
395
+ * @return {!Uint8Array}
396
+ */
397
+ proto.google.crypto.tink.AesGcmKey.prototype.getKeyValue_asU8 = function() {
398
+ return /** @type {!Uint8Array} */ (jspb.Message.bytesAsU8(
399
+ this.getKeyValue()));
400
+ };
401
+
402
+
403
+ /**
404
+ * @param {!(string|Uint8Array)} value
405
+ * @return {!proto.google.crypto.tink.AesGcmKey} returns this
406
+ */
407
+ proto.google.crypto.tink.AesGcmKey.prototype.setKeyValue = function(value) {
408
+ return jspb.Message.setProto3BytesField(this, 3, value);
409
+ };
410
+
411
+
412
+ goog.object.extend(exports, proto.google.crypto.tink);
package/src/awskms.js ADDED
@@ -0,0 +1,14 @@
1
+ const { KMSClient, DecryptCommand } = require("@aws-sdk/client-kms");
2
+
3
+ const client = new KMSClient();
4
+
5
+ async function decrypt(data) {
6
+ const command = new DecryptCommand({
7
+ CiphertextBlob: data
8
+ });
9
+
10
+ const { Plaintext } = await client.send(command);
11
+ return Plaintext;
12
+ }
13
+
14
+ module.exports = { decrypt };