Skip to content

Commit

Permalink
Add isAvailable() procedure.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed May 11, 2024
1 parent 17328f5 commit ccbdf46
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 65 deletions.
2 changes: 1 addition & 1 deletion nimcrypto/sha2/sha2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "."/[sha2_common, sha2_ref, sha2_avx, sha2_avx2, sha2_sha, sha2_neon]
export hash
export Sha2Context, Sha2Implementation, sizeDigest, sizeBlock, name,
sha224, sha256, sha384, sha512, sha512_224, sha512_256, sha2,
cpufeatures
cpufeatures, isAvailable

proc reset*(ctx: var Sha2Context) {.noinit.} =
ctx.length = 0'u64
Expand Down
46 changes: 46 additions & 0 deletions nimcrypto/sha2/sha2_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,49 @@ func getImplementation*(ctx: Sha2Context,
"on [aarch64] platform"
else:
Sha2Module.Ref

func isAvailable*(ctx: typedesc[Sha2Context],
implementation: Sha2Implementation,
features: set[CpuFeature]): bool =
## This function returns ``true`` if current combination of ``implementation``
## and CPU ``features`` are available for the specific SHA2 context ``ctx``.
when defined(nimvm):
true
elif defined(amd64):
case implementation
of Sha2Implementation.Auto, Sha2Implementation.Ref:
true
of Sha2Implementation.Avx:
if CpuFeature.AVX in features:
true
else:
false
of Sha2Implementation.Avx2:
if CpuFeature.AVX2 in features:
true
else:
false
of Sha2Implementation.ShaExt:
when ctx.bsize == sha256.sizeBlock():
if CpuFeature.SHA2EXT in features:
true
else:
false
else:
false
elif defined(arm64):
case implementation
of Sha2Implementation.Auto, Sha2Implementation.Ref:
true
of Sha2Implementation.Avx, Sha2Implementation.Avx2:
false
of Sha2Implementation.ShaExt:
when ctx.bsize == sha256.sizeBlock():
if CpuFeature.SHA2EXT in features:
true
else:
false
else:
false
else:
true
12 changes: 1 addition & 11 deletions tests/testhmac.nim
Original file line number Diff line number Diff line change
Expand Up @@ -848,22 +848,12 @@ suite "HMAC Tests":
hctx1.isFullZero() == true
hctx2.isFullZero() == true

proc checkCombination(HashType: typedesc,
implementation: Sha2Implementation,
cpu: set[CpuFeature]): bool =
try:
var ctx: HashType
ctx.init(implementation, cpu)
true
except Defect:
false

let cpuFeatures = getCpuFeatures()
template doSha2Test(description: static[string],
implementation: Sha2Implementation,
HashType: typedesc) =
test description & " test vectors [" & toLower($implementation) & "]":
if not(checkCombination(HashType, implementation, cpuFeatures)):
if not(isAvailable(HashType, implementation, cpuFeatures)):
skip()
else:
var ctx: HMAC[HashType]
Expand Down
14 changes: 2 additions & 12 deletions tests/testkdf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -727,23 +727,13 @@ suite "PBKDF2-HMAC-SHA1/SHA224/256/384/512 tests suite":
else:
raiseAssert "Unknown context!"

proc checkCombination(HashType: typedesc,
implementation: Sha2Implementation,
cpu: set[CpuFeature]): bool =
try:
var ctx: HashType
ctx.init(implementation, cpu)
true
except Defect:
false

let cpuFeatures = getCpuFeatures()

template doSha2KdfTest1(description: static[string],
implementation: Sha2Implementation,
HashType: typedesc) =
test description & " test (1 iteration) [" & toLower($implementation) & "]":
if not(checkCombination(HashType, implementation, cpuFeatures)):
if not(isAvailable(HashType, implementation, cpuFeatures)):
skip()
else:
var
Expand All @@ -766,7 +756,7 @@ suite "PBKDF2-HMAC-SHA1/SHA224/256/384/512 tests suite":
test description & " test (100,000 iteration) [" &
toLower($implementation) & "]":
when defined(release):
if not(checkCombination(HashType, implementation, cpuFeatures)):
if not(isAvailable(HashType, implementation, cpuFeatures)):
skip()
else:
var
Expand Down
20 changes: 5 additions & 15 deletions tests/testscrypt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,11 @@ suite "Scrypt KDF tests suite":
p = 16, keyLen = 32)
check stringarg == openarrayarg

