xref: /openssl/crypto/sha/sha1dgst.c (revision fecb3aae)
1 /*
2  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * SHA-1 low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/crypto.h>
17 #include <openssl/opensslconf.h>
18 
19 #include <openssl/opensslv.h>
20 #include <openssl/evp.h>
21 #include <openssl/sha.h>
22 
23 /* The implementation is in crypto/md32_common.h */
24 
25 #include "sha_local.h"
26 #include "crypto/sha.h"
27 
ossl_sha1_ctrl(SHA_CTX * sha1,int cmd,int mslen,void * ms)28 int ossl_sha1_ctrl(SHA_CTX *sha1, int cmd, int mslen, void *ms)
29 {
30     unsigned char padtmp[40];
31     unsigned char sha1tmp[SHA_DIGEST_LENGTH];
32 
33     if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
34         return -2;
35 
36     if (sha1 == NULL)
37         return 0;
38 
39     /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
40     if (mslen != 48)
41         return 0;
42 
43     /* At this point hash contains all handshake messages, update
44      * with master secret and pad_1.
45      */
46 
47     if (SHA1_Update(sha1, ms, mslen) <= 0)
48         return 0;
49 
50     /* Set padtmp to pad_1 value */
51     memset(padtmp, 0x36, sizeof(padtmp));
52 
53     if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
54         return 0;
55 
56     if (!SHA1_Final(sha1tmp, sha1))
57         return 0;
58 
59     /* Reinitialise context */
60 
61     if (!SHA1_Init(sha1))
62         return 0;
63 
64     if (SHA1_Update(sha1, ms, mslen) <= 0)
65         return 0;
66 
67     /* Set padtmp to pad_2 value */
68     memset(padtmp, 0x5c, sizeof(padtmp));
69 
70     if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
71         return 0;
72 
73     if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
74         return 0;
75 
76     /* Now when ctx is finalised it will return the SSL v3 hash value */
77     OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
78 
79     return 1;
80 }
81