This source file includes following definitions.
- shaxxx_stream
- sha512_stream
- sha384_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_SHA512
27 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
28 #endif
29 #include "sha512.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 % 128 != 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 sha512_ctx *),
51 void *(*finish_ctx) (struct sha512_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 sha512_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 sha512_process_block (buffer, BLOCKSIZE, &ctx);
112 }
113
114 process_partial_block:;
115
116
117 if (sum > 0)
118 sha512_process_bytes (buffer, sum, &ctx);
119
120
121 finish_ctx (&ctx, resblock);
122 free (buffer);
123 return 0;
124 }
125
126 int
127 sha512_stream (FILE *stream, void *resblock)
128 {
129 return shaxxx_stream (stream, "sha512", resblock, SHA512_DIGEST_SIZE,
130 sha512_init_ctx, sha512_finish_ctx);
131 }
132
133 int
134 sha384_stream (FILE *stream, void *resblock)
135 {
136 return shaxxx_stream (stream, "sha384", resblock, SHA384_DIGEST_SIZE,
137 sha384_init_ctx, sha384_finish_ctx);
138 }
139
140
141
142
143
144
145