pmac_done.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_done(pmac_state *state, unsigned char *out, unsigned long *outlen)
  15. {
  16. int err, 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->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) {
  24. return CRYPT_INVALID_ARG;
  25. }
  26. /* handle padding. If multiple xor in L/x */
  27. if (state->buflen == state->block_len) {
  28. /* xor Lr against the checksum */
  29. for (x = 0; x < state->block_len; x++) {
  30. state->checksum[x] ^= state->block[x] ^ state->Lr[x];
  31. }
  32. } else {
  33. /* otherwise xor message bytes then the 0x80 byte */
  34. for (x = 0; x < state->buflen; x++) {
  35. state->checksum[x] ^= state->block[x];
  36. }
  37. state->checksum[x] ^= 0x80;
  38. }
  39. /* encrypt it */
  40. cipher_descriptor[state->cipher_idx].ecb_encrypt(state->checksum, state->checksum, &state->key);
  41. /* store it */
  42. for (x = 0; x < state->block_len && x <= (int)*outlen; x++) {
  43. out[x] = state->checksum[x];
  44. }
  45. *outlen = x;
  46. #ifdef CLEAN_STACK
  47. zeromem(state, sizeof(*state));
  48. #endif
  49. return CRYPT_OK;
  50. }
  51. #endif