blob: ad6dd507e674f7954c1b1e2f50196a99c540ad61 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#ifndef OPENSSL_HEADER_DIGEST_H
58#define OPENSSL_HEADER_DIGEST_H
59
60#include <openssl/base.h>
61
62#if defined(__cplusplus)
63extern "C" {
64#endif
65
66
David Benjamin4512b792017-08-18 19:21:50 -040067// Digest functions.
68//
69// An EVP_MD abstracts the details of a specific hash function allowing code to
70// deal with the concept of a "hash function" without needing to know exactly
71// which hash function it is.
Adam Langley95c29f32014-06-20 12:00:00 -070072
73
David Benjamin4512b792017-08-18 19:21:50 -040074// Hash algorithms.
75//
76// The following functions return |EVP_MD| objects that implement the named hash
77// function.
Adam Langley95c29f32014-06-20 12:00:00 -070078
Adam Langleyc9eb7ea2014-08-22 11:06:14 -070079OPENSSL_EXPORT const EVP_MD *EVP_md4(void);
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070080OPENSSL_EXPORT const EVP_MD *EVP_md5(void);
81OPENSSL_EXPORT const EVP_MD *EVP_sha1(void);
82OPENSSL_EXPORT const EVP_MD *EVP_sha224(void);
83OPENSSL_EXPORT const EVP_MD *EVP_sha256(void);
84OPENSSL_EXPORT const EVP_MD *EVP_sha384(void);
85OPENSSL_EXPORT const EVP_MD *EVP_sha512(void);
Adam Langley95c29f32014-06-20 12:00:00 -070086
David Benjamin4512b792017-08-18 19:21:50 -040087// EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of
88// MD5 and SHA-1, as used in TLS 1.1 and below.
David Benjamin00505ec2014-11-26 16:38:00 -050089OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void);
90
David Benjamin4512b792017-08-18 19:21:50 -040091// EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no
92// such digest is known.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070093OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid);
Adam Langley95c29f32014-06-20 12:00:00 -070094
David Benjamin4512b792017-08-18 19:21:50 -040095// EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL
96// if no such digest is known.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070097OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj);
Adam Langley95c29f32014-06-20 12:00:00 -070098
99
David Benjamin4512b792017-08-18 19:21:50 -0400100// Digest contexts.
101//
102// An EVP_MD_CTX represents the state of a specific digest operation in
103// progress.
Adam Langley95c29f32014-06-20 12:00:00 -0700104
David Benjamin4512b792017-08-18 19:21:50 -0400105// EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the
106// same as setting the structure to zero.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700107OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700108
David Benjamin4512b792017-08-18 19:21:50 -0400109// EVP_MD_CTX_create allocates and initialises a fresh |EVP_MD_CTX| and returns
110// it, or NULL on allocation failure.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700111OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700112
David Benjamin4512b792017-08-18 19:21:50 -0400113// EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a
114// freshly initialised state. It does not free |ctx| itself. It returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700115OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700116
David Benjamin4512b792017-08-18 19:21:50 -0400117// EVP_MD_CTX_destroy calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700118OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700119
David Benjamin4512b792017-08-18 19:21:50 -0400120// EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a
121// copy of |in|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700122OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
Adam Langley95c29f32014-06-20 12:00:00 -0700123
124
David Benjamin4512b792017-08-18 19:21:50 -0400125// Digest operations.
Adam Langley95c29f32014-06-20 12:00:00 -0700126
David Benjamin4512b792017-08-18 19:21:50 -0400127// EVP_DigestInit_ex configures |ctx|, which must already have been
128// initialised, for a fresh hashing operation using |type|. It returns one on
129// success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700130OPENSSL_EXPORT int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
131 ENGINE *engine);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin4512b792017-08-18 19:21:50 -0400133// EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is
134// initialised before use.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700135OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
Adam Langley95c29f32014-06-20 12:00:00 -0700136
David Benjamin4512b792017-08-18 19:21:50 -0400137// EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation
138// in |ctx|. It returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700139OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
140 size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700141
David Benjamin4512b792017-08-18 19:21:50 -0400142// EVP_MAX_MD_SIZE is the largest digest size supported, in bytes.
143// Functions that output a digest generally require the buffer have
144// at least this much space.
145#define EVP_MAX_MD_SIZE 64 // SHA-512 is the longest so far.
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin4512b792017-08-18 19:21:50 -0400147// EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in
148// bytes.
149#define EVP_MAX_MD_BLOCK_SIZE 128 // SHA-512 is the longest so far.
David Benjamin582d2842016-04-16 17:10:01 -0400150
David Benjamin4512b792017-08-18 19:21:50 -0400151// EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to
152// |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most
153// |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the
154// number of bytes written. It returns one. After this call, the hash cannot be
155// updated or finished again until |EVP_DigestInit_ex| is called to start
156// another hashing operation.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700157OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out,
158 unsigned int *out_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700159
David Benjamin4512b792017-08-18 19:21:50 -0400160// EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that
161// |EVP_MD_CTX_cleanup| is called on |ctx| before returning.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700162OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out,
163 unsigned int *out_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamin4512b792017-08-18 19:21:50 -0400165// EVP_Digest performs a complete hashing operation in one call. It hashes |len|
166// bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes
167// are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL
168// then |*out_size| is set to the number of bytes written. It returns one on
169// success and zero otherwise.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700170OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out,
171 unsigned int *md_out_size, const EVP_MD *type,
172 ENGINE *impl);
Adam Langley95c29f32014-06-20 12:00:00 -0700173
174
David Benjamin4512b792017-08-18 19:21:50 -0400175// Digest function accessors.
176//
177// These functions allow code to learn details about an abstract hash
178// function.
Adam Langley95c29f32014-06-20 12:00:00 -0700179
David Benjamin4512b792017-08-18 19:21:50 -0400180// EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.)
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700181OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md);
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjamin4512b792017-08-18 19:21:50 -0400183// EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*|
184// values, ORed together.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700185OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md);
Adam Langley95c29f32014-06-20 12:00:00 -0700186
David Benjamin4512b792017-08-18 19:21:50 -0400187// EVP_MD_size returns the digest size of |md|, in bytes.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700188OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md);
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjamin4512b792017-08-18 19:21:50 -0400190// EVP_MD_block_size returns the native block-size of |md|, in bytes.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700191OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md);
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin4512b792017-08-18 19:21:50 -0400193// EVP_MD_FLAG_PKEY_DIGEST indicates the the digest function is used with a
194// specific public key in order to verify signatures. (For example,
195// EVP_dss1.)
Adam Langley95c29f32014-06-20 12:00:00 -0700196#define EVP_MD_FLAG_PKEY_DIGEST 1
197
David Benjamin4512b792017-08-18 19:21:50 -0400198// EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509
199// DigestAlgorithmIdentifier representing this digest function should be
200// undefined rather than NULL.
Adam Langley95c29f32014-06-20 12:00:00 -0700201#define EVP_MD_FLAG_DIGALGID_ABSENT 2
202
203
David Benjamin4512b792017-08-18 19:21:50 -0400204// Deprecated functions.
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin4512b792017-08-18 19:21:50 -0400206// EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of
207// |in|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700208OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in);
Adam Langley95c29f32014-06-20 12:00:00 -0700209
David Benjamin4512b792017-08-18 19:21:50 -0400210// EVP_add_digest does nothing and returns one. It exists only for
211// compatibility with OpenSSL.
Adam Langleybed8ce72014-09-05 17:04:51 -0700212OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest);
213
David Benjamin4512b792017-08-18 19:21:50 -0400214// EVP_get_digestbyname returns an |EVP_MD| given a human readable name in
215// |name|, or NULL if the name is unknown.
Matt Braithwaite9f8ef2d2015-04-28 19:38:03 -0700216OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *);
217
David Benjamin4512b792017-08-18 19:21:50 -0400218// EVP_dss1 returns the value of EVP_sha1(). This was provided by OpenSSL to
219// specifiy the original DSA signatures, which were fixed to use SHA-1. Note,
220// however, that attempting to sign or verify DSA signatures with the EVP
221// interface will always fail.
Adam Langley1aa03f02016-05-20 08:22:33 -0700222OPENSSL_EXPORT const EVP_MD *EVP_dss1(void);
223
Adam Langley95c29f32014-06-20 12:00:00 -0700224
David Benjamin4512b792017-08-18 19:21:50 -0400225// Digest operation accessors.
Adam Langley95c29f32014-06-20 12:00:00 -0700226
David Benjamin4512b792017-08-18 19:21:50 -0400227// EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not
228// been set.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700229OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjamin4512b792017-08-18 19:21:50 -0400231// EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It
232// will crash if a digest hasn't been set on |ctx|.
David Benjamin1a0a8b62016-04-16 15:23:54 -0400233OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700234
David Benjamin4512b792017-08-18 19:21:50 -0400235// EVP_MD_CTX_block_size returns the block size of the digest function used by
236// |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|.
David Benjamin1a0a8b62016-04-16 15:23:54 -0400237OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjamin4512b792017-08-18 19:21:50 -0400239// EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|.
240// (For example, |NID_sha256|.) It will crash if a digest hasn't been set on
241// |ctx|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700242OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langley95c29f32014-06-20 12:00:00 -0700244
245struct evp_md_pctx_ops;
246
247struct env_md_ctx_st {
David Benjamin4512b792017-08-18 19:21:50 -0400248 // digest is the underlying digest function, or NULL if not set.
Adam Langley95c29f32014-06-20 12:00:00 -0700249 const EVP_MD *digest;
David Benjamin4512b792017-08-18 19:21:50 -0400250 // md_data points to a block of memory that contains the hash-specific
251 // context.
Adam Langley95c29f32014-06-20 12:00:00 -0700252 void *md_data;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamin4512b792017-08-18 19:21:50 -0400254 // pctx is an opaque (at this layer) pointer to additional context that
255 // EVP_PKEY functions may store in this object.
Adam Langley95c29f32014-06-20 12:00:00 -0700256 EVP_PKEY_CTX *pctx;
257
David Benjamin4512b792017-08-18 19:21:50 -0400258 // pctx_ops, if not NULL, points to a vtable that contains functions to
259 // manipulate |pctx|.
Adam Langley95c29f32014-06-20 12:00:00 -0700260 const struct evp_md_pctx_ops *pctx_ops;
261} /* EVP_MD_CTX */;
262
Adam Langley95c29f32014-06-20 12:00:00 -0700263
264#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400265} // extern C
David Benjaminf0e935d2016-09-06 18:10:19 -0400266
267#if !defined(BORINGSSL_NO_CXX)
268extern "C++" {
269
270namespace bssl {
271
272BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_destroy)
273
274using ScopedEVP_MD_CTX =
275 internal::StackAllocated<EVP_MD_CTX, int, EVP_MD_CTX_init,
276 EVP_MD_CTX_cleanup>;
277
278} // namespace bssl
279
280} // extern C++
281#endif
282
Adam Langley95c29f32014-06-20 12:00:00 -0700283#endif
284
Adam Langley95c29f32014-06-20 12:00:00 -0700285#define DIGEST_R_INPUT_NOT_INITIALIZED 100
David Benjamin3cb047e2017-03-14 00:48:11 -0400286#define DIGEST_R_DECODE_ERROR 101
287#define DIGEST_R_UNKNOWN_HASH 102
Adam Langley95c29f32014-06-20 12:00:00 -0700288
David Benjamin4512b792017-08-18 19:21:50 -0400289#endif // OPENSSL_HEADER_DIGEST_H