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.
 
 
 
 
 

97 lines
2.8 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. #define read _read
  16. #else
  17. #include <unistd.h>
  18. #endif
  19. #include <string.h>
  20. #include <decaf/shake.h>
  21. #include <decaf/sha512.h>
  22. static void usage(void) {
  23. fprintf(
  24. stderr,
  25. "decaf_shakesum [shake256|shake128|sha3-224|sha3-384|sha3-512|sha512] < infile > outfile\n"
  26. );
  27. }
  28. int main(int argc, char **argv) {
  29. (void)argc; (void)argv;
  30. decaf_keccak_sponge_t sponge;
  31. decaf_sha512_ctx_t decaf_sha512;
  32. unsigned char buf[1024];
  33. unsigned int outlen = 512;
  34. decaf_shake256_gen_init(sponge);
  35. int use_sha512 = 0;
  36. /* Sloppy. Real utility would parse --algo, --size ... */
  37. if (argc > 1) {
  38. if (!strcmp(argv[1], "shake256") || !strcmp(argv[1], "SHAKE256")) {
  39. outlen = 512;
  40. decaf_shake256_gen_init(sponge);
  41. } else if (!strcmp(argv[1], "shake128") || !strcmp(argv[1], "SHAKE128")) {
  42. outlen = 512;
  43. decaf_shake128_gen_init(sponge);
  44. } else if (!strcmp(argv[1], "sha3-224") || !strcmp(argv[1], "SHA3-224")) {
  45. outlen = 224/8;
  46. decaf_sha3_224_gen_init(sponge);
  47. } else if (!strcmp(argv[1], "sha3-256") || !strcmp(argv[1], "SHA3-256")) {
  48. outlen = 256/8;
  49. decaf_sha3_256_gen_init(sponge);
  50. } else if (!strcmp(argv[1], "sha3-384") || !strcmp(argv[1], "SHA3-384")) {
  51. outlen = 384/8;
  52. decaf_sha3_384_gen_init(sponge);
  53. } else if (!strcmp(argv[1], "sha3-512") || !strcmp(argv[1], "SHA3-512")) {
  54. outlen = 512/8;
  55. decaf_sha3_512_gen_init(sponge);
  56. } else if (!strcmp(argv[1], "sha512") || !strcmp(argv[1], "SHA512")) {
  57. outlen = 512/8;
  58. use_sha512 = 1;
  59. decaf_sha512_init(decaf_sha512);
  60. } else {
  61. usage();
  62. return 2;
  63. }
  64. }
  65. ssize_t red;
  66. do {
  67. red = read(0, buf, sizeof(buf));
  68. if (red>0) {
  69. if (use_sha512) decaf_sha512_update(decaf_sha512,buf,red);
  70. else decaf_sha3_update(sponge,buf,red);
  71. }
  72. } while (red>0);
  73. if (use_sha512) {
  74. decaf_sha512_final(decaf_sha512,buf,outlen);
  75. decaf_sha512_destroy(decaf_sha512);
  76. } else {
  77. decaf_sha3_output(sponge,buf,outlen);
  78. decaf_sha3_destroy(sponge);
  79. }
  80. unsigned i;
  81. for (i=0; i<outlen; i++) {
  82. printf("%02x", buf[i]);
  83. }
  84. printf("\n");
  85. return 0;
  86. }