xtea.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* xtea.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file xtea.c
  18. * \brief XTEA implemantation
  19. * copy'n'pasted from http://en.wikipedia.org/wiki/XTEA
  20. * and slightly modified
  21. */
  22. #include <stdint.h>
  23. void xtea_enc(void* dest, const void* v, const void* k) {
  24. uint8_t i;
  25. uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
  26. uint32_t sum=0, delta=0x9E3779B9;
  27. for(i=0; i<32; i++) {
  28. v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
  29. sum += delta;
  30. v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
  31. }
  32. ((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
  33. }
  34. void xtea_dec(void* dest, const void* v, const void* k) {
  35. uint8_t i;
  36. uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
  37. uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
  38. for(i=0; i<32; i++) {
  39. v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
  40. sum -= delta;
  41. v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
  42. }
  43. ((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
  44. }