1/* Copyright (c) 1998,2011,2014 Apple Inc.  All Rights Reserved.
2 *
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC.  ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
10 *
11 * FeeHash.c - generic, portable MD5 hash object
12 *
13 * Revision History
14 * ----------------
15 * 10/06/98		ap
16 *	Changed to compile with C++.
17 * 22 Aug 96 at NeXT
18 *	Created.
19 */
20
21#include "ckconfig.h"
22
23#if	CRYPTKIT_MD5_ENABLE
24
25#include "feeTypes.h"
26#include "feeHash.h"
27#include "ckMD5.h"
28#include "falloc.h"
29#include "platform.h"
30
31/*
32 * Private data for this object. A feeHash handle is cast to aa pointer
33 * to one of these.
34 */
35typedef struct {
36	MD5Context		context;
37	int 			isDone;
38	unsigned char 	digest[MD5_DIGEST_SIZE];
39} hashInst;
40
41/*
42 * Alloc and init an empty hash object.
43 */
44feeHash feeHashAlloc(void)
45{
46	hashInst *hinst;
47
48	hinst = (hashInst *) fmalloc(sizeof(hashInst));
49	MD5Init(&hinst->context);
50	hinst->isDone = 0;
51	return hinst;
52}
53
54void feeHashReinit(feeHash hash)
55{
56	hashInst *hinst = (hashInst *) hash;
57
58	MD5Init(&hinst->context);
59	hinst->isDone = 0;
60}
61
62/*
63 * Free a hash object.
64 */
65void feeHashFree(feeHash hash)
66{
67	hashInst *hinst = (hashInst *) hash;
68
69	memset(hinst, 0, sizeof(hashInst));
70	ffree(hinst);
71}
72
73/*
74 * Add some data to the hash object.
75 */
76void feeHashAddData(feeHash hash,
77	const unsigned char *data,
78	unsigned dataLen)
79{
80	hashInst *hinst = (hashInst *) hash;
81
82	if(hinst->isDone) {
83		/*
84		 * Log some kind of error here...
85		 */
86		return;
87	}
88	MD5Update(&hinst->context, data, dataLen);
89}
90
91/*
92 * Obtain a pointer to completed message digest, and the length of the digest.
93 */
94unsigned char *feeHashDigest(feeHash hash)
95{
96	hashInst *hinst = (hashInst *) hash;
97
98	if(!hinst->isDone) {
99		MD5Final(&hinst->context, hinst->digest);
100		hinst->isDone = 1;
101	}
102	return hinst->digest;
103}
104
105unsigned feeHashDigestLen(void)
106{
107	return MD5_DIGEST_SIZE;
108}
109
110#endif	/* CRYPTKIT_MD5_ENABLE*/
111