]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/SHA1_MD5_Object.cpp
Security-30.1.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((unsigned char *)digest, &mCtx);
55 mIsDone = true;
56 }
57
58 UInt32 MD5Object::digestSizeInBytes() const
59 {
60 return MD5_DIGEST_SIZE;
61 }
62
63 /***
64 *** SHA1
65 ***/
66 void SHA1Object::digestInit()
67 {
68 mIsDone = false;
69 shsInit(&mCtx);
70 mBufferCount = 0;
71 }
72
73 void SHA1Object::digestUpdate(
74 const void *data,
75 size_t len)
76 {
77 size_t cnt;
78 uint8 *uData = (uint8 *)data;
79
80 if(mIsDone) {
81 throw std::runtime_error("SHA1 digestUpdate after final");
82 }
83
84 // deal with miniscule input leaving still less than one block
85 if (mBufferCount + len < SHS_BLOCKSIZE) {
86 memcpy(mBuffer + mBufferCount, uData, len);
87 mBufferCount += len;
88 return;
89 }
90
91 // fill possible partial existing buffer and process
92 if (mBufferCount > 0) {
93 cnt = SHS_BLOCKSIZE - mBufferCount;
94 memcpy(mBuffer + mBufferCount, uData, cnt);
95 shsUpdate(&mCtx, mBuffer, SHS_BLOCKSIZE);
96 uData += cnt;
97 len -= cnt;
98 }
99
100 // process remaining whole buffer multiples
101 UInt32 blocks = len / SHS_BLOCKSIZE;
102 if(blocks) {
103 cnt = blocks * SHS_BLOCKSIZE;
104 shsUpdate(&mCtx, uData, cnt);
105 uData += cnt;
106 len -= cnt;
107 }
108
109 // keep remainder
110 mBufferCount = len;
111 if (len > 0) {
112 memcpy(mBuffer, uData, len);
113 }
114 }
115
116 void SHA1Object::digestFinal(
117 void *digest)
118 {
119 if(mIsDone) {
120 throw std::runtime_error("SHA1 digestFinal after final");
121 }
122 if (mBufferCount > 0) {
123 shsUpdate(&mCtx, mBuffer, mBufferCount);
124 }
125 shsFinal(&mCtx);
126 memcpy(digest, mCtx.digest, SHS_DIGESTSIZE);
127 mIsDone = true;
128 }
129
130 UInt32 SHA1Object::digestSizeInBytes() const
131 {
132 return SHS_DIGESTSIZE;
133 }
134