]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_transform/lib/Digest.h
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_transform / lib / Digest.h
1 #include "Transform.h"
2 #include "TransformFactory.h"
3
4 #include <CommonCrypto/CommonDigest.h>
5 #include <CommonCrypto/CommonHMAC.h>
6
7
8 // set up an abstraction for digest. We have to do this because
9 // CommonCrypto doesn't support a unified digest model
10
11 class Digest
12 {
13 protected:
14 CFStringRef mDigestType;
15 size_t mDigestLength;
16
17 public:
18 Digest(CFStringRef digestType, size_t digestLength);
19
20 virtual ~Digest();
21
22 virtual void Update(const void* buffer, size_t length) = 0;
23 virtual size_t DigestLength() = 0;
24 virtual const void* Finalize() = 0;
25
26 virtual CFDictionaryRef CopyState();
27
28 static int LengthForType(CFStringRef type);
29 };
30
31
32
33 class MD2Digest : public Digest
34 {
35 protected:
36 CC_MD2_CTX mContext;
37 u_int8_t mDigestBuffer[CC_MD2_DIGEST_LENGTH];
38
39 public:
40 MD2Digest();
41
42 void Update(const void* buffer, size_t length);
43 size_t DigestLength();
44 const void* Finalize();
45 };
46
47
48
49 class MD4Digest : public Digest
50 {
51 protected:
52 CC_MD4_CTX mContext;
53 u_int8_t mDigestBuffer[CC_MD4_DIGEST_LENGTH];
54
55 public:
56 MD4Digest();
57
58 void Update(const void* buffer, size_t length);
59 size_t DigestLength();
60 const void* Finalize();
61 };
62
63
64
65 class MD5Digest : public Digest
66 {
67 protected:
68 CC_MD5_CTX mContext;
69 u_int8_t mDigestBuffer[CC_MD5_DIGEST_LENGTH];
70
71 public:
72 MD5Digest();
73
74 void Update(const void* buffer, size_t length);
75 size_t DigestLength();
76 const void* Finalize();
77 };
78
79
80
81 class SHA1Digest : public Digest
82 {
83 protected:
84 CC_SHA1_CTX mContext;
85 u_int8_t mDigestBuffer[CC_SHA1_DIGEST_LENGTH];
86
87 public:
88 SHA1Digest();
89
90 void Update(const void* buffer, size_t length);
91 size_t DigestLength();
92 const void* Finalize();
93 };
94
95
96
97 class SHA256Digest : public Digest
98 {
99 protected:
100 CC_SHA256_CTX mContext;
101 u_int8_t mDigestBuffer[CC_SHA256_DIGEST_LENGTH];
102
103 public:
104 SHA256Digest();
105
106 void Update(const void* buffer, size_t length);
107 size_t DigestLength();
108 const void* Finalize();
109 };
110
111
112
113 class SHA224Digest : public Digest
114 {
115 protected:
116 CC_SHA256_CTX mContext;
117 u_int8_t mDigestBuffer[CC_SHA256_DIGEST_LENGTH];
118
119 public:
120 SHA224Digest();
121
122 void Update(const void* buffer, size_t length);
123 size_t DigestLength();
124 const void* Finalize();
125 };
126
127
128
129 class SHA512Digest : public Digest
130 {
131 protected:
132 CC_SHA512_CTX mContext;
133 u_int8_t mDigestBuffer[CC_SHA512_DIGEST_LENGTH];
134
135 public:
136 SHA512Digest();
137
138 void Update(const void* buffer, size_t length);
139 size_t DigestLength();
140 const void* Finalize();
141 };
142
143
144
145 class SHA384Digest : public Digest
146 {
147 protected:
148 CC_SHA512_CTX mContext;
149 u_int8_t mDigestBuffer[CC_SHA512_DIGEST_LENGTH];
150
151 public:
152 SHA384Digest();
153
154 void Update(const void* buffer, size_t length);
155 size_t DigestLength();
156 const void* Finalize();
157 };
158
159
160
161 class Hmac : public Digest
162 {
163 protected:
164 bool mInitialized;
165 CCHmacContext mHMACContext;
166 u_int8_t *mDigestBuffer;
167 CFDataRef mKey;
168 Transform* mParentTransform;
169 CCHmacAlgorithm mAlg;
170
171 void Initialize();
172
173 public:
174 Hmac(Transform* parentTransform, CFStringRef digestType, CCHmacAlgorithm alg, size_t length);
175 virtual ~Hmac();
176 void Update(const void* buffer, size_t length);
177 size_t DigestLength();
178 const void* Finalize();
179 };
180
181
182 class DigestTransform : public Transform
183 {
184 protected:
185 Digest* mDigest;
186
187 DigestTransform();
188
189 public:
190 static CFTypeRef Make() CF_RETURNS_RETAINED;
191 virtual ~DigestTransform();
192
193 CFErrorRef Setup(CFTypeRef digestType, CFIndex length);
194
195 virtual void AttributeChanged(CFStringRef name, CFTypeRef value);
196
197 static TransformFactory* MakeTransformFactory();
198
199 CFDictionaryRef CopyState();
200 void RestoreState(CFDictionaryRef state);
201 static CFTypeID GetCFTypeID();
202 };