2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
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 obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
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 EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
22 Contains: HMAC routines used by TLS
24 Written by: Doug Mitchell
28 #include "appleCdsa.h"
29 #include "sslMemory.h"
30 #include "cryptType.h"
31 #include "sslDigests.h"
34 #include <Security/cssm.h>
36 /* Per-session state, opaque to callers; all fields set at alloc time */
39 CSSM_CC_HANDLE ccHand
;
40 const struct HMACReference
*hmac
;
43 #pragma mark *** Common CDSA_based HMAC routines ***
45 /* Create an HMAC session */
46 static OSStatus
HMAC_Alloc(
47 const struct HMACReference
*hmac
,
51 HMACContextRef
*hmacCtx
) // RETURNED
57 HMACContextRef href
= (HMACContextRef
)sslMalloc(sizeof(struct HMACContext
));
67 * Since the key is present in the CDSA context, we cook up the context now.
68 * Currently we can't reuse an HMAC context if the key changes.
72 calg
= CSSM_ALGID_SHA1HMAC
;
75 calg
= CSSM_ALGID_MD5HMAC
;
79 return errSSLInternal
;
81 serr
= sslSetUpSymmKey(&cssmKey
,
83 CSSM_KEYUSE_SIGN
| CSSM_KEYUSE_VERIFY
,
84 CSSM_FALSE
, /* don't malloc/copy key */
90 if(attachToCsp(ctx
)) {
93 crtn
= CSSM_CSP_CreateMacContext(ctx
->cspHand
,
98 stPrintCdsaError("CSSM_CSP_CreateMacContext", crtn
);
108 static OSStatus
HMAC_Free(
109 HMACContextRef hmacCtx
)
111 if(hmacCtx
!= NULL
) {
112 if(hmacCtx
->ccHand
!= 0) {
113 CSSM_DeleteContext(hmacCtx
->ccHand
);
122 static OSStatus
HMAC_Init(
123 HMACContextRef hmacCtx
)
127 if(hmacCtx
== NULL
) {
128 return errSSLInternal
;
130 assert(hmacCtx
->ctx
!= NULL
);
131 assert(hmacCtx
->hmac
!= NULL
);
132 assert(hmacCtx
->ccHand
!= 0);
134 crtn
= CSSM_GenerateMacInit(hmacCtx
->ccHand
);
136 stPrintCdsaError("CSSM_GenerateMacInit", crtn
);
142 /* normal crypt ops */
143 static OSStatus
HMAC_Update(
144 HMACContextRef hmacCtx
,
151 if(hmacCtx
== NULL
) {
152 return errSSLInternal
;
154 assert(hmacCtx
->ctx
!= NULL
);
155 assert(hmacCtx
->hmac
!= NULL
);
156 assert(hmacCtx
->ccHand
!= 0);
157 cdata
.Data
= (uint8
*)data
;
158 cdata
.Length
= dataLen
;
159 crtn
= CSSM_GenerateMacUpdate(hmacCtx
->ccHand
, &cdata
, 1);
161 stPrintCdsaError("CSSM_GenerateMacUpdate", crtn
);
167 static OSStatus
HMAC_Final(
168 HMACContextRef hmacCtx
,
169 void *hmac
, // mallocd by caller
170 unsigned *hmacLen
) // IN/OUT
175 if(hmacCtx
== NULL
) {
176 return errSSLInternal
;
178 if((hmac
== NULL
) || (hmacLen
== 0)) {
179 return errSSLInternal
;
181 assert(hmacCtx
->ctx
!= NULL
);
182 assert(hmacCtx
->hmac
!= NULL
);
183 assert(hmacCtx
->ccHand
!= 0);
184 cdata
.Data
= (uint8
*)hmac
;
185 cdata
.Length
= *hmacLen
;
186 crtn
= CSSM_GenerateMacFinal(hmacCtx
->ccHand
, &cdata
);
188 stPrintCdsaError("CSSM_GenerateMacFinal", crtn
);
191 *hmacLen
= cdata
.Length
;
196 static OSStatus
HMAC_Hmac (
197 HMACContextRef hmacCtx
,
200 void *hmac
, // mallocd by caller
201 unsigned *hmacLen
) // IN/OUT
204 const HMACReference
*hmacRef
;
206 if(hmacCtx
== NULL
) {
207 return errSSLInternal
;
209 hmacRef
= hmacCtx
->hmac
;
210 assert(hmacRef
!= NULL
);
211 serr
= hmacRef
->init(hmacCtx
);
215 serr
= hmacRef
->update(hmacCtx
, data
, dataLen
);
219 return hmacRef
->final(hmacCtx
, hmac
, hmacLen
);
222 #pragma mark *** Null HMAC ***
224 static OSStatus
HMAC_AllocNull(
225 const struct HMACReference
*hmac
,
229 HMACContextRef
*hmacCtx
) // RETURNED
235 static OSStatus
HMAC_FreeNull(
236 HMACContextRef hmacCtx
)
241 static OSStatus
HMAC_InitNull(
242 HMACContextRef hmacCtx
)
247 static OSStatus
HMAC_UpdateNull(
248 HMACContextRef hmacCtx
,
255 static OSStatus
HMAC_FinalNull(
256 HMACContextRef hmacCtx
,
257 void *hmac
, // mallocd by caller
258 unsigned *hmacLen
) // IN/OUT
263 static OSStatus
HMAC_HmacNull (
264 HMACContextRef hmacCtx
,
267 void *hmac
, // mallocd by caller
273 const HMACReference TlsHmacNull
= {
284 const HMACReference TlsHmacSHA1
= {
295 const HMACReference TlsHmacMD5
= {
306 const HashHmacReference HashHmacNull
= {
311 const HashHmacReference HashHmacMD5
= {
316 const HashHmacReference HashHmacSHA1
= {