xsha1.c revision 706f2543
1#ifdef HAVE_DIX_CONFIG_H
2#include <dix-config.h>
3#endif
4
5#include "os.h"
6#include "xsha1.h"
7
8#if defined(HAVE_SHA1_IN_LIBMD)  /* Use libmd for SHA1 */ \
9	|| defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
10
11# include <sha1.h>
12
13void *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}
21
22int 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}
28
29int 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}
36
37#elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
38
39#include <CommonCrypto/CommonDigest.h>
40
41void *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}
49
50int 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}
56
57int 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}
64
65#elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
66
67# include <gcrypt.h>
68
69void *x_sha1_init(void)
70{
71    static int init;
72    gcry_md_hd_t h;
73    gcry_error_t err;
74
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    }
82
83    err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
84    if (err)
85        return NULL;
86    return h;
87}
88
89int 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}
95
96int 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}
103
104#elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
105
106# include <libsha1.h>
107
108void *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}
116
117int x_sha1_update(void *ctx, void *data, int size)
118{
119    sha1_hash(data, size, ctx);
120    return 1;
121}
122
123int x_sha1_final(void *ctx, unsigned char result[20])
124{
125    sha1_end(result, ctx);
126    free(ctx);
127    return 1;
128}
129
130#else /* Use OpenSSL's libcrypto */
131
132# include <stddef.h>  /* buggy openssl/sha.h wants size_t */
133# include <openssl/sha.h>
134
135void *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}
148
149int 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}
158
159int 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}
167
168#endif
169