1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 PMAC implementation, process data, by Tom St Denis
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
26 int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)
30 unsigned char Z[MAXBLOCKSIZE];
32 LTC_ARGCHK(pmac != NULL);
33 LTC_ARGCHK(in != NULL);
34 if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {
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;
44 if (pmac->buflen == 0 && inlen > 16) {
46 for (x = 0; x < (inlen - 16); x += 16) {
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]));
51 if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {
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]));
64 /* ok if the block is full we xor in prev, encrypt and replace prev */
65 if (pmac->buflen == pmac->block_len) {
67 for (x = 0; x < (unsigned long)pmac->block_len; x++) {
68 Z[x] = pmac->Li[x] ^ pmac->block[x];
70 if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {
73 for (x = 0; x < (unsigned long)pmac->block_len; x++) {
74 pmac->checksum[x] ^= Z[x];
80 n = MIN(inlen, (unsigned long)(pmac->block_len - pmac->buflen));
81 XMEMCPY(pmac->block + pmac->buflen, in, n);
87 #ifdef LTC_CLEAN_STACK
88 zeromem(Z, sizeof(Z));
96 /* ref: $Format:%D$ */
97 /* git commit: $Format:%H$ */
98 /* commit time: $Format:%ai$ */