ctr_start.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_start(int cipher, const unsigned char *count, const unsigned char *key, int keylen,
  14. int num_rounds, symmetric_CTR *ctr)
  15. {
  16. int x, err;
  17. _ARGCHK(count != NULL);
  18. _ARGCHK(key != NULL);
  19. _ARGCHK(ctr != NULL);
  20. /* bad param? */
  21. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  22. return err;
  23. }
  24. /* setup cipher */
  25. if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) {
  26. return err;
  27. }
  28. /* copy ctr */
  29. ctr->blocklen = cipher_descriptor[cipher].block_length;
  30. ctr->cipher = cipher;
  31. ctr->padlen = 0;
  32. ctr->mode = 0;
  33. for (x = 0; x < ctr->blocklen; x++) {
  34. ctr->ctr[x] = count[x];
  35. }
  36. cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
  37. return CRYPT_OK;
  38. }
  39. #endif