]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/digestobject.h
Security-54.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / digestobject.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * digestobject.h - generic virtual Digest base class
21 */
22
23 #ifndef _DIGEST_OBJECT_H_
24 #define _DIGEST_OBJECT_H_
25
26 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
27 #include <Security/cssmalloc.h>
28
29 /* common virtual digest class */
30 class DigestObject {
31 public:
32 DigestObject() : mInitFlag(false), mIsDone(false) { }
33 virtual ~DigestObject() { }
34
35 /*
36 * The remaining functions must be implemented by subclass.
37 */
38 /* init is reusable */
39 virtual void digestInit() = 0;
40
41 /* add some data */
42 virtual void digestUpdate(
43 const void *data,
44 size_t len) = 0;
45
46 /* obtain digest (once only per init, update, ... cycle) */
47 virtual void digestFinal(
48 void *digest) = 0; /* RETURNED, alloc'd by caller */
49
50 /* sublass-specific copy */
51 virtual DigestObject *digestClone() const = 0;
52
53 virtual size_t digestSizeInBytes() const = 0;
54
55 protected:
56 bool mInitFlag;
57 bool mIsDone;
58
59 bool initFlag() { return mInitFlag; }
60 void setInitFlag(bool flag) { mInitFlag = flag; }
61 bool isDone() { return mIsDone; }
62 void setIsDone(bool done) { mIsDone = done; }
63 };
64
65 /*
66 * NullDigest.h - nop digest for use with raw signature algorithms.
67 * NullDigest(someData) = someData.
68 */
69 class NullDigest : public DigestObject
70 {
71 public:
72 NullDigest() : mInBuf(NULL), mInBufSize(0)
73 {
74 }
75
76 void digestInit()
77 {
78 /* reusable - reset */
79 if(mInBufSize) {
80 assert(mInBuf != NULL);
81 memset(mInBuf, 0, mInBufSize);
82 CssmAllocator::standard().free(mInBuf);
83 mInBufSize = 0;
84 mInBuf = NULL;
85 }
86 }
87
88 ~NullDigest()
89 {
90 digestInit();
91 }
92
93 void digestUpdate(
94 const void *data,
95 size_t len)
96 {
97 mInBuf = CssmAllocator::standard().realloc(mInBuf, mInBufSize + len);
98 memmove((uint8 *)mInBuf + mInBufSize, data, len);
99 mInBufSize += len;
100 }
101
102 virtual void digestFinal(
103 void *digest)
104 {
105 memmove(digest, mInBuf, mInBufSize);
106 }
107
108 virtual DigestObject *digestClone() const
109 {
110 NullDigest *cloned = new NullDigest;
111 cloned->digestUpdate(mInBuf, mInBufSize);
112 return cloned;
113 }
114
115 /* unique to NullDigest - just obtain current data ptr, no copy */
116 virtual const void *digestPtr() { return mInBuf; }
117
118 size_t digestSizeInBytes() const
119 {
120 return mInBufSize;
121 }
122
123 private:
124 void *mInBuf;
125 size_t mInBufSize;
126 };
127
128 #endif /* _DIGEST_OBJECT_H_ */