Skip to content

Commit

Permalink
regression: 4002: add NIST hmac vectors
Browse files Browse the repository at this point in the history
Adds NIST HMAC test vectors to regression case 4002 with
CFG_HASH_NIST_VECTORS=y. Only the first test in each group is
used if CFG_HASH_NIST_VECTORS_LIMITED=y.

Without CFG_HASH_NIST_VECTORS=y, CFG_HASH_NIST_VECTORS_LIMITED has no
effect.

Signed-off-by: Olivier Masse <[email protected]>
  • Loading branch information
omasse-linaro committed Oct 11, 2023
1 parent e611a33 commit 62aca5b
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
20 changes: 20 additions & 0 deletions host/xtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ if (CFG_HASH_NIST_VECTORS)
${CMAKE_CURRENT_BINARY_DIR}/${infile}.h)
endmacro(CREATE_NIST_SHA3_VECTORS)

macro(CREATE_NIST_HMAC_VECTORS infile)
add_custom_command(
OUTPUT ${infile}.h
COMMAND ${Python_EXECUTABLE} ${OPTEE_TEST_ROOT_DIR}/scripts/rsp_to_hmac_test.py
--inf ${infile}.rsp
--outf ${CMAKE_CURRENT_BINARY_DIR}/${infile}.h
DEPENDS ${OPTEE_TEST_ROOT_DIR}/scripts/rsp_to_hmac_test.py ${infile}.rsp
DEPENDS download_418c3837d38f249d6668146bd0090db24dd3c02d2e6797e3de33860a387ae4bd
)

set_property(SOURCE regression_4000.c APPEND PROPERTY OBJECT_DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/${infile}.h)
endmacro(CREATE_NIST_HMAC_VECTORS)

DOWNLOAD_FILE(
https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/shs/shabytetestvectors.zip
shabytetestvectors.zip
Expand All @@ -302,6 +316,11 @@ if (CFG_HASH_NIST_VECTORS)
sha-3bytetestvectors.zip
cd07701af2e47f5cc889d642528b4bf11f8b6eb55797c7307a96828ed8d8fc8c
)
DOWNLOAD_FILE(
https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/hmactestvectors.zip
hmactestvectors.zip
418c3837d38f249d6668146bd0090db24dd3c02d2e6797e3de33860a387ae4bd
)

CREATE_NIST_SHA_VECTORS(SHA1LongMsg SHA1)
CREATE_NIST_SHA_VECTORS(SHA1ShortMsg SHA1)
Expand All @@ -321,6 +340,7 @@ if (CFG_HASH_NIST_VECTORS)
CREATE_NIST_SHA3_VECTORS(SHA3_384ShortMsg SHA3_384)
CREATE_NIST_SHA3_VECTORS(SHA3_512LongMsg SHA3_512)
CREATE_NIST_SHA3_VECTORS(SHA3_512ShortMsg SHA3_512)
CREATE_NIST_HMAC_VECTORS(HMAC)
endif()

################################################################################
Expand Down
23 changes: 23 additions & 0 deletions host/xtest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,20 @@ endif
ifeq ($(CFG_HASH_NIST_VECTORS),y)
SHA_NIST_VECTORS = SHA1 SHA224 SHA256 SHA384 SHA512
SHA3_NIST_VECTORS = SHA3_224 SHA3_256 SHA3_384 SHA3_512
HMAC_NIST_VECTORS = HMAC

cleanfiles += $(out-dir)/shabytetestvectors.zip
cleanfiles += $(out-dir)/sha-3bytetestvectors.zip
cleanfiles += $(out-dir)/hmactestvectors.zip
$(out-dir)/shabytetestvectors.zip:
@echo ' DL $@'
$(q)curl https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/shs/shabytetestvectors.zip -o $@
$(out-dir)/sha-3bytetestvectors.zip:
@echo ' DL $@'
$(q)curl https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip -o $@
$(out-dir)/hmactestvectors.zip:
@echo ' DL $@'
$(q)curl https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/hmactestvectors.zip -o $@

define create-nist-sha-vectors
cleanfiles += $(out-dir)/xtest/$(1)$(2).h $(out-dir)/$(1)$(2).rsp
Expand Down Expand Up @@ -361,6 +366,22 @@ $(out-dir)/xtest/$(1)$(2).h: $(out-dir)/$(1)$(2).rsp
$(CURDIR)/regression_4000.c: $(out-dir)/xtest/$(1)$(2).h
endef

define create-nist-hmac-vectors
cleanfiles += $(out-dir)/xtest/$(1).h $(out-dir)/$(1).rsp

$(out-dir)/$(1).rsp: $(out-dir)/hmactestvectors.zip
@echo ' UNZIP $$@'
$(q)unzip -o $$< $$(notdir $$@) -d $$(dir $$@)
$(q)touch $$@

