Blame crypto/fips/fips_standalone_hmac.c

Packit Service 084de1
/* ====================================================================
Packit Service 084de1
 * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
Packit Service 084de1
 *
Packit Service 084de1
 * Redistribution and use in source and binary forms, with or without
Packit Service 084de1
 * modification, are permitted provided that the following conditions
Packit Service 084de1
 * are met:
Packit Service 084de1
 *
Packit Service 084de1
 * 1. Redistributions of source code must retain the above copyright
Packit Service 084de1
 *    notice, this list of conditions and the following disclaimer. 
Packit Service 084de1
 *
Packit Service 084de1
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 084de1
 *    notice, this list of conditions and the following disclaimer in
Packit Service 084de1
 *    the documentation and/or other materials provided with the
Packit Service 084de1
 *    distribution.
Packit Service 084de1
 *
Packit Service 084de1
 * 3. All advertising materials mentioning features or use of this
Packit Service 084de1
 *    software must display the following acknowledgment:
Packit Service 084de1
 *    "This product includes software developed by the OpenSSL Project
Packit Service 084de1
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
Packit Service 084de1
 *
Packit Service 084de1
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
Packit Service 084de1
 *    endorse or promote products derived from this software without
Packit Service 084de1
 *    prior written permission. For written permission, please contact
Packit Service 084de1
 *    openssl-core@openssl.org.
Packit Service 084de1
 *
Packit Service 084de1
 * 5. Products derived from this software may not be called "OpenSSL"
Packit Service 084de1
 *    nor may "OpenSSL" appear in their names without prior written
Packit Service 084de1
 *    permission of the OpenSSL Project.
Packit Service 084de1
 *
Packit Service 084de1
 * 6. Redistributions of any form whatsoever must retain the following
Packit Service 084de1
 *    acknowledgment:
Packit Service 084de1
 *    "This product includes software developed by the OpenSSL Project
Packit Service 084de1
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
Packit Service 084de1
 *
Packit Service 084de1
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
Packit Service 084de1
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service 084de1
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit Service 084de1
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
Packit Service 084de1
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 084de1
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 084de1
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Packit Service 084de1
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service 084de1
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit Service 084de1
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit Service 084de1
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit Service 084de1
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 084de1
 *
Packit Service 084de1
 */
Packit Service 084de1
Packit Service 084de1
#include <stdio.h>
Packit Service 084de1
#include <stdlib.h>
Packit Service 084de1
#include <string.h>
Packit Service 084de1
#include <openssl/opensslconf.h>
Packit Service 084de1
#include <openssl/hmac.h>
Packit Service 084de1
#include <openssl/sha.h>
Packit Service 084de1
Packit Service 084de1
int main(int argc, char **argv)
Packit Service 084de1
{
Packit Service 084de1
#ifdef OPENSSL_FIPS
Packit Service 084de1
    static char key[] = "orboDeJITITejsirpADONivirpUkvarP";
Packit Service 084de1
    int n, binary = 0;
Packit Service 084de1
Packit Service 084de1
    if (argc < 2) {
Packit Service 084de1
        fprintf(stderr, "%s [<file>]+\n", argv[0]);
Packit Service 084de1
        exit(1);
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    n = 1;
Packit Service 084de1
    if (!strcmp(argv[n], "-binary")) {
Packit Service 084de1
        n++;
Packit Service 084de1
        binary = 1;             /* emit binary fingerprint... */
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    for (; n < argc; ++n) {
Packit Service 084de1
        FILE *f = fopen(argv[n], "rb");
Packit Service 084de1
        HMAC_CTX *hmac_ctx;
Packit Service 084de1
        unsigned char mac[EVP_MAX_MD_SIZE];
Packit Service 084de1
        unsigned int len;
Packit Service 084de1
        unsigned int i;
Packit Service 084de1
Packit Service 084de1
        if (!f) {
Packit Service 084de1
            perror(argv[n]);
Packit Service 084de1
            exit(2);
Packit Service 084de1
        }
Packit Service 084de1
        hmac_ctx = HMAC_CTX_new();
Packit Service 084de1
        if (!hmac_ctx)
Packit Service 084de1
            exit(3);
Packit Service 084de1
Packit Service 084de1
        if (HMAC_Init_ex(hmac_ctx, key, strlen(key), EVP_sha256(), NULL) <= 0) {
Packit Service 084de1
            fprintf(stderr, "HMAC SHA256 initialization failed.\n");
Packit Service 084de1
            exit(4);
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
        for (;;) {
Packit Service 084de1
            unsigned char buf[1024];
Packit Service 084de1
            size_t l = fread(buf, 1, sizeof buf, f);
Packit Service 084de1
Packit Service 084de1
            if (l == 0) {
Packit Service 084de1
                if (ferror(f)) {
Packit Service 084de1
                    perror(argv[n]);
Packit Service 084de1
                    exit(3);
Packit Service 084de1
                } else
Packit Service 084de1
                    break;
Packit Service 084de1
            }
Packit Service 084de1
            if (HMAC_Update(hmac_ctx, buf, l) <= 0) {
Packit Service 084de1
                fprintf(stderr, "HMAC_Update() failed.\n");
Packit Service 084de1
                exit(4);
Packit Service 084de1
            }
Packit Service 084de1
        }
Packit Service 084de1
        if (HMAC_Final(hmac_ctx, mac, &len) <= 0) {
Packit Service 084de1
            fprintf(stderr, "HMAC_Final() failed.\n");
Packit Service 084de1
            exit(4);
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
        if (binary) {
Packit Service 084de1
            fwrite(mac, len, 1, stdout);
Packit Service 084de1
            break;              /* ... for single(!) file */
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
/*      printf("HMAC-SHA1(%s)= ",argv[n]); */
Packit Service 084de1
        for (i = 0; i < len; ++i)
Packit Service 084de1
            printf("%02x", mac[i]);
Packit Service 084de1
        printf("\n");
Packit Service 084de1
    }
Packit Service 084de1
#endif
Packit Service 084de1
    return 0;
Packit Service 084de1
}