evp/bio_enc.c: perform enc_read operation without using overlapping buffers.
[openssl.git] / crypto / evp / m_md5_sha1.c
1 /*
2  * Copyright 2015-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 #if !defined(OPENSSL_NO_MD5)
11
12 # include <openssl/evp.h>
13 # include <openssl/objects.h>
14 # include <openssl/x509.h>
15 # include <openssl/md5.h>
16 # include <openssl/sha.h>
17 # include "internal/cryptlib.h"
18 # include "internal/evp_int.h"
19 # include <openssl/rsa.h>
20
21 struct md5_sha1_ctx {
22     MD5_CTX md5;
23     SHA_CTX sha1;
24 };
25
26 static int init(EVP_MD_CTX *ctx)
27 {
28     struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
29     if (!MD5_Init(&mctx->md5))
30         return 0;
31     return SHA1_Init(&mctx->sha1);
32 }
33
34 static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
35 {
36     struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
37     if (!MD5_Update(&mctx->md5, data, count))
38         return 0;
39     return SHA1_Update(&mctx->sha1, data, count);
40 }
41
42 static int final(EVP_MD_CTX *ctx, unsigned char *md)
43 {
44     struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
45     if (!MD5_Final(md, &mctx->md5))
46         return 0;
47     return SHA1_Final(md + MD5_DIGEST_LENGTH, &mctx->sha1);
48 }
49
50 static int ctrl(EVP_MD_CTX *ctx, int cmd, int mslen, void *ms)
51 {
52     unsigned char padtmp[48];
53     unsigned char md5tmp[MD5_DIGEST_LENGTH];
54     unsigned char sha1tmp[SHA_DIGEST_LENGTH];
55     struct md5_sha1_ctx *mctx = EVP_MD_CTX_md_data(ctx);
56
57     if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
58         return 0;
59
60     /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
61     if (mslen != 48)
62         return 0;
63
64     /* At this point hash contains all handshake messages, update
65      * with master secret and pad_1.
66      */
67
68     if (update(ctx, ms, mslen) <= 0)
69         return 0;
70
71     /* Set padtmp to pad_1 value */
72     memset(padtmp, 0x36, sizeof(padtmp));
73
74     if (!MD5_Update(&mctx->md5, padtmp, sizeof(padtmp)))
75         return 0;
76
77     if (!MD5_Final(md5tmp, &mctx->md5))
78         return 0;
79
80     if (!SHA1_Update(&mctx->sha1, padtmp, 40))
81         return 0;
82
83     if (!SHA1_Final(sha1tmp, &mctx->sha1))
84         return 0;
85
86     /* Reinitialise context */
87
88     if (!init(ctx))
89         return 0;
90
91     if (update(ctx, ms, mslen) <= 0)
92         return 0;
93
94     /* Set padtmp to pad_2 value */
95     memset(padtmp, 0x5c, sizeof(padtmp));
96
97     if (!MD5_Update(&mctx->md5, padtmp, sizeof(padtmp)))
98         return 0;
99
100     if (!MD5_Update(&mctx->md5, md5tmp, sizeof(md5tmp)))
101         return 0;
102
103     if (!SHA1_Update(&mctx->sha1, padtmp, 40))
104         return 0;
105
106     if (!SHA1_Update(&mctx->sha1, sha1tmp, sizeof(sha1tmp)))
107         return 0;
108
109     /* Now when ctx is finalised it will return the SSL v3 hash value */
110
111     OPENSSL_cleanse(md5tmp, sizeof(md5tmp));
112     OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
113
114     return 1;
115
116 }
117
118 static const EVP_MD md5_sha1_md = {
119     NID_md5_sha1,
120     NID_md5_sha1,
121     MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
122     0,
123     init,
124     update,
125     final,
126     NULL,
127     NULL,
128     MD5_CBLOCK,
129     sizeof(EVP_MD *) + sizeof(struct md5_sha1_ctx),
130     ctrl
131 };
132
133 const EVP_MD *EVP_md5_sha1(void)
134 {
135     return &md5_sha1_md;
136 }
137 #endif