Skip to content

Commit

Permalink
feat(block-ciphers): add DES algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
aykxt committed Apr 5, 2021
1 parent 97a6e2a commit 15d408e
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [AES] (Rijndael)
- [Blowfish]
- [CAST5] (CAST-128)
- [DES]
- ECB, CBC, CFB, OFB and CTR [block modes]

### [Message Authentication Code] algorithms (MACs)
Expand Down Expand Up @@ -64,6 +65,7 @@ reviews. **USE AT YOUR OWN RISK**
[AES]: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
[Blowfish]: https://en.wikipedia.org/wiki/Blowfish_(cipher)
[CAST5]: https://en.wikipedia.org/wiki/CAST-128
[DES]: https://en.wikipedia.org/wiki/Data_Encryption_Standard
[Message Authentication Code]: https://en.wikipedia.org/wiki/Message_authentication_code
[HMAC]: https://en.wikipedia.org/wiki/HMAC
[Key Derivation Functions]: https://en.wikipedia.org/wiki/Key_derivation_function
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/aes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { args } from "./utils/benchmarkArgs.ts";
const { runs: _runs, ...opts } = args;
const runs = _runs || 25;

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

Expand Down
1 change: 1 addition & 0 deletions benchmarks/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { args } from "./utils/benchmarkArgs.ts";
import "./aes.ts";
import "./blowfish.ts";
import "./cast5.ts";
import "./des.ts";

const { runs: _, ...opts } = args;

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/blowfish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { args } from "./utils/benchmarkArgs.ts";
const { runs: _runs, ...opts } = args;
const runs = _runs || 25;

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

Expand Down
103 changes: 103 additions & 0 deletions benchmarks/des.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { bench, runBenchmarks } from "../dev_deps.ts";
import { Des } from "../des.ts";
import { Cbc, Cfb, Ctr, Ecb, Ofb } from "../block-modes.ts";
import { args } from "./utils/benchmarkArgs.ts";

const { runs: _runs, ...opts } = args;
const runs = _runs || 25;

const key = new Uint8Array(8);
const iv = new Uint8Array(Des.BLOCK_SIZE);
const data = new Uint8Array(1024 * 1024 * 2);

