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