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