Skip to content

Commit

Permalink
refactor!: rewrite AES and Blowfish logic
Browse files Browse the repository at this point in the history
- AES and Blowfish performance significantly improved
- added example to README.md
- added more tests
- dev dependencies moved to dev_deps.ts

BREAKING CHANGE: Block modes are now separated from encryption
logic and can be instantiated directly, e.g. AesEcb
  • Loading branch information
aykxt committed Mar 11, 2021
1 parent 2246d5b commit 5288fa3
Show file tree
Hide file tree
Showing 24 changed files with 864 additions and 954 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/
.vscode/
.coverage/
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
# Crypto
![ci](https://github.com/aykxt/crypto/workflows/ci/badge.svg)

![ci](https://github.com/aykxt/crypto/workflows/ci/badge.svg)

A collection of useful crypto algorithms written in Typescript.

> ⚠ This project is still in an early stage of development. Expect **breaking
> changes**.
## Supported algorithms

### Block ciphers
- AES

- AES (Advanced Encryption Standard)
- Blowfish
- ECB and CBC block modes

## Examples

```ts
import { AesEcb } from "https://deno.land/x/crypto/aes.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

//deno-fmt-ignore
const key = new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);

const cipher = new AesEcb(key);

//deno-fmt-ignore
const data = new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);

const encrypted = cipher.encrypt(data);

assertEquals(cipher.decrypt(encrypted), data);
```
2 changes: 2 additions & 0 deletions aes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AesCbc, AesEcb } from "./src/aes/mod.ts";
export { Padding } from "./src/utils/padding.ts";
112 changes: 86 additions & 26 deletions benchmarks/aes.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,101 @@
import {
bench,
runBenchmarks,
} from "https://deno.land/std@0.74.0/testing/bench.ts";
import { AES } from "../src/aes/mod.ts";
import { bench, runBenchmarks } from "../dev_deps.ts";
import { AesCbc, AesEcb } from "../src/aes/mod.ts";
import { AES as GodCryptoAES } from "https://deno.land/x/god_crypto@v1.4.9/aes.ts";

// deno-fmt-ignore
const key = new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
const data = new Uint8Array(1024 * 1024 * 2);

bench({
name: "AES-ECB 2MiB",
name: "AES-128-ECB 2MiB Encrypt",
runs: 50,
func(b) {
const data = new Uint8Array(1024 * 1024 * 2);
const bf = new AES("abcdefghijklmnop", {
padding: AES.PADDING.NONE,
});
const cipher = new AesEcb(key);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "AES-128-ECB 2MiB Decrypt",
runs: 50,
func(b) {
const cipher = new AesEcb(key);
b.start();
const enc = bf.encrypt(data);
bf.decrypt(enc);
cipher.decrypt(data);
b.stop();
},
});

bench({
name: "AES-CBC 2MiB",
name: "AES-128-CBC 2MiB Encrypt",
runs: 50,
func(b) {
const data = new Uint8Array(1024 * 1024 * 2);
const bf = new AES(
"abcdefghijklmnop",
{
mode: AES.MODE.CBC,
// deno-fmt-ignore
iv: "abcdefghijklmnop",
padding: AES.PADDING.NONE,
},
);
b.start();
const enc = bf.encrypt(data);
bf.decrypt(enc);
const cipher = new AesCbc(key, new Uint8Array(16));
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "AES-128-CBC 2MiB Decrypt",
runs: 50,
func(b) {
const cipher = new AesCbc(key, new Uint8Array(16));
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
name: "AES-128-ECB (GodCrypto) 2MiB Encrypt",
runs: 5, // takes too long
async func(b) {
const cipher = new GodCryptoAES(key, { mode: "ecb" });
b.start();
await cipher.encrypt(data);
b.stop();
},
});

bench({
name: "AES-128-ECB (GodCrypto) 2MiB Decrypt",
runs: 5, // takes too long
async func(b) {
const cipher = new GodCryptoAES(key, { mode: "ecb" });
b.start();
await cipher.decrypt(data);
b.stop();
},
});

bench({
name: "AES-128-CBC (GodCrypto) 2MiB Encrypt",
runs: 5, // takes too long
async func(b) {
const cipher = new GodCryptoAES(key, {
mode: "cbc",
iv: new Uint8Array(16),
});
b.start();
await cipher.encrypt(data);
b.stop();
},
});

bench({
name: "AES-128-CBC (GodCrypto) 2MiB Decrypt",
runs: 5, // takes too long
async func(b) {
const cipher = new GodCryptoAES(key, {
mode: "cbc",
iv: new Uint8Array(16),
});
b.start();
await cipher.decrypt(data);
b.stop();
},
});
Expand Down
59 changes: 39 additions & 20 deletions benchmarks/blowfish.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
import {
bench,
runBenchmarks,
} from "https://deno.land/std@0.74.0/testing/bench.ts";
import { Blowfish } from "../src/blowfish/mod.ts";
import { bench, runBenchmarks } from "../dev_deps.ts";
import { BlowfishCbc, BlowfishEcb } from "../src/blowfish/mod.ts";

const key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
const data = new Uint8Array(1024 * 1024 * 2);

bench({
name: "Blowfish-ECB 2MiB Encrypt",
runs: 50,
func(b) {
const bf = new BlowfishEcb(key);
b.start();
bf.encrypt(data);
b.stop();
},
});

bench({
name: "BF-ECB 2MiB",
name: "Blowfish-ECB 2MiB Decrypt",
runs: 50,
func(b) {
const data = new Uint8Array(1024 * 1024 * 2);
const bf = new Blowfish("abcdefgh");
const bf = new BlowfishEcb(key);
b.start();
bf.decrypt(data);
b.stop();
},
});

bench({
name: "Blowfish-CBC 2MiB Encrypt",
runs: 50,
func(b) {
const bf = new BlowfishCbc(
key,
new Uint8Array(8),
);

b.start();
const enc = bf.encrypt(data);
bf.decrypt(enc);
bf.encrypt(data);
b.stop();
},
});

bench({
name: "BF-CBC 2MiB",
name: "Blowfish-CBC 2MiB Decrypt",
runs: 50,
func(b) {
const data = new Uint8Array(1024 * 1024 * 2);
const bf = new Blowfish(
"abcdefgh",
{
mode: Blowfish.MODE.CBC,
iv: new Uint8Array([10, 20, 30, 40, 50, 60, 70, 80]),
},
const bf = new BlowfishCbc(
key,
new Uint8Array(8),
);
b.start();
const enc = bf.encrypt(data);
bf.decrypt(enc);
bf.decrypt(data);
b.stop();
},
});
Expand Down
2 changes: 2 additions & 0 deletions blowfish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { BlowfishCbc, BlowfishEcb } from "./src/blowfish/mod.ts";
export { Padding } from "./src/utils/padding.ts";
8 changes: 8 additions & 0 deletions dev_deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
bench,
runBenchmarks,
} from "https://deno.land/std@0.90.0/testing/bench.ts";
export {
assertEquals,
assertThrows,
} from "https://deno.land/std@0.90.0/testing/asserts.ts";
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Blowfish } from "./src/blowfish/mod.ts";
export { AES } from "./src/aes/mod.ts";
export { BlowfishCbc, BlowfishEcb } from "./blowfish.ts";
export { AesCbc, AesEcb } from "./aes.ts";
export { Padding } from "./src/utils/padding.ts";
Loading

0 comments on commit 5288fa3

Please sign in to comment.