FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/evp.c
/* This function allows cipher specific parameters to be
    determined and set. */
This function allows cipher specific parameters to be
determined and set.
This function allows cipher specific parameters to be determined and set .
int wolfSSL_EVP_CIPHER_CTX_ctrl(WOLFSSL_EVP_CIPHER_CTX *ctx, int type, \
                                    int arg, void *ptr)
    {
        int ret = WOLFSSL_FAILURE;
#if defined(HAVE_AESGCM) && !defined(HAVE_SELFTEST) && !defined(WC_NO_RNG)
        WC_RNG rng;
#endif
        if (ctx == NULL)
            return WOLFSSL_FAILURE;
        (void)arg;
        (void)ptr;
        WOLFSSL_ENTER("EVP_CIPHER_CTX_ctrl");
        switch(type) {
            case EVP_CTRL_INIT:
                wolfSSL_EVP_CIPHER_CTX_init(ctx);
                if(ctx)
                    ret = WOLFSSL_SUCCESS;
                break;
            case EVP_CTRL_SET_KEY_LENGTH:
                ret = wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, arg);
                break;
#if defined(HAVE_AESGCM) && !defined(HAVE_SELFTEST) && !defined(WC_NO_RNG)
            case EVP_CTRL_GCM_SET_IVLEN:
                if(arg <= 0 || arg > 16)
                    return WOLFSSL_FAILURE;
                ret = wolfSSL_EVP_CIPHER_CTX_set_iv_length(ctx, arg);
                break;
            case EVP_CTRL_AEAD_SET_IV_FIXED:
                if (arg == -1) {
                    ret = wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, (byte*)ptr, ctx->ivSz);
                }
                else {
                    if ((arg < 4) || (ctx->ivSz - arg) < 8) {
                        WOLFSSL_MSG("Fixed field or invocation field too short");
                        ret = WOLFSSL_FAILURE;
                        break;
                    }
                    if (wc_InitRng(&rng) != 0) {
                        WOLFSSL_MSG("wc_InitRng failed");
                        ret = WOLFSSL_FAILURE;
                        break;
                    }
                    if (arg) {
                        XMEMCPY(ctx->iv, ptr, arg);
                    }
                    if (wc_RNG_GenerateBlock(&rng, ctx->iv   + arg,
                                                   ctx->ivSz - arg) != 0) {
                        WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
                        ret = WOLFSSL_FAILURE;
                    }
                    if (wc_FreeRng(&rng) != 0) {
                        WOLFSSL_MSG("wc_FreeRng failed");
                        ret = WOLFSSL_FAILURE;
                        break;
                    }
                }
                break;
#if !defined(_WIN32) && !defined(HAVE_FIPS)
            case EVP_CTRL_GCM_IV_GEN:
                if (ctx->cipher.aes.keylen == 0 || ctx->ivSz == 0) {
                    ret = WOLFSSL_FAILURE;
                    WOLFSSL_MSG("Key or IV not set");
                    break;
                }
                if ((ret = wc_AesGcmSetExtIV(&ctx->cipher.aes, ctx->iv, ctx->ivSz)) != 0) {
                    WOLFSSL_MSG("wc_AesGcmSetIV failed");
                    ret = WOLFSSL_FAILURE;
                }
                IncCtr(ctx->iv, ctx->ivSz);
                break;
#endif
            case EVP_CTRL_AEAD_SET_TAG:
                if(arg <= 0 || arg > 16 || (ptr == NULL))
                    return WOLFSSL_FAILURE;
                XMEMCPY(ctx->authTag, ptr, arg);
                ctx->authTagSz = arg;
                ret = WOLFSSL_SUCCESS;
                break;
            case EVP_CTRL_AEAD_GET_TAG:
                if(arg <= 0 || arg > 16)
                    return WOLFSSL_FAILURE;
                XMEMCPY(ptr, ctx->authTag, arg);
                ret = WOLFSSL_SUCCESS;
                break;
#endif 
            default:
                WOLFSSL_MSG("EVP_CIPHER_CTX_ctrl operation not yet handled");
                ret = WOLFSSL_FAILURE;
        }
        return ret;
    }
