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