• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #ifndef OPENSSL_HEADER_DIGEST_H
11 #define OPENSSL_HEADER_DIGEST_H
12 
13 #include <openssl/base.h>
14 
15 #if defined(__cplusplus)
16 extern "C" {
17 #endif
18 
19 
20 // Digest functions.
21 //
22 // An EVP_MD abstracts the details of a specific hash function allowing code to
23 // deal with the concept of a "hash function" without needing to know exactly
24 // which hash function it is.
25 
26 
27 // Hash algorithms.
28 //
29 // The following functions return |EVP_MD| objects that implement the named hash
30 // function.
31 
32 OPENSSL_EXPORT const EVP_MD *EVP_md4(void);
33 OPENSSL_EXPORT const EVP_MD *EVP_md5(void);
34 OPENSSL_EXPORT const EVP_MD *EVP_sha1(void);
35 OPENSSL_EXPORT const EVP_MD *EVP_sha224(void);
36 OPENSSL_EXPORT const EVP_MD *EVP_sha256(void);
37 OPENSSL_EXPORT const EVP_MD *EVP_sha384(void);
38 OPENSSL_EXPORT const EVP_MD *EVP_sha512(void);
39 OPENSSL_EXPORT const EVP_MD *EVP_sha512_256(void);
40 OPENSSL_EXPORT const EVP_MD *EVP_blake2b256(void);
41 
42 // EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of
43 // MD5 and SHA-1, as used in TLS 1.1 and below.
44 OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void);
45 
46 // EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no
47 // such digest is known.
48 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid);
49 
50 // EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL
51 // if no such digest is known.
52 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj);
53 
54 
55 // Digest contexts.
56 //
57 // An EVP_MD_CTX represents the state of a specific digest operation in
58 // progress.
59 
60 // EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the
61 // same as setting the structure to zero.
62 OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
63 
64 // EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns
65 // it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to
66 // release the resulting object.
67 OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void);
68 
69 // EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a
70 // freshly initialised state. It does not free |ctx| itself. It returns one.
71 OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
72 
73 // EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the
74 // actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain
75 // more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but
76 // this function does not zero out more than just the digest state even in that
77 // case.
78 OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx);
79 
80 // EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself.
81 OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
82 
83 // EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a
84 // copy of |in|. It returns one on success and zero on allocation failure.
85 OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
86 
87 // EVP_MD_CTX_move sets |out|, which must already be initialised, to the hash
88 // state in |in|. |in| is mutated and left in an empty state.
89 OPENSSL_EXPORT void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in);
90 
91 // EVP_MD_CTX_reset calls |EVP_MD_CTX_cleanup| followed by |EVP_MD_CTX_init|. It
92 // returns one.
93 OPENSSL_EXPORT int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
94 
95 
96 // Digest operations.
97 
98 // EVP_DigestInit_ex configures |ctx|, which must already have been
99 // initialised, for a fresh hashing operation using |type|. It returns one on
100 // success and zero on allocation failure.
101 OPENSSL_EXPORT int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
102                                      ENGINE *engine);
103 
104 // EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is
105 // initialised before use.
106 OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
107 
108 // EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation
109 // in |ctx|. It returns one.
110 OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
111                                     size_t len);
112 
113 // EVP_MAX_MD_SIZE is the largest digest size supported, in bytes.
114 // Functions that output a digest generally require the buffer have
115 // at least this much space.
116 #define EVP_MAX_MD_SIZE 64  // SHA-512 is the longest so far.
117 
118 // EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in
119 // bytes.
120 #define EVP_MAX_MD_BLOCK_SIZE 128  // SHA-512 is the longest so far.
121 
122 // EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to
123 // |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most
124 // |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the
125 // number of bytes written. It returns one. After this call, the hash cannot be
126 // updated or finished again until |EVP_DigestInit_ex| is called to start
127 // another hashing operation.
128 OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out,
129                                       unsigned int *out_size);
130 
131 // EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that
132 // |EVP_MD_CTX_cleanup| is called on |ctx| before returning.
133 OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out,
134                                    unsigned int *out_size);
135 
136 // EVP_Digest performs a complete hashing operation in one call. It hashes |len|
137 // bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes
138 // are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL
139 // then |*out_size| is set to the number of bytes written. It returns one on
140 // success and zero otherwise.
141 OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out,
142                               unsigned int *md_out_size, const EVP_MD *type,
143                               ENGINE *impl);
144 
145 
146 // Digest function accessors.
147 //
148 // These functions allow code to learn details about an abstract hash
149 // function.
150 
151 // EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.)
152 OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md);
153 
154 // EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*|
155 // values, ORed together.
156 OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md);
157 
158 // EVP_MD_size returns the digest size of |md|, in bytes.
159 OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md);
160 
161 // EVP_MD_block_size returns the native block-size of |md|, in bytes.
162 OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md);
163 
164 // EVP_MD_FLAG_PKEY_DIGEST indicates that the digest function is used with a
165 // specific public key in order to verify signatures. (For example,
166 // EVP_dss1.)
167 #define EVP_MD_FLAG_PKEY_DIGEST 1
168 
169 // EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509
170 // DigestAlgorithmIdentifier representing this digest function should be
171 // undefined rather than NULL.
172 #define EVP_MD_FLAG_DIGALGID_ABSENT 2
173 
174 // EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function
175 // (XOF). This flag is defined for compatibility and will never be set in any
176 // |EVP_MD| in BoringSSL.
177 #define EVP_MD_FLAG_XOF 4
178 
179 
180 // Digest operation accessors.
181 
182 // EVP_MD_CTX_get0_md returns the underlying digest function, or NULL if one has
183 // not been set.
184 OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx);
185 
186 // EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not
187 // been set. (This is the same as |EVP_MD_CTX_get0_md| but OpenSSL has
188 // deprecated this spelling.)
189 OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
190 
191 // EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It
192 // will crash if a digest hasn't been set on |ctx|.
193 OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
194 
195 // EVP_MD_CTX_block_size returns the block size of the digest function used by
196 // |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|.
197 OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx);
198 
199 // EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|.
200 // (For example, |NID_sha256|.) It will crash if a digest hasn't been set on
201 // |ctx|.
202 OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx);
203 
204 
205 // ASN.1 functions.
206 //
207 // These functions allow code to parse and serialize AlgorithmIdentifiers for
208 // hash functions.
209 
210 // EVP_parse_digest_algorithm parses an AlgorithmIdentifier structure containing
211 // a hash function OID (for example, 2.16.840.1.101.3.4.2.1 is SHA-256) and
212 // advances |cbs|. The parameters field may either be omitted or a NULL. It
213 // returns the digest function or NULL on error.
214 OPENSSL_EXPORT const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs);
215 
216 // EVP_marshal_digest_algorithm marshals |md| as an AlgorithmIdentifier
217 // structure and appends the result to |cbb|. It returns one on success and zero
218 // on error.
219 OPENSSL_EXPORT int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md);
220 
221 
222 // Deprecated functions.
223 
224 // EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of
225 // |in|. It returns one on success and zero on error.
226 OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in);
227 
228 // EVP_add_digest does nothing and returns one. It exists only for
229 // compatibility with OpenSSL.
230 OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest);
231 
232 // EVP_get_digestbyname returns an |EVP_MD| given a human readable name in
233 // |name|, or NULL if the name is unknown.
234 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *);
235 
236 // EVP_dss1 returns the value of EVP_sha1(). This was provided by OpenSSL to
237 // specifiy the original DSA signatures, which were fixed to use SHA-1. Note,
238 // however, that attempting to sign or verify DSA signatures with the EVP
239 // interface will always fail.
240 OPENSSL_EXPORT const EVP_MD *EVP_dss1(void);
241 
242 // EVP_MD_CTX_create calls |EVP_MD_CTX_new|.
243 OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void);
244 
245 // EVP_MD_CTX_destroy calls |EVP_MD_CTX_free|.
246 OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
247 
248 // EVP_DigestFinalXOF returns zero and adds an error to the error queue.
249 // BoringSSL does not support any XOF digests.
250 OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out,
251                                       size_t len);
252 
253 // EVP_MD_meth_get_flags calls |EVP_MD_flags|.
254 OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md);
255 
256 // EVP_MD_CTX_set_flags does nothing.
257 OPENSSL_EXPORT void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
258 
259 // EVP_MD_CTX_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS
260 // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms
261 // (it's up the the caller to use the FIPS module in a fashion compliant with
262 // their needs). Thus this exists only to allow code to compile.
263 #define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0
264 
265 // EVP_MD_nid calls |EVP_MD_type|.
266 OPENSSL_EXPORT int EVP_MD_nid(const EVP_MD *md);
267 
268 
269 struct evp_md_pctx_ops;
270 
271 struct env_md_ctx_st {
272   // digest is the underlying digest function, or NULL if not set.
273   const EVP_MD *digest;
274   // md_data points to a block of memory that contains the hash-specific
275   // context.
276   void *md_data;
277 
278   // pctx is an opaque (at this layer) pointer to additional context that
279   // EVP_PKEY functions may store in this object.
280   EVP_PKEY_CTX *pctx;
281 
282   // pctx_ops, if not NULL, points to a vtable that contains functions to
283   // manipulate |pctx|.
284   const struct evp_md_pctx_ops *pctx_ops;
285 } /* EVP_MD_CTX */;
286 
287 
288 #if defined(__cplusplus)
289 }  // extern C
290 
291 #if !defined(BORINGSSL_NO_CXX)
292 extern "C++" {
293 
294 BSSL_NAMESPACE_BEGIN
295 
296 BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free)
297 
298 using ScopedEVP_MD_CTX =
299     internal::StackAllocatedMovable<EVP_MD_CTX, int, EVP_MD_CTX_init,
300                                     EVP_MD_CTX_cleanup, EVP_MD_CTX_move>;
301 
302 BSSL_NAMESPACE_END
303 
304 }  // extern C++
305 #endif
306 
307 #endif
308 
309 #define DIGEST_R_INPUT_NOT_INITIALIZED 100
310 #define DIGEST_R_DECODE_ERROR 101
311 #define DIGEST_R_UNKNOWN_HASH 102
312 
313 #endif  // OPENSSL_HEADER_DIGEST_H
314