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