#ifndef MISC_DEFS_H_
#define MISC_DEFS_H_

#define _ROUND_MASK(a)           ((a) - 1)
#define ROUND_UP(x, a)           (((x) + _ROUND_MASK(a)) & ~_ROUND_MASK(a))
#define ROUND_DOWN(x, a)         ((x) & ~_ROUND_MASK(a))

#define CHECK_OSSL_MALLOC_FAILURE ((ERR_GET_REASON(ERR_peek_error())) == ERR_R_MALLOC_FAILURE)
#define PRINT_OSSL_ERROR() \
    do {\
        if (0 != ERR_peek_error()) {\
            MB_LOGE("Internal OPENSSL error: \n");\
            MB_LOGE("   OSSL ERR_GET_LIB - %u\n", ERR_GET_LIB(ERR_peek_error()));\
            MB_LOGE("   OSSL ERR_GET_FUNC - %u\n", ERR_GET_FUNC(ERR_peek_error()));\
            MB_LOGE("   OSSL ERR_GET_REASON - %u\n", ERR_GET_REASON(ERR_peek_error()));\
        }\
    } while(0)

#define PRINT_OSSL_ERROR_AND_PANIC(panic_result) \
    do {\
        PRINT_OSSL_ERROR(); \
        TEE_Panic(panic_result);\
    } while(0)

/* Prevents Insecure Compiler Optimization that removes the "memset" function
 * as part of "dead store removal" optimization
 * (when "memset" is the last function or so on). */
#define DO_NOT_OPTIMIZE(variable) *(volatile char *)variable = *(volatile char *)variable

#define RSA_PKCS1_OAEP_SHA224_PADDING	41
#define RSA_PKCS1_OAEP_SHA256_PADDING	42
#define RSA_PKCS1_OAEP_SHA384_PADDING	43
#define RSA_PKCS1_OAEP_SHA512_PADDING	44

#define MAX_KEY_LEN	(64)
#define MAX_IV_LEN	(32)

#define MAX_ATTRIBUTE_NUMBER 8

#endif
