1 /* $OpenBSD: m_md5_sha1.c,v 1.4 2022/01/14 08:38:06 tb Exp $ */
2 /*
3  * Copyright (c) 2017 Joel Sing <jsing@openbsd.org>
4  *
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.
8  *
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.
16  */
17 
18 #include <openssl/evp.h>
19 #include <openssl/md5.h>
20 #include <openssl/objects.h>
21 #include <openssl/sha.h>
22 
23 #ifndef OPENSSL_NO_RSA
24 #include <openssl/rsa.h>
25 #endif
26 
27 #include "evp_locl.h"
28 
29 struct md5_sha1_ctx {
30 	MD5_CTX md5;
31 	SHA_CTX sha1;
32 };
33 
34 static int
35 md5_sha1_init(EVP_MD_CTX *ctx)
36 {
37 	struct md5_sha1_ctx *mdctx = ctx->md_data;
38 
39 	if (!MD5_Init(&mdctx->md5))
40 		return 0;
41 	if (!SHA1_Init(&mdctx->sha1))
42 		return 0;
43 
44 	return 1;
45 }
46 
47 static int
48 md5_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
49 {
50 	struct md5_sha1_ctx *mdctx = ctx->md_data;
51 
52 	if (!MD5_Update(&mdctx->md5, data, count))
53 		return 0;
54 	if (!SHA1_Update(&mdctx->sha1, data, count))
55 		return 0;
56 
57 	return 1;
58 }
59 
60 static int
61 md5_sha1_final(EVP_MD_CTX *ctx, unsigned char *out)
62 {
63 	struct md5_sha1_ctx *mdctx = ctx->md_data;
64 
65 	if (!MD5_Final(out, &mdctx->md5))
66 		return 0;
67 	if (!SHA1_Final(out + MD5_DIGEST_LENGTH, &mdctx->sha1))
68 		return 0;
69 
70 	return 1;
71 }
72 
73 static const EVP_MD md5_sha1_md = {
74         .type = NID_md5_sha1,
75         .pkey_type = NID_md5_sha1,
76         .md_size = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
77         .flags = 0,
78         .init = md5_sha1_init,
79         .update = md5_sha1_update,
80         .final = md5_sha1_final,
81         .block_size = MD5_CBLOCK, /* MD5_CBLOCK == SHA_CBLOCK */
82         .ctx_size = sizeof(EVP_MD *) + sizeof(struct md5_sha1_ctx),
83 };
84 
85 const EVP_MD *
86 EVP_md5_sha1(void)
87 {
88 	return &md5_sha1_md;
89 }
90