pmac_process.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /* PMAC implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef PMAC
  14. int pmac_process(pmac_state *state, const unsigned char *buf, unsigned long len)
  15. {
  16. int err, n, x;
  17. unsigned char Z[MAXBLOCKSIZE];
  18. _ARGCHK(state != NULL);
  19. _ARGCHK(buf != NULL);
  20. if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) {
  21. return err;
  22. }
  23. if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) ||
  24. (state->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) {
  25. return CRYPT_INVALID_ARG;
  26. }
  27. while (len != 0) {
  28. /* ok if the block is full we xor in prev, encrypt and replace prev */
  29. if (state->buflen == state->block_len) {
  30. pmac_shift_xor(state);
  31. for (x = 0; x < state->block_len; x++) {
  32. Z[x] = state->Li[x] ^ state->block[x];
  33. }
  34. cipher_descriptor[state->cipher_idx].ecb_encrypt(Z, Z, &state->key);
  35. for (x = 0; x < state->block_len; x++) {
  36. state->checksum[x] ^= Z[x];
  37. }
  38. state->buflen = 0;
  39. }
  40. /* add bytes */
  41. n = MIN(len, (unsigned long)(state->block_len - state->buflen));
  42. XMEMCPY(state->block + state->buflen, buf, n);
  43. state->buflen += n;
  44. len -= n;
  45. buf += n;
  46. }
  47. #ifdef CLEAN_STACK
  48. zeromem(Z, sizeof(Z));
  49. #endif
  50. return CRYPT_OK;
  51. }
  52. #endif