Blowfish.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "pycrypto_common.h"
  29. #include "block_base.h"
  30. FAKE_INIT(raw_blowfish)
  31. #include "Blowfish-tables.h"
  32. #define MODULE_NAME Blowfish
  33. #define BLOCK_SIZE 8 /* 64-bit block size */
  34. #define KEY_SIZE 0 /* variable key size */
  35. #define BLOWFISH_MAGIC 0xf9d565deu
  36. typedef struct {
  37. uint32_t magic;
  38. /* P permutation */
  39. uint32_t P[18];
  40. /* Subkeys (S-boxes) */
  41. uint32_t S1[256];
  42. uint32_t S2[256];
  43. uint32_t S3[256];
  44. uint32_t S4[256];
  45. } block_state;
  46. /* The Blowfish round function F. Everything is taken modulo 2**32 */
  47. #define F(a, b, c, d) (((a) + (b)) ^ (c)) + (d)
  48. static uint32_t bytes_to_word(const unsigned char *in)
  49. {
  50. /* big endian */
  51. return (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
  52. }
  53. static void word_to_bytes(uint32_t w, unsigned char *out)
  54. {
  55. /* big endian */
  56. out[0] = (w >> 24) & 0xff;
  57. out[1] = (w >> 16) & 0xff;
  58. out[2] = (w >> 8) & 0xff;
  59. out[3] = w & 0xff;
  60. }
  61. static void inline_encrypt(block_state *self, uint32_t *pxL, uint32_t *pxR)
  62. {
  63. int i;
  64. uint32_t xL = *pxL;
  65. uint32_t xR = *pxR;
  66. uint32_t tmp;
  67. for (i = 0; i < 16; i++) {
  68. xL ^= self->P[i];
  69. /* a || b || c || d = xL (big endian) */
  70. xR ^= F(self->S1[(xL >> 24) & 0xff], /* S1[a] */
  71. self->S2[(xL >> 16) & 0xff], /* S2[b] */
  72. self->S3[(xL >> 8) & 0xff], /* S3[c] */
  73. self->S4[xL & 0xff]); /* S4[d] */
  74. /* Swap xL, xR */
  75. tmp = xL; xL = xR; xR = tmp;
  76. }
  77. /* Swap xL, xR */
  78. tmp = xL; xL = xR; xR = tmp;
  79. xR ^= self->P[16];
  80. xL ^= self->P[17];
  81. *pxL = xL;
  82. *pxR = xR;
  83. }
  84. static void inline_decrypt(block_state *self, uint32_t *pxL, uint32_t *pxR)
  85. {
  86. int i;
  87. uint32_t xL = *pxL;
  88. uint32_t xR = *pxR;
  89. uint32_t tmp;
  90. xL ^= self->P[17];
  91. xR ^= self->P[16];
  92. /* Swap xL, xR */
  93. tmp = xL; xL = xR; xR = tmp;
  94. for (i = 15; i >= 0; i--) {
  95. /* Swap xL, xR */
  96. tmp = xL; xL = xR; xR = tmp;
  97. /* a || b || c || d = xL (big endian) */
  98. xR ^= F(self->S1[(xL >> 24) & 0xff], /* S1[a] */
  99. self->S2[(xL >> 16) & 0xff], /* S2[b] */
  100. self->S3[(xL >> 8) & 0xff], /* S3[c] */
  101. self->S4[xL & 0xff]); /* S4[d] */
  102. xL ^= self->P[i];
  103. }
  104. *pxL = xL;
  105. *pxR = xR;
  106. }
  107. static void block_encrypt(block_state *self, const unsigned char *in, unsigned char *out)
  108. {
  109. uint32_t xL, xR;
  110. /* big endian */
  111. xL = bytes_to_word(in);
  112. xR = bytes_to_word(in+4);
  113. inline_encrypt(self, &xL, &xR);
  114. /* big endian */
  115. word_to_bytes(xL, out);
  116. word_to_bytes(xR, out+4);
  117. }
  118. static void block_decrypt(block_state *self, const unsigned char *in, unsigned char *out)
  119. {
  120. uint32_t xL, xR;
  121. /* big endian */
  122. xL = bytes_to_word(in);
  123. xR = bytes_to_word(in+4);
  124. inline_decrypt(self, &xL, &xR);
  125. /* big endian */
  126. word_to_bytes(xL, out);
  127. word_to_bytes(xR, out+4);
  128. }
  129. static int block_init(block_state *self, const unsigned char *key, int keylen)
  130. {
  131. uint32_t word;
  132. int i;
  133. uint32_t xL, xR;
  134. self->magic = 0;
  135. if (keylen < 1) {
  136. return ERR_KEY_SIZE;
  137. } else if (keylen > 56) {
  138. return ERR_KEY_SIZE;
  139. }
  140. /* Initialize the P-array with the digits of Pi, and XOR it with the key */
  141. word = 0;
  142. for (i = 0; i < 18*4; i++) {
  143. word = (word << 8) | key[i % keylen];
  144. if ((i & 3) == 3) {
  145. self->P[i >> 2] = initial_P[i >> 2] ^ word;
  146. word = 0;
  147. }
  148. }
  149. /* Initialize the S-boxes with more digits of Pi */
  150. memcpy(self->S1, initial_S1, 256*sizeof(uint32_t));
  151. memcpy(self->S2, initial_S2, 256*sizeof(uint32_t));
  152. memcpy(self->S3, initial_S3, 256*sizeof(uint32_t));
  153. memcpy(self->S4, initial_S4, 256*sizeof(uint32_t));
  154. /* Stir the subkeys */
  155. xL = xR = 0;
  156. for (i = 0; i < 18; i += 2) {
  157. inline_encrypt(self, &xL, &xR);
  158. self->P[i] = xL;
  159. self->P[i+1] = xR;
  160. }
  161. for (i = 0; i < 256; i += 2) {
  162. inline_encrypt(self, &xL, &xR);
  163. self->S1[i] = xL;
  164. self->S1[i+1] = xR;
  165. }
  166. for (i = 0; i < 256; i += 2) {
  167. inline_encrypt(self, &xL, &xR);
  168. self->S2[i] = xL;
  169. self->S2[i+1] = xR;
  170. }
  171. for (i = 0; i < 256; i += 2) {
  172. inline_encrypt(self, &xL, &xR);
  173. self->S3[i] = xL;
  174. self->S3[i+1] = xR;
  175. }
  176. for (i = 0; i < 256; i += 2) {
  177. inline_encrypt(self, &xL, &xR);
  178. self->S4[i] = xL;
  179. self->S4[i+1] = xR;
  180. }
  181. self->magic = BLOWFISH_MAGIC;
  182. return 0;
  183. }
  184. static void block_finalize(block_state *self)
  185. {
  186. }
  187. #include "block_common.c"
  188. /* vim:set ts=4 sw=4 sts=4 expandtab: */