pmac_file.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_file(int cipher,
  15. const unsigned char *key, unsigned long keylen,
  16. const char *filename,
  17. unsigned char *out, unsigned long *outlen)
  18. {
  19. #ifdef NO_FILE
  20. return CRYPT_NOP;
  21. #else
  22. int err, x;
  23. pmac_state pmac;
  24. FILE *in;
  25. unsigned char buf[512];
  26. _ARGCHK(key != NULL);
  27. _ARGCHK(filename != NULL);
  28. _ARGCHK(out != NULL);
  29. _ARGCHK(outlen != NULL);
  30. in = fopen(filename, "rb");
  31. if (in == NULL) {
  32. return CRYPT_FILE_NOTFOUND;
  33. }
  34. if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) {
  35. fclose(in);
  36. return err;
  37. }
  38. do {
  39. x = fread(buf, 1, sizeof(buf), in);
  40. if ((err = pmac_process(&pmac, buf, x)) != CRYPT_OK) {
  41. fclose(in);
  42. return err;
  43. }
  44. } while (x == sizeof(buf));
  45. fclose(in);
  46. if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) {
  47. return err;
  48. }
  49. #ifdef CLEAN_STACK
  50. zeromem(buf, sizeof(buf));
  51. #endif
  52. return CRYPT_OK;
  53. #endif
  54. }
  55. #endif