1 /* $OpenBSD: t1_hash.c,v 1.2 2017/05/06 16:18:36 jsing Exp $ */
3 * Copyright (c) 2017 Joel Sing <jsing@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <openssl/ssl.h>
23 tls1_handshake_hash_init(SSL
*s
)
29 tls1_handshake_hash_free(s
);
31 if (!ssl_get_handshake_evp_md(s
, &md
)) {
32 SSLerrorx(ERR_R_INTERNAL_ERROR
);
36 if ((S3I(s
)->handshake_hash
= EVP_MD_CTX_create()) == NULL
) {
37 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
40 if (!EVP_DigestInit_ex(S3I(s
)->handshake_hash
, md
, NULL
)) {
41 SSLerror(s
, ERR_R_EVP_LIB
);
45 dlen
= BIO_get_mem_data(S3I(s
)->handshake_buffer
, &data
);
47 SSLerror(s
, SSL_R_BAD_HANDSHAKE_LENGTH
);
50 if (!tls1_handshake_hash_update(s
, data
, dlen
)) {
51 SSLerror(s
, ERR_R_EVP_LIB
);
58 tls1_handshake_hash_free(s
);
64 tls1_handshake_hash_update(SSL
*s
, const unsigned char *buf
, size_t len
)
66 if (S3I(s
)->handshake_hash
== NULL
)
69 return EVP_DigestUpdate(S3I(s
)->handshake_hash
, buf
, len
);
73 tls1_handshake_hash_value(SSL
*s
, const unsigned char *out
, size_t len
,
76 EVP_MD_CTX
*mdctx
= NULL
;
80 if (EVP_MD_CTX_size(S3I(s
)->handshake_hash
) > len
)
83 if ((mdctx
= EVP_MD_CTX_create()) == NULL
) {
84 SSLerror(s
, ERR_R_MALLOC_FAILURE
);
87 if (!EVP_MD_CTX_copy_ex(mdctx
, S3I(s
)->handshake_hash
)) {
88 SSLerror(s
, ERR_R_EVP_LIB
);
91 if (!EVP_DigestFinal_ex(mdctx
, (unsigned char *)out
, &mdlen
)) {
92 SSLerror(s
, ERR_R_EVP_LIB
);
101 EVP_MD_CTX_destroy(mdctx
);
107 tls1_handshake_hash_free(SSL
*s
)
109 EVP_MD_CTX_destroy(S3I(s
)->handshake_hash
);
110 S3I(s
)->handshake_hash
= NULL
;