|
| 1 | +/* hash-file.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2020 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | + |
| 23 | +#ifndef WOLFSSL_USER_SETTINGS |
| 24 | +#include <wolfssl/options.h> |
| 25 | +#endif |
| 26 | +#include <wolfssl/wolfcrypt/settings.h> |
| 27 | +#include <wolfssl/ssl.h> |
| 28 | +#include <wolfssl/wolfcrypt/hash.h> |
| 29 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 30 | + |
| 31 | +#ifndef CHUNK_SIZE |
| 32 | +#define CHUNK_SIZE 1024 |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifndef NO_HASH_WRAPPERS |
| 36 | +enum wc_HashType hash_type_from_string(char* name) |
| 37 | +{ |
| 38 | + if (strcmp(name, "MD2") == 0) { |
| 39 | + return WC_HASH_TYPE_MD2; |
| 40 | + } |
| 41 | + else if (strcmp(name, "MD4") == 0) { |
| 42 | + return WC_HASH_TYPE_MD4; |
| 43 | + } |
| 44 | + else if (strcmp(name, "MD5") == 0) { |
| 45 | + return WC_HASH_TYPE_MD5; |
| 46 | + } |
| 47 | + else if (strcmp(name, "SHA") == 0) { |
| 48 | + return WC_HASH_TYPE_SHA; |
| 49 | + } |
| 50 | + else if (strcmp(name, "SHA224") == 0) { |
| 51 | + return WC_HASH_TYPE_SHA224; |
| 52 | + } |
| 53 | + else if (strcmp(name, "SHA256") == 0) { |
| 54 | + return WC_HASH_TYPE_SHA256; |
| 55 | + } |
| 56 | + else if (strcmp(name, "SHA384") == 0) { |
| 57 | + return WC_HASH_TYPE_SHA384; |
| 58 | + } |
| 59 | + else if (strcmp(name, "SHA512") == 0) { |
| 60 | + return WC_HASH_TYPE_SHA512; |
| 61 | + } |
| 62 | + else if (strcmp(name, "SHA3-224") == 0) { |
| 63 | + return WC_HASH_TYPE_SHA3_224; |
| 64 | + } |
| 65 | + else if (strcmp(name, "SHA3-256") == 0) { |
| 66 | + return WC_HASH_TYPE_SHA3_256; |
| 67 | + } |
| 68 | + else if (strcmp(name, "SHA3-384") == 0) { |
| 69 | + return WC_HASH_TYPE_SHA3_384; |
| 70 | + } |
| 71 | + else if (strcmp(name, "SHA3-512") == 0) { |
| 72 | + return WC_HASH_TYPE_SHA3_512; |
| 73 | + } |
| 74 | + else if (strcmp(name, "BLAKE2B") == 0) { |
| 75 | + return WC_HASH_TYPE_BLAKE2B; |
| 76 | + } |
| 77 | + else if (strcmp(name, "BLAKE2S") == 0) { |
| 78 | + return WC_HASH_TYPE_BLAKE2S; |
| 79 | + } |
| 80 | +#ifndef WOLFSSL_NOSHA512_224 |
| 81 | + else if (strcmp(name, "SHA512-224") == 0) { |
| 82 | + return WC_HASH_TYPE_SHA512_224; |
| 83 | + } |
| 84 | +#endif |
| 85 | +#ifndef WOLFSSL_NOSHA512_256 |
| 86 | + else if (strcmp(name, "SHA512-256") == 0) { |
| 87 | + return WC_HASH_TYPE_SHA512_256; |
| 88 | + } |
| 89 | +#endif |
| 90 | +#ifdef WOLFSSL_SHAKE128 |
| 91 | + else if (strcmp(name, "SHAKE128") == 0) { |
| 92 | + return WC_HASH_TYPE_SHAKE128; |
| 93 | + } |
| 94 | +#endif |
| 95 | +#ifdef WOLFSSL_SHAKE256 |
| 96 | + else if (strcmp(name, "SHAKE256") == 0) { |
| 97 | + return WC_HASH_TYPE_SHAKE256; |
| 98 | + } |
| 99 | +#endif |
| 100 | +#ifdef WOLFSSL_SM3 |
| 101 | + else if (strcmp(name, "SM3") == 0) { |
| 102 | + return WC_HASH_TYPE_SHAKE256; |
| 103 | + } |
| 104 | +#endif |
| 105 | + else { |
| 106 | + return WC_HASH_TYPE_NONE; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void print_wolfssl_error(const char* msg, int err) |
| 111 | +{ |
| 112 | +#ifndef NO_ERROR_STRINGS |
| 113 | + printf("%s: %s (%d)\n", msg, wc_GetErrorString(err), err); |
| 114 | +#else |
| 115 | + printf("%s: %d\n", msg, err); |
| 116 | +#endif |
| 117 | +} |
| 118 | + |
| 119 | +void usage(void) |
| 120 | +{ |
| 121 | + printf("./hash-file <alg> <file to hash>\n"); |
| 122 | + exit(-99); |
| 123 | +} |
| 124 | +#endif |
| 125 | + |
| 126 | +int main(int argc, char** argv) |
| 127 | +{ |
| 128 | + int ret = -1; |
| 129 | +#ifndef NO_HASH_WRAPPERS |
| 130 | + wc_HashAlg hashAlg; |
| 131 | + enum wc_HashType hashType; |
| 132 | + byte hash[WC_MAX_DIGEST_SIZE]; |
| 133 | + byte rawInput[CHUNK_SIZE]; |
| 134 | + FILE* inputStream; |
| 135 | + char* hashName = NULL; |
| 136 | + char* fName = NULL; |
| 137 | + int fileLength = 0; |
| 138 | + int i, chunkSz; |
| 139 | + |
| 140 | + if (argc < 3) |
| 141 | + usage(); |
| 142 | + hashName = argv[1]; |
| 143 | + fName = argv[2]; |
| 144 | + |
| 145 | + printf("Hash algorithme %s\n", hashName); |
| 146 | + hashType = hash_type_from_string(hashName); |
| 147 | + if (hashType == WC_HASH_TYPE_NONE) { |
| 148 | + printf("ERROR: hash algorithm not recognized.\n"); |
| 149 | + return -1; |
| 150 | + } |
| 151 | + |
| 152 | + printf("Hash input file %s\n", fName); |
| 153 | + inputStream = fopen(fName, "rb"); |
| 154 | + if (inputStream == NULL) { |
| 155 | + printf("ERROR: Unable to open file\n"); |
| 156 | + return -1; |
| 157 | + } |
| 158 | + |
| 159 | + /* find length of the file */ |
| 160 | + fseek(inputStream, 0, SEEK_END); |
| 161 | + fileLength = (int) ftell(inputStream); |
| 162 | + fseek(inputStream, 0, SEEK_SET); |
| 163 | + |
| 164 | + ret = wc_HashInit(&hashAlg, hashType); |
| 165 | + if (ret != 0) { |
| 166 | + print_wolfssl_error("Failed to initialize hash structure", ret); |
| 167 | + fclose(inputStream); |
| 168 | + return -1; |
| 169 | + } |
| 170 | + |
| 171 | + /* Loop reading a block at a time, finishing with any excess */ |
| 172 | + for (i = 0; i < fileLength; i += CHUNK_SIZE) { |
| 173 | + chunkSz = CHUNK_SIZE; |
| 174 | + if (chunkSz > fileLength - i) |
| 175 | + chunkSz = fileLength - i; |
| 176 | + |
| 177 | + ret = fread(rawInput, 1, chunkSz, inputStream); |
| 178 | + if (ret != chunkSz) { |
| 179 | + printf("ERROR: Failed to read the appropriate amount\n"); |
| 180 | + ret = -1; |
| 181 | + break; |
| 182 | + } |
| 183 | + |
| 184 | + ret = wc_HashUpdate(&hashAlg, hashType, rawInput, chunkSz); |
| 185 | + if (ret != 0) { |
| 186 | + print_wolfssl_error("Failed to update the hash", ret); |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if (ret == 0) { |
| 192 | + ret = wc_HashFinal(&hashAlg, hashType, hash); |
| 193 | + if (ret != 0) { |
| 194 | + print_wolfssl_error("Failed to generate hash", ret); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (ret != 0) { |
| 199 | + printf("ERROR: Hash operation failed"); |
| 200 | + } |
| 201 | + else { |
| 202 | + printf("Hash result is: "); |
| 203 | + int sz = wc_HashGetDigestSize(hashType); |
| 204 | + for (i = 0; i < sz; i++) |
| 205 | + printf("%02x", hash[i]); |
| 206 | + printf("\n"); |
| 207 | + } |
| 208 | + |
| 209 | + fclose(inputStream); |
| 210 | + wc_HashFree(&hashAlg, hashType); |
| 211 | +#else |
| 212 | + printf("Please remove NO_HASH_WRAPPERS from wolfCrypt configuration\n"); |
| 213 | +#endif |
| 214 | + return ret; |
| 215 | +} |
0 commit comments