]>
Commit | Line | Data |
---|---|---|
4a42e264 SL |
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/params.h> | |
11 | #include <openssl/core_numbers.h> | |
12 | #include <openssl/core_names.h> | |
13 | #include <openssl/evp.h> | |
14 | #include "internal/cryptlib.h" | |
15 | #include "internal/modes_int.h" | |
16 | #include "internal/ciphermode_platform.h" | |
17 | ||
18 | #define MAXCHUNK ((size_t)1 << (sizeof(long) * 8 - 2)) | |
19 | #define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4)) | |
20 | ||
21 | #define GENERIC_BLOCK_SIZE 16 | |
22 | #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */ | |
23 | #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */ | |
24 | #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */ | |
25 | #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */ | |
26 | ||
27 | #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args | |
28 | ||
29 | typedef struct prov_cipher_hw_st PROV_CIPHER_HW; | |
30 | typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX; | |
31 | ||
32 | typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out, | |
33 | const unsigned char *in, size_t len); | |
34 | ||
35 | struct prov_cipher_ctx_st { | |
36 | block128_f block; | |
37 | union { | |
38 | cbc128_f cbc; | |
39 | ctr128_f ctr; | |
40 | } stream; | |
41 | ||
42 | /* | |
43 | * num contains the number of bytes of |iv| which are valid for modes that | |
44 | * manage partial blocks themselves. | |
45 | */ | |
46 | size_t num; | |
47 | ||
48 | int mode; | |
49 | int enc; /* Set to 1 for encrypt, or 0 otherwise */ | |
50 | size_t bufsz; /* Number of bytes in buf */ | |
51 | size_t keylen; /* key size (in bytes) */ | |
52 | size_t ivlen; | |
53 | size_t blocksize; | |
54 | uint64_t flags; | |
55 | unsigned int pad : 1; /* Whether padding should be used or not */ | |
56 | ||
57 | /* Buffer of partial blocks processed via update calls */ | |
58 | unsigned char buf[GENERIC_BLOCK_SIZE]; | |
59 | unsigned char iv[GENERIC_BLOCK_SIZE]; | |
60 | const PROV_CIPHER_HW *hw; /* hardware specific functions */ | |
61 | const void *ks; /* Pointer to algorithm specific key data */ | |
62 | OPENSSL_CTX *libctx; | |
63 | }; | |
64 | ||
65 | struct prov_cipher_hw_st { | |
66 | int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen); | |
67 | PROV_CIPHER_HW_FN *cipher; | |
68 | }; | |
69 | ||
70 | OSSL_OP_cipher_encrypt_init_fn cipher_generic_einit; | |
71 | OSSL_OP_cipher_decrypt_init_fn cipher_generic_dinit; | |
72 | OSSL_OP_cipher_update_fn cipher_generic_block_update; | |
73 | OSSL_OP_cipher_final_fn cipher_generic_block_final; | |
74 | OSSL_OP_cipher_update_fn cipher_generic_stream_update; | |
75 | OSSL_OP_cipher_final_fn cipher_generic_stream_final; | |
76 | OSSL_OP_cipher_cipher_fn cipher_generic_cipher; | |
77 | OSSL_OP_cipher_get_ctx_params_fn cipher_generic_get_ctx_params; | |
78 | OSSL_OP_cipher_set_ctx_params_fn cipher_generic_set_ctx_params; | |
79 | OSSL_OP_cipher_gettable_params_fn cipher_generic_gettable_params; | |
80 | OSSL_OP_cipher_gettable_ctx_params_fn cipher_generic_gettable_ctx_params; | |
81 | OSSL_OP_cipher_settable_ctx_params_fn cipher_generic_settable_ctx_params; | |
82 | OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params; | |
83 | OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params; | |
84 | int cipher_generic_get_params(OSSL_PARAM params[], int md, unsigned long flags, | |
85 | int kbits, int blkbits, int ivbits); | |
86 | void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits, | |
87 | size_t ivbits, int mode, | |
88 | const PROV_CIPHER_HW *hw, void *provctx); | |
89 | ||
90 | #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ | |
91 | blkbits, ivbits, typ) \ | |
92 | static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \ | |
93 | static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ | |
94 | { \ | |
95 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ | |
96 | kbits, blkbits, ivbits); \ | |
97 | } \ | |
98 | static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \ | |
99 | static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \ | |
100 | { \ | |
101 | PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \ | |
102 | if (ctx != NULL) { \ | |
103 | cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \ | |
104 | EVP_CIPH_##UCMODE##_MODE, \ | |
105 | PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \ | |
106 | } \ | |
107 | return ctx; \ | |
108 | } \ | |
109 | const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \ | |
110 | { OSSL_FUNC_CIPHER_NEWCTX, \ | |
111 | (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ | |
112 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ | |
113 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ | |
114 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \ | |
115 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \ | |
116 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\ | |
117 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \ | |
118 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ | |
119 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ | |
120 | (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \ | |
121 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ | |
122 | (void (*)(void))cipher_generic_get_ctx_params }, \ | |
123 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | |
124 | (void (*)(void))cipher_generic_set_ctx_params }, \ | |
125 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ | |
126 | (void (*)(void))cipher_generic_gettable_params }, \ | |
127 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | |
128 | (void (*)(void))cipher_generic_gettable_ctx_params }, \ | |
129 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | |
130 | (void (*)(void))cipher_generic_settable_ctx_params }, \ | |
131 | { 0, NULL } \ | |
132 | }; | |
133 | ||
134 | PROV_CIPHER_HW_FN cipher_hw_generic_cbc; | |
135 | PROV_CIPHER_HW_FN cipher_hw_generic_ecb; | |
136 | PROV_CIPHER_HW_FN cipher_hw_generic_ofb128; | |
137 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb128; | |
138 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb8; | |
139 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb1; | |
140 | PROV_CIPHER_HW_FN cipher_hw_generic_ctr; | |
141 | PROV_CIPHER_HW_FN cipher_hw_chunked_cbc; | |
142 | PROV_CIPHER_HW_FN cipher_hw_chunked_cfb8; | |
143 | PROV_CIPHER_HW_FN cipher_hw_chunked_cfb128; | |
144 | PROV_CIPHER_HW_FN cipher_hw_chunked_ofb128; | |
145 | #define cipher_hw_chunked_ecb cipher_hw_generic_ecb | |
146 | #define cipher_hw_chunked_ctr cipher_hw_generic_ctr | |
147 | #define cipher_hw_chunked_cfb1 cipher_hw_generic_cfb1 | |
148 | ||
149 |