Home | History | Annotate | Download | only in pmac
      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 
     13 /**
     14   @file pmac_done.c
     15   PMAC implementation, terminate a session, by Tom St Denis
     16 */
     17 
     18 #ifdef LTC_PMAC
     19 
     20 int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen)
     21 {
     22    int err, x;
     23 
     24    LTC_ARGCHK(state != NULL);
     25    LTC_ARGCHK(out   != NULL);
     26    if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) {
     27       return err;
     28    }
     29 
     30    if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) ||
     31        (state->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) {
     32       return CRYPT_INVALID_ARG;
     33    }
     34 
     35 
     36    /* handle padding.  If multiple xor in L/x */
     37 
     38    if (state->buflen == state->block_len) {
     39       /* xor Lr against the checksum */
     40       for (x = 0; x < state->block_len; x++) {
     41           state->checksum[x] ^= state->block[x] ^ state->Lr[x];
     42       }
     43    } else {
     44       /* otherwise xor message bytes then the 0x80 byte */
     45       for (x = 0; x < state->buflen; x++) {
     46           state->checksum[x] ^= state->block[x];
     47       }
     48       state->checksum[x] ^= 0x80;
     49    }
     50 
     51    /* encrypt it */
     52    if ((err = cipher_descriptor[state->cipher_idx].ecb_encrypt(state->checksum, state->checksum, &state->key)) != CRYPT_OK) {
     53       return err;
     54    }
     55    cipher_descriptor[state->cipher_idx].done(&state->key);
     56 
     57    /* store it */
     58    for (x = 0; x < state->block_len && x < (int)*outlen; x++) {
     59        out[x] = state->checksum[x];
     60    }
     61    *outlen = x;
     62 
     63 #ifdef LTC_CLEAN_STACK
     64    zeromem(state, sizeof(*state));
     65 #endif
     66    return CRYPT_OK;
     67 }
     68 
     69 #endif
     70 
     71 
     72 /* $Source: /cvs/libtom/libtomcrypt/src/mac/pmac/pmac_done.c,v $ */
     73 /* $Revision: 1.8 $ */
     74 /* $Date: 2006/11/03 00:39:49 $ */
     75