Coverage Report

Created: 2025-10-28 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/ciphers/cipher_null.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2024 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 <string.h>
11
#include <openssl/crypto.h>
12
#include <openssl/core_dispatch.h>
13
#include <openssl/proverr.h>
14
#include "prov/implementations.h"
15
#include "prov/ciphercommon.h"
16
#include "prov/providercommon.h"
17
#include "providers/implementations/ciphers/cipher_null.inc"
18
19
typedef struct prov_cipher_null_ctx_st {
20
    int enc;
21
    size_t tlsmacsize;
22
    const unsigned char *tlsmac;
23
} PROV_CIPHER_NULL_CTX;
24
25
static OSSL_FUNC_cipher_newctx_fn null_newctx;
26
static void *null_newctx(void *provctx)
27
0
{
28
0
    if (!ossl_prov_is_running())
29
0
        return NULL;
30
31
0
    return OPENSSL_zalloc(sizeof(PROV_CIPHER_NULL_CTX));
32
0
}
33
34
static OSSL_FUNC_cipher_freectx_fn null_freectx;
35
static void null_freectx(void *vctx)
36
0
{
37
0
    OPENSSL_free(vctx);
38
0
}
39
40
static OSSL_FUNC_cipher_encrypt_init_fn null_einit;
41
static int null_einit(void *vctx, const unsigned char *key, size_t keylen,
42
                      const unsigned char *iv, size_t ivlen,
43
                      const OSSL_PARAM params[])
44
0
{
45
0
    PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
46
47
0
    if (!ossl_prov_is_running())
48
0
        return 0;
49
50
0
    ctx->enc = 1;
51
0
    return 1;
52
0
}
53
54
static OSSL_FUNC_cipher_decrypt_init_fn null_dinit;
55
static int null_dinit(void *vctx, const unsigned char *key, size_t keylen,
56
                      const unsigned char *iv, size_t ivlen,
57
                      const OSSL_PARAM params[])
58
0
{
59
0
    if (!ossl_prov_is_running())
60
0
        return 0;
61
62
0
    return 1;
63
0
}
64
65
static OSSL_FUNC_cipher_cipher_fn null_cipher;
66
static int null_cipher(void *vctx, unsigned char *out, size_t *outl,
67
                       size_t outsize, const unsigned char *in, size_t inl)
68
0
{
69
0
    PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
70
71
0
    if (!ossl_prov_is_running())
72
0
        return 0;
73
74
0
    if (!ctx->enc && ctx->tlsmacsize > 0) {
75
        /*
76
         * TLS NULL cipher as per:
77
         * https://tools.ietf.org/html/rfc5246#section-6.2.3.1
78
         */
79
0
        if (inl < ctx->tlsmacsize)
80
0
            return 0;
81
0
        ctx->tlsmac = in + inl - ctx->tlsmacsize;
82
0
        inl -= ctx->tlsmacsize;
83
0
    }
84
0
    if (outsize < inl)
85
0
        return 0;
86
0
    if (out != NULL && in != out)
87
0
        memcpy(out, in, inl);
88
0
    *outl = inl;
89
0
    return 1;
90
0
}
91
92
static OSSL_FUNC_cipher_final_fn null_final;
93
static int null_final(void *vctx, unsigned char *out, size_t *outl,
94
                      size_t outsize)
95
0
{
96
0
    if (!ossl_prov_is_running())
97
0
        return 0;
98
99
0
    *outl = 0;
100
0
    return 1;
101
0
}
102
103
static OSSL_FUNC_cipher_get_params_fn null_get_params;
104
static int null_get_params(OSSL_PARAM params[])
105
16
{
106
16
    return ossl_cipher_generic_get_params(params, 0, 0, 0, 8, 0);
107
16
}
108
109
static OSSL_FUNC_cipher_gettable_ctx_params_fn null_gettable_ctx_params;
110
static const OSSL_PARAM *null_gettable_ctx_params(ossl_unused void *cctx,
111
                                                  ossl_unused void *provctx)
112
16
{
113
16
    return null_get_ctx_params_list;
114
16
}
115
116
static OSSL_FUNC_cipher_get_ctx_params_fn null_get_ctx_params;
117
static int null_get_ctx_params(void *vctx, OSSL_PARAM params[])
118
0
{
119
0
    PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
120
0
    struct null_get_ctx_params_st p;
121
122
0
    if (ctx == NULL || !null_get_ctx_params_decoder(params, &p))
123
0
        return 0;
124
125
0
    if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, 0)) {
126
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
127
0
        return 0;
128
0
    }
129
130
0
    if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, 0)) {
131
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
132
0
        return 0;
133
0
    }
134
135
0
    if (p.mac != NULL
136
0
        && !OSSL_PARAM_set_octet_ptr(p.mac, ctx->tlsmac, ctx->tlsmacsize)) {
137
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
138
0
        return 0;
139
0
    }
140
0
    return 1;
141
0
}
142
143
static OSSL_FUNC_cipher_settable_ctx_params_fn null_settable_ctx_params;
144
static const OSSL_PARAM *null_settable_ctx_params(ossl_unused void *cctx,
145
                                                  ossl_unused void *provctx)
146
0
{
147
0
    return null_set_ctx_params_list;
148
0
}
149
150
static OSSL_FUNC_cipher_set_ctx_params_fn null_set_ctx_params;
151
static int null_set_ctx_params(void *vctx, const OSSL_PARAM params[])
152
0
{
153
0
    PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
154
0
    struct null_set_ctx_params_st p;
155
156
0
    if (ctx == NULL || !null_set_ctx_params_decoder(params, &p))
157
0
        return 0;
158
159
0
    if (p.macsize != NULL
160
0
            && !OSSL_PARAM_get_size_t(p.macsize, &ctx->tlsmacsize)) {
161
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
162
0
        return 0;
163
0
    }
164
165
0
    return 1;
166
0
}
167
168
const OSSL_DISPATCH ossl_null_functions[] = {
169
    { OSSL_FUNC_CIPHER_NEWCTX,
170
      (void (*)(void)) null_newctx },
171
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) null_freectx },
172
    { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) null_newctx },
173
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))null_einit },
174
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))null_dinit },
175
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))null_cipher },
176
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))null_final },
177
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))null_cipher },
178
    { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void)) null_get_params },
179
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
180
        (void (*)(void))ossl_cipher_generic_gettable_params },
181
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))null_get_ctx_params },
182
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
183
      (void (*)(void))null_gettable_ctx_params },
184
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))null_set_ctx_params },
185
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
186
      (void (*)(void))null_settable_ctx_params },
187
    OSSL_DISPATCH_END
188
};