Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref!: Switch to AES-GCM, and update tooling #3

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Because you don't have to be an expert to _use_ cryptography!

## Example

Encrypt and Decrypt with AES-CBC.
Encrypt and Decrypt with AES-GCM.

```js
let Cipher = require("@root/cipher");
Expand Down Expand Up @@ -179,10 +179,12 @@ The _Initialization Vector_ (_IV_) is a _salt_ that prevents known-plaintext
attacks - meaning that if you encrypt the same message with the same key twice,
you get a different encrypted output.

The first 16-bytes are for the _IV_. \
The following bytes are the data. \
The first 12-bytes (96-bits) are for the _IV_. The following bytes are the data
and the _Tag_.

If the data is somehow corrupted or truncated, but the first bytes are intact,
the IV can be used to restore the first partial data.
it may be possible to use the IV to restore some of the partial data (though
_Tag_ verification will likely fail).

# LICENSE

Expand Down
9 changes: 7 additions & 2 deletions cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ let Cipher = module.exports;

let Crypto = globalThis.crypto;

const IV_SIZE = 16;
const IV_SIZE = 12;
const TAG_SIZE = 12;

let decoder = new TextDecoder();
let encoder = new TextEncoder();
Expand All @@ -19,7 +20,11 @@ Cipher.create = function (sharedSecret) {
const NON_EXTRACTABLE = false;
/** @type {"raw"}*/
let keyFormat = "raw";
let algoOpts = { name: "AES-CBC" };
let algoOpts = {
name: "AES-GCM",
tagLength: TAG_SIZE * 8,
// additionalData: null,
};
/** @type {Array<"encrypt" | "decrypt" | "sign" | "verify">} */
let keyUsages = ["encrypt", "decrypt"];
/** @type {CryptoKey} */
Expand Down
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
},
"include": [
"cipher.js",
"tests/webcrypto.js",
"bin/**/*.js",
"lib/**/*.js",
"src/**/*.js"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@root/cipher",
"version": "0.1.0",
"description": "A simple AES-CBC 128-bit cipher codec (for encrypting and decrypting)",
"version": "1.0.0",
"description": "A simple AES-GCM cipher codec (for encrypting and decrypting)",
"main": "cipher.js",
"files": [
"cipher.js"
"cipher.js"
],
"scripts": {
"bump": "npm version -m \"chore(release): bump to v%s\"",
Expand All @@ -24,8 +24,10 @@
},
"keywords": [
"aes",
"cbc",
"gcm",
"128-bit",
"192-bit",
"256-bit",
"cipher"
],
"author": "AJ ONeal <aj@therootcompany.com> (https://therootcompany.com/)",
Expand Down
21 changes: 12 additions & 9 deletions tests/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ async function test() {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x90, 0xa0, 0xb0,
0xc0, 0xd0, 0xe0, 0xf0,
]);
let iv128 = new Uint8Array([
let iv96 = new Uint8Array([
0x01, 0x00, 0x90, 0xa0, 0x05, 0x06, 0x07, 0x08, 0xb0, 0xc0, 0xd0, 0x0f,
0xe0, 0x02, 0x03, 0x04,
]);
let exp64 = "AQCQoAUGBwiwwNAP4AIDBLLnH3xgsW4DmAlTqEw3nDI";
let enc64 = await testOne(secret128, iv128);
let exp64 = "AQCQoAUGBwiwwNAPAEh1XzzWzTxRk1ePbbFmv4fdg3gDHtM";
let enc64 = await testOne(secret128, iv96, SHOW);

Assert.equal(enc64, exp64);
}
Expand All @@ -33,13 +32,12 @@ async function test() {
0xc0, 0xd0, 0xe0, 0xf0, 0x00, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
]);
// iv is always 16 bytes
let iv128 = new Uint8Array([
// iv is always 12 bytes
let iv96 = new Uint8Array([
0x01, 0x00, 0x90, 0xa0, 0x05, 0x06, 0x07, 0x08, 0xb0, 0xc0, 0xd0, 0x0f,
0xe0, 0x02, 0x03, 0x04,
]);
let exp64 = "AQCQoAUGBwiwwNAP4AIDBAMOdOmrmBWpKmHe8IDqnJU";
let enc64 = await testOne(secret256, iv128);
let exp64 = "AQCQoAUGBwiwwNAP71XDYYfrZgBi6VUBfuJHhoLG5FL7pPk";
let enc64 = await testOne(secret256, iv96, SHOW);

Assert.equal(enc64, exp64);
}
Expand All @@ -51,6 +49,11 @@ async function test() {
}
}

/**
* @param {Uint8Array} sharedSecret
* @param {Uint8Array} [testIv]
* @param {Boolean} [show]
*/
async function testOne(sharedSecret, testIv, show) {
let cipher = Cipher.create(sharedSecret);

Expand Down
Loading