]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/pmac/pmac_process.c
remove rmd hashes
[zpackage] / libtomcrypt / src / mac / pmac / pmac_process.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 #include "tomcrypt.h"
10
11 /**
12    @file pmac_process.c
13    PMAC implementation, process data, by Tom St Denis
14 */
15
16
17 #ifdef LTC_PMAC
18
19 /**
20   Process data in a PMAC stream
21   @param pmac     The PMAC state
22   @param in       The data to send through PMAC
23   @param inlen    The length of the data to send through PMAC
24   @return CRYPT_OK if successful
25 */
26 int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)
27 {
28    int err, n;
29    unsigned long x;
30    unsigned char Z[MAXBLOCKSIZE];
31
32    LTC_ARGCHK(pmac != NULL);
33    LTC_ARGCHK(in   != NULL);
34    if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {
35       return err;
36    }
37
38    if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) ||
39        (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) {
40       return CRYPT_INVALID_ARG;
41    }
42
43 #ifdef LTC_FAST
44    if (pmac->buflen == 0 && inlen > 16) {
45       unsigned long y;
46       for (x = 0; x < (inlen - 16); x += 16) {
47           pmac_shift_xor(pmac);
48           for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
49               *(LTC_FAST_TYPE_PTR_CAST(&Z[y])) = *(LTC_FAST_TYPE_PTR_CAST(&in[y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&pmac->Li[y]));
50           }
51           if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {
52              return err;
53           }
54           for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
55               *(LTC_FAST_TYPE_PTR_CAST(&pmac->checksum[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&Z[y]));
56           }
57           in += 16;
58       }
59       inlen -= x;
60    }
61 #endif
62
63    while (inlen != 0) {
64        /* ok if the block is full we xor in prev, encrypt and replace prev */
65        if (pmac->buflen == pmac->block_len) {
66           pmac_shift_xor(pmac);
67           for (x = 0; x < (unsigned long)pmac->block_len; x++) {
68                Z[x] = pmac->Li[x] ^ pmac->block[x];
69           }
70           if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {
71              return err;
72            }
73           for (x = 0; x < (unsigned long)pmac->block_len; x++) {
74               pmac->checksum[x] ^= Z[x];
75           }
76           pmac->buflen = 0;
77        }
78
79        /* add bytes */
80        n = MIN(inlen, (unsigned long)(pmac->block_len - pmac->buflen));
81        XMEMCPY(pmac->block + pmac->buflen, in, n);
82        pmac->buflen  += n;
83        inlen         -= n;
84        in            += n;
85    }
86
87 #ifdef LTC_CLEAN_STACK
88    zeromem(Z, sizeof(Z));
89 #endif
90
91    return CRYPT_OK;
92 }
93
94 #endif
95
96 /* ref:         $Format:%D$ */
97 /* git commit:  $Format:%H$ */
98 /* commit time: $Format:%ai$ */