ocb_encrypt.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(ocb_state *ocb, const unsigned char *pt, unsigned char *ct)
  15. {
  16. unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE];
  17. int err, x;
  18. _ARGCHK(ocb != NULL);
  19. _ARGCHK(pt != NULL);
  20. _ARGCHK(ct != NULL);
  21. if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
  22. return err;
  23. }
  24. if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
  25. return CRYPT_INVALID_ARG;
  26. }
  27. /* compute checksum */
  28. for (x = 0; x < ocb->block_len; x++) {
  29. ocb->checksum[x] ^= pt[x];
  30. }
  31. /* Get Z[i] value */
  32. ocb_shift_xor(ocb, Z);
  33. /* xor pt in, encrypt, xor Z out */
  34. for (x = 0; x < ocb->block_len; x++) {
  35. tmp[x] = pt[x] ^ Z[x];
  36. }
  37. cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, ct, &ocb->key);
  38. for (x = 0; x < ocb->block_len; x++) {
  39. ct[x] ^= Z[x];
  40. }
  41. #ifdef CLEAN_STACK
  42. zeromem(Z, sizeof(Z));
  43. zeromem(tmp, sizeof(tmp));
  44. #endif
  45. return CRYPT_OK;
  46. }
  47. #endif