asherah 1.0.3

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.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ Asherah envelope encryption and key rotation library
2
+
3
+ This is a wrapper of the Asherah Go implementation using the Cobhan FFI library
4
+
5
+ Example code:
6
+
7
+
8
+ ```
9
+ import asherah from 'asherah'
10
+
11
+ asherah.setup('static', 'memory', null, null, null, null, null, 'TestService', 'TestProduct', null, true, true)
12
+
13
+ var data = Buffer.from('mysecretdata', 'utf8');
14
+
15
+ var encrypted = asherah.encrypt('partition', data);
16
+ console.log(encrypted);
17
+
18
+ var decrypted = asherah.decrypt('partition', encrypted);
19
+
20
+ console.log("Decrypted: " + decrypted.toString('utf8'));
21
+
22
+ ```
Binary file
Binary file
Binary file
Binary file
package/index.js ADDED
@@ -0,0 +1,132 @@
1
+ import cobhan from 'cobhan'
2
+
3
+ // KeyMeta contains the ID and Created timestamp for an encryption key.
4
+
5
+ /**
6
+ * @typedef {Object} KeyMeta
7
+ * @property {string} ID
8
+ * @property {number} Created
9
+ */
10
+
11
+ // DataRowRecord contains the encrypted key and provided data, as well as the information
12
+ // required to decrypt the key encryption key. This struct should be stored in your
13
+ // data persistence as it's required to decrypt data.
14
+
15
+ /**
16
+ * @typedef {Object} DataRowRecord
17
+ * @property {EnvelopeKeyRecord} Key
18
+ * @property {Buffer} Data
19
+ */
20
+
21
+ // EnvelopeKeyRecord represents an encrypted key and is the data structure used
22
+ // to persist the key in our key table. It also contains the meta data
23
+ // of the key used to encrypt it.
24
+
25
+ /**
26
+ * @typedef {Object} EnvelopeKeyRecord
27
+ * @property {number} Created
28
+ * @property {Buffer} EncryptedKey
29
+ * @property {KeyMeta} ParentKeyMeta
30
+ */
31
+
32
+ const libasherah = cobhan.load_platform_library('node_modules/asherah/binaries', 'libasherah', {
33
+ 'Encrypt': ['int32', ['pointer', 'pointer', 'pointer', 'pointer', 'pointer', 'pointer', 'pointer']],
34
+ 'Decrypt': ['int32', ['pointer', 'pointer', 'pointer', 'int64', 'pointer', 'int64', 'pointer']],
35
+ 'Setup': ['int32', ['pointer', 'pointer', 'pointer', 'pointer', 'pointer', 'pointer', 'int32', 'pointer', 'pointer', 'pointer', 'int32', 'int32' ]],
36
+ });
37
+
38
+ /**
39
+ * @param {string} kmsType
40
+ * @param {string} metastore
41
+ * @param {string} [rdbmsConnectionString]
42
+ * @param {string} [dynamoDbEndpoint]
43
+ * @param {string} [dynamoDbRegion]
44
+ * @param {string} [dynamoDbTableName]
45
+ * @param {boolean} [enableRegionSuffix]
46
+ * @param {string} serviceName
47
+ * @param {string} productId
48
+ * @param {string} [preferredRegion]
49
+ * @param {boolean} verbose
50
+ * @param {boolean} sessionCache
51
+ */
52
+ function setup(kmsType, metastore, rdbmsConnectionString, dynamoDbEndpoint, dynamoDbRegion, dynamoDbTableName, enableRegionSuffix, serviceName, productId, preferredRegion, verbose, sessionCache) {
53
+ const kmsTypeBuffer = cobhan.string_to_cbuffer(kmsType)
54
+ const metastoreBuffer = cobhan.string_to_cbuffer(metastore)
55
+ const rdbmsConnectionStringBuffer = cobhan.string_to_cbuffer(rdbmsConnectionString)
56
+ const dynamoDbEndpointBuffer = cobhan.string_to_cbuffer(dynamoDbEndpoint)
57
+ const dynamoDbRegionBuffer = cobhan.string_to_cbuffer(dynamoDbRegion)
58
+ const dynamoDbTableNameBuffer = cobhan.string_to_cbuffer(dynamoDbTableName)
59
+ const enableRegionSuffixInt = enableRegionSuffix ? 1 : 0
60
+ const serviceNameBuffer = cobhan.string_to_cbuffer(serviceName)
61
+ const productIdBuffer = cobhan.string_to_cbuffer(productId)
62
+ const preferredRegionBuffer = cobhan.string_to_cbuffer(preferredRegion)
63
+ const verboseInt = verbose ? 1 : 0
64
+ const sessionCacheInt = sessionCache ? 1 : 0
65
+
66
+ const result = libasherah.Setup(kmsTypeBuffer, metastoreBuffer, rdbmsConnectionStringBuffer, dynamoDbEndpointBuffer, dynamoDbRegionBuffer, dynamoDbTableNameBuffer, enableRegionSuffixInt, serviceNameBuffer, productIdBuffer, preferredRegionBuffer, verboseInt, sessionCacheInt);
67
+ if (result < 0) {
68
+ throw new Error('setup failed: ' + result);
69
+ }
70
+ }
71
+
72
+ /**
73
+ * @param {string} partitionId
74
+ * @param {DataRowRecord} dataRowRecord
75
+ * @return {Buffer}
76
+ */
77
+ function decrypt(partitionId, dataRowRecord) {
78
+ const partitionIdBuffer = cobhan.string_to_cbuffer(partitionId);
79
+ const encryptedDataBuffer = cobhan.buffer_to_cbuffer(dataRowRecord['Data']);
80
+ const encryptedKeyBuffer = cobhan.buffer_to_cbuffer(dataRowRecord['Key']['EncryptedKey']);
81
+ const created = dataRowRecord['Key']['Created'];
82
+ const parentKeyIdBuffer = cobhan.string_to_cbuffer(dataRowRecord['Key']['ParentKeyMeta']['ID']);
83
+ const parentKeyCreated = dataRowRecord['Key']['ParentKeyMeta']['Created'];
84
+
85
+ const outputDataBuffer = cobhan.allocate_cbuffer(encryptedDataBuffer.length + 256);
86
+
87
+ const result = libasherah.Decrypt(partitionIdBuffer, encryptedDataBuffer, encryptedKeyBuffer, created, parentKeyIdBuffer, parentKeyCreated, outputDataBuffer);
88
+ if (result < 0) {
89
+ throw new Error('decrypt failed: ' + result);
90
+ }
91
+
92
+ return cobhan.cbuffer_to_buffer(outputDataBuffer);
93
+ }
94
+
95
+ /**
96
+ * @param {string} partitionId
97
+ * @param {Buffer} data
98
+ * @return {DataRowRecord}
99
+ */
100
+ function encrypt(partitionId, data) {
101
+ const partitionIdBuffer = cobhan.string_to_cbuffer(partitionId);
102
+ const dataBuffer = cobhan.buffer_to_cbuffer(data);
103
+ const outputEncryptedDataBuffer = cobhan.allocate_cbuffer(data.length + 256);
104
+ const outputEncryptedKeyBuffer = cobhan.allocate_cbuffer(256);
105
+ const outputCreatedBuffer = cobhan.int64_to_buffer(0);
106
+ const outputParentKeyIdBuffer = cobhan.allocate_cbuffer(256);
107
+ const outputParentKeyCreatedBuffer = cobhan.int64_to_buffer(0);
108
+
109
+ const result = libasherah.Encrypt(partitionIdBuffer, dataBuffer, outputEncryptedDataBuffer, outputEncryptedKeyBuffer,
110
+ outputCreatedBuffer, outputParentKeyIdBuffer, outputParentKeyCreatedBuffer);
111
+
112
+ if (result < 0) {
113
+ throw new Error('encrypt failed: ' + result);
114
+ }
115
+ const parentKeyId = cobhan.cbuffer_to_string(outputParentKeyIdBuffer);
116
+ console.log("Encrypt returned parent key ID: " + parentKeyId);
117
+ const dataRowRecord = {
118
+ Data: cobhan.cbuffer_to_buffer(outputEncryptedDataBuffer),
119
+ Key: {
120
+ EncryptedKey: cobhan.cbuffer_to_buffer(outputEncryptedKeyBuffer),
121
+ Created: cobhan.buffer_to_int64(outputCreatedBuffer),
122
+ ParentKeyMeta: {
123
+ ID: parentKeyId,
124
+ Created: cobhan.buffer_to_int64(outputParentKeyCreatedBuffer)
125
+ }
126
+ }
127
+ };
128
+
129
+ return dataRowRecord;
130
+ }
131
+
132
+ export default { encrypt, decrypt, setup };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "asherah",
3
+ "version": "1.0.3",
4
+ "description": "Asherah envelope encryption and key rotation library",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/jgowdy/asherah-cobhan.git"
10
+ },
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 1"
13
+ },
14
+ "keywords": [],
15
+ "author": "Jeremiah Gowdy <jeremiah@gowdy.me>",
16
+ "license": "ISC",
17
+ "files": [
18
+ "binaries/*.so",
19
+ "binaries/*.dylib"
20
+ ],
21
+ "dependencies": {
22
+ "cobhan": "^1.0.6"
23
+ }
24
+ }