]> git.saurik.com Git - apple/xnu.git/blob - libkern/crypto/corecrypto_sha2.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / libkern / crypto / corecrypto_sha2.c
1 /*
2 * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <libkern/crypto/crypto_internal.h>
30 #include <libkern/crypto/sha2.h>
31 #include <kern/debug.h>
32 #include <corecrypto/ccdigest.h>
33
34 #if defined(CRYPTO_SHA2)
35
36 void SHA256_Init(SHA256_CTX *ctx)
37 {
38 const struct ccdigest_info *di;
39 di=g_crypto_funcs->ccsha256_di;
40
41 /* Make sure the context size for the digest info fits in the one we have */
42 if(ccdigest_di_size(di)>sizeof(SHA256_CTX))
43 panic("%s: inconsistent size for SHA256 context", __FUNCTION__);
44
45 g_crypto_funcs->ccdigest_init_fn(di, ctx->ctx);
46 }
47
48 void SHA256_Update(SHA256_CTX *ctx, const void *data, size_t len)
49 {
50 const struct ccdigest_info *di;
51 di=g_crypto_funcs->ccsha256_di;
52
53 g_crypto_funcs->ccdigest_update_fn(di, ctx->ctx, len, data);
54 }
55
56 void SHA256_Final(void *digest, SHA256_CTX *ctx)
57 {
58 const struct ccdigest_info *di;
59 di=g_crypto_funcs->ccsha256_di;
60
61 ccdigest_final(di, ctx->ctx, digest);
62 }
63
64 void SHA384_Init(SHA384_CTX *ctx)
65 {
66 const struct ccdigest_info *di;
67 di=g_crypto_funcs->ccsha384_di;
68
69 /* Make sure the context size for the digest info fits in the one we have */
70 if(ccdigest_di_size(di)>sizeof(SHA384_CTX))
71 panic("%s: inconsistent size for SHA384 context", __FUNCTION__);
72
73 g_crypto_funcs->ccdigest_init_fn(di, ctx->ctx);
74 }
75
76 void SHA384_Update(SHA384_CTX *ctx, const void *data, size_t len)
77 {
78 const struct ccdigest_info *di;
79 di=g_crypto_funcs->ccsha384_di;
80
81 g_crypto_funcs->ccdigest_update_fn(di, ctx->ctx, len, data);
82 }
83
84
85 void SHA384_Final(void *digest, SHA384_CTX *ctx)
86 {
87 const struct ccdigest_info *di;
88 di=g_crypto_funcs->ccsha384_di;
89
90 ccdigest_final(di, ctx->ctx, digest);
91 }
92
93 void SHA512_Init(SHA512_CTX *ctx)
94 {
95 const struct ccdigest_info *di;
96 di=g_crypto_funcs->ccsha512_di;
97
98 /* Make sure the context size for the digest info fits in the one we have */
99 if(ccdigest_di_size(di)>sizeof(SHA512_CTX))
100 panic("%s: inconsistent size for SHA512 context", __FUNCTION__);
101
102 g_crypto_funcs->ccdigest_init_fn(di, ctx->ctx);
103 }
104
105 void SHA512_Update(SHA512_CTX *ctx, const void *data, size_t len)
106 {
107 const struct ccdigest_info *di;
108 di=g_crypto_funcs->ccsha512_di;
109
110 g_crypto_funcs->ccdigest_update_fn(di, ctx->ctx, len, data);
111 }
112
113 void SHA512_Final(void *digest, SHA512_CTX *ctx)
114 {
115 const struct ccdigest_info *di;
116 di=g_crypto_funcs->ccsha512_di;
117
118 ccdigest_final(di, ctx->ctx, digest);
119 }
120
121 #else
122
123 /* As these are part of the KPI, we need to stub them out for any kernle cofiguration that does not support SHA2. */
124
125 void SHA384_Init(__unused SHA384_CTX *ctx)
126 {
127 panic("SHA384_Init");
128 }
129
130 void SHA384_Update(__unused SHA384_CTX *ctx, __unused const void *data, __unused size_t len)
131 {
132 panic("SHA384_Update");
133 }
134
135 void SHA384_Final(__unused void *digest, __unused SHA384_CTX *ctx)
136 {
137 panic("SHA384_Final");
138 }
139
140 #endif
141