This source file includes following definitions.
- shaxxx_stream
- sha256_stream
- sha224_stream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <config.h>
24
25
26 #if HAVE_OPENSSL_SHA256
27 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
28 #endif
29 #include "sha256.h"
30
31 #include <stdlib.h>
32
33 #if USE_UNLOCKED_IO
34 # include "unlocked-io.h"
35 #endif
36
37 #include "af_alg.h"
38
39 #define BLOCKSIZE 32768
40 #if BLOCKSIZE % 64 != 0
41 # error "invalid BLOCKSIZE"
42 #endif
43
44
45
46
47
48 static int
49 shaxxx_stream (FILE *stream, char const *alg, void *resblock,
50 ssize_t hashlen, void (*init_ctx) (struct sha256_ctx *),
51 void *(*finish_ctx) (struct sha256_ctx *, void *))
52 {
53 switch (afalg_stream (stream, alg, resblock, hashlen))
54 {
55 case 0: return 0;
56 case -EIO: return 1;
57 }
58
59 char *buffer = malloc (BLOCKSIZE + 72);
60 if (!buffer)
61 return 1;
62
63 struct sha256_ctx ctx;
64 init_ctx (&ctx);
65 size_t sum;
66
67
68 while (1)
69 {
70
71
72
73 size_t n;
74 sum = 0;
75
76
77 while (1)
78 {
79
80
81
82
83
84 if (feof (stream))
85 goto process_partial_block;
86
87 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
88
89 sum += n;
90
91 if (sum == BLOCKSIZE)
92 break;
93
94 if (n == 0)
95 {
96
97
98
99 if (ferror (stream))
100 {
101 free (buffer);
102 return 1;
103 }
104 goto process_partial_block;
105 }
106 }
107
108
109
110
111 sha256_process_block (buffer, BLOCKSIZE, &ctx);
112 }
113
114 process_partial_block:;
115
116
117 if (sum > 0)
118 sha256_process_bytes (buffer, sum, &ctx);
119
120
121 finish_ctx (&ctx, resblock);
122 free (buffer);
123 return 0;
124 }
125
126 int
127 sha256_stream (FILE *stream, void *resblock)
128 {
129 return shaxxx_stream (stream, "sha256", resblock, SHA256_DIGEST_SIZE,
130 sha256_init_ctx, sha256_finish_ctx);
131 }
132
133 int
134 sha224_stream (FILE *stream, void *resblock)
135 {
136 return shaxxx_stream (stream, "sha224", resblock, SHA224_DIGEST_SIZE,
137 sha224_init_ctx, sha224_finish_ctx);
138 }
139
140
141
142
143
144
145