# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/.
import json import os import subprocess
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives import serialization import binascii
# Imports a JSON testvector file. def import_testvector(file): """Import a JSON testvector file and return an array of the contained objects.""" with open(file) as f:
vectors = json.loads(f.read()) return vectors
# Convert a test data string to a hex array. def string_to_hex_array(string): """Convert a string of hex chars to a string representing a C-format array of hex bytes."""
b = bytearray.fromhex(string)
result = '{' + ', '.join("{:#04x}".format(x) for x in b) + '}' return result
# Writes one AES-GCM testvector into C-header format. (Not clang-format conform) class AESGCM(): """Class that provides the generator function for a single AES-GCM test case."""
def format_testcase(self, vector): """Format an AES-GCM testcase object. Return a string in C-header format."""
result = '{{ {},\n'.format(vector['tcId']) for key in ['key', 'msg', 'aad', 'iv']:
result += ' \"{}\",\n'.format(vector[key])
result += ' \"\",\n'
result += ' \"{}\",\n'.format(vector['tag'])
result += ' \"{}\",\n'.format(vector['ct'] + vector['tag'])
result += ' {},\n'.format(str(vector['result'] == 'invalid').lower())
result += ' {}}},\n\n'.format(str('ZeroLengthIv'in vector['flags']).lower())
return result
# Writes one AES-CMAC testvector into C-header format. (Not clang-format conform) class AESCMAC(): """Class that provides the generator function for a single AES-CMAC test case."""
def format_testcase(self, vector): """Format an AES-CMAC testcase object. Return a string in C-header format."""
result = '{{ {},\n'.format(vector['tcId']) for key in ['comment', 'key', 'msg', 'tag']:
result += ' \"{}\",\n'.format(vector[key])
result += ' {}}},\n\n'.format(str(vector['result'] == 'invalid').lower())
return result
# Writes one AES-CBC testvector into C-header format. (Not clang-format conform) class AESCBC(): """Class that provides the generator function for a single AES-CBC test case."""
def format_testcase(self, vector): """Format an AES-CBC testcase object. Return a string in C-header format."""
result = '{{ {},\n'.format(vector['tcId']) for key in ['key', 'msg', 'iv']:
result += ' \"{}\",\n'.format(vector[key])
result += ' \"{}\",\n'.format(vector['ct'])
result += ' {}}},\n\n'.format(str(vector['result'] == 'valid'and len(vector['flags']) == 0).lower())
return result
# Writes one ChaChaPoly testvector into C-header format. (Not clang-format conform) class ChaChaPoly(): """Class that provides the generator function for a single ChaCha test case."""
def format_testcase(self, testcase): """Format an ChaCha testcase object. Return a string in C-header format."""
result = '\n// Comment: {}'.format(testcase['comment'])
result += '\n{{{},\n'.format(testcase['tcId']-1) for key in ['msg', 'aad', 'key', 'iv']:
result += '{},\n'.format(string_to_hex_array(testcase[key]))
ct = testcase['ct'] + testcase['tag']
result += '{},\n'.format(string_to_hex_array(ct))
result += '{},\n'.format(str(testcase['result'] == 'invalid').lower())
result += '{}}},\n'.format(str(testcase['comment'] == 'invalid nonce size').lower())
return result
class DSA():
pub_keys = {} def format_testcase(self, testcase, key, hash_oid, keySize, out_defs):
key_name = "kPubKey" if key in self.pub_keys:
key_name = self.pub_keys[key] else:
key_name += str(len(self.pub_keys))
self.pub_keys[key] = key_name
out_defs.append('static const std::vector ' + key_name + string_to_hex_array(key) + ';\n\n')
result = '\n// Comment: {}'.format(testcase['comment'])
result += '\n// tcID: {}\n'.format(testcase['tcId'])
result += '{{{}, {},\n'.format(hash_oid, testcase['tcId'])
result += '{},\n'.format(string_to_hex_array(testcase['sig']))
result += '{},\n'.format(key_name)
result += '{},\n'.format(string_to_hex_array(testcase['msg']))
valid = testcase['result'] == 'valid'or (testcase['result'] == 'acceptable'and'NoLeadingZero'in testcase['flags'])
result += '{}}},\n'.format(str(valid).lower())
return result
class HKDF(): """Class that provides the generator function for a single HKDF test case."""
def format_testcase(self, vector): """Format an HKDF testcase object. Return a string in C-header format."""
result = '{{ {},\n'.format(vector['tcId']) for key in ['ikm', 'salt', 'info', "okm"]:
result += ' \"{}\",\n'.format(vector[key])
result += ' {},\n'.format(vector['size'])
result += ' {}}},\n\n'.format(str(vector['result'] == 'valid').lower())
result = '\n// Comment: {}'.format(testcase['comment'])
result += '\n// tcID: {}\n'.format(testcase['tcId'])
result += '{{{}, {}, {},\n'.format(hash_oid, mgf_hash, testcase['tcId'])
result += '{},\n'.format(string_to_hex_array(testcase['msg']))
result += '{},\n'.format(string_to_hex_array(testcase['ct']))
result += '{},\n'.format(string_to_hex_array(testcase['label']))
result += '{},\n'.format(key_name)
valid = testcase['result'] == 'valid'
result += '{}}},\n'.format(str(valid).lower())
return result
class HMAC(): """Class that provides the generator function for a single HMAC test case."""
def format_testcase(self, vector): """Format a HMAC testcase object. Return a string in C-header format."""
result = '{{ {},\n'.format(vector['tcId']) for key in ['comment', 'key', 'msg', "tag"]:
result += ' \"{}\",\n'.format(vector[key])
result += ' {}}},\n\n'.format(str(vector['result'] == 'invalid').lower())
return result
def getSha(sha):
s = sha.split("-") return"SEC_OID_SHA" + s[1]
def getMgfSha(sha):
s = sha.split("-") return"CKG_MGF1_SHA" + s[1]
def generate_vectors_file(params): """
Generate and store a .h-file with test vectors for one test.
params -- Dictionary with parameters for test vector generation for the desired test. """
with open(os.path.join(script_dir, params['target']), 'w') as target:
target.write(header) for definition in shared_defs:
target.write(definition)
target.write(vectors_file)
standard_params = { 'includes': ['"testvectors_base/test-structs.h"'], 'license': """/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */ """,
'top_comment': """/* This file is generated from sources in nss/gtests/common/wycheproof
* automatically and should not be touched manually.
* Generation is trigged by calling python3 genTestVectors.py */ """
}
# Parameters that describe the generation of a testvector file for each supoorted test. # source -- relative path to the wycheproof JSON source file with testvectors. # base -- relative path to non-wycheproof vectors. # target -- relative path to where the finished .h-file is written. # array_init -- string to initialize the c-header style array of testvectors. # formatter -- the test case formatter class to be used for this test. # crop_size_end -- number of characters removed from the end of the last generated test vector to close the array definition. # section -- name of the section # comment -- additional comments to add to the file just before definition of the test vector array.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.