new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_crypto / sha1 / sha1_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "sha1_op.h"
14 #include "headers/defs.h"
15
16 /* OpenSSL SHA-1
17  * Only use if OpenSSL is not available
18 #ifndef LIBOPENSSL_ENABLED
19 #include "sha.h"
20 #include "sha_locl.h"
21 #else
22 #include <openssl/sha.h>
23 #endif
24 */
25
26 #include "sha_locl.h"
27
28
29 int OS_SHA1_File(const char *fname, os_sha1 output, int mode)
30 {
31     SHA_CTX c;
32     FILE *fp;
33     unsigned char buf[2048 + 2];
34     unsigned char md[SHA_DIGEST_LENGTH];
35     size_t n;
36
37     memset(output, 0, 65);
38     buf[2049] = '\0';
39
40     fp = fopen(fname, mode == OS_BINARY ? "rb" : "r");
41     if (!fp) {
42         return (-1);
43     }
44
45     SHA1_Init(&c);
46     while ((n = fread(buf, 1, 2048, fp)) > 0) {
47         buf[n] = '\0';
48         SHA1_Update(&c, buf, n);
49     }
50
51     SHA1_Final(&(md[0]), &c);
52
53     for (n = 0; n < SHA_DIGEST_LENGTH; n++) {
54         snprintf(output, 3, "%02x", md[n]);
55         output += 2;
56     }
57
58     fclose(fp);
59
60     return (0);
61 }