git: fdbd0ba75d99 - main - test/sys/opencrypto: Fix NIST KAT parser iterator

From: Kornel Dulęba <kd_at_FreeBSD.org>
Date: Thu, 06 Oct 2022 14:52:50 UTC
The branch main has been updated by kd:

URL: https://cgit.FreeBSD.org/src/commit/?id=fdbd0ba75d99f1909b13a9f9ece5e8c576bd8977

commit fdbd0ba75d99f1909b13a9f9ece5e8c576bd8977
Author:     Kornel Dulęba <kd@FreeBSD.org>
AuthorDate: 2022-10-06 14:20:58 +0000
Commit:     Kornel Dulęba <kd@FreeBSD.org>
CommitDate: 2022-10-06 14:42:31 +0000

    test/sys/opencrypto: Fix NIST KAT parser iterator
    
    When yield a.k.a "generator" iterator is used we need to return all
    data using "yield", before returning from the function.
    Because of that only encryption tests were run for AES-CBC, other modes
    were affected as well.
    Add one more loop to the iterator "next" routine to fix that.
    This unveiled a problem in the GCM AEAD parser logic, which didn't
    correctly handle tests cases with empty plaintext, i.e. AAD only.
    Include the fix in this patch as it's a rather trivial one.
    
    Obtained from:  Semihalf
    Differential Revision: https://reviews.freebsd.org/D36861
---
 tests/sys/opencrypto/cryptodev.py | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/tests/sys/opencrypto/cryptodev.py b/tests/sys/opencrypto/cryptodev.py
index d97a731d37ba..7e9b1f6a9fbb 100644
--- a/tests/sys/opencrypto/cryptodev.py
+++ b/tests/sys/opencrypto/cryptodev.py
@@ -38,6 +38,7 @@ from fcntl import ioctl
 import os
 import platform
 import random
+import re
 import signal
 from struct import pack as _pack
 import sys
@@ -258,7 +259,7 @@ class Crypto:
         caead.op = op
         caead.flags = CRD_F_IV_EXPLICIT
         caead.flags = 0
-        if src is not None and len(src) != 0:
+        if src:
             src = str_to_ascii(src)
             caead.len = len(src)
             s = array.array('B', src)
@@ -287,7 +288,7 @@ class Crypto:
 
         ioctl(_cryptodev, CIOCCRYPTAEAD, bytes(caead))
 
-        if src is not None:
+        if src:
             s = array_tobytes(s)
         else:
             s = empty_bytes()
@@ -358,6 +359,7 @@ class KATParser:
         self._pending = None
         self.fname = fname
         self.fp = None
+        self.field_re = re.compile(r"\[(?P<field>[^]]+)\]")
 
     def __enter__(self):
         self.fp = open(self.fname)
@@ -372,24 +374,22 @@ class KATParser:
 
     def __next__(self):
         while True:
-            didread = False
-            if self._pending is not None:
-                i = self._pending
-                self._pending = None
-            else:
-                i = self.fp.readline()
-                didread = True
-
-            if didread and not i:
-                return
-
-            if not i.startswith('#') and i.strip():
-                break
+            while True:
+                if self._pending is not None:
+                    i = self._pending
+                    self._pending = None
+                else:
+                    i = self.fp.readline()
+                    if not i:
+                        return
+
+                if not i.startswith('#') and i.strip():
+                    break
 
-        if i[0] == '[':
-            yield i[1:].split(']', 1)[0], self.fielditer()
-        else:
-            raise ValueError('unknown line: %r' % repr(i))
+            matches = self.field_re.match(i)
+            if matches is None:
+                raise ValueError("Unknown line: %r" % (i))
+            yield matches.group("field"), self.fielditer()
 
     def eatblanks(self):
         while True: