1 #ifdef HAVE_DIX_CONFIG_H
2 #include <dix-config.h>
3 #endif
5 #include "os.h"
6 #include "xsha1.h"
8 #if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \
9 || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
11 # include <sha1.h>
13 void *x_sha1_init(void)
14 {
15 SHA1_CTX *ctx = malloc(sizeof(*ctx));
16 if (!ctx)
17 return NULL;
18 SHA1Init(ctx);
19 return ctx;
20 }
22 int x_sha1_update(void *ctx, void *data, int size)
23 {
24 SHA1_CTX *sha1_ctx = ctx;
25 SHA1Update(sha1_ctx, data, size);
26 return 1;
27 }
29 int x_sha1_final(void *ctx, unsigned char result[20])
30 {
31 SHA1_CTX *sha1_ctx = ctx;
32 SHA1Final(result, sha1_ctx);
33 free(sha1_ctx);
34 return 1;
35 }
37 #elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
39 #include <CommonCrypto/CommonDigest.h>
41 void *x_sha1_init(void)
42 {
43 CC_SHA1_CTX *ctx = malloc(sizeof(*ctx));
44 if (!ctx)
45 return NULL;
46 CC_SHA1_Init(ctx);
47 return ctx;
48 }
50 int x_sha1_update(void *ctx, void *data, int size)
51 {
52 CC_SHA1_CTX *sha1_ctx = ctx;
53 CC_SHA1_Update(sha1_ctx, data, size);
54 return 1;
55 }
57 int x_sha1_final(void *ctx, unsigned char result[20])
58 {
59 CC_SHA1_CTX *sha1_ctx = ctx;
60 CC_SHA1_Final(result, sha1_ctx);
61 free(sha1_ctx);
62 return 1;
63 }
65 #elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
67 # include <gcrypt.h>
69 void *x_sha1_init(void)
70 {
71 static int init;
72 gcry_md_hd_t h;
73 gcry_error_t err;
75 if (!init) {
76 if (!gcry_check_version(NULL))
77 return NULL;
78 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
79 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
80 init = 1;
81 }
83 err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
84 if (err)
85 return NULL;
86 return h;
87 }
89 int x_sha1_update(void *ctx, void *data, int size)
90 {
91 gcry_md_hd_t h = ctx;
92 gcry_md_write(h, data, size);
93 return 1;
94 }
96 int x_sha1_final(void *ctx, unsigned char result[20])
97 {
98 gcry_md_hd_t h = ctx;
99 memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20);
100 gcry_md_close(h);
101 return 1;
102 }
104 #elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
106 # include <libsha1.h>
108 void *x_sha1_init(void)
109 {
110 sha1_ctx *ctx = malloc(sizeof(*ctx));
111 if(!ctx)
112 return NULL;
113 sha1_begin(ctx);
114 return ctx;
115 }
117 int x_sha1_update(void *ctx, void *data, int size)
118 {
119 sha1_hash(data, size, ctx);
120 return 1;
121 }
123 int x_sha1_final(void *ctx, unsigned char result[20])
124 {
125 sha1_end(result, ctx);
126 free(ctx);
127 return 1;
128 }
130 #else /* Use OpenSSL's libcrypto */
132 # include <stddef.h> /* buggy openssl/sha.h wants size_t */
133 # include <openssl/sha.h>
135 void *x_sha1_init(void)
136 {
137 int ret;
138 SHA_CTX *ctx = malloc(sizeof(*ctx));
139 if (!ctx)
140 return NULL;
141 ret = SHA1_Init(ctx);
142 if (!ret) {
143 free(ctx);
144 return NULL;
145 }
146 return ctx;
147 }
149 int x_sha1_update(void *ctx, void *data, int size)
150 {
151 int ret;
152 SHA_CTX *sha_ctx = ctx;
153 ret = SHA1_Update(sha_ctx, data, size);
154 if (!ret)
155 free(sha_ctx);
156 return ret;
157 }
159 int x_sha1_final(void *ctx, unsigned char result[20])
160 {
161 int ret;
162 SHA_CTX *sha_ctx = ctx;
163 ret = SHA1_Final(result, sha_ctx);
164 free(sha_ctx);
165 return ret;
166 }
168 #endif