1sha2(3EXT)                Extended Library Functions                sha2(3EXT)
2
3
4

NAME

6       sha2,   SHA2Init,   SHA2Update,  SHA2Final,  SHA256Init,  SHA256Update,
7       SHA256Final,   SHA384Init,   SHA384Update,   SHA384Final,   SHA512Init,
8       SHA512Update, SHA512Final - SHA2 digest functions
9

SYNOPSIS

11       cc [ flag ... ] file ... -lmd [ library ... ]
12       #include <sha2.h>
13
14       void SHA2Init(uint64_t mech, SHA2_CTX *context);
15
16
17       void SHA2Update(SHA2_CTX *context, unsigned char *input,
18            unsigned int inlen);
19
20
21       void SHA2Final(unsigned char *output, SHA2_CTX *context);
22
23
24       void SHA256Init(SHA256_CTX *context);
25
26
27       void SHA256Update(SHA256_CTX *context, unsigned char *input,
28            unsigned int inlen);
29
30
31       void SHA256Final(unsigned char *output, SHA256_CTX *context);
32
33
34       void SHA384Init(SHA384_CTX *context);
35
36
37       void SHA384Update(SHA384_CTX *context, unsigned char *input,
38            unsigned int inlen);
39
40
41       void SHA384Final(unsigned char *output, 384_CTX *context);
42
43
44       void SHA512Init(SHA512_CTX *context);
45
46
47       void SHA512Update(SHA512_CTX *context, unsigned char *input,
48            unsigned int inlen);
49
50
51       void SHA512Final(unsigned char *output, 512_CTX *context);
52
53

DESCRIPTION

55       The  SHA2Init(),  SHA2Update(),  SHA2Final()  functions  implement  the
56       SHA256, SHA384 and SHA512  message-digest  algorithms.  The  algorithms
57       take  as  input  a  message  of arbitrary length and produces a 200-bit
58       "fingerprint" or "message digest" as output.  The  SHA2  message-digest
59       algorithms  are  intended  for  digital signature applications in which
60       large files are "compressed" in a secure manner before being  encrypted
61       with  a  private  (secret)  key under a public-key cryptosystem such as
62       RSA.
63
64       SHA2Init(), SHA2Update(), SHA2Final()
65
66           The SHA2Init(), SHA2Update(), and SHA2Final()  functions  allow  an
67           SHA2  digest  to  be computed over multiple message blocks. Between
68           blocks, the state of the SHA2 computation is held in an  SHA2  con‐
69           text  structure allocated by the caller. A complete digest computa‐
70           tion consists of calls to SHA2 functions in  the  following  order:
71           one  call to SHA2Init(), one or more calls to SHA2Update(), and one
72           call to SHA2Final().
73
74           The SHA2Init() function  initializes  the  SHA2  context  structure
75           pointed  to by context. The mech argument is one of SHA256, SHA512,
76           SHA384.
77
78           The SHA2Update() function computes a partial  SHA2  digest  on  the
79           inlen-byte  message block pointed to by input, and updates the SHA2
80           context structure pointed to by context accordingly.
81
82           The SHA2Final() function  generates  the  final  SHA2Final  digest,
83           using  the  SHA2  context structure pointed to by context. The SHA2
84           digest is written to output. After a call to SHA2Final(), the state
85           of  the  context  structure  is undefined. It must be reinitialized
86           with SHA2Init() before it can be used again.
87
88
89       SHA256Init(), SHA256Update(), SHA256Final(), SHA384Init(),
90       SHA384Update(), SHA384Final(), SHA512Init(), SHA512Update(),
91       SHA512Final()
92
93           Alternative APIs exist as named above.  The  Update()  and  Final()
94           sets  of  functions  operate  exactly  as  the previously described
95           SHA2Update()   and   SHA2Final()   functions.   The   SHA256Init(),
96           SHA384Init(), and SHA512Init() functions do not take the mech argu‐
97           ment as it is implicit in the function names.
98
99

RETURN VALUES

101       These functions do not return a value.
102

EXAMPLES

104       Example 1 Authenticate a message found in multiple buffers
105
106
107       The following is a sample function that authenticates a  message  found
108       in  multiple  buffers.  The calling function provides an authentication
109       buffer to contain the result of the SHA2 digest.
110
111
112         #include <sys/types.h>
113         #include <sys/uio.h>
114         #include <sha2.h>
115
116         int
117         AuthenticateMsg(unsigned char *auth_buffer, struct iovec
118                         *messageIov, unsigned int num_buffers)
119         {
120             SHA2_CTX sha2_context;
121             unsigned int i;
122
123             SHA2Init(SHA384, &sha2_context);
124
125             for(i=0; i<num_buffers; i++)
126             {
127                  SHA2Update(&sha2_context, messageIov->iov_base,
128                            messageIov->iov_len);
129                  messageIov += sizeof(struct iovec);
130             }
131
132             SHA2Final(auth_buffer, &sha2_context);
133
134             return 0;
135         }
136
137
138       Example 2 Authenticate a message found in multiple buffers
139
140
141       The following is a sample function that authenticates a  message  found
142       in  multiple  buffers.  The calling function provides an authentication
143       buffer that will contain the result of the SHA384 digest, using  alter‐
144       native interfaces.
145
146
147         int
148         AuthenticateMsg(unsigned char *auth_buffer, struct iovec
149                         *messageIov, unsigned int num_buffers)
150         {
151             SHA384_CTX ctx;
152             unsigned int i;
153
154             SHA384Init(&ctx);
155
156             for(i=0, i<num_buffers; i++
157             {
158                  SHA384Update(&ctx, messageIov->iov_base,
159                            messageIov->iov_len);
160                  messageIov += sizeof(struct iovec);
161             }
162
163             SHA384Final(auth_buffer, &ctx);
164
165             return 0;
166         }
167
168

ATTRIBUTES

170       See attributes(5) for descriptions of the following attributes:
171
172
173
174
175       ┌─────────────────────────────┬─────────────────────────────┐
176       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
177       ├─────────────────────────────┼─────────────────────────────┤
178       │Interface Stability          │Committed                    │
179       ├─────────────────────────────┼─────────────────────────────┤
180       │MT-Level                     │MT-Safe                      │
181       └─────────────────────────────┴─────────────────────────────┘
182

SEE ALSO

184       libmd(3LIB)
185
186
187       FIPS 180-2
188
189
190
191SunOS 5.11                        13 Nov 2007                       sha2(3EXT)
Impressum