OpenSSL3.0 learning 12 provider cipher CSDN creation punch in

Posted by muralimohan001 on Sat, 05 Feb 2022 22:06:19 +0100

📒 Blog home page: Actor's blog
🎉 Welcome to pay attention 🔎 give the thumbs-up 👍 Collection ⭐ Leave a message 📝
❤️ Look forward to communicating together!
🙏 The author's level is very limited. If you find an error, please let me know. Thank you!
🌺 If you have any questions, you can communicate by private letter!!!

🌵 outline

 #include <openssl/core_dispatch.h>
 #include <openssl/core_names.h>
 
 /*
  * None of these are actual functions, but are displayed like this for
  * the function signatures for functions that are offered as function
  * pointers in OSSL_DISPATCH arrays.
  */
 
 /* Context management */
 void *OSSL_FUNC_cipher_newctx(void *provctx);
 void OSSL_FUNC_cipher_freectx(void *cctx);
 void *OSSL_FUNC_cipher_dupctx(void *cctx);
 
 /* Encryption/decryption */
 int OSSL_FUNC_cipher_encrypt_init(void *cctx, const unsigned char *key,
                                   size_t keylen, const unsigned char *iv,
                                   size_t ivlen, const OSSL_PARAM params[]);
 int OSSL_FUNC_cipher_decrypt_init(void *cctx, const unsigned char *key,
                                   size_t keylen, const unsigned char *iv,
                                   size_t ivlen, const OSSL_PARAM params[]);
 int OSSL_FUNC_cipher_update(void *cctx, unsigned char *out, size_t *outl,
                             size_t outsize, const unsigned char *in, size_t inl);
 int OSSL_FUNC_cipher_final(void *cctx, unsigned char *out, size_t *outl,
                            size_t outsize);
 int OSSL_FUNC_cipher_cipher(void *cctx, unsigned char *out, size_t *outl,
                             size_t outsize, const unsigned char *in, size_t inl);
 
 /* Cipher parameter descriptors */
 const OSSL_PARAM *OSSL_FUNC_cipher_gettable_params(void *provctx);
 
 /* Cipher operation parameter descriptors */
 const OSSL_PARAM *OSSL_FUNC_cipher_gettable_ctx_params(void *cctx,
                                                        void *provctx);
 const OSSL_PARAM *OSSL_FUNC_cipher_settable_ctx_params(void *cctx,
                                                        void *provctx);
 
 /* Cipher parameters */
 int OSSL_FUNC_cipher_get_params(OSSL_PARAM params[]);
 
 /* Cipher operation parameters */
 int OSSL_FUNC_cipher_get_ctx_params(void *cctx, OSSL_PARAM params[]);
 int OSSL_FUNC_cipher_set_ctx_params(void *cctx, const OSSL_PARAM params[]);

🎄 describe

CIPHER operation enables the provider to implement cryptographic algorithm and EVP through API function_ EncryptInit_ ex,EVP_EncryptUpdate and EVP_EncryptFinal (and decryption equivalents and other related functions) provides it to the application.

All the "functions" mentioned here are used as function pointers in libcrypto and ossl_ It is passed between providers in the dispatch array through the * * provider provided by the provider_ query_ Ossl returned by operation() * * function_ Algorithm array.

All of these "functions" have a name called OSSL_FUNC_{name}_fn, and a helper function, which is used to_ FUNC_ Ossl for {name}_ Retrieves a function pointer from the dispatch element. For example, "function" OSSL_FUNC_cipher_newctx() has the following:

 typedef void *(OSSL_OSSL_FUNC_cipher_newctx_fn)(void *provctx);
 static ossl_inline OSSL_OSSL_FUNC_cipher_newctx_fn
     OSSL_FUNC_cipher_newctx(const OSSL_DISPATCH *opf);

OSSL_ The dispatch array is created by OpenSSL core_ dispatch. The numbers provided as macros in H are indexed as follows:

 OSSL_FUNC_cipher_newctx               OSSL_FUNC_CIPHER_NEWCTX
 OSSL_FUNC_cipher_freectx              OSSL_FUNC_CIPHER_FREECTX
 OSSL_FUNC_cipher_dupctx               OSSL_FUNC_CIPHER_DUPCTX

 OSSL_FUNC_cipher_encrypt_init         OSSL_FUNC_CIPHER_ENCRYPT_INIT
 OSSL_FUNC_cipher_decrypt_init         OSSL_FUNC_CIPHER_DECRYPT_INIT
 OSSL_FUNC_cipher_update               OSSL_FUNC_CIPHER_UPDATE
 OSSL_FUNC_cipher_final                OSSL_FUNC_CIPHER_FINAL
 OSSL_FUNC_cipher_cipher               OSSL_FUNC_CIPHER_CIPHER

 OSSL_FUNC_cipher_get_params           OSSL_FUNC_CIPHER_GET_PARAMS
 OSSL_FUNC_cipher_get_ctx_params       OSSL_FUNC_CIPHER_GET_CTX_PARAMS
 OSSL_FUNC_cipher_set_ctx_params       OSSL_FUNC_CIPHER_SET_CTX_PARAMS

 OSSL_FUNC_cipher_gettable_params      OSSL_FUNC_CIPHER_GETTABLE_PARAMS
 OSSL_FUNC_cipher_gettable_ctx_params  OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
 OSSL_FUNC_cipher_settable_ctx_params  OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS

