]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/opencrypto/cryptotest.py
Run the plain SHA digest tests from NIST.
[FreeBSD/FreeBSD.git] / tests / sys / opencrypto / cryptotest.py
1 #!/usr/local/bin/python2
2 #
3 # Copyright (c) 2014 The FreeBSD Foundation
4 # All rights reserved.
5 #
6 # This software was developed by John-Mark Gurney under
7 # the sponsorship from the FreeBSD Foundation.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1.  Redistributions of source code must retain the above copyright
12 #     notice, this list of conditions and the following disclaimer.
13 # 2.  Redistributions in binary form must reproduce the above copyright
14 #     notice, this list of conditions and the following disclaimer in the
15 #     documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 # SUCH DAMAGE.
28 #
29 # $FreeBSD$
30 #
31
32 from __future__ import print_function
33 import errno
34 import cryptodev
35 import itertools
36 import os
37 import struct
38 import unittest
39 from cryptodev import *
40 from glob import iglob
41
42 katdir = '/usr/local/share/nist-kat'
43
44 def katg(base, glob):
45         assert os.path.exists(katdir), "Please 'pkg install nist-kat'"
46         if not os.path.exists(os.path.join(katdir, base)):
47                 raise unittest.SkipTest("Missing %s test vectors" % (base))
48         return iglob(os.path.join(katdir, base, glob))
49
50 aesmodules = [ 'cryptosoft0', 'aesni0', 'ccr0', 'ccp0' ]
51 desmodules = [ 'cryptosoft0', ]
52 shamodules = [ 'cryptosoft0', 'aesni0', 'ccr0', 'ccp0' ]
53
54 def GenTestCase(cname):
55         try:
56                 crid = cryptodev.Crypto.findcrid(cname)
57         except IOError:
58                 return None
59
60         class GendCryptoTestCase(unittest.TestCase):
61                 ###############
62                 ##### AES #####
63                 ###############
64                 @unittest.skipIf(cname not in aesmodules, 'skipping AES-XTS on %s' % (cname))
65                 def test_xts(self):
66                         for i in katg('XTSTestVectors/format tweak value input - data unit seq no', '*.rsp'):
67                                 self.runXTS(i, cryptodev.CRYPTO_AES_XTS)
68
69                 @unittest.skipIf(cname not in aesmodules, 'skipping AES-CBC on %s' % (cname))
70                 def test_cbc(self):
71                         for i in katg('KAT_AES', 'CBC[GKV]*.rsp'):
72                                 self.runCBC(i)
73
74                 @unittest.skipIf(cname not in aesmodules, 'skipping AES-GCM on %s' % (cname))
75                 def test_gcm(self):
76                         for i in katg('gcmtestvectors', 'gcmEncrypt*'):
77                                 self.runGCM(i, 'ENCRYPT')
78
79                         for i in katg('gcmtestvectors', 'gcmDecrypt*'):
80                                 self.runGCM(i, 'DECRYPT')
81
82                 _gmacsizes = { 32: cryptodev.CRYPTO_AES_256_NIST_GMAC,
83                         24: cryptodev.CRYPTO_AES_192_NIST_GMAC,
84                         16: cryptodev.CRYPTO_AES_128_NIST_GMAC,
85                 }
86                 def runGCM(self, fname, mode):
87                         curfun = None
88                         if mode == 'ENCRYPT':
89                                 swapptct = False
90                                 curfun = Crypto.encrypt
91                         elif mode == 'DECRYPT':
92                                 swapptct = True
93                                 curfun = Crypto.decrypt
94                         else:
95                                 raise RuntimeError('unknown mode: %r' % repr(mode))
96
97                         for bogusmode, lines in cryptodev.KATParser(fname,
98                             [ 'Count', 'Key', 'IV', 'CT', 'AAD', 'Tag', 'PT', ]):
99                                 for data in lines:
100                                         curcnt = int(data['Count'])
101                                         cipherkey = data['Key'].decode('hex')
102                                         iv = data['IV'].decode('hex')
103                                         aad = data['AAD'].decode('hex')
104                                         tag = data['Tag'].decode('hex')
105                                         if 'FAIL' not in data:
106                                                 pt = data['PT'].decode('hex')
107                                         ct = data['CT'].decode('hex')
108
109                                         if len(iv) != 12:
110                                                 # XXX - isn't supported
111                                                 continue
112
113                                         try:
114                                                 c = Crypto(cryptodev.CRYPTO_AES_NIST_GCM_16,
115                                                     cipherkey,
116                                                     mac=self._gmacsizes[len(cipherkey)],
117                                                     mackey=cipherkey, crid=crid,
118                                                     maclen=16)
119                                         except EnvironmentError, e:
120                                                 # Can't test algorithms the driver does not support.
121                                                 if e.errno != errno.EOPNOTSUPP:
122                                                         raise
123                                                 continue
124
125                                         if mode == 'ENCRYPT':
126                                                 try:
127                                                         rct, rtag = c.encrypt(pt, iv, aad)
128                                                 except EnvironmentError, e:
129                                                         # Can't test inputs the driver does not support.
130                                                         if e.errno != errno.EINVAL:
131                                                                 raise
132                                                         continue
133                                                 rtag = rtag[:len(tag)]
134                                                 data['rct'] = rct.encode('hex')
135                                                 data['rtag'] = rtag.encode('hex')
136                                                 self.assertEqual(rct, ct, repr(data))
137                                                 self.assertEqual(rtag, tag, repr(data))
138                                         else:
139                                                 if len(tag) != 16:
140                                                         continue
141                                                 args = (ct, iv, aad, tag)
142                                                 if 'FAIL' in data:
143                                                         self.assertRaises(IOError,
144                                                                 c.decrypt, *args)
145                                                 else:
146                                                         try:
147                                                                 rpt, rtag = c.decrypt(*args)
148                                                         except EnvironmentError, e:
149                                                                 # Can't test inputs the driver does not support.
150                                                                 if e.errno != errno.EINVAL:
151                                                                         raise
152                                                                 continue
153                                                         data['rpt'] = rpt.encode('hex')
154                                                         data['rtag'] = rtag.encode('hex')
155                                                         self.assertEqual(rpt, pt,
156                                                             repr(data))
157
158                 def runCBC(self, fname):
159                         curfun = None
160                         for mode, lines in cryptodev.KATParser(fname,
161                             [ 'COUNT', 'KEY', 'IV', 'PLAINTEXT', 'CIPHERTEXT', ]):
162                                 if mode == 'ENCRYPT':
163                                         swapptct = False
164                                         curfun = Crypto.encrypt
165                                 elif mode == 'DECRYPT':
166                                         swapptct = True
167                                         curfun = Crypto.decrypt
168                                 else:
169                                         raise RuntimeError('unknown mode: %r' % repr(mode))
170
171                                 for data in lines:
172                                         curcnt = int(data['COUNT'])
173                                         cipherkey = data['KEY'].decode('hex')
174                                         iv = data['IV'].decode('hex')
175                                         pt = data['PLAINTEXT'].decode('hex')
176                                         ct = data['CIPHERTEXT'].decode('hex')
177
178                                         if swapptct:
179                                                 pt, ct = ct, pt
180                                         # run the fun
181                                         c = Crypto(cryptodev.CRYPTO_AES_CBC, cipherkey, crid=crid)
182                                         r = curfun(c, pt, iv)
183                                         self.assertEqual(r, ct)
184
185                 def runXTS(self, fname, meth):
186                         curfun = None
187                         for mode, lines in cryptodev.KATParser(fname,
188                             [ 'COUNT', 'DataUnitLen', 'Key', 'DataUnitSeqNumber', 'PT',
189                             'CT' ]):
190                                 if mode == 'ENCRYPT':
191                                         swapptct = False
192                                         curfun = Crypto.encrypt
193                                 elif mode == 'DECRYPT':
194                                         swapptct = True
195                                         curfun = Crypto.decrypt
196                                 else:
197                                         raise RuntimeError('unknown mode: %r' % repr(mode))
198
199                                 for data in lines:
200                                         curcnt = int(data['COUNT'])
201                                         nbits = int(data['DataUnitLen'])
202                                         cipherkey = data['Key'].decode('hex')
203                                         iv = struct.pack('QQ', int(data['DataUnitSeqNumber']), 0)
204                                         pt = data['PT'].decode('hex')
205                                         ct = data['CT'].decode('hex')
206
207                                         if nbits % 128 != 0:
208                                                 # XXX - mark as skipped
209                                                 continue
210                                         if swapptct:
211                                                 pt, ct = ct, pt
212                                         # run the fun
213                                         try:
214                                                 c = Crypto(meth, cipherkey, crid=crid)
215                                                 r = curfun(c, pt, iv)
216                                         except EnvironmentError, e:
217                                                 # Can't test hashes the driver does not support.
218                                                 if e.errno != errno.EOPNOTSUPP:
219                                                         raise
220                                                 continue
221                                         self.assertEqual(r, ct)
222
223                 ###############
224                 ##### DES #####
225                 ###############
226                 @unittest.skipIf(cname not in desmodules, 'skipping DES on %s' % (cname))
227                 def test_tdes(self):
228                         for i in katg('KAT_TDES', 'TCBC[a-z]*.rsp'):
229                                 self.runTDES(i)
230
231                 def runTDES(self, fname):
232                         curfun = None
233                         for mode, lines in cryptodev.KATParser(fname,
234                             [ 'COUNT', 'KEYs', 'IV', 'PLAINTEXT', 'CIPHERTEXT', ]):
235                                 if mode == 'ENCRYPT':
236                                         swapptct = False
237                                         curfun = Crypto.encrypt
238                                 elif mode == 'DECRYPT':
239                                         swapptct = True
240                                         curfun = Crypto.decrypt
241                                 else:
242                                         raise RuntimeError('unknown mode: %r' % repr(mode))
243
244                                 for data in lines:
245                                         curcnt = int(data['COUNT'])
246                                         key = data['KEYs'] * 3
247                                         cipherkey = key.decode('hex')
248                                         iv = data['IV'].decode('hex')
249                                         pt = data['PLAINTEXT'].decode('hex')
250                                         ct = data['CIPHERTEXT'].decode('hex')
251
252                                         if swapptct:
253                                                 pt, ct = ct, pt
254                                         # run the fun
255                                         c = Crypto(cryptodev.CRYPTO_3DES_CBC, cipherkey, crid=crid)
256                                         r = curfun(c, pt, iv)
257                                         self.assertEqual(r, ct)
258
259                 ###############
260                 ##### SHA #####
261                 ###############
262                 @unittest.skipIf(cname not in shamodules, 'skipping SHA on %s' % str(cname))
263                 def test_sha(self):
264                         for i in katg('shabytetestvectors', 'SHA*Msg.rsp'):
265                                 self.runSHA(i)
266
267                 def runSHA(self, fname):
268                         # Skip SHA512_(224|256) tests
269                         if fname.find('SHA512_') != -1:
270                                 return
271
272                         for hashlength, lines in cryptodev.KATParser(fname,
273                             [ 'Len', 'Msg', 'MD' ]):
274                                 # E.g., hashlength will be "L=20" (bytes)
275                                 hashlen = int(hashlength.split("=")[1])
276
277                                 if hashlen == 20:
278                                         alg = cryptodev.CRYPTO_SHA1
279                                 elif hashlen == 28:
280                                         alg = cryptodev.CRYPTO_SHA2_224
281                                 elif hashlen == 32:
282                                         alg = cryptodev.CRYPTO_SHA2_256
283                                 elif hashlen == 48:
284                                         alg = cryptodev.CRYPTO_SHA2_384
285                                 elif hashlen == 64:
286                                         alg = cryptodev.CRYPTO_SHA2_512
287                                 else:
288                                         # Skip unsupported hashes
289                                         # Slurp remaining input in section
290                                         for data in lines:
291                                                 continue
292                                         continue
293
294                                 for data in lines:
295                                         msg = data['Msg'].decode('hex')
296                                         msg = msg[:int(data['Len'])]
297                                         md = data['MD'].decode('hex')
298
299                                         try:
300                                                 c = Crypto(mac=alg, crid=crid,
301                                                     maclen=hashlen)
302                                         except EnvironmentError, e:
303                                                 # Can't test hashes the driver does not support.
304                                                 if e.errno != errno.EOPNOTSUPP:
305                                                         raise
306                                                 continue
307
308                                         _, r = c.encrypt(msg, iv="")
309
310                                         self.assertEqual(r, md, "Actual: " + \
311                                             repr(r.encode("hex")) + " Expected: " + repr(data) + " on " + cname)
312
313                 @unittest.skipIf(cname not in shamodules, 'skipping SHA-HMAC on %s' % str(cname))
314                 def test_sha1hmac(self):
315                         for i in katg('hmactestvectors', 'HMAC.rsp'):
316                                 self.runSHA1HMAC(i)
317
318                 def runSHA1HMAC(self, fname):
319                         for hashlength, lines in cryptodev.KATParser(fname,
320                             [ 'Count', 'Klen', 'Tlen', 'Key', 'Msg', 'Mac' ]):
321                                 # E.g., hashlength will be "L=20" (bytes)
322                                 hashlen = int(hashlength.split("=")[1])
323
324                                 blocksize = None
325                                 if hashlen == 20:
326                                         alg = cryptodev.CRYPTO_SHA1_HMAC
327                                         blocksize = 64
328                                 elif hashlen == 28:
329                                         alg = cryptodev.CRYPTO_SHA2_224_HMAC
330                                         blocksize = 64
331                                 elif hashlen == 32:
332                                         alg = cryptodev.CRYPTO_SHA2_256_HMAC
333                                         blocksize = 64
334                                 elif hashlen == 48:
335                                         alg = cryptodev.CRYPTO_SHA2_384_HMAC
336                                         blocksize = 128
337                                 elif hashlen == 64:
338                                         alg = cryptodev.CRYPTO_SHA2_512_HMAC
339                                         blocksize = 128
340                                 else:
341                                         # Skip unsupported hashes
342                                         # Slurp remaining input in section
343                                         for data in lines:
344                                                 continue
345                                         continue
346
347                                 for data in lines:
348                                         key = data['Key'].decode('hex')
349                                         msg = data['Msg'].decode('hex')
350                                         mac = data['Mac'].decode('hex')
351                                         tlen = int(data['Tlen'])
352
353                                         if len(key) > blocksize:
354                                                 continue
355
356                                         try:
357                                                 c = Crypto(mac=alg, mackey=key,
358                                                     crid=crid, maclen=hashlen)
359                                         except EnvironmentError, e:
360                                                 # Can't test hashes the driver does not support.
361                                                 if e.errno != errno.EOPNOTSUPP:
362                                                         raise
363                                                 continue
364
365                                         _, r = c.encrypt(msg, iv="")
366
367                                         self.assertEqual(r[:tlen], mac, "Actual: " + \
368                                             repr(r.encode("hex")) + " Expected: " + repr(data))
369
370         return GendCryptoTestCase
371
372 cryptosoft = GenTestCase('cryptosoft0')
373 aesni = GenTestCase('aesni0')
374 ccr = GenTestCase('ccr0')
375 ccp = GenTestCase('ccp0')
376
377 if __name__ == '__main__':
378         unittest.main()