3
0

tls_symmetric.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (C) 2017 Denys Vlasenko
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. /* The part below is a section of matrixssl-3-7-2b-open/crypto/cryptolib.h
  7. * Changes are flagged with //bbox
  8. */
  9. /******************************************************************************/
  10. /* 32-bit Rotates */
  11. /******************************************************************************/
  12. #if defined(_MSC_VER)
  13. /******************************************************************************/
  14. /* instrinsic rotate */
  15. #include <stdlib.h>
  16. #pragma intrinsic(_lrotr,_lrotl)
  17. #define ROR(x,n) _lrotr(x,n)
  18. #define ROL(x,n) _lrotl(x,n)
  19. /******************************************************************************/
  20. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && \
  21. !defined(INTEL_CC) && !defined(PS_NO_ASM)
  22. static ALWAYS_INLINE unsigned ROL(unsigned word, int i)
  23. {
  24. if (__builtin_constant_p(i)) { //box
  25. // Rotates by constant use fewer registers,
  26. // and on many Intel CPUs rotates by %cl take 2 cycles, not 1.
  27. asm ("roll %2,%0"
  28. :"=r" (word)
  29. :"0" (word),"i" (i));
  30. return word;
  31. } //box
  32. asm ("roll %%cl,%0"
  33. :"=r" (word)
  34. :"0" (word),"c" (i));
  35. return word;
  36. }
  37. static ALWAYS_INLINE unsigned ROR(unsigned word, int i)
  38. {
  39. if (__builtin_constant_p(i)) { //box
  40. asm ("rorl %2,%0"
  41. :"=r" (word)
  42. :"0" (word),"i" (i));
  43. return word;
  44. } //box
  45. asm ("rorl %%cl,%0"
  46. :"=r" (word)
  47. :"0" (word),"c" (i));
  48. return word;
  49. }
  50. /******************************************************************************/
  51. #else
  52. /* rotates the hard way */
  53. #define ROL(x, y) \
  54. ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | \
  55. (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & \
  56. 0xFFFFFFFFUL)
  57. #define ROR(x, y) \
  58. ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
  59. ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
  60. #endif /* 32-bit Rotates */
  61. /******************************************************************************/
  62. #ifdef HAVE_NATIVE_INT64
  63. #ifdef _MSC_VER
  64. #define CONST64(n) n ## ui64
  65. #else
  66. #define CONST64(n) n ## ULL
  67. #endif
  68. #endif
  69. /******************************************************************************/
  70. /*
  71. Endian helper macros
  72. */
  73. #if defined (ENDIAN_NEUTRAL)
  74. #define STORE32L(x, y) { \
  75. (y)[3] = (unsigned char)(((x)>>24)&255); \
  76. (y)[2] = (unsigned char)(((x)>>16)&255); \
  77. (y)[1] = (unsigned char)(((x)>>8)&255); \
  78. (y)[0] = (unsigned char)((x)&255); \
  79. }
  80. #define LOAD32L(x, y) { \
  81. x = ((unsigned long)((y)[3] & 255)<<24) | \
  82. ((unsigned long)((y)[2] & 255)<<16) | \
  83. ((unsigned long)((y)[1] & 255)<<8) | \
  84. ((unsigned long)((y)[0] & 255)); \
  85. }
  86. #define STORE64L(x, y) { \
  87. (y)[7] = (unsigned char)(((x)>>56)&255); \
  88. (y)[6] = (unsigned char)(((x)>>48)&255); \
  89. (y)[5] = (unsigned char)(((x)>>40)&255); \
  90. (y)[4] = (unsigned char)(((x)>>32)&255); \
  91. (y)[3] = (unsigned char)(((x)>>24)&255); \
  92. (y)[2] = (unsigned char)(((x)>>16)&255); \
  93. (y)[1] = (unsigned char)(((x)>>8)&255); \
  94. (y)[0] = (unsigned char)((x)&255); \
  95. }
  96. #define LOAD64L(x, y) { \
  97. x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
  98. (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
  99. (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
  100. (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
  101. }
  102. #define STORE32H(x, y) { \
  103. (y)[0] = (unsigned char)(((x)>>24)&255); \
  104. (y)[1] = (unsigned char)(((x)>>16)&255); \
  105. (y)[2] = (unsigned char)(((x)>>8)&255); \
  106. (y)[3] = (unsigned char)((x)&255); \
  107. }
  108. #define LOAD32H(x, y) { \
  109. x = ((unsigned long)((y)[0] & 255)<<24) | \
  110. ((unsigned long)((y)[1] & 255)<<16) | \
  111. ((unsigned long)((y)[2] & 255)<<8) | \
  112. ((unsigned long)((y)[3] & 255)); \
  113. }
  114. #define STORE64H(x, y) { \
  115. (y)[0] = (unsigned char)(((x)>>56)&255); \
  116. (y)[1] = (unsigned char)(((x)>>48)&255); \
  117. (y)[2] = (unsigned char)(((x)>>40)&255); \
  118. (y)[3] = (unsigned char)(((x)>>32)&255); \
  119. (y)[4] = (unsigned char)(((x)>>24)&255); \
  120. (y)[5] = (unsigned char)(((x)>>16)&255); \
  121. (y)[6] = (unsigned char)(((x)>>8)&255); \
  122. (y)[7] = (unsigned char)((x)&255); \
  123. }
  124. #define LOAD64H(x, y) { \
  125. x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
  126. (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
  127. (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
  128. (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); \
  129. }
  130. #endif /* ENDIAN_NEUTRAL */
  131. #ifdef ENDIAN_LITTLE
  132. #define STORE32H(x, y) { \
  133. (y)[0] = (unsigned char)(((x)>>24)&255); \
  134. (y)[1] = (unsigned char)(((x)>>16)&255); \
  135. (y)[2] = (unsigned char)(((x)>>8)&255); \
  136. (y)[3] = (unsigned char)((x)&255); \
  137. }
  138. #define LOAD32H(x, y) { \
  139. x = ((unsigned long)((y)[0] & 255)<<24) | \
  140. ((unsigned long)((y)[1] & 255)<<16) | \
  141. ((unsigned long)((y)[2] & 255)<<8) | \
  142. ((unsigned long)((y)[3] & 255)); \
  143. }
  144. #define STORE64H(x, y) { \
  145. (y)[0] = (unsigned char)(((x)>>56)&255); \
  146. (y)[1] = (unsigned char)(((x)>>48)&255); \
  147. (y)[2] = (unsigned char)(((x)>>40)&255); \
  148. (y)[3] = (unsigned char)(((x)>>32)&255); \
  149. (y)[4] = (unsigned char)(((x)>>24)&255); \
  150. (y)[5] = (unsigned char)(((x)>>16)&255); \
  151. (y)[6] = (unsigned char)(((x)>>8)&255); \
  152. (y)[7] = (unsigned char)((x)&255); \
  153. }
  154. #define LOAD64H(x, y) { \
  155. x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
  156. (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
  157. (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
  158. (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); }
  159. #ifdef ENDIAN_32BITWORD
  160. #define STORE32L(x, y) { \
  161. unsigned long __t = (x); memcpy(y, &__t, 4); \
  162. }
  163. #define LOAD32L(x, y) memcpy(&(x), y, 4);
  164. #define STORE64L(x, y) { \
  165. (y)[7] = (unsigned char)(((x)>>56)&255); \
  166. (y)[6] = (unsigned char)(((x)>>48)&255); \
  167. (y)[5] = (unsigned char)(((x)>>40)&255); \
  168. (y)[4] = (unsigned char)(((x)>>32)&255); \
  169. (y)[3] = (unsigned char)(((x)>>24)&255); \
  170. (y)[2] = (unsigned char)(((x)>>16)&255); \
  171. (y)[1] = (unsigned char)(((x)>>8)&255); \
  172. (y)[0] = (unsigned char)((x)&255); \
  173. }
  174. #define LOAD64L(x, y) { \
  175. x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
  176. (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
  177. (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
  178. (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
  179. }
  180. #else /* 64-bit words then */
  181. #define STORE32L(x, y) \
  182. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  183. #define LOAD32L(x, y) \
  184. { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
  185. #define STORE64L(x, y) \
  186. { uint64 __t = (x); memcpy(y, &__t, 8); }
  187. #define LOAD64L(x, y) \
  188. { memcpy(&(x), y, 8); }
  189. #endif /* ENDIAN_64BITWORD */
  190. #endif /* ENDIAN_LITTLE */
  191. #ifdef ENDIAN_BIG
  192. #define STORE32L(x, y) { \
  193. (y)[3] = (unsigned char)(((x)>>24)&255); \
  194. (y)[2] = (unsigned char)(((x)>>16)&255); \
  195. (y)[1] = (unsigned char)(((x)>>8)&255); \
  196. (y)[0] = (unsigned char)((x)&255); \
  197. }
  198. #define LOAD32L(x, y) { \
  199. x = ((unsigned long)((y)[3] & 255)<<24) | \
  200. ((unsigned long)((y)[2] & 255)<<16) | \
  201. ((unsigned long)((y)[1] & 255)<<8) | \
  202. ((unsigned long)((y)[0] & 255)); \
  203. }
  204. #define STORE64L(x, y) { \
  205. (y)[7] = (unsigned char)(((x)>>56)&255); \
  206. (y)[6] = (unsigned char)(((x)>>48)&255); \
  207. (y)[5] = (unsigned char)(((x)>>40)&255); \
  208. (y)[4] = (unsigned char)(((x)>>32)&255); \
  209. (y)[3] = (unsigned char)(((x)>>24)&255); \
  210. (y)[2] = (unsigned char)(((x)>>16)&255); \
  211. (y)[1] = (unsigned char)(((x)>>8)&255); \
  212. (y)[0] = (unsigned char)((x)&255); \
  213. }
  214. #define LOAD64L(x, y) { \
  215. x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48) | \
  216. (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32) | \
  217. (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16) | \
  218. (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
  219. }
  220. #ifdef ENDIAN_32BITWORD
  221. #define STORE32H(x, y) \
  222. { unsigned int __t = (x); memcpy(y, &__t, 4); }
  223. #define LOAD32H(x, y) memcpy(&(x), y, 4);
  224. #define STORE64H(x, y) { \
  225. (y)[0] = (unsigned char)(((x)>>56)&255); \
  226. (y)[1] = (unsigned char)(((x)>>48)&255); \
  227. (y)[2] = (unsigned char)(((x)>>40)&255); \
  228. (y)[3] = (unsigned char)(((x)>>32)&255); \
  229. (y)[4] = (unsigned char)(((x)>>24)&255); \
  230. (y)[5] = (unsigned char)(((x)>>16)&255); \
  231. (y)[6] = (unsigned char)(((x)>>8)&255); \
  232. (y)[7] = (unsigned char)((x)&255); \
  233. }
  234. #define LOAD64H(x, y) { \
  235. x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48)| \
  236. (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32)| \
  237. (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16)| \
  238. (((uint64)((y)[6] & 255))<<8)| (((uint64)((y)[7] & 255))); \
  239. }
  240. #else /* 64-bit words then */
  241. #define STORE32H(x, y) \
  242. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  243. #define LOAD32H(x, y) \
  244. { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
  245. #define STORE64H(x, y) \
  246. { uint64 __t = (x); memcpy(y, &__t, 8); }
  247. #define LOAD64H(x, y) \
  248. { memcpy(&(x), y, 8); }
  249. #endif /* ENDIAN_64BITWORD */
  250. #endif /* ENDIAN_BIG */
  251. #ifdef HAVE_NATIVE_INT64
  252. #define ROL64c(x, y) \
  253. ( (((x)<<((uint64)(y)&63)) | \
  254. (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
  255. #define ROR64c(x, y) \
  256. ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)(y)&CONST64(63))) | \
  257. ((x)<<((uint64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
  258. #endif /* HAVE_NATIVE_INT64 */
  259. /******************************************************************************/
  260. /* The part below is taken almost verbatim from matrixssl-3-7-2b-open/crypto/symmetric/.
  261. * Changes are flagged with //bbox
  262. */
  263. /**
  264. * @file symmetric.h
  265. * @version 33ef80f (HEAD, tag: MATRIXSSL-3-7-2-OPEN, tag: MATRIXSSL-3-7-2-COMM, origin/master, origin/HEAD, master)
  266. *
  267. * Header for internal symmetric key cryptography support.
  268. */
  269. /*
  270. * Copyright (c) 2013-2015 INSIDE Secure Corporation
  271. * Copyright (c) PeerSec Networks, 2002-2011
  272. * All Rights Reserved
  273. *
  274. * The latest version of this code is available at http://www.matrixssl.org
  275. *
  276. * This software is open source; you can redistribute it and/or modify
  277. * it under the terms of the GNU General Public License as published by
  278. * the Free Software Foundation; either version 2 of the License, or
  279. * (at your option) any later version.
  280. *
  281. * This General Public License does NOT permit incorporating this software
  282. * into proprietary programs. If you are unable to comply with the GPL, a
  283. * commercial license for this software may be purchased from INSIDE at
  284. * http://www.insidesecure.com/eng/Company/Locations
  285. *
  286. * This program is distributed in WITHOUT ANY WARRANTY; without even the
  287. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  288. * See the GNU General Public License for more details.
  289. *
  290. * You should have received a copy of the GNU General Public License
  291. * along with this program; if not, write to the Free Software
  292. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  293. * http://www.gnu.org/copyleft/gpl.html
  294. */
  295. /******************************************************************************/
  296. #ifndef _h_PS_SYMMETRIC
  297. #define _h_PS_SYMMETRIC
  298. /******************************************************************************/
  299. #ifdef USE_AES
  300. /******************************************************************************/
  301. #ifndef USE_AES_CBC_EXTERNAL
  302. typedef struct {
  303. uint32 eK[64], dK[64];
  304. int32 Nr;
  305. } psAesKey_t;
  306. typedef struct {
  307. int32 blocklen;
  308. unsigned char IV[16];
  309. psAesKey_t key;
  310. #if defined(USE_AES_GCM) || defined(USE_AES_CCM)
  311. unsigned char EncCtr[16];
  312. unsigned char CtrBlock[16];
  313. #endif
  314. #ifdef USE_AES_GCM
  315. unsigned char gInit[16];
  316. uint32 TagTemp[4];
  317. unsigned char Hash_SubKey[16];
  318. uint32 ProcessedBitCount[4];
  319. uint32 InputBufferCount;
  320. uint32 OutputBufferCount;
  321. union
  322. {
  323. unsigned char Buffer[128];
  324. uint32 BufferAlignment;
  325. } Input;
  326. #endif /* USE_AES_GCM */
  327. #ifdef USE_AES_CCM
  328. uint32_t ccmTagTemp[16 / sizeof(uint32_t)]; /* 32 */
  329. union
  330. {
  331. /* Used for formatting IV. */
  332. uint8_t Temporary[16];
  333. /* Used for processing Mac. */
  334. uint8_t Y0[16];
  335. } u; /* 48 */
  336. #endif /* USE_AES_CCM */
  337. } psAesCipher_t;
  338. #endif /* USE_AES_CBC_EXTERNAL */
  339. #endif /* USE_AES */
  340. #ifdef USE_IDEA
  341. #define SSL_IDEA_KEY_LEN 16
  342. #define SSL_IDEA_IV_LEN 8
  343. #define SSL_IDEA_BLOCK_LEN 8
  344. typedef struct {
  345. uint16 key_schedule[52];
  346. } psIdeaKey_t;
  347. typedef struct {
  348. psIdeaKey_t key;
  349. uint32 IV[2];
  350. short for_encryption;
  351. short inverted;
  352. } idea_CBC;
  353. #endif
  354. /******************************************************************************/
  355. /******************************************************************************/
  356. #ifdef USE_SEED
  357. /******************************************************************************/
  358. #define SSL_SEED_KEY_LEN 16
  359. #define SSL_SEED_IV_LEN 16
  360. typedef struct {
  361. uint32 K[32], dK[32];
  362. } psSeedKey_t;
  363. typedef struct {
  364. int32 blocklen;
  365. unsigned char IV[16];
  366. psSeedKey_t key;
  367. } seed_CBC;
  368. #endif /* USE_SEED */
  369. /******************************************************************************/
  370. /******************************************************************************/
  371. #if defined(USE_3DES) || defined(USE_DES)
  372. /******************************************************************************/
  373. #define DES3_KEY_LEN 24
  374. #define DES3_IV_LEN 8
  375. #define DES_KEY_LEN 8
  376. typedef struct {
  377. uint32 ek[3][32], dk[3][32];
  378. } psDes3Key_t;
  379. /*
  380. A block cipher CBC structure
  381. */
  382. typedef struct {
  383. int32 blocklen;
  384. unsigned char IV[8];
  385. psDes3Key_t key;
  386. } des3_CBC;
  387. #endif /* USE_3DES || USE_DES */
  388. /******************************************************************************/
  389. /******************************************************************************/
  390. #ifdef USE_ARC4
  391. typedef struct {
  392. unsigned char state[256];
  393. uint32 byteCount;
  394. unsigned char x;
  395. unsigned char y;
  396. } psRc4Key_t;
  397. #endif /* USE_ARC4 */
  398. /******************************************************************************/
  399. #ifdef USE_RC2
  400. typedef struct {
  401. unsigned xkey[64];
  402. } psRc2Key_t;
  403. typedef struct {
  404. int32 blocklen;
  405. unsigned char IV[8];
  406. psRc2Key_t key;
  407. } rc2_CBC;
  408. #endif /* USE_RC2 */
  409. /******************************************************************************/
  410. /* Universal types and defines */
  411. /******************************************************************************/
  412. #define MAXBLOCKSIZE 24
  413. typedef union {
  414. #ifdef USE_RC2
  415. rc2_CBC rc2;
  416. #endif
  417. #ifdef USE_ARC4
  418. psRc4Key_t arc4;
  419. #endif
  420. #ifdef USE_3DES
  421. des3_CBC des3;
  422. #endif
  423. #ifdef USE_AES
  424. psAesCipher_t aes;
  425. #endif
  426. #ifdef USE_SEED
  427. seed_CBC seed;
  428. #endif
  429. #ifdef USE_IDEA
  430. idea_CBC idea;
  431. #endif
  432. } psCipherContext_t;
  433. #define byte(x, n) (((x) >> (8 * (n))) & 255)
  434. #endif /* _h_PS_SYMMETRIC */
  435. /******************************************************************************/