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
,
107 static OSStatus
HMAC_Free(
108 HMACContextRef hmacCtx
)
110 if(hmacCtx
!= NULL
) {
111 if(hmacCtx
->ccHand
!= 0) {
112 CSSM_DeleteContext(hmacCtx
->ccHand
);
121 static OSStatus
HMAC_Init(
122 HMACContextRef hmacCtx
)
126 if(hmacCtx
== NULL
) {
127 return errSSLInternal
;
129 assert(hmacCtx
->ctx
!= NULL
);
130 assert(hmacCtx
->hmac
!= NULL
);
131 assert(hmacCtx
->ccHand
!= 0);
133 crtn
= CSSM_GenerateMacInit(hmacCtx
->ccHand
);
140 /* normal crypt ops */
141 static OSStatus
HMAC_Update(
142 HMACContextRef hmacCtx
,
149 if(hmacCtx
== NULL
) {
150 return errSSLInternal
;
152 assert(hmacCtx
->ctx
!= NULL
);
153 assert(hmacCtx
->hmac
!= NULL
);
154 assert(hmacCtx
->ccHand
!= 0);
155 cdata
.Data
= (uint8
*)data
;
156 cdata
.Length
= dataLen
;
157 crtn
= CSSM_GenerateMacUpdate(hmacCtx
->ccHand
, &cdata
, 1);
164 static OSStatus
HMAC_Final(
165 HMACContextRef hmacCtx
,
166 void *hmac
, // mallocd by caller
167 unsigned *hmacLen
) // IN/OUT
172 if(hmacCtx
== NULL
) {
173 return errSSLInternal
;
175 if((hmac
== NULL
) || (hmacLen
== 0)) {
176 return errSSLInternal
;
178 assert(hmacCtx
->ctx
!= NULL
);
179 assert(hmacCtx
->hmac
!= NULL
);
180 assert(hmacCtx
->ccHand
!= 0);
181 cdata
.Data
= (uint8
*)hmac
;
182 cdata
.Length
= *hmacLen
;
183 crtn
= CSSM_GenerateMacFinal(hmacCtx
->ccHand
, &cdata
);
187 *hmacLen
= cdata
.Length
;
192 static OSStatus
HMAC_Hmac (
193 HMACContextRef hmacCtx
,
196 void *hmac
, // mallocd by caller
197 unsigned *hmacLen
) // IN/OUT
200 const HMACReference
*hmacRef
;
202 if(hmacCtx
== NULL
) {
203 return errSSLInternal
;
205 hmacRef
= hmacCtx
->hmac
;
206 assert(hmacRef
!= NULL
);
207 serr
= hmacRef
->init(hmacCtx
);
211 serr
= hmacRef
->update(hmacCtx
, data
, dataLen
);
215 return hmacRef
->final(hmacCtx
, hmac
, hmacLen
);
218 #pragma mark *** Null HMAC ***
220 static OSStatus
HMAC_AllocNull(
221 const struct HMACReference
*hmac
,
225 HMACContextRef
*hmacCtx
) // RETURNED
231 static OSStatus
HMAC_FreeNull(
232 HMACContextRef hmacCtx
)
237 static OSStatus
HMAC_InitNull(
238 HMACContextRef hmacCtx
)
243 static OSStatus
HMAC_UpdateNull(
244 HMACContextRef hmacCtx
,
251 static OSStatus
HMAC_FinalNull(
252 HMACContextRef hmacCtx
,
253 void *hmac
, // mallocd by caller
254 unsigned *hmacLen
) // IN/OUT
259 static OSStatus
HMAC_HmacNull (
260 HMACContextRef hmacCtx
,
263 void *hmac
, // mallocd by caller
269 const HMACReference TlsHmacNull
= {
280 const HMACReference TlsHmacSHA1
= {
291 const HMACReference TlsHmacMD5
= {
302 const HashHmacReference HashHmacNull
= {
307 const HashHmacReference HashHmacMD5
= {
312 const HashHmacReference HashHmacSHA1
= {