Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c245955
Showing
20 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
/** @type {import('aegir/types').PartialOptions} */ | ||
export default { | ||
build: { | ||
bundlesizeMax: '17kB' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
node_modules | ||
build | ||
dist | ||
.docs | ||
.coverage | ||
node_modules | ||
package-lock.json | ||
yarn.lock | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 - <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: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c> | ||
|
||
// Decrypting | ||
const decipher = create(key128, IV) | ||
const decryptedBuffer = decipher.decrypt(encryptedBuffer) | ||
|
||
console.log(decryptedBuffer) | ||
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c> | ||
|
||
console.log(decryptedBuffer.toString('utf-8')) | ||
// prints: Hello, world! | ||
``` | ||
|
||
# Install | ||
|
||
```console | ||
$ npm i @libp2p/aes-ctr | ||
``` | ||
|
||
## Browser `<script>` tag | ||
|
||
Loading this module through a script tag will make it's exports available as `Libp2pAesCtr` in the global namespace. | ||
|
||
```html | ||
<script src="https://unpkg.com/@libp2p/aes-ctr/dist/index.min.js"></script> | ||
``` | ||
|
||
# API Docs | ||
|
||
- <https://libp2p.github.io/js-libp2p-aes-ctr> | ||
|
||
# License | ||
|
||
Licensed under either of | ||
|
||
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) | ||
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{ | ||
"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:webkit": "aegir test -t browser -- --browser webkit", | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import crypto from 'crypto' | ||
|
||
export const createCipheriv = crypto.createCipheriv | ||
export const createDecipheriv = crypto.createDecipheriv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c> | ||
* | ||
* // Decrypting | ||
* const decipher = create(key128, IV) | ||
* const decryptedBuffer = decipher.decrypt(encryptedBuffer) | ||
* | ||
* console.log(decryptedBuffer) | ||
* // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c> | ||
* | ||
* 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 | ||
} |
Oops, something went wrong.