sha1_nacl.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/hash/sha1.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include "base/sys_byteorder.h"
  9. namespace base {
  10. // Implementation of SHA-1. Only handles data in byte-sized blocks,
  11. // which simplifies the code a fair bit.
  12. // Identifier names follow notation in FIPS PUB 180-3, where you'll
  13. // also find a description of the algorithm:
  14. // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf
  15. // Usage example:
  16. //
  17. // SecureHashAlgorithm sha;
  18. // while(there is data to hash)
  19. // sha.Update(moredata, size of data);
  20. // sha.Final();
  21. // memcpy(somewhere, sha.Digest(), 20);
  22. //
  23. // to reuse the instance of sha, call sha.Init();
  24. static inline uint32_t f(uint32_t t, uint32_t B, uint32_t C, uint32_t D) {
  25. if (t < 20)
  26. return (B & C) | ((~B) & D);
  27. if (t < 40)
  28. return B ^ C ^ D;
  29. if (t < 60)
  30. return (B & C) | (B & D) | (C & D);
  31. return B ^ C ^ D;
  32. }
  33. static inline uint32_t S(uint32_t n, uint32_t X) {
  34. return (X << n) | (X >> (32 - n));
  35. }
  36. static inline uint32_t K(uint32_t t) {
  37. if (t < 20)
  38. return 0x5a827999;
  39. if (t < 40)
  40. return 0x6ed9eba1;
  41. if (t < 60)
  42. return 0x8f1bbcdc;
  43. return 0xca62c1d6;
  44. }
  45. void SHA1Context::Init() {
  46. A = 0;
  47. B = 0;
  48. C = 0;
  49. D = 0;
  50. E = 0;
  51. cursor = 0;
  52. l = 0;
  53. H[0] = 0x67452301;
  54. H[1] = 0xefcdab89;
  55. H[2] = 0x98badcfe;
  56. H[3] = 0x10325476;
  57. H[4] = 0xc3d2e1f0;
  58. }
  59. void SHA1Context::Update(const void* data, size_t nbytes) {
  60. const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
  61. while (nbytes--) {
  62. M[cursor++] = *d++;
  63. if (cursor >= 64) {
  64. Process();
  65. }
  66. l += 8;
  67. }
  68. }
  69. void SHA1Context::Final() {
  70. Pad();
  71. Process();
  72. for (auto& t : H) {
  73. t = ByteSwap(t);
  74. }
  75. }
  76. const unsigned char* SHA1Context::GetDigest() const {
  77. return reinterpret_cast<const unsigned char*>(H);
  78. }
  79. void SHA1Context::Pad() {
  80. M[cursor++] = 0x80;
  81. if (cursor > 64 - 8) {
  82. // pad out to next block
  83. while (cursor < 64) {
  84. M[cursor++] = 0;
  85. }
  86. Process();
  87. }
  88. while (cursor < 64 - 8) {
  89. M[cursor++] = 0;
  90. }
  91. M[cursor++] = (l >> 56) & 0xff;
  92. M[cursor++] = (l >> 48) & 0xff;
  93. M[cursor++] = (l >> 40) & 0xff;
  94. M[cursor++] = (l >> 32) & 0xff;
  95. M[cursor++] = (l >> 24) & 0xff;
  96. M[cursor++] = (l >> 16) & 0xff;
  97. M[cursor++] = (l >> 8) & 0xff;
  98. M[cursor++] = l & 0xff;
  99. }
  100. void SHA1Context::Process() {
  101. uint32_t t;
  102. // Each a...e corresponds to a section in the FIPS 180-3 algorithm.
  103. // a.
  104. //
  105. // W and M are in a union, so no need to memcpy.
  106. // memcpy(W, M, sizeof(M));
  107. for (t = 0; t < 16; ++t) {
  108. W[t] = ByteSwap(W[t]);
  109. }
  110. // b.
  111. for (t = 16; t < 80; ++t) {
  112. W[t] = S(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
  113. }
  114. // c.
  115. A = H[0];
  116. B = H[1];
  117. C = H[2];
  118. D = H[3];
  119. E = H[4];
  120. // d.
  121. for (t = 0; t < 80; ++t) {
  122. uint32_t TEMP = S(5, A) + f(t, B, C, D) + E + W[t] + K(t);
  123. E = D;
  124. D = C;
  125. C = S(30, B);
  126. B = A;
  127. A = TEMP;
  128. }
  129. // e.
  130. H[0] += A;
  131. H[1] += B;
  132. H[2] += C;
  133. H[3] += D;
  134. H[4] += E;
  135. cursor = 0;
  136. }
  137. // These functions allow streaming SHA-1 operations.
  138. void SHA1Init(SHA1Context& context) {
  139. context.Init();
  140. }
  141. void SHA1Update(const StringPiece data, SHA1Context& context) {
  142. context.Update(data.data(), data.size());
  143. }
  144. void SHA1Final(SHA1Context& context, SHA1Digest& digest) {
  145. context.Final();
  146. memcpy(digest.data(), context.GetDigest(), kSHA1Length);
  147. }
  148. SHA1Digest SHA1HashSpan(span<const uint8_t> data) {
  149. SHA1Digest hash;
  150. SHA1HashBytes(data.data(), data.size(), hash.data());
  151. return hash;
  152. }
  153. std::string SHA1HashString(StringPiece str) {
  154. char hash[kSHA1Length];
  155. SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.data()),
  156. str.length(), reinterpret_cast<unsigned char*>(hash));
  157. return std::string(hash, kSHA1Length);
  158. }
  159. void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash) {
  160. SHA1Context context;
  161. context.Update(data, len);
  162. context.Final();
  163. memcpy(hash, context.GetDigest(), kSHA1Length);
  164. }
  165. } // namespace base