]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/pbkdDigest.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / lib / pbkdDigest.cpp
1 /*
2 * Copyright (c) 2003,2011,2014 Apple 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 /* the casts are necessary to cover the polymorphous context types */
27 DigestOps Md2Ops = {
28 (DigestInitFcn)CC_MD2_Init,
29 (DigestUpdateFcn)CC_MD2_Update,
30 (DigestFinalFcn)CC_MD2_Final
31 };
32 DigestOps Md5Ops = {
33 (DigestInitFcn)CC_MD5_Init,
34 (DigestUpdateFcn)CC_MD5_Update,
35 (DigestFinalFcn)CC_MD5_Final
36 };
37 DigestOps Sha1Ops = {
38 (DigestInitFcn)CC_SHA1_Init,
39 (DigestUpdateFcn)CC_SHA1_Update,
40 (DigestFinalFcn)CC_SHA1_Final
41 };
42
43 /* Ops on a DigestCtx - all return zero on error, like the underlying digests do */
44 int DigestCtxInit(
45 DigestCtx *ctx,
46 CSSM_ALGORITHMS hashAlg)
47 {
48 switch(hashAlg) {
49 case CSSM_ALGID_SHA1:
50 ctx->ops = &Sha1Ops;
51 break;
52 case CSSM_ALGID_MD5:
53 ctx->ops = &Md5Ops;
54 break;
55 case CSSM_ALGID_MD2:
56 ctx->ops = &Md2Ops;
57 break;
58 default:
59 return 0;
60 }
61 ctx->hashAlg = hashAlg;
62 return ctx->ops->init(&ctx->dig);
63 }
64
65 void DigestCtxFree(
66 DigestCtx *ctx)
67 {
68 memset(ctx, 0, sizeof(DigestCtx));
69 }
70
71 int DigestCtxUpdate(
72 DigestCtx *ctx,
73 const void *textPtr,
74 uint32 textLen)
75 {
76 return ctx->ops->update(&ctx->dig, textPtr, textLen);
77 }
78
79 int DigestCtxFinal(
80 DigestCtx *ctx,
81 void *digest)
82 {
83 return ctx->ops->final(digest, &ctx->dig);
84 }