1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 
11 /**
12   @file eax_encrypt_authenticate_memory.c
13   EAX implementation, encrypt a block of memory, by Tom St Denis
14 */
15 #include "tomcrypt_private.h"
16 
17 #ifdef LTC_EAX_MODE
18 
19 /**
20    EAX encrypt and produce an authentication tag
21    @param cipher     The index of the cipher desired
22    @param key        The secret key to use
23    @param keylen     The length of the secret key (octets)
24    @param nonce      The session nonce [use once]
25    @param noncelen   The length of the nonce
26    @param header     The header for the session
27    @param headerlen  The length of the header (octets)
28    @param pt         The plaintext
29    @param ptlen      The length of the plaintext (octets)
30    @param ct         [out] The ciphertext
31    @param tag        [out] The destination tag
32    @param taglen     [in/out] The max size and resulting size of the authentication tag
33    @return CRYPT_OK if successful
34 */
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)35 int eax_encrypt_authenticate_memory(int cipher,
36     const unsigned char *key,    unsigned long keylen,
37     const unsigned char *nonce,  unsigned long noncelen,
38     const unsigned char *header, unsigned long headerlen,
39     const unsigned char *pt,     unsigned long ptlen,
40           unsigned char *ct,
41           unsigned char *tag,    unsigned long *taglen)
42 {
43    int err;
44    eax_state *eax;
45 
46    LTC_ARGCHK(key    != NULL);
47    LTC_ARGCHK(pt     != NULL);
48    LTC_ARGCHK(ct     != NULL);
49    LTC_ARGCHK(tag    != NULL);
50    LTC_ARGCHK(taglen != NULL);
51 
52    eax = XMALLOC(sizeof(*eax));
53 
54    if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
55       goto LBL_ERR;
56    }
57 
58    if ((err = eax_encrypt(eax, pt, ct, ptlen)) != CRYPT_OK) {
59       goto LBL_ERR;
60    }
61 
62    if ((err = eax_done(eax, tag, taglen)) != CRYPT_OK) {
63       goto LBL_ERR;
64    }
65 
66    err = CRYPT_OK;
67 LBL_ERR:
68 #ifdef LTC_CLEAN_STACK
69    zeromem(eax, sizeof(*eax));
70 #endif
71 
72    XFREE(eax);
73 
74    return err;
75 }
76 
77 #endif
78 
79 /* ref:         $Format:%D$ */
80 /* git commit:  $Format:%H$ */
81 /* commit time: $Format:%ai$ */
82