Merge commit 'dfc115332c94a2f62058ac7f2bce7631fbd20b3d'
[unleashed/tickless.git] / lib / libssl / t1_hash.c
blobaef6e65729b074321531cc7590a6a5e51cace31a
1 /* $OpenBSD: t1_hash.c,v 1.2 2017/05/06 16:18:36 jsing Exp $ */
2 /*
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.
18 #include "ssl_locl.h"
20 #include <openssl/ssl.h>
22 int
23 tls1_handshake_hash_init(SSL *s)
25 const EVP_MD *md;
26 long dlen;
27 void *data;
29 tls1_handshake_hash_free(s);
31 if (!ssl_get_handshake_evp_md(s, &md)) {
32 SSLerrorx(ERR_R_INTERNAL_ERROR);
33 goto err;
36 if ((S3I(s)->handshake_hash = EVP_MD_CTX_create()) == NULL) {
37 SSLerror(s, ERR_R_MALLOC_FAILURE);
38 goto err;
40 if (!EVP_DigestInit_ex(S3I(s)->handshake_hash, md, NULL)) {
41 SSLerror(s, ERR_R_EVP_LIB);
42 goto err;
45 dlen = BIO_get_mem_data(S3I(s)->handshake_buffer, &data);
46 if (dlen <= 0) {
47 SSLerror(s, SSL_R_BAD_HANDSHAKE_LENGTH);
48 goto err;
50 if (!tls1_handshake_hash_update(s, data, dlen)) {
51 SSLerror(s, ERR_R_EVP_LIB);
52 goto err;
55 return 1;
57 err:
58 tls1_handshake_hash_free(s);
60 return 0;
63 int
64 tls1_handshake_hash_update(SSL *s, const unsigned char *buf, size_t len)
66 if (S3I(s)->handshake_hash == NULL)
67 return 1;
69 return EVP_DigestUpdate(S3I(s)->handshake_hash, buf, len);
72 int
73 tls1_handshake_hash_value(SSL *s, const unsigned char *out, size_t len,
74 size_t *outlen)
76 EVP_MD_CTX *mdctx = NULL;
77 unsigned int mdlen;
78 int ret = 0;
80 if (EVP_MD_CTX_size(S3I(s)->handshake_hash) > len)
81 goto err;
83 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
84 SSLerror(s, ERR_R_MALLOC_FAILURE);
85 goto err;
87 if (!EVP_MD_CTX_copy_ex(mdctx, S3I(s)->handshake_hash)) {
88 SSLerror(s, ERR_R_EVP_LIB);
89 goto err;
91 if (!EVP_DigestFinal_ex(mdctx, (unsigned char *)out, &mdlen)) {
92 SSLerror(s, ERR_R_EVP_LIB);
93 goto err;
95 if (outlen != NULL)
96 *outlen = mdlen;
98 ret = 1;
100 err:
101 EVP_MD_CTX_destroy(mdctx);
103 return (ret);
106 void
107 tls1_handshake_hash_free(SSL *s)
109 EVP_MD_CTX_destroy(S3I(s)->handshake_hash);
110 S3I(s)->handshake_hash = NULL;