1 /* 2 * Copyright 2019-2023 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_poly1305 cipher */ 11 12 #include <openssl/proverr.h> 13 #include "cipher_chacha20_poly1305.h" 14 #include "prov/implementations.h" 15 #include "prov/providercommon.h" 16 17 #define CHACHA20_POLY1305_KEYLEN CHACHA_KEY_SIZE 18 #define CHACHA20_POLY1305_BLKLEN 1 19 #define CHACHA20_POLY1305_MAX_IVLEN 12 20 #define CHACHA20_POLY1305_MODE 0 21 #define CHACHA20_POLY1305_FLAGS (PROV_CIPHER_FLAG_AEAD \ 22 | PROV_CIPHER_FLAG_CUSTOM_IV) 23 24 static OSSL_FUNC_cipher_newctx_fn chacha20_poly1305_newctx; 25 static OSSL_FUNC_cipher_freectx_fn chacha20_poly1305_freectx; 26 static OSSL_FUNC_cipher_encrypt_init_fn chacha20_poly1305_einit; 27 static OSSL_FUNC_cipher_decrypt_init_fn chacha20_poly1305_dinit; 28 static OSSL_FUNC_cipher_get_params_fn chacha20_poly1305_get_params; 29 static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_poly1305_get_ctx_params; 30 static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_poly1305_set_ctx_params; 31 static OSSL_FUNC_cipher_cipher_fn chacha20_poly1305_cipher; 32 static OSSL_FUNC_cipher_final_fn chacha20_poly1305_final; 33 static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_poly1305_gettable_ctx_params; 34 #define chacha20_poly1305_settable_ctx_params ossl_cipher_aead_settable_ctx_params 35 #define chacha20_poly1305_gettable_params ossl_cipher_generic_gettable_params 36 #define chacha20_poly1305_update chacha20_poly1305_cipher 37 38 static void *chacha20_poly1305_newctx(void *provctx) 39 { 40 PROV_CHACHA20_POLY1305_CTX *ctx; 41 42 if (!ossl_prov_is_running()) 43 return NULL; 44 45 ctx = OPENSSL_zalloc(sizeof(*ctx)); 46 if (ctx != NULL) { 47 ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8, 48 CHACHA20_POLY1305_BLKLEN * 8, 49 CHACHA20_POLY1305_IVLEN * 8, 50 CHACHA20_POLY1305_MODE, 51 CHACHA20_POLY1305_FLAGS, 52 ossl_prov_cipher_hw_chacha20_poly1305( 53 CHACHA20_POLY1305_KEYLEN * 8), 54 NULL); 55 ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH; 56 ossl_chacha20_initctx(&ctx->chacha); 57 } 58 return ctx; 59 } 60 61 static void chacha20_poly1305_freectx(void *vctx) 62 { 63 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; 64 65 if (ctx != NULL) { 66 ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); 67 OPENSSL_clear_free(ctx, sizeof(*ctx)); 68 } 69 } 70 71 static int chacha20_poly1305_get_params(OSSL_PARAM params[]) 72 { 73 return ossl_cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS, 74 CHACHA20_POLY1305_KEYLEN * 8, 75 CHACHA20_POLY1305_BLKLEN * 8, 76 CHACHA20_POLY1305_IVLEN * 8); 77 } 78 79 static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[]) 80 { 81 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; 82 OSSL_PARAM *p; 83 84 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN); 85 if (p != NULL) { 86 if (!OSSL_PARAM_set_size_t(p, CHACHA20_POLY1305_IVLEN)) { 87 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 88 return 0; 89 } 90 } 91 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); 92 if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_POLY1305_KEYLEN)) { 93 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 94 return 0; 95 } 96 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN); 97 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tag_len)) { 98 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 99 return 0; 100 } 101 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD); 102 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) { 103 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 104 return 0; 105 } 106 107 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG); 108 if (p != NULL) { 109 if (p->data_type != OSSL_PARAM_OCTET_STRING) { 110 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 111 return 0; 112 } 113 if (!ctx->base.enc) { 114 ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET); 115 return 0; 116 } 117 if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) { 118 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); 119 return 0; 120 } 121 memcpy(p->data, ctx->tag, p->data_size); 122 } 123 124 return 1; 125 } 126 127 static const OSSL_PARAM chacha20_poly1305_known_gettable_ctx_params[] = { 128 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), 129 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), 130 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL), 131 OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), 132 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), 133 OSSL_PARAM_END 134 }; 135 static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params 136 (ossl_unused void *cctx, ossl_unused void *provctx) 137 { 138 return chacha20_poly1305_known_gettable_ctx_params; 139 } 140 141 static int chacha20_poly1305_set_ctx_params(void *vctx, 142 const OSSL_PARAM params[]) 143 { 144 const OSSL_PARAM *p; 145 size_t len; 146 PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx; 147 PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = 148 (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; 149 150 if (params == NULL) 151 return 1; 152 153 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); 154 if (p != NULL) { 155 if (!OSSL_PARAM_get_size_t(p, &len)) { 156 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 157 return 0; 158 } 159 if (len != CHACHA20_POLY1305_KEYLEN) { 160 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); 161 return 0; 162 } 163 } 164 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); 165 if (p != NULL) { 166 if (!OSSL_PARAM_get_size_t(p, &len)) { 167 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 168 return 0; 169 } 170 if (len != CHACHA20_POLY1305_MAX_IVLEN) { 171 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); 172 return 0; 173 } 174 } 175 176 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG); 177 if (p != NULL) { 178 if (p->data_type != OSSL_PARAM_OCTET_STRING) { 179 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 180 return 0; 181 } 182 if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) { 183 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH); 184 return 0; 185 } 186 if (p->data != NULL) { 187 if (ctx->base.enc) { 188 ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED); 189 return 0; 190 } 191 memcpy(ctx->tag, p->data, p->data_size); 192 } 193 ctx->tag_len = p->data_size; 194 } 195 196 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); 197 if (p != NULL) { 198 if (p->data_type != OSSL_PARAM_OCTET_STRING) { 199 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 200 return 0; 201 } 202 len = hw->tls_init(&ctx->base, p->data, p->data_size); 203 if (len == 0) { 204 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); 205 return 0; 206 } 207 ctx->tls_aad_pad_sz = len; 208 } 209 210 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED); 211 if (p != NULL) { 212 if (p->data_type != OSSL_PARAM_OCTET_STRING) { 213 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 214 return 0; 215 } 216 if (hw->tls_iv_set_fixed(&ctx->base, p->data, p->data_size) == 0) { 217 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); 218 return 0; 219 } 220 } 221 /* ignore OSSL_CIPHER_PARAM_AEAD_MAC_KEY */ 222 return 1; 223 } 224 225 static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, 226 size_t keylen, const unsigned char *iv, 227 size_t ivlen, const OSSL_PARAM params[]) 228 { 229 int ret; 230 231 /* The generic function checks for ossl_prov_is_running() */ 232 ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); 233 if (ret && iv != NULL) { 234 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; 235 PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = 236 (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; 237 238 hw->initiv(ctx); 239 } 240 if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) 241 ret = 0; 242 return ret; 243 } 244 245 static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, 246 size_t keylen, const unsigned char *iv, 247 size_t ivlen, const OSSL_PARAM params[]) 248 { 249 int ret; 250 251 /* The generic function checks for ossl_prov_is_running() */ 252 ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); 253 if (ret && iv != NULL) { 254 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; 255 PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = 256 (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; 257 258 hw->initiv(ctx); 259 } 260 if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) 261 ret = 0; 262 return ret; 263 } 264 265 static int chacha20_poly1305_cipher(void *vctx, unsigned char *out, 266 size_t *outl, size_t outsize, 267 const unsigned char *in, size_t inl) 268 { 269 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; 270 PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = 271 (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; 272 273 if (!ossl_prov_is_running()) 274 return 0; 275 276 if (inl == 0) { 277 *outl = 0; 278 return 1; 279 } 280 281 if (outsize < inl) { 282 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); 283 return 0; 284 } 285 286 if (!hw->aead_cipher(ctx, out, outl, in, inl)) 287 return 0; 288 289 return 1; 290 } 291 292 static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl, 293 size_t outsize) 294 { 295 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; 296 PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = 297 (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw; 298 299 if (!ossl_prov_is_running()) 300 return 0; 301 302 if (hw->aead_cipher(ctx, out, outl, NULL, 0) <= 0) 303 return 0; 304 305 *outl = 0; 306 return 1; 307 } 308 309 /* ossl_chacha20_ossl_poly1305_functions */ 310 const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = { 311 { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx }, 312 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx }, 313 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit }, 314 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit }, 315 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update }, 316 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final }, 317 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher }, 318 { OSSL_FUNC_CIPHER_GET_PARAMS, 319 (void (*)(void))chacha20_poly1305_get_params }, 320 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, 321 (void (*)(void))chacha20_poly1305_gettable_params }, 322 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, 323 (void (*)(void))chacha20_poly1305_get_ctx_params }, 324 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, 325 (void (*)(void))chacha20_poly1305_gettable_ctx_params }, 326 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, 327 (void (*)(void))chacha20_poly1305_set_ctx_params }, 328 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, 329 (void (*)(void))chacha20_poly1305_settable_ctx_params }, 330 { 0, NULL } 331 }; 332 333