aboutsummaryrefslogtreecommitdiff
path: root/src/sw/hash_tester.py
blob: c76c273b13060387287a73fd232a9c5b2079ee87 (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#=======================================================================
#
# hash_tester.py
# --------------
# This program sends several commands to the coretest_hashed subsystem
# in order to verify the SHA-1 and SHA-256 hash function cores.
# The program uses the built in hash implementations in Python
# to do functional comparison and validation.
#
# Note: This program requires the PySerial module.
# http://pyserial.sourceforge.net/
#
# 
# Author: Joachim Strömbergson
# Copyright (c) 2014, SUNET
# 
# Redistribution and use in source and binary forms, with or 
# without modification, are permitted provided that the following 
# conditions are met: 
# 
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
# 
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in 
#    the documentation and/or other materials provided with the 
#    distribution. 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#=======================================================================
 
#-------------------------------------------------------------------
# Python module imports.
#-------------------------------------------------------------------
import sys
import serial
import os
import time
import threading
import hashlib

 
#-------------------------------------------------------------------
# Defines.
#-------------------------------------------------------------------
# Serial port defines.
# CONFIGURE YOUR DEVICE HERE!
SERIAL_DEVICE = '/dev/cu.usbserial-A801SA6T'
BAUD_RATE = 9600
DATA_BITS = 8
STOP_BITS = 1


# Verbose operation on/off
VERBOSE = False


# Memory map.
SOC                   = '\x55'
EOC                   = '\xaa'
READ_CMD              = '\x10'
WRITE_CMD             = '\x11'

UART_ADDR_PREFIX      = '\x00'

SHA1_ADDR_PREFIX      = '\x10'
SHA1_ADDR_NAME0       = '\x00'
SHA1_ADDR_NAME1       = '\x01'
SHA1_ADDR_VERSION     = '\x02'
SHA1_ADDR_CTRL        = '\x08'
SHA1_CTRL_INIT_BIT    = 0
SHA1_CTRL_NEXT_BIT    = 1
SHA1_ADDR_STATUS      = '\x09'
SHA1_STATUS_READY_BIT = 0
SHA1_STATUS_VALID_BIT = 1
SHA1_ADDR_BLOCK0      = '\x10'
SHA1_ADDR_BLOCK1      = '\x11'
SHA1_ADDR_BLOCK2      = '\x12'
SHA1_ADDR_BLOCK3      = '\x13'
SHA1_ADDR_BLOCK4      = '\x14'
SHA1_ADDR_BLOCK5      = '\x15'
SHA1_ADDR_BLOCK6      = '\x16'
SHA1_ADDR_BLOCK7      = '\x17'
SHA1_ADDR_BLOCK8      = '\x18'
SHA1_ADDR_BLOCK9      = '\x19'
SHA1_ADDR_BLOCK10     = '\x1a'
SHA1_ADDR_BLOCK11     = '\x1b'
SHA1_ADDR_BLOCK12     = '\x1c'
SHA1_ADDR_BLOCK13     = '\x1d'
SHA1_ADDR_BLOCK14     = '\x1e'
SHA1_ADDR_BLOCK15     = '\x1f'
SHA1_ADDR_DIGEST0     = '\x20'
SHA1_ADDR_DIGEST1     = '\x21'
SHA1_ADDR_DIGEST2     = '\x22'
SHA1_ADDR_DIGEST3     = '\x23'
SHA1_ADDR_DIGEST4     = '\x24'

SHA256_ADDR_PREFIX      = '\x20'
SHA256_ADDR_NAME0       = '\x00'
SHA256_ADDR_NAME1       = '\x01'
SHA256_ADDR_VERSION     = '\x02'
SHA256_ADDR_CTRL        = '\x08'
SHA256_CTRL_INIT_BIT    = 0
SHA256_CTRL_NEXT_BIT    = 1
SHA256_ADDR_STATUS      = '\x09'
SHA256_STATUS_READY_BIT = 0
SHA256_STATUS_VALID_BIT = 1
SHA256_ADDR_BLOCK0      = '\x10'
SHA256_ADDR_BLOCK1      = '\x11'
SHA256_ADDR_BLOCK2      = '\x12'
SHA256_ADDR_BLOCK3      = '\x13'
SHA256_ADDR_BLOCK4      = '\x14'
SHA256_ADDR_BLOCK5      = '\x15'
SHA256_ADDR_BLOCK6      = '\x16'
SHA256_ADDR_BLOCK7      = '\x17'
SHA256_ADDR_BLOCK8      = '\x18'
SHA256_ADDR_BLOCK9      = '\x19'
SHA256_ADDR_BLOCK10     = '\x1a'
SHA256_ADDR_BLOCK11     = '\x1b'
SHA256_ADDR_BLOCK12     = '\x1c'
SHA256_ADDR_BLOCK13     = '\x1d'
SHA256_ADDR_BLOCK14     = '\x1e'
SHA256_ADDR_BLOCK15     = '\x1f'
SHA256_ADDR_DIGEST0     = '\x20'
SHA256_ADDR_DIGEST1     = '\x21'
SHA256_ADDR_DIGEST2     = '\x22'
SHA256_ADDR_DIGEST3     = '\x23'
SHA256_ADDR_DIGEST4     = '\x24'
SHA256_ADDR_DIGEST5     = '\x25'
SHA256_ADDR_DIGEST6     = '\x26'
SHA256_ADDR_DIGEST7     = '\x27'

NAME0_ADDR            = '\x00'
NAME1_ADDR            = '\x01'
VERSION_ADDR          = '\x02'

sha1_block_addr = [SHA1_ADDR_BLOCK0,  SHA1_ADDR_BLOCK1,
                   SHA1_ADDR_BLOCK2,  SHA1_ADDR_BLOCK3,
                   SHA1_ADDR_BLOCK4,  SHA1_ADDR_BLOCK5,
                   SHA1_ADDR_BLOCK6,  SHA1_ADDR_BLOCK7,
                   SHA1_ADDR_BLOCK8,  SHA1_ADDR_BLOCK9,
                   SHA1_ADDR_BLOCK10, SHA1_ADDR_BLOCK11,
                   SHA1_ADDR_BLOCK12, SHA1_ADDR_BLOCK13,
                   SHA1_ADDR_BLOCK14, SHA1_ADDR_BLOCK15]

sha1_digest_addr = [SHA1_ADDR_DIGEST0, SHA1_ADDR_DIGEST1,
                    SHA1_ADDR_DIGEST2, SHA1_ADDR_DIGEST3,
                    SHA1_ADDR_DIGEST4]

sha256_block_addr = [SHA256_ADDR_BLOCK0,  SHA256_ADDR_BLOCK1,
                     SHA256_ADDR_BLOCK2,  SHA256_ADDR_BLOCK3,
                     SHA256_ADDR_BLOCK4,  SHA256_ADDR_BLOCK5,
                     SHA256_ADDR_BLOCK6,  SHA256_ADDR_BLOCK7,
                     SHA256_ADDR_BLOCK8,  SHA256_ADDR_BLOCK9,
                     SHA256_ADDR_BLOCK10, SHA256_ADDR_BLOCK11,
                     SHA256_ADDR_BLOCK12, SHA256_ADDR_BLOCK13,
                     SHA256_ADDR_BLOCK14, SHA256_ADDR_BLOCK15]

sha256_digest_addr = [SHA256_ADDR_DIGEST0,  SHA256_ADDR_DIGEST1,
                      SHA256_ADDR_DIGEST2,  SHA256_ADDR_DIGEST3,
                      SHA256_ADDR_DIGEST4,  SHA256_ADDR_DIGEST5,
                      SHA256_ADDR_DIGEST6,  SHA256_ADDR_DIGEST7]


#-------------------------------------------------------------------
# print_response()
#
# Parses a received buffer and prints the response.
#-------------------------------------------------------------------
def print_response(buffer):
    if VERBOSE:
        print "Length of response: %d" % len(buffer)
        if buffer[0] == '\xaa':
            print "Response contains correct Start of Response (SOR)"
        if buffer[-1] == '\x55':
            print "Response contains correct End of Response (EOR)"

    response_code = ord(buffer[1])

    if response_code == 0xfe:
        print "UNKNOWN response code received."

    elif response_code == 0xfd:
        print "ERROR response code received."

    elif response_code == 0x7f:
        read_addr = ord(buffer[2]) * 256 + ord(buffer[3])
        read_data = (ord(buffer[4]) * 16777216) + (ord(buffer[5]) * 65536) +\
                    (ord(buffer[6]) * 256) + ord(buffer[7])
        print "READ_OK. address 0x%02x = 0x%08x." % (read_addr, read_data)

    elif response_code == 0x7e:
        read_addr = ord(buffer[2]) * 256 + ord(buffer[3])
        print "WRITE_OK. address 0x%02x." % (read_addr)

    elif response_code == 0x7d:
        print "RESET_OK."

    else:
        print "Response 0x%02x is unknown." % response_code
        print buffer
        

#-------------------------------------------------------------------
# read_serial_thread()
#
# Function used in a thread to read from the serial port and
# collect response from coretest.
#-------------------------------------------------------------------
def read_serial_thread(serialport):
    if VERBOSE:
        print "Serial port response thread started. Waiting for response..."
        
    buffer = []
    while True:
        if serialport.isOpen():
            response = serialport.read()
            buffer.append(response)
            if ((response == '\x55') and len(buffer) > 7):
                print_response(buffer)
                buffer = []
        else:
            print "No open device yet."
            time.sleep(0.1)
            

#-------------------------------------------------------------------
# write_serial_bytes()
#
# Send the bytes in the buffer to coretest over the serial port.
#-------------------------------------------------------------------
def write_serial_bytes(tx_cmd, serialport):
    if VERBOSE:
        print "Command to be sent:", tx_cmd
    
    for tx_byte in tx_cmd:
        serialport.write(tx_byte)

    # Allow the device to complete the transaction.
    time.sleep(0.1)


#-------------------------------------------------------------------
# single_block_test_sha256()
#
# Write a given block to SHA-256 and perform single block
# processing.
#-------------------------------------------------------------------
def single_block_test_sha256(block, ser):
    # Write block to SHA-2.
    for i in range(len(block) / 4):
        message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\
                  block[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start initial block hashing, wait and check status.
    write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser)
    time.sleep(0.1)
    write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser)

    # Extract contents of the digest registers.
    for i in range(8):
        message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC]
        write_serial_bytes(message, ser)
    print""


#-------------------------------------------------------------------
# double_block_test_sha256()
#
# Run double block message test.
#-------------------------------------------------------------------
def double_block_test_sha256(block1, block2, ser):
    # Write block1 to SHA-256.
    for i in range(len(block1) / 4):
        message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\
                  block1[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start initial block hashing, wait and check status.
    write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser)
    time.sleep(0.1)
    write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser)

    # Extract contents of the digest registers.
    for i in range(8):
        message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC]
        write_serial_bytes(message, ser)
    print""

    # Write block2 to SHA-256.
    for i in range(len(block2) / 4):
        message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\
                  block2[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start next block hashing, wait and check status.
    write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x02', EOC], ser)
    time.sleep(0.1)
    write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser)

    # Extract contents of the digest registers.
    for i in range(8):
        message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC]
        write_serial_bytes(message, ser)
    print""


#-------------------------------------------------------------------
# huge_message_test_sha256()

# Test with a message with a huge number (n) number of blocks.
#-------------------------------------------------------------------
def huge_message_test_sha256(block, n, ser):
    # Write block to SHA-256.
    for i in range(len(block) / 4):
        message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\
                  block[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start initial block hashing, wait.
    write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser)
    time.sleep(0.1)
    print "Block 0 done."

    # First blocl done.
    n = n - 1

    for i in range(n):
        # Start next block hashing, wait.
        write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x02', EOC], ser)
        time.sleep(0.1)
        print "Block %d done." % (i + 1)

        # Extract contents of the digest registers.
        for i in range(8):
            message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC]
            write_serial_bytes(message, ser)
            print""


#-------------------------------------------------------------------
# single_block_test_sha1()
#
# Write a given block to SHA-1 and perform single block
# processing.
#-------------------------------------------------------------------
def single_block_test_sha1(block, ser):
    # Write block to SHA-1.
    for i in range(len(block) / 4):
        message = [SOC, WRITE_CMD, SHA1_ADDR_PREFIX,] + [sha1_block_addr[i]] +\
                  block[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start initial block hashing, wait and check status.
    write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser)
    time.sleep(0.1)
    write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS,   EOC], ser)

    # Extract the digest.
    for i in range(5):
        message = [SOC, READ_CMD, SHA1_ADDR_PREFIX] + [sha1_digest_addr[i]] + [EOC]
        write_serial_bytes(message, ser)
    print""
    

#-------------------------------------------------------------------
# double_block_test_sha1
#
# Run double block message test for SHA-1.
#-------------------------------------------------------------------
def double_block_test_sha1(block1, block2, ser):
    # Write block1 to SHA-1.
    for i in range(len(block1) / 4):
        message = [SOC, WRITE_CMD, SHA1_ADDR_PREFIX,] + [sha1_block_addr[i]] +\
                  block1[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start initial block hashing, wait and check status.
    write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser)
    time.sleep(0.1)
    write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS,   EOC], ser)

    # Extract the digest.
    for i in range(5):
        message = [SOC, READ_CMD, SHA1_ADDR_PREFIX] + [sha1_digest_addr[i]] + [EOC]
        write_serial_bytes(message, ser)
    print""

    # Write block2 to SHA-1.
    for i in range(len(block2) / 4):
        message = [SOC, WRITE_CMD, SHA1_ADDR_PREFIX,] + [sha1_block_addr[i]] +\
                  block2[(i * 4) : ((i * 4 ) + 4)] + [EOC]
        write_serial_bytes(message, ser)

    # Start next block hashing, wait and check status.
    write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x02', EOC], ser)
    time.sleep(0.1)
    write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS,   EOC], ser)

    # Extract the digest.
    for i in range(5):
        message = [SOC, READ_CMD, SHA1_ADDR_PREFIX] + [sha1_digest_addr[i]] + [EOC]
        write_serial_bytes(message, ser)
    print""


#-------------------------------------------------------------------
# main()
#
# Parse any arguments and run the tests.
#-------------------------------------------------------------------
def main():
    # Open device
    ser = serial.Serial()
    ser.port=SERIAL_DEVICE
    ser.baudrate=BAUD_RATE
    ser.bytesize=DATA_BITS
    ser.parity='N'
    ser.stopbits=STOP_BITS
    ser.timeout=1
    ser.writeTimeout=0

    if VERBOSE:
        print "Setting up a serial port and starting a receive thread."

    try:
        ser.open()
    except:
        print "Error: Can't open serial device."
        sys.exit(1)

    try:
        my_thread = threading.Thread(target=read_serial_thread, args=(ser,))
    except:
        print "Error: Can't start thread."
        sys.exit()
        
    my_thread.daemon = True
    my_thread.start()


    # TC1: Read name and version from SHA-1 core.
    print "TC1: Reading name, type and version words from SHA-1 core."
    write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_NAME0, EOC], ser)
    write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_NAME1, EOC], ser)
    write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_VERSION, EOC], ser)
    print""


    # TC2: Single block message test as specified by NIST.
    print "TC2: Single block message test for SHA-1."
    tc2_block = ['\x61', '\x62', '\x63', '\x80', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                 '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x18']

    tc2_sha1_expected = [0xa9993e36, 0x4706816a, 0xba3e2571,
                         0x7850c26c, 0x9cd0d89d]

    print "TC2: Expected digest values as specified by NIST:"
    for i in tc2_sha1_expected:
        print("0x%08x " % i)
    print("")
    single_block_test_sha1(tc2_block, ser)


    # TC3: Double block message test as specified by NIST.
    print "TC3: Double block message test for SHA-1."
    tc3_1_block = ['\x61', '\x62', '\x63', '\x64', '\x62', '\x63', '\x64', '\x65',
                   '\x63', '\x64', '\x65', '\x66', '\x64', '\x65', '\x66', '\x67',
                   '\x65', '\x66', '\x67', '\x68', '\x66', '\x67', '\x68', '\x69',
                   '\x67', '\x68', '\x69', '\x6A', '\x68', '\x69', '\x6A', '\x6B',
                   '\x69', '\x6A', '\x6B', '\x6C', '\x6A', '\x6B', '\x6C', '\x6D',
                   '\x6B', '\x6C', '\x6D', '\x6E', '\x6C', '\x6D', '\x6E', '\x6F',
                   '\x6D', '\x6E', '\x6F', '\x70', '\x6E', '\x6F', '\x70', '\x71',
                   '\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']

    tc3_2_block = ['\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
                   '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\xC0']

    tc3_1_sha1_expected = [0xF4286818, 0xC37B27AE, 0x0408F581,
                           0x84677148, 0x4A566572]

    tc3_2_sha1_expected = [0x84983E44, 0x1C3BD26E, 0xBAAE4AA1,
                           0xF95129E5, 0xE54670F1]

    print "TC3: Expected digest values for first block as specified by NIST:"
    for i in tc3_1_sha1_expected:
        print("0x%08x " % i)
    print("")
    print "TC3: Expected digest values for second block as specified by NIST:"
    for i in tc3_2_sha1_expected:
        print("0x%08x " % i)
    print("")
    double_block_test_sha1(tc3_1_block, tc3_2_block, ser)


    # TC4: Read name and version from SHA-256 core.
    print "TC4: Reading name, type and version words from SHA-256 core."
    my_cmd = [SOC, READ_CMD, SHA256_ADDR_PREFIX, NAME0_ADDR, EOC]
    write_serial_bytes(my_cmd, ser)
    my_cmd = [SOC, READ_CMD, SHA256_ADDR_PREFIX, NAME1_ADDR, EOC]
    write_serial_bytes(my_cmd, ser)
    my_cmd = [SOC, READ_CMD, SHA256_ADDR_PREFIX, VERSION_ADDR, EOC]
    write_serial_bytes(my_cmd, ser)
    print""


    # TC5: Single block message test as specified by NIST.
    print "TC5: Single block message test for SHA-256."

    tc5_sha256_expected = [0xBA7816BF, 0x8F01CFEA, 0x414140DE, 0x5DAE2223,
                           0xB00361A3, 0x96177A9C, 0xB410FF61, 0xF20015AD]

    print "TC5: Expected digest values as specified by NIST:"
    for i in tc5_sha256_expected:
        print("0x%08x " % i)
    print("")
    single_block_test_sha256(tc2_block, ser)


    # TC6: Double block message test as specified by NIST.
    print "TC6: Double block message test for SHA-256."

    tc6_1_sha256_expected = [0x85E655D6, 0x417A1795, 0x3363376A, 0x624CDE5C,
                             0x76E09589, 0xCAC5F811, 0xCC4B32C1, 0xF20E533A]

    tc6_2_sha256_expected = [0x248D6A61, 0xD20638B8, 0xE5C02693, 0x0C3E6039,
                             0xA33CE459, 0x64FF2167, 0xF6ECEDD4, 0x19DB06C1]

    print "TC6: Expected digest values for first block as specified by NIST:"
    for i in tc6_1_sha256_expected:
        print("0x%08x " % i)
    print("")
    print "TC6: Expected digest values for second block as specified by NIST:"
    for i in tc6_2_sha256_expected:
        print("0x%08x " % i)
    print("")
    double_block_test_sha256(tc3_1_block, tc3_2_block, ser)

    # TC7: Huge message test.
#    n = 10
#    print "TC7: Message with %d blocks test for SHA-256." % n
#    tc7_block = ['\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef',
#                 '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f',
#                 '\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef',
#                 '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f',
#                 '\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef',
#                 '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f',
#                 '\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef',
#                 '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f']
#
#    tc7_expected = [0xf407ff0d, 0xb9dce2f6, 0x9b9759a9, 0xd3cdc805,
#                    0xf250086d, 0x73bbefd5, 0xa972e0f7, 0x61a9c13e]
#
#    print "TC7: Expected digest values after %d blocks:" %n
#    for i in tc7_expected:
#        print("0x%08x " % i)
#    print("")
#    huge_message_test_sha256(tc7_block, n, ser)
    
    # Exit nicely.
    if VERBOSE:
        print "Done. Closing device."
    ser.close()


#-------------------------------------------------------------------
# __name__
# Python thingy which allows the file to be run standalone as
# well as parsed from within a Python interpreter.
#-------------------------------------------------------------------
if __name__=="__main__": 
    # Run the main function.
    sys.exit(main())


#=======================================================================
# EOF hash_tester.py
#=======================================================================