shakesum.c 2.74 KB
Newer Older
1 2
/**
 * @cond internal
3
 * @file decaf_shakesum.c
4 5 6 7 8 9 10 11
 * @copyright
 *   Copyright (c) 2015 Cryptography Research, Inc.  \n
 *   Released under the MIT License.  See LICENSE.txt for license information.
 * @author Mike Hamburg
 * @brief SHA3 utility, to be combined with test vectors eventually...
 */

#include <stdio.h>
mrolinek's avatar
mrolinek committed
12 13 14 15 16
#if defined _MSC_VER  // MSVC has not unistd.h
#include <io.h>
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#else
17
#include <unistd.h>
mrolinek's avatar
mrolinek committed
18
#endif
19
#include <string.h>
20
#include <decaf/shake.h>
Michael Hamburg's avatar
Michael Hamburg committed
21
#include <decaf/sha512.h>
22

23
static void usage(void) {
24 25
    fprintf(
        stderr,
26
        "decaf_shakesum [shake256|shake128|sha3-224|sha3-384|sha3-512|sha512] < infile > outfile\n"
27 28 29
    );
}

30 31 32
int main(int argc, char **argv) {
    (void)argc; (void)argv;

33 34
    decaf_keccak_sponge_t sponge;
    decaf_sha512_ctx_t decaf_sha512;
35 36 37
    unsigned char buf[1024];
    
    unsigned int outlen = 512;
38
    decaf_shake256_gen_init(sponge);
Michael Hamburg's avatar
Michael Hamburg committed
39 40
    
    int use_sha512 = 0;
41 42 43 44 45

    /* Sloppy.  Real utility would parse --algo, --size ... */
    if (argc > 1) {
        if (!strcmp(argv[1], "shake256") || !strcmp(argv[1], "SHAKE256")) {
            outlen = 512;
46
            decaf_shake256_gen_init(sponge);
47 48
        } else if (!strcmp(argv[1], "shake128") || !strcmp(argv[1], "SHAKE128")) {
            outlen = 512;
49
            decaf_shake128_gen_init(sponge);
50 51
        } else if (!strcmp(argv[1], "sha3-224") || !strcmp(argv[1], "SHA3-224")) {
            outlen = 224/8;
52
            decaf_sha3_224_gen_init(sponge);
53 54
        } else if (!strcmp(argv[1], "sha3-256") || !strcmp(argv[1], "SHA3-256")) {
            outlen = 256/8;
55
            decaf_sha3_256_gen_init(sponge);
56 57
        } else if (!strcmp(argv[1], "sha3-384") || !strcmp(argv[1], "SHA3-384")) {
            outlen = 384/8;
58
            decaf_sha3_384_gen_init(sponge);
59 60
        } else if (!strcmp(argv[1], "sha3-512") || !strcmp(argv[1], "SHA3-512")) {
            outlen = 512/8;
61
            decaf_sha3_512_gen_init(sponge);
Michael Hamburg's avatar
Michael Hamburg committed
62 63 64
        } else if (!strcmp(argv[1], "sha512") || !strcmp(argv[1], "SHA512")) {
            outlen = 512/8;
            use_sha512 = 1;
65
            decaf_sha512_init(decaf_sha512);
66 67 68
        } else {
            usage();
            return 2;
69 70 71 72 73 74
        }
    }

    ssize_t red;
    do {
        red = read(0, buf, sizeof(buf));
Michael Hamburg's avatar
Michael Hamburg committed
75
        if (red>0) {
76 77
            if (use_sha512) decaf_sha512_update(decaf_sha512,buf,red);
            else decaf_sha3_update(sponge,buf,red);
Michael Hamburg's avatar
Michael Hamburg committed
78
        }
79 80
    } while (red>0);

Michael Hamburg's avatar
Michael Hamburg committed
81
    if (use_sha512) {
82 83
        decaf_sha512_final(decaf_sha512,buf,outlen);
        decaf_sha512_destroy(decaf_sha512);
Michael Hamburg's avatar
Michael Hamburg committed
84
    } else {
85
        decaf_sha3_output(sponge,buf,outlen);
86
        decaf_sha3_destroy(sponge);
Michael Hamburg's avatar
Michael Hamburg committed
87
    }
88 89 90 91 92 93 94 95
    unsigned i;
    for (i=0; i<outlen; i++) {
        printf("%02x", buf[i]);
    }
    printf("\n");

    return 0;
}