Blowfish.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. *
  3. * Blowfish.c : Blowfish implementation
  4. *
  5. * Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. *
  7. * =======================================================================
  8. * The contents of this file are dedicated to the public domain. To the extent
  9. * that dedication to the public domain is not available, everyone is granted a
  10. * worldwide, perpetual, royalty-free, non-exclusive license to exercise all
  11. * rights associated with the contents of this file for any purpose whatsoever.
  12. * No rights are reserved.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. * =======================================================================
  22. *
  23. * Country of origin: Canada
  24. *
  25. * The Blowfish algorithm is documented at
  26. * http://www.schneier.com/paper-blowfish-fse.html
  27. */
  28. #include "config.h"
  29. #if HAVE_STDINT_H
  30. # include <stdint.h>
  31. #elif defined(__sun) || defined(__sun__)
  32. # include <sys/inttypes.h>
  33. #else
  34. # error "stdint.h not found"
  35. #endif
  36. #include <assert.h>
  37. #include <string.h>
  38. #include "Python.h"
  39. #include "Blowfish-tables.h"
  40. #define MODULE_NAME _Blowfish
  41. #define BLOCK_SIZE 8 /* 64-bit block size */
  42. #define KEY_SIZE 0 /* variable key size */
  43. #define BLOWFISH_MAGIC 0xf9d565deu
  44. typedef struct {
  45. uint32_t magic;
  46. /* P permutation */
  47. uint32_t P[18];
  48. /* Subkeys (S-boxes) */
  49. uint32_t S1[256];
  50. uint32_t S2[256];
  51. uint32_t S3[256];
  52. uint32_t S4[256];
  53. } Blowfish_state;
  54. /* The Blowfish round function F. Everything is taken modulo 2**32 */
  55. #define F(a, b, c, d) (((a) + (b)) ^ (c)) + (d)
  56. static inline uint32_t bytes_to_word(const unsigned char *in)
  57. {
  58. /* big endian */
  59. return (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
  60. }
  61. static inline void word_to_bytes(uint32_t w, unsigned char *out)
  62. {
  63. /* big endian */
  64. out[0] = (w >> 24) & 0xff;
  65. out[1] = (w >> 16) & 0xff;
  66. out[2] = (w >> 8) & 0xff;
  67. out[3] = w & 0xff;
  68. }
  69. static inline void inline_encrypt(Blowfish_state *self, uint32_t *pxL, uint32_t *pxR)
  70. {
  71. int i;
  72. uint32_t xL = *pxL;
  73. uint32_t xR = *pxR;
  74. uint32_t tmp;
  75. for (i = 0; i < 16; i++) {
  76. xL ^= self->P[i];
  77. /* a || b || c || d = xL (big endian) */
  78. xR ^= F(self->S1[(xL >> 24) & 0xff], /* S1[a] */
  79. self->S2[(xL >> 16) & 0xff], /* S2[b] */
  80. self->S3[(xL >> 8) & 0xff], /* S3[c] */
  81. self->S4[xL & 0xff]); /* S4[d] */
  82. /* Swap xL, xR */
  83. tmp = xL; xL = xR; xR = tmp;
  84. }
  85. /* Swap xL, xR */
  86. tmp = xL; xL = xR; xR = tmp;
  87. xR ^= self->P[16];
  88. xL ^= self->P[17];
  89. *pxL = xL;
  90. *pxR = xR;
  91. }
  92. static inline void inline_decrypt(Blowfish_state *self, uint32_t *pxL, uint32_t *pxR)
  93. {
  94. int i;
  95. uint32_t xL = *pxL;
  96. uint32_t xR = *pxR;
  97. uint32_t tmp;
  98. xL ^= self->P[17];
  99. xR ^= self->P[16];
  100. /* Swap xL, xR */
  101. tmp = xL; xL = xR; xR = tmp;
  102. for (i = 15; i >= 0; i--) {
  103. /* Swap xL, xR */
  104. tmp = xL; xL = xR; xR = tmp;
  105. /* a || b || c || d = xL (big endian) */
  106. xR ^= F(self->S1[(xL >> 24) & 0xff], /* S1[a] */
  107. self->S2[(xL >> 16) & 0xff], /* S2[b] */
  108. self->S3[(xL >> 8) & 0xff], /* S3[c] */
  109. self->S4[xL & 0xff]); /* S4[d] */
  110. xL ^= self->P[i];
  111. }
  112. *pxL = xL;
  113. *pxR = xR;
  114. }
  115. static void Blowfish_encrypt(Blowfish_state *self, const unsigned char *in, unsigned char *out)
  116. {
  117. uint32_t xL, xR;
  118. /* Make sure the object is initialized */
  119. assert(self->magic == BLOWFISH_MAGIC);
  120. /* big endian */
  121. xL = bytes_to_word(in);
  122. xR = bytes_to_word(in+4);
  123. inline_encrypt(self, &xL, &xR);
  124. /* big endian */
  125. word_to_bytes(xL, out);
  126. word_to_bytes(xR, out+4);
  127. }
  128. static void Blowfish_decrypt(Blowfish_state *self, const unsigned char *in, unsigned char *out)
  129. {
  130. uint32_t xL, xR;
  131. /* Make sure the object is initialized */
  132. assert(self->magic == BLOWFISH_MAGIC);
  133. /* big endian */
  134. xL = bytes_to_word(in);
  135. xR = bytes_to_word(in+4);
  136. inline_decrypt(self, &xL, &xR);
  137. /* big endian */
  138. word_to_bytes(xL, out);
  139. word_to_bytes(xR, out+4);
  140. }
  141. static void Blowfish_init(Blowfish_state *self, const unsigned char *key, int keylen)
  142. {
  143. uint32_t word;
  144. int i;
  145. uint32_t xL, xR;
  146. self->magic = 0;
  147. if (keylen < 1) {
  148. PyErr_SetString(PyExc_ValueError, "Key cannot be empty");
  149. return;
  150. } else if (keylen > 56) {
  151. PyErr_SetString(PyExc_ValueError, "Maximum key size is 448 bits");
  152. return;
  153. }
  154. /* Initialize the P-array with the digits of Pi, and XOR it with the key */
  155. word = 0;
  156. for (i = 0; i < 18*4; i++) {
  157. word = (word << 8) | key[i % keylen];
  158. if ((i & 3) == 3) {
  159. self->P[i >> 2] = initial_P[i >> 2] ^ word;
  160. word = 0;
  161. }
  162. }
  163. /* Initialize the S-boxes with more digits of Pi */
  164. memcpy(self->S1, initial_S1, 256*sizeof(uint32_t));
  165. memcpy(self->S2, initial_S2, 256*sizeof(uint32_t));
  166. memcpy(self->S3, initial_S3, 256*sizeof(uint32_t));
  167. memcpy(self->S4, initial_S4, 256*sizeof(uint32_t));
  168. /* Stir the subkeys */
  169. xL = xR = 0;
  170. for (i = 0; i < 18; i += 2) {
  171. inline_encrypt(self, &xL, &xR);
  172. self->P[i] = xL;
  173. self->P[i+1] = xR;
  174. }
  175. for (i = 0; i < 256; i += 2) {
  176. inline_encrypt(self, &xL, &xR);
  177. self->S1[i] = xL;
  178. self->S1[i+1] = xR;
  179. }
  180. for (i = 0; i < 256; i += 2) {
  181. inline_encrypt(self, &xL, &xR);
  182. self->S2[i] = xL;
  183. self->S2[i+1] = xR;
  184. }
  185. for (i = 0; i < 256; i += 2) {
  186. inline_encrypt(self, &xL, &xR);
  187. self->S3[i] = xL;
  188. self->S3[i+1] = xR;
  189. }
  190. for (i = 0; i < 256; i += 2) {
  191. inline_encrypt(self, &xL, &xR);
  192. self->S4[i] = xL;
  193. self->S4[i+1] = xR;
  194. }
  195. self->magic = BLOWFISH_MAGIC;
  196. }
  197. #define block_state Blowfish_state
  198. #define block_init Blowfish_init
  199. #define block_encrypt Blowfish_encrypt
  200. #define block_decrypt Blowfish_decrypt
  201. #include "block_template.c"
  202. /* vim:set ts=4 sw=4 sts=4 expandtab: */