1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 EAX implementation, initialized EAX state, by Tom St Denis
19 Initialized an EAX state
20 @param eax [out] The EAX state to initialize
21 @param cipher The index of the desired cipher
22 @param key The secret key
23 @param keylen The length of the secret key (octets)
24 @param nonce The use-once nonce for the session
25 @param noncelen The length of the nonce (octets)
26 @param header The header for the EAX state
27 @param headerlen The header length (octets)
28 @return CRYPT_OK if successful
30 int eax_init(eax_state *eax, int cipher,
31 const unsigned char *key, unsigned long keylen,
32 const unsigned char *nonce, unsigned long noncelen,
33 const unsigned char *header, unsigned long headerlen)
41 LTC_ARGCHK(eax != NULL);
42 LTC_ARGCHK(key != NULL);
43 LTC_ARGCHK(nonce != NULL);
45 LTC_ARGCHK(header != NULL);
48 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
51 blklen = cipher_descriptor[cipher].block_length;
54 buf = XMALLOC(MAXBLOCKSIZE);
55 omac = XMALLOC(sizeof(*omac));
57 if (buf == NULL || omac == NULL) {
67 /* N = LTC_OMAC_0K(nonce) */
68 zeromem(buf, MAXBLOCKSIZE);
69 if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
74 if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
78 if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
83 if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
87 /* H = LTC_OMAC_1K(header) */
88 zeromem(buf, MAXBLOCKSIZE);
91 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
96 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
100 if (headerlen != 0) {
101 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
106 /* note we don't finish the headeromac, this allows us to add more header later */
108 /* setup the CTR mode */
109 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) {
113 /* setup the LTC_OMAC for the ciphertext */
114 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
119 zeromem(buf, MAXBLOCKSIZE);
121 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
127 #ifdef LTC_CLEAN_STACK
128 zeromem(buf, MAXBLOCKSIZE);
129 zeromem(omac, sizeof(*omac));
140 /* ref: $Format:%D$ */
141 /* git commit: $Format:%H$ */
142 /* commit time: $Format:%ai$ */