int GCMContext::starts(State & state, mbedtls_gcm_context * context){ Stack * stack = state.stack; if (stack->is<LUA_TNUMBER>(1) && stack->is<LUA_TSTRING>(2)){ std::string ivStr = stack->toLString(2); size_t ivLength = ivStr.length(); unsigned char * additionalData = nullptr; size_t addLength = 0; if (stack->is<LUA_TSTRING>(3)){ std::string additional = stack->toLString(3); addLength = additional.length(); additionalData = new unsigned char[addLength]; memcpy(additionalData, additional.c_str(), addLength); } int mode = stack->to<int>(1); int result = mbedtls_gcm_starts(context, mode, reinterpret_cast<const unsigned char*>(ivStr.c_str()), ivLength, additionalData, addLength); if (additionalData){ delete[] additionalData; } stack->push<int>(result); return 1; } return 0; }
int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, const unsigned char *ad, size_t ad_len ) { if( NULL == ctx || NULL == ctx->cipher_info ) return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) { return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation, ctx->iv, ctx->iv_size, ad, ad_len ); } return( 0 ); }
int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, const unsigned char *ad, size_t ad_len ) { if( NULL == ctx || NULL == ctx->cipher_info ) return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); #if defined(MBEDTLS_GCM_C) if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) { return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation, ctx->iv, ctx->iv_size, ad, ad_len ); } #endif #if defined(MBEDTLS_CHACHAPOLY_C) if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) { int result; mbedtls_chachapoly_mode_t mode; mode = ( ctx->operation == MBEDTLS_ENCRYPT ) ? MBEDTLS_CHACHAPOLY_ENCRYPT : MBEDTLS_CHACHAPOLY_DECRYPT; result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx, ctx->iv, mode ); if ( result != 0 ) return( result ); return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx, ad, ad_len ); } #endif return( 0 ); }