blowfish.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* ===================================================================
  2. *
  3. * Copyright (c) 2019, Helder Eijs <helderijs@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. * ===================================================================
  30. */
  31. #include "common.h"
  32. #include "endianess.h"
  33. #include "block_base.h"
  34. #include "blowfish_init.c"
  35. #ifdef EKS
  36. #define NON_STANDARD_START_OPERATION
  37. #define MODULE_NAME EKSBlowfish
  38. FAKE_INIT(raw_eksblowfish)
  39. #else
  40. #define MODULE_NAME Blowfish
  41. FAKE_INIT(raw_blowfish)
  42. #endif
  43. #define BLOCK_SIZE 8
  44. #define KEY_SIZE 0
  45. struct block_state {
  46. uint32_t S[4][256];
  47. uint32_t P[18];
  48. };
  49. static inline void swap(uint32_t *L, uint32_t *R)
  50. {
  51. uint32_t tmp;
  52. tmp = *L;
  53. *L = *R;
  54. *R = tmp;
  55. }
  56. static inline uint32_t F(const struct block_state *ctx, uint32_t x)
  57. {
  58. uint8_t a, b, c, d;
  59. uint32_t res;
  60. a = (x >> 24) & 0xFF;
  61. b = (x >> 16) & 0xFF;
  62. c = (x >> 8) & 0xFF;
  63. d = (x >> 0) & 0xFF;
  64. res = ctx->S[0][a] + ctx->S[1][b];
  65. res ^= ctx->S[2][c];
  66. res += ctx->S[3][d];
  67. return res;
  68. }
  69. static void bf_encrypt(const struct block_state *state, uint32_t *Lx, uint32_t *Rx)
  70. {
  71. unsigned i;
  72. uint32_t L, R;
  73. L = *Lx;
  74. R = *Rx;
  75. for (i=0; i<16; i++) {
  76. L ^= state->P[i];
  77. R ^= F(state, L);
  78. swap(&L, &R);
  79. }
  80. swap(&L, &R);
  81. R ^= state->P[16];
  82. L ^= state->P[17];
  83. *Lx = L;
  84. *Rx = R;
  85. }
  86. static void bf_decrypt(const struct block_state *state, uint32_t *Lx, uint32_t *Rx)
  87. {
  88. unsigned i;
  89. uint32_t L, R;
  90. L = *Lx;
  91. R = *Rx;
  92. L ^= state->P[17];
  93. R ^= state->P[16];
  94. swap(&L, &R);
  95. for (i=0; i<16; i++) {
  96. swap(&L, &R);
  97. R ^= F(state, L);
  98. L ^= state->P[15-i];
  99. }
  100. *Lx = L;
  101. *Rx = R;
  102. }
  103. static inline void xorP(uint32_t P[18], const uint8_t *key, size_t keylength)
  104. {
  105. uint8_t P_buf[4*18];
  106. size_t P_idx;
  107. unsigned i;
  108. P_idx = 0;
  109. while (P_idx < sizeof(P_buf)) {
  110. size_t tc;
  111. tc = MIN(keylength, sizeof(P_buf) - P_idx);
  112. memcpy(P_buf + P_idx, key, tc);
  113. P_idx += tc;
  114. }
  115. P_idx = 0;
  116. for (i=0; i<18; i++) {
  117. P[i] ^= LOAD_U32_BIG(P_buf + P_idx);
  118. P_idx += 4;
  119. }
  120. }
  121. static inline void encryptState(struct block_state *state, const uint8_t *key, size_t keylength)
  122. {
  123. unsigned i, j;
  124. uint32_t L, R;
  125. xorP(state->P, key, keylength);
  126. L = R = 0;
  127. for (i=0; i<18; i+=2) {
  128. bf_encrypt(state, &L, &R);
  129. state->P[i] = L;
  130. state->P[i+1] = R;
  131. }
  132. for (j=0; j<4; j++) {
  133. for (i=0; i<256; i+=2) {
  134. bf_encrypt(state, &L, &R);
  135. state->S[j][i] = L;
  136. state->S[j][i+1] = R;
  137. }
  138. }
  139. }
  140. #ifndef EKS
  141. static int block_init(struct block_state *state, const uint8_t *key, size_t keylength)
  142. {
  143. /* Allowed key length: 32 to 448 bits */
  144. if (keylength < 4 || keylength > 56) {
  145. return ERR_KEY_SIZE;
  146. }
  147. memcpy(state->S, S_init, sizeof S_init);
  148. memcpy(state->P, P_init, sizeof P_init);
  149. encryptState(state, key, keylength);
  150. return 0;
  151. }
  152. #else
  153. static inline uint32_t read_u32_circ(const uint8_t *base, size_t len, size_t *idx)
  154. {
  155. uint8_t buf[4];
  156. unsigned i;
  157. for (i=0; i<4; i++) {
  158. buf[i] = base[*idx];
  159. (*idx)++;
  160. if (len == *idx)
  161. *idx = 0;
  162. }
  163. return LOAD_U32_BIG(buf);
  164. }
  165. static int encryptStateWithSalt(struct block_state *state, const uint8_t *key, size_t keylength, const uint8_t *salt, size_t saltlength)
  166. {
  167. uint32_t L, R;
  168. unsigned i, j;
  169. size_t idx;
  170. xorP(state->P, key, keylength);
  171. L = R = 0;
  172. idx = 0;
  173. for (i=0; i<18; i+=2) {
  174. L ^= read_u32_circ(salt, saltlength, &idx);
  175. R ^= read_u32_circ(salt, saltlength, &idx);
  176. bf_encrypt(state, &L, &R);
  177. state->P[i] = L;
  178. state->P[i+1] = R;
  179. }
  180. for (j=0; j<4; j++) {
  181. for (i=0; i<256; i+=2) {
  182. L ^= read_u32_circ(salt, saltlength, &idx);
  183. R ^= read_u32_circ(salt, saltlength, &idx);
  184. bf_encrypt(state, &L, &R);
  185. state->S[j][i] = L;
  186. state->S[j][i+1] = R;
  187. }
  188. }
  189. return 0;
  190. }
  191. static int block_init(struct block_state *state, const uint8_t *key, size_t keylength, const uint8_t *salt, size_t saltlength, unsigned cost, unsigned invert)
  192. {
  193. unsigned i;
  194. unsigned iterations;
  195. if (keylength > 72) {
  196. return ERR_KEY_SIZE;
  197. }
  198. /* InitState */
  199. memcpy(state->S, S_init, sizeof S_init);
  200. memcpy(state->P, P_init, sizeof P_init);
  201. encryptStateWithSalt(state, key, keylength, salt, saltlength);
  202. iterations = 1U << cost;
  203. if (!invert) {
  204. for (i=0; i<iterations; i++) {
  205. encryptState(state, salt, saltlength);
  206. encryptState(state, key, keylength);
  207. }
  208. } else {
  209. for (i=0; i<iterations; i++) {
  210. encryptState(state, key, keylength);
  211. encryptState(state, salt, saltlength);
  212. }
  213. }
  214. return 0;
  215. }
  216. #endif
  217. static void block_finalize(struct block_state* state)
  218. {
  219. }
  220. static inline void block_encrypt(struct block_state *state, const uint8_t *in, uint8_t *out)
  221. {
  222. uint32_t L, R;
  223. L = LOAD_U32_BIG(in);
  224. R = LOAD_U32_BIG(in + 4);
  225. bf_encrypt(state, &L, &R);
  226. STORE_U32_BIG(out, L);
  227. STORE_U32_BIG(out + 4, R);
  228. }
  229. static inline void block_decrypt(struct block_state *state, const uint8_t *in, uint8_t *out)
  230. {
  231. uint32_t L, R;
  232. L = LOAD_U32_BIG(in);
  233. R = LOAD_U32_BIG(in + 4);
  234. bf_decrypt(state, &L, &R);
  235. STORE_U32_BIG(out, L);
  236. STORE_U32_BIG(out + 4, R);
  237. }
  238. #include "block_common.c"
  239. #ifdef EKS
  240. EXPORT_SYM int CIPHER_START_OPERATION(const uint8_t key[], size_t key_len, const uint8_t salt[], size_t salt_len, unsigned cost, unsigned invert, CIPHER_STATE_TYPE **pResult)
  241. {
  242. BlockBase *block_base;
  243. if ((key == NULL) || (salt == NULL) || (pResult == NULL))
  244. return ERR_NULL;
  245. *pResult = calloc(1, sizeof(CIPHER_STATE_TYPE));
  246. if (NULL == *pResult)
  247. return ERR_MEMORY;
  248. block_base = &((*pResult)->base_state);
  249. block_base->encrypt = &CIPHER_ENCRYPT;
  250. block_base->decrypt = &CIPHER_DECRYPT;
  251. block_base->destructor = &CIPHER_STOP_OPERATION;
  252. block_base->block_len = BLOCK_SIZE;
  253. return block_init(&(*pResult)->algo_state, (unsigned char*)key, key_len, salt, salt_len, cost, invert);
  254. }
  255. #endif