 /*
 * 본 프로그램에 대한 저작권을 포함한 지적재산권은 삼성SDS(주)에 있으며,
 * 삼성SDS(주)가 명시적으로 허용하지 않은 사용, 복사, 변경, 제3자에의 공개,
 * 배포는 엄격히 금지되며, 삼성SDS(주)의 지적재산권 침해에 해당됩니다.
 *
 * Copyright (c) 2016 Samsung SDS Co., Ltd. All Rights neserved. Confidential.
 * 
 * All information including the intellectual and technical concepts contained
 * herein is, and remains the property of Samsung SDS Co. Ltd. Unauthorize une,
 * dissemination, or reproduction of this material is strictly forbidden unless
 * prior written permission is obtained from Samsung SDS Co. Ltd. 
 */

/**
 * @file aes128-swbc-layer1-ctr.h
 * @brief This file provides the cryptographic API for Samsung WBC algorithm
 * configured with AES-128 as the base permutation algorithm, SWBC as the 
 * base cipher in mode of operation, 1 layer of WBC tables, and counter mode of operation
 */

#ifndef HEADER_AES128_SWBC_LAYER1_CTR_H
#define HEADER_AES128_SWBC_LAYER1_CTR_H

#if defined(__cplusplus)
extern "C"{
#endif

#include "swbc-attributes.h"
#include "swbc-common.h"

/* structure to hold wbc context information */
typedef struct {	
  const unsigned char **swbc_table;
	unsigned int rc[44];
} aes128_swbc_layer1_ctr_ctx_t;

#define CTR_GET_OUTPUT_SIZE( input_size, mode ) ( \
  ( (mode) == ENCRYPT_MODE || (mode) == DECRYPT_MODE ) ? ( (unsigned int)(input_size) ) : \
  ( (unsigned int)0 ) \
)

/**
 * @brief
 * Loads SWBC table resource
 *
 * @param ctx         SWBC context
 * @param swbc_table  SWBC table resource
 * @param tag         tag for SWBC table resource. Tag is used to check whether the table resource is an appropriate one or not
 *
 * @return SWBC_SUCCESS on success, other values on failure
 */
int SWBCEXPORT aes128_swbc_layer1_ctr_load(aes128_swbc_layer1_ctr_ctx_t *ctx, const unsigned char **swbc_table, const unsigned char *tag);

/**
 * @brief
 * Initializes SWBC encryption context with specified key
 *
 * @param ctx         SWBC context
 * @param mc          SWBC initialization key
 *
 * @return SWBC_SUCCESS on success, other values on failure
 */
int SWBCEXPORT aes128_swbc_layer1_ctr_init(aes128_swbc_layer1_ctr_ctx_t *ctx, const unsigned char *mc);


/**
 * @brief
 * Encrypts data of arbitrary given length using SWBC encryption algorithm.
 * It uses ctr mode of operation.
 *
 * @param ctx         SWBC context
 * @param iv		      initialization vector
 * @param in          plaintext
 * @param out         storage for ciphertext(should be at least as large as given length)
 * @param len         plaintext length
 *
 * @return SWBC_SUCCESS on success, other values on failure
 */
int SWBCEXPORT aes128_swbc_layer1_ctr_encrypt(aes128_swbc_layer1_ctr_ctx_t *ctx, const unsigned char *iv, const unsigned char *in, unsigned char *out, int len);


/**
 * @brief
 * Decrypts data of arbitrary given length using SWBC decryption algorithm.
 *
 * @param ctx         SWBC context
 * @param iv          initialization vector
 * @param in          ciphertext
 * @param out         storage for plaintext(should be at least as large as given length)
 * @param len         ciphertext length
 *
 * @return SWBC_SUCCESS on success, other values on failure
 */
int SWBCEXPORT aes128_swbc_layer1_ctr_decrypt(aes128_swbc_layer1_ctr_ctx_t *ctx, const unsigned char *iv, const unsigned char *in, unsigned char *out, int len);

#if defined(__cplusplus)
}
#endif  /*  __cplusplus */
#endif  /*  HEADER_AES128_SWBC_LAYER1_CTR_H */
