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"
31 #include "cryptType.h"
35 #include <Security/cssm.h>
37 /* Per-session state, opaque to callers; all fields set at alloc time */
40 CSSM_CC_HANDLE ccHand
;
41 const struct HMACReference
*hmac
;
44 #pragma mark *** Common CDSA_based HMAC routines ***
46 /* Create an HMAC session */
47 static SSLErr
HMAC_Alloc(
48 const struct HMACReference
*hmac
,
52 HMACContextRef
*hmacCtx
) // RETURNED
58 HMACContextRef href
= sslMalloc(sizeof(struct HMACContext
));
68 * Since the key is present in the CDSA context, we cook up the context now.
69 * Currently we can't reuse an HMAC context if the key changes.
73 calg
= CSSM_ALGID_SHA1HMAC
;
76 calg
= CSSM_ALGID_MD5HMAC
;
80 return SSLInternalError
;
82 serr
= sslSetUpSymmKey(&cssmKey
,
84 CSSM_KEYUSE_SIGN
| CSSM_KEYUSE_VERIFY
,
85 CSSM_FALSE
, /* don't malloc/copy key */
91 if(attachToCsp(ctx
)) {
94 crtn
= CSSM_CSP_CreateMacContext(ctx
->cspHand
,
99 return SSLCryptoError
;
108 static SSLErr
HMAC_Free(
109 HMACContextRef hmacCtx
)
111 if(hmacCtx
!= NULL
) {
112 if(hmacCtx
->ccHand
!= 0) {
113 CSSM_DeleteContext(hmacCtx
->ccHand
);
122 static SSLErr
HMAC_Init(
123 HMACContextRef hmacCtx
)
127 if(hmacCtx
== NULL
) {
128 return SSLInternalError
;
130 assert(hmacCtx
->ctx
!= NULL
);
131 assert(hmacCtx
->hmac
!= NULL
);
132 assert(hmacCtx
->ccHand
!= 0);
134 crtn
= CSSM_GenerateMacInit(hmacCtx
->ccHand
);
136 return SSLCryptoError
;
141 /* normal crypt ops */
142 static SSLErr
HMAC_Update(
143 HMACContextRef hmacCtx
,
150 if(hmacCtx
== NULL
) {
151 return SSLInternalError
;
153 assert(hmacCtx
->ctx
!= NULL
);
154 assert(hmacCtx
->hmac
!= NULL
);
155 assert(hmacCtx
->ccHand
!= 0);
156 cdata
.Data
= (uint8
*)data
;
157 cdata
.Length
= dataLen
;
158 crtn
= CSSM_GenerateMacUpdate(hmacCtx
->ccHand
, &cdata
, 1);
160 return SSLCryptoError
;
165 static SSLErr
HMAC_Final(
166 HMACContextRef hmacCtx
,
167 void *hmac
, // mallocd by caller
168 unsigned *hmacLen
) // IN/OUT
173 if(hmacCtx
== NULL
) {
174 return SSLInternalError
;
176 if((hmac
== NULL
) || (hmacLen
== 0)) {
177 return SSLInternalError
;
179 assert(hmacCtx
->ctx
!= NULL
);
180 assert(hmacCtx
->hmac
!= NULL
);
181 assert(hmacCtx
->ccHand
!= 0);
182 cdata
.Data
= (uint8
*)hmac
;
183 cdata
.Length
= *hmacLen
;
184 crtn
= CSSM_GenerateMacFinal(hmacCtx
->ccHand
, &cdata
);
186 return SSLCryptoError
;
188 *hmacLen
= cdata
.Length
;
193 static SSLErr
HMAC_Hmac (
194 HMACContextRef hmacCtx
,
197 void *hmac
, // mallocd by caller
198 unsigned *hmacLen
) // IN/OUT
201 const HMACReference
*hmacRef
;
203 if(hmacCtx
== NULL
) {
204 return SSLInternalError
;
206 hmacRef
= hmacCtx
->hmac
;
207 assert(hmacRef
!= NULL
);
208 serr
= hmacRef
->init(hmacCtx
);
212 serr
= hmacRef
->update(hmacCtx
, data
, dataLen
);
216 return hmacRef
->final(hmacCtx
, hmac
, hmacLen
);
219 #pragma mark *** Null HMAC ***
221 static SSLErr
HMAC_AllocNull(
222 const struct HMACReference
*hmac
,
226 HMACContextRef
*hmacCtx
) // RETURNED
232 static SSLErr
HMAC_FreeNull(
233 HMACContextRef hmacCtx
)
238 static SSLErr
HMAC_InitNull(
239 HMACContextRef hmacCtx
)
244 static SSLErr
HMAC_UpdateNull(
245 HMACContextRef hmacCtx
,
252 static SSLErr
HMAC_FinalNull(
253 HMACContextRef hmacCtx
,
254 void *hmac
, // mallocd by caller
255 unsigned *hmacLen
) // IN/OUT
260 static SSLErr
HMAC_HmacNull (
261 HMACContextRef hmacCtx
,
264 void *hmac
, // mallocd by caller
270 const HMACReference TlsHmacNull
= {
281 const HMACReference TlsHmacSHA1
= {
292 const HMACReference TlsHmacMD5
= {
303 const HashHmacReference HashHmacNull
= {
308 const HashHmacReference HashHmacMD5
= {
313 const HashHmacReference HashHmacSHA1
= {