| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import re |
| 4 |
|
| 5 |
import attr |
| 6 |
|
| 7 |
from cryptodatahub.common.utils import name_to_enum_item_name |
| 8 |
|
| 9 |
|
| 10 |
@attr.s(frozen=True) |
| 11 |
class OqsParameterSet: |
| 12 |
identifier = attr.ib(validator=attr.validators.instance_of(str)) |
| 13 |
name = attr.ib(validator=attr.validators.instance_of(str)) |
| 14 |
encapsulation_key_size = attr.ib(validator=attr.validators.instance_of(int)) |
| 15 |
decapsulation_key_size = attr.ib(validator=attr.validators.instance_of(int)) |
| 16 |
ciphertext_size = attr.ib(validator=attr.validators.instance_of(int)) |
| 17 |
shared_secret_size = attr.ib(validator=attr.validators.instance_of(int)) |
| 18 |
security_category = attr.ib(validator=attr.validators.instance_of(int)) |
| 19 |
|
| 20 |
|
| 21 |
SIZE_FIELDS = ( |
| 22 |
'encapsulation_key_size', 'decapsulation_key_size', 'ciphertext_size', 'shared_secret_size', |
| 23 |
) |
| 24 |
_SIZE_FIELD_BY_DATASHEET_FIELD = { |
| 25 |
'encapsulation_key_size': 'length-public-key', |
| 26 |
'decapsulation_key_size': 'length-secret-key', |
| 27 |
'ciphertext_size': 'length-ciphertext', |
| 28 |
'shared_secret_size': 'length-shared-secret', |
| 29 |
} |
| 30 |
_MARKDOWN_COLUMNS_BY_FIELD = { |
| 31 |
'security_category': ('claimed nist security level', 'claimed nist level'), |
| 32 |
'encapsulation_key_size': ('public key size (bytes)',), |
| 33 |
'decapsulation_key_size': ('secret key size (bytes)',), |
| 34 |
'ciphertext_size': ('ciphertext size (bytes)',), |
| 35 |
'shared_secret_size': ('shared secret size (bytes)',), |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
def get_parameter_sets(datasheet): |
| 40 |
"""The parameter sets of a machine readable datasheet, by their own name.""" |
| 41 |
parameter_sets = {} |
| 42 |
for parameter_set in datasheet.get('parameter-sets', []): |
| 43 |
if 'claimed-nist-level' not in parameter_set: |
| 44 |
continue |
| 45 |
if any(field not in parameter_set for field in _SIZE_FIELD_BY_DATASHEET_FIELD.values()): |
| 46 |
continue |
| 47 |
|
| 48 |
parameter_sets[parameter_set['name']] = OqsParameterSet( |
| 49 |
identifier=name_to_enum_item_name(parameter_set['name']), |
| 50 |
name=parameter_set['name'], |
| 51 |
security_category=parameter_set['claimed-nist-level'], |
| 52 |
**{ |
| 53 |
field: parameter_set[datasheet_field] * 8 |
| 54 |
for field, datasheet_field in _SIZE_FIELD_BY_DATASHEET_FIELD.items() |
| 55 |
}, |
| 56 |
) |
| 57 |
|
| 58 |
return parameter_sets |
| 59 |
|
| 60 |
|
| 61 |
def get_nist_round(datasheet): |
| 62 |
nist_round = datasheet.get('nist-round') |
| 63 |
|
| 64 |
return nist_round if isinstance(nist_round, int) else None |
| 65 |
|
| 66 |
|
| 67 |
def get_parameter_sets_of_markdown(markdown): |
| 68 |
"""The same parameter sets out of the markdown form the earlier releases carry. |
| 69 |
|
| 70 |
Those releases state the numbers in a table instead of a machine readable |
| 71 |
file, and the table holds every value a record needs. |
| 72 |
""" |
| 73 |
parameter_sets = {} |
| 74 |
header = None |
| 75 |
for line in markdown.splitlines(): |
| 76 |
if '|' not in line: |
| 77 |
header = None |
| 78 |
continue |
| 79 |
|
| 80 |
cells = [cell.strip() for cell in line.strip().strip('|').split('|')] |
| 81 |
if all(re.fullmatch(r':?-{2,}:?', cell) for cell in cells if cell): |
| 82 |
continue |
| 83 |
|
| 84 |
lowered = [cell.lower() for cell in cells] |
| 85 |
if 'parameter set' in lowered: |
| 86 |
header = lowered |
| 87 |
continue |
| 88 |
|
| 89 |
if header is None or len(cells) != len(header): |
| 90 |
continue |
| 91 |
|
| 92 |
parameter_set = _get_parameter_set_of_row(dict(zip(header, cells))) |
| 93 |
if parameter_set is not None: |
| 94 |
parameter_sets[parameter_set.name] = parameter_set |
| 95 |
|
| 96 |
return parameter_sets |
| 97 |
|
| 98 |
|
| 99 |
def _get_parameter_set_of_row(row): |
| 100 |
name = row.get('parameter set', '').replace('*', '').strip() |
| 101 |
if not name: |
| 102 |
return None |
| 103 |
|
| 104 |
values = {} |
| 105 |
for field, columns in _MARKDOWN_COLUMNS_BY_FIELD.items(): |
| 106 |
cell = next((row[column] for column in columns if column in row), None) |
| 107 |
if cell is None or not re.fullmatch(r'[\d ,]+', cell): |
| 108 |
return None |
| 109 |
|
| 110 |
value = int(cell.replace(',', '').replace(' ', '')) |
| 111 |
values[field] = value * 8 if field in SIZE_FIELDS else value |
| 112 |
|
| 113 |
return OqsParameterSet(identifier=name_to_enum_item_name(name), name=name, **values) |
| 114 |
|
| 115 |
|
| 116 |
def get_nist_round_of_markdown(markdown): |
| 117 |
match = re.search(r'\*\*Version\*\*: NIST Round (\d)', markdown) |
| 118 |
|
| 119 |
return int(match.group(1)) if match else None |