ctr_setiv.c 914 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include "mycrypt.h"
  12. #ifdef CTR
  13. int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr)
  14. {
  15. int err;
  16. _ARGCHK(IV != NULL);
  17. _ARGCHK(ctr != NULL);
  18. /* bad param? */
  19. if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
  20. return err;
  21. }
  22. if (len != (unsigned long)ctr->blocklen) {
  23. return CRYPT_INVALID_ARG;
  24. }
  25. /* set IV */
  26. XMEMCPY(ctr->ctr, IV, len);
  27. /* force next block */
  28. ctr->padlen = 0;
  29. cipher_descriptor[ctr->cipher].ecb_encrypt(IV, ctr->pad, &ctr->key);
  30. return CRYPT_OK;
  31. }
  32. #endif