bench({
name: "DES-ECB 2MiB Encrypt",
runs,
func(b) {
const cipher = new Ecb(Des, key);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "DES-ECB 2MiB Decrypt",
runs,
func(b) {
const cipher = new Ecb(Des, key);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
name: "DES-CBC 2MiB Encrypt",
runs,
func(b) {
const cipher = new Cbc(Des, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "DES-CBC 2MiB Decrypt",
runs,
func(b) {
const cipher = new Cbc(Des, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
name: "DES-CFB 2MiB Encrypt",
runs,
func(b) {
const cipher = new Cfb(Des, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
name: "DES-CFB 2MiB Decrypt",
runs,
func(b) {
const cipher = new Cfb(Des, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

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

bench({
name: "DES-CTR 2MiB Encrypt/Decrypt",
runs,
func(b) {
const cipher = new Ctr(Des, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

if (import.meta.main) {
runBenchmarks(opts);
}
2 changes: 2 additions & 0 deletions des.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Des } from "./src/des/mod.ts";
export type { BlockCipher } from "./src/block-modes/mod.ts";
26 changes: 26 additions & 0 deletions src/des/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// deno-fmt-ignore-file
export const SP1 = [16843776,0,65536,16843780,16842756,66564,4,65536,1024,16843776,16843780,1024,16778244,16842756,16777216,4,1028,16778240,16778240,66560,66560,16842752,16842752,16778244,65540,16777220,16777220,65540,0,1028,66564,16777216,65536,16843780,4,16842752,16843776,16777216,16777216,1024,16842756,65536,66560,16777220,1024,4,16778244,66564,16843780,65540,16842752,16778244,16777220,1028,66564,16843776,1028,16778240,16778240,0,65540,66560,0,16842756];
export const SP2 = [2148565024,2147516416,32768,1081376,1048576,32,2148532256,2147516448,2147483680,2148565024,2148564992,2147483648,2147516416,1048576,32,2148532256,1081344,1048608,2147516448,0,2147483648,32768,1081376,2148532224,1048608,2147483680,0,1081344,32800,2148564992,2148532224,32800,0,1081376,2148532256,1048576,2147516448,2148532224,2148564992,32768,2148532224,2147516416,32,2148565024,1081376,32,32768,2147483648,32800,2148564992,1048576,2147483680,1048608,2147516448,2147483680,1048608,1081344,0,2147516416,32800,2147483648,2148532256,2148565024,1081344];
export const SP3 = [520,134349312,0,134348808,134218240,0,131592,134218240,131080,134217736,134217736,131072,134349320,131080,134348800,520,134217728,8,134349312,512,131584,134348800,134348808,131592,134218248,131584,131072,134218248,8,134349320,512,134217728,134349312,134217728,131080,520,131072,134349312,134218240,0,512,131080,134349320,134218240,134217736,512,0,134348808,134218248,131072,134217728,134349320,8,131592,131584,134217736,134348800,134218248,520,134348800,131592,8,134348808,131584];
export const SP4 = [8396801,8321,8321,128,8396928,8388737,8388609,8193,0,8396800,8396800,8396929,129,0,8388736,8388609,1,8192,8388608,8396801,128,8388608,8193,8320,8388737,1,8320,8388736,8192,8396928,8396929,129,8388736,8388609,8396800,8396929,129,0,0,8396800,8320,8388736,8388737,1,8396801,8321,8321,128,8396929,129,1,8192,8388609,8193,8396928,8388737,8193,8320,8388608,8396801,128,8388608,8192,8396928];
export const SP5 = [256,34078976,34078720,1107296512,524288,256,1073741824,34078720,1074266368,524288,33554688,1074266368,1107296512,1107820544,524544,1073741824,33554432,1074266112,1074266112,0,1073742080,1107820800,1107820800,33554688,1107820544,1073742080,0,1107296256,34078976,33554432,1107296256,524544,524288,1107296512,256,33554432,1073741824,34078720,1107296512,1074266368,33554688,1073741824,1107820544,34078976,1074266368,256,33554432,1107820544,1107820800,524544,1107296256,1107820800,34078720,0,1074266112,1107296256,524544,33554688,1073742080,524288,0,1074266112,34078976,1073742080];
export const SP6 = [536870928,541065216,16384,541081616,541065216,16,541081616,4194304,536887296,4210704,4194304,536870928,4194320,536887296,536870912,16400,0,4194320,536887312,16384,4210688,536887312,16,541065232,541065232,0,4210704,541081600,16400,4210688,541081600,536870912,536887296,16,541065232,4210688,541081616,4194304,16400,536870928,4194304,536887296,536870912,16400,536870928,541081616,4210688,541065216,4210704,541081600,0,541065232,16,16384,541065216,4210704,16384,4194320,536887312,0,541081600,536870912,4194320,536887312];
export const SP7 = [2097152,69206018,67110914,0,2048,67110914,2099202,69208064,69208066,2097152,0,67108866,2,67108864,69206018,2050,67110912,2099202,2097154,67110912,67108866,69206016,69208064,2097154,69206016,2048,2050,69208066,2099200,2,67108864,2099200,67108864,2099200,2097152,67110914,67110914,69206018,69206018,2,2097154,67108864,67110912,2097152,69208064,2050,2099202,69208064,2050,67108866,69208066,69206016,2099200,0,2,69208066,0,2099202,69206016,2048,67108866,67110912,2048,2097154];
export const SP8 = [268439616,4096,262144,268701760,268435456,268439616,64,268435456,262208,268697600,268701760,266240,268701696,266304,4096,64,268697600,268435520,268439552,4160,266240,262208,268697664,268701696,4160,0,0,268697664,268435520,268439552,266304,262144,266304,262144,268701696,4096,64,268697664,4096,266304,268439552,64,268435520,268697600,268697664,268435456,262144,268439616,0,268701760,262208,268435520,268697600,268439552,268439616,0,268701760,266240,266240,4160,4160,262208,268435456,268701696];

export const PC2_0 = [0,4,536870912,536870916,65536,65540,536936448,536936452,512,516,536871424,536871428,66048,66052,536936960,536936964];
export const PC2_1 = [0,1,1048576,1048577,67108864,67108865,68157440,68157441,256,257,1048832,1048833,67109120,67109121,68157696,68157697];
export const PC2_2 = [0,8,2048,2056,16777216,16777224,16779264,16779272,0,8,2048,2056,16777216,16777224,16779264,16779272];
export const PC2_3 = [0,2097152,134217728,136314880,8192,2105344,134225920,136323072,131072,2228224,134348800,136445952,139264,2236416,134356992,136454144];
export const PC2_4 = [0,262144,16,262160,0,262144,16,262160,4096,266240,4112,266256,4096,266240,4112,266256];
export const PC2_5 = [0,1024,32,1056,0,1024,32,1056,33554432,33555456,33554464,33555488,33554432,33555456,33554464,33555488];
export const PC2_6 = [0,268435456,524288,268959744,2,268435458,524290,268959746,0,268435456,524288,268959744,2,268435458,524290,268959746];
export const PC2_7 = [0,65536,2048,67584,536870912,536936448,536872960,536938496,131072,196608,133120,198656,537001984,537067520,537004032,537069568];
export const PC2_8 = [0,262144,0,262144,2,262146,2,262146,33554432,33816576,33554432,33816576,33554434,33816578,33554434,33816578];
export const PC2_9 = [0,268435456,8,268435464,0,268435456,8,268435464,1024,268436480,1032,268436488,1024,268436480,1032,268436488];
export const PC2_10 = [0,32,0,32,1048576,1048608,1048576,1048608,8192,8224,8192,8224,1056768,1056800,1056768,1056800];
export const PC2_11 = [0,16777216,512,16777728,2097152,18874368,2097664,18874880,67108864,83886080,67109376,83886592,69206016,85983232,69206528,85983744];
export const PC2_12 = [0,4096,134217728,134221824,524288,528384,134742016,134746112,16,4112,134217744,134221840,524304,528400,134742032,134746128];
export const PC2_13 = [0,4,256,260,0,4,256,260,1,5,257,261,1,5,257,261];

export const SHIFTS = [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0];
192 changes: 192 additions & 0 deletions src/des/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import type { BlockCipher } from "../block-modes/base.ts";
// deno-fmt-ignore
import { PC2_0, PC2_1, PC2_10, PC2_11, PC2_12, PC2_13, PC2_2, PC2_3, PC2_4, PC2_5, PC2_6, PC2_7, PC2_8, PC2_9, SHIFTS, SP1, SP2, SP3, SP4, SP5, SP6, SP7, SP8 } from "./consts.ts";

/**
* Data Encryption Standard (DES) block cipher.
*
* Note: This is a low level class. Use a block cipher mode to
* encrypt and decrypt data.
*/
export class Des implements BlockCipher {
/**
* The block size of the block cipher in bytes
*/
static readonly BLOCK_SIZE = 8;
#keys = new Uint32Array(32);

constructor(key: Uint8Array) {
if (key.length != 8) {
throw new Error("Invalid key length (must be 8 bytes)");
}

const keyV = new DataView(key.buffer);
let l = keyV.getUint32(0);
let r = keyV.getUint32(4);
let t = (l >>> 4 ^ r) & 0x0f0f0f0f;
r ^= t;
l ^= t << 4;
t = (r >>> 16 ^ l) & 0x0000ffff;
l ^= t;
r ^= t << 16;
t = (l >>> 2 ^ r) & 0x33333333;
r ^= t;
l ^= t << 2;
t = (r >>> 16 ^ l) & 0x0000ffff;
l ^= t;
r ^= t << 16;
t = (l >>> 1 ^ r) & 0x55555555;
r ^= t;
l ^= t << 1;
t = (r >>> 8 ^ l) & 0x00ff00ff;
l ^= t;
r ^= t << 8;
t = (l >>> 1 ^ r) & 0x55555555;
r ^= t;
l ^= t << 1;
t = l << 8 | r >>> 20 & 0x000000f0;
l = r << 24 | r << 8 & 0xff0000 | r >>> 8 & 0xff00 | r >>> 24 & 0xf0;
r = t;

for (let i = 0; i < 32; i += 2) {
if (SHIFTS[i / 2]) {
l = l << 2 | l >>> 26;
r = r << 2 | r >>> 26;
} else {
l = l << 1 | l >>> 27;
r = r << 1 | r >>> 27;
}
l &= -0xf;
r &= -0xf;
const lt = PC2_0[l >>> 28] | PC2_1[(l >>> 24) & 0xf] |
PC2_2[(l >>> 20) & 0xf] | PC2_3[(l >>> 16) & 0xf] |
PC2_4[(l >>> 12) & 0xf] | PC2_5[(l >>> 8) & 0xf] |
PC2_6[(l >>> 4) & 0xf];
const rt = PC2_7[r >>> 28] | PC2_8[(r >>> 24) & 0xf] |
PC2_9[(r >>> 20) & 0xf] | PC2_10[(r >>> 16) & 0xf] |
PC2_11[(r >>> 12) & 0xf] | PC2_12[(r >>> 8) & 0xf] |
PC2_13[(r >>> 4) & 0xf];
t = ((rt >>> 16) ^ lt) & 0x0000ffff;
this.#keys[i] = lt ^ t;
this.#keys[i + 1] = rt ^ (t << 16);
}
}

encryptBlock(data: DataView, offset: number) {
let l = data.getUint32(offset);
let r = data.getUint32(offset + 4);
let t = (l >>> 4 ^ r) & 0x0f0f0f0f;
r ^= t;
l ^= t << 4;
t = (l >>> 16 ^ r) & 0x0000ffff;
r ^= t;
l ^= t << 16;
t = (r >>> 2 ^ l) & 0x33333333;
l ^= t;
r ^= t << 2;
t = (r >>> 8 ^ l) & 0x00ff00ff;
l ^= t;
r ^= t << 8;
t = (l >>> 1 ^ r) & 0x55555555;
r ^= t;
l ^= t << 1;
l = l << 1 | l >>> 31;
r = r << 1 | r >>> 31;

for (let i = 0; i < 32; i += 2) {
const r1 = r ^ this.#keys[i];
const r2 = (r >>> 4 | r << 28) ^ this.#keys[i + 1];
t = l, l = r;
r = t ^ (
SP2[r1 >>> 24 & 0x3f] |
SP4[r1 >>> 16 & 0x3f] |
SP6[r1 >>> 8 & 0x3f] |
SP8[r1 & 0x3f] |
SP1[r2 >>> 24 & 0x3f] |
SP3[r2 >>> 16 & 0x3f] |
SP5[r2 >>> 8 & 0x3f] |
SP7[r2 & 0x3f]
);
}

t = l, l = r, r = t;
l = l >>> 1 | l << 31;
r = r >>> 1 | r << 31;
t = (l >>> 1 ^ r) & 0x55555555;
r ^= t;
l ^= t << 1;
t = (r >>> 8 ^ l) & 0x00ff00ff;
l ^= t;
r ^= t << 8;
t = (r >>> 2 ^ l) & 0x33333333;
l ^= t;
r ^= t << 2;
t = (l >>> 16 ^ r) & 0x0000ffff;
r ^= t;
l ^= t << 16;
t = (l >>> 4 ^ r) & 0x0f0f0f0f;
r ^= t;
l ^= t << 4;
data.setUint32(offset, l);
data.setUint32(offset + 4, r);
}

decryptBlock(data: DataView, offset: number) {
let l = data.getUint32(offset);
let r = data.getUint32(offset + 4);
let t = (l >>> 4 ^ r) & 0x0f0f0f0f;
r ^= t;
l ^= t << 4;
t = (l >>> 16 ^ r) & 0x0000ffff;
r ^= t;
l ^= t << 16;
t = (r >>> 2 ^ l) & 0x33333333;
l ^= t;
r ^= t << 2;
t = (r >>> 8 ^ l) & 0x00ff00ff;
l ^= t;
r ^= t << 8;
t = (l >>> 1 ^ r) & 0x55555555;
r ^= t;
l ^= t << 1;
l = l << 1 | l >>> 31;
r = r << 1 | r >>> 31;

for (let i = 30; i >= 0; i -= 2) {
const r1 = r ^ this.#keys[i];
const r2 = (r >>> 4 | r << 28) ^ this.#keys[i + 1];
t = l, l = r;
r = t ^ (
SP2[r1 >>> 24 & 0x3f] |
SP4[r1 >>> 16 & 0x3f] |
SP6[r1 >>> 8 & 0x3f] |
SP8[r1 & 0x3f] |
SP1[r2 >>> 24 & 0x3f] |
SP3[r2 >>> 16 & 0x3f] |
SP5[r2 >>> 8 & 0x3f] |
SP7[r2 & 0x3f]
);
}

t = l, l = r, r = t;
l = l >>> 1 | l << 31;
r = r >>> 1 | r << 31;
t = (l >>> 1 ^ r) & 0x55555555;
r ^= t;
l ^= t << 1;
t = (r >>> 8 ^ l) & 0x00ff00ff;
l ^= t;
r ^= t << 8;
t = (r >>> 2 ^ l) & 0x33333333;
l ^= t;
r ^= t << 2;
t = (l >>> 16 ^ r) & 0x0000ffff;
r ^= t;
l ^= t << 16;
t = (l >>> 4 ^ r) & 0x0f0f0f0f;
r ^= t;
l ^= t << 4;
data.setInt32(offset, l);
data.setInt32(offset + 4, r);
}
}
26 changes: 26 additions & 0 deletions tests/des.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { assertEquals, decodeHex } from "../dev_deps.ts";
import { Des } from "../des.ts";

Deno.test("[Block Cipher] DES", () => {
const testData = [
["0000000000000000", "0000000000000000", "8CA64DE9C1B123A7"],
["FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "7359B2163E4EDC58"],
["3000000000000000", "1000000000000001", "958E6E627A05557B"],
["1111111111111111", "1111111111111111", "F40379AB9E0EC533"],
["0123456789ABCDEF", "1111111111111111", "17668DFC7292532D"],
["1111111111111111", "0123456789ABCDEF", "8A5AE1F81AB8F2DD"],
["FEDCBA9876543210", "0123456789ABCDEF", "ED39D950FA74BCC4"],
];

for (const [key, plaintext, ciphertext] of testData) {
const des = new Des(decodeHex(key));
const data = decodeHex(plaintext);
const dataView = new DataView(data.buffer);

des.encryptBlock(dataView, 0);
assertEquals(data, decodeHex(ciphertext));

des.decryptBlock(dataView, 0);
assertEquals(data, decodeHex(plaintext));
}
});

0 comments on commit 15d408e

Please sign in to comment.