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.
 
 
 
 
 

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