From 63b782a17a08bfc5e2ded50c0031222a3f32b110 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Fri, 30 Mar 2018 15:05:22 +0300 Subject: [PATCH] Fix issue with block/key/digest sizes. Add tests for this issue. Version 0.2.1. --- nimcrypto.nimble | 2 +- nimcrypto/hmac.nim | 23 +++++++------- nimcrypto/ripemd.nim | 4 +-- nimcrypto/sha2.nim | 6 ++-- tests/testbcmode.nim | 66 ++++++++++++++++++++++++++++++++++++++ tests/testhmac.nim | 46 +++++++++++++++++++++++++++ tests/testkeccak.nim | 72 ++++++++++++++++++++++++++++++++++++++++++ tests/testrijndael.nim | 40 +++++++++++++++++++++++ tests/testripemd.nim | 22 +++++++++++++ tests/testsha2.nim | 42 ++++++++++++++++++++---- tests/testtwofish.nim | 22 +++++++++++++ 11 files changed, 322 insertions(+), 23 deletions(-) diff --git a/nimcrypto.nimble b/nimcrypto.nimble index 017eb40..84f40a3 100644 --- a/nimcrypto.nimble +++ b/nimcrypto.nimble @@ -1,7 +1,7 @@ mode = ScriptMode.Verbose packageName = "nimcrypto" -version = "0.2.0" +version = "0.2.1" author = "Eugene Kabanov" description = "Nim cryptographic library" license = "MIT" diff --git a/nimcrypto/hmac.nim b/nimcrypto/hmac.nim index 5420234..ec2c73a 100644 --- a/nimcrypto/hmac.nim +++ b/nimcrypto/hmac.nim @@ -26,10 +26,11 @@ type opadctx: HashType template sizeBlock*(h: HMAC[Sha2Context]): uint = - uint(h.HashType.bsize) + uint(h.HashType.sizeBlock) + # uint(h.HashType.bsize div 8) template sizeBlock*(h: HMAC[RipemdContext]): uint = - 64'u + uint(h.HashType.sizeBlock) template sizeBlock*(h: HMAC[KeccakContext]): uint = when h.HashType.kind == Keccak or h.HashType.kind == Sha3: @@ -46,9 +47,12 @@ template sizeBlock*(h: HMAC[KeccakContext]): uint = else: {.fatal: "Choosen hash primitive is not yet supported!".} -template sizeDigest*(h: HMAC[Sha2Context]): uint = uint(h.mdctx.bits) -template sizeDigest*(h: HMAC[RipemdContext]): uint = uint(h.mdctx.bits) -template sizeDigest*(h: HMAC[KeccakContext]): uint = uint(h.mdctx.bits) +template sizeDigest*(h: HMAC[Sha2Context]): uint = + uint(h.mdctx.sizeDigest) +template sizeDigest*(h: HMAC[RipemdContext]): uint = + uint(h.mdctx.sizeDigest) +template sizeDigest*(h: HMAC[KeccakContext]): uint = + uint(h.mdctx.sizeDigest) proc init*[T](hmctx: var HMAC[T], key: ptr byte, ulen: uint) = mixin init, update, finish @@ -69,11 +73,9 @@ proc init*[T](hmctx: var HMAC[T], key: ptr byte, ulen: uint) = else: if ulen > 0'u: copyMem(addr k[0], key, ulen) - var i = 0'u - while i < sizeBlock: + for i in 0..