Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/opencrypto/cryptodev.py
Show All 32 Lines | ||||||||||||||||||||
import array | import array | |||||||||||||||||||
import binascii | import binascii | |||||||||||||||||||
from fcntl import ioctl | from fcntl import ioctl | |||||||||||||||||||
import os | import os | |||||||||||||||||||
import platform | import platform | |||||||||||||||||||
import random | import random | |||||||||||||||||||
import re | ||||||||||||||||||||
import signal | import signal | |||||||||||||||||||
from struct import pack as _pack | from struct import pack as _pack | |||||||||||||||||||
import sys | import sys | |||||||||||||||||||
import time | import time | |||||||||||||||||||
import dpkt | import dpkt | |||||||||||||||||||
from cryptodevh import * | from cryptodevh import * | |||||||||||||||||||
▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | def _doop(self, op, src, iv, mac=None): | |||||||||||||||||||
return s | return s | |||||||||||||||||||
def _doaead(self, op, src, aad, iv, tag=None): | def _doaead(self, op, src, aad, iv, tag=None): | |||||||||||||||||||
caead = CryptAEAD() | caead = CryptAEAD() | |||||||||||||||||||
caead.ses = self._ses | caead.ses = self._ses | |||||||||||||||||||
caead.op = op | caead.op = op | |||||||||||||||||||
caead.flags = CRD_F_IV_EXPLICIT | caead.flags = CRD_F_IV_EXPLICIT | |||||||||||||||||||
caead.flags = 0 | caead.flags = 0 | |||||||||||||||||||
if src is not None and len(src) != 0: | if src: | |||||||||||||||||||
src = str_to_ascii(src) | src = str_to_ascii(src) | |||||||||||||||||||
caead.len = len(src) | caead.len = len(src) | |||||||||||||||||||
s = array.array('B', src) | s = array.array('B', src) | |||||||||||||||||||
caead.src = caead.dst = s.buffer_info()[0] | caead.src = caead.dst = s.buffer_info()[0] | |||||||||||||||||||
aad = str_to_ascii(aad) | aad = str_to_ascii(aad) | |||||||||||||||||||
caead.aadlen = len(aad) | caead.aadlen = len(aad) | |||||||||||||||||||
saad = array.array('B', aad) | saad = array.array('B', aad) | |||||||||||||||||||
caead.aad = saad.buffer_info()[0] | caead.aad = saad.buffer_info()[0] | |||||||||||||||||||
Show All 12 Lines | def _doaead(self, op, src, aad, iv, tag=None): | |||||||||||||||||||
caead.tag = tag.buffer_info()[0] | caead.tag = tag.buffer_info()[0] | |||||||||||||||||||
ivbuf = array.array('B', iv) | ivbuf = array.array('B', iv) | |||||||||||||||||||
caead.ivlen = len(iv) | caead.ivlen = len(iv) | |||||||||||||||||||
caead.iv = ivbuf.buffer_info()[0] | caead.iv = ivbuf.buffer_info()[0] | |||||||||||||||||||
ioctl(_cryptodev, CIOCCRYPTAEAD, bytes(caead)) | ioctl(_cryptodev, CIOCCRYPTAEAD, bytes(caead)) | |||||||||||||||||||
if src is not None: | if src: | |||||||||||||||||||
ngie: Why not just use a truthiness statement instead? | ||||||||||||||||||||
Done Inline ActionsI wanted to match the condition in line 261. kd: I wanted to match the condition in line 261.
I've changed both of them instead. | ||||||||||||||||||||
s = array_tobytes(s) | s = array_tobytes(s) | |||||||||||||||||||
else: | else: | |||||||||||||||||||
s = empty_bytes() | s = empty_bytes() | |||||||||||||||||||
return s, array_tobytes(tag) | return s, array_tobytes(tag) | |||||||||||||||||||
def perftest(self, op, size, timeo=3): | def perftest(self, op, size, timeo=3): | |||||||||||||||||||
inp = array.array('B', (random.randint(0, 255) for x in range(size))) | inp = array.array('B', (random.randint(0, 255) for x in range(size))) | |||||||||||||||||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | class MismatchError(Exception): | |||||||||||||||||||
pass | pass | |||||||||||||||||||
class KATParser: | class KATParser: | |||||||||||||||||||
def __init__(self, fname, fields): | def __init__(self, fname, fields): | |||||||||||||||||||
self.fields = set(fields) | self.fields = set(fields) | |||||||||||||||||||
self._pending = None | self._pending = None | |||||||||||||||||||
self.fname = fname | self.fname = fname | |||||||||||||||||||
self.fp = None | self.fp = None | |||||||||||||||||||
self.field_re = re.compile(r"\[(?P<field>[^]]+)\]") | ||||||||||||||||||||
def __enter__(self): | def __enter__(self): | |||||||||||||||||||
self.fp = open(self.fname) | self.fp = open(self.fname) | |||||||||||||||||||
return self | return self | |||||||||||||||||||
def __exit__(self, exc_type, exc_value, exc_tb): | def __exit__(self, exc_type, exc_value, exc_tb): | |||||||||||||||||||
if self.fp is not None: | if self.fp is not None: | |||||||||||||||||||
self.fp.close() | self.fp.close() | |||||||||||||||||||
def __iter__(self): | def __iter__(self): | |||||||||||||||||||
return self | return self | |||||||||||||||||||
def __next__(self): | def __next__(self): | |||||||||||||||||||
while True: | while True: | |||||||||||||||||||
didread = False | while True: | |||||||||||||||||||
if self._pending is not None: | if self._pending is not None: | |||||||||||||||||||
i = self._pending | i = self._pending | |||||||||||||||||||
self._pending = None | self._pending = None | |||||||||||||||||||
else: | else: | |||||||||||||||||||
i = self.fp.readline() | i = self.fp.readline() | |||||||||||||||||||
didread = True | if not i: | |||||||||||||||||||
if didread and not i: | ||||||||||||||||||||
return | return | |||||||||||||||||||
if not i.startswith('#') and i.strip(): | if not i.startswith('#') and i.strip(): | |||||||||||||||||||
Not Done Inline Actions
didread is only used once; this logic can be compressed, avoiding the need for an intermediate variable. ngie: `didread` is only used once; this logic can be compressed, avoiding the need for an… | ||||||||||||||||||||
break | break | |||||||||||||||||||
if i[0] == '[': | matches = self.field_re.match(i) | |||||||||||||||||||
yield i[1:].split(']', 1)[0], self.fielditer() | if matches is None: | |||||||||||||||||||
else: | raise ValueError("Unknown line: %r" % (i)) | |||||||||||||||||||
raise ValueError('unknown line: %r' % repr(i)) | yield matches.group("field"), self.fielditer() | |||||||||||||||||||
Not Done Inline ActionsThis seems better expressed with a regular expression, e.g., # `field_re` should be initialized outside the loops for performance reasons. field_re = re.compile(r"\[(?P<field>[^]]+)\]") matches = field_re.match(i) if matches is None: raise ValueError("Unknown line: %r" % (i)) yield matches.group("field"), self.fielditer() ngie: This seems better expressed with a regular expression, e.g.,
```
lang=python
# `field_re`… | ||||||||||||||||||||
def eatblanks(self): | def eatblanks(self): | |||||||||||||||||||
while True: | while True: | |||||||||||||||||||
line = self.fp.readline() | line = self.fp.readline() | |||||||||||||||||||
if line == '': | if line == '': | |||||||||||||||||||
break | break | |||||||||||||||||||
line = line.strip() | line = line.strip() | |||||||||||||||||||
if line: | if line: | |||||||||||||||||||
▲ Show 20 Lines • Show All 336 Lines • Show Last 20 Lines |
Why not just use a truthiness statement instead?