ssl/statem: Replace size_t with int and add the checks
[openssl.git] / providers / common / digests / sha2_prov.c
1 /*
2  * Copyright 2019 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 #include <openssl/crypto.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/evp.h>
13 #include <openssl/sha.h>
14 #include <openssl/evp.h>
15 #include <openssl/params.h>
16 #include <openssl/core_names.h>
17 #include "internal/core_mkdigest.h"
18 #include "internal/provider_algs.h"
19 #include "internal/sha.h"
20
21 static OSSL_OP_digest_set_ctx_params_fn sha1_set_params;
22 static OSSL_OP_digest_settable_ctx_params_fn sha1_settable_params;
23
24 static const OSSL_PARAM known_sha1_ctx_params[] = {
25     {OSSL_DIGEST_PARAM_SSL3_MS, OSSL_PARAM_OCTET_STRING, NULL, 0, 0},
26     OSSL_PARAM_END
27 };
28
29 static const OSSL_PARAM *sha1_settable_params(void)
30 {
31     return known_sha1_ctx_params;
32 }
33
34 /* Special set_params method for SSL3 */
35 static int sha1_set_params(void *vctx, const OSSL_PARAM params[])
36 {
37     const OSSL_PARAM *p;
38     SHA_CTX *ctx = (SHA_CTX *)vctx;
39
40     if (ctx != NULL && params != NULL) {
41         p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SSL3_MS);
42         if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING)
43             return sha1_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET, p->data_size,
44                              p->data);
45     }
46     return 0;
47 }
48
49 OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(sha1, SHA_CTX,
50                            SHA_CBLOCK, SHA_DIGEST_LENGTH,
51                            EVP_MD_FLAG_DIGALGID_ABSENT,
52                            SHA1_Init, SHA1_Update, SHA1_Final,
53                            sha1_settable_params, sha1_set_params)
54
55 OSSL_FUNC_DIGEST_CONSTRUCT(sha224, SHA256_CTX,
56                            SHA256_CBLOCK, SHA224_DIGEST_LENGTH,
57                            EVP_MD_FLAG_DIGALGID_ABSENT,
58                            SHA224_Init, SHA224_Update, SHA224_Final)
59
60 OSSL_FUNC_DIGEST_CONSTRUCT(sha256, SHA256_CTX,
61                            SHA256_CBLOCK, SHA256_DIGEST_LENGTH,
62                            EVP_MD_FLAG_DIGALGID_ABSENT,
63                            SHA256_Init, SHA256_Update, SHA256_Final)
64
65 OSSL_FUNC_DIGEST_CONSTRUCT(sha384, SHA512_CTX,
66                            SHA512_CBLOCK, SHA384_DIGEST_LENGTH,
67                            EVP_MD_FLAG_DIGALGID_ABSENT,
68                            SHA384_Init, SHA384_Update, SHA384_Final)
69
70 OSSL_FUNC_DIGEST_CONSTRUCT(sha512, SHA512_CTX,
71                            SHA512_CBLOCK, SHA512_DIGEST_LENGTH,
72                            EVP_MD_FLAG_DIGALGID_ABSENT,
73                            SHA512_Init, SHA512_Update, SHA512_Final)
74
75 OSSL_FUNC_DIGEST_CONSTRUCT(sha512_224, SHA512_CTX,
76                            SHA512_CBLOCK, SHA224_DIGEST_LENGTH,
77                            EVP_MD_FLAG_DIGALGID_ABSENT,
78                            sha512_224_init, SHA512_Update, SHA512_Final)
79
80 OSSL_FUNC_DIGEST_CONSTRUCT(sha512_256, SHA512_CTX,
81                            SHA512_CBLOCK, SHA256_DIGEST_LENGTH,
82                            EVP_MD_FLAG_DIGALGID_ABSENT,
83                            sha512_256_init, SHA512_Update, SHA512_Final)
84