tomcrypt/src/encauth/eax/eax_init.c

145 lines
3.6 KiB
C
Raw Normal View History

2004-05-12 20:42:16 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*
2007-07-20 17:48:02 +00:00
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
2004-05-12 20:42:16 +00:00
*/
2004-12-30 23:55:53 +00:00
/**
@file eax_init.c
EAX implementation, initialized EAX state, by Tom St Denis
*/
#include "tomcrypt.h"
2004-05-12 20:42:16 +00:00
2007-07-20 17:48:02 +00:00
#ifdef LTC_EAX_MODE
2004-05-12 20:42:16 +00:00
2004-12-30 23:55:53 +00:00
/**
Initialized an EAX state
@param eax [out] The EAX state to initialize
@param cipher The index of the desired cipher
@param key The secret key
@param keylen The length of the secret key (octets)
@param nonce The use-once nonce for the session
@param noncelen The length of the nonce (octets)
@param header The header for the EAX state
@param headerlen The header length (octets)
@return CRYPT_OK if successful
*/
int eax_init(eax_state *eax, int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen,
2004-05-12 20:42:16 +00:00
const unsigned char *header, unsigned long headerlen)
{
2004-06-20 02:41:49 +00:00
unsigned char *buf;
2004-05-12 20:42:16 +00:00
int err, blklen;
2004-06-20 02:41:49 +00:00
omac_state *omac;
2004-05-12 20:42:16 +00:00
unsigned long len;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(eax != NULL);
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(nonce != NULL);
2004-05-12 20:42:16 +00:00
if (headerlen > 0) {
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(header != NULL);
2004-05-12 20:42:16 +00:00
}
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
blklen = cipher_descriptor[cipher].block_length;
2004-06-20 02:41:49 +00:00
/* allocate ram */
buf = XMALLOC(MAXBLOCKSIZE);
2004-12-30 23:55:53 +00:00
omac = XMALLOC(sizeof(*omac));
2004-06-20 02:41:49 +00:00
if (buf == NULL || omac == NULL) {
if (buf != NULL) {
XFREE(buf);
}
if (omac != NULL) {
XFREE(omac);
}
return CRYPT_MEM;
}
2007-07-20 17:48:02 +00:00
/* N = LTC_OMAC_0K(nonce) */
2004-06-20 02:41:49 +00:00
zeromem(buf, MAXBLOCKSIZE);
if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
/* omac the [0]_n */
2004-06-20 02:41:49 +00:00
if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
/* omac the nonce */
2004-06-20 02:41:49 +00:00
if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
/* store result */
len = sizeof(eax->N);
2004-06-20 02:41:49 +00:00
if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
2007-07-20 17:48:02 +00:00
/* H = LTC_OMAC_1K(header) */
2004-06-20 02:41:49 +00:00
zeromem(buf, MAXBLOCKSIZE);
2004-05-12 20:42:16 +00:00
buf[blklen - 1] = 1;
if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
/* omac the [1]_n */
if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
/* omac the header */
if (headerlen != 0) {
if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
}
/* note we don't finish the headeromac, this allows us to add more header later */
/* setup the CTR mode */
2005-06-09 00:08:13 +00:00
if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
2007-07-20 17:48:02 +00:00
/* setup the LTC_OMAC for the ciphertext */
2004-05-12 20:42:16 +00:00
if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
/* omac [2]_n */
2004-06-20 02:41:49 +00:00
zeromem(buf, MAXBLOCKSIZE);
2004-05-12 20:42:16 +00:00
buf[blklen-1] = 2;
if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
2004-12-30 23:55:53 +00:00
goto LBL_ERR;
2004-05-12 20:42:16 +00:00
}
2004-06-20 02:41:49 +00:00
err = CRYPT_OK;
2004-12-30 23:55:53 +00:00
LBL_ERR:
#ifdef LTC_CLEAN_STACK
2004-06-20 02:41:49 +00:00
zeromem(buf, MAXBLOCKSIZE);
2004-12-30 23:55:53 +00:00
zeromem(omac, sizeof(*omac));
2004-05-12 20:42:16 +00:00
#endif
2004-06-20 02:41:49 +00:00
XFREE(omac);
XFREE(buf);
return err;
2004-05-12 20:42:16 +00:00
}
#endif
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */