Path: blob/master/libs/tomcrypt/src/encauth/eax/eax_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*/89/**10@file eax_init.c11EAX implementation, initialized EAX state, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_EAX_MODE1617/**18Initialized an EAX state19@param eax [out] The EAX state to initialize20@param cipher The index of the desired cipher21@param key The secret key22@param keylen The length of the secret key (octets)23@param nonce The use-once nonce for the session24@param noncelen The length of the nonce (octets)25@param header The header for the EAX state26@param headerlen The header length (octets)27@return CRYPT_OK if successful28*/29int eax_init(eax_state *eax, int cipher,30const unsigned char *key, unsigned long keylen,31const unsigned char *nonce, unsigned long noncelen,32const unsigned char *header, unsigned long headerlen)33{34unsigned char *buf;35int err, blklen;36omac_state *omac;37unsigned long len;383940LTC_ARGCHK(eax != NULL);41LTC_ARGCHK(key != NULL);42LTC_ARGCHK(nonce != NULL);43if (headerlen > 0) {44LTC_ARGCHK(header != NULL);45}4647if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {48return err;49}50blklen = cipher_descriptor[cipher].block_length;5152/* allocate ram */53buf = XMALLOC(MAXBLOCKSIZE);54omac = XMALLOC(sizeof(*omac));5556if (buf == NULL || omac == NULL) {57if (buf != NULL) {58XFREE(buf);59}60if (omac != NULL) {61XFREE(omac);62}63return CRYPT_MEM;64}6566/* N = LTC_OMAC_0K(nonce) */67zeromem(buf, MAXBLOCKSIZE);68if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {69goto LBL_ERR;70}7172/* omac the [0]_n */73if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {74goto LBL_ERR;75}76/* omac the nonce */77if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {78goto LBL_ERR;79}80/* store result */81len = sizeof(eax->N);82if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {83goto LBL_ERR;84}8586/* H = LTC_OMAC_1K(header) */87zeromem(buf, MAXBLOCKSIZE);88buf[blklen - 1] = 1;8990if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {91goto LBL_ERR;92}9394/* omac the [1]_n */95if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {96goto LBL_ERR;97}98/* omac the header */99if (headerlen != 0) {100if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {101goto LBL_ERR;102}103}104105/* note we don't finish the headeromac, this allows us to add more header later */106107/* setup the CTR mode */108if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) {109goto LBL_ERR;110}111112/* setup the LTC_OMAC for the ciphertext */113if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {114goto LBL_ERR;115}116117/* omac [2]_n */118zeromem(buf, MAXBLOCKSIZE);119buf[blklen-1] = 2;120if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {121goto LBL_ERR;122}123124err = CRYPT_OK;125LBL_ERR:126#ifdef LTC_CLEAN_STACK127zeromem(buf, MAXBLOCKSIZE);128zeromem(omac, sizeof(*omac));129#endif130131XFREE(omac);132XFREE(buf);133134return err;135}136137#endif138139140