xtea.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright of XTEA encryption algorithm by David Wheeler and Roger Needham
  3. * at the Computer Laboratory of Cambridge University.
  4. *
  5. * Kindly released to Public Domain.
  6. */
  7. /*
  8. * Copyright of Implementation 2010 by Ulrich Prinz (uprinz2@netscape.net)
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the copyright holders nor the names of
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  35. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. * For additional information see http://www.ethernut.de/
  39. */
  40. /*
  41. * \file gorp/crypt/xtea.c
  42. * \brief Fast and effcient Extended Tiny Encryption Algorythm.
  43. *
  44. * \verbatim
  45. * $Id$
  46. * \endverbatim
  47. */
  48. #include <compiler.h>
  49. #include <stdint.h>
  50. #include <string.h>
  51. #include <gorp/xtea.h>
  52. #define XTDELTA 0x9e3779b9
  53. #define XTSUM 0xC6EF3720
  54. #define ROUNDS 32
  55. /*!
  56. * \brief Encrypt a 64bit block with XTEA algorythm.
  57. *
  58. * \para w Destination pointer to 2x32 bit encrypted block.
  59. * \para v Source pointer of 2x32 bit plain text block.
  60. * \para k Pointer to 128 bit (4*32bit) key block.
  61. */
  62. void XTeaCrypt(uint32_t *w, const uint32_t *v, const XTeaKeyBlock_t k)
  63. {
  64. register uint32_t y=v[0];
  65. register uint32_t z=v[1];
  66. register uint32_t sum=0;
  67. register uint32_t delta=XTDELTA;
  68. uint_fast8_t n=ROUNDS;
  69. while (n-- > 0) {
  70. y += (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum&3]);
  71. sum += delta;
  72. z += (((y << 4) ^ (y >> 5)) + y) ^ (sum + k[sum>>11 & 3]);
  73. }
  74. w[0]=y; w[1]=z;
  75. }
  76. /*!
  77. * \brief Decrypt a 64bit block with XTEA algorythm.
  78. *
  79. * \para w Destination pointer to 2x32 bit decrypted block.
  80. * \para v Source pointer of 2x32 bit encrypted block.
  81. * \para k Pointer to 128 bit (4*32bit) key block.
  82. */
  83. void XTeaDecrypt(uint32_t *w, const uint32_t *v, const XTeaKeyBlock_t k)
  84. {
  85. register uint32_t y=v[0];
  86. register uint32_t z=v[1];
  87. register uint32_t sum=XTSUM;
  88. register uint32_t delta=XTDELTA;
  89. uint_fast8_t n=32;
  90. while (n-- > 0) {
  91. z -= (((y << 4) ^ (y >> 5)) + y) ^ (sum + k[sum>>11 & 3]);
  92. sum -= delta;
  93. y -= (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum&3]);
  94. }
  95. w[0]=y; w[1]=z;
  96. }
  97. /*!
  98. * \brief Encrypt a string with XTEA algorythm.
  99. *
  100. * Respect that the strings or buffers of src and dst strings
  101. * have to have a length of multiple of 8 as it is a 64 bit based
  102. * algorhythm.
  103. *
  104. * \para dst Target buffer to place the encrypted string into.
  105. * \para src Source buffer to get the clear text string from.
  106. * \para len Length of the string to encrypt.
  107. * \para pass Buffer of 64 bytes used as passphrase.
  108. */
  109. void XTeaCryptStr( char *dst, const char *src, uint16_t len, const char *pass)
  110. {
  111. uint16_t l;
  112. uint16_t i;
  113. XTeaKeyBlock_t K = { 0,0,0,0};
  114. /* Prepare pass as XTEA Key Block */
  115. l = strlen(pass);
  116. if( l>sizeof( XTeaKeyBlock_t))
  117. l = sizeof( XTeaKeyBlock_t);
  118. memcpy( K, pass, l);
  119. i = 0; l = strlen( src);
  120. while (i<len) {
  121. XTeaCrypt( (uint32_t*)dst, (const uint32_t*)src, K);
  122. src+=8; dst+=8; i+=8;
  123. }
  124. }
  125. /*!
  126. * \brief Decrypt a string with XTEA algorythm.
  127. *
  128. * Respect that the strings or buffers of src and dst strings
  129. * have to have a length of multiple of 8 as it is a 64 bit based
  130. * algorhythm.
  131. *
  132. * \para dst Target buffer to place the decrypted string into.
  133. * \para src Source buffer to get the encrypted string from.
  134. * \para len Length of the string to decrypt.
  135. * \para pass Buffer of 64 bytes used as passphrase.
  136. */
  137. void XTeaDecryptStr( char * dst, const char *src, uint16_t len, const char *pass)
  138. {
  139. uint16_t l;
  140. uint16_t i;
  141. XTeaKeyBlock_t K = { 0,0,0,0};
  142. /* Prepare pass as XTEA Key Block */
  143. l = strlen(pass);
  144. if( l>sizeof( XTeaKeyBlock_t))
  145. l = sizeof( XTeaKeyBlock_t);
  146. memcpy( K, pass, l);
  147. i = 0; l = strlen( src);
  148. while (i<len) {
  149. XTeaDecrypt( (uint32_t*)dst, (uint32_t const*)src, K);
  150. src+=8; dst+=8; i+=8;
  151. }
  152. }