Path: blob/master/libs/tomcrypt/src/encauth/ccm/ccm_init.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910#ifdef LTC_CCM_MODE1112/**13Initialize a CCM state14@param ccm The CCM state to initialize15@param cipher The index of the cipher to use16@param key The secret key17@param keylen The length of the secret key18@param ptlen The length of the plain/cipher text that will be processed19@param taglen The max length of the MAC tag20@param aadlen The length of the AAD2122@return CRYPT_OK on success23*/24int ccm_init(ccm_state *ccm, int cipher,25const unsigned char *key, int keylen, int ptlen, int taglen, int aadlen)26{27int err;2829LTC_ARGCHK(ccm != NULL);30LTC_ARGCHK(key != NULL);31LTC_ARGCHK(taglen != 0);3233XMEMSET(ccm, 0, sizeof(ccm_state));3435/* check cipher input */36if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {37return err;38}39if (cipher_descriptor[cipher].block_length != 16) {40return CRYPT_INVALID_CIPHER;41}4243/* make sure the taglen is even and <= 16 */44ccm->taglen = taglen;45ccm->taglen &= ~1;46if (ccm->taglen > 16) {47ccm->taglen = 16;48}4950/* can't use < 4 */51if (ccm->taglen < 4) {52return CRYPT_INVALID_ARG;53}5455/* schedule key */56if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ccm->K)) != CRYPT_OK) {57return err;58}59ccm->cipher = cipher;6061/* let's get the L value */62ccm->ptlen = ptlen;63ccm->L = 0;64while (ptlen) {65++ccm->L;66ptlen >>= 8;67}68if (ccm->L <= 1) {69ccm->L = 2;70}7172ccm->aadlen = aadlen;73return CRYPT_OK;74}7576#endif777879