]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/feeHash.c
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / libsecurity_cryptkit / lib / feeHash.c
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 #include "feeTypes.h"
24 #include "feeHash.h"
25 #include "ckMD5.h"
26 #include "falloc.h"
27 #include "platform.h"
28
29 /*
30 * Private data for this object. A feeHash handle is cast to aa pointer
31 * to one of these.
32 */
33 typedef struct {
34 MD5Context context;
35 int isDone;
36 unsigned char digest[MD5_DIGEST_SIZE];
37 } hashInst;
38
39 /*
40 * Alloc and init an empty hash object.
41 */
42 feeHash feeHashAlloc(void)
43 {
44 hashInst *hinst;
45
46 hinst = (hashInst *) fmalloc(sizeof(hashInst));
47 MD5Init(&hinst->context);
48 hinst->isDone = 0;
49 return hinst;
50 }
51
52 void feeHashReinit(feeHash hash)
53 {
54 hashInst *hinst = (hashInst *) hash;
55
56 MD5Init(&hinst->context);
57 hinst->isDone = 0;
58 }
59
60 /*
61 * Free a hash object.
62 */
63 void feeHashFree(feeHash hash)
64 {
65 hashInst *hinst = (hashInst *) hash;
66
67 memset(hinst, 0, sizeof(hashInst));
68 ffree(hinst);
69 }
70
71 /*
72 * Add some data to the hash object.
73 */
74 void feeHashAddData(feeHash hash,
75 const unsigned char *data,
76 unsigned dataLen)
77 {
78 hashInst *hinst = (hashInst *) hash;
79
80 if(hinst->isDone) {
81 /*
82 * Log some kind of error here...
83 */
84 return;
85 }
86 MD5Update(&hinst->context, data, dataLen);
87 }
88
89 /*
90 * Obtain a pointer to completed message digest, and the length of the digest.
91 */
92 unsigned char *feeHashDigest(feeHash hash)
93 {
94 hashInst *hinst = (hashInst *) hash;
95
96 if(!hinst->isDone) {
97 MD5Final(&hinst->context, hinst->digest);
98 hinst->isDone = 1;
99 }
100 return hinst->digest;
101 }
102
103 unsigned feeHashDigestLen(void)
104 {
105 return MD5_DIGEST_SIZE;
106 }
107