ocb_decrypt_verify_memory.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /* OCB Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef OCB_MODE
  14. int ocb_decrypt_verify_memory(int cipher,
  15. const unsigned char *key, unsigned long keylen,
  16. const unsigned char *nonce,
  17. const unsigned char *ct, unsigned long ctlen,
  18. unsigned char *pt,
  19. const unsigned char *tag, unsigned long taglen,
  20. int *res)
  21. {
  22. int err;
  23. ocb_state *ocb;
  24. _ARGCHK(key != NULL);
  25. _ARGCHK(nonce != NULL);
  26. _ARGCHK(pt != NULL);
  27. _ARGCHK(ct != NULL);
  28. _ARGCHK(tag != NULL);
  29. _ARGCHK(res != NULL);
  30. /* allocate memory */
  31. ocb = XMALLOC(sizeof(ocb_state));
  32. if (ocb == NULL) {
  33. return CRYPT_MEM;
  34. }
  35. if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) {
  36. goto __ERR;
  37. }
  38. while (ctlen > (unsigned long)ocb->block_len) {
  39. if ((err = ocb_decrypt(ocb, ct, pt)) != CRYPT_OK) {
  40. goto __ERR;
  41. }
  42. ctlen -= ocb->block_len;
  43. pt += ocb->block_len;
  44. ct += ocb->block_len;
  45. }
  46. err = ocb_done_decrypt(ocb, ct, ctlen, pt, tag, taglen, res);
  47. __ERR:
  48. #ifdef CLEAN_STACK
  49. zeromem(ocb, sizeof(ocb_state));
  50. #endif
  51. XFREE(ocb);
  52. return err;
  53. }
  54. #endif