| 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 <libkern/libkern.h> | 
|---|
| 32 | #include <kern/debug.h> | 
|---|
| 33 | #include <corecrypto/ccdigest.h> | 
|---|
| 34 |  | 
|---|
| 35 | #if defined(CRYPTO_SHA2) | 
|---|
| 36 |  | 
|---|
| 37 | void | 
|---|
| 38 | SHA256_Init(SHA256_CTX *ctx) | 
|---|
| 39 | { | 
|---|
| 40 | const struct ccdigest_info *di; | 
|---|
| 41 | di = g_crypto_funcs->ccsha256_di; | 
|---|
| 42 |  | 
|---|
| 43 | /* Make sure the context size for the digest info fits in the one we have */ | 
|---|
| 44 | if (ccdigest_di_size(di) > sizeof(SHA256_CTX)) { | 
|---|
| 45 | panic( "%s: inconsistent size for SHA256 context", __FUNCTION__); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | g_crypto_funcs->ccdigest_init_fn(di, ctx->ctx); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | void | 
|---|
| 52 | SHA256_Update(SHA256_CTX *ctx, const void *data, size_t len) | 
|---|
| 53 | { | 
|---|
| 54 | const struct ccdigest_info *di; | 
|---|
| 55 | di = g_crypto_funcs->ccsha256_di; | 
|---|
| 56 |  | 
|---|
| 57 | g_crypto_funcs->ccdigest_update_fn(di, ctx->ctx, len, data); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void | 
|---|
| 61 | SHA256_Final(void *digest, SHA256_CTX *ctx) | 
|---|
| 62 | { | 
|---|
| 63 | const struct ccdigest_info *di; | 
|---|
| 64 | di = g_crypto_funcs->ccsha256_di; | 
|---|
| 65 |  | 
|---|
| 66 | ccdigest_final(di, ctx: ctx->ctx, digest); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void | 
|---|
| 70 | SHA384_Init(SHA384_CTX *ctx) | 
|---|
| 71 | { | 
|---|
| 72 | const struct ccdigest_info *di; | 
|---|
| 73 | di = g_crypto_funcs->ccsha384_di; | 
|---|
| 74 |  | 
|---|
| 75 | /* Make sure the context size for the digest info fits in the one we have */ | 
|---|
| 76 | if (ccdigest_di_size(di) > sizeof(SHA384_CTX)) { | 
|---|
| 77 | panic( "%s: inconsistent size for SHA384 context", __FUNCTION__); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | g_crypto_funcs->ccdigest_init_fn(di, ctx->ctx); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void | 
|---|
| 84 | SHA384_Update(SHA384_CTX *ctx, const void *data, size_t len) | 
|---|
| 85 | { | 
|---|
| 86 | const struct ccdigest_info *di; | 
|---|
| 87 | di = g_crypto_funcs->ccsha384_di; | 
|---|
| 88 |  | 
|---|
| 89 | g_crypto_funcs->ccdigest_update_fn(di, ctx->ctx, len, data); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | void | 
|---|
| 94 | SHA384_Final(void *digest, SHA384_CTX *ctx) | 
|---|
| 95 | { | 
|---|
| 96 | const struct ccdigest_info *di; | 
|---|
| 97 | di = g_crypto_funcs->ccsha384_di; | 
|---|
| 98 |  | 
|---|
| 99 | ccdigest_final(di, ctx: ctx->ctx, digest); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void | 
|---|
| 103 | SHA512_Init(SHA512_CTX *ctx) | 
|---|
| 104 | { | 
|---|
| 105 | const struct ccdigest_info *di; | 
|---|
| 106 | di = g_crypto_funcs->ccsha512_di; | 
|---|
| 107 |  | 
|---|
| 108 | /* Make sure the context size for the digest info fits in the one we have */ | 
|---|
| 109 | if (ccdigest_di_size(di) > sizeof(SHA512_CTX)) { | 
|---|
| 110 | panic( "%s: inconsistent size for SHA512 context", __FUNCTION__); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | g_crypto_funcs->ccdigest_init_fn(di, ctx->ctx); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void | 
|---|
| 117 | SHA512_Update(SHA512_CTX *ctx, const void *data, size_t len) | 
|---|
| 118 | { | 
|---|
| 119 | const struct ccdigest_info *di; | 
|---|
| 120 | di = g_crypto_funcs->ccsha512_di; | 
|---|
| 121 |  | 
|---|
| 122 | g_crypto_funcs->ccdigest_update_fn(di, ctx->ctx, len, data); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void | 
|---|
| 126 | SHA512_Final(void *digest, SHA512_CTX *ctx) | 
|---|
| 127 | { | 
|---|
| 128 | const struct ccdigest_info *di; | 
|---|
| 129 | di = g_crypto_funcs->ccsha512_di; | 
|---|
| 130 |  | 
|---|
| 131 | ccdigest_final(di, ctx: ctx->ctx, digest); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | #else | 
|---|
| 135 |  | 
|---|
| 136 | /* As these are part of the KPI, we need to stub them out for any kernel configuration that does not support SHA2. */ | 
|---|
| 137 |  | 
|---|
| 138 | void UNSUPPORTED_API(SHA256_Init, SHA256_CTX *ctx); | 
|---|
| 139 | void UNSUPPORTED_API(SHA384_Init, SHA384_CTX *ctx); | 
|---|
| 140 | void UNSUPPORTED_API(SHA512_Init, SHA512_CTX *ctx); | 
|---|
| 141 | void UNSUPPORTED_API(SHA256_Update, SHA256_CTX *ctx, const void *data, size_t len); | 
|---|
| 142 | void UNSUPPORTED_API(SHA384_Update, SHA384_CTX *ctx, const void *data, size_t len); | 
|---|
| 143 | void UNSUPPORTED_API(SHA512_Update, SHA512_CTX *ctx, const void *data, size_t len); | 
|---|
| 144 | void UNSUPPORTED_API(SHA256_Final, void *digest, SHA256_CTX *ctx); | 
|---|
| 145 | void UNSUPPORTED_API(SHA384_Final, void *digest, SHA384_CTX *ctx); | 
|---|
| 146 | void UNSUPPORTED_API(SHA512_Final, void *digest, SHA512_CTX *ctx); | 
|---|
| 147 |  | 
|---|
| 148 | #endif | 
|---|
| 149 |  | 
|---|