From 13416b8cbcbf1ab7978f2c3eebe49de7fe62a5f9 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 12 Jan 2024 11:01:52 +0100 Subject: [PATCH] feat: initial import --- .aegir.js | 7 ++ .github/dependabot.yml | 11 ++ .github/workflows/js-test-and-release.yml | 27 +++++ .github/workflows/semantic-pull-request.yml | 12 +++ .github/workflows/stale.yml | 13 +++ .gitignore | 9 ++ LICENSE | 4 + LICENSE-APACHE | 5 + LICENSE-MIT | 19 ++++ README.md | 77 ++++++++++++++ package.json | 72 ++++++++++++++ src/cipher-mode.ts | 15 +++ src/ciphers-browser.ts | 31 ++++++ src/ciphers.ts | 4 + src/index.ts | 73 ++++++++++++++ test/fixtures/aes.ts | 36 +++++++ test/fixtures/go-aes.ts | 18 ++++ test/index.spec.ts | 105 ++++++++++++++++++++ tsconfig.json | 10 ++ typedoc.json | 5 + 20 files changed, 553 insertions(+) create mode 100644 .aegir.js create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/js-test-and-release.yml create mode 100644 .github/workflows/semantic-pull-request.yml create mode 100644 .github/workflows/stale.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 package.json create mode 100644 src/cipher-mode.ts create mode 100644 src/ciphers-browser.ts create mode 100644 src/ciphers.ts create mode 100644 src/index.ts create mode 100644 test/fixtures/aes.ts create mode 100644 test/fixtures/go-aes.ts create mode 100644 test/index.spec.ts create mode 100644 tsconfig.json create mode 100644 typedoc.json diff --git a/.aegir.js b/.aegir.js new file mode 100644 index 0000000..1afcd21 --- /dev/null +++ b/.aegir.js @@ -0,0 +1,7 @@ + +/** @type {import('aegir/types').PartialOptions} */ +export default { + build: { + bundlesizeMax: '17kB' + } +} diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..d401a77 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +version: 2 +updates: +- package-ecosystem: npm + directory: "/" + schedule: + interval: daily + time: "10:00" + open-pull-requests-limit: 20 + commit-message: + prefix: "deps" + prefix-development: "deps(dev)" diff --git a/.github/workflows/js-test-and-release.yml b/.github/workflows/js-test-and-release.yml new file mode 100644 index 0000000..1d7ff79 --- /dev/null +++ b/.github/workflows/js-test-and-release.yml @@ -0,0 +1,27 @@ +name: test & maybe release + +on: + push: + branches: + - main + pull_request: + workflow_dispatch: + +permissions: + contents: write + id-token: write + packages: write + pull-requests: write + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }} + cancel-in-progress: true + +jobs: + js-test-and-release: + uses: pl-strflt/uci/.github/workflows/js-test-and-release.yml@v0.0 + secrets: + DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + UCI_GITHUB_TOKEN: ${{ secrets.UCI_GITHUB_TOKEN }} diff --git a/.github/workflows/semantic-pull-request.yml b/.github/workflows/semantic-pull-request.yml new file mode 100644 index 0000000..bd00f09 --- /dev/null +++ b/.github/workflows/semantic-pull-request.yml @@ -0,0 +1,12 @@ +name: Semantic PR + +on: + pull_request_target: + types: + - opened + - edited + - synchronize + +jobs: + main: + uses: pl-strflt/.github/.github/workflows/reusable-semantic-pull-request.yml@v0.3 diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 0000000..16d65d7 --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,13 @@ +name: Close and mark stale issue + +on: + schedule: + - cron: '0 0 * * *' + +permissions: + issues: write + pull-requests: write + +jobs: + stale: + uses: pl-strflt/.github/.github/workflows/reusable-stale-issue.yml@v0.3 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ad9e67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules +build +dist +.docs +.coverage +node_modules +package-lock.json +yarn.lock +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..20ce483 --- /dev/null +++ b/LICENSE @@ -0,0 +1,4 @@ +This project is dual licensed under MIT and Apache-2.0. + +MIT: https://www.opensource.org/licenses/mit +Apache-2.0: https://www.apache.org/licenses/license-2.0 diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..14478a3 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,5 @@ +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..72dc60d --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,19 @@ +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8cdab1a --- /dev/null +++ b/README.md @@ -0,0 +1,77 @@ +[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) +[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io) +[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-aes-ctr.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-aes-ctr) +[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-aes-ctr/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p-aes-ctr/actions/workflows/js-test-and-release.yml?query=branch%3Amain) + +> Streaming AES-CTR for node and browsers + +# About + +WebCrypto does not support streaming encryption - + +In browsers this module uses `node-forge` to expose a streaming interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197. + +In node.js it uses the regular streaming API exported by the `crypto` module. + +This uses `CTR` mode. + +## Example + +```js +import { create } from '@libp2p/aes-ctr' + +// Setting up Key and IV + +// A 16 bytes array, 128 Bits, AES-128 is chosen +const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) + +// A 16 bytes array, 128 Bits, +const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) + +const decryptedMessage = 'Hello, world!' + +// Encrypting +const cipher = create(key128, IV) +const encryptedBuffer = cipher.encrypt(Uint8Array.from(decryptedMessage)) +console.log(encryptedBuffer) +// prints: + +// Decrypting +const decipher = create(key128, IV) +const decryptedBuffer = decipher.decrypt(encryptedBuffer) + +console.log(decryptedBuffer) +// prints: + +console.log(decryptedBuffer.toString('utf-8')) +// prints: Hello, world! +``` + +# Install + +```console +$ npm i @libp2p/aes-ctr +``` + +## Browser ` +``` + +# API Docs + +- + +# License + +Licensed under either of + +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](LICENSE-MIT) / ) + +# Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/package.json b/package.json new file mode 100644 index 0000000..4f2069f --- /dev/null +++ b/package.json @@ -0,0 +1,72 @@ +{ + "name": "@libp2p/aes-ctr", + "version": "0.0.0", + "description": "Streaming AES-CTR for node and browsers", + "license": "Apache-2.0 OR MIT", + "homepage": "https://github.com/libp2p/js-libp2p-aes-ctr#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/libp2p/js-libp2p-aes-ctr.git" + }, + "bugs": { + "url": "https://github.com/libp2p/js-libp2p-aes-ctr/issues" + }, + "publishConfig": { + "access": "public", + "provenance": true + }, + "keywords": [ + "IPFS", + "aes", + "aes-ctr", + "crypto", + "libp2p" + ], + "type": "module", + "types": "./dist/src/index.d.ts", + "files": [ + "src", + "dist", + "!dist/test", + "!**/*.tsbuildinfo" + ], + "exports": { + ".": { + "types": "./src/index.d.ts", + "import": "./dist/src/index.js" + } + }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "project": true, + "sourceType": "module" + } + }, + "scripts": { + "clean": "aegir clean", + "lint": "aegir lint", + "dep-check": "aegir dep-check", + "build": "aegir build", + "test": "aegir test", + "test:chrome": "aegir test -t browser", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "test:electron-main": "aegir test -t electron-main", + "docs": "aegir docs" + }, + "dependencies": { + "@libp2p/interface": "^1.1.1", + "node-forge": "^1.3.1", + "uint8arrays": "^5.0.0" + }, + "devDependencies": { + "@types/mocha": "^10.0.0", + "aegir": "^42.0.0" + }, + "browser": { + "./dist/src/ciphers.js": "./dist/src/ciphers-browser.js" + } +} diff --git a/src/cipher-mode.ts b/src/cipher-mode.ts new file mode 100644 index 0000000..7aabcab --- /dev/null +++ b/src/cipher-mode.ts @@ -0,0 +1,15 @@ +import { CodeError } from '@libp2p/interface' + +const CIPHER_MODES = { + 16: 'aes-128-ctr', + 32: 'aes-256-ctr' +} + +export function cipherMode (key: Uint8Array): string { + if (key.length === 16 || key.length === 32) { + return CIPHER_MODES[key.length] + } + + const modes = Object.entries(CIPHER_MODES).map(([k, v]) => `${k} (${v})`).join(' / ') + throw new CodeError(`Invalid key length ${key.length} bytes. Must be ${modes}`, 'ERR_INVALID_KEY_LENGTH') +} diff --git a/src/ciphers-browser.ts b/src/ciphers-browser.ts new file mode 100644 index 0000000..da94b50 --- /dev/null +++ b/src/ciphers-browser.ts @@ -0,0 +1,31 @@ +import 'node-forge/lib/aes.js' +// @ts-expect-error types are missing +import forge from 'node-forge/lib/forge.js' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' + +export interface Cipher { + update(data: Uint8Array): Uint8Array +} + +export function createCipheriv (mode: any, key: Uint8Array, iv: Uint8Array): Cipher { + const cipher2 = forge.cipher.createCipher('AES-CTR', uint8ArrayToString(key, 'ascii')) + cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') }) + return { + update: (data: Uint8Array) => { + cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii'))) + return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii') + } + } +} + +export function createDecipheriv (mode: any, key: Uint8Array, iv: Uint8Array): Cipher { + const cipher2 = forge.cipher.createDecipher('AES-CTR', uint8ArrayToString(key, 'ascii')) + cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') }) + return { + update: (data: Uint8Array) => { + cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii'))) + return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii') + } + } +} diff --git a/src/ciphers.ts b/src/ciphers.ts new file mode 100644 index 0000000..c1a2cd7 --- /dev/null +++ b/src/ciphers.ts @@ -0,0 +1,4 @@ +import crypto from 'crypto' + +export const createCipheriv = crypto.createCipheriv +export const createDecipheriv = crypto.createDecipheriv diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..4241c1e --- /dev/null +++ b/src/index.ts @@ -0,0 +1,73 @@ +/** + * @packageDocumentation + * + * WebCrypto does not support streaming encryption - https://github.com/w3c/webcrypto/issues/73 + * + * In browsers this module uses `node-forge` to expose a streaming interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197. + * + * In node.js it uses the regular streaming API exported by the `crypto` module. + * + * This uses `CTR` mode. + * + * @example + * + * ```js + * import { create } from '@libp2p/aes-ctr' + * + * // Setting up Key and IV + * + * // A 16 bytes array, 128 Bits, AES-128 is chosen + * const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) + * + * // A 16 bytes array, 128 Bits, + * const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) + * + * const decryptedMessage = 'Hello, world!' + * + * // Encrypting + * const cipher = create(key128, IV) + * const encryptedBuffer = cipher.encrypt(Uint8Array.from(decryptedMessage)) + * console.log(encryptedBuffer) + * // prints: + * + * // Decrypting + * const decipher = create(key128, IV) + * const decryptedBuffer = decipher.decrypt(encryptedBuffer) + * + * console.log(decryptedBuffer) + * // prints: + * + * console.log(decryptedBuffer.toString('utf-8')) + * // prints: Hello, world! + * ``` + */ + +import { cipherMode } from './cipher-mode.js' +import * as ciphers from './ciphers.js' + +export interface AESCipher { + encrypt(data: Uint8Array): Uint8Array + decrypt(data: Uint8Array): Uint8Array +} + +/** + * @param key - The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used + * @param iv - Must have length `16` + */ +export function create (key: Uint8Array, iv: Uint8Array): AESCipher { + const mode = cipherMode(key) + const cipher = ciphers.createCipheriv(mode, key, iv) + const decipher = ciphers.createDecipheriv(mode, key, iv) + + const res: AESCipher = { + encrypt (data) { + return cipher.update(data) + }, + + decrypt (data) { + return decipher.update(data) + } + } + + return res +} diff --git a/test/fixtures/aes.ts b/test/fixtures/aes.ts new file mode 100644 index 0000000..f696dd6 --- /dev/null +++ b/test/fixtures/aes.ts @@ -0,0 +1,36 @@ +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' + +export default { + 16: { + inputs: [ + uint8ArrayFromString('Ly8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLw', 'base64'), + uint8ArrayFromString('GBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGA', 'base64'), + uint8ArrayFromString('BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBw', 'base64'), + uint8ArrayFromString('GRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGQ', 'base64'), + uint8ArrayFromString('MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMA', 'base64') + ], + outputs: [ + uint8ArrayFromString('eWgAlzmJFu/qlzoPZBbkblX4+Q+RgN8ZwK+EqWLL52rgZs70HdUkAhrVXh2G24hJ1LAhX8ZblIuE/LZzdKCSwgBhtQDBlRUz+GEgPlaZ7kMmIjePRsFjax9DWmE3P0XLIelK7Q', 'base64'), + uint8ArrayFromString('bax/s27eT9tXTEbMpm645VoxoPxOOkkzmNoDyAp8mHWJKBd/ODnLH2XjH8bfVmJ4ZFZ0kI5/RK/56BZTFkQ85pIWmcFDBTP979JQH/5nuZF7Y82vnJC/Qx/sK2LF6x8yReRkQA', 'base64'), + uint8ArrayFromString('v11+v7QqdpAcvNO/04KqmYbws1NLFypEnsh7mzmpmIUhclodg1tGaVMtKC9NYGEIKAFu9WqsmJIFcoQAsx8sThNtXMfiJAxKtPHga1MNpxv7ZcFiMVrhxUvVkDTrglz324vRhA', 'base64'), + uint8ArrayFromString('v241vvosvogW0YPIcB89t/dfBPlFk+5KEkfFc43iZlxbgLWsQ20RpTQKNx834f2MmiNoPndnxZh9hoy1qkxLcsO8RMUcL3RSIoDoeg7leqEk1KGkkVbX6d4yj1mDIILEbSTM/g', 'base64'), + uint8ArrayFromString('YY4IJUWtfLRhRi1bxH64h9Voq1nnPysqB/VLpc22GDKq2YBwctfRkYfrs9QFUY7HNd0n76cV7aiR+fpsAvdZSeTj/5t5nc1gKyBw0a1gjyvcjBrNIiI1nSmnfevzVQ0OXW3pug', 'base64') + ] + }, + 32: { + inputs: [ + uint8ArrayFromString('RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERA', 'base64'), + uint8ArrayFromString('CgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCg', 'base64'), + uint8ArrayFromString('S0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSw', 'base64'), + uint8ArrayFromString('IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIw', 'base64'), + uint8ArrayFromString('SEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISA', 'base64') + ], + outputs: [ + uint8ArrayFromString('xjtba97WH+ZwAceuEeDC4YpBOqAf+WYQSi5VHuj5eGwmpWkbDGJibIKjEIl0aJm8Bfuj9AA6Ac8ZTrzSzd0whEjRC0MOKtpHlPx+tzwgXSR9Z791UvG+sAGBdnCLmbI4PVs0xg', 'base64'), + uint8ArrayFromString('zvFjePijOR7YVv/AWcGwEU4+UJW96xudr/gHE95Ab8fMoxoQX91GIO8EOqL97QgzXgOlut/SdGXkUmdMSiwzdb2MhOa88xielV2T4nHDHxgJExuEtJgaQX2QVqcpkJ7MTC61bg', 'base64'), + uint8ArrayFromString('maHJQlcu8leI7pfGgXo+zY78bbvutz9f8GHc0SWQ7VT7lZjeWcjQd9UmQRMC/MF9Xky2xvN5/RAt/lIvks4paf7t7I121sXkO30tyD0YbhrrXK//VXc5oJFrzhw+CqZZBxT28w', 'base64'), + uint8ArrayFromString('T9cWHYSC1vjtfOo2104A0/beNls1AjEoAMq8Gbh5pOu9YQ4AU6ZYPjcxT5mIwYXOrGPPSfbYwGsyzqfyGbQ/uMk9WvLfwA2MH/BwnfpajgMoDGo/SSpPUhQpu60XVTv91L9tLg', 'base64'), + uint8ArrayFromString('yeA4QKfgfDETM9Di2DIMSQ//nGxis5BuIZcrQOOZeCcVlyk99RQfF23VbTcjKHptKQogsBm4W7Cxhor8oAJsK97vrgKRSiKD7dbrZhrMfEBlhrotNx00N6tfrFbyZY2Z3qGAUw', 'base64') + ] + } +} diff --git a/test/fixtures/go-aes.ts b/test/fixtures/go-aes.ts new file mode 100644 index 0000000..4663a67 --- /dev/null +++ b/test/fixtures/go-aes.ts @@ -0,0 +1,18 @@ +export default { + 16: { + inputs: [ + Uint8Array.from([47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47]), + Uint8Array.from([24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24]), + Uint8Array.from([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]), + Uint8Array.from([25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25]), + Uint8Array.from([48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]) + ], + outputs: [ + Uint8Array.from([121, 104, 0, 151, 57, 137, 22, 239, 234, 151, 58, 15, 100, 22, 228, 110, 85, 248, 249, 15, 145, 128, 223, 25, 192, 175, 132, 169, 98, 203, 231, 106, 224, 102, 206, 244, 29, 213, 36, 2, 26, 213, 94, 29, 134, 219, 136, 73, 212, 176, 33, 95, 198, 91, 148, 139, 132, 252, 182, 115, 116, 160, 146, 194, 0, 97, 181, 0, 193, 149, 21, 51, 248, 97, 32, 62, 86, 153, 238, 67, 38, 34, 55, 143, 70, 193, 99, 107, 31, 67, 90, 97, 55, 63, 69, 203, 33, 233, 74, 237]), + Uint8Array.from([109, 172, 127, 179, 110, 222, 79, 219, 87, 76, 70, 204, 166, 110, 184, 229, 90, 49, 160, 252, 78, 58, 73, 51, 152, 218, 3, 200, 10, 124, 152, 117, 137, 40, 23, 127, 56, 57, 203, 31, 101, 227, 31, 198, 223, 86, 98, 120, 100, 86, 116, 144, 142, 127, 68, 175, 249, 232, 22, 83, 22, 68, 60, 230, 146, 22, 153, 193, 67, 5, 51, 253, 239, 210, 80, 31, 254, 103, 185, 145, 123, 99, 205, 175, 156, 144, 191, 67, 31, 236, 43, 98, 197, 235, 31, 50, 69, 228, 100, 64]), + Uint8Array.from([191, 93, 126, 191, 180, 42, 118, 144, 28, 188, 211, 191, 211, 130, 170, 153, 134, 240, 179, 83, 75, 23, 42, 68, 158, 200, 123, 155, 57, 169, 152, 133, 33, 114, 90, 29, 131, 91, 70, 105, 83, 45, 40, 47, 77, 96, 97, 8, 40, 1, 110, 245, 106, 172, 152, 146, 5, 114, 132, 0, 179, 31, 44, 78, 19, 109, 92, 199, 226, 36, 12, 74, 180, 241, 224, 107, 83, 13, 167, 27, 251, 101, 193, 98, 49, 90, 225, 197, 75, 213, 144, 52, 235, 130, 92, 247, 219, 139, 209, 132]), + Uint8Array.from([191, 110, 53, 190, 250, 44, 190, 136, 22, 209, 131, 200, 112, 31, 61, 183, 247, 95, 4, 249, 69, 147, 238, 74, 18, 71, 197, 115, 141, 226, 102, 92, 91, 128, 181, 172, 67, 109, 17, 165, 52, 10, 55, 31, 55, 225, 253, 140, 154, 35, 104, 62, 119, 103, 197, 152, 125, 134, 140, 181, 170, 76, 75, 114, 195, 188, 68, 197, 28, 47, 116, 82, 34, 128, 232, 122, 14, 229, 122, 161, 36, 212, 161, 164, 145, 86, 215, 233, 222, 50, 143, 89, 131, 32, 130, 196, 109, 36, 204, 254]), + Uint8Array.from([97, 142, 8, 37, 69, 173, 124, 180, 97, 70, 45, 91, 196, 126, 184, 135, 213, 104, 171, 89, 231, 63, 43, 42, 7, 245, 75, 165, 205, 182, 24, 50, 170, 217, 128, 112, 114, 215, 209, 145, 135, 235, 179, 212, 5, 81, 142, 199, 53, 221, 39, 239, 167, 21, 237, 168, 145, 249, 250, 108, 2, 247, 89, 73, 228, 227, 255, 155, 121, 157, 205, 96, 43, 32, 112, 209, 173, 96, 143, 43, 220, 140, 26, 205, 34, 34, 53, 157, 41, 167, 125, 235, 243, 85, 13, 14, 93, 109, 233, 186]) + ] + } +} diff --git a/test/index.spec.ts b/test/index.spec.ts new file mode 100644 index 0000000..21ce9b4 --- /dev/null +++ b/test/index.spec.ts @@ -0,0 +1,105 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-disable valid-jsdoc */ +/* eslint-env mocha */ +import { expect } from 'aegir/chai' +import * as aes from '../src/index.js' +import fixtures from './fixtures/aes.js' +import goFixtures from './fixtures/go-aes.js' +import type { AESCipher } from '../src/index.js' + +const bytes = [{ + length: 16, + hash: 'AES-128' +}, { + length: 32, + hash: 'AES-256' +}] + +describe('AES-CTR', () => { + bytes.forEach(({ length, hash }) => { + it(`${hash} - encrypt and decrypt`, async () => { + const key = new Uint8Array(length) + key.fill(5) + + const iv = new Uint8Array(16) + iv.fill(1) + + const cipher = aes.create(key, iv) + + await encryptAndDecrypt(cipher) + await encryptAndDecrypt(cipher) + await encryptAndDecrypt(cipher) + await encryptAndDecrypt(cipher) + await encryptAndDecrypt(cipher) + }) + }) + + bytes.forEach(({ length, hash }) => { + it(`${hash} - fixed - encrypt and decrypt`, async () => { + const key = new Uint8Array(length) + key.fill(5) + + const iv = new Uint8Array(16) + iv.fill(1) + + const cipher = aes.create(key, iv) + // @ts-expect-error cannot index fixtures like this + const fixture = fixtures[length] + + for (let i = 0; i < fixture.inputs.length; i++) { + const input = fixture.inputs[i] + const output = fixture.outputs[i] + const encrypted = cipher.encrypt(input) + expect(encrypted).to.have.length(output.length) + expect(encrypted).to.eql(output) + const decrypted = cipher.decrypt(encrypted) + expect(decrypted).to.eql(input) + } + }) + }) + + bytes.forEach(({ length, hash }) => { + // @ts-expect-error cannot index fixtures like this + if (goFixtures[length] == null) { + return + } + + it(`${hash} - go interop - encrypt and decrypt`, async () => { + const key = new Uint8Array(length) + key.fill(5) + + const iv = new Uint8Array(16) + iv.fill(1) + + const cipher = aes.create(key, iv) + // @ts-expect-error cannot index fixtures like this + const fixture = goFixtures[length] + + for (let i = 0; i < fixture.inputs.length; i++) { + const input = fixture.inputs[i] + const output = fixture.outputs[i] + const encrypted = cipher.encrypt(input) + expect(encrypted).to.have.length(output.length) + expect(encrypted).to.eql(output) + const decrypted = cipher.decrypt(encrypted) + expect(decrypted).to.eql(input) + } + }) + }) + + it('checks key length', () => { + const key = new Uint8Array(5) + const iv = new Uint8Array(16) + return expect(() => aes.create(key, iv)).to.throw().with.property('code', 'ERR_INVALID_KEY_LENGTH') + }) +}) + +async function encryptAndDecrypt (cipher: AESCipher): Promise { + const data = new Uint8Array(100) + data.fill(Math.ceil(Math.random() * 100)) + + const encrypted = cipher.encrypt(data) + const decrypted = cipher.decrypt(encrypted) + + expect(decrypted).to.be.eql(data) +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..13a3599 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "test" + ] +} diff --git a/typedoc.json b/typedoc.json new file mode 100644 index 0000000..f599dc7 --- /dev/null +++ b/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": [ + "./src/index.ts" + ] +}