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