]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/SHA1_MD5_Object.cpp
Security-163.tar.gz
[apple/security.git] / AppleCSP / MiscCSPAlgs / SHA1_MD5_Object.cpp
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.cpp - generic C++ implementations of SHA1 and MD5.
21 *
22 * Created 2/19/2001 by dmitch.
23 */
24
25 #include "SHA1_MD5_Object.h"
26 #include <stdexcept>
27 #include <string.h>
28
29 /***
30 *** MD5
31 ***/
32 void MD5Object::digestInit()
33 {
34 mIsDone = false;
35 MD5Init(&mCtx);
36 }
37
38 void MD5Object::digestUpdate(
39 const void *data,
40 size_t len)
41 {
42 if(mIsDone) {
43 throw std::runtime_error("MD5 digestUpdate after final");
44 }
45 MD5Update(&mCtx, (unsigned char *)data, len);
46 }
47
48 void MD5Object::digestFinal(
49 void *digest)
50 {
51 if(mIsDone) {
52 throw std::runtime_error("MD5 digestFinal after final");
53 }
54 MD5Final(&mCtx, (unsigned char *)digest);
55 mIsDone = true;
56 }
57
58 /* use default memberwise init */
59 DigestObject *MD5Object::digestClone() const
60 {
61 return new MD5Object(*this);
62 }
63
64 UInt32 MD5Object::digestSizeInBytes() const
65 {
66 return MD5_DIGEST_SIZE;
67 }
68
69 /***
70 *** SHA1
71 ***/
72 void SHA1Object::digestInit()
73 {
74 mIsDone = false;
75 shsInit(&mCtx);
76 mBufferCount = 0;
77 }
78
79 void SHA1Object::digestUpdate(
80 const void *data,
81 size_t len)
82 {
83 size_t cnt;
84 uint8 *uData = (uint8 *)data;
85
86 if(mIsDone) {
87 throw std::runtime_error("SHA1 digestUpdate after final");
88 }
89
90 // deal with miniscule input leaving still less than one block
91 if (mBufferCount + len < SHS_BLOCKSIZE) {
92 memcpy(mBuffer + mBufferCount, uData, len);
93 mBufferCount += len;
94 return;
95 }
96
97 // fill possible partial existing buffer and process
98 if (mBufferCount > 0) {
99 cnt = SHS_BLOCKSIZE - mBufferCount;
100 memcpy(mBuffer + mBufferCount, uData, cnt);
101 shsUpdate(&mCtx, mBuffer, SHS_BLOCKSIZE);
102 uData += cnt;
103 len -= cnt;
104 }
105
106 // process remaining whole buffer multiples
107 UInt32 blocks = len / SHS_BLOCKSIZE;
108 if(blocks) {
109 cnt = blocks * SHS_BLOCKSIZE;
110 shsUpdate(&mCtx, uData, cnt);
111 uData += cnt;
112 len -= cnt;
113 }
114
115 // keep remainder
116 mBufferCount = len;
117 if (len > 0) {
118 memcpy(mBuffer, uData, len);
119 }
120 }
121
122 void SHA1Object::digestFinal(
123 void *digest)
124 {
125 if(mIsDone) {
126 throw std::runtime_error("SHA1 digestFinal after final");
127 }
128 if (mBufferCount > 0) {
129 shsUpdate(&mCtx, mBuffer, mBufferCount);
130 }
131 shsFinal(&mCtx);
132 memcpy(digest, mCtx.digest, SHS_DIGESTSIZE);
133 mIsDone = true;
134 }
135
136 /* use default memberwise init */
137 DigestObject *SHA1Object::digestClone() const
138 {
139 return new SHA1Object(*this);
140 }
141
142 UInt32 SHA1Object::digestSizeInBytes() const
143 {
144 return SHS_DIGESTSIZE;
145 }
146