omac_done.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_done(omac_state *state, unsigned char *out, unsigned long *outlen)
  15. {
  16. int err, mode, x;
  17. _ARGCHK(state != NULL);
  18. _ARGCHK(out != 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. /* figure out mode */
  27. if (state->buflen != state->blklen) {
  28. /* add the 0x80 byte */
  29. state->block[state->buflen++] = 0x80;
  30. /* pad with 0x00 */
  31. while (state->buflen < state->blklen) {
  32. state->block[state->buflen++] = 0x00;
  33. }
  34. mode = 1;
  35. } else {
  36. mode = 0;
  37. }
  38. /* now xor prev + Lu[mode] */
  39. for (x = 0; x < state->blklen; x++) {
  40. state->block[x] ^= state->prev[x] ^ state->Lu[mode][x];
  41. }
  42. /* encrypt it */
  43. cipher_descriptor[state->cipher_idx].ecb_encrypt(state->block, state->block, &state->key);
  44. /* output it */
  45. for (x = 0; x < state->blklen && (unsigned long)x < *outlen; x++) {
  46. out[x] = state->block[x];
  47. }
  48. *outlen = x;
  49. #ifdef CLEAN_STACK
  50. zeromem(state, sizeof(*state));
  51. #endif
  52. return CRYPT_OK;
  53. }
  54. #endif