1/* 2 * Copyright (c) 2003-2005 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 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 */ 27DigestOps Md5Ops = { 28 (DigestInitFcn)MD5_Init, 29 (DigestUpdateFcn)MD5_Update, 30 (DigestFinalFcn)MD5_Final 31}; 32DigestOps Sha1Ops = { 33 (DigestInitFcn)SHA1_Init, 34 (DigestUpdateFcn)SHA1_Update, 35 (DigestFinalFcn)SHA1_Final 36}; 37 38/* Ops on a DigestCtx */ 39int DigestCtxInit( 40 DigestCtx *ctx, 41 CSSM_BOOL isSha1) 42{ 43 if(isSha1) { 44 ctx->ops = &Sha1Ops; 45 } 46 else { 47 ctx->ops = &Md5Ops; 48 } 49 ctx->isSha1 = isSha1; 50 return ctx->ops->init(&ctx->dig); 51} 52 53void DigestCtxFree( 54 DigestCtx *ctx) 55{ 56 memset(ctx, 0, sizeof(DigestCtx)); 57} 58 59int DigestCtxUpdate( 60 DigestCtx *ctx, 61 const void *textPtr, 62 uint32 textLen) 63{ 64 return ctx->ops->update(&ctx->dig, textPtr, textLen); 65} 66 67int DigestCtxFinal( 68 DigestCtx *ctx, 69 void *digest) 70{ 71 return ctx->ops->final(digest, &ctx->dig); 72} 73