gf128mul.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* gf128mul.c - GF(2^128) multiplication functions
  2. *
  3. * Copyright (c) 2003, Dr Brian Gladman, Worcester, UK.
  4. * Copyright (c) 2006, Rik Snel <rsnel@cube.dyndns.org>
  5. *
  6. * Based on Dr Brian Gladman's (GPL'd) work published at
  7. * http://gladman.plushost.co.uk/oldsite/cryptography_technology/index.php
  8. * See the original copyright notice below.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. */
  15. /*
  16. ---------------------------------------------------------------------------
  17. Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved.
  18. LICENSE TERMS
  19. The free distribution and use of this software in both source and binary
  20. form is allowed (with or without changes) provided that:
  21. 1. distributions of this source code include the above copyright
  22. notice, this list of conditions and the following disclaimer;
  23. 2. distributions in binary form include the above copyright
  24. notice, this list of conditions and the following disclaimer
  25. in the documentation and/or other associated materials;
  26. 3. the copyright holder's name is not used to endorse products
  27. built using this software without specific written permission.
  28. ALTERNATIVELY, provided that this notice is retained in full, this product
  29. may be distributed under the terms of the GNU General Public License (GPL),
  30. in which case the provisions of the GPL apply INSTEAD OF those given above.
  31. DISCLAIMER
  32. This software is provided 'as is' with no explicit or implied warranties
  33. in respect of its properties, including, but not limited to, correctness
  34. and/or fitness for purpose.
  35. ---------------------------------------------------------------------------
  36. Issue 31/01/2006
  37. This file provides fast multiplication in GF(128) as required by several
  38. cryptographic authentication modes
  39. */
  40. #include <crypto/gf128mul.h>
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/slab.h>
  44. #define gf128mul_dat(q) { \
  45. q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\
  46. q(0x08), q(0x09), q(0x0a), q(0x0b), q(0x0c), q(0x0d), q(0x0e), q(0x0f),\
  47. q(0x10), q(0x11), q(0x12), q(0x13), q(0x14), q(0x15), q(0x16), q(0x17),\
  48. q(0x18), q(0x19), q(0x1a), q(0x1b), q(0x1c), q(0x1d), q(0x1e), q(0x1f),\
  49. q(0x20), q(0x21), q(0x22), q(0x23), q(0x24), q(0x25), q(0x26), q(0x27),\
  50. q(0x28), q(0x29), q(0x2a), q(0x2b), q(0x2c), q(0x2d), q(0x2e), q(0x2f),\
  51. q(0x30), q(0x31), q(0x32), q(0x33), q(0x34), q(0x35), q(0x36), q(0x37),\
  52. q(0x38), q(0x39), q(0x3a), q(0x3b), q(0x3c), q(0x3d), q(0x3e), q(0x3f),\
  53. q(0x40), q(0x41), q(0x42), q(0x43), q(0x44), q(0x45), q(0x46), q(0x47),\
  54. q(0x48), q(0x49), q(0x4a), q(0x4b), q(0x4c), q(0x4d), q(0x4e), q(0x4f),\
  55. q(0x50), q(0x51), q(0x52), q(0x53), q(0x54), q(0x55), q(0x56), q(0x57),\
  56. q(0x58), q(0x59), q(0x5a), q(0x5b), q(0x5c), q(0x5d), q(0x5e), q(0x5f),\
  57. q(0x60), q(0x61), q(0x62), q(0x63), q(0x64), q(0x65), q(0x66), q(0x67),\
  58. q(0x68), q(0x69), q(0x6a), q(0x6b), q(0x6c), q(0x6d), q(0x6e), q(0x6f),\
  59. q(0x70), q(0x71), q(0x72), q(0x73), q(0x74), q(0x75), q(0x76), q(0x77),\
  60. q(0x78), q(0x79), q(0x7a), q(0x7b), q(0x7c), q(0x7d), q(0x7e), q(0x7f),\
  61. q(0x80), q(0x81), q(0x82), q(0x83), q(0x84), q(0x85), q(0x86), q(0x87),\
  62. q(0x88), q(0x89), q(0x8a), q(0x8b), q(0x8c), q(0x8d), q(0x8e), q(0x8f),\
  63. q(0x90), q(0x91), q(0x92), q(0x93), q(0x94), q(0x95), q(0x96), q(0x97),\
  64. q(0x98), q(0x99), q(0x9a), q(0x9b), q(0x9c), q(0x9d), q(0x9e), q(0x9f),\
  65. q(0xa0), q(0xa1), q(0xa2), q(0xa3), q(0xa4), q(0xa5), q(0xa6), q(0xa7),\
  66. q(0xa8), q(0xa9), q(0xaa), q(0xab), q(0xac), q(0xad), q(0xae), q(0xaf),\
  67. q(0xb0), q(0xb1), q(0xb2), q(0xb3), q(0xb4), q(0xb5), q(0xb6), q(0xb7),\
  68. q(0xb8), q(0xb9), q(0xba), q(0xbb), q(0xbc), q(0xbd), q(0xbe), q(0xbf),\
  69. q(0xc0), q(0xc1), q(0xc2), q(0xc3), q(0xc4), q(0xc5), q(0xc6), q(0xc7),\
  70. q(0xc8), q(0xc9), q(0xca), q(0xcb), q(0xcc), q(0xcd), q(0xce), q(0xcf),\
  71. q(0xd0), q(0xd1), q(0xd2), q(0xd3), q(0xd4), q(0xd5), q(0xd6), q(0xd7),\
  72. q(0xd8), q(0xd9), q(0xda), q(0xdb), q(0xdc), q(0xdd), q(0xde), q(0xdf),\
  73. q(0xe0), q(0xe1), q(0xe2), q(0xe3), q(0xe4), q(0xe5), q(0xe6), q(0xe7),\
  74. q(0xe8), q(0xe9), q(0xea), q(0xeb), q(0xec), q(0xed), q(0xee), q(0xef),\
  75. q(0xf0), q(0xf1), q(0xf2), q(0xf3), q(0xf4), q(0xf5), q(0xf6), q(0xf7),\
  76. q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) \
  77. }
  78. /* Given the value i in 0..255 as the byte overflow when a field element
  79. in GHASH is multiplied by x^8, this function will return the values that
  80. are generated in the lo 16-bit word of the field value by applying the
  81. modular polynomial. The values lo_byte and hi_byte are returned via the
  82. macro xp_fun(lo_byte, hi_byte) so that the values can be assembled into
  83. memory as required by a suitable definition of this macro operating on
  84. the table above
  85. */
  86. #define xx(p, q) 0x##p##q
  87. #define xda_bbe(i) ( \
  88. (i & 0x80 ? xx(43, 80) : 0) ^ (i & 0x40 ? xx(21, c0) : 0) ^ \
  89. (i & 0x20 ? xx(10, e0) : 0) ^ (i & 0x10 ? xx(08, 70) : 0) ^ \
  90. (i & 0x08 ? xx(04, 38) : 0) ^ (i & 0x04 ? xx(02, 1c) : 0) ^ \
  91. (i & 0x02 ? xx(01, 0e) : 0) ^ (i & 0x01 ? xx(00, 87) : 0) \
  92. )
  93. #define xda_lle(i) ( \
  94. (i & 0x80 ? xx(e1, 00) : 0) ^ (i & 0x40 ? xx(70, 80) : 0) ^ \
  95. (i & 0x20 ? xx(38, 40) : 0) ^ (i & 0x10 ? xx(1c, 20) : 0) ^ \
  96. (i & 0x08 ? xx(0e, 10) : 0) ^ (i & 0x04 ? xx(07, 08) : 0) ^ \
  97. (i & 0x02 ? xx(03, 84) : 0) ^ (i & 0x01 ? xx(01, c2) : 0) \
  98. )
  99. static const u16 gf128mul_table_lle[256] = gf128mul_dat(xda_lle);
  100. static const u16 gf128mul_table_bbe[256] = gf128mul_dat(xda_bbe);
  101. /* These functions multiply a field element by x, by x^4 and by x^8
  102. * in the polynomial field representation. It uses 32-bit word operations
  103. * to gain speed but compensates for machine endianess and hence works
  104. * correctly on both styles of machine.
  105. */
  106. static void gf128mul_x_lle(be128 *r, const be128 *x)
  107. {
  108. u64 a = be64_to_cpu(x->a);
  109. u64 b = be64_to_cpu(x->b);
  110. u64 _tt = gf128mul_table_lle[(b << 7) & 0xff];
  111. r->b = cpu_to_be64((b >> 1) | (a << 63));
  112. r->a = cpu_to_be64((a >> 1) ^ (_tt << 48));
  113. }
  114. static void gf128mul_x_bbe(be128 *r, const be128 *x)
  115. {
  116. u64 a = be64_to_cpu(x->a);
  117. u64 b = be64_to_cpu(x->b);
  118. u64 _tt = gf128mul_table_bbe[a >> 63];
  119. r->a = cpu_to_be64((a << 1) | (b >> 63));
  120. r->b = cpu_to_be64((b << 1) ^ _tt);
  121. }
  122. void gf128mul_x_ble(be128 *r, const be128 *x)
  123. {
  124. u64 a = le64_to_cpu(x->a);
  125. u64 b = le64_to_cpu(x->b);
  126. u64 _tt = gf128mul_table_bbe[b >> 63];
  127. r->a = cpu_to_le64((a << 1) ^ _tt);
  128. r->b = cpu_to_le64((b << 1) | (a >> 63));
  129. }
  130. EXPORT_SYMBOL(gf128mul_x_ble);
  131. static void gf128mul_x8_lle(be128 *x)
  132. {
  133. u64 a = be64_to_cpu(x->a);
  134. u64 b = be64_to_cpu(x->b);
  135. u64 _tt = gf128mul_table_lle[b & 0xff];
  136. x->b = cpu_to_be64((b >> 8) | (a << 56));
  137. x->a = cpu_to_be64((a >> 8) ^ (_tt << 48));
  138. }
  139. static void gf128mul_x8_bbe(be128 *x)
  140. {
  141. u64 a = be64_to_cpu(x->a);
  142. u64 b = be64_to_cpu(x->b);
  143. u64 _tt = gf128mul_table_bbe[a >> 56];
  144. x->a = cpu_to_be64((a << 8) | (b >> 56));
  145. x->b = cpu_to_be64((b << 8) ^ _tt);
  146. }
  147. void gf128mul_lle(be128 *r, const be128 *b)
  148. {
  149. be128 p[8];
  150. int i;
  151. p[0] = *r;
  152. for (i = 0; i < 7; ++i)
  153. gf128mul_x_lle(&p[i + 1], &p[i]);
  154. memset(r, 0, sizeof(*r));
  155. for (i = 0;;) {
  156. u8 ch = ((u8 *)b)[15 - i];
  157. if (ch & 0x80)
  158. be128_xor(r, r, &p[0]);
  159. if (ch & 0x40)
  160. be128_xor(r, r, &p[1]);
  161. if (ch & 0x20)
  162. be128_xor(r, r, &p[2]);
  163. if (ch & 0x10)
  164. be128_xor(r, r, &p[3]);
  165. if (ch & 0x08)
  166. be128_xor(r, r, &p[4]);
  167. if (ch & 0x04)
  168. be128_xor(r, r, &p[5]);
  169. if (ch & 0x02)
  170. be128_xor(r, r, &p[6]);
  171. if (ch & 0x01)
  172. be128_xor(r, r, &p[7]);
  173. if (++i >= 16)
  174. break;
  175. gf128mul_x8_lle(r);
  176. }
  177. }
  178. EXPORT_SYMBOL(gf128mul_lle);
  179. void gf128mul_bbe(be128 *r, const be128 *b)
  180. {
  181. be128 p[8];
  182. int i;
  183. p[0] = *r;
  184. for (i = 0; i < 7; ++i)
  185. gf128mul_x_bbe(&p[i + 1], &p[i]);
  186. memset(r, 0, sizeof(*r));
  187. for (i = 0;;) {
  188. u8 ch = ((u8 *)b)[i];
  189. if (ch & 0x80)
  190. be128_xor(r, r, &p[7]);
  191. if (ch & 0x40)
  192. be128_xor(r, r, &p[6]);
  193. if (ch & 0x20)
  194. be128_xor(r, r, &p[5]);
  195. if (ch & 0x10)
  196. be128_xor(r, r, &p[4]);
  197. if (ch & 0x08)
  198. be128_xor(r, r, &p[3]);
  199. if (ch & 0x04)
  200. be128_xor(r, r, &p[2]);
  201. if (ch & 0x02)
  202. be128_xor(r, r, &p[1]);
  203. if (ch & 0x01)
  204. be128_xor(r, r, &p[0]);
  205. if (++i >= 16)
  206. break;
  207. gf128mul_x8_bbe(r);
  208. }
  209. }
  210. EXPORT_SYMBOL(gf128mul_bbe);
  211. /* This version uses 64k bytes of table space.
  212. A 16 byte buffer has to be multiplied by a 16 byte key
  213. value in GF(128). If we consider a GF(128) value in
  214. the buffer's lowest byte, we can construct a table of
  215. the 256 16 byte values that result from the 256 values
  216. of this byte. This requires 4096 bytes. But we also
  217. need tables for each of the 16 higher bytes in the
  218. buffer as well, which makes 64 kbytes in total.
  219. */
  220. /* additional explanation
  221. * t[0][BYTE] contains g*BYTE
  222. * t[1][BYTE] contains g*x^8*BYTE
  223. * ..
  224. * t[15][BYTE] contains g*x^120*BYTE */
  225. struct gf128mul_64k *gf128mul_init_64k_lle(const be128 *g)
  226. {
  227. struct gf128mul_64k *t;
  228. int i, j, k;
  229. t = kzalloc(sizeof(*t), GFP_KERNEL);
  230. if (!t)
  231. goto out;
  232. for (i = 0; i < 16; i++) {
  233. t->t[i] = kzalloc(sizeof(*t->t[i]), GFP_KERNEL);
  234. if (!t->t[i]) {
  235. gf128mul_free_64k(t);
  236. t = NULL;
  237. goto out;
  238. }
  239. }
  240. t->t[0]->t[128] = *g;
  241. for (j = 64; j > 0; j >>= 1)
  242. gf128mul_x_lle(&t->t[0]->t[j], &t->t[0]->t[j + j]);
  243. for (i = 0;;) {
  244. for (j = 2; j < 256; j += j)
  245. for (k = 1; k < j; ++k)
  246. be128_xor(&t->t[i]->t[j + k],
  247. &t->t[i]->t[j], &t->t[i]->t[k]);
  248. if (++i >= 16)
  249. break;
  250. for (j = 128; j > 0; j >>= 1) {
  251. t->t[i]->t[j] = t->t[i - 1]->t[j];
  252. gf128mul_x8_lle(&t->t[i]->t[j]);
  253. }
  254. }
  255. out:
  256. return t;
  257. }
  258. EXPORT_SYMBOL(gf128mul_init_64k_lle);
  259. struct gf128mul_64k *gf128mul_init_64k_bbe(const be128 *g)
  260. {
  261. struct gf128mul_64k *t;
  262. int i, j, k;
  263. t = kzalloc(sizeof(*t), GFP_KERNEL);
  264. if (!t)
  265. goto out;
  266. for (i = 0; i < 16; i++) {
  267. t->t[i] = kzalloc(sizeof(*t->t[i]), GFP_KERNEL);
  268. if (!t->t[i]) {
  269. gf128mul_free_64k(t);
  270. t = NULL;
  271. goto out;
  272. }
  273. }
  274. t->t[0]->t[1] = *g;
  275. for (j = 1; j <= 64; j <<= 1)
  276. gf128mul_x_bbe(&t->t[0]->t[j + j], &t->t[0]->t[j]);
  277. for (i = 0;;) {
  278. for (j = 2; j < 256; j += j)
  279. for (k = 1; k < j; ++k)
  280. be128_xor(&t->t[i]->t[j + k],
  281. &t->t[i]->t[j], &t->t[i]->t[k]);
  282. if (++i >= 16)
  283. break;
  284. for (j = 128; j > 0; j >>= 1) {
  285. t->t[i]->t[j] = t->t[i - 1]->t[j];
  286. gf128mul_x8_bbe(&t->t[i]->t[j]);
  287. }
  288. }
  289. out:
  290. return t;
  291. }
  292. EXPORT_SYMBOL(gf128mul_init_64k_bbe);
  293. void gf128mul_free_64k(struct gf128mul_64k *t)
  294. {
  295. int i;
  296. for (i = 0; i < 16; i++)
  297. kfree(t->t[i]);
  298. kfree(t);
  299. }
  300. EXPORT_SYMBOL(gf128mul_free_64k);
  301. void gf128mul_64k_lle(be128 *a, struct gf128mul_64k *t)
  302. {
  303. u8 *ap = (u8 *)a;
  304. be128 r[1];
  305. int i;
  306. *r = t->t[0]->t[ap[0]];
  307. for (i = 1; i < 16; ++i)
  308. be128_xor(r, r, &t->t[i]->t[ap[i]]);
  309. *a = *r;
  310. }
  311. EXPORT_SYMBOL(gf128mul_64k_lle);
  312. void gf128mul_64k_bbe(be128 *a, struct gf128mul_64k *t)
  313. {
  314. u8 *ap = (u8 *)a;
  315. be128 r[1];
  316. int i;
  317. *r = t->t[0]->t[ap[15]];
  318. for (i = 1; i < 16; ++i)
  319. be128_xor(r, r, &t->t[i]->t[ap[15 - i]]);
  320. *a = *r;
  321. }
  322. EXPORT_SYMBOL(gf128mul_64k_bbe);
  323. /* This version uses 4k bytes of table space.
  324. A 16 byte buffer has to be multiplied by a 16 byte key
  325. value in GF(128). If we consider a GF(128) value in a
  326. single byte, we can construct a table of the 256 16 byte
  327. values that result from the 256 values of this byte.
  328. This requires 4096 bytes. If we take the highest byte in
  329. the buffer and use this table to get the result, we then
  330. have to multiply by x^120 to get the final value. For the
  331. next highest byte the result has to be multiplied by x^112
  332. and so on. But we can do this by accumulating the result
  333. in an accumulator starting with the result for the top
  334. byte. We repeatedly multiply the accumulator value by
  335. x^8 and then add in (i.e. xor) the 16 bytes of the next
  336. lower byte in the buffer, stopping when we reach the
  337. lowest byte. This requires a 4096 byte table.
  338. */
  339. struct gf128mul_4k *gf128mul_init_4k_lle(const be128 *g)
  340. {
  341. struct gf128mul_4k *t;
  342. int j, k;
  343. t = kzalloc(sizeof(*t), GFP_KERNEL);
  344. if (!t)
  345. goto out;
  346. t->t[128] = *g;
  347. for (j = 64; j > 0; j >>= 1)
  348. gf128mul_x_lle(&t->t[j], &t->t[j+j]);
  349. for (j = 2; j < 256; j += j)
  350. for (k = 1; k < j; ++k)
  351. be128_xor(&t->t[j + k], &t->t[j], &t->t[k]);
  352. out:
  353. return t;
  354. }
  355. EXPORT_SYMBOL(gf128mul_init_4k_lle);
  356. struct gf128mul_4k *gf128mul_init_4k_bbe(const be128 *g)
  357. {
  358. struct gf128mul_4k *t;
  359. int j, k;
  360. t = kzalloc(sizeof(*t), GFP_KERNEL);
  361. if (!t)
  362. goto out;
  363. t->t[1] = *g;
  364. for (j = 1; j <= 64; j <<= 1)
  365. gf128mul_x_bbe(&t->t[j + j], &t->t[j]);
  366. for (j = 2; j < 256; j += j)
  367. for (k = 1; k < j; ++k)
  368. be128_xor(&t->t[j + k], &t->t[j], &t->t[k]);
  369. out:
  370. return t;
  371. }
  372. EXPORT_SYMBOL(gf128mul_init_4k_bbe);
  373. void gf128mul_4k_lle(be128 *a, struct gf128mul_4k *t)
  374. {
  375. u8 *ap = (u8 *)a;
  376. be128 r[1];
  377. int i = 15;
  378. *r = t->t[ap[15]];
  379. while (i--) {
  380. gf128mul_x8_lle(r);
  381. be128_xor(r, r, &t->t[ap[i]]);
  382. }
  383. *a = *r;
  384. }
  385. EXPORT_SYMBOL(gf128mul_4k_lle);
  386. void gf128mul_4k_bbe(be128 *a, struct gf128mul_4k *t)
  387. {
  388. u8 *ap = (u8 *)a;
  389. be128 r[1];
  390. int i = 0;
  391. *r = t->t[ap[0]];
  392. while (++i < 16) {
  393. gf128mul_x8_bbe(r);
  394. be128_xor(r, r, &t->t[ap[i]]);
  395. }
  396. *a = *r;
  397. }
  398. EXPORT_SYMBOL(gf128mul_4k_bbe);
  399. MODULE_LICENSE("GPL");
  400. MODULE_DESCRIPTION("Functions for multiplying elements of GF(2^128)");