]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/hashTimeSA/SHA1.c
1 /* Copyright (c) 1998,2004 Apple Computer, 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 COMPUTER, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE COMPUTER,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * SHA1.c - generic, portable SHA-1 hash object
16 * Changed to compile with C++.
17 * 07 Jan 1998 Doug Mitchell at Apple
22 #include "SHA1_priv.h"
25 /* for now map falloc to malloc, FIXME */
27 #define fmalloc(s) malloc(s)
28 #define ffree(p) free(p)
31 * Private data for this object. A sha1Obj handle is cast to a pointer
39 * For storing partial blocks
41 BYTE dataBuf
[SHS_BLOCKSIZE
];
42 unsigned bufBytes
; // valid bytes in dataBuf[p]
46 * Alloc and init an empty sha1 object.
48 sha1Obj
sha1Alloc(void)
52 sinst
= (sha1Inst
*)fmalloc(sizeof(sha1Inst
));
56 shsInit(&sinst
->context
);
57 sha1Reinit((sha1Obj
)sinst
);
58 return (sha1Obj
)sinst
;
62 * Reusable init function.
64 void sha1Reinit(sha1Obj sha1
)
66 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
68 shsInit(&sinst
->context
);
74 * Free an sha1 object.
76 void sha1Free(sha1Obj sha1
)
78 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
80 memset(sha1
, 0, sizeof(sha1Inst
));
85 * Add some data to the sha1 object.
87 void sha1AddData(sha1Obj sha1
,
88 const unsigned char *data
,
91 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
97 * Log some kind of error here...
103 * First deal with partial buffered block
105 if(sinst
->bufBytes
!= 0) {
106 toMove
= SHS_BLOCKSIZE
- sinst
->bufBytes
;
107 if(toMove
> dataLen
) {
110 memmove(sinst
->dataBuf
+sinst
->bufBytes
, data
, toMove
);
113 sinst
->bufBytes
+= toMove
;
114 if(sinst
->bufBytes
== SHS_BLOCKSIZE
) {
115 shsUpdate(&sinst
->context
, sinst
->dataBuf
, SHS_BLOCKSIZE
);
121 * Now the bulk of the data, in a multiple of full blocks
123 blocks
= dataLen
/ SHS_BLOCKSIZE
;
124 toMove
= blocks
* SHS_BLOCKSIZE
;
126 shsUpdate(&sinst
->context
, data
, toMove
);
132 * Store any remainder in dataBuf
135 memmove(sinst
->dataBuf
, data
, dataLen
);
136 sinst
->bufBytes
= dataLen
;
141 * Obtain a pointer to completed message digest, and the length of the digest.
143 unsigned char *sha1Digest(sha1Obj sha1
)
145 sha1Inst
*sinst
= (sha1Inst
*) sha1
;
149 * Deal with partial resid block
151 if(sinst
->bufBytes
!= 0) {
152 shsUpdate(&sinst
->context
, sinst
->dataBuf
,
156 shsFinal(&sinst
->context
);
160 * FIXME - should do explicit conversion to char array....?
162 return (unsigned char *)sinst
->context
.digest
;
165 /* As above, with copy. */
166 void sha1GetDigest(sha1Obj sha1
,
167 unsigned char *digest
)
169 unsigned char *dig
= sha1Digest(sha1
);
170 memmove(digest
, dig
, SHS_DIGESTSIZE
);
173 unsigned sha1DigestLen(void)
175 return SHS_DIGESTSIZE
;