comparison libtomcrypt/src/mac/pmac/pmac_process.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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
93
94 /* $Source: /cvs/libtom/libtomcrypt/src/mac/pmac/pmac_process.c,v $ */
95 /* $Revision: 1.5 $ */
96 /* $Date: 2005/05/05 14:35:59 $ */