]> git.saurik.com Git - apple/security.git/blob - AppleCSP/PBKDF2/pbkdDigest.cpp
Security-164.1.tar.gz
[apple/security.git] / AppleCSP / PBKDF2 / pbkdDigest.cpp
1 /*
2 * Copyright (c) 2003 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
7 * obtain a copy of the License at http://www.apple.com/publicsource and
8 * read it before 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
12 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
13 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
15 * Please see the License for the specific language governing rights and
16 * limitations under the License.
17 */
18 /*
19 * pbkdDigest.cpp - SHA1/MD5 digest object for HMAC and PBE routines
20 */
21
22 #include "pbkdDigest.h"
23 #include <Security/cssmerr.h>
24 #include <string.h>
25
26 /* Ops on a DigestCtx */
27 /* Note caller has to memset(0) the DigestCtx before using */
28 CSSM_RETURN DigestCtxInit(
29 DigestCtx *ctx,
30 CSSM_BOOL isSha1)
31 {
32 if(isSha1) {
33 if(ctx->dig.sha1Context == NULL) {
34 ctx->dig.sha1Context = sha1Alloc();
35 if(ctx->dig.sha1Context == NULL) {
36 return CSSMERR_CSP_MEMORY_ERROR;
37 }
38 }
39 else {
40 sha1Reinit(ctx->dig.sha1Context);
41 }
42 }
43 else {
44 MD5Init(&ctx->dig.md5Context);
45 }
46 ctx->isSha1 = isSha1;
47 return CSSM_OK;
48 }
49
50 void DigestCtxFree(
51 DigestCtx *ctx)
52 {
53 if(ctx->isSha1) {
54 sha1Free(ctx->dig.sha1Context);
55 }
56 memset(ctx, 0, sizeof(DigestCtx));
57 }
58
59 void DigestCtxUpdate(
60 DigestCtx *ctx,
61 const void *textPtr,
62 UInt32 textLen)
63 {
64 if(ctx->isSha1) {
65 sha1AddData(ctx->dig.sha1Context, (unsigned char *)textPtr, textLen);
66 }
67 else {
68 MD5Update(&ctx->dig.md5Context, (unsigned char *)textPtr, textLen);
69 }
70 }
71
72 void DigestCtxFinal(
73 DigestCtx *ctx,
74 void *digest)
75 {
76 if(ctx->isSha1) {
77 sha1GetDigest(ctx->dig.sha1Context, (unsigned char *)digest);
78 }
79 else {
80 MD5Final(&ctx->dig.md5Context, (unsigned char *)digest);
81 }
82 }