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.
 
 
 
 
 

96 lines
2.7 KiB

  1. /**
  2. * @cond internal
  3. * @file decaf_shakesum.c
  4. * @copyright
  5. * Copyright (c) 2015 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. * @brief SHA3 utility, to be combined with test vectors eventually...
  9. */
  10. #include <stdio.h>
  11. #if defined _MSC_VER // MSVC has not unistd.h
  12. #include <io.h>
  13. #include <BaseTsd.h>
  14. typedef SSIZE_T ssize_t;
  15. #else
  16. #include <unistd.h>
  17. #endif
  18. #include <string.h>
  19. #include <decaf/shake.h>
  20. #include <decaf/sha512.h>
  21. static void usage() {
  22. fprintf(
  23. stderr,
  24. "decaf_shakesum [shake256|shake128|sha3-224|sha3-384|sha3-512|sha512] < infile > outfile\n"
  25. );
  26. }
  27. int main(int argc, char **argv) {
  28. (void)argc; (void)argv;
  29. decaf_keccak_sponge_t sponge;
  30. decaf_sha512_ctx_t decaf_sha512;
  31. unsigned char buf[1024];
  32. unsigned int outlen = 512;
  33. decaf_shake256_gen_init(sponge);
  34. int use_sha512 = 0;
  35. /* Sloppy. Real utility would parse --algo, --size ... */
  36. if (argc > 1) {
  37. if (!strcmp(argv[1], "shake256") || !strcmp(argv[1], "SHAKE256")) {
  38. outlen = 512;
  39. decaf_shake256_gen_init(sponge);
  40. } else if (!strcmp(argv[1], "shake128") || !strcmp(argv[1], "SHAKE128")) {
  41. outlen = 512;
  42. decaf_shake128_gen_init(sponge);
  43. } else if (!strcmp(argv[1], "sha3-224") || !strcmp(argv[1], "SHA3-224")) {
  44. outlen = 224/8;
  45. decaf_sha3_224_gen_init(sponge);
  46. } else if (!strcmp(argv[1], "sha3-256") || !strcmp(argv[1], "SHA3-256")) {
  47. outlen = 256/8;
  48. decaf_sha3_256_gen_init(sponge);
  49. } else if (!strcmp(argv[1], "sha3-384") || !strcmp(argv[1], "SHA3-384")) {
  50. outlen = 384/8;
  51. decaf_sha3_384_gen_init(sponge);
  52. } else if (!strcmp(argv[1], "sha3-512") || !strcmp(argv[1], "SHA3-512")) {
  53. outlen = 512/8;
  54. decaf_sha3_512_gen_init(sponge);
  55. } else if (!strcmp(argv[1], "sha512") || !strcmp(argv[1], "SHA512")) {
  56. outlen = 512/8;
  57. use_sha512 = 1;
  58. decaf_sha512_init(decaf_sha512);
  59. } else {
  60. usage();
  61. return 2;
  62. }
  63. }
  64. ssize_t red;
  65. do {
  66. red = read(0, buf, sizeof(buf));
  67. if (red>0) {
  68. if (use_sha512) decaf_sha512_update(decaf_sha512,buf,red);
  69. else decaf_sha3_update(sponge,buf,red);
  70. }
  71. } while (red>0);
  72. if (use_sha512) {
  73. decaf_sha512_final(decaf_sha512,buf,outlen);
  74. decaf_sha512_destroy(decaf_sha512);
  75. } else {
  76. decaf_sha3_output(sponge,buf,outlen);
  77. decaf_sha3_destroy(sponge);
  78. }
  79. unsigned i;
  80. for (i=0; i<outlen; i++) {
  81. printf("%02x", buf[i]);
  82. }
  83. printf("\n");
  84. return 0;
  85. }