/*************************************************************************
* Copyright (C) 2020 Intel Corporation
*
* Licensed under the Apache License,  Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 	http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law  or agreed  to  in  writing,  software
* distributed under  the License  is  distributed  on  an  "AS IS"  BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the  specific  language  governing  permissions  and
* limitations under the License.
*************************************************************************/

#include <crypto_mb/status.h>
#include <crypto_mb/sm3.h>

#include <internal/sm3/sm3_mb16.h>
#include <internal/common/ifma_defs.h>

// Disable optimization for VS17
#if defined(_MSC_VER) && (_MSC_VER < 1920) && !defined(__INTEL_COMPILER)
#pragma optimize("", off)
#endif

DLL_PUBLIC
mbx_status16 OWNAPI(mbx_sm3_update_mb16)(const int8u* const msg_pa[16],
                                         int len[16],
                                         SM3_CTX_mb16* p_state)
{
    mbx_status16 status = 0;

    /* test input pointers */
    if (NULL == msg_pa || NULL == len || NULL == p_state) {
        status = MBX_SET_STS16_ALL(MBX_STATUS_NULL_PARAM_ERR);
        return status;
    }

    for (int buf_no = 0; buf_no < SM3_NUM_BUFFERS; buf_no++) {
        /* Check the negative input length */
        if (len[buf_no] < 0) {
            status = MBX_SET_STS16(status, buf_no, MBX_STATUS_MISMATCH_PARAM_ERR);
            return status;
        }
        /* Check the positive input length and NULL input message */
        if (len[buf_no] && !msg_pa[buf_no]) {
            status = MBX_SET_STS16(status, buf_no, MBX_STATUS_NULL_PARAM_ERR);
            return status;
        }
    }

#if (_MBX >= _MBX_K1)
    status |= internal_avx512_sm3_update_mb16(msg_pa, len, p_state);
#else
    status = MBX_SET_STS16_ALL(MBX_STATUS_UNSUPPORTED_ISA_ERR);
#endif /* #if (_MBX>=_MBX_K1) */
    return status;
}
