• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 
12 /**
13     @file eax_decrypt_verify_memory.c
14     EAX implementation, decrypt block of memory, by Tom St Denis
15 */
16 #include "tomcrypt.h"
17 
18 #ifdef EAX_MODE
19 
20 /**
21    Decrypt a block of memory and verify the provided MAC tag with EAX
22    @param cipher     The index of the cipher desired
23    @param key        The secret key
24    @param keylen     The length of the key (octets)
25    @param nonce      The nonce data (use once) for the session
26    @param noncelen   The length of the nonce data.
27    @param header     The session header data
28    @param headerlen  The length of the header (octets)
29    @param ct         The ciphertext
30    @param ctlen      The length of the ciphertext (octets)
31    @param pt         [out] The plaintext
32    @param tag        The authentication tag provided by the encoder
33    @param taglen     [in/out] The length of the tag (octets)
34    @param stat       [out] The result of the decryption (1==valid tag, 0==invalid)
35    @return CRYPT_OK if successful regardless of the resulting tag comparison
36 */
eax_decrypt_verify_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 * ct,unsigned long ctlen,unsigned char * pt,unsigned char * tag,unsigned long taglen,int * stat)37 int eax_decrypt_verify_memory(int cipher,
38     const unsigned char *key,    unsigned long keylen,
39     const unsigned char *nonce,  unsigned long noncelen,
40     const unsigned char *header, unsigned long headerlen,
41     const unsigned char *ct,     unsigned long ctlen,
42           unsigned char *pt,
43           unsigned char *tag,    unsigned long taglen,
44           int           *stat)
45 {
46    int            err;
47    eax_state     *eax;
48    unsigned char *buf;
49    unsigned long  buflen;
50 
51    LTC_ARGCHK(stat != NULL);
52    LTC_ARGCHK(key  != NULL);
53    LTC_ARGCHK(pt   != NULL);
54    LTC_ARGCHK(ct   != NULL);
55    LTC_ARGCHK(tag  != NULL);
56 
57    /* default to zero */
58    *stat = 0;
59 
60    /* allocate ram */
61    buf = XMALLOC(taglen);
62    eax = XMALLOC(sizeof(*eax));
63    if (eax == NULL || buf == NULL) {
64       if (eax != NULL) {
65          XFREE(eax);
66       }
67       if (buf != NULL) {
68          XFREE(buf);
69       }
70       return CRYPT_MEM;
71    }
72 
73    if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
74       goto LBL_ERR;
75    }
76 
77    if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
78       goto LBL_ERR;
79    }
80 
81    buflen = taglen;
82    if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
83       goto LBL_ERR;
84    }
85 
86    /* compare tags */
87    if (buflen >= taglen && XMEMCMP(buf, tag, taglen) == 0) {
88       *stat = 1;
89    }
90 
91    err = CRYPT_OK;
92 LBL_ERR:
93 #ifdef LTC_CLEAN_STACK
94    zeromem(buf, taglen);
95    zeromem(eax, sizeof(*eax));
96 #endif
97 
98    XFREE(eax);
99    XFREE(buf);
100 
101    return err;
102 }
103 
104 #endif
105 
106 /* $Source: /cvs/libtom/libtomcrypt/src/encauth/eax/eax_decrypt_verify_memory.c,v $ */
107 /* $Revision: 1.5 $ */
108 /* $Date: 2006/11/01 09:28:17 $ */
109