Skip to content

Commit

Permalink
refactor!: generic block cipher modes
Browse files Browse the repository at this point in the history
  • Loading branch information
aykxt committed Mar 16, 2021
1 parent 0c20792 commit e0329bf
Show file tree
Hide file tree
Showing 21 changed files with 585 additions and 788 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ A collection of useful crypto algorithms written in Typescript.
## Examples

```ts
import { AesEcb } from "https://deno.land/x/crypto/aes.ts";
import { Aes } from "https://deno.land/x/crypto/aes.ts";
import { Ecb } from "https://deno.land/x/crypto/block-modes.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);
const cipher = new Ecb(Aes, key);

//deno-fmt-ignore
const data = new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
Expand Down
4 changes: 2 additions & 2 deletions aes.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { AesCbc, AesCfb, AesEcb, AesOfb } from "./src/aes/mod.ts";
export { Padding } from "./src/utils/padding.ts";
export { Aes } from "./src/aes/mod.ts";
export type { BlockCipher } from "./src/block-modes/mod.ts";
30 changes: 10 additions & 20 deletions benchmarks/aes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bench, runBenchmarks } from "../dev_deps.ts";
import { AesCbc, AesCfb, AesEcb, AesOfb } from "../src/aes/mod.ts";
import { Aes } from "../aes.ts";
import { Cbc, Cfb, Ecb, Ofb } from "../block-modes.ts";
import { AES as GodCryptoAES } from "https://deno.land/x/god_crypto@v1.4.9/aes.ts";
import { args } from "./utils/benchmarkArgs.ts";

Expand All @@ -15,7 +16,7 @@ bench({
name: "AES-128-ECB 2MiB Encrypt",
runs,
func(b) {
const cipher = new AesEcb(key);
const cipher = new Ecb(Aes, key);
b.start();
cipher.encrypt(data);
b.stop();
Expand All @@ -26,7 +27,7 @@ bench({
name: "AES-128-ECB 2MiB Decrypt",
runs,
func(b) {
const cipher = new AesEcb(key);
const cipher = new Ecb(Aes, key);
b.start();
cipher.decrypt(data);
b.stop();
Expand All @@ -37,7 +38,7 @@ bench({
name: "AES-128-CBC 2MiB Encrypt",
runs,
func(b) {
const cipher = new AesCbc(key, iv);
const cipher = new Cbc(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
Expand All @@ -48,7 +49,7 @@ bench({
name: "AES-128-CBC 2MiB Decrypt",
runs,
func(b) {
const cipher = new AesCbc(key, iv);
const cipher = new Cbc(Aes, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
Expand All @@ -59,7 +60,7 @@ bench({
name: "AES-128-CFB 2MiB Encrypt",
runs,
func(b) {
const cipher = new AesCfb(key, iv);
const cipher = new Cfb(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
Expand All @@ -70,35 +71,24 @@ bench({
name: "AES-128-CFB 2MiB Decrypt",
runs,
func(b) {
const cipher = new AesCfb(key, iv);
const cipher = new Cfb(Aes, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
name: "AES-128-OFB 2MiB Encrypt",
name: "AES-128-OFB 2MiB Encrypt/Decrypt",
runs,
func(b) {
const cipher = new AesOfb(key, iv);
const cipher = new Ofb(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "AES-128-OFB 2MiB Decrypt",
runs,
func(b) {
const cipher = new AesOfb(key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
name: "AES-128-ECB (GodCrypto) 2MiB Encrypt",
runs,
Expand Down
33 changes: 20 additions & 13 deletions benchmarks/blowfish.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { bench, runBenchmarks } from "../dev_deps.ts";
import {
BlowfishCbc,
BlowfishCfb,
BlowfishEcb,
BlowfishOfb,
} from "../src/blowfish/mod.ts";
import { Blowfish } from "../blowfish.ts";
import { Cbc, Cfb, Ecb, Ofb } from "../block-modes.ts";
import { args } from "./utils/benchmarkArgs.ts";

const { runs: _runs, ...opts } = args;
Expand All @@ -18,7 +14,18 @@ bench({
name: "Blowfish-ECB 2MiB Encrypt",
runs,
func(b) {
const cipher = new BlowfishEcb(key);
const cipher = new Ecb(Blowfish, key);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "Blowfish-ECB 2MiB Encrypt",
runs,
func(b) {
const cipher = new Ecb(Blowfish, key);
b.start();
cipher.encrypt(data);
b.stop();
Expand All @@ -29,7 +36,7 @@ bench({
name: "Blowfish-ECB 2MiB Decrypt",
runs,
func(b) {
const cipher = new BlowfishEcb(key);
const cipher = new Ecb(Blowfish, key);
b.start();
cipher.decrypt(data);
b.stop();
Expand All @@ -40,7 +47,7 @@ bench({
name: "Blowfish-CBC 2MiB Encrypt",
runs,
func(b) {
const cipher = new BlowfishCbc(key, iv);
const cipher = new Cbc(Blowfish, key, iv);

b.start();
cipher.encrypt(data);
Expand All @@ -52,7 +59,7 @@ bench({
name: "Blowfish-CBC 2MiB Decrypt",
runs,
func(b) {
const bf = new BlowfishCbc(key, iv);
const bf = new Cbc(Blowfish, key, iv);
b.start();
bf.decrypt(data);
b.stop();
Expand All @@ -63,7 +70,7 @@ bench({
name: "Blowfish-CFB 2MiB Encrypt",
runs,
func(b) {
const cipher = new BlowfishCfb(key, iv);
const cipher = new Cfb(Blowfish, key, iv);

b.start();
cipher.encrypt(data);
Expand All @@ -75,7 +82,7 @@ bench({
name: "Blowfish-CFB 2MiB Decrypt",
runs,
func(b) {
const bf = new BlowfishCfb(key, iv);
const bf = new Cfb(Blowfish, key, iv);
b.start();
bf.decrypt(data);
b.stop();
Expand All @@ -87,7 +94,7 @@ bench({
name: "Blowfish-OFB 2MiB Encrypt/Decrypt",
runs,
func(b) {
const cipher = new BlowfishOfb(key, iv);
const cipher = new Ofb(Blowfish, key, iv);

b.start();
cipher.encrypt(data);
Expand Down
3 changes: 3 additions & 0 deletions block-modes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { BlockCipherMode, Cbc, Cfb, Ecb, Ofb } from "./src/block-modes/mod.ts";
export type { BlockCipher, BlockCipherClass } from "./src/block-modes/mod.ts";
export { Padding } from "./src/utils/padding.ts";
9 changes: 2 additions & 7 deletions blowfish.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
export {
BlowfishCbc,
BlowfishCfb,
BlowfishEcb,
BlowfishOfb,
} from "./src/blowfish/mod.ts";
export { Padding } from "./src/utils/padding.ts";
export { Blowfish } from "./src/blowfish/mod.ts";
export type { BlockCipher } from "./src/block-modes/mod.ts";
146 changes: 0 additions & 146 deletions src/aes/aes.ts

This file was deleted.

Loading

0 comments on commit e0329bf

Please sign in to comment.