]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/ckSHA1.c
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_cryptkit / lib / ckSHA1.c
1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
2 *
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 ***************************************************************************
10 *
11 * ckSHA1.c - generic, portable SHA-1 hash object
12 *
13 * Revision History
14 * ----------------
15 * 10/06/98 ap
16 * Changed to compile with C++.
17 * 07 Jan 1998 at Apple
18 * Created.
19 */
20
21 #include "ckconfig.h"
22 #include "feeTypes.h"
23 #include "ckSHA1.h"
24
25 /*
26 * For linking with AppleCSP: use libSystem SHA1 implementation.
27 */
28 #include <CommonCrypto/CommonDigest.h>
29 #include "falloc.h"
30 #include "platform.h"
31
32 /*
33 * Trivial wrapper for SHA_CTX; a sha1Obj is a pointer to this.
34 */
35 typedef struct {
36 CC_SHA1_CTX ctx;
37 unsigned char digest[CC_SHA1_DIGEST_LENGTH];
38 } Sha1Obj;
39
40 sha1Obj sha1Alloc(void)
41 {
42 void *rtn = fmalloc(sizeof(Sha1Obj));
43 memset(rtn, 0, sizeof(Sha1Obj));
44 CC_SHA1_Init(&(((Sha1Obj *)rtn)->ctx));
45 return (sha1Obj)rtn;
46 }
47
48 void sha1Reinit(sha1Obj sha1)
49 {
50 Sha1Obj *ctx = (Sha1Obj *)sha1;
51 CC_SHA1_Init(&ctx->ctx);
52 }
53
54 void sha1Free(sha1Obj sha1)
55 {
56 memset(sha1, 0, sizeof(Sha1Obj));
57 ffree(sha1);
58 }
59
60 void sha1AddData(sha1Obj sha1,
61 const unsigned char *data,
62 unsigned dataLen)
63 {
64 Sha1Obj *ctx = (Sha1Obj *)sha1;
65 CC_SHA1_Update(&ctx->ctx, data, dataLen);
66 }
67
68 unsigned char *sha1Digest(sha1Obj sha1)
69 {
70 Sha1Obj *ctx = (Sha1Obj *)sha1;
71 CC_SHA1_Final(ctx->digest, &ctx->ctx);
72 return ctx->digest;
73 }
74
75 unsigned sha1DigestLen(void)
76 {
77 return CC_SHA1_DIGEST_LENGTH;
78 }
79