Skip to content

Commit

Permalink
Optimize for performance and latest Cobhan (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdy authored Mar 22, 2022
1 parent d0c77cf commit 8ef19e7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
34 changes: 27 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -48,6 +49,7 @@
},
"types": "dist/asherah.d.ts",
"dependencies": {
"cobhan": "^1.0.31"
"cobhan": "^1.0.33",
"ref-napi": "^3.0.3"
}
}
15 changes: 0 additions & 15 deletions scripts/verify-tag.sh

This file was deleted.

17 changes: 10 additions & 7 deletions src/asherah.ts
Original file line number Diff line number Diff line change
@@ -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) */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit 8ef19e7

Please sign in to comment.