From 8ef19e788d3fda46a6c96bf8825fb898965a7b9b Mon Sep 17 00:00:00 2001 From: Jeremiah Gowdy Date: Mon, 21 Mar 2022 17:24:29 -0700 Subject: [PATCH] Optimize for performance and latest Cobhan (#19) --- package-lock.json | 34 +++++++++++++++++++++++++++------- package.json | 4 +++- scripts/verify-tag.sh | 15 --------------- src/asherah.ts | 17 ++++++++++------- 4 files changed, 40 insertions(+), 30 deletions(-) delete mode 100755 scripts/verify-tag.sh diff --git a/package-lock.json b/package-lock.json index 7671878..8b2cfbf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,13 +9,15 @@ "version": "0.0.0", "license": "MIT", "dependencies": { - "cobhan": "^1.0.31" + "cobhan": "^1.0.33", + "ref-napi": "^3.0.3" }, "devDependencies": { "@types/benchmark": "^2.1.1", "@types/chai": "^4.3.0", "@types/mocha": "^9.1.0", "@types/node": "^17.0.21", + "@types/ref-napi": "^3.0.4", "@typescript-eslint/eslint-plugin": "^5.13.0", "@typescript-eslint/parser": "^5.13.0", "benchmark": "^2.1.4", @@ -710,6 +712,15 @@ "integrity": "sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ==", "dev": true }, + "node_modules/@types/ref-napi": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/ref-napi/-/ref-napi-3.0.4.tgz", + "integrity": "sha512-ng8SCmdZbz1GHaW3qgGoX9IaHoIvgMqgBHLe3sv18NbAkHVgnjRW8fJq51VTUm4lnJyLu60q9/002o7qjOg13g==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "5.14.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.14.0.tgz", @@ -1275,9 +1286,9 @@ } }, "node_modules/cobhan": { - "version": "1.0.31", - "resolved": "https://registry.npmjs.org/cobhan/-/cobhan-1.0.31.tgz", - "integrity": "sha512-PZ4RM2vtvxEtBa+IIh9lH7fV435z4pcsXhnSbXKjN/sGCPkOXiZQOydSvFWH9rPJDnqXkP3IOR2+S/eXAju8Hw==", + "version": "1.0.33", + "resolved": "https://registry.npmjs.org/cobhan/-/cobhan-1.0.33.tgz", + "integrity": "sha512-Yox3ecfiCO45wn3Hp3gG7/G1O/C5tQZR6GW7A/4haPZtHbXmJZz8t9sFUdigjMsPSPVK/KmD+RmSeQI079MMqQ==", "dependencies": { "ffi-napi": "^4.0.3", "fs": "^0.0.1-security" @@ -4454,6 +4465,15 @@ "integrity": "sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ==", "dev": true }, + "@types/ref-napi": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/ref-napi/-/ref-napi-3.0.4.tgz", + "integrity": "sha512-ng8SCmdZbz1GHaW3qgGoX9IaHoIvgMqgBHLe3sv18NbAkHVgnjRW8fJq51VTUm4lnJyLu60q9/002o7qjOg13g==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@typescript-eslint/eslint-plugin": { "version": "5.14.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.14.0.tgz", @@ -4832,9 +4852,9 @@ } }, "cobhan": { - "version": "1.0.31", - "resolved": "https://registry.npmjs.org/cobhan/-/cobhan-1.0.31.tgz", - "integrity": "sha512-PZ4RM2vtvxEtBa+IIh9lH7fV435z4pcsXhnSbXKjN/sGCPkOXiZQOydSvFWH9rPJDnqXkP3IOR2+S/eXAju8Hw==", + "version": "1.0.33", + "resolved": "https://registry.npmjs.org/cobhan/-/cobhan-1.0.33.tgz", + "integrity": "sha512-Yox3ecfiCO45wn3Hp3gG7/G1O/C5tQZR6GW7A/4haPZtHbXmJZz8t9sFUdigjMsPSPVK/KmD+RmSeQI079MMqQ==", "requires": { "ffi-napi": "^4.0.3", "fs": "^0.0.1-security" diff --git a/package.json b/package.json index ee1dde0..f712de3 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@types/chai": "^4.3.0", "@types/mocha": "^9.1.0", "@types/node": "^17.0.21", + "@types/ref-napi": "^3.0.4", "@typescript-eslint/eslint-plugin": "^5.13.0", "@typescript-eslint/parser": "^5.13.0", "benchmark": "^2.1.4", @@ -48,6 +49,7 @@ }, "types": "dist/asherah.d.ts", "dependencies": { - "cobhan": "^1.0.31" + "cobhan": "^1.0.33", + "ref-napi": "^3.0.3" } } diff --git a/scripts/verify-tag.sh b/scripts/verify-tag.sh deleted file mode 100755 index f24d806..0000000 --- a/scripts/verify-tag.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -apt-get update -apt-get install -y jq - -CURRENT_TAG=${1} -PACKAGE_VER=$(jq -r .version package.json) -echo "Current tag is ${CURRENT_TAG}" -echo "Current package.json version is ${PACKAGE_VER}" -if [[ "${CURRENT_TAG}" == "${PACKAGE_VER}" ]]; then - exit 0 -fi - -echo "Tag / Version mismatch!" -exit 1 diff --git a/src/asherah.ts b/src/asherah.ts index 72a19e1..c995c8b 100644 --- a/src/asherah.ts +++ b/src/asherah.ts @@ -1,5 +1,6 @@ import { allocate_cbuffer, buffer_to_cbuffer, cbuffer_to_buffer, cbuffer_to_string, json_to_cbuffer, load_platform_library, string_to_cbuffer, header_size } from 'cobhan'; import fs from 'fs'; +import ref from 'ref-napi'; export type AsherahConfig = { /** The name of this service (Required) */ @@ -42,13 +43,15 @@ export type AsherahConfig = { const binaries_path = find_binaries() -const libasherah = load_platform_library(binaries_path, 'libasherah', { - 'SetupJson': ['int32', ['pointer']], - 'EncryptToJson': ['int32', ['pointer', 'pointer', 'pointer']], - 'DecryptFromJson': ['int32', ['pointer', 'pointer', 'pointer']], - 'Shutdown': ['void', []] +const libasherah: any = load_platform_library(binaries_path, 'libasherah', { + 'SetupJson': [ref.types.int32, [ref.refType(ref.types.void)]], + 'EncryptToJson': [ref.types.int32, [ref.refType(ref.types.void), ref.refType(ref.types.void), ref.refType(ref.types.void)]], + 'DecryptFromJson': [ref.types.int32, [ref.refType(ref.types.void), ref.refType(ref.types.void), ref.refType(ref.types.void)]], + 'Shutdown': [ref.types.void, []] }); +const DecryptFromJson = libasherah["DecryptFromJson"]; +const EncryptToJson = libasherah["EncryptToJson"]; const EstimatedEncryptionOverhead = 48 const EstimatedEnvelopeOverhead = 185 @@ -88,7 +91,7 @@ export function decrypt(partitionId: string, dataRowRecord: string): Buffer { const jsonBuffer = string_to_cbuffer(dataRowRecord); const outputDataBuffer = allocate_cbuffer(jsonBuffer.byteLength); - const result = libasherah.DecryptFromJson(partitionIdBuffer, jsonBuffer, outputDataBuffer); + const result = DecryptFromJson(partitionIdBuffer, jsonBuffer, outputDataBuffer); if (result < 0) { throw new Error('decrypt failed: ' + result); } @@ -101,7 +104,7 @@ export function encrypt(partitionId: string, data: Buffer): string { const dataBuffer = buffer_to_cbuffer(data); const outputJsonBuffer = allocate_cbuffer(estimate_buffer(data.byteLength, partitionId.length)); - const result = libasherah.EncryptToJson(partitionIdBuffer, dataBuffer, outputJsonBuffer) + const result = EncryptToJson(partitionIdBuffer, dataBuffer, outputJsonBuffer) if (result < 0) { throw new Error('encrypt failed: ' + result);