$(out-dir)/xtest/$(1).h: $(out-dir)/$(1).rsp
@echo ' GEN $$@'
$(q)$(PYTHON3) ../../scripts/rsp_to_hmac_test.py --inf $$< --outf $$@ \
$(if $(filter y,$(CFG_HASH_NIST_VECTORS_LIMITED)),--limited)

$(CURDIR)/regression_4000.c: $(out-dir)/xtest/$(1).h
endef

$(foreach v, $(SHA_NIST_VECTORS), $(eval $(call \
create-nist-sha-vectors,$v,LongMsg)))
$(foreach v, $(SHA_NIST_VECTORS), $(eval $(call \
Expand All @@ -369,6 +390,8 @@ $(foreach v, $(SHA3_NIST_VECTORS), $(eval $(call \
create-nist-sha-3-vectors,$v,LongMsg)))
$(foreach v, $(SHA3_NIST_VECTORS), $(eval $(call \
create-nist-sha-3-vectors,$v,ShortMsg)))
$(foreach v, $(HMAC_NIST_VECTORS), $(eval $(call \
create-nist-hmac-vectors,$v)))
endif


Expand Down
3 changes: 3 additions & 0 deletions host/xtest/regression_4000.c
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,9 @@ static const struct xtest_mac_case mac_cases[] = {
#include "CMACVerAES192.h"
#include "CMACVerAES256.h"
#endif
#ifdef CFG_HASH_NIST_VECTORS
#include "HMAC.h"
#endif
};

static void xtest_tee_test_4002(ADBG_Case_t *c)
Expand Down
148 changes: 148 additions & 0 deletions scripts/rsp_to_hmac_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright 2023 NXP
#

limited = False

def to_compound_str(val):
assert len(val) % 2 == 0, "Only even sized values supported"
if len(val) > 0:
import re
a = re.findall('..', val)
b = "(const uint8_t []){"
for s in a:
b += "0x" + s + ", "
b += "}, " + repr(len(val) / 2) + ","
else:
b = "NULL, 0,"
return b


def generate_case(outf, myvars, algo):
if limited and myvars['Count'] != '0':
return

if algo == "SHA1":
# hashsize is 20 bytes
if int(myvars['Tlen']) != 20:
return
# SHA1 maximum key size is 512 bits
if int(myvars['Klen']) > 64:
return
elif algo == "SHA224":
# hashsize is 28 bytes
if int(myvars['Tlen']) != 28:
return
# SHA224 maximum key size is 512 bits
if int(myvars['Klen']) > 64:
return
elif algo == "SHA256":
# hashsize is 32 bytes
if int(myvars['Tlen']) != 32:
return
# SHA256 maximum key size is 1024 bits
if int(myvars['Klen']) > 128:
return
elif algo == "SHA384":
# hashsize is 48 bytes
if int(myvars['Tlen']) != 48:
return
# SHA384 maximum key size is 1024 bits
if int(myvars['Klen']) > 128:
return
elif algo == "SHA512":
# hashsize is 64 bytes
if int(myvars['Tlen']) != 64:
return
# SHA512 maximum key size is 1024 bits
if int(myvars['Klen']) > 128:
return

outf.write('{ TEE_ALG_HMAC_' + algo + ', TEE_TYPE_HMAC_' + algo + ',\n')
outf.write('/* Key */ ' + to_compound_str(myvars['Key']) + '\n')
outf.write('0,\n')
outf.write('/* Msg */ ' + to_compound_str(myvars['Msg']) + '\n')
outf.write('/* Mac */ ' + to_compound_str(myvars['Mac']) + '\n')
outf.write('false },\n')


def get_args():
import argparse

parser = argparse.ArgumentParser()

parser.add_argument('--inf', required=True,
type=argparse.FileType('r'),
help='Name of input RSP file')

parser.add_argument('--outf', required=True,
type=argparse.FileType('w'),
help='Name of output C file')

parser.add_argument('--limited', action="store_true",
help='Only run one test case from each group')

return parser.parse_args()


def main():
import re
global limited
args = get_args()
inf = args.inf
outf = args.outf
myvars = {}

algo = "SHA1"
limited = args.limited

for line in inf:
myl = line.strip()
if len(myl) == 0:
continue
if re.match('^#', myl):
continue
s = re.split('\W+', myl)
s = list(filter(None, s))
if len(s) == 0:
continue
name = s[0]

if name == 'L':
if s[1] == '20':
algo = "SHA1"
elif s[1] == '28':
algo = "SHA224"
elif s[1] == '32':
algo = "SHA256"
elif s[1] == '48':
algo = "SHA384"
elif s[1] == '64':
algo = "SHA512"
continue

if name == 'Count':
if len(myvars) > 1:
generate_case(outf, myvars, algo)
myvars = {}

if len(s) < 2:
myvars[s[0]] = ''
else:
myvars[s[0]] = s[1]

if len(s) < 2:
continue
val = s[1]

if val == '00':
myvars[s[0]] = ''

if len(myvars) > 1:
generate_case(outf, myvars, algo)


if __name__ == "__main__":
main()

0 comments on commit 62aca5b

Please sign in to comment.