Fixed stupid bug, so the different algos are actually used. However, we should find a better solution than using choose...

This commit is contained in:
Johannes Loher 2016-02-17 02:39:40 +01:00
parent 791e048012
commit 3523f500e6
5 changed files with 48 additions and 36 deletions

View file

@ -1,6 +1,7 @@
module bitmanip; module bitmanip;
private import std.traits : isUnsigned; private import std.traits : isUnsigned;
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)
@ -35,3 +36,26 @@ unittest
assert(test[2].rotateRight(1) == 64); assert(test[2].rotateRight(1) == 64);
assert(test[2].rotateRight(7) == 1); assert(test[2].rotateRight(7) == 1);
} }
alias littleEndianInv = nativeToLittleEndian;
uint littleEndian(in ubyte[] input) @safe pure nothrow @nogc
in
{
assert(input.length == uint.sizeof);
}
body
{
ubyte[uint.sizeof] buf = input;
return littleEndianToNative!uint(buf);
}
unittest
{
ubyte[] test0 = [0, 0, 0, 0];
ubyte[] test1 = [86, 75, 30, 9];
ubyte[] test2 = [255, 255, 255, 250];
assert(littleEndian(test0) == 0x00000000);
assert(littleEndian(test1) == 0x091e4b56);
assert(littleEndian(test2) == 0xfaffffff);
}

View file

@ -3,9 +3,9 @@ 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;
private import endian;
public: public:
@ -67,6 +67,13 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
return copy; return copy;
} }
} }
// We need this, so choose works with, if hasElaborateCopyConstructor!R
static if (hasElaborateCopyConstructor!R)
this(this)
{
}
} }
return rangeResult(range, key, nonce); return rangeResult(range, key, nonce);
} }

View file

@ -1,6 +1,6 @@
module cipher; module cipher;
private import std.range : isInputRange, ElementType; private import std.range : isInputRange, ElementType, chooseAmong;
private import salsa20; private import salsa20;
private import chacha20; private import chacha20;
import std.stdio; import std.stdio;
@ -16,12 +16,7 @@ enum Cipher
auto cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher) auto cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
if(isInputRange!R && is(ElementType!R : ubyte)) if(isInputRange!R && is(ElementType!R : ubyte))
{ {
final switch(cipher) return chooseAmong(cipher,
{ range.salsa20Cipher(key, nonce),
case Cipher.salsa20: range.chacha20Cipher(key, nonce));
return range.salsa20Cipher(key, nonce);
case Cipher.chacha20:
return range.salsa20Cipher(key, nonce);
}
} }

View file

@ -1,24 +1 @@
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative;
alias littleEndianInv = nativeToLittleEndian;
uint littleEndian(in ubyte[] input) @safe pure nothrow @nogc
in
{
assert(input.length == uint.sizeof);
}
body
{
ubyte[uint.sizeof] buf = input;
return littleEndianToNative!uint(buf);
}
unittest
{
ubyte[] test0 = [0, 0, 0, 0];
ubyte[] test1 = [86, 75, 30, 9];
ubyte[] test2 = [255, 255, 255, 250];
assert(littleEndian(test0) == 0x00000000);
assert(littleEndian(test1) == 0x091e4b56);
assert(littleEndian(test2) == 0xfaffffff);
}

View file

@ -3,9 +3,9 @@ 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;
private import endian;
public: public:
@ -65,7 +65,16 @@ auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
return copy; return copy;
} }
} }
// We need this, so choose works with, if hasElaborateCopyConstructor!R
static if (hasElaborateCopyConstructor!R)
this(this)
{
} }
}
return rangeResult(range, key, nonce); return rangeResult(range, key, nonce);
} }