Coverage Report

Created: 2023-06-08 06:40

/src/openssl/providers/implementations/ciphers/cipher_chacha20.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2021 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
/* Dispatch functions for chacha20 cipher */
11
12
#include <openssl/proverr.h>
13
#include "cipher_chacha20.h"
14
#include "prov/implementations.h"
15
#include "prov/providercommon.h"
16
17
0
#define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
18
0
#define CHACHA20_BLKLEN (1)
19
0
#define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
20
0
#define CHACHA20_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
21
22
static OSSL_FUNC_cipher_newctx_fn chacha20_newctx;
23
static OSSL_FUNC_cipher_freectx_fn chacha20_freectx;
24
static OSSL_FUNC_cipher_get_params_fn chacha20_get_params;
25
static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params;
26
static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params;
27
static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
28
static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
29
#define chacha20_cipher ossl_cipher_generic_cipher
30
#define chacha20_update ossl_cipher_generic_stream_update
31
#define chacha20_final ossl_cipher_generic_stream_final
32
#define chacha20_gettable_params ossl_cipher_generic_gettable_params
33
34
void ossl_chacha20_initctx(PROV_CHACHA20_CTX *ctx)
35
0
{
36
0
    ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
37
0
                                CHACHA20_BLKLEN * 8,
38
0
                                CHACHA20_IVLEN * 8,
39
0
                                0, CHACHA20_FLAGS,
40
0
                                ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8),
41
0
                                NULL);
42
0
}
43
44
static void *chacha20_newctx(void *provctx)
45
0
{
46
0
    PROV_CHACHA20_CTX *ctx;
47
48
0
    if (!ossl_prov_is_running())
49
0
        return NULL;
50
51
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
52
0
    if (ctx != NULL)
53
0
        ossl_chacha20_initctx(ctx);
54
0
    return ctx;
55
0
}
56
57
static void chacha20_freectx(void *vctx)
58
0
{
59
0
    PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
60
61
0
    if (ctx != NULL) {
62
0
        ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
63
0
        OPENSSL_clear_free(ctx, sizeof(*ctx));
64
0
    }
65
0
}
66
67
static int chacha20_get_params(OSSL_PARAM params[])
68
0
{
69
0
    return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
70
0
                                          CHACHA20_KEYLEN * 8,
71
0
                                          CHACHA20_BLKLEN * 8,
72
0
                                          CHACHA20_IVLEN * 8);
73
0
}
74
75
static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
76
0
{
77
0
    OSSL_PARAM *p;
78
79
0
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
80
0
    if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) {
81
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
82
0
        return 0;
83
0
    }
84
0
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
85
0
    if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) {
86
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
87
0
        return 0;
88
0
    }
89
90
0
    return 1;
91
0
}
92
93
static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
94
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
95
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
96
    OSSL_PARAM_END
97
};
98
const OSSL_PARAM *chacha20_gettable_ctx_params(ossl_unused void *cctx,
99
                                               ossl_unused void *provctx)
100
0
{
101
0
    return chacha20_known_gettable_ctx_params;
102
0
}
103
104
static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
105
0
{
106
0
    const OSSL_PARAM *p;
107
0
    size_t len;
108
109
0
    if (params == NULL)
110
0
        return 1;
111
112
0
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
113
0
    if (p != NULL) {
114
0
        if (!OSSL_PARAM_get_size_t(p, &len)) {
115
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
116
0
            return 0;
117
0
        }
118
0
        if (len != CHACHA20_KEYLEN) {
119
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
120
0
            return 0;
121
0
        }
122
0
    }
123
0
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
124
0
    if (p != NULL) {
125
0
        if (!OSSL_PARAM_get_size_t(p, &len)) {
126
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
127
0
            return 0;
128
0
        }
129
0
        if (len != CHACHA20_IVLEN) {
130
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
131
0
            return 0;
132
0
        }
133
0
    }
134
0
    return 1;
135
0
}
136
137
static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
138
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
139
    OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
140
    OSSL_PARAM_END
141
};
142
const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx,
143
                                               ossl_unused void *provctx)
144
0
{
145
0
    return chacha20_known_settable_ctx_params;
146
0
}
147
148
int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
149
                        const unsigned char *iv, size_t ivlen,
150
                        const OSSL_PARAM params[])
151
0
{
152
0
    int ret;
153
154
    /* The generic function checks for ossl_prov_is_running() */
155
0
    ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
156
0
    if (ret && iv != NULL) {
157
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
158
0
        PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
159
160
0
        hw->initiv(ctx);
161
0
    }
162
0
    if (ret && !chacha20_set_ctx_params(vctx, params))
163
0
        ret = 0;
164
0
    return ret;
165
0
}
166
167
int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
168
                        const unsigned char *iv, size_t ivlen,
169
                        const OSSL_PARAM params[])
170
0
{
171
0
    int ret;
172
173
    /* The generic function checks for ossl_prov_is_running() */
174
0
    ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
175
0
    if (ret && iv != NULL) {
176
0
        PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
177
0
        PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
178
179
0
        hw->initiv(ctx);
180
0
    }
181
0
    if (ret && !chacha20_set_ctx_params(vctx, params))
182
0
        ret = 0;
183
0
    return ret;
184
0
}
185
186
/* ossl_chacha20_functions */
187
const OSSL_DISPATCH ossl_chacha20_functions[] = {
188
    { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
189
    { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
190
    { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit },
191
    { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit },
192
    { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
193
    { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
194
    { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
195
    { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
196
    { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))chacha20_gettable_params },
197
    { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
198
    { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
199
        (void (*)(void))chacha20_gettable_ctx_params },
200
    { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
201
    { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
202
        (void (*)(void))chacha20_settable_ctx_params },
203
    OSSL_DISPATCH_END
204
};
205