1
2 /*
3 * Licensed Materials - Property of IBM
4 *
5 * trousers - An open source TCG Software Stack
6 *
7 * (C) Copyright International Business Machines Corp. 2006
8 *
9 */
10
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <errno.h>
17
18 #include <openssl/evp.h>
19
20 #include "trousers/tss.h"
21 #include "trousers_types.h"
22 #include "tcs_tsp.h"
23 #include "tcslog.h"
24
25 /*
26 * Hopefully this will make the code clearer since
27 * OpenSSL returns 1 on success
28 */
29 #define EVP_SUCCESS 1
30
31 TSS_RESULT
Hash(UINT32 HashType,UINT32 BufSize,BYTE * Buf,BYTE * Digest)32 Hash(UINT32 HashType, UINT32 BufSize, BYTE* Buf, BYTE* Digest)
33 {
34 EVP_MD_CTX *md_ctx;
35 unsigned int result_size;
36 int rv;
37
38 md_ctx = EVP_MD_CTX_create();
39
40 switch (HashType) {
41 case TSS_HASH_SHA1:
42 rv = EVP_DigestInit(md_ctx, EVP_sha1());
43 break;
44 default:
45 rv = TCSERR(TSS_E_BAD_PARAMETER);
46 goto out;
47 break;
48 }
49
50 if (rv != EVP_SUCCESS) {
51 rv = TCSERR(TSS_E_INTERNAL_ERROR);
52 goto out;
53 }
54
55 rv = EVP_DigestUpdate(md_ctx, Buf, BufSize);
56 if (rv != EVP_SUCCESS) {
57 rv = TCSERR(TSS_E_INTERNAL_ERROR);
58 goto out;
59 }
60
61 result_size = EVP_MD_CTX_size(md_ctx);
62 rv = EVP_DigestFinal(md_ctx, Digest, &result_size);
63 if (rv != EVP_SUCCESS) {
64 rv = TCSERR(TSS_E_INTERNAL_ERROR);
65 } else
66 rv = TSS_SUCCESS;
67
68 out:
69 EVP_MD_CTX_destroy(md_ctx);
70 return rv;
71 }
72