THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.8KB

  1. #ifndef HASH_H
  2. #define HASH_H
  3. #include "git-compat-util.h"
  4. #if defined(SHA1_PPC)
  5. #include "ppc/sha1.h"
  6. #elif defined(SHA1_APPLE)
  7. #include <CommonCrypto/CommonDigest.h>
  8. #elif defined(SHA1_OPENSSL)
  9. #include <openssl/sha.h>
  10. #elif defined(SHA1_DC)
  11. #include "sha1dc_git.h"
  12. #else /* SHA1_BLK */
  13. #include "block-sha1/sha1.h"
  14. #endif
  15. #if defined(SHA256_GCRYPT)
  16. #include "sha256/gcrypt.h"
  17. #elif defined(SHA256_OPENSSL)
  18. #include <openssl/sha.h>
  19. #else
  20. #include "sha256/block/sha256.h"
  21. #endif
  22. #ifndef platform_SHA_CTX
  23. /*
  24. * platform's underlying implementation of SHA-1; could be OpenSSL,
  25. * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
  26. * SHA-1 header may have already defined platform_SHA_CTX for our
  27. * own implementations like block-sha1 and ppc-sha1, so we list
  28. * the default for OpenSSL compatible SHA-1 implementations here.
  29. */
  30. #define platform_SHA_CTX SHA_CTX
  31. #define platform_SHA1_Init SHA1_Init
  32. #define platform_SHA1_Update SHA1_Update
  33. #define platform_SHA1_Final SHA1_Final
  34. #endif
  35. #define git_SHA_CTX platform_SHA_CTX
  36. #define git_SHA1_Init platform_SHA1_Init
  37. #define git_SHA1_Update platform_SHA1_Update
  38. #define git_SHA1_Final platform_SHA1_Final
  39. #ifndef platform_SHA256_CTX
  40. #define platform_SHA256_CTX SHA256_CTX
  41. #define platform_SHA256_Init SHA256_Init
  42. #define platform_SHA256_Update SHA256_Update
  43. #define platform_SHA256_Final SHA256_Final
  44. #endif
  45. #define git_SHA256_CTX platform_SHA256_CTX
  46. #define git_SHA256_Init platform_SHA256_Init
  47. #define git_SHA256_Update platform_SHA256_Update
  48. #define git_SHA256_Final platform_SHA256_Final
  49. #ifdef SHA1_MAX_BLOCK_SIZE
  50. #include "compat/sha1-chunked.h"
  51. #undef git_SHA1_Update
  52. #define git_SHA1_Update git_SHA1_Update_Chunked
  53. #endif
  54. /*
  55. * Note that these constants are suitable for indexing the hash_algos array and
  56. * comparing against each other, but are otherwise arbitrary, so they should not
  57. * be exposed to the user or serialized to disk. To know whether a
  58. * git_hash_algo struct points to some usable hash function, test the format_id
  59. * field for being non-zero. Use the name field for user-visible situations and
  60. * the format_id field for fixed-length fields on disk.
  61. */
  62. /* An unknown hash function. */
  63. #define GIT_HASH_UNKNOWN 0
  64. /* SHA-1 */
  65. #define GIT_HASH_SHA1 1
  66. /* SHA-256 */
  67. #define GIT_HASH_SHA256 2
  68. /* Number of algorithms supported (including unknown). */
  69. #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
  70. /* A suitably aligned type for stack allocations of hash contexts. */
  71. union git_hash_ctx {
  72. git_SHA_CTX sha1;
  73. git_SHA256_CTX sha256;
  74. };
  75. typedef union git_hash_ctx git_hash_ctx;
  76. typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
  77. typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
  78. typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
  79. struct git_hash_algo {
  80. /*
  81. * The name of the algorithm, as appears in the config file and in
  82. * messages.
  83. */
  84. const char *name;
  85. /* A four-byte version identifier, used in pack indices. */
  86. uint32_t format_id;
  87. /* The length of the hash in binary. */
  88. size_t rawsz;
  89. /* The length of the hash in hex characters. */
  90. size_t hexsz;
  91. /* The block size of the hash. */
  92. size_t blksz;
  93. /* The hash initialization function. */
  94. git_hash_init_fn init_fn;
  95. /* The hash update function. */
  96. git_hash_update_fn update_fn;
  97. /* The hash finalization function. */
  98. git_hash_final_fn final_fn;
  99. /* The OID of the empty tree. */
  100. const struct object_id *empty_tree;
  101. /* The OID of the empty blob. */
  102. const struct object_id *empty_blob;
  103. };
  104. extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
  105. /*
  106. * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
  107. * the name doesn't match a known algorithm.
  108. */
  109. int hash_algo_by_name(const char *name);
  110. /* Identical, except based on the format ID. */
  111. int hash_algo_by_id(uint32_t format_id);
  112. /* Identical, except based on the length. */
  113. int hash_algo_by_length(int len);
  114. /* Identical, except for a pointer to struct git_hash_algo. */
  115. static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
  116. {
  117. return p - hash_algos;
  118. }
  119. /* The length in bytes and in hex digits of an object name (SHA-1 value). */
  120. #define GIT_SHA1_RAWSZ 20
  121. #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
  122. /* The block size of SHA-1. */
  123. #define GIT_SHA1_BLKSZ 64
  124. /* The length in bytes and in hex digits of an object name (SHA-256 value). */
  125. #define GIT_SHA256_RAWSZ 32
  126. #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
  127. /* The block size of SHA-256. */
  128. #define GIT_SHA256_BLKSZ 64
  129. /* The length in byte and in hex digits of the largest possible hash value. */
  130. #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
  131. #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
  132. /* The largest possible block size for any supported hash. */
  133. #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
  134. struct object_id {
  135. unsigned char hash[GIT_MAX_RAWSZ];
  136. };
  137. #define the_hash_algo the_repository->hash_algo
  138. #endif