int wolfSSL_EVP_CIPHER_CTX_ctrl ( WOLFSSL_EVP_CIPHER_CTX * ctx , int type , int arg , void * ptr ) { int ret = WOLFSSL_FAILURE ; #if defined ( HAVE_AESGCM ) && ! defined ( HAVE_SELFTEST ) && ! defined ( WC_NO_RNG ) WC_RNG rng ; #endif if ( ctx == NULL ) return WOLFSSL_FAILURE ; ( void ) arg ; ( void ) ptr ; WOLFSSL_ENTER ( " " ) ; switch ( type ) { case EVP_CTRL_INIT : wolfSSL_EVP_CIPHER_CTX_init ( ctx ) ; if ( ctx ) ret = WOLFSSL_SUCCESS ; break ; case EVP_CTRL_SET_KEY_LENGTH : ret = wolfSSL_EVP_CIPHER_CTX_set_key_length ( ctx , arg ) ; break ; #if defined ( HAVE_AESGCM ) && ! defined ( HAVE_SELFTEST ) && ! defined ( WC_NO_RNG ) case EVP_CTRL_GCM_SET_IVLEN : if ( arg <= 0 || arg > 16 ) return WOLFSSL_FAILURE ; ret = wolfSSL_EVP_CIPHER_CTX_set_iv_length ( ctx , arg ) ; break ; case EVP_CTRL_AEAD_SET_IV_FIXED : if ( arg == -1 ) { ret = wolfSSL_EVP_CIPHER_CTX_set_iv ( ctx , ( byte * ) ptr , ctx -> ivSz ) ; } else { if ( ( arg < 4 ) || ( ctx -> ivSz - arg ) < 8 ) { WOLFSSL_MSG ( " " ) ; ret = WOLFSSL_FAILURE ; break ; } if ( wc_InitRng ( & rng ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = WOLFSSL_FAILURE ; break ; } if ( arg ) { XMEMCPY ( ctx -> iv , ptr , arg ) ; } if ( wc_RNG_GenerateBlock ( & rng , ctx -> iv + arg , ctx -> ivSz - arg ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = WOLFSSL_FAILURE ; } if ( wc_FreeRng ( & rng ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = WOLFSSL_FAILURE ; break ; } } break ; #if ! defined ( _WIN32 ) && ! defined ( HAVE_FIPS ) case EVP_CTRL_GCM_IV_GEN : if ( ctx -> cipher . aes . keylen == 0 || ctx -> ivSz == 0 ) { ret = WOLFSSL_FAILURE ; WOLFSSL_MSG ( " " ) ; break ; } if ( ( ret = wc_AesGcmSetExtIV ( & ctx -> cipher . aes , ctx -> iv , ctx -> ivSz ) ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = WOLFSSL_FAILURE ; } IncCtr ( ctx -> iv , ctx -> ivSz ) ; break ; #endif case EVP_CTRL_AEAD_SET_TAG : if ( arg <= 0 || arg > 16 || ( ptr == NULL ) ) return WOLFSSL_FAILURE ; XMEMCPY ( ctx -> authTag , ptr , arg ) ; ctx -> authTagSz = arg ; ret = WOLFSSL_SUCCESS ; break ; case EVP_CTRL_AEAD_GET_TAG : if ( arg <= 0 || arg > 16 ) return WOLFSSL_FAILURE ; XMEMCPY ( ptr , ctx -> authTag , arg ) ; ret = WOLFSSL_SUCCESS ; break ; #endif default : WOLFSSL_MSG ( " " ) ; ret = WOLFSSL_FAILURE ; } return ret ; }
This function allows cipher specific parameters to be
determined and set.
This function allows cipher specific parameters to be determined and set .
/* arg == -1 copies ctx->ivSz from ptr */ /*
                     * Fixed field must be at least 4 bytes and invocation
                     * field at least 8.
                     */ /* rng is freed immediately after if block so no need
                         * to do it here
                         */ /* OpenSSL increments the IV. Not sure why */ /* HAVE_AESGCM && !HAVE_SELFTEST && !WC_NO_RNG */