1
2#if defined(__arm__)
3#include "localcrypto_sha1.c" /* We can't use corecrypto.kext yet, so use local version for now. */
4#else
5
6#include <libkern/crypto/crypto_internal.h>
7#include <libkern/crypto/sha1.h>
8#include <kern/debug.h>
9#include <corecrypto/ccdigest.h>
10
11
12static uint64_t getCount(SHA1_CTX *ctx)
13{
14	return ctx->c.b64[0];
15}
16
17static void setCount(SHA1_CTX *ctx, uint64_t count)
18{
19	ctx->c.b64[0]=count;
20}
21
22/* Copy a ccdigest ctx into a legacy SHA1 context */
23static void DiToSHA1(const struct ccdigest_info *di, struct ccdigest_ctx *di_ctx, SHA1_CTX *sha1_ctx)
24{
25	setCount(sha1_ctx, ccdigest_nbits(di, di_ctx)/8+ccdigest_num(di, di_ctx));
26	memcpy(sha1_ctx->m.b8, ccdigest_data(di, di_ctx), di->block_size);
27	memcpy(sha1_ctx->h.b8, ccdigest_state_ccn(di, di_ctx), di->state_size);
28}
29
30/* Copy a legacy SHA1 context into a ccdigest ctx  */
31static void SHA1ToDi(const struct ccdigest_info *di, SHA1_CTX *sha1_ctx, struct ccdigest_ctx *di_ctx)
32{
33	uint64_t count = getCount(sha1_ctx);
34
35	ccdigest_num(di, di_ctx)=count%di->block_size;
36	ccdigest_nbits(di, di_ctx)=(count-ccdigest_num(di, di_ctx))*8;
37	memcpy(ccdigest_data(di, di_ctx), sha1_ctx->m.b8, di->block_size);
38	memcpy(ccdigest_state_ccn(di, di_ctx), sha1_ctx->h.b8, di->state_size);
39}
40
41void SHA1Init(SHA1_CTX *ctx)
42{
43	const struct ccdigest_info *di=g_crypto_funcs->ccsha1_di;
44	ccdigest_di_decl(di, di_ctx);
45
46	g_crypto_funcs->ccdigest_init_fn(di, di_ctx);
47
48	DiToSHA1(di, di_ctx, ctx);
49}
50
51void SHA1Update(SHA1_CTX *ctx, const void *data, size_t len)
52{
53	const struct ccdigest_info *di=g_crypto_funcs->ccsha1_di;
54	ccdigest_di_decl(di, di_ctx);
55
56	SHA1ToDi(di, ctx, di_ctx);
57	g_crypto_funcs->ccdigest_update_fn(di, di_ctx, len, data);
58	DiToSHA1(di, di_ctx, ctx);
59}
60
61void SHA1Final(void *digest, SHA1_CTX *ctx)
62{
63	const struct ccdigest_info *di=g_crypto_funcs->ccsha1_di;
64	ccdigest_di_decl(di, di_ctx);
65
66	SHA1ToDi(di, ctx, di_ctx);
67	ccdigest_final(di, di_ctx, digest);
68}
69
70#ifdef XNU_KERNEL_PRIVATE
71void SHA1UpdateUsePhysicalAddress(SHA1_CTX *ctx, const void *data, size_t len)
72{
73	//TODO: What the hell ?
74	SHA1Update(ctx, data, len);
75}
76#endif
77
78/* This is not publicised in header, but exported in libkern.exports */
79void SHA1Final_r(SHA1_CTX *context, void *digest);
80void SHA1Final_r(SHA1_CTX *context, void *digest)
81{
82	SHA1Final(digest, context);
83}
84
85
86/*
87 * This function is called by the SHA1 hardware kext during its init.
88 * This will register the function to call to perform SHA1 using hardware.
89 */
90#include <sys/types.h>
91#include <libkern/OSAtomic.h>
92#include <sys/systm.h>
93
94typedef kern_return_t (*InKernelPerformSHA1Func)(void *ref, const void *data, size_t dataLen, u_int32_t *inHash, u_int32_t options, u_int32_t *outHash, Boolean usePhysicalAddress);
95void sha1_hardware_hook(Boolean option, InKernelPerformSHA1Func func, void *ref);
96static void *SHA1Ref;
97static InKernelPerformSHA1Func performSHA1WithinKernelOnly;
98
99void sha1_hardware_hook(Boolean option, InKernelPerformSHA1Func func, void *ref)
100{
101	if(option) {
102		// Establish the hook. The hardware is ready.
103		OSCompareAndSwapPtr((void*)NULL, (void*)ref, (void * volatile*)&SHA1Ref);
104
105		if(!OSCompareAndSwapPtr((void *)NULL, (void *)func, (void * volatile *)&performSHA1WithinKernelOnly)) {
106			panic("sha1_hardware_hook: Called twice.. Should never happen\n");
107		}
108	}
109	else {
110		// The hardware is going away. Tear down the hook.
111		performSHA1WithinKernelOnly = NULL;
112		SHA1Ref = NULL;
113	}
114}
115
116#endif /* !__arm__ */
117