diff --git a/nimcrypto.nimble b/nimcrypto.nimble index 9e0e664..65088ef 100644 --- a/nimcrypto.nimble +++ b/nimcrypto.nimble @@ -1,6 +1,6 @@ # Package -version = "0.5.2" +version = "0.5.3" author = "Eugene Kabanov" description = "Nim cryptographic library" license = "MIT" diff --git a/nimcrypto/blake2.nim b/nimcrypto/blake2.nim index 3c6506f..55b7bc7 100644 --- a/nimcrypto/blake2.nim +++ b/nimcrypto/blake2.nim @@ -139,8 +139,8 @@ else: B2S_G(v, 3, 4, 9, 14, m[Sigma[n][14]], m[Sigma[n][15]]) proc blake2Transform(ctx: var Blake2bContext, last: bool) {.inline.} = - var v: array[16, uint64] - var m: array[16, uint64] + var v {.noinit.}: array[16, uint64] + var m {.noinit.}: array[16, uint64] when nimvm: for i in 0 ..< 8: @@ -201,8 +201,8 @@ proc blake2Transform(ctx: var Blake2bContext, last: bool) {.inline.} = ctx.h[7] = ctx.h[7] xor (v[7] xor v[7 + 8]) proc blake2Transform(ctx: var Blake2sContext, last: bool) {.inline.} = - var v: array[16, uint32] - var m: array[16, uint32] + var v {.noinit.}: array[16, uint32] + var m {.noinit.}: array[16, uint32] when nimvm: for i in 0 ..< 8: diff --git a/nimcrypto/keccak.nim b/nimcrypto/keccak.nim index 1d2c4fd..f5ad9f2 100644 --- a/nimcrypto/keccak.nim +++ b/nimcrypto/keccak.nim @@ -231,9 +231,10 @@ else: a[0] = a[0] xor RNDC[r] proc keccakTransform(data: var array[200, byte]) {.inline.} = - var bc: array[5, uint64] - var st: array[25, uint64] - var t: uint64 + var + bc {.noinit.}: array[5, uint64] + st {.noinit.}: array[25, uint64] + t: uint64 when nimvm: for i in 0 ..< len(st): diff --git a/nimcrypto/pbkdf2.nim b/nimcrypto/pbkdf2.nim index 4f9ef34..466883e 100644 --- a/nimcrypto/pbkdf2.nim +++ b/nimcrypto/pbkdf2.nim @@ -34,7 +34,6 @@ proc pbkdf2*[T, M, N](ctx: var HMAC[T], password: openarray[M], md: array[int(ctx.sizeDigest), byte] ctr: uint32 glength: int - olength: int bytesWrite: int when not((M is byte) or (M is char)): @@ -43,12 +42,17 @@ proc pbkdf2*[T, M, N](ctx: var HMAC[T], password: openarray[M], when not((N is byte) or (N is char)): {.fatal: "Choosen salt type is not supported!".} + when (sizeof(int) != 8) and (sizeof(int) != 4): + {.fatal: "Choosen architecture is not supported!".} + ctr = 1 glength = 0 - when sizeof(int) == 8: - olength = min(len(output), int(0xFFFF_FFFF)) # 2^32 - 1 - elif sizeof(int) == 4: - olength = len(output) + let olength = + when sizeof(int) == 8: + min(len(output), int(0xFFFF_FFFF)) # 2^32 - 1 + else: + len(output) + while glength < olength: beStore32(counter, 0, ctr) ctx.init(password) @@ -68,7 +72,7 @@ proc pbkdf2*[T, M, N](ctx: var HMAC[T], password: openarray[M], glength = glength + bytesWrite ctr = ctr + 1 ctx.clear() - result = int(glength) + int(glength) proc pbkdf2*[T, M, N](ctx: var HMAC[T], password: openarray[M], salt: openarray[N], c: int, @@ -85,11 +89,9 @@ proc pbkdf2*[T, M, N](ctx: var HMAC[T], password: openarray[M], ## ## Returns number of bytes stored on success, or 0 on error. if outlen == -1: - result = pbkdf2(ctx, password, salt, c, - output.toOpenArray(0, len(output) - 1)) + pbkdf2(ctx, password, salt, c, output.toOpenArray(0, len(output) - 1)) else: - result = pbkdf2(ctx, password, salt, c, - output.toOpenArray(0, outlen)) + pbkdf2(ctx, password, salt, c, output.toOpenArray(0, outlen)) proc pbkdf2*[T, M](hashtype: typedesc, password: openarray[T], salt: openarray[M], c: int, @@ -103,7 +105,9 @@ proc pbkdf2*[T, M](hashtype: typedesc, password: openarray[T], ## ``outlen`` - length of bytes to be stored. ## ## Returns sequence of bytes. + var res: seq[byte] if outlen > 0: var ctx: HMAC[hashtype] - result = newSeq[byte](outlen) - discard pbkdf2(ctx, password, salt, c, result) + res.setLen(outlen) + discard pbkdf2(ctx, password, salt, c, res) + res diff --git a/nimcrypto/rijndael.nim b/nimcrypto/rijndael.nim index 6e34341..d09e260 100644 --- a/nimcrypto/rijndael.nim +++ b/nimcrypto/rijndael.nim @@ -270,7 +270,7 @@ when sizeof(int) == 4: swap8(q[3], q[7]) proc subWord(x: uint32): uint32 = - var q: array[8, uint32] + var q {.noinit.}: array[8, uint32] q[0] = x q[1] = x q[2] = x @@ -461,7 +461,7 @@ when sizeof(int) == 4: proc encrypt*(ctx: RijndaelContext, input: openarray[byte], output: var openarray[byte]) = - var q: array[8, uint32] + var q {.noinit.}: array[8, uint32] q[0] = leLoad32(input, 0) q[2] = leLoad32(input, 4) q[4] = leLoad32(input, 8) @@ -489,7 +489,7 @@ when sizeof(int) == 4: proc decrypt*(ctx: RijndaelContext, input: openarray[byte], output: var openarray[byte]) = - var q: array[8, uint32] + var q {.noinit.}: array[8, uint32] q[0] = leLoad32(input, 0) q[2] = leLoad32(input, 4) q[4] = leLoad32(input, 8) diff --git a/nimcrypto/ripemd.nim b/nimcrypto/ripemd.nim index baa22c1..3498c25 100644 --- a/nimcrypto/ripemd.nim +++ b/nimcrypto/ripemd.nim @@ -475,7 +475,7 @@ proc ripemd128Transform(state: var array[4, uint32], data: openarray[byte]) = bbb = state[1] ccc = state[2] ddd = state[3] - X: array[16, uint32] + X {.noinit.}: array[16, uint32] X[0] = leLoad32(data, 0); X[1] = leLoad32(data, 4) X[2] = leLoad32(data, 8); X[3] = leLoad32(data, 12) @@ -512,7 +512,7 @@ proc ripemd256Transform(state: var array[8, uint32], data: openarray[byte]) = bbb = state[5] ccc = state[6] ddd = state[7] - X: array[16, uint32] + X {.noinit.}: array[16, uint32] X[0] = leLoad32(data, 0); X[1] = leLoad32(data, 4) X[2] = leLoad32(data, 8); X[3] = leLoad32(data, 12) @@ -559,7 +559,7 @@ proc ripemd160Transform(state: var array[5, uint32], ccc = state[2] ddd = state[3] eee = state[4] - X: array[16, uint32] + X {.noinit.}: array[16, uint32] X[0] = leLoad32(data, 0); X[1] = leLoad32(data, 4) X[2] = leLoad32(data, 8); X[3] = leLoad32(data, 12) @@ -602,7 +602,7 @@ proc ripemd320Transform(state: var array[10, uint32], ccc = state[7] ddd = state[8] eee = state[9] - X: array[16, uint32] + X {.noinit.}: array[16, uint32] X[0] = leLoad32(data, 0); X[1] = leLoad32(data, 4) X[2] = leLoad32(data, 8); X[3] = leLoad32(data, 12) diff --git a/nimcrypto/sha.nim b/nimcrypto/sha.nim index fae4cda..3a3570e 100644 --- a/nimcrypto/sha.nim +++ b/nimcrypto/sha.nim @@ -82,7 +82,7 @@ proc sha1Transform[T: bchar](ctx: var Sha1Context, offset: int) {.noinit, inline.} = var A, B, C, D, E: uint32 - arr: array[16, uint32] + arr {.noinit.}: array[16, uint32] A = ctx.h[0] B = ctx.h[1] @@ -227,14 +227,15 @@ proc update*(ctx: var Sha1Context, pbytes: ptr byte, proc finish*(ctx: var Sha1Context, data: var openarray[byte]): uint {.inline, discardable.} = - var onebyte: array[1, byte] - var pad: array[8, byte] + let + one80 = [0x80'u8] + one00 = [0x00'u8] + var + pad {.noinit.}: array[8, byte] beStore64(pad, 0, uint64(ctx.size shl 3)) - onebyte[0] = 0x80'u8 - update(ctx, onebyte) - onebyte[0] = 0x00'u8 + update(ctx, one80) while (ctx.size and 63'u64) != (64 - 8): - update(ctx, onebyte) + update(ctx, one00) update(ctx, pad) if len(data) >= int(ctx.sizeDigest): result = ctx.sizeDigest diff --git a/nimcrypto/sha2.nim b/nimcrypto/sha2.nim index 08d5537..9fa97bf 100644 --- a/nimcrypto/sha2.nim +++ b/nimcrypto/sha2.nim @@ -234,8 +234,9 @@ template ROUND512(a, b, c, d, e, f, g, h, z) = h = t0 + t1 proc sha256Transform(state: var array[8, uint32], data: openarray[byte]) = - var t0, t1: uint32 - var W: array[64, uint32] + var + t0, t1: uint32 + W {.noinit.}: array[64, uint32] W[0] = beLoad32(data, 0); W[1] = beLoad32(data, 4); W[2] = beLoad32(data, 8); W[3] = beLoad32(data, 12) @@ -336,8 +337,9 @@ proc sha256Transform(state: var array[8, uint32], data: openarray[byte]) = state[7] = state[7] + s7 proc sha512Transform(state: var array[8, uint64], data: openarray[byte]) = - var t0, t1: uint64 - var W: array[80, uint64] + var + t0, t1: uint64 + W {.noinit.}: array[80, uint64] W[0] = beLoad64(data, 0); W[1] = beLoad64(data, 8); W[2] = beLoad64(data, 16); W[3] = beLoad64(data, 24)