proc checkCombination(HashType: typedesc,
implementation: Sha2Implementation,
cpu: set[CpuFeature]): bool =
try:
var ctx: HashType
ctx.init(implementation, cpu)
true
except Defect:
false

let cpuFeatures = getCpuFeatures()
for implementation in Sha2Implementation:
test "scrypt N=16, r=1, p=1, keyLen=64 [" &
toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
let key = fromHex(
Expand All @@ -218,7 +208,7 @@ suite "Scrypt KDF tests suite":

test "scrypt N=1024, r=8, p=16, keyLen=64 [" &
toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
let key = fromHex(
Expand All @@ -230,7 +220,7 @@ suite "Scrypt KDF tests suite":

test "scrypt N=16384, r=8, p=1, keyLen=64 [" &
toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
let key = fromHex(
Expand All @@ -243,7 +233,7 @@ suite "Scrypt KDF tests suite":

test "scrypt N=1048576, r=8, p=1, keyLen=32 [" &
toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
when defined(cpu64):
Expand All @@ -257,7 +247,7 @@ suite "Scrypt KDF tests suite":

test "scrypt N=1048576, r=8, p=1, keyLen=64 [" &
toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
when defined(cpu64):
Expand Down
42 changes: 16 additions & 26 deletions tests/testsha2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -456,20 +456,10 @@ suite "SHA2 Tests":
test "SHA2-512 million(a) test":
check sha512.millionAtest1() == stripSpaces(digest1ma512)

proc checkCombination(HashType: typedesc,
implementation: Sha2Implementation,
cpu: set[CpuFeature]): bool =
try:
var ctx: HashType
ctx.init(implementation, cpu)
true
except Defect:
false

let cpuFeatures = getCpuFeatures()
for implementation in Sha2Implementation:
test "SHA2-224 test vectors [" & toLower($implementation) & "]":
if not(checkCombination(sha224, implementation, cpuFeatures)):
if not(isAvailable(sha224, implementation, cpuFeatures)):
skip()
else:
for i in 0 ..< len(code224):
Expand Down Expand Up @@ -507,7 +497,7 @@ suite "SHA2 Tests":
ctx224.isFullZero() == true

test "SHA2-224 empty update() test [" & toLower($implementation) & "]":
if not(checkCombination(sha224, implementation, cpuFeatures)):
if not(isAvailable(sha224, implementation, cpuFeatures)):
skip()
else:
var data: seq[byte]
Expand All @@ -525,7 +515,7 @@ suite "SHA2 Tests":
ctx2.finish().data == edigest

test "SHA2-256 test vectors [" & toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
for i in 0 ..< len(code256):
Expand Down Expand Up @@ -559,7 +549,7 @@ suite "SHA2 Tests":
ctx256.isFullZero() == true

test "SHA2-256 empty update() test [" & toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
var data: seq[byte]
Expand All @@ -577,7 +567,7 @@ suite "SHA2 Tests":
ctx2.finish().data == edigest

test "SHA2-384 test vectors [" & toLower($implementation) & "]":
if not(checkCombination(sha384, implementation, cpuFeatures)):
if not(isAvailable(sha384, implementation, cpuFeatures)):
skip()
else:
for i in 0 ..< len(code384):
Expand Down Expand Up @@ -612,7 +602,7 @@ suite "SHA2 Tests":
ctx384.isFullZero() == true

test "SHA2-384 empty update() test [" & toLower($implementation) & "]":
if not(checkCombination(sha384, implementation, cpuFeatures)):
if not(isAvailable(sha384, implementation, cpuFeatures)):
skip()
else:
var data: seq[byte]
Expand All @@ -630,7 +620,7 @@ suite "SHA2 Tests":
ctx2.finish().data == edigest

test "SHA2-512 test vectors [" & toLower($implementation) & "]":
if not(checkCombination(sha512, implementation, cpuFeatures)):
if not(isAvailable(sha512, implementation, cpuFeatures)):
skip()
else:
for i in 0 ..< len(code512):
Expand Down Expand Up @@ -664,7 +654,7 @@ suite "SHA2 Tests":
ctx512.isFullZero() == true

test "SHA2-512 empty update() test [" & toLower($implementation) & "]":
if not(checkCombination(sha512, implementation, cpuFeatures)):
if not(isAvailable(sha512, implementation, cpuFeatures)):
skip()
else:
var data: seq[byte]
Expand All @@ -682,7 +672,7 @@ suite "SHA2 Tests":
ctx2.finish().data == edigest

test "SHA2-512/224 test vectors [" & toLower($implementation) & "]":
if not(checkCombination(sha512_224, implementation, cpuFeatures)):
if not(isAvailable(sha512_224, implementation, cpuFeatures)):
skip()
else:
for i in 0 ..< len(code512_224):
Expand All @@ -709,7 +699,7 @@ suite "SHA2 Tests":
ctx512_224.isFullZero() == true

test "SHA2-512/224 empty update() test [" & toLower($implementation) & "]":
if not(checkCombination(sha512_224, implementation, cpuFeatures)):
if not(isAvailable(sha512_224, implementation, cpuFeatures)):
skip()
else:
var data: seq[byte]
Expand All @@ -727,7 +717,7 @@ suite "SHA2 Tests":
ctx2.finish().data == edigest

test "SHA2-512/256 test vectors [" & toLower($implementation) & "]":
if not(checkCombination(sha512_256, implementation, cpuFeatures)):
if not(isAvailable(sha512_256, implementation, cpuFeatures)):
skip()
else:
for i in 0 ..< len(code512_256):
Expand All @@ -754,7 +744,7 @@ suite "SHA2 Tests":
ctx512_256.isFullZero() == true

test "SHA2-512/256 empty update() test [" & toLower($implementation) & "]":
if not(checkCombination(sha512_256, implementation, cpuFeatures)):
if not(isAvailable(sha512_256, implementation, cpuFeatures)):
skip()
else:
var data: seq[byte]
Expand All @@ -779,25 +769,25 @@ suite "SHA2 Tests":
$ctx.finish()

test "SHA2-224 million(a) test [" & toLower($implementation) & "]":
if not(checkCombination(sha224, implementation, cpuFeatures)):
if not(isAvailable(sha224, implementation, cpuFeatures)):
skip()
else:
check sha224.millionAtest2() == stripSpaces(digest1ma224)

test "SHA2-256 million(a) test [" & toLower($implementation) & "]":
if not(checkCombination(sha256, implementation, cpuFeatures)):
if not(isAvailable(sha256, implementation, cpuFeatures)):
skip()
else:
check sha256.millionAtest2() == stripSpaces(digest1ma256)

test "SHA2-384 million(a) test [" & toLower($implementation) & "]":
if not(checkCombination(sha384, implementation, cpuFeatures)):
if not(isAvailable(sha384, implementation, cpuFeatures)):
skip()
else:
check sha384.millionAtest2() == stripSpaces(digest1ma384)

test "SHA2-512 million(a) test [" & toLower($implementation) & "]":
if not(checkCombination(sha512, implementation, cpuFeatures)):
if not(isAvailable(sha512, implementation, cpuFeatures)):
skip()
else:
check sha512.millionAtest2() == stripSpaces(digest1ma512)

0 comments on commit ccbdf46

Please sign in to comment.