2
0

omac_process.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /* OMAC1 Support by Tom St Denis (for 64 and 128 bit block ciphers only) */
  12. #include "mycrypt.h"
  13. #ifdef OMAC
  14. int omac_process(omac_state *state, const unsigned char *buf, unsigned long len)
  15. {
  16. int err, n, x;
  17. _ARGCHK(state != NULL);
  18. _ARGCHK(buf != NULL);
  19. if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) {
  20. return err;
  21. }
  22. if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) ||
  23. (state->blklen > (int)sizeof(state->block)) || (state->buflen > state->blklen)) {
  24. return CRYPT_INVALID_ARG;
  25. }
  26. while (len != 0) {
  27. /* ok if the block is full we xor in prev, encrypt and replace prev */
  28. if (state->buflen == state->blklen) {
  29. for (x = 0; x < state->blklen; x++) {
  30. state->block[x] ^= state->prev[x];
  31. }
  32. cipher_descriptor[state->cipher_idx].ecb_encrypt(state->block, state->prev, &state->key);
  33. state->buflen = 0;
  34. }
  35. /* add bytes */
  36. n = MIN(len, (unsigned long)(state->blklen - state->buflen));
  37. XMEMCPY(state->block + state->buflen, buf, n);
  38. state->buflen += n;
  39. len -= n;
  40. buf += n;
  41. }
  42. return CRYPT_OK;
  43. }
  44. #endif