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 #define CHACHA20_KEYLEN (CHACHA_KEY_SIZE) 18 #define CHACHA20_BLKLEN (1) 19 #define CHACHA20_IVLEN (CHACHA_CTR_SIZE) 20 #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 { 36 ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8, 37 CHACHA20_BLKLEN * 8, 38 CHACHA20_IVLEN * 8, 39 0, CHACHA20_FLAGS, 40 ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8), 41 NULL); 42 } 43 44 static void *chacha20_newctx(void *provctx) 45 { 46 PROV_CHACHA20_CTX *ctx; 47 48 if (!ossl_prov_is_running()) 49 return NULL; 50 51 ctx = OPENSSL_zalloc(sizeof(*ctx)); 52 if (ctx != NULL) 53 ossl_chacha20_initctx(ctx); 54 return ctx; 55 } 56 57 static void chacha20_freectx(void *vctx) 58 { 59 PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx; 60 61 if (ctx != NULL) { 62 ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); 63 OPENSSL_clear_free(ctx, sizeof(*ctx)); 64 } 65 } 66 67 static int chacha20_get_params(OSSL_PARAM params[]) 68 { 69 return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS, 70 CHACHA20_KEYLEN * 8, 71 CHACHA20_BLKLEN * 8, 72 CHACHA20_IVLEN * 8); 73 } 74 75 static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[]) 76 { 77 OSSL_PARAM *p; 78 79 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN); 80 if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) { 81 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 82 return 0; 83 } 84 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); 85 if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) { 86 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); 87 return 0; 88 } 89 90 return 1; 91 } 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 { 101 return chacha20_known_gettable_ctx_params; 102 } 103 104 static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[]) 105 { 106 const OSSL_PARAM *p; 107 size_t len; 108 109 if (params == NULL) 110 return 1; 111 112 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); 113 if (p != NULL) { 114 if (!OSSL_PARAM_get_size_t(p, &len)) { 115 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 116 return 0; 117 } 118 if (len != CHACHA20_KEYLEN) { 119 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); 120 return 0; 121 } 122 } 123 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); 124 if (p != NULL) { 125 if (!OSSL_PARAM_get_size_t(p, &len)) { 126 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); 127 return 0; 128 } 129 if (len != CHACHA20_IVLEN) { 130 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); 131 return 0; 132 } 133 } 134 return 1; 135 } 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 { 145 return chacha20_known_settable_ctx_params; 146 } 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 { 152 int ret; 153 154 /* The generic function checks for ossl_prov_is_running() */ 155 ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); 156 if (ret && iv != NULL) { 157 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; 158 PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw; 159 160 hw->initiv(ctx); 161 } 162 if (ret && !chacha20_set_ctx_params(vctx, params)) 163 ret = 0; 164 return ret; 165 } 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 { 171 int ret; 172 173 /* The generic function checks for ossl_prov_is_running() */ 174 ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); 175 if (ret && iv != NULL) { 176 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; 177 PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw; 178 179 hw->initiv(ctx); 180 } 181 if (ret && !chacha20_set_ctx_params(vctx, params)) 182 ret = 0; 183 return ret; 184 } 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 { 0, NULL } 204 }; 205 206