Home | History | Annotate | Download | only in xcbc
      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 (at) gmail.com, http://libtomcrypt.com
     10  */
     11 #include "tomcrypt.h"
     12 #include <stdarg.h>
     13 
     14 /**
     15   @file xcbc_memory_multi.c
     16   XCBC support, process multiple blocks of memory, Tom St Denis
     17 */
     18 
     19 #ifdef LTC_XCBC
     20 
     21 /**
     22    XCBC multiple blocks of memory
     23    @param cipher    The index of the desired cipher
     24    @param key       The secret key
     25    @param keylen    The length of the secret key (octets)
     26    @param out       [out] The destination of the authentication tag
     27    @param outlen    [in/out]  The max size and resulting size of the authentication tag (octets)
     28    @param in        The data to send through XCBC
     29    @param inlen     The length of the data to send through XCBC (octets)
     30    @param ...       tuples of (data,len) pairs to XCBC, terminated with a (NULL,x) (x=don't care)
     31    @return CRYPT_OK if successful
     32 */
     33 int xcbc_memory_multi(int cipher,
     34                 const unsigned char *key, unsigned long keylen,
     35                       unsigned char *out, unsigned long *outlen,
     36                 const unsigned char *in,  unsigned long inlen, ...)
     37 {
     38    int                  err;
     39    xcbc_state          *xcbc;
     40    va_list              args;
     41    const unsigned char *curptr;
     42    unsigned long        curlen;
     43 
     44    LTC_ARGCHK(key    != NULL);
     45    LTC_ARGCHK(in     != NULL);
     46    LTC_ARGCHK(out    != NULL);
     47    LTC_ARGCHK(outlen != NULL);
     48 
     49    /* allocate ram for xcbc state */
     50    xcbc = XMALLOC(sizeof(xcbc_state));
     51    if (xcbc == NULL) {
     52       return CRYPT_MEM;
     53    }
     54 
     55    /* xcbc process the message */
     56    if ((err = xcbc_init(xcbc, cipher, key, keylen)) != CRYPT_OK) {
     57       goto LBL_ERR;
     58    }
     59    va_start(args, inlen);
     60    curptr = in;
     61    curlen = inlen;
     62    for (;;) {
     63       /* process buf */
     64       if ((err = xcbc_process(xcbc, curptr, curlen)) != CRYPT_OK) {
     65          goto LBL_ERR;
     66       }
     67       /* step to next */
     68       curptr = va_arg(args, const unsigned char*);
     69       if (curptr == NULL) {
     70          break;
     71       }
     72       curlen = va_arg(args, unsigned long);
     73    }
     74    if ((err = xcbc_done(xcbc, out, outlen)) != CRYPT_OK) {
     75       goto LBL_ERR;
     76    }
     77 LBL_ERR:
     78 #ifdef LTC_CLEAN_STACK
     79    zeromem(xcbc, sizeof(xcbc_state));
     80 #endif
     81    XFREE(xcbc);
     82    va_end(args);
     83    return err;
     84 }
     85 
     86 #endif
     87 
     88 /* $Source: /cvs/libtom/libtomcrypt/src/mac/xcbc/xcbc_memory_multi.c,v $ */
     89 /* $Revision: 1.1 $ */
     90 /* $Date: 2006/11/03 01:53:25 $ */
     91