tomcrypt/eax_init.c

127 lines
3.0 KiB
C
Raw Normal View History

2004-05-12 16:42:16 -04: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.
*
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
*/
/* EAX Implementation by Tom St Denis */
#include "mycrypt.h"
#ifdef EAX_MODE
int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen)
{
2004-06-19 22:41:49 -04:00
unsigned char *buf;
2004-05-12 16:42:16 -04:00
int err, blklen;
2004-06-19 22:41:49 -04:00
omac_state *omac;
2004-05-12 16:42:16 -04:00
unsigned long len;
_ARGCHK(eax != NULL);
_ARGCHK(key != NULL);
_ARGCHK(nonce != NULL);
if (headerlen > 0) {
_ARGCHK(header != NULL);
}
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
blklen = cipher_descriptor[cipher].block_length;
2004-06-19 22:41:49 -04:00
/* allocate ram */
buf = XMALLOC(MAXBLOCKSIZE);
omac = XMALLOC(sizeof(omac_state));
if (buf == NULL || omac == NULL) {
if (buf != NULL) {
XFREE(buf);
}
if (omac != NULL) {
XFREE(omac);
}
return CRYPT_MEM;
}
2004-05-12 16:42:16 -04:00
/* N = OMAC_0K(nonce) */
2004-06-19 22:41:49 -04:00
zeromem(buf, MAXBLOCKSIZE);
if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* omac the [0]_n */
2004-06-19 22:41:49 -04:00
if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* omac the nonce */
2004-06-19 22:41:49 -04:00
if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* store result */
len = sizeof(eax->N);
2004-06-19 22:41:49 -04:00
if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* H = OMAC_1K(header) */
2004-06-19 22:41:49 -04:00
zeromem(buf, MAXBLOCKSIZE);
2004-05-12 16:42:16 -04:00
buf[blklen - 1] = 1;
if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* omac the [1]_n */
if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* omac the header */
if (headerlen != 0) {
if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-05-12 16:42:16 -04:00
}
}
/* note we don't finish the headeromac, this allows us to add more header later */
/* setup the CTR mode */
if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* use big-endian counter */
eax->ctr.mode = 1;
/* setup the OMAC for the ciphertext */
if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-05-12 16:42:16 -04:00
}
/* omac [2]_n */
2004-06-19 22:41:49 -04:00
zeromem(buf, MAXBLOCKSIZE);
2004-05-12 16:42:16 -04:00
buf[blklen-1] = 2;
if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
2004-06-19 22:41:49 -04:00
goto __ERR;
2004-05-12 16:42:16 -04:00
}
2004-06-19 22:41:49 -04:00
err = CRYPT_OK;
__ERR:
2004-05-12 16:42:16 -04:00
#ifdef CLEAN_STACK
2004-06-19 22:41:49 -04:00
zeromem(buf, MAXBLOCKSIZE);
zeromem(omac, sizeof(omac_state));
2004-05-12 16:42:16 -04:00
#endif
2004-06-19 22:41:49 -04:00
XFREE(omac);
XFREE(buf);
return err;
2004-05-12 16:42:16 -04:00
}
#endif