removed internal globals

This commit is contained in:
Giovanni Petrantoni 2020-02-17 13:00:41 +09:00 committed by Dmitriy Ryajov
parent e0e371994c
commit 93a480e6f3
2 changed files with 18 additions and 11 deletions

View File

@ -44,17 +44,18 @@ proc intoChaChaPolyTag*(s: seq[byte]): ChaChaPolyTag =
# this is reconciled at runtime # this is reconciled at runtime
# we do this in the global scope / module init # we do this in the global scope / module init
# try for the best first template fetchImpl: untyped =
var # try for the best first
chachapoly_native_impl: Poly1305Run = poly1305CtmulqGet() var
chacha_native_impl: Chacha20Run = chacha20Sse2Get() chachapoly_native_impl {.inject.}: Poly1305Run = poly1305CtmulqGet()
chacha_native_impl {.inject.}: Chacha20Run = chacha20Sse2Get()
# fall back if not available # fall back if not available
if chachapoly_native_impl == nil: if chachapoly_native_impl == nil:
chachapoly_native_impl = poly1305CtmulRun chachapoly_native_impl = poly1305CtmulRun
if chacha_native_impl == nil: if chacha_native_impl == nil:
chacha_native_impl = chacha20CtRun chacha_native_impl = chacha20CtRun
proc encrypt*(_: type[ChaChaPoly], proc encrypt*(_: type[ChaChaPoly],
key: var ChaChaPolyKey, key: var ChaChaPolyKey,
@ -62,6 +63,8 @@ proc encrypt*(_: type[ChaChaPoly],
tag: var ChaChaPolyTag, tag: var ChaChaPolyTag,
data: var openarray[byte], data: var openarray[byte],
aad: var openarray[byte]) = aad: var openarray[byte]) =
fetchImpl()
chachapoly_native_impl( chachapoly_native_impl(
addr key[0], addr key[0],
addr nonce[0], addr nonce[0],
@ -79,6 +82,8 @@ proc decrypt*(_: type[ChaChaPoly],
tag: var ChaChaPolyTag, tag: var ChaChaPolyTag,
data: var openarray[byte], data: var openarray[byte],
aad: var openarray[byte]) = aad: var openarray[byte]) =
fetchImpl()
chachapoly_native_impl( chachapoly_native_impl(
addr key[0], addr key[0],
addr nonce[0], addr nonce[0],

View File

@ -46,8 +46,6 @@ const
] ]
Basepoint*: Curve25519Key = [9.byte, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Basepoint*: Curve25519Key = [9.byte, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let defaultBrEc = brEcGetDefault()
proc byteswap*(buf: var Curve25519Key) {.inline.} = proc byteswap*(buf: var Curve25519Key) {.inline.} =
for i in 0..<16: for i in 0..<16:
let let
@ -56,6 +54,8 @@ proc byteswap*(buf: var Curve25519Key) {.inline.} =
buf[31 - i] = x buf[31 - i] = x
proc mul*(_: type[Curve25519], dst: var Curve25519Key, scalar: Curve25519Key, point: Curve25519Key) = proc mul*(_: type[Curve25519], dst: var Curve25519Key, scalar: Curve25519Key, point: Curve25519Key) =
let defaultBrEc = brEcGetDefault()
# The source point is provided in array G (of size Glen bytes); # The source point is provided in array G (of size Glen bytes);
# the multiplication result is written over it. # the multiplication result is written over it.
dst = scalar dst = scalar
@ -74,6 +74,8 @@ proc mul*(_: type[Curve25519], dst: var Curve25519Key, scalar: Curve25519Key, po
assert res == 1 assert res == 1
proc mulgen*(_: type[Curve25519], dst: var Curve25519Key, scalar: Curve25519Key) = proc mulgen*(_: type[Curve25519], dst: var Curve25519Key, scalar: Curve25519Key) =
let defaultBrEc = brEcGetDefault()
block iterate: block iterate:
while true: while true:
block derive: block derive: