1 | // =================================================================== |
---|
2 | // |
---|
3 | // Copyright (c) 2005, Intel Corp. |
---|
4 | // All rights reserved. |
---|
5 | // |
---|
6 | // Redistribution and use in source and binary forms, with or without |
---|
7 | // modification, are permitted provided that the following conditions |
---|
8 | // are met: |
---|
9 | // |
---|
10 | // * Redistributions of source code must retain the above copyright |
---|
11 | // notice, this list of conditions and the following disclaimer. |
---|
12 | // * Redistributions in binary form must reproduce the above |
---|
13 | // copyright notice, this list of conditions and the following |
---|
14 | // disclaimer in the documentation and/or other materials provided |
---|
15 | // with the distribution. |
---|
16 | // * Neither the name of Intel Corporation nor the names of its |
---|
17 | // contributors may be used to endorse or promote products derived |
---|
18 | // from this software without specific prior written permission. |
---|
19 | // |
---|
20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
23 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
24 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
25 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
26 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
27 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
29 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
31 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | // =================================================================== |
---|
33 | // |
---|
34 | // hash.c |
---|
35 | // |
---|
36 | // This file will handle all the TPM Hash functionality |
---|
37 | // |
---|
38 | // ================================================================== |
---|
39 | |
---|
40 | #include <string.h> |
---|
41 | #include <openssl/crypto.h> |
---|
42 | #include <openssl/err.h> |
---|
43 | #include <openssl/evp.h> |
---|
44 | #include <openssl/rand.h> |
---|
45 | #include <openssl/hmac.h> |
---|
46 | #include <openssl/sha.h> |
---|
47 | #include <openssl/bn.h> |
---|
48 | #include <openssl/rsa.h> |
---|
49 | |
---|
50 | #include "tcg.h" // for TPM_SUCCESS |
---|
51 | #include "crypto.h" |
---|
52 | |
---|
53 | static SHA_CTX g_shaContext; |
---|
54 | |
---|
55 | void Crypto_HMAC( const BYTE* text, |
---|
56 | int text_len, |
---|
57 | const BYTE* key, |
---|
58 | int key_len, |
---|
59 | BYTE* digest) { |
---|
60 | if (text == NULL || key == NULL || text_len == 0 || key_len == 0) |
---|
61 | return; |
---|
62 | |
---|
63 | HMAC(EVP_sha1(), key, key_len, text, text_len, digest, NULL); |
---|
64 | } |
---|
65 | |
---|
66 | TPM_RESULT Crypto_HMAC_buf (const buffer_t * text, |
---|
67 | const buffer_t * key, |
---|
68 | BYTE * o_digest) { /* presumably of 20 bytes */ |
---|
69 | |
---|
70 | Crypto_HMAC (text->bytes, text->size, |
---|
71 | key->bytes, key->size, |
---|
72 | o_digest); |
---|
73 | |
---|
74 | return TPM_SUCCESS; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | /* |
---|
79 | * SHA1 |
---|
80 | * (OUT) Create a SHA1 hash of text. Calls all three SHA1 steps internally |
---|
81 | */ |
---|
82 | void Crypto_SHA1Full( const BYTE* text, |
---|
83 | uint32_t size, |
---|
84 | BYTE* hash) { |
---|
85 | |
---|
86 | if (text == NULL || size == 0) |
---|
87 | return; |
---|
88 | |
---|
89 | // Run SHA1Start + SHAUpdate (if necessary) + SHAComplete |
---|
90 | uint32_t maxBytes; // Not used for anything |
---|
91 | Crypto_SHA1Start(&maxBytes); |
---|
92 | |
---|
93 | while (size > 64){ |
---|
94 | Crypto_SHA1Update(64, text); |
---|
95 | size -= 64; |
---|
96 | text += 64; |
---|
97 | } |
---|
98 | |
---|
99 | Crypto_SHA1Complete(size, text, hash); |
---|
100 | } |
---|
101 | |
---|
102 | // same thing using buffer_t |
---|
103 | TPM_RESULT Crypto_SHA1Full_buf (const buffer_t * buf, |
---|
104 | BYTE * o_digest) { |
---|
105 | |
---|
106 | if (buf->bytes == NULL || buf->size == 0) |
---|
107 | return TPM_BAD_PARAMETER; |
---|
108 | |
---|
109 | Crypto_SHA1Full (buf->bytes, buf->size, o_digest); |
---|
110 | |
---|
111 | return TPM_SUCCESS; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /* |
---|
116 | * Initialize SHA1 |
---|
117 | * (OUT) Maximum number of bytes that can be sent to SHA1Update. |
---|
118 | * Must be a multiple of 64 bytes. |
---|
119 | */ |
---|
120 | void Crypto_SHA1Start(uint32_t* maxNumBytes) { |
---|
121 | int max = SHA_CBLOCK; |
---|
122 | // Initialize the crypto library |
---|
123 | SHA1_Init(&g_shaContext); |
---|
124 | *maxNumBytes = max; |
---|
125 | } |
---|
126 | |
---|
127 | /* |
---|
128 | * Process SHA1 |
---|
129 | * @numBytes: (IN) The number of bytes in hashData. |
---|
130 | * Must be a multiple of 64 bytes. |
---|
131 | * @hashData: (IN) Bytes to be hashed. |
---|
132 | */ |
---|
133 | void Crypto_SHA1Update(int numBytes, const BYTE* hashData) { |
---|
134 | |
---|
135 | if (hashData == NULL || numBytes == 0 || numBytes%64 != 0) |
---|
136 | return; |
---|
137 | |
---|
138 | SHA1_Update(&g_shaContext, hashData, numBytes); |
---|
139 | } |
---|
140 | |
---|
141 | /* |
---|
142 | * Complete the SHA1 process |
---|
143 | * @hashDataSize: (IN) Number of bytes in hashData. |
---|
144 | * Must be a multiple of 64 bytes. |
---|
145 | * @hashData: (IN) Final bytes to be hashed. |
---|
146 | * @hashValue: (OUT) The output of the SHA-1 hash. |
---|
147 | */ |
---|
148 | void Crypto_SHA1Complete(int hashDataSize, |
---|
149 | const BYTE* hashData, |
---|
150 | BYTE* hashValue) { |
---|
151 | SHA1_Update(&g_shaContext, hashData, hashDataSize); |
---|
152 | SHA1_Final(hashValue, &g_shaContext); |
---|
153 | } |
---|