1706f2543Smrg#ifdef HAVE_DIX_CONFIG_H 2706f2543Smrg#include <dix-config.h> 3706f2543Smrg#endif 4706f2543Smrg 5706f2543Smrg#include "os.h" 6706f2543Smrg#include "xsha1.h" 7706f2543Smrg 8706f2543Smrg#if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \ 9706f2543Smrg || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */ 10706f2543Smrg 11706f2543Smrg# include <sha1.h> 12706f2543Smrg 13706f2543Smrgvoid *x_sha1_init(void) 14706f2543Smrg{ 15706f2543Smrg SHA1_CTX *ctx = malloc(sizeof(*ctx)); 16706f2543Smrg if (!ctx) 17706f2543Smrg return NULL; 18706f2543Smrg SHA1Init(ctx); 19706f2543Smrg return ctx; 20706f2543Smrg} 21706f2543Smrg 22706f2543Smrgint x_sha1_update(void *ctx, void *data, int size) 23706f2543Smrg{ 24706f2543Smrg SHA1_CTX *sha1_ctx = ctx; 25706f2543Smrg SHA1Update(sha1_ctx, data, size); 26706f2543Smrg return 1; 27706f2543Smrg} 28706f2543Smrg 29706f2543Smrgint x_sha1_final(void *ctx, unsigned char result[20]) 30706f2543Smrg{ 31706f2543Smrg SHA1_CTX *sha1_ctx = ctx; 32706f2543Smrg SHA1Final(result, sha1_ctx); 33706f2543Smrg free(sha1_ctx); 34706f2543Smrg return 1; 35706f2543Smrg} 36706f2543Smrg 37706f2543Smrg#elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */ 38706f2543Smrg 39706f2543Smrg#include <CommonCrypto/CommonDigest.h> 40706f2543Smrg 41706f2543Smrgvoid *x_sha1_init(void) 42706f2543Smrg{ 43706f2543Smrg CC_SHA1_CTX *ctx = malloc(sizeof(*ctx)); 44706f2543Smrg if (!ctx) 45706f2543Smrg return NULL; 46706f2543Smrg CC_SHA1_Init(ctx); 47706f2543Smrg return ctx; 48706f2543Smrg} 49706f2543Smrg 50706f2543Smrgint x_sha1_update(void *ctx, void *data, int size) 51706f2543Smrg{ 52706f2543Smrg CC_SHA1_CTX *sha1_ctx = ctx; 53706f2543Smrg CC_SHA1_Update(sha1_ctx, data, size); 54706f2543Smrg return 1; 55706f2543Smrg} 56706f2543Smrg 57706f2543Smrgint x_sha1_final(void *ctx, unsigned char result[20]) 58706f2543Smrg{ 59706f2543Smrg CC_SHA1_CTX *sha1_ctx = ctx; 60706f2543Smrg CC_SHA1_Final(result, sha1_ctx); 61706f2543Smrg free(sha1_ctx); 62706f2543Smrg return 1; 63706f2543Smrg} 64706f2543Smrg 65706f2543Smrg#elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */ 66706f2543Smrg 67706f2543Smrg# include <gcrypt.h> 68706f2543Smrg 69706f2543Smrgvoid *x_sha1_init(void) 70706f2543Smrg{ 71706f2543Smrg static int init; 72706f2543Smrg gcry_md_hd_t h; 73706f2543Smrg gcry_error_t err; 74706f2543Smrg 75706f2543Smrg if (!init) { 76706f2543Smrg if (!gcry_check_version(NULL)) 77706f2543Smrg return NULL; 78706f2543Smrg gcry_control(GCRYCTL_DISABLE_SECMEM, 0); 79706f2543Smrg gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); 80706f2543Smrg init = 1; 81706f2543Smrg } 82706f2543Smrg 83706f2543Smrg err = gcry_md_open(&h, GCRY_MD_SHA1, 0); 84706f2543Smrg if (err) 85706f2543Smrg return NULL; 86706f2543Smrg return h; 87706f2543Smrg} 88706f2543Smrg 89706f2543Smrgint x_sha1_update(void *ctx, void *data, int size) 90706f2543Smrg{ 91706f2543Smrg gcry_md_hd_t h = ctx; 92706f2543Smrg gcry_md_write(h, data, size); 93706f2543Smrg return 1; 94706f2543Smrg} 95706f2543Smrg 96706f2543Smrgint x_sha1_final(void *ctx, unsigned char result[20]) 97706f2543Smrg{ 98706f2543Smrg gcry_md_hd_t h = ctx; 99706f2543Smrg memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20); 100706f2543Smrg gcry_md_close(h); 101706f2543Smrg return 1; 102706f2543Smrg} 103706f2543Smrg 104706f2543Smrg#elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */ 105706f2543Smrg 106706f2543Smrg# include <libsha1.h> 107706f2543Smrg 108706f2543Smrgvoid *x_sha1_init(void) 109706f2543Smrg{ 110706f2543Smrg sha1_ctx *ctx = malloc(sizeof(*ctx)); 111706f2543Smrg if(!ctx) 112706f2543Smrg return NULL; 113706f2543Smrg sha1_begin(ctx); 114706f2543Smrg return ctx; 115706f2543Smrg} 116706f2543Smrg 117706f2543Smrgint x_sha1_update(void *ctx, void *data, int size) 118706f2543Smrg{ 119706f2543Smrg sha1_hash(data, size, ctx); 120706f2543Smrg return 1; 121706f2543Smrg} 122706f2543Smrg 123706f2543Smrgint x_sha1_final(void *ctx, unsigned char result[20]) 124706f2543Smrg{ 125706f2543Smrg sha1_end(result, ctx); 126706f2543Smrg free(ctx); 127706f2543Smrg return 1; 128706f2543Smrg} 129706f2543Smrg 130706f2543Smrg#else /* Use OpenSSL's libcrypto */ 131706f2543Smrg 132706f2543Smrg# include <stddef.h> /* buggy openssl/sha.h wants size_t */ 133706f2543Smrg# include <openssl/sha.h> 134706f2543Smrg 135706f2543Smrgvoid *x_sha1_init(void) 136706f2543Smrg{ 137706f2543Smrg int ret; 138706f2543Smrg SHA_CTX *ctx = malloc(sizeof(*ctx)); 139706f2543Smrg if (!ctx) 140706f2543Smrg return NULL; 141706f2543Smrg ret = SHA1_Init(ctx); 142706f2543Smrg if (!ret) { 143706f2543Smrg free(ctx); 144706f2543Smrg return NULL; 145706f2543Smrg } 146706f2543Smrg return ctx; 147706f2543Smrg} 148706f2543Smrg 149706f2543Smrgint x_sha1_update(void *ctx, void *data, int size) 150706f2543Smrg{ 151706f2543Smrg int ret; 152706f2543Smrg SHA_CTX *sha_ctx = ctx; 153706f2543Smrg ret = SHA1_Update(sha_ctx, data, size); 154706f2543Smrg if (!ret) 155706f2543Smrg free(sha_ctx); 156706f2543Smrg return ret; 157706f2543Smrg} 158706f2543Smrg 159706f2543Smrgint x_sha1_final(void *ctx, unsigned char result[20]) 160706f2543Smrg{ 161706f2543Smrg int ret; 162706f2543Smrg SHA_CTX *sha_ctx = ctx; 163706f2543Smrg ret = SHA1_Final(result, sha_ctx); 164706f2543Smrg free(sha_ctx); 165706f2543Smrg return ret; 166706f2543Smrg} 167706f2543Smrg 168706f2543Smrg#endif 169