crypto_block.c 907 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "crypto_block.h"
  2. #include "crypto_uint64.h"
  3. #include "uint64_unpack.h"
  4. #include "uint64_pack.h"
  5. /*
  6. TEA with double-size words.
  7. XXX: Switch to crypto_block_aes256.
  8. XXX: Build crypto_stream_aes256 on top of crypto_block_aes256.
  9. */
  10. int crypto_block(
  11. unsigned char *out,
  12. const unsigned char *in,
  13. const unsigned char *k
  14. )
  15. {
  16. crypto_uint64 v0 = uint64_unpack(in + 0);
  17. crypto_uint64 v1 = uint64_unpack(in + 8);
  18. crypto_uint64 k0 = uint64_unpack(k + 0);
  19. crypto_uint64 k1 = uint64_unpack(k + 8);
  20. crypto_uint64 k2 = uint64_unpack(k + 16);
  21. crypto_uint64 k3 = uint64_unpack(k + 24);
  22. crypto_uint64 sum = 0;
  23. crypto_uint64 delta = 0x9e3779b97f4a7c15;
  24. int i;
  25. for (i = 0;i < 32;++i) {
  26. sum += delta;
  27. v0 += ((v1<<7) + k0) ^ (v1 + sum) ^ ((v1>>12) + k1);
  28. v1 += ((v0<<16) + k2) ^ (v0 + sum) ^ ((v0>>8) + k3);
  29. }
  30. uint64_pack(out + 0,v0);
  31. uint64_pack(out + 8,v1);
  32. return 0;
  33. }