crypto.c revision 1.1.1.1
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
31TSS_RESULT
32Hash(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	switch (HashType) {
39		case TSS_HASH_SHA1:
40			rv = EVP_DigestInit(&md_ctx, EVP_sha1());
41			break;
42		default:
43			rv = TCSERR(TSS_E_BAD_PARAMETER);
44			goto out;
45			break;
46	}
47
48	if (rv != EVP_SUCCESS) {
49		rv = TCSERR(TSS_E_INTERNAL_ERROR);
50		goto out;
51	}
52
53	rv = EVP_DigestUpdate(&md_ctx, Buf, BufSize);
54	if (rv != EVP_SUCCESS) {
55		rv = TCSERR(TSS_E_INTERNAL_ERROR);
56		goto out;
57	}
58
59	result_size = EVP_MD_CTX_size(&md_ctx);
60	rv = EVP_DigestFinal(&md_ctx, Digest, &result_size);
61	if (rv != EVP_SUCCESS) {
62		rv = TCSERR(TSS_E_INTERNAL_ERROR);
63	} else
64		rv = TSS_SUCCESS;
65
66out:
67	return rv;
68}
69