Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Rebuild and add JS test for encryptOnly and decryptOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Oct 25, 2023
1 parent dd6d0d0 commit 998bd12
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
15 changes: 14 additions & 1 deletion example/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import {from_hex, to_hex, decrypt_and_verify, gen_signing_key, encrypt_and_sign, public_key_from_secret, constant_time_eq } from "x25519-chacha20poly1305";
import {from_hex, to_hex, decrypt_and_verify, gen_signing_key, encrypt_and_sign, public_key_from_secret, constant_time_eq, encryptOnly, decryptOnly } from "x25519-chacha20poly1305";

let empty = new Uint8Array(32);
let hempty = to_hex(empty);
Expand Down Expand Up @@ -28,3 +28,16 @@ console.log(decrypted_plaintext);
let is_equal = constant_time_eq(decrypted_plaintext, plaintext);
console.log("alice encrypted plaintext is equal to bob decrypted ciphertext: ", is_equal);

// Test encryptOnly:

// Alice encrypts and signs the message to bob.
let encrypted_msg = encryptOnly(alice_sk, plaintext, bob_pk);
console.log(encrypted_msg);

// Bob decrypts the message.
decrypted_plaintext = decryptOnly(bob_sk, encrypted_msg);

console.log(decrypted_plaintext);
// Check the original plaintext equals the decrypted plaintext.
is_equal = constant_time_eq(decrypted_plaintext, plaintext);
console.log("alice encrypted plaintext is equal to bob decrypted ciphertext: ", is_equal);
16 changes: 16 additions & 0 deletions pkg/x25519_chacha20poly1305.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ export function decrypt_and_verify(sk: Uint8Array, msg: string): Uint8Array;
* @returns {boolean}
*/
export function constant_time_eq(a: Uint8Array, b: Uint8Array): boolean;
/**
* Encrypts, signs, and serializes a SignedMessage to JSON.
* @param {Uint8Array} sk
* @param {Uint8Array} msg
* @param {Uint8Array} pk
* @returns {string}
*/
export function encryptOnly(sk: Uint8Array, msg: Uint8Array, pk: Uint8Array): string;
/**
* Deserializes and decrypts a json encoded `EncryptedMessage`.
* Returns the plaintext.
* @param {Uint8Array} sk
* @param {string} msg
* @returns {Uint8Array}
*/
export function decryptOnly(sk: Uint8Array, msg: string): Uint8Array;
68 changes: 68 additions & 0 deletions pkg/x25519_chacha20poly1305.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,74 @@ module.exports.constant_time_eq = function(a, b) {
return ret !== 0;
};

/**
* Encrypts, signs, and serializes a SignedMessage to JSON.
* @param {Uint8Array} sk
* @param {Uint8Array} msg
* @param {Uint8Array} pk
* @returns {string}
*/
module.exports.encryptOnly = function(sk, msg, pk) {
let deferred5_0;
let deferred5_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(sk, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(msg, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm0(pk, wasm.__wbindgen_malloc);
const len2 = WASM_VECTOR_LEN;
wasm.encryptOnly(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
var ptr4 = r0;
var len4 = r1;
if (r3) {
ptr4 = 0; len4 = 0;
throw takeObject(r2);
}
deferred5_0 = ptr4;
deferred5_1 = len4;
return getStringFromWasm0(ptr4, len4);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred5_0, deferred5_1, 1);
}
};

/**
* Deserializes and decrypts a json encoded `EncryptedMessage`.
* Returns the plaintext.
* @param {Uint8Array} sk
* @param {string} msg
* @returns {Uint8Array}
*/
module.exports.decryptOnly = function(sk, msg) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(sk, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passStringToWasm0(msg, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
wasm.decryptOnly(retptr, ptr0, len0, ptr1, len1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v3;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};

function handleError(f, args) {
try {
return f.apply(this, args);
Expand Down
Binary file modified pkg/x25519_chacha20poly1305_bg.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions pkg/x25519_chacha20poly1305_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export function gen_signing_key(a: number): void;
export function encrypt_and_sign(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
export function decrypt_and_verify(a: number, b: number, c: number, d: number, e: number): void;
export function constant_time_eq(a: number, b: number, c: number, d: number): number;
export function encryptOnly(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
export function decryptOnly(a: number, b: number, c: number, d: number, e: number): void;
export function rustsecp256k1_v0_4_1_context_create(a: number): number;
export function rustsecp256k1_v0_4_1_context_destroy(a: number): void;
export function rustsecp256k1_v0_4_1_default_illegal_callback_fn(a: number, b: number): void;
Expand Down

0 comments on commit 998bd12

Please sign in to comment.