Coverage Report

Created: 2026-12-01 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/cipher-ctr-mt-provider.c
Line
Count
Source
1
/*
2
 * OpenSSH Multi-threaded AES-CTR Cipher Provider for OpenSSL 3
3
 *
4
 * Author: Chris Rapier <rapier@psc.edu>
5
 * Copyright (c) 2022 Pittsburgh Supercomputing Center. All rights reserved.
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
/* based on vienere.c from https://github.com/provider-corner/vigenere by
21
 * Richard Levitte provided under a CC0 Public License. */
22
23
#include "includes.h"
24
25
/* only for systems with OSSL 3.0+ */
26
#ifdef WITH_OPENSSL3
27
28
#include <sys/types.h>
29
#include <string.h>
30
#include <openssl/core.h>
31
#include <openssl/core_dispatch.h>
32
#include <openssl/core_names.h>
33
#include <openssl/err.h>
34
#include <openssl/evp.h>
35
#include "xmalloc.h"
36
#include "ossl3-provider-err.h"
37
#include "num.h"
38
#include "cipher-ctr-mt-functions.h"
39
40
#define ERR_HANDLE(ctx) ((ctx)->provctx->proverr_handle)
41
42
/* forward declartion of cipher functions */
43
/* cipher context functions */
44
OSSL_FUNC_cipher_newctx_fn aes_mt_newctx_256;
45
OSSL_FUNC_cipher_newctx_fn aes_mt_newctx_192;
46
OSSL_FUNC_cipher_newctx_fn aes_mt_newctx_128;
47
OSSL_FUNC_cipher_freectx_fn aes_mt_freectx;
48
49
/* param related function*/
50
static OSSL_FUNC_cipher_get_params_fn aes_mt_get_params_256;
51
static OSSL_FUNC_cipher_get_params_fn aes_mt_get_params_192;
52
static OSSL_FUNC_cipher_get_params_fn aes_mt_get_params_128;
53
static OSSL_FUNC_cipher_gettable_params_fn aes_mt_gettable_params;
54
static OSSL_FUNC_cipher_set_ctx_params_fn aes_mt_set_ctx_params;
55
static OSSL_FUNC_cipher_get_ctx_params_fn aes_mt_get_ctx_params;
56
static OSSL_FUNC_cipher_settable_ctx_params_fn aes_mt_settable_ctx_params;
57
static OSSL_FUNC_cipher_gettable_ctx_params_fn aes_mt_gettable_ctx_params;
58
59
/* en/decipher functions */
60
OSSL_FUNC_cipher_encrypt_init_fn aes_mt_start_threads;
61
OSSL_FUNC_cipher_decrypt_init_fn aes_mt_start_threads;
62
OSSL_FUNC_cipher_update_fn aes_mt_do_cipher;
63
64
/* provider context */
65
static OSSL_FUNC_provider_query_operation_fn aes_mt_prov_query;
66
static OSSL_FUNC_provider_get_reason_strings_fn aes_mt_prov_reasons;
67
static OSSL_FUNC_provider_teardown_fn aes_mt_prov_teardown;
68
static OSSL_FUNC_provider_get_params_fn aes_mt_prov_get_params; 
69
OSSL_provider_init_fn OSSL_provider_init; /* need this? */
70
71
/* error functions */
72
OSSL_FUNC_core_new_error_fn *c_new_error;
73
OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
74
OSSL_FUNC_core_vset_error_fn *c_vset_error;
75
OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
76
OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
77
OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
78
79
/* Errors used in this provider */
80
#define AES_MT_E_MALLOC           1
81
#define AES_MT_BAD_KEYLEN         3
82
83
/* typedef for function pointers */
84
typedef void(*fptr_t)(void);
85
86
/* all of the various arrays we need */
87
88
const OSSL_ITEM reasons[] = {
89
  { AES_MT_E_MALLOC, "Memory allocation failure" },
90
  { AES_MT_BAD_KEYLEN, "Only 256, 192, and 128 Key lengths are supported" },
91
  { 0, NULL } /* Termination */
92
};
93
94
/* function mapping for 256|192|128 key lengths */
95
const OSSL_DISPATCH aes_mt_funcs_256[] = {
96
  { OSSL_FUNC_CIPHER_NEWCTX, (fptr_t)aes_mt_newctx_256 } ,
97
  { OSSL_FUNC_CIPHER_FREECTX, (fptr_t)aes_mt_freectx },
98
  { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (fptr_t)aes_mt_start_threads },
99
  { OSSL_FUNC_CIPHER_DECRYPT_INIT, (fptr_t)aes_mt_start_threads },
100
  { OSSL_FUNC_CIPHER_UPDATE, (fptr_t)aes_mt_do_cipher },
101
  { OSSL_FUNC_CIPHER_GET_PARAMS, (fptr_t)aes_mt_get_params_256 },
102
  { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (fptr_t)aes_mt_gettable_params },
103
  { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (fptr_t)aes_mt_get_ctx_params },
104
  { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
105
    (fptr_t)aes_mt_gettable_ctx_params },
106
  { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (fptr_t)aes_mt_set_ctx_params },
107
  { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
108
    (fptr_t)aes_mt_settable_ctx_params },
109
  { 0, NULL }
110
};
111
112
const OSSL_DISPATCH aes_mt_funcs_192[] = {
113
  { OSSL_FUNC_CIPHER_NEWCTX, (fptr_t)aes_mt_newctx_192 } ,
114
  { OSSL_FUNC_CIPHER_FREECTX, (fptr_t)aes_mt_freectx },
115
  { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (fptr_t)aes_mt_start_threads },
116
  { OSSL_FUNC_CIPHER_DECRYPT_INIT, (fptr_t)aes_mt_start_threads },
117
  { OSSL_FUNC_CIPHER_UPDATE, (fptr_t)aes_mt_do_cipher },
118
  { OSSL_FUNC_CIPHER_GET_PARAMS, (fptr_t)aes_mt_get_params_192 },
119
  { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (fptr_t)aes_mt_gettable_params },
120
  { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (fptr_t)aes_mt_get_ctx_params },
121
  { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
122
    (fptr_t)aes_mt_gettable_ctx_params },
123
  { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (fptr_t)aes_mt_set_ctx_params },
124
  { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
125
    (fptr_t)aes_mt_settable_ctx_params },
126
  { 0, NULL }
127
};
128
129
const OSSL_DISPATCH aes_mt_funcs_128[] = {
130
  { OSSL_FUNC_CIPHER_NEWCTX, (fptr_t)aes_mt_newctx_128 } ,
131
  { OSSL_FUNC_CIPHER_FREECTX, (fptr_t)aes_mt_freectx },
132
  { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (fptr_t)aes_mt_start_threads },
133
  { OSSL_FUNC_CIPHER_DECRYPT_INIT, (fptr_t)aes_mt_start_threads },
134
  { OSSL_FUNC_CIPHER_UPDATE, (fptr_t)aes_mt_do_cipher },
135
  { OSSL_FUNC_CIPHER_GET_PARAMS, (fptr_t)aes_mt_get_params_128 },
136
  { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (fptr_t)aes_mt_gettable_params },
137
  { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (fptr_t)aes_mt_get_ctx_params },
138
  { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
139
    (fptr_t)aes_mt_gettable_ctx_params },
140
  { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (fptr_t)aes_mt_set_ctx_params },
141
  { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
142
    (fptr_t)aes_mt_settable_ctx_params },
143
  { 0, NULL }
144
};
145
146
/* the ciphers found in this provider */
147
const OSSL_ALGORITHM aes_mt_ciphers[] = {
148
  { "aes_ctr_mt_256", "provider=hpnssh", aes_mt_funcs_256, NULL },
149
  { "aes_ctr_mt_192", "provider=hpnssh", aes_mt_funcs_192, NULL },
150
  { "aes_ctr_mt_128", "provider=hpnssh", aes_mt_funcs_128, NULL },
151
  { NULL, NULL, NULL, NULL }
152
};
153
154
/* function mapping for provider methods */
155
const OSSL_DISPATCH provider_functions[] = {
156
  { OSSL_FUNC_PROVIDER_TEARDOWN, (fptr_t)aes_mt_prov_teardown },
157
  { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (fptr_t)aes_mt_prov_query },
158
  { OSSL_FUNC_PROVIDER_GET_PARAMS, (fptr_t)aes_mt_prov_get_params },
159
  { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS, (fptr_t)aes_mt_prov_reasons },
160
  { 0, NULL }
161
};
162
163
static const OSSL_PARAM ctx_get_param_table[] = {
164
        { "keylen", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(size_t), 0 },
165
        { NULL, 0, NULL, 0, 0 },
166
};
167
168
static const OSSL_PARAM cipher_get_param_table[] = {
169
        { "blocksize", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(size_t), 0 },
170
        { "keylen", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(size_t), 0 },
171
        { NULL, 0, NULL, 0, 0 },
172
};
173
174
static const OSSL_PARAM cipher_set_param_table[] = {
175
  { "keylen", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(size_t), 0 },
176
  { NULL, 0, NULL, 0, 0 },
177
};
178
179
180
/* provider functions start here */
181
182
static void provider_ctx_free(struct provider_ctx_st *ctx)
183
0
{
184
0
    if (ctx != NULL)
185
0
        proverr_free_handle(ctx->proverr_handle);
186
0
    free(ctx);
187
0
}
188
189
static struct provider_ctx_st *provider_ctx_new(const OSSL_CORE_HANDLE *core,
190
                                                const OSSL_DISPATCH *in)
191
0
{
192
0
    struct provider_ctx_st *ctx;
193
194
0
    if ((ctx = malloc(sizeof(*ctx))) != NULL
195
0
        && (ctx->proverr_handle = proverr_new_handle(core, in)) != NULL) {
196
0
        ctx->core_handle = core;
197
0
    } else {
198
0
        provider_ctx_free(ctx);
199
0
        ctx = NULL;
200
0
    }
201
0
    return ctx;
202
0
}
203
204
205
/* returns the appropriate algo table for the requested function
206
 * in this case we should only be working with OP_CIPHER */
207
const OSSL_ALGORITHM *aes_mt_prov_query(void *provctx, int operation_id,
208
             int *no_store)
209
0
{
210
0
  switch (operation_id) {
211
0
  case OSSL_OP_CIPHER:
212
0
    return aes_mt_ciphers;
213
0
  }
214
0
  return NULL;
215
0
}
216
217
const OSSL_ITEM *aes_mt_prov_reasons(void *provctx)
218
0
{
219
0
  return reasons;
220
0
}
221
222
static int aes_mt_prov_get_params(void *provctx, OSSL_PARAM *params)
223
0
{
224
0
  OSSL_PARAM *p;
225
0
  int ok = 1;
226
227
0
  char *VERSION="1.0";
228
0
  char *BUILDTYPE="aes_ctr_mt@hpnssh.org";
229
  
230
0
  for(p = params; p->key != NULL; p++)
231
0
    if (strcasecmp(p->key, "version") == 0) {
232
0
      *(const void **)p->data = VERSION;
233
0
      p->return_size = strlen(VERSION);
234
0
    } else if (strcasecmp(p->key, "buildinfo") == 0
235
0
         && BUILDTYPE[0] != '\0') {
236
0
      *(const void **)p->data = BUILDTYPE;
237
0
      p->return_size = strlen(BUILDTYPE);
238
0
    }
239
0
  return ok;
240
0
}
241
242
/* The function that tears down this provider */
243
static void aes_mt_prov_teardown(void *vprovctx)
244
0
{
245
0
    provider_ctx_free(vprovctx);
246
0
}
247
248
int OSSL_provider_init(const OSSL_CORE_HANDLE *core,
249
           const OSSL_DISPATCH *in,
250
           const OSSL_DISPATCH **out,
251
           void **vprovctx)
