comparison src/mac/pmac/pmac_process.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children 39d5d58461d6
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
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, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file pmac_process.c
15 PMAC implementation, process data, by Tom St Denis
16 */
17
18
19 #ifdef PMAC
20
21 /**
22 Process data in a PMAC stream
23 @param pmac The PMAC state
24 @param in The data to send through PMAC
25 @param inlen The length of the data to send through PMAC
26 @return CRYPT_OK if successful
27 */
28 int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)
29 {
30 int err, n;
31 unsigned long x;
32 unsigned char Z[MAXBLOCKSIZE];
33
34 LTC_ARGCHK(pmac != NULL);
35 LTC_ARGCHK(in != NULL);
36 if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {
37 return err;
38 }
39
40 if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) ||
41 (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) {
42 return CRYPT_INVALID_ARG;
43 }
44
45 #ifdef LTC_FAST
46 if (pmac->buflen == 0 && inlen > 16) {
47 unsigned long y;
48 for (x = 0; x < (inlen - 16); x += 16) {
49 pmac_shift_xor(pmac);
50 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
51 *((LTC_FAST_TYPE*)(&Z[y])) = *((LTC_FAST_TYPE*)(&in[y])) ^ *((LTC_FAST_TYPE*)(&pmac->Li[y]));
52 }
53 cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key);
54 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
55 *((LTC_FAST_TYPE*)(&pmac->checksum[y])) ^= *((LTC_FAST_TYPE*)(&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 cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key);
71 for (x = 0; x < (unsigned long)pmac->block_len; x++) {
72 pmac->checksum[x] ^= Z[x];
73 }
74 pmac->buflen = 0;
75 }
76
77 /* add bytes */
78 n = MIN(inlen, (unsigned long)(pmac->block_len - pmac->buflen));
79 XMEMCPY(pmac->block + pmac->buflen, in, n);
80 pmac->buflen += n;
81 inlen -= n;
82 in += n;
83 }
84
85 #ifdef LTC_CLEAN_STACK
86 zeromem(Z, sizeof(Z));
87 #endif
88
89 return CRYPT_OK;
90 }
91
92 #endif