sha_tests.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 2010 The Chromium OS 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. */
  5. /* FIPS 180-2 Tests for message digest functions. */
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "sha_test_vectors.h"
  11. int SHA1_tests(void) {
  12. int i, success = 1;
  13. uint8_t sha1_digest[SHA1_DIGEST_SIZE];
  14. uint8_t* test_inputs[3];
  15. test_inputs[0] = (uint8_t *) oneblock_msg;
  16. test_inputs[1] = (uint8_t *) multiblock_msg1;
  17. test_inputs[2] = (uint8_t *) long_msg;
  18. for (i = 0; i < 3; i++) {
  19. internal_SHA1(test_inputs[i], strlen((char *)test_inputs[i]),
  20. sha1_digest);
  21. if (!memcmp(sha1_digest, sha1_results[i], SHA1_DIGEST_SIZE)) {
  22. fprintf(stderr, "Test vector %d PASSED for SHA-1\n", i+1);
  23. }
  24. else {
  25. fprintf(stderr, "Test vector %d FAILED for SHA-1\n", i+1);
  26. success = 0;
  27. }
  28. }
  29. return success;
  30. }
  31. int SHA256_tests(void) {
  32. int i, success = 1;
  33. uint8_t sha256_digest[SHA256_DIGEST_SIZE];
  34. uint8_t* test_inputs[3];
  35. test_inputs[0] = (uint8_t *) oneblock_msg;
  36. test_inputs[1] = (uint8_t *) multiblock_msg1;
  37. test_inputs[2] = (uint8_t *) long_msg;
  38. for (i = 0; i < 3; i++) {
  39. internal_SHA256(test_inputs[i], strlen((char *)test_inputs[i]),
  40. sha256_digest);
  41. if (!memcmp(sha256_digest, sha256_results[i], SHA256_DIGEST_SIZE)) {
  42. fprintf(stderr, "Test vector %d PASSED for SHA-256\n", i+1);
  43. }
  44. else {
  45. fprintf(stderr, "Test vector %d FAILED for SHA-256\n", i+1);
  46. success = 0;
  47. }
  48. }
  49. return success;
  50. }
  51. int SHA512_tests(void) {
  52. int i, success = 1;
  53. uint8_t sha512_digest[SHA512_DIGEST_SIZE];
  54. uint8_t* test_inputs[3];
  55. test_inputs[0] = (uint8_t *) oneblock_msg;
  56. test_inputs[1] = (uint8_t *) multiblock_msg2;
  57. test_inputs[2] = (uint8_t *) long_msg;
  58. for (i = 0; i < 3; i++) {
  59. internal_SHA512(test_inputs[i], strlen((char *)test_inputs[i]),
  60. sha512_digest);
  61. if (!memcmp(sha512_digest, sha512_results[i], SHA512_DIGEST_SIZE)) {
  62. fprintf(stderr, "Test vector %d PASSED for SHA-512\n", i+1);
  63. }
  64. else {
  65. fprintf(stderr, "Test vector %d FAILED for SHA-512\n", i+1);
  66. success = 0;
  67. }
  68. }
  69. return success;
  70. }
  71. int main(int argc, char* argv[]) {
  72. int success = 1;
  73. /* Initialize long_msg with 'a' x 1,000,000 */
  74. long_msg = (char *) malloc(1000001);
  75. memset(long_msg, 'a', 1000000);
  76. long_msg[1000000]=0;
  77. if (!SHA1_tests())
  78. success = 0;
  79. if (!SHA256_tests())
  80. success = 0;
  81. if (!SHA512_tests())
  82. success = 0;
  83. free(long_msg);
  84. return !success;
  85. }