Cryptographic algorithms may not be able to achieve all these functions. In order to be a consistent set of functions, there must be at least a complete set of "encryption" functions, or a complete set of "decryption" functions, or a "password" function. In all cases, OSSL_FUNC_cipher_newctx and OSSL_FUNC_cipher_freectx functions must exist. All other functions are optional.

🌲 Context management function

OSSL_FUNC_cipher_newctx() should create and return a pointer to the provider side structure to hold context information during password operations. A pointer to this context will be passed back in many other cryptographic operation function calls. The parameter provctx is the provider context generated during provider initialization.

OSSL_FUNC_cipher_freectx() passes a pointer to the provider side password context in the cctx parameter. This function should free any resources associated with the context.

OSSL_FUNC_cipher_dupctx() should copy the provider side password context in the cctx parameter and return a duplicate copy.

🌳 Encryption / decryption function

OSSL_FUNC_cipher_encrypt_init() initializes the password operation for encryption, giving the newly created provider side password context in the cctx parameter. The key to be used is given in the form of a key, and the length of the key is keylen bytes. The IV to be used is given in IV, ivlen bytes long. Parameter (if not NULL), it should be used in a similar way to using OSSL_FUNC_cipher_set_ctx_params() is set in the context.

OSSL_FUNC_cipher_decrypt_init() and OSSL_FUNC_cipher_encrypt_init () is the same, except that it initializes the context of the decryption operation.

Call OSSL_FUNC_cipher_update() to provide the data to be encrypted / decrypted as part of the previously initialized password operation. The cctx parameter contains a pointer to the previously initialized provider side context. OSSL_FUNC_cipher_update() should encrypt / decrypt the integer byte data of in at the position pointed by in. The encrypted data shall be stored in out, and the amount of data written to * out shall not exceed super large bytes. For a single password operation, you can call ossl multiple times_ FUNC_ cipher_ update(). The password implementation is responsible for processing the input length that is not a multiple of the block length. In this case, the cryptographic implementation usually caches part of the input data block until the complete block is obtained. Out may be in the same position as in, but it should not overlap partially. And EVP_EncryptUpdate and EVP_ The oversized decryptupdate record is the same as expected.

OSSL_FUNC_cipher_final() completes and passes the previous OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init() starts the encryption or decryption of and OSS L_ FUNC_ cipher_ Update() call. The cctx parameter contains a pointer to the provider side context. Any final encryption / decryption output shall be written to the output and the amount of data shall be written to * outl, which shall not exceed super large bytes. And EVP_EncryptFinal and EVP_DecryptFinal records the same expectations for oversizes.

OSSL_FUNC_cipher_cipher() uses the provider side password context in the cctx parameter to perform encryption / decryption, which should be preceded by calling OSSL_FUNC_cipher_encrypt_init() or OSSL_FUNC_cipher_decrypt_init() to initialize. This should call the original underlying cryptographic function without any padding. This invokes EVP as an application in the provider_ The result of cipher is called. The application is responsible for ensuring that the input is a multiple of the block length. The data to be encrypted / decrypted will be in and 1 byte long. The output of encryption / decryption shall be stored in out, and the amount of data stored shall be put into * out, which shall not exceed super large bytes.

🌴 Password parameters

For more details on the parameter structures used by these functions, see OSSL_PARAM.

OSSL_FUNC_cipher_get_params () gets the details of the algorithm implementation and stores them in params.

OSSL_FUNC_cipher_set_ctx_params() sets the password operation parameter of the provider side password context cctx to params. Any parameter setting is an additional setting to any previously set parameter setting. Passing NULL for params should return true.

OSSL_FUNC_cipher_get_ctx_params () gets the password operation details from the given provider side password context cctx and stores them in params. Passing NULL for params should return true.

OSSL_FUNC_cipher_gettable_params(),OSSL_FUNC_cipher_gettable_ctx_params() and OSSL_FUNC_cipher_settable_ctx_params() returns constant OSSL_PARAM array as OSSL_FUNC_cipher_get_params(),OSSL_FUNC_cipher_get_ctx_params() and OSSL_FUNC_cipher_set_ctx_params() can handle the descriptor of the parameter. OSSL_FUNC_cipher_gettable_ctx_params() and OSSL_FUNC_cipher_settable_ctx_params () will return the parameter associated with the provider side context cctx (if it is not NULL), then it is the current state. Otherwise, they will return the parameters associated with the provider-side algorithm provctx.

The parameters currently recognized by the built-in password are listed in EVP_EncryptInit in parameters. Not all parameters are related to or understood by all passwords.

🌱 Return value

OSSL_FUNC_cipher_newctx() and OSSL_FUNC_cipher_dupctx() should return the newly created provider side password context or NULL on failure.

OSSL_FUNC_cipher_encrypt_init(),OSSL_FUNC_cipher_decrypt_init(),OSSL_FUNC_cipher_update(),OSSL_FUNC_cipher_final(),OSSL_FUNC_cipher_cipher(),OSSL_FUNC_cipher_get_params(),OSSL_ FUNC_ cipher_ get_ ctx_ And ossl (params)_ FUNC_ cipher_ set_ ctx_ Params() should return 1 for success or 0 for errors.

OSSL_FUNC_cipher_gettable_params(),OSSL_FUNC_cipher_gettable_ctx_params() and OSSL_FUNC_cipher_settable_ctx_params() should return constant OSSL_PARAM array, NULL if not provided.

Topics: C OpenSSL security cryptology