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