2 * Copyright (c) 2000-2001,2005-2007,2010-2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * sslDigests.c - Interface between SSL and SHA, MD5 digest implementations
28 #include "sslContext.h"
29 #include "cryptType.h"
30 #include "sslMemory.h"
31 #include "sslDigests.h"
34 #include <CommonCrypto/CommonDigest.h>
37 #define DIGEST_PRINT 0
39 #define dgprintf(s) printf s
44 const UInt8 SSLMACPad1
[MAX_MAC_PADDING
] =
46 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
47 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
48 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
49 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
50 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
51 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36
54 const UInt8 SSLMACPad2
[MAX_MAC_PADDING
] =
56 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
57 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
58 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
59 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
60 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
61 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C
65 * Public general hash functions
69 * A convenience wrapper for HashReference.clone, which has the added benefit of
70 * allocating the state buffer for the caller.
74 const HashReference
*ref
,
75 const SSLBuffer
*state
,
80 if ((err
= SSLAllocBuffer(newState
, ref
->contextSize
, ctx
)) != 0)
82 return ref
->clone(state
, newState
);
86 * Wrapper for HashReference.init.
89 ReadyHash(const HashReference
*ref
, SSLBuffer
*state
, SSLContext
*ctx
)
92 if ((err
= SSLAllocBuffer(state
, ref
->contextSize
, ctx
)) != 0)
94 return ref
->init(state
, ctx
);
98 * Wrapper for HashReference.close. Tolerates NULL state and frees it if it's
101 OSStatus
CloseHash(const HashReference
*ref
, SSLBuffer
*state
, SSLContext
*ctx
)
105 if(state
->data
== NULL
) {
108 serr
= ref
->close(state
, ctx
);
112 return SSLFreeBuffer(state
, ctx
);
117 static OSStatus
HashNullInit(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
) {
120 static OSStatus
HashNullUpdate(SSLBuffer
*digestCtx
, const SSLBuffer
*data
) {
123 static OSStatus
HashNullFinal(SSLBuffer
*digestCtx
, SSLBuffer
*digest
) {
126 static OSStatus
HashNullClose(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
) {
129 static OSStatus
HashNullClone(const SSLBuffer
*src
, SSLBuffer
*dest
) {
134 static OSStatus
HashMD5Init(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
136 assert(digestCtx
->length
>= sizeof(CC_MD5_CTX
));
137 CC_MD5_CTX
*ctx
= (CC_MD5_CTX
*)digestCtx
->data
;
139 dgprintf(("###HashMD5Init ctx %p\n", ctx
));
143 static OSStatus
HashMD5Update(SSLBuffer
*digestCtx
, const SSLBuffer
*data
)
145 /* 64 bits cast: safe, SSL records are always smaller than 2^32 bytes */
146 assert(digestCtx
->length
>= sizeof(CC_MD5_CTX
));
147 CC_MD5_CTX
*ctx
= (CC_MD5_CTX
*)digestCtx
->data
;
148 CC_MD5_Update(ctx
, data
->data
, (CC_LONG
)data
->length
);
152 static OSStatus
HashMD5Final(SSLBuffer
*digestCtx
, SSLBuffer
*digest
)
154 assert(digestCtx
->length
>= sizeof(CC_MD5_CTX
));
155 CC_MD5_CTX
*ctx
= (CC_MD5_CTX
*)digestCtx
->data
;
156 dgprintf(("###HashMD5Final ctx %p\n", ctx
));
157 assert(digest
->length
>= CC_MD5_DIGEST_LENGTH
);
158 //if (digest->length < CC_MD5_DIGEST_LENGTH)
159 // return errSSLCrypto;
160 CC_MD5_Final(digest
->data
, ctx
);
161 digest
->length
= CC_MD5_DIGEST_LENGTH
;
165 static OSStatus
HashMD5Close(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
167 assert(digestCtx
->length
>= sizeof(CC_MD5_CTX
));
171 static OSStatus
HashMD5Clone(const SSLBuffer
*src
, SSLBuffer
*dst
)
176 assert(src
->length
>= sizeof(CC_MD5_CTX
));
177 assert(dst
->length
>= sizeof(CC_MD5_CTX
));
179 srcCtx
= (CC_MD5_CTX
*)src
->data
;
180 dstCtx
= (CC_MD5_CTX
*)dst
->data
;
181 dgprintf(("###HashMD5Clone srcCtx %p dstCtx %p\n", srcCtx
, dstCtx
));
183 memcpy(dstCtx
, srcCtx
, sizeof(CC_MD5_CTX
));
188 static OSStatus
HashSHA1Init(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
190 assert(digestCtx
->length
>= sizeof(CC_SHA1_CTX
));
191 CC_SHA1_CTX
*ctx
= (CC_SHA1_CTX
*)digestCtx
->data
;
193 dgprintf(("###HashSHA1Init ctx %p\n", ctx
));
197 static OSStatus
HashSHA1Update(SSLBuffer
*digestCtx
, const SSLBuffer
*data
)
199 /* 64 bits cast: safe, SSL records are always smaller than 2^32 bytes */
200 assert(digestCtx
->length
>= sizeof(CC_SHA1_CTX
));
201 CC_SHA1_CTX
*ctx
= (CC_SHA1_CTX
*)digestCtx
->data
;
202 CC_SHA1_Update(ctx
, data
->data
, (CC_LONG
)data
->length
);
206 static OSStatus
HashSHA1Final(SSLBuffer
*digestCtx
, SSLBuffer
*digest
)
208 assert(digestCtx
->length
>= sizeof(CC_SHA1_CTX
));
209 CC_SHA1_CTX
*ctx
= (CC_SHA1_CTX
*)digestCtx
->data
;
210 dgprintf(("###HashSHA1Final ctx %p\n", ctx
));
211 assert(digest
->length
>= CC_SHA1_DIGEST_LENGTH
);
212 //if (digest->length < CC_SHA1_DIGEST_LENGTH)
213 // return errSSLCrypto;
214 CC_SHA1_Final(digest
->data
, ctx
);
215 digest
->length
= CC_SHA1_DIGEST_LENGTH
;
219 static OSStatus
HashSHA1Close(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
221 assert(digestCtx
->length
>= sizeof(CC_SHA1_CTX
));
225 static OSStatus
HashSHA1Clone(const SSLBuffer
*src
, SSLBuffer
*dst
)
230 assert(src
->length
>= sizeof(CC_SHA1_CTX
));
231 assert(dst
->length
>= sizeof(CC_SHA1_CTX
));
233 srcCtx
= (CC_SHA1_CTX
*)src
->data
;
234 dstCtx
= (CC_SHA1_CTX
*)dst
->data
;
235 dgprintf(("###HashSHA1Clone srcCtx %p dstCtx %p\n", srcCtx
, dstCtx
));
237 memcpy(dstCtx
, srcCtx
, sizeof(CC_SHA1_CTX
));
242 static OSStatus
HashSHA256Init(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
244 assert(digestCtx
->length
>= sizeof(CC_SHA256_CTX
));
245 CC_SHA256_CTX
*ctx
= (CC_SHA256_CTX
*)digestCtx
->data
;
247 dgprintf(("###HashSHA256Init ctx %p\n", ctx
));
251 static OSStatus
HashSHA256Update(SSLBuffer
*digestCtx
, const SSLBuffer
*data
)
253 /* 64 bits cast: safe, SSL records are always smaller than 2^32 bytes */
254 assert(digestCtx
->length
>= sizeof(CC_SHA256_CTX
));
255 CC_SHA256_CTX
*ctx
= (CC_SHA256_CTX
*)digestCtx
->data
;
256 CC_SHA256_Update(ctx
, data
->data
, (CC_LONG
)data
->length
);
260 static OSStatus
HashSHA256Final(SSLBuffer
*digestCtx
, SSLBuffer
*digest
)
262 assert(digestCtx
->length
>= sizeof(CC_SHA256_CTX
));
263 CC_SHA256_CTX
*ctx
= (CC_SHA256_CTX
*)digestCtx
->data
;
264 dgprintf(("###HashSHA256Final ctx %p\n", ctx
));
265 assert(digest
->length
>= CC_SHA256_DIGEST_LENGTH
);
266 //if (digest->length < CC_SHA256_DIGEST_LENGTH)
267 // return errSSLCrypto;
268 CC_SHA256_Final(digest
->data
, ctx
);
269 digest
->length
= CC_SHA256_DIGEST_LENGTH
;
273 static OSStatus
HashSHA256Close(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
275 assert(digestCtx
->length
>= sizeof(CC_SHA256_CTX
));
279 static OSStatus
HashSHA256Clone(const SSLBuffer
*src
, SSLBuffer
*dst
)
281 CC_SHA256_CTX
*srcCtx
;
282 CC_SHA256_CTX
*dstCtx
;
284 assert(src
->length
>= sizeof(CC_SHA256_CTX
));
285 assert(dst
->length
>= sizeof(CC_SHA256_CTX
));
287 srcCtx
= (CC_SHA256_CTX
*)src
->data
;
288 dstCtx
= (CC_SHA256_CTX
*)dst
->data
;
289 dgprintf(("###HashSHA256Clone srcCtx %p dstCtx %p\n", srcCtx
, dstCtx
));
291 memcpy(dstCtx
, srcCtx
, sizeof(CC_SHA256_CTX
));
296 static OSStatus
HashSHA384Init(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
298 assert(digestCtx
->length
>= sizeof(CC_SHA512_CTX
));
299 CC_SHA512_CTX
*ctx
= (CC_SHA512_CTX
*)digestCtx
->data
;
301 dgprintf(("###HashSHA384Init ctx %p\n", ctx
));
305 static OSStatus
HashSHA384Update(SSLBuffer
*digestCtx
, const SSLBuffer
*data
)
307 /* 64 bits cast: safe, SSL records are always smaller than 2^32 bytes */
308 assert(digestCtx
->length
>= sizeof(CC_SHA512_CTX
));
309 CC_SHA512_CTX
*ctx
= (CC_SHA512_CTX
*)digestCtx
->data
;
310 CC_SHA384_Update(ctx
, data
->data
, (CC_LONG
)data
->length
);
314 static OSStatus
HashSHA384Final(SSLBuffer
*digestCtx
, SSLBuffer
*digest
)
316 assert(digestCtx
->length
>= sizeof(CC_SHA512_CTX
));
317 CC_SHA512_CTX
*ctx
= (CC_SHA512_CTX
*)digestCtx
->data
;
318 dgprintf(("###HashSHA384Final ctx %p\n", ctx
));
319 assert(digest
->length
>= CC_SHA384_DIGEST_LENGTH
);
320 //if (digest->length < CC_SHA384_DIGEST_LENGTH)
321 // return errSSLCrypto;
322 CC_SHA384_Final(digest
->data
, ctx
);
323 digest
->length
= CC_SHA384_DIGEST_LENGTH
;
327 static OSStatus
HashSHA384Close(SSLBuffer
*digestCtx
, SSLContext
*sslCtx
)
329 assert(digestCtx
->length
>= sizeof(CC_SHA512_CTX
));
333 static OSStatus
HashSHA384Clone(const SSLBuffer
*src
, SSLBuffer
*dst
)
335 CC_SHA512_CTX
*srcCtx
;
336 CC_SHA512_CTX
*dstCtx
;
338 assert(src
->length
>= sizeof(CC_SHA512_CTX
));
339 assert(dst
->length
>= sizeof(CC_SHA512_CTX
));
341 srcCtx
= (CC_SHA512_CTX
*)src
->data
;
342 dstCtx
= (CC_SHA512_CTX
*)dst
->data
;
343 dgprintf(("###HashSHA384Clone srcCtx %p dstCtx %p\n", srcCtx
, dstCtx
));
345 memcpy(dstCtx
, srcCtx
, sizeof(CC_SHA512_CTX
));
350 * These are the handles by which the bulk of digesting work
353 const HashReference SSLHashNull
=
365 const HashReference SSLHashMD5
=
368 CC_MD5_DIGEST_LENGTH
,
377 const HashReference SSLHashSHA1
=
380 CC_SHA1_DIGEST_LENGTH
,
389 const HashReference SSLHashSHA256
=
391 sizeof(CC_SHA256_CTX
),
392 CC_SHA256_DIGEST_LENGTH
,
393 CC_SHA256_BLOCK_BYTES
,
401 const HashReference SSLHashSHA384
=
403 sizeof(CC_SHA512_CTX
),
404 CC_SHA384_DIGEST_LENGTH
,
405 CC_SHA384_BLOCK_BYTES
,