Fixed byte order for count/nonce for chacha and salsa (now it is the same as in the reference implementations)
This commit is contained in:
parent
91df0c722b
commit
f09a006b27
3 changed files with 19 additions and 7 deletions
|
@ -2,7 +2,7 @@ module bitmanip;
|
||||||
|
|
||||||
private import std.traits : isUnsigned;
|
private import std.traits : isUnsigned;
|
||||||
|
|
||||||
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative;
|
private import std.bitmanip : nativeToLittleEndian, littleEndianToNative, nativeToBigEndian, bigEndianToNative;
|
||||||
|
|
||||||
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)
|
||||||
|
@ -49,6 +49,19 @@ body
|
||||||
return littleEndianToNative!uint(buf);
|
return littleEndianToNative!uint(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
alias bigEndianInv = nativeToBigEndian;
|
||||||
|
|
||||||
|
uint bigEndian(in ubyte[] input) @safe pure nothrow @nogc
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(input.length == uint.sizeof);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
ubyte[uint.sizeof] buf = input;
|
||||||
|
return bigEndianToNative!uint(buf);
|
||||||
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
ubyte[] test0 = [0, 0, 0, 0];
|
ubyte[] test0 = [0, 0, 0, 0];
|
||||||
|
|
|
@ -40,8 +40,7 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
|
||||||
assert(!empty);
|
assert(!empty);
|
||||||
if (++chachaCounter == 64)
|
if (++chachaCounter == 64)
|
||||||
{
|
{
|
||||||
chachaSection = chacha20Exp(key, concat!(ubyte[16])(nonce,
|
chachaSection = chacha20Exp(key, concat!(ubyte[16])(bigEndianInv(++count), nonce));
|
||||||
littleEndianInv(++count)));
|
|
||||||
chachaCounter = 0;
|
chachaCounter = 0;
|
||||||
}
|
}
|
||||||
range.popFront();
|
range.popFront();
|
||||||
|
@ -56,8 +55,8 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rangeResult(0UL, range, chacha20Exp(key, concat!(ubyte[16])(nonce,
|
return rangeResult(0UL, range, chacha20Exp(key, concat!(ubyte[16])([0,
|
||||||
littleEndianInv(0UL))));
|
0, 0, 0, 0, 0, 0, 0], nonce)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create more unittests!!!
|
// TODO: Create more unittests!!!
|
||||||
|
|
|
@ -41,7 +41,7 @@ auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
|
||||||
if (++salsaCounter == 64)
|
if (++salsaCounter == 64)
|
||||||
{
|
{
|
||||||
salsaSection = salsa20Exp(key, concat!(ubyte[16])(nonce,
|
salsaSection = salsa20Exp(key, concat!(ubyte[16])(nonce,
|
||||||
littleEndianInv(++count)));
|
bigEndianInv(++count)));
|
||||||
salsaCounter = 0;
|
salsaCounter = 0;
|
||||||
}
|
}
|
||||||
range.popFront();
|
range.popFront();
|
||||||
|
@ -57,7 +57,7 @@ auto salsa20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rangeResult(0UL, range, salsa20Exp(key, concat!(ubyte[16])(nonce,
|
return rangeResult(0UL, range, salsa20Exp(key, concat!(ubyte[16])(nonce,
|
||||||
littleEndianInv(0UL))));
|
[0, 0, 0, 0, 0, 0, 0, 0])));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create more unittests!!!
|
// TODO: Create more unittests!!!
|
||||||
|
|
Loading…
Reference in a new issue