Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/opencrypto/cryptotest.py
Show All 39 Lines | |||||
from glob import iglob | from glob import iglob | ||||
katdir = '/usr/local/share/nist-kat' | katdir = '/usr/local/share/nist-kat' | ||||
def katg(base, glob): | def katg(base, glob): | ||||
assert os.path.exists(os.path.join(katdir, base)), "Please 'pkg install nist-kat'" | assert os.path.exists(os.path.join(katdir, base)), "Please 'pkg install nist-kat'" | ||||
return iglob(os.path.join(katdir, base, glob)) | return iglob(os.path.join(katdir, base, glob)) | ||||
aesmodules = [ 'cryptosoft0', 'aesni0', 'ccr0' ] | aesmodules = [ 'cryptosoft0', 'aesni0', 'ccr0', 'ccp0' ] | ||||
desmodules = [ 'cryptosoft0', ] | desmodules = [ 'cryptosoft0', ] | ||||
shamodules = [ 'cryptosoft0', 'aesni0', 'ccr0' ] | shamodules = [ 'cryptosoft0', 'aesni0', 'ccr0', 'ccp0' ] | ||||
def GenTestCase(cname): | def GenTestCase(cname): | ||||
try: | try: | ||||
crid = cryptodev.Crypto.findcrid(cname) | crid = cryptodev.Crypto.findcrid(cname) | ||||
except IOError: | except IOError: | ||||
return None | return None | ||||
class GendCryptoTestCase(unittest.TestCase): | class GendCryptoTestCase(unittest.TestCase): | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | def runGCM(self, fname, mode): | ||||
if 'FAIL' not in data: | if 'FAIL' not in data: | ||||
pt = data['PT'].decode('hex') | pt = data['PT'].decode('hex') | ||||
ct = data['CT'].decode('hex') | ct = data['CT'].decode('hex') | ||||
if len(iv) != 12: | if len(iv) != 12: | ||||
# XXX - isn't supported | # XXX - isn't supported | ||||
continue | continue | ||||
try: | |||||
c = Crypto(cryptodev.CRYPTO_AES_NIST_GCM_16, | c = Crypto(cryptodev.CRYPTO_AES_NIST_GCM_16, | ||||
cipherkey, | cipherkey, | ||||
mac=self._gmacsizes[len(cipherkey)], | mac=self._gmacsizes[len(cipherkey)], | ||||
mackey=cipherkey, crid=crid) | mackey=cipherkey, crid=crid) | ||||
except EnvironmentError, e: | |||||
# Can't test hashes the driver does not support. | |||||
if e.errno != errno.EOPNOTSUPP: | |||||
raise | |||||
continue | |||||
if mode == 'ENCRYPT': | if mode == 'ENCRYPT': | ||||
rct, rtag = c.encrypt(pt, iv, aad) | rct, rtag = c.encrypt(pt, iv, aad) | ||||
rtag = rtag[:len(tag)] | rtag = rtag[:len(tag)] | ||||
data['rct'] = rct.encode('hex') | data['rct'] = rct.encode('hex') | ||||
data['rtag'] = rtag.encode('hex') | data['rtag'] = rtag.encode('hex') | ||||
self.assertEqual(rct, ct, repr(data)) | self.assertEqual(rct, ct, repr(data)) | ||||
self.assertEqual(rtag, tag, repr(data)) | self.assertEqual(rtag, tag, repr(data)) | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | def runXTS(self, fname, meth): | ||||
ct = data['CT'].decode('hex') | ct = data['CT'].decode('hex') | ||||
if nbits % 128 != 0: | if nbits % 128 != 0: | ||||
# XXX - mark as skipped | # XXX - mark as skipped | ||||
continue | continue | ||||
if swapptct: | if swapptct: | ||||
pt, ct = ct, pt | pt, ct = ct, pt | ||||
# run the fun | # run the fun | ||||
try: | |||||
c = Crypto(meth, cipherkey, crid=crid) | c = Crypto(meth, cipherkey, crid=crid) | ||||
except EnvironmentError, e: | |||||
# Can't test hashes the driver does not support. | |||||
if e.errno != errno.EOPNOTSUPP: | |||||
raise | |||||
continue | |||||
r = curfun(c, pt, iv) | r = curfun(c, pt, iv) | ||||
self.assertEqual(r, ct) | self.assertEqual(r, ct) | ||||
############### | ############### | ||||
##### DES ##### | ##### DES ##### | ||||
############### | ############### | ||||
@unittest.skipIf(cname not in desmodules, 'skipping DES on %s' % (cname)) | @unittest.skipIf(cname not in desmodules, 'skipping DES on %s' % (cname)) | ||||
def test_tdes(self): | def test_tdes(self): | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | def runSHA1HMAC(self, fname): | ||||
self.assertEqual(r[:maclen], mac[:maclen], "Actual: " + \ | self.assertEqual(r[:maclen], mac[:maclen], "Actual: " + \ | ||||
repr(r[:maclen].encode("hex")) + " Expected: " + repr(data)) | repr(r[:maclen].encode("hex")) + " Expected: " + repr(data)) | ||||
return GendCryptoTestCase | return GendCryptoTestCase | ||||
cryptosoft = GenTestCase('cryptosoft0') | cryptosoft = GenTestCase('cryptosoft0') | ||||
aesni = GenTestCase('aesni0') | aesni = GenTestCase('aesni0') | ||||
ccr = GenTestCase('ccr0') | ccr = GenTestCase('ccr0') | ||||
ccp = GenTestCase('ccp0') | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
unittest.main() | unittest.main() |