sha1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * FIPS-180-1 compliant SHA-1 implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * The SHA-1 standard was published by NIST in 1993.
  21. *
  22. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  23. */
  24. #include "common.h"
  25. #if defined(MBEDTLS_SHA1_C)
  26. #include "mbedtls/sha1.h"
  27. #include "mbedtls/platform_util.h"
  28. #include "mbedtls/error.h"
  29. #include <string.h>
  30. #include "mbedtls/platform.h"
  31. #if !defined(MBEDTLS_SHA1_ALT)
  32. void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
  33. {
  34. memset(ctx, 0, sizeof(mbedtls_sha1_context));
  35. }
  36. void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
  37. {
  38. if (ctx == NULL) {
  39. return;
  40. }
  41. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha1_context));
  42. }
  43. void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
  44. const mbedtls_sha1_context *src)
  45. {
  46. *dst = *src;
  47. }
  48. /*
  49. * SHA-1 context setup
  50. */
  51. int mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
  52. {
  53. ctx->total[0] = 0;
  54. ctx->total[1] = 0;
  55. ctx->state[0] = 0x67452301;
  56. ctx->state[1] = 0xEFCDAB89;
  57. ctx->state[2] = 0x98BADCFE;
  58. ctx->state[3] = 0x10325476;
  59. ctx->state[4] = 0xC3D2E1F0;
  60. return 0;
  61. }
  62. #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
  63. int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
  64. const unsigned char data[64])
  65. {
  66. struct {
  67. uint32_t temp, W[16], A, B, C, D, E;
  68. } local;
  69. local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
  70. local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
  71. local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
  72. local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
  73. local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
  74. local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
  75. local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
  76. local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
  77. local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
  78. local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
  79. local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
  80. local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
  81. local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
  82. local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
  83. local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
  84. local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
  85. #define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
  86. #define R(t) \
  87. ( \
  88. local.temp = local.W[((t) - 3) & 0x0F] ^ \
  89. local.W[((t) - 8) & 0x0F] ^ \
  90. local.W[((t) - 14) & 0x0F] ^ \
  91. local.W[(t) & 0x0F], \
  92. (local.W[(t) & 0x0F] = S(local.temp, 1)) \
  93. )
  94. #define P(a, b, c, d, e, x) \
  95. do \
  96. { \
  97. (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
  98. (b) = S((b), 30); \
  99. } while (0)
  100. local.A = ctx->state[0];
  101. local.B = ctx->state[1];
  102. local.C = ctx->state[2];
  103. local.D = ctx->state[3];
  104. local.E = ctx->state[4];
  105. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  106. #define K 0x5A827999
  107. P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
  108. P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
  109. P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
  110. P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
  111. P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
  112. P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
  113. P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
  114. P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
  115. P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
  116. P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
  117. P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
  118. P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
  119. P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
  120. P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
  121. P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
  122. P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
  123. P(local.E, local.A, local.B, local.C, local.D, R(16));
  124. P(local.D, local.E, local.A, local.B, local.C, R(17));
  125. P(local.C, local.D, local.E, local.A, local.B, R(18));
  126. P(local.B, local.C, local.D, local.E, local.A, R(19));
  127. #undef K
  128. #undef F
  129. #define F(x, y, z) ((x) ^ (y) ^ (z))
  130. #define K 0x6ED9EBA1
  131. P(local.A, local.B, local.C, local.D, local.E, R(20));
  132. P(local.E, local.A, local.B, local.C, local.D, R(21));
  133. P(local.D, local.E, local.A, local.B, local.C, R(22));
  134. P(local.C, local.D, local.E, local.A, local.B, R(23));
  135. P(local.B, local.C, local.D, local.E, local.A, R(24));
  136. P(local.A, local.B, local.C, local.D, local.E, R(25));
  137. P(local.E, local.A, local.B, local.C, local.D, R(26));
  138. P(local.D, local.E, local.A, local.B, local.C, R(27));
  139. P(local.C, local.D, local.E, local.A, local.B, R(28));
  140. P(local.B, local.C, local.D, local.E, local.A, R(29));
  141. P(local.A, local.B, local.C, local.D, local.E, R(30));
  142. P(local.E, local.A, local.B, local.C, local.D, R(31));
  143. P(local.D, local.E, local.A, local.B, local.C, R(32));
  144. P(local.C, local.D, local.E, local.A, local.B, R(33));
  145. P(local.B, local.C, local.D, local.E, local.A, R(34));
  146. P(local.A, local.B, local.C, local.D, local.E, R(35));
  147. P(local.E, local.A, local.B, local.C, local.D, R(36));
  148. P(local.D, local.E, local.A, local.B, local.C, R(37));
  149. P(local.C, local.D, local.E, local.A, local.B, R(38));
  150. P(local.B, local.C, local.D, local.E, local.A, R(39));
  151. #undef K
  152. #undef F
  153. #define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  154. #define K 0x8F1BBCDC
  155. P(local.A, local.B, local.C, local.D, local.E, R(40));
  156. P(local.E, local.A, local.B, local.C, local.D, R(41));
  157. P(local.D, local.E, local.A, local.B, local.C, R(42));
  158. P(local.C, local.D, local.E, local.A, local.B, R(43));
  159. P(local.B, local.C, local.D, local.E, local.A, R(44));
  160. P(local.A, local.B, local.C, local.D, local.E, R(45));
  161. P(local.E, local.A, local.B, local.C, local.D, R(46));
  162. P(local.D, local.E, local.A, local.B, local.C, R(47));
  163. P(local.C, local.D, local.E, local.A, local.B, R(48));
  164. P(local.B, local.C, local.D, local.E, local.A, R(49));
  165. P(local.A, local.B, local.C, local.D, local.E, R(50));
  166. P(local.E, local.A, local.B, local.C, local.D, R(51));
  167. P(local.D, local.E, local.A, local.B, local.C, R(52));
  168. P(local.C, local.D, local.E, local.A, local.B, R(53));
  169. P(local.B, local.C, local.D, local.E, local.A, R(54));
  170. P(local.A, local.B, local.C, local.D, local.E, R(55));
  171. P(local.E, local.A, local.B, local.C, local.D, R(56));
  172. P(local.D, local.E, local.A, local.B, local.C, R(57));
  173. P(local.C, local.D, local.E, local.A, local.B, R(58));
  174. P(local.B, local.C, local.D, local.E, local.A, R(59));
  175. #undef K
  176. #undef F
  177. #define F(x, y, z) ((x) ^ (y) ^ (z))
  178. #define K 0xCA62C1D6
  179. P(local.A, local.B, local.C, local.D, local.E, R(60));
  180. P(local.E, local.A, local.B, local.C, local.D, R(61));
  181. P(local.D, local.E, local.A, local.B, local.C, R(62));
  182. P(local.C, local.D, local.E, local.A, local.B, R(63));
  183. P(local.B, local.C, local.D, local.E, local.A, R(64));
  184. P(local.A, local.B, local.C, local.D, local.E, R(65));
  185. P(local.E, local.A, local.B, local.C, local.D, R(66));
  186. P(local.D, local.E, local.A, local.B, local.C, R(67));
  187. P(local.C, local.D, local.E, local.A, local.B, R(68));
  188. P(local.B, local.C, local.D, local.E, local.A, R(69));
  189. P(local.A, local.B, local.C, local.D, local.E, R(70));
  190. P(local.E, local.A, local.B, local.C, local.D, R(71));
  191. P(local.D, local.E, local.A, local.B, local.C, R(72));
  192. P(local.C, local.D, local.E, local.A, local.B, R(73));
  193. P(local.B, local.C, local.D, local.E, local.A, R(74));
  194. P(local.A, local.B, local.C, local.D, local.E, R(75));
  195. P(local.E, local.A, local.B, local.C, local.D, R(76));
  196. P(local.D, local.E, local.A, local.B, local.C, R(77));
  197. P(local.C, local.D, local.E, local.A, local.B, R(78));
  198. P(local.B, local.C, local.D, local.E, local.A, R(79));
  199. #undef K
  200. #undef F
  201. ctx->state[0] += local.A;
  202. ctx->state[1] += local.B;
  203. ctx->state[2] += local.C;
  204. ctx->state[3] += local.D;
  205. ctx->state[4] += local.E;
  206. /* Zeroise buffers and variables to clear sensitive data from memory. */
  207. mbedtls_platform_zeroize(&local, sizeof(local));
  208. return 0;
  209. }
  210. #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
  211. /*
  212. * SHA-1 process buffer
  213. */
  214. int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
  215. const unsigned char *input,
  216. size_t ilen)
  217. {
  218. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  219. size_t fill;
  220. uint32_t left;
  221. if (ilen == 0) {
  222. return 0;
  223. }
  224. left = ctx->total[0] & 0x3F;
  225. fill = 64 - left;
  226. ctx->total[0] += (uint32_t) ilen;
  227. ctx->total[0] &= 0xFFFFFFFF;
  228. if (ctx->total[0] < (uint32_t) ilen) {
  229. ctx->total[1]++;
  230. }
  231. if (left && ilen >= fill) {
  232. memcpy((void *) (ctx->buffer + left), input, fill);
  233. if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
  234. return ret;
  235. }
  236. input += fill;
  237. ilen -= fill;
  238. left = 0;
  239. }
  240. while (ilen >= 64) {
  241. if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
  242. return ret;
  243. }
  244. input += 64;
  245. ilen -= 64;
  246. }
  247. if (ilen > 0) {
  248. memcpy((void *) (ctx->buffer + left), input, ilen);
  249. }
  250. return 0;
  251. }
  252. /*
  253. * SHA-1 final digest
  254. */
  255. int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
  256. unsigned char output[20])
  257. {
  258. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  259. uint32_t used;
  260. uint32_t high, low;
  261. /*
  262. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  263. */
  264. used = ctx->total[0] & 0x3F;
  265. ctx->buffer[used++] = 0x80;
  266. if (used <= 56) {
  267. /* Enough room for padding + length in current block */
  268. memset(ctx->buffer + used, 0, 56 - used);
  269. } else {
  270. /* We'll need an extra block */
  271. memset(ctx->buffer + used, 0, 64 - used);
  272. if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
  273. return ret;
  274. }
  275. memset(ctx->buffer, 0, 56);
  276. }
  277. /*
  278. * Add message length
  279. */
  280. high = (ctx->total[0] >> 29)
  281. | (ctx->total[1] << 3);
  282. low = (ctx->total[0] << 3);
  283. MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
  284. MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
  285. if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
  286. return ret;
  287. }
  288. /*
  289. * Output final state
  290. */
  291. MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
  292. MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
  293. MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
  294. MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
  295. MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
  296. return 0;
  297. }
  298. #endif /* !MBEDTLS_SHA1_ALT */
  299. /*
  300. * output = SHA-1( input buffer )
  301. */
  302. int mbedtls_sha1(const unsigned char *input,
  303. size_t ilen,
  304. unsigned char output[20])
  305. {
  306. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  307. mbedtls_sha1_context ctx;
  308. mbedtls_sha1_init(&ctx);
  309. if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
  310. goto exit;
  311. }
  312. if ((ret = mbedtls_sha1_update(&ctx, input, ilen)) != 0) {
  313. goto exit;
  314. }
  315. if ((ret = mbedtls_sha1_finish(&ctx, output)) != 0) {
  316. goto exit;
  317. }
  318. exit:
  319. mbedtls_sha1_free(&ctx);
  320. return ret;
  321. }
  322. #if defined(MBEDTLS_SELF_TEST)
  323. /*
  324. * FIPS-180-1 test vectors
  325. */
  326. static const unsigned char sha1_test_buf[3][57] =
  327. {
  328. { "abc" },
  329. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  330. { "" }
  331. };
  332. static const size_t sha1_test_buflen[3] =
  333. {
  334. 3, 56, 1000
  335. };
  336. static const unsigned char sha1_test_sum[3][20] =
  337. {
  338. { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  339. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
  340. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  341. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
  342. { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  343. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
  344. };
  345. /*
  346. * Checkup routine
  347. */
  348. int mbedtls_sha1_self_test(int verbose)
  349. {
  350. int i, j, buflen, ret = 0;
  351. unsigned char buf[1024];
  352. unsigned char sha1sum[20];
  353. mbedtls_sha1_context ctx;
  354. mbedtls_sha1_init(&ctx);
  355. /*
  356. * SHA-1
  357. */
  358. for (i = 0; i < 3; i++) {
  359. if (verbose != 0) {
  360. mbedtls_printf(" SHA-1 test #%d: ", i + 1);
  361. }
  362. if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
  363. goto fail;
  364. }
  365. if (i == 2) {
  366. memset(buf, 'a', buflen = 1000);
  367. for (j = 0; j < 1000; j++) {
  368. ret = mbedtls_sha1_update(&ctx, buf, buflen);
  369. if (ret != 0) {
  370. goto fail;
  371. }
  372. }
  373. } else {
  374. ret = mbedtls_sha1_update(&ctx, sha1_test_buf[i],
  375. sha1_test_buflen[i]);
  376. if (ret != 0) {
  377. goto fail;
  378. }
  379. }
  380. if ((ret = mbedtls_sha1_finish(&ctx, sha1sum)) != 0) {
  381. goto fail;
  382. }
  383. if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
  384. ret = 1;
  385. goto fail;
  386. }
  387. if (verbose != 0) {
  388. mbedtls_printf("passed\n");
  389. }
  390. }
  391. if (verbose != 0) {
  392. mbedtls_printf("\n");
  393. }
  394. goto exit;
  395. fail:
  396. if (verbose != 0) {
  397. mbedtls_printf("failed\n");
  398. }
  399. exit:
  400. mbedtls_sha1_free(&ctx);
  401. return ret;
  402. }
  403. #endif /* MBEDTLS_SELF_TEST */
  404. #endif /* MBEDTLS_SHA1_C */