1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /**
5   @file eax_encrypt_authenticate_memory.c
6   EAX implementation, encrypt a block of memory, by Tom St Denis
7 */
8 #include "tomcrypt_private.h"
9 
10 #ifdef LTC_EAX_MODE
11 
12 /**
13    EAX encrypt and produce an authentication tag
14    @param cipher     The index of the cipher desired
15    @param key        The secret key to use
16    @param keylen     The length of the secret key (octets)
17    @param nonce      The session nonce [use once]
18    @param noncelen   The length of the nonce
19    @param header     The header for the session
20    @param headerlen  The length of the header (octets)
21    @param pt         The plaintext
22    @param ptlen      The length of the plaintext (octets)
23    @param ct         [out] The ciphertext
24    @param tag        [out] The destination tag
25    @param taglen     [in/out] The max size and resulting size of the authentication tag
26    @return CRYPT_OK if successful
27 */
eax_encrypt_authenticate_memory(int cipher,const unsigned char * key,unsigned long keylen,const unsigned char * nonce,unsigned long noncelen,const unsigned char * header,unsigned long headerlen,const unsigned char * pt,unsigned long ptlen,unsigned char * ct,unsigned char * tag,unsigned long * taglen)28 int eax_encrypt_authenticate_memory(int cipher,
29     const unsigned char *key,    unsigned long keylen,
30     const unsigned char *nonce,  unsigned long noncelen,
31     const unsigned char *header, unsigned long headerlen,
32     const unsigned char *pt,     unsigned long ptlen,
33           unsigned char *ct,
34           unsigned char *tag,    unsigned long *taglen)
35 {
36    int err;
37    eax_state *eax;
38 
39    LTC_ARGCHK(key    != NULL);
40    LTC_ARGCHK(pt     != NULL);
41    LTC_ARGCHK(ct     != NULL);
42    LTC_ARGCHK(tag    != NULL);
43    LTC_ARGCHK(taglen != NULL);
44 
45    eax = XMALLOC(sizeof(*eax));
46 
47    if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
48       goto LBL_ERR;
49    }
50 
51    if ((err = eax_encrypt(eax, pt, ct, ptlen)) != CRYPT_OK) {
52       goto LBL_ERR;
53    }
54 
55    if ((err = eax_done(eax, tag, taglen)) != CRYPT_OK) {
56       goto LBL_ERR;
57    }
58 
59    err = CRYPT_OK;
60 LBL_ERR:
61 #ifdef LTC_CLEAN_STACK
62    zeromem(eax, sizeof(*eax));
63 #endif
64 
65    XFREE(eax);
66 
67    return err;
68 }
69 
70 #endif
71