xsha1.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #ifdef HAVE_DIX_CONFIG_H
  2. #include <dix-config.h>
  3. #endif
  4. #include "os.h"
  5. #include "xsha1.h"
  6. #if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \
  7. || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
  8. #include <sha1.h>
  9. void *
  10. x_sha1_init(void)
  11. {
  12. SHA1_CTX *ctx = malloc(sizeof(*ctx));
  13. if (!ctx)
  14. return NULL;
  15. SHA1Init(ctx);
  16. return ctx;
  17. }
  18. int
  19. x_sha1_update(void *ctx, void *data, int size)
  20. {
  21. SHA1_CTX *sha1_ctx = ctx;
  22. SHA1Update(sha1_ctx, data, size);
  23. return 1;
  24. }
  25. int
  26. x_sha1_final(void *ctx, unsigned char result[20])
  27. {
  28. SHA1_CTX *sha1_ctx = ctx;
  29. SHA1Final(result, sha1_ctx);
  30. free(sha1_ctx);
  31. return 1;
  32. }
  33. #elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
  34. #include <CommonCrypto/CommonDigest.h>
  35. void *
  36. x_sha1_init(void)
  37. {
  38. CC_SHA1_CTX *ctx = malloc(sizeof(*ctx));
  39. if (!ctx)
  40. return NULL;
  41. CC_SHA1_Init(ctx);
  42. return ctx;
  43. }
  44. int
  45. x_sha1_update(void *ctx, void *data, int size)
  46. {
  47. CC_SHA1_CTX *sha1_ctx = ctx;
  48. CC_SHA1_Update(sha1_ctx, data, size);
  49. return 1;
  50. }
  51. int
  52. x_sha1_final(void *ctx, unsigned char result[20])
  53. {
  54. CC_SHA1_CTX *sha1_ctx = ctx;
  55. CC_SHA1_Final(result, sha1_ctx);
  56. free(sha1_ctx);
  57. return 1;
  58. }
  59. #elif defined(HAVE_SHA1_IN_CRYPTOAPI) /* Use CryptoAPI for SHA1 */
  60. #define WIN32_LEAN_AND_MEAN
  61. #include <X11/Xwindows.h>
  62. #include <wincrypt.h>
  63. static HCRYPTPROV hProv;
  64. void *
  65. x_sha1_init(void)
  66. {
  67. HCRYPTHASH *ctx = malloc(sizeof(*ctx));
  68. if (!ctx)
  69. return NULL;
  70. CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  71. CryptCreateHash(hProv, CALG_SHA1, 0, 0, ctx);
  72. return ctx;
  73. }
  74. int
  75. x_sha1_update(void *ctx, void *data, int size)
  76. {
  77. HCRYPTHASH *hHash = ctx;
  78. CryptHashData(*hHash, data, size, 0);
  79. return 1;
  80. }
  81. int
  82. x_sha1_final(void *ctx, unsigned char result[20])
  83. {
  84. HCRYPTHASH *hHash = ctx;
  85. DWORD len = 20;
  86. CryptGetHashParam(*hHash, HP_HASHVAL, result, &len, 0);
  87. CryptDestroyHash(*hHash);
  88. CryptReleaseContext(hProv, 0);
  89. free(ctx);
  90. return 1;
  91. }
  92. #elif defined(HAVE_SHA1_IN_LIBNETTLE) /* Use libnettle for SHA1 */
  93. #include <nettle/sha.h>
  94. void *
  95. x_sha1_init(void)
  96. {
  97. struct sha1_ctx *ctx = malloc(sizeof(*ctx));
  98. if (!ctx)
  99. return NULL;
  100. sha1_init(ctx);
  101. return ctx;
  102. }
  103. int
  104. x_sha1_update(void *ctx, void *data, int size)
  105. {
  106. sha1_update(ctx, size, data);
  107. return 1;
  108. }
  109. int
  110. x_sha1_final(void *ctx, unsigned char result[20])
  111. {
  112. sha1_digest(ctx, 20, result);
  113. free(ctx);
  114. return 1;
  115. }
  116. #elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
  117. #include <gcrypt.h>
  118. void *
  119. x_sha1_init(void)
  120. {
  121. static int init;
  122. gcry_md_hd_t h;
  123. gcry_error_t err;
  124. if (!init) {
  125. if (!gcry_check_version(NULL))
  126. return NULL;
  127. gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
  128. gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
  129. init = 1;
  130. }
  131. err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
  132. if (err)
  133. return NULL;
  134. return h;
  135. }
  136. int
  137. x_sha1_update(void *ctx, void *data, int size)
  138. {
  139. gcry_md_hd_t h = ctx;
  140. gcry_md_write(h, data, size);
  141. return 1;
  142. }
  143. int
  144. x_sha1_final(void *ctx, unsigned char result[20])
  145. {
  146. gcry_md_hd_t h = ctx;
  147. memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20);
  148. gcry_md_close(h);
  149. return 1;
  150. }
  151. #elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
  152. #include <libsha1.h>
  153. void *
  154. x_sha1_init(void)
  155. {
  156. sha1_ctx *ctx = malloc(sizeof(*ctx));
  157. if (!ctx)
  158. return NULL;
  159. sha1_begin(ctx);
  160. return ctx;
  161. }
  162. int
  163. x_sha1_update(void *ctx, void *data, int size)
  164. {
  165. sha1_hash(data, size, ctx);
  166. return 1;
  167. }
  168. int
  169. x_sha1_final(void *ctx, unsigned char result[20])
  170. {
  171. sha1_end(result, ctx);
  172. free(ctx);
  173. return 1;
  174. }
  175. #else /* Use OpenSSL's libcrypto */
  176. #include <stddef.h> /* buggy openssl/sha.h wants size_t */
  177. #include <openssl/sha.h>
  178. void *
  179. x_sha1_init(void)
  180. {
  181. int ret;
  182. SHA_CTX *ctx = malloc(sizeof(*ctx));
  183. if (!ctx)
  184. return NULL;
  185. ret = SHA1_Init(ctx);
  186. if (!ret) {
  187. free(ctx);
  188. return NULL;
  189. }
  190. return ctx;
  191. }
  192. int
  193. x_sha1_update(void *ctx, void *data, int size)
  194. {
  195. int ret;
  196. SHA_CTX *sha_ctx = ctx;
  197. ret = SHA1_Update(sha_ctx, data, size);
  198. if (!ret)
  199. free(sha_ctx);
  200. return ret;
  201. }
  202. int
  203. x_sha1_final(void *ctx, unsigned char result[20])
  204. {
  205. int ret;
  206. SHA_CTX *sha_ctx = ctx;
  207. ret = SHA1_Final(result, sha_ctx);
  208. free(sha_ctx);
  209. return ret;
  210. }
  211. #endif