252
0
{
253
0
    if ((*vprovctx = provider_ctx_new(core, in)) == NULL)
254
0
      return 0;
255
0
    *out = provider_functions;
256
0
    return 1;
257
0
}
258
259
/* parameter functions for 256|192|128 bit key lengths */
260
static int aes_mt_get_params_256(OSSL_PARAM params[])
261
0
{
262
0
  OSSL_PARAM *p;
263
0
  int ok = 1;
264
  
265
0
  for (p = params; p->key != NULL; p++) {
266
0
    if (strcasecmp(p->key, "blocksize") == 0)
267
0
      if (provnum_set_size_t(p, AES_BLOCK_SIZE) < 0) {
268
0
        ok = 0;
269
0
        continue;
270
0
      }
271
0
    if (strcasecmp(p->key, "keylen") == 0) {
272
0
      size_t keyl = 32;
273
      
274
0
      if (provnum_set_size_t(p, keyl) < 0) {
275
0
        ok = 0;
276
0
        continue;
277
0
      }
278
0
    }
279
0
  }
280
0
  return ok;
281
0
}
282
283
static int aes_mt_get_params_192(OSSL_PARAM params[])
284
0
{
285
0
  OSSL_PARAM *p;
286
0
  int ok = 1;
287
  
288
0
  for (p = params; p->key != NULL; p++) {
289
0
    if (strcasecmp(p->key, "blocksize") == 0)
290
0
      if (provnum_set_size_t(p, AES_BLOCK_SIZE) < 0) {
291
0
        ok = 0;
292
0
        continue;
293
0
      }
294
0
    if (strcasecmp(p->key, "keylen") == 0) {
295
0
      size_t keyl = 24;
296
      
297
0
      if (provnum_set_size_t(p, keyl) < 0) {
298
0
        ok = 0;
299
0
        continue;
300
0
      }
301
0
    }
302
0
  }
303
0
  return ok;
304
0
}
305
306
static int aes_mt_get_params_128(OSSL_PARAM params[])
307
0
{
308
0
  OSSL_PARAM *p;
309
0
  int ok = 1;
310
  
311
0
  for (p = params; p->key != NULL; p++) {
312
0
    if (strcasecmp(p->key, "blocksize") == 0)
313
0
      if (provnum_set_size_t(p, AES_BLOCK_SIZE) < 0) {
314
0
        ok = 0;
315
0
        continue;
316
0
      }
317
0
    if (strcasecmp(p->key, "keylen") == 0) {
318
0
      size_t keyl = 16;
319
      
320
0
      if (provnum_set_size_t(p, keyl) < 0) {
321
0
        ok = 0;
322
0
        continue;
323
0
      }
324
0
    }
325
0
  }
326
0
  return ok;
327
0
}
328
329
/* Parameters that libcrypto can get from this implementation */
330
static const OSSL_PARAM *aes_mt_gettable_params(void *provctx)
331
0
{
332
0
  return cipher_get_param_table;
333
0
}
334
335
static const OSSL_PARAM *aes_mt_gettable_ctx_params(void *cctx, void *provctx)
336
0
{
337
0
  return ctx_get_param_table;
338
0
}
339
340
static int aes_mt_get_ctx_params(void *vctx, OSSL_PARAM params[])
341
0
{
342
  /* vctx is the EVP_CIPHER_CTX from aes_mt_newctx_*(). */
343
0
  EVP_CIPHER_CTX *evp_ctx = vctx;
344
0
  OSSL_PARAM *p;
345
0
  int ok = 1;
346
347
0
  for (p = params; p->key != NULL; p++)
348
0
    if (strcasecmp(p->key, "keylen") == 0 &&
349
0
        provnum_set_size_t(p, EVP_CIPHER_CTX_key_length(evp_ctx)) < 0)
350
0
      ok = 0;
351
0
  return ok;
352
0
}
353
354
/* Parameters that libcrypto can send to this implementation */
355
static const OSSL_PARAM *aes_mt_settable_ctx_params(void *cctx, void *provctx)
356
0
{
357
0
    return cipher_set_param_table;
358
0
}
359
360
static int aes_mt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
361
0
{
362
  /* vctx is the EVP_CIPHER_CTX from aes_mt_newctx_*(). */
363
0
  EVP_CIPHER_CTX *evp_ctx = vctx;
364
0
  struct aes_mt_ctx_st *aes_mt_ctx;
365
0
  const OSSL_PARAM *p;
366
0
  int ok = 1;
367
368
  /* Each AES-CTR-MT variant has one fixed key length. */
369
0
  for (p = params; p->key != NULL; p++)
370
0
    if (strcasecmp(p->key, "keylen") == 0) {
371
0
      size_t keyl = 0;
372
373
0
      if (provnum_get_size_t(&keyl, p) < 0 ||
374
0
          keyl != (size_t)EVP_CIPHER_CTX_key_length(evp_ctx)) {
375
0
        aes_mt_ctx = EVP_CIPHER_CTX_get_app_data(evp_ctx);
376
0
        if (aes_mt_ctx != NULL)
377
          ERR_raise(ERR_HANDLE(aes_mt_ctx),
378
0
              AES_MT_BAD_KEYLEN);
379
0
        ok = 0;
380
0
      }
381
0
    }
382
0
  return ok;
383
0
}
384
385
#endif /*WITH_OPENSSL3*/