]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/ckSHA1.c
1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * ckSHA1.c - generic, portable SHA-1 hash object
16 * Changed to compile with C++.
17 * 07 Jan 1998 at Apple
25 #if CRYPTKIT_LIBMD_DIGEST
27 * For linking with AppleCSP: use libSystem SHA1 implementation.
29 #include <CommonCrypto/CommonDigest.h>
31 #include "ckSHA1_priv.h"
36 #if CRYPTKIT_LIBMD_DIGEST
38 * Trivial wrapper for SHA_CTX; a sha1Obj is a pointer to this.
42 unsigned char digest
[CC_SHA1_DIGEST_LENGTH
];
45 sha1Obj
sha1Alloc(void)
47 void *rtn
= fmalloc(sizeof(Sha1Obj
));
48 memset(rtn
, 0, sizeof(Sha1Obj
));
49 CC_SHA1_Init(&(((Sha1Obj
*)rtn
)->ctx
));
53 void sha1Reinit(sha1Obj sha1
)
55 Sha1Obj
*ctx
= (Sha1Obj
*)sha1
;
56 CC_SHA1_Init(&ctx
->ctx
);
59 void sha1Free(sha1Obj sha1
)
61 memset(sha1
, 0, sizeof(Sha1Obj
));
65 void sha1AddData(sha1Obj sha1
,
66 const unsigned char *data
,
69 Sha1Obj
*ctx
= (Sha1Obj
*)sha1
;
70 CC_SHA1_Update(&ctx
->ctx
, data
, dataLen
);
73 unsigned char *sha1Digest(sha1Obj sha1
)
75 Sha1Obj
*ctx
= (Sha1Obj
*)sha1
;
76 CC_SHA1_Final(ctx
->digest
, &ctx
->ctx
);
80 unsigned sha1DigestLen(void)
82 return CC_SHA1_DIGEST_LENGTH
;
85 #else /* standalone cryptkit implementation */
88 * Private data for this object. A sha1Obj handle is cast to a pointer
96 * For storing partial blocks
98 BYTE dataBuf
[SHS_BLOCKSIZE
];
99 unsigned bufBytes
; // valid bytes in dataBuf[p]
103 * Alloc and init an empty sha1 object.
105 sha1Obj
sha1Alloc(void)
109 sinst
= (sha1Inst
*)fmalloc(sizeof(sha1Inst
));
113 shsInit(&sinst
->context
);
114 sha1Reinit((sha1Obj
)sinst
);
115 return (sha1Obj
)sinst
;
119 * Reusable init function.
121 void sha1Reinit(sha1Obj sha1
)
123 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
125 shsInit(&sinst
->context
);
131 * Free an sha1 object.
133 void sha1Free(sha1Obj sha1
)
135 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
137 memset(sha1
, 0, sizeof(sha1Inst
));
142 * Add some data to the sha1 object.
144 void sha1AddData(sha1Obj sha1
,
145 const unsigned char *data
,
148 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
154 * Log some kind of error here...
160 * First deal with partial buffered block
162 if(sinst
->bufBytes
!= 0) {
163 toMove
= SHS_BLOCKSIZE
- sinst
->bufBytes
;
164 if(toMove
> dataLen
) {
167 memmove(sinst
->dataBuf
+sinst
->bufBytes
, data
, toMove
);
170 sinst
->bufBytes
+= toMove
;
171 if(sinst
->bufBytes
== SHS_BLOCKSIZE
) {
172 shsUpdate(&sinst
->context
, sinst
->dataBuf
, SHS_BLOCKSIZE
);
178 * Now the bulk of the data, in a multiple of full blocks
180 blocks
= dataLen
/ SHS_BLOCKSIZE
;
181 toMove
= blocks
* SHS_BLOCKSIZE
;
183 shsUpdate(&sinst
->context
, data
, toMove
);
189 * Store any remainder in dataBuf
192 memmove(sinst
->dataBuf
, data
, dataLen
);
193 sinst
->bufBytes
= dataLen
;
198 * Obtain a pointer to completed message digest, and the length of the digest.
200 unsigned char *sha1Digest(sha1Obj sha1
)
202 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
206 * Deal with partial resid block
208 if(sinst
->bufBytes
!= 0) {
209 shsUpdate(&sinst
->context
, sinst
->dataBuf
,
213 shsFinal(&sinst
->context
);
217 * FIXME - should do explicit conversion to char array....?
219 return (unsigned char *)sinst
->context
.digest
;
222 unsigned sha1DigestLen(void)
224 return SHS_DIGESTSIZE
;
227 #endif /* CRYPTKIT_LIBMD_DIGEST */