OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 | 2 |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 # Run tests for cryptographic routine implementations - Message digests | 7 # Run tests for cryptographic routine implementations - Message digests |
8 # and RSA Signature verification. | 8 # and RSA Signature verification. |
9 | 9 |
| 10 return_code=0 |
10 hash_algos=( sha1 sha256 sha512 ) | 11 hash_algos=( sha1 sha256 sha512 ) |
11 key_lengths=( 1024 2048 4096 8192 ) | 12 key_lengths=( 1024 2048 4096 8192 ) |
12 TEST_FILE=test_file | 13 TEST_FILE=test_file |
13 TEST_FILE_SIZE=1000000 | 14 TEST_FILE_SIZE=1000000 |
14 UTIL_DIR=../utils/ | |
15 KEY_DIR=testkeys | |
16 | 15 |
17 # Generate public key signatures on an input file for various combinations | 16 # Generate public key signatures on an input file for various combinations |
18 # of message digest algorithms and RSA key sizes. | 17 # of message digest algorithms and RSA key sizes. |
19 function generate_signatures { | 18 function generate_signatures { |
20 algorithmcounter=0 | 19 algorithmcounter=0 |
21 for keylen in ${key_lengths[@]} | 20 for keylen in ${key_lengths[@]} |
22 do | 21 do |
23 for hashalgo in ${hash_algos[@]} | 22 for hashalgo in ${hash_algos[@]} |
24 do | 23 do |
25 ${UTIL_DIR}/signature_digest $algorithmcounter $1 | openssl rsautl -sign \ | 24 ${UTIL_DIR}/signature_digest $algorithmcounter $1 | openssl rsautl -sign \ |
26 -pkcs -inkey ${KEY_DIR}/key_rsa${keylen}.pem \ | 25 -pkcs -inkey ${KEY_DIR}/key_rsa${keylen}.pem \ |
27 > $1.rsa${keylen}\_${hashalgo}.sig | 26 > $1.rsa${keylen}\_${hashalgo}.sig |
28 let algorithmcounter=algorithmcounter+1 | 27 let algorithmcounter=algorithmcounter+1 |
29 done | 28 done |
30 done | 29 done |
31 } | 30 } |
32 | 31 |
33 function test_signatures { | 32 function test_signatures { |
34 algorithmcounter=0 | 33 algorithmcounter=0 |
35 for keylen in ${key_lengths[@]} | 34 for keylen in ${key_lengths[@]} |
36 do | 35 do |
37 for hashalgo in ${hash_algos[@]} | 36 for hashalgo in ${hash_algos[@]} |
38 do | 37 do |
39 echo "For RSA-$keylen and $hashalgo:" | 38 echo "For RSA-$keylen and $hashalgo:" |
40 ${UTIL_DIR}/verify_data $algorithmcounter \ | 39 ${UTIL_DIR}/verify_data $algorithmcounter \ |
41 ${KEY_DIR}/key_rsa${keylen}.keyb \ | 40 ${KEY_DIR}/key_rsa${keylen}.keyb \ |
42 ${TEST_FILE}.rsa${keylen}_${hashalgo}.sig ${TEST_FILE} | 41 ${TEST_FILE}.rsa${keylen}_${hashalgo}.sig ${TEST_FILE} |
| 42 if [ $? -ne 0 ] |
| 43 then |
| 44 return_code=255 |
| 45 fi |
43 let algorithmcounter=algorithmcounter+1 | 46 let algorithmcounter=algorithmcounter+1 |
44 done | 47 done |
45 done | 48 done |
46 } | 49 } |
47 | 50 |
48 function pre_work { | 51 function pre_work { |
49 # Generate a file with random bytes for signature tests. | 52 # Generate a file with random bytes for signature tests. |
50 echo "Generating test file..." | 53 echo "Generating test file..." |
51 dd if=/dev/urandom of=${TEST_FILE} bs=${TEST_FILE_SIZE} count=1 | 54 dd if=/dev/urandom of=${TEST_FILE} bs=${TEST_FILE_SIZE} count=1 |
52 echo "Generating signatures..." | 55 echo "Generating signatures..." |
53 generate_signatures $TEST_FILE | 56 generate_signatures $TEST_FILE |
54 } | 57 } |
55 | 58 |
56 function cleanup { | 59 function cleanup { |
57 rm ${TEST_FILE} ${TEST_FILE}.*.sig | 60 rm ${TEST_FILE} ${TEST_FILE}.*.sig |
58 } | 61 } |
59 | 62 |
60 echo "Testing message digests..." | 63 # Determine script directory. |
61 ./sha_tests | 64 if [[ $0 == '/'* ]]; |
| 65 then |
| 66 SCRIPT_DIR="`dirname $0`" |
| 67 elif [[ $0 == './'* ]]; |
| 68 then |
| 69 SCRIPT_DIR="`pwd`" |
| 70 else |
| 71 SCRIPT_DIR="`pwd`"/"`dirname $0`" |
| 72 fi |
| 73 UTIL_DIR=`dirname ${SCRIPT_DIR}`/utils |
| 74 KEY_DIR=${SCRIPT_DIR}/testkeys |
| 75 |
| 76 echo "Generating test cases..." |
| 77 pre_work |
62 | 78 |
63 echo | 79 echo |
64 echo "Testing signature verification..." | 80 echo "Testing signature verification..." |
65 pre_work | |
66 test_signatures | 81 test_signatures |
67 | 82 |
68 echo | 83 echo |
69 echo "Cleaning up..." | 84 echo "Cleaning up..." |
70 cleanup | 85 cleanup |
71 | 86 |
| 87 exit $return_code |
72 | 88 |
OLD | NEW |