Formating fixes

This commit is contained in:
Johannes Loher 2016-02-28 14:25:58 +01:00
parent 753a0cd366
commit 8c203bd412
6 changed files with 76 additions and 73 deletions

View file

@ -1,7 +1,7 @@
module actions; module actions;
import std.stdio; import std.stdio : File, stdin, stdout, writeln;
import std.base64: Base64; import std.base64 : Base64;
import std.random : Random, uniform; import std.random : Random, uniform;
import std.algorithm : joiner; import std.algorithm : joiner;
@ -17,24 +17,24 @@ void encrypt(string keyFileName, Cipher cipher, bool armor)
{ {
auto key = loadKey(keyFileName, armor); auto key = loadKey(keyFileName, armor);
ubyte[8] nonce; ubyte[8] nonce;
if(armor) if (armor)
{ {
ubyte[] buf; ubyte[] buf;
foreach(b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher)) foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
{ {
buf ~= [b]; buf ~= [b];
if(buf.length == 57) if (buf.length == 57)
{ {
stdout.writeln(Base64.encode(buf)); stdout.writeln(Base64.encode(buf));
buf = []; buf = [];
} }
} }
if(buf !is null) if (buf !is null)
stdout.writeln(Base64.encode(buf)); stdout.writeln(Base64.encode(buf));
} }
else else
{ {
foreach(b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher)) foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
stdout.rawWrite([b]); stdout.rawWrite([b]);
} }
} }
@ -43,17 +43,17 @@ void decrypt(string keyFileName, Cipher cipher, bool armor)
{ {
auto key = loadKey(keyFileName, armor); auto key = loadKey(keyFileName, armor);
ubyte[8] nonce; ubyte[8] nonce;
if(armor) if (armor)
{ {
ubyte[] buf; ubyte[] buf;
foreach(b; Base64.decoder(stdin.byLine).joiner.cipherFunction(key, nonce, cipher)) foreach (b; Base64.decoder(stdin.byLine).joiner.cipherFunction(key, nonce, cipher))
{ {
stdout.rawWrite([b]); stdout.rawWrite([b]);
} }
} }
else else
{ {
foreach(b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher)) foreach (b; stdin.byChunk(chunkSize).joiner.cipherFunction(key, nonce, cipher))
stdout.rawWrite([b]); stdout.rawWrite([b]);
} }
} }
@ -62,7 +62,8 @@ void generateKey(bool armor)
{ {
auto rng = Random(); auto rng = Random();
auto randomDevice = File(randomDeviceName, "r"); auto randomDevice = File(randomDeviceName, "r");
scope(exit) randomDevice.close(); scope (exit)
randomDevice.close();
uint[1] seed; uint[1] seed;
randomDevice.rawRead(seed); randomDevice.rawRead(seed);
rng.seed(seed[0]); rng.seed(seed[0]);
@ -71,7 +72,7 @@ void generateKey(bool armor)
foreach (ref b; key) foreach (ref b; key)
b = uniform!ubyte(rng); b = uniform!ubyte(rng);
if(armor) if (armor)
{ {
writeln(Base64.encode(key)); writeln(Base64.encode(key));
} }
@ -86,11 +87,13 @@ private:
ubyte[32] loadKey(string filename, bool armor) ubyte[32] loadKey(string filename, bool armor)
{ {
auto keyFile = File(filename, "r"); auto keyFile = File(filename, "r");
scope (exit)
keyFile.close();
ubyte[32] key; ubyte[32] key;
if(armor) if (armor)
{ {
ubyte[] tempKey; ubyte[] tempKey;
foreach(line; keyFile.byLine) foreach (line; keyFile.byLine)
tempKey ~= Base64.decode(line); tempKey ~= Base64.decode(line);
key = tempKey; key = tempKey;
} }

View file

@ -1,15 +1,13 @@
import std.getopt; import std.getopt;
import std.string : format; import std.string : format;
import std.stdio: stderr, writeln; import std.stdio : stderr, writeln;
import cipher : Cipher; import cipher : Cipher;
import actions; import actions;
int main(string[] args) int main(string[] args)
{ {
bool[string] actions = [ "genKey" : false, bool[string] actions = ["genKey" : false, "encrypt" : false, "decrypt" : false];
"encrypt" : false,
"decrypt" : false ];
Cipher cipher = Cipher.chacha20; Cipher cipher = Cipher.chacha20;
string keyFileName = "symkey.asc"; string keyFileName = "symkey.asc";
@ -18,8 +16,7 @@ int main(string[] args)
GetoptResult result; GetoptResult result;
try try
{ {
result = getopt( result = getopt(args,
args,
std.getopt.config.bundling, std.getopt.config.bundling,
"gen-key|g", "Generate a new 256 bit key.", &actions["genKey"], "gen-key|g", "Generate a new 256 bit key.", &actions["genKey"],
"encrypt|e", "Encrypt a message.", &actions["encrypt"], "encrypt|e", "Encrypt a message.", &actions["encrypt"],
@ -28,34 +25,34 @@ int main(string[] args)
"key|k", "The file which contains the key (default: %s).".format(keyFileName), &keyFileName, "key|k", "The file which contains the key (default: %s).".format(keyFileName), &keyFileName,
"armor|a", "use ascii-armored I/O.", &armor); "armor|a", "use ascii-armored I/O.", &armor);
} }
catch(Exception e) catch (Exception e)
{ {
stderr.writeln(e.msg); stderr.writeln(e.msg);
return 1; return 1;
} }
size_t numberOfActions; size_t numberOfActions;
foreach(value; actions.values) foreach (value; actions.values)
numberOfActions += value; numberOfActions += value;
if(numberOfActions == 1) if (numberOfActions == 1)
{ {
try try
{ {
if(actions["genKey"]) if (actions["genKey"])
{ {
generateKey(armor); generateKey(armor);
} }
else if(actions["encrypt"]) else if (actions["encrypt"])
{ {
encrypt(keyFileName, cipher, armor); encrypt(keyFileName, cipher, armor);
} }
else if(actions["decrypt"]) else if (actions["decrypt"])
{ {
decrypt(keyFileName, cipher, armor); decrypt(keyFileName, cipher, armor);
} }
} }
catch(Exception e) catch (Exception e)
{ {
stderr.writeln(e.msg); stderr.writeln(e.msg);
return 1; return 1;
@ -66,7 +63,7 @@ int main(string[] args)
result.helpWanted = true; result.helpWanted = true;
} }
if(result.helpWanted) if (result.helpWanted)
printHelp(result.options); printHelp(result.options);
return 0; return 0;

View file

@ -4,7 +4,7 @@ private import std.traits : isUnsigned;
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative; private import std.bitmanip : nativeToLittleEndian, littleEndianToNative;
UIntType rotateLeft(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure @safe UIntType rotateLeft(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure @safe
if(isUnsigned!UIntType) if (isUnsigned!UIntType)
{ {
auto reducedLen = len % (8 * UIntType.sizeof); auto reducedLen = len % (8 * UIntType.sizeof);
// TODO: ensure the compiler does not create different code paths here // TODO: ensure the compiler does not create different code paths here
@ -21,7 +21,7 @@ unittest
} }
UIntType rotateRight(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure @safe UIntType rotateRight(UIntType)(in UIntType val, in size_t len) nothrow @nogc pure @safe
if(isUnsigned!UIntType) if (isUnsigned!UIntType)
{ {
auto reducedLen = len % (8 * UIntType.sizeof); auto reducedLen = len % (8 * UIntType.sizeof);
// TODO: ensure the compiler does not create different code paths here // TODO: ensure the compiler does not create different code paths here

View file

@ -3,7 +3,6 @@ module chacha20;
private import std.string : format; private import std.string : format;
private import std.range : isInputRange, isForwardRange, ElementType; private import std.range : isInputRange, isForwardRange, ElementType;
private import std.array; private import std.array;
private import std.traits : hasElaborateCopyConstructor;
private import bitmanip; private import bitmanip;
@ -12,7 +11,7 @@ public:
// TODO: Check unittests (Use reliable software to check if the results are correct) // TODO: Check unittests (Use reliable software to check if the results are correct)
auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if(isInputRange!R && is(ElementType!R : ubyte)) if (isInputRange!R && is(ElementType!R : ubyte))
{ {
static struct rangeResult static struct rangeResult
{ {
@ -47,7 +46,7 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
{ {
assert(!empty); assert(!empty);
chachaSection.popFront(); chachaSection.popFront();
if(chachaSection.empty) if (chachaSection.empty)
{ {
++count; ++count;
chachaSection = chacha20Exp(key, nonce ~ littleEndianInv(count)); chachaSection = chacha20Exp(key, nonce ~ littleEndianInv(count));
@ -223,8 +222,8 @@ out(result)
} }
body body
{ {
auto x00 = littleEndian(input[0..4]), x01 = littleEndian(input[4..8]), auto x00 = littleEndian(input[00..04]), x01 = littleEndian(input[04..08]),
x02 = littleEndian(input[8..12]), x03 = littleEndian(input[12..16]), x02 = littleEndian(input[08..12]), x03 = littleEndian(input[12..16]),
x04 = littleEndian(input[16..20]), x05 = littleEndian(input[20..24]), x04 = littleEndian(input[16..20]), x05 = littleEndian(input[20..24]),
x06 = littleEndian(input[24..28]), x07 = littleEndian(input[28..32]), x06 = littleEndian(input[24..28]), x07 = littleEndian(input[28..32]),
x08 = littleEndian(input[32..36]), x09 = littleEndian(input[36..40]), x08 = littleEndian(input[32..36]), x09 = littleEndian(input[36..40]),
@ -237,7 +236,7 @@ body
y08 = x08, y09 = x09, y10 = x10, y11 = x11, y08 = x08, y09 = x09, y10 = x10, y11 = x11,
y12 = x12, y13 = x13, y14 = x14, y15 = x15; y12 = x12, y13 = x13, y14 = x14, y15 = x15;
foreach(i; 0..10) foreach (i; 0..10)
{ {
mixin(colRound!(x00, x01, x02, x03, x04, x05, x06, x07, mixin(colRound!(x00, x01, x02, x03, x04, x05, x06, x07,
x08, x09, x10, x11, x12, x13, x14, x15)); x08, x09, x10, x11, x12, x13, x14, x15));
@ -280,7 +279,7 @@ unittest
75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95, 75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95,
225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108, 225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108,
94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186]; 94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186];
foreach(i; 0..1000000) foreach (i; 0..1000000)
test3 = chacha20(test3); test3 = chacha20(test3);
assert(test0 == [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, assert(test0 == [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -330,12 +329,12 @@ unittest
ubyte[] n; ubyte[] n;
key.length = 32; key.length = 32;
n.length = 16; n.length = 16;
foreach(i; 0..16) foreach (i; 0..16)
key[i] = cast(ubyte)(i + 1); key[i] = cast(ubyte)(i + 1);
foreach(i; 16..32) foreach (i; 16..32)
key[i] = cast(ubyte)(i + 200 - 15); key[i] = cast(ubyte)(i + 200 - 15);
foreach(i; 0..16) foreach (i; 0..16)
n[i] = cast(ubyte)(i + 1+ 100); n[i] = cast(ubyte)(i + 1+ 100);
assert(chacha20Exp(key, n) == [ 2, 7, 55,183,240,232, 0,145,207,208,120,131,146, 9,130, 31, assert(chacha20Exp(key, n) == [ 2, 7, 55,183,240,232, 0,145,207,208,120,131,146, 9,130, 31,

View file

@ -1,6 +1,8 @@
module cipher; module cipher;
private import std.range : isInputRange, ElementType, InputRange, ForwardRange, inputRangeObject; private import std.range : isInputRange, isForwardRange, ElementType, InputRange, ForwardRange, inputRangeObject;
private import std.string : format;
private import salsa20; private import salsa20;
private import chacha20; private import chacha20;
@ -12,8 +14,11 @@ enum Cipher
chacha20 chacha20
} }
mixin(cipherFunctionString.format(q{InputRange}, q{isInputRange!R && !(isForwardRange!R)})); mixin(cipherFunctionString.format(q{InputRange},
mixin(cipherFunctionString.format(q{ForwardRange}, q{isForwardRange!R})); q{isInputRange!R && !(isForwardRange!R)}));
mixin(cipherFunctionString.format(q{ForwardRange},
q{isForwardRange!R}));
unittest unittest
{ {
@ -34,9 +39,9 @@ private:
enum cipherFunctionString = q{ enum cipherFunctionString = q{
%s!(ElementType!R) cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher) %s!(ElementType!R) cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
if(is(ElementType!R : ubyte) && %s) if (is(ElementType!R : ubyte) && %s)
{ {
final switch(cipher) final switch (cipher)
{ {
case Cipher.salsa20: case Cipher.salsa20:
return range.salsa20Cipher(key, nonce).inputRangeObject; return range.salsa20Cipher(key, nonce).inputRangeObject;

View file

@ -3,14 +3,13 @@ module salsa20;
private import std.string : format; private import std.string : format;
private import std.range : isInputRange, isForwardRange, ElementType; private import std.range : isInputRange, isForwardRange, ElementType;
private import std.array; private import std.array;
private import std.traits : hasElaborateCopyConstructor;
private import bitmanip; private import bitmanip;
public: public:
auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce) auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if(isInputRange!R && is(ElementType!R : ubyte)) if (isInputRange!R && is(ElementType!R : ubyte))
{ {
static struct rangeResult static struct rangeResult
{ {
@ -45,7 +44,7 @@ auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
{ {
assert(!empty); assert(!empty);
salsaSection.popFront(); salsaSection.popFront();
if(salsaSection.empty) if (salsaSection.empty)
{ {
++count; ++count;
salsaSection = salsa20Exp(key, nonce ~ littleEndianInv(count)); salsaSection = salsa20Exp(key, nonce ~ littleEndianInv(count));
@ -223,8 +222,8 @@ out(result)
} }
body body
{ {
auto x00 = littleEndian(input[0..4]), x01 = littleEndian(input[4..8]), auto x00 = littleEndian(input[00..04]), x01 = littleEndian(input[04..08]),
x02 = littleEndian(input[8..12]), x03 = littleEndian(input[12..16]), x02 = littleEndian(input[08..12]), x03 = littleEndian(input[12..16]),
x04 = littleEndian(input[16..20]), x05 = littleEndian(input[20..24]), x04 = littleEndian(input[16..20]), x05 = littleEndian(input[20..24]),
x06 = littleEndian(input[24..28]), x07 = littleEndian(input[28..32]), x06 = littleEndian(input[24..28]), x07 = littleEndian(input[28..32]),
x08 = littleEndian(input[32..36]), x09 = littleEndian(input[36..40]), x08 = littleEndian(input[32..36]), x09 = littleEndian(input[36..40]),
@ -237,7 +236,7 @@ body
y08 = x08, y09 = x09, y10 = x10, y11 = x11, y08 = x08, y09 = x09, y10 = x10, y11 = x11,
y12 = x12, y13 = x13, y14 = x14, y15 = x15; y12 = x12, y13 = x13, y14 = x14, y15 = x15;
foreach(i; 0..10) foreach (i; 0..10)
{ {
mixin(colRound!(x00, x01, x02, x03, x04, x05, x06, x07, mixin(colRound!(x00, x01, x02, x03, x04, x05, x06, x07,
x08, x09, x10, x11, x12, x13, x14, x15)); x08, x09, x10, x11, x12, x13, x14, x15));
@ -280,7 +279,7 @@ unittest
75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95, 75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95,
225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108, 225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108,
94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186]; 94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186];
foreach(i; 0..1000000) foreach (i; 0..1000000)
test3 = salsa20(test3); test3 = salsa20(test3);
assert(test0 == [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, assert(test0 == [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -330,12 +329,12 @@ unittest
ubyte[] n; ubyte[] n;
key.length = 32; key.length = 32;
n.length = 16; n.length = 16;
foreach(i; 0..16) foreach (i; 0..16)
key[i] = cast(ubyte)(i + 1); key[i] = cast(ubyte)(i + 1);
foreach(i; 16..32) foreach (i; 16..32)
key[i] = cast(ubyte)(i + 200 - 15); key[i] = cast(ubyte)(i + 200 - 15);
foreach(i; 0..16) foreach (i; 0..16)
n[i] = cast(ubyte)(i + 1+ 100); n[i] = cast(ubyte)(i + 1+ 100);
assert(salsa20Exp(key, n) == [ 69, 37, 68, 39, 41, 15,107,193,255,139,122, 6,170,233,217, 98, assert(salsa20Exp(key, n) == [ 69, 37, 68, 39, 41, 15,107,193,255,139,122, 6,170,233,217, 98,