1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
# Parse ASCON-AEAD128 test vectors from Ascon-C reference format to OpenSSL format.
#
# Input format:
# Count = N
# Key = ...
# Nonce = ...
# PT = ...
# AD = ...
# CT = ... (Ciphertext + Tag concatenated, where Tag is the last 16 bytes)
# Result = ... (optional; "valid ..." or "invalid ...")
#
# Output format:
# # <Result text> (if Result was present)
# Cipher = ascon-aead128
# Key = ...
# IV = ...
# Plaintext = ...
# AAD = ...
# Tag = ... (last 16 bytes of CT)
# Ciphertext = ... (CT without the last 16 bytes)
# Operation = DECRYPT (only if Result text started with "invalid")
# Result = CIPHERFINAL_ERROR (only if Result text started with "invalid")
# Reason = bad decrypt (only if Result text started with "invalid")
#
# NB "bad decrypt" = PROV_R_BAD_DECRYPT
import hashlib
import os
import re
import sys
def parse_hex_field(value):
"""Parse a hex field value, handling empty strings."""
if not value or value.strip() == '':
return ''
# Remove any whitespace
return value.strip().upper()
def hex_to_bytes(hex_str):
"""Convert hex string to bytes."""
if not hex_str:
return b''
return bytes.fromhex(hex_str)
def bytes_to_hex(byte_data):
"""Convert bytes to hex string (uppercase, no spaces)."""
if not byte_data:
return ''
return byte_data.hex().upper()
def parse_test_vectors(input_file):
"""Parse test vectors from the input file."""
vectors = []
current_vector = {}
with open(input_file, 'r') as f:
for line in f:
line = line.strip()
# Skip comments and empty lines (but empty lines end a vector)
if line.startswith('#'):
continue
if not line:
# Empty line ends current vector
if current_vector:
vectors.append(current_vector)
current_vector = {}
continue
# Parse key-value pairs
match = re.match(r'(\w+)\s*=\s*(.*)', line)
if match:
key = match.group(1).strip()
value = match.group(2).strip()
if key == 'Count':
# Start of new vector
if current_vector:
vectors.append(current_vector)
current_vector = {}
elif key == 'Key':
current_vector['Key'] = parse_hex_field(value)
elif key == 'Nonce':
current_vector['Nonce'] = parse_hex_field(value)
elif key == 'PT':
current_vector['PT'] = parse_hex_field(value)
elif key == 'AD':
current_vector['AD'] = parse_hex_field(value)
elif key == 'CT':
current_vector['CT'] = parse_hex_field(value)
elif key == 'Result':
current_vector['Result'] = value
# Don't forget the last vector if file doesn't end with blank line
if current_vector:
vectors.append(current_vector)
return vectors
def format_output(vectors, input_file):
"""Format vectors in the output format."""
output_lines = []
# Header: sha256 digest and source filename
with open(input_file, 'rb') as f:
digest = hashlib.sha256(f.read()).hexdigest()
basename = os.path.basename(input_file)
output_lines.append(f"# {digest}")
output_lines.append(f"Title = {basename}")
output_lines.append("")
for vec in vectors:
# Get fields with defaults
key = vec.get('Key', '')
nonce = vec.get('Nonce', '')
pt = vec.get('PT', '')
ad = vec.get('AD', '')
ct = vec.get('CT', '')
result = vec.get('Result', '')
# Split CT into Ciphertext (all but last 16 bytes) and Tag (last 16 bytes)
ct_bytes = hex_to_bytes(ct)
if len(ct_bytes) >= 16:
# Tag is the last 16 bytes
tag_bytes = ct_bytes[-16:]
ciphertext_bytes = ct_bytes[:-16]
else:
# If CT is less than 16 bytes, it's all ciphertext, tag is empty
tag_bytes = b''
ciphertext_bytes = ct_bytes
tag_hex = bytes_to_hex(tag_bytes)
ciphertext_hex = bytes_to_hex(ciphertext_bytes)
# Emit comment from Result field if present
if result:
output_lines.append(f"# {result}")
# Format output
output_lines.append("Cipher = ascon-aead128")
output_lines.append(f"Key = {key}")
output_lines.append(f"IV = {nonce}")
output_lines.append(f"Plaintext = {pt}")
output_lines.append(f"AAD = {ad}")
output_lines.append(f"Tag = {tag_hex}")
output_lines.append(f"Ciphertext = {ciphertext_hex}")
# Negative test: Result starts with "invalid"
if result.lower().startswith('invalid'):
output_lines.append("Operation = DECRYPT")
output_lines.append("Result = CIPHERFINAL_ERROR")
output_lines.append("Reason = bad decrypt")
output_lines.append("") # Blank line between vectors
# Footer: test count
output_lines.append(f"# TestCount: {len(vectors)}")
output_lines.append("")
return '\n'.join(output_lines)
def write_to_file(output_file, vectors, input_file):
"""Write parsed vectors to the output file."""
try:
with open(output_file, 'w') as f:
output = format_output(vectors, input_file)
f.write(output)
except IOError as e:
print(f"Error writing to file '{output_file}': {e}", file=sys.stderr)
sys.exit(1)
def main():
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: python3 ascon_aead128_parse.py <input_file> [output_file]", file=sys.stderr)
print(" If output_file is provided, results will be appended to it.", file=sys.stderr)
print(" Otherwise, results will be printed to stdout.", file=sys.stderr)
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) == 3 else None
try:
vectors = parse_test_vectors(input_file)
if output_file:
write_to_file(output_file, vectors, input_file)
else:
output = format_output(vectors, input_file)
print(output)
except FileNotFoundError:
print(f"Error: File '{input_file}' not found", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()
|