Skip to content

Commit

Permalink
added more tests and fixed missing crypto functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brainfoolong committed Nov 23, 2023
1 parent 69bd4f5 commit b47997b
Show file tree
Hide file tree
Showing 12 changed files with 966 additions and 337 deletions.
10 changes: 7 additions & 3 deletions build/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ const fs = require('fs')

const packageJson = require('../package.json')
const srcFile = __dirname + '/../dist/ascon.js'
let contents = fs.readFileSync(srcFile).toString()
const srcFileEs6 = __dirname + '/../dist/ascon.es6.js'
let contents = fs.readFileSync(srcFile).toString().replace('export default JsAscon;', '')
contents = '// js-ascon v' + packageJson.version + ' @ ' + packageJson.homepage + '\n' + contents
contents = contents.replace(/export default class JsAscon/, 'class JsAscon')
contents += `
if (typeof module !== 'undefined' && module.exports) {
module.exports = JsAscon
}
if(typeof crypto === 'undefined' && typeof global !== 'undefined'){
global.crypto = require('crypto')
}
`
fs.writeFileSync(srcFile, contents)
let contentsCommonJs = contents
fs.writeFileSync(srcFile, contentsCommonJs)
let contentsEs6 = contents.replace(/^class JsAscon/m, 'export default class JsAscon')
fs.writeFileSync(srcFileEs6, contentsEs6)
627 changes: 627 additions & 0 deletions dist/ascon.es6.js

Large diffs are not rendered by default.

34 changes: 27 additions & 7 deletions dist/ascon.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// js-ascon v1.0.0 @ https://github.com/brainfoolong/js-ascon
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Javascript / Typescript implementation of Ascon v1.2
* Heavily inspired by the python implementation of https://github.com/meichlseder/pyascon
Expand All @@ -19,7 +17,7 @@ class JsAscon {
*/
static encryptToHex(secretKey, messageToEncrypt, associatedData = null, cipherVariant = 'Ascon-128') {
const key = JsAscon.hash(secretKey, 'Ascon-Xof', cipherVariant === 'Ascon-80pq' ? 20 : 16);
const nonce = crypto.getRandomValues(new Uint8Array(16));
const nonce = JsAscon.getRandomUintArray(16);
const ciphertext = JsAscon.encrypt(key, nonce, associatedData !== null ? JSON.stringify(associatedData) : '', JSON.stringify(messageToEncrypt), cipherVariant);
return JsAscon.byteArrayToHex(ciphertext).substring(2) + JsAscon.byteArrayToHex(nonce).substring(2);
}
Expand Down Expand Up @@ -462,10 +460,12 @@ class JsAscon {
* @return {Uint8Array}
*/
static anyToByteArray(val) {
if (val instanceof Uint8Array)
if (val instanceof Uint8Array) {
return val;
if (Array.isArray(val))
}
if (Array.isArray(val)) {
return new Uint8Array(val);
}
return new TextEncoder().encode(val);
}
/**
Expand Down Expand Up @@ -513,8 +513,9 @@ class JsAscon {
if (offset < 0) {
offset = byteArray.length + offset;
}
if (byteArray.length - 1 < offset)
if (byteArray.length - 1 < offset) {
return 0n;
}
return new DataView(byteArray.buffer).getBigUint64(offset);
}
/**
Expand Down Expand Up @@ -567,6 +568,24 @@ class JsAscon {
throw new Error(errorMessage);
}
}
/**
* Generate a uint array with random bytes with given length
* @param {number} length
* @return {Uint8Array}
*/
static getRandomUintArray(length) {
if (typeof crypto === 'undefined') {
new Error('JsAscon requires the "crypto" library to be installed');
}
if (typeof crypto.getRandomValues === 'function') {
return crypto.getRandomValues(new Uint8Array(length));
}
// @ts-ignore
if (typeof crypto.randomBytes === 'function') {
// @ts-ignore
return JsAscon.anyToByteArray(crypto.randomBytes(length));
}
}
/**
* Debug output
* @param {any} msg
Expand Down Expand Up @@ -594,14 +613,15 @@ class JsAscon {
}
JsAscon.debugEnabled = false;
JsAscon.debugPermutationEnabled = false;
exports.default = JsAscon;

if (typeof BigInt === 'undefined') {
throw new Error('Cannot use JsAscon library, BigInt datatype is missing');
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = JsAscon
}

if(typeof crypto === 'undefined' && typeof global !== 'undefined'){
global.crypto = require('crypto')
}
108 changes: 0 additions & 108 deletions playwright.config.ts

This file was deleted.

33 changes: 29 additions & 4 deletions src/ascon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class JsAscon {
cipherVariant: string = 'Ascon-128'
): string {
const key = JsAscon.hash(secretKey, 'Ascon-Xof', cipherVariant === 'Ascon-80pq' ? 20 : 16)
const nonce = crypto.getRandomValues(new Uint8Array(16))
const nonce = JsAscon.getRandomUintArray(16)
const ciphertext = JsAscon.encrypt(
key,
nonce,
Expand Down Expand Up @@ -599,8 +599,12 @@ export default class JsAscon {
* @return {Uint8Array}
*/
public static anyToByteArray (val: any): Uint8Array {
if (val instanceof Uint8Array) return val
if (Array.isArray(val)) return new Uint8Array(val)
if (val instanceof Uint8Array) {
return val
}
if (Array.isArray(val)) {
return new Uint8Array(val)
}
return new TextEncoder().encode(val)
}

Expand Down Expand Up @@ -651,7 +655,9 @@ export default class JsAscon {
if (offset < 0) {
offset = byteArray.length + offset
}
if (byteArray.length - 1 < offset) return 0n
if (byteArray.length - 1 < offset) {
return 0n
}
return new DataView(byteArray.buffer).getBigUint64(offset)
}

Expand Down Expand Up @@ -717,6 +723,25 @@ export default class JsAscon {
}
}

/**
* Generate a uint array with random bytes with given length
* @param {number} length
* @return {Uint8Array}
*/
public static getRandomUintArray (length: number): Uint8Array {
if (typeof crypto === 'undefined') {
new Error('JsAscon requires the "crypto" library to be installed')
}
if (typeof crypto.getRandomValues === 'function') {
return crypto.getRandomValues(new Uint8Array(length))
}
// @ts-ignore
if (typeof crypto.randomBytes === 'function') {
// @ts-ignore
return JsAscon.anyToByteArray(crypto.randomBytes(length))
}
}

/**
* Debug output
* @param {any} msg
Expand Down
23 changes: 23 additions & 0 deletions tests/browser-tests.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Js Ascon Browser Tests</title>
<script src="../dist/ascon.js"></script>
<script>
function require (file) {
if (file.endsWith('/ascon')) {
return JsAscon
}
const script = document.createElement('script')
script.src = file
document.head.append(script)
}
</script>
<script src="../tests/test-all.js"></script>
<script src="../tests/performance.js"></script>
</head>
<body>

</body>
</html>
Loading

0 comments on commit b47997b

Please sign in to comment.