aes_modes.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2006, Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The free distribution and use of this software in both source and binary
  6. form is allowed (with or without changes) provided that:
  7. 1. distributions of this source code include the above copyright
  8. notice, this list of conditions and the following disclaimer;
  9. 2. distributions in binary form include the above copyright
  10. notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other associated materials;
  12. 3. the copyright holder's name is not used to endorse products
  13. built using this software without specific written permission.
  14. ALTERNATIVELY, provided that this notice is retained in full, this product
  15. may be distributed under the terms of the GNU General Public License (GPL),
  16. in which case the provisions of the GPL apply INSTEAD OF those given above.
  17. DISCLAIMER
  18. This software is provided 'as is' with no explicit or implied warranties
  19. in respect of its properties, including, but not limited to, correctness
  20. and/or fitness for purpose.
  21. ---------------------------------------------------------------------------
  22. Issue 09/09/2006
  23. These subroutines implement multiple block AES modes for ECB, CBC, CFB,
  24. OFB and CTR encryption, The code provides support for the VIA Advanced
  25. Cryptography Engine (ACE).
  26. NOTE: In the following subroutines, the AES contexts (ctx) must be
  27. 16 byte aligned if VIA ACE is being used
  28. */
  29. #include <string.h>
  30. #include <assert.h>
  31. #include "aesopt.h"
  32. #if defined( AES_MODES )
  33. #if defined(__cplusplus)
  34. extern "C"
  35. {
  36. #endif
  37. #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
  38. #pragma intrinsic(memcpy)
  39. #define in_line __inline
  40. #else
  41. #define in_line
  42. #endif
  43. #define BFR_BLOCKS 8
  44. /* These values are used to detect long word alignment in order to */
  45. /* speed up some buffer operations. This facility may not work on */
  46. /* some machines so this define can be commented out if necessary */
  47. #define FAST_BUFFER_OPERATIONS
  48. #define lp32(x) ((uint_32t*)(x))
  49. //[{]
  50. #if defined( _MSC_VER ) && ( _MSC_VER > 1200 )
  51. # define aligned_uint_8t(n) __declspec(align(n)) uint_8t _zrtp_bg_al_##n
  52. #elif defined( __GNUC__ ) || (__MWERKS__)
  53. # define aligned_uint_8t(n) uint_8t _zrtp_bg_al_##n __attribute__ ((aligned(n)))
  54. #else // disabled for VC6
  55. # undef FAST_BUFFER_OPERATIONS
  56. #endif
  57. #ifdef FAST_BUFFER_OPERATIONS
  58. aligned_uint_8t( 4); /* a variable that is 4 byte aligned */
  59. aligned_uint_8t(16); /* a variable that is 16 byte aligned */
  60. #define addr_offset(x,n) (((uint_8t*)(x) - &_zrtp_bg_al_##n) & ((n) - 1))
  61. #endif
  62. //[}]
  63. #if defined( USE_VIA_ACE_IF_PRESENT )
  64. #include "aes_via_ace.h"
  65. #pragma pack(16)
  66. aligned_array(unsigned long, enc_gen_table, 12, 16) = NEH_ENC_GEN_DATA;
  67. aligned_array(unsigned long, enc_load_table, 12, 16) = NEH_ENC_LOAD_DATA;
  68. aligned_array(unsigned long, enc_hybrid_table, 12, 16) = NEH_ENC_HYBRID_DATA;
  69. aligned_array(unsigned long, dec_gen_table, 12, 16) = NEH_DEC_GEN_DATA;
  70. aligned_array(unsigned long, dec_load_table, 12, 16) = NEH_DEC_LOAD_DATA;
  71. aligned_array(unsigned long, dec_hybrid_table, 12, 16) = NEH_DEC_HYBRID_DATA;
  72. /* NOTE: These control word macros must only be used after */
  73. /* a key has been set up because they depend on key size */
  74. #if NEH_KEY_TYPE == NEH_LOAD
  75. #define kd_adr(c) ((uint_8t*)(c)->ks)
  76. #elif NEH_KEY_TYPE == NEH_GENERATE
  77. #define kd_adr(c) ((uint_8t*)(c)->ks + (c)->inf.b[0])
  78. #else
  79. #define kd_adr(c) ((uint_8t*)(c)->ks + ((c)->inf.b[0] == 160 ? 160 : 0))
  80. #endif
  81. #else
  82. #define aligned_array(type, name, no, stride) type name[no]
  83. #define aligned_auto(type, name, no, stride) type name[no]
  84. #endif
  85. #if defined( _MSC_VER ) && _MSC_VER > 1200
  86. #define via_cwd(cwd, ty, dir, len) \
  87. unsigned long* cwd = (dir##_##ty##_table + ((len - 128) >> 4))
  88. #else
  89. #define via_cwd(cwd, ty, dir, len) \
  90. aligned_auto(unsigned long, cwd, 4, 16); \
  91. cwd[1] = cwd[2] = cwd[3] = 0; \
  92. cwd[0] = neh_##dir##_##ty##_key(len)
  93. #endif
  94. AES_RETURN zrtp_bg_aes_mode_reset(aes_encrypt_ctx ctx[1])
  95. {
  96. ctx->inf.b[2] = 0;
  97. return EXIT_SUCCESS;
  98. }
  99. AES_RETURN zrtp_bg_aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
  100. int len, const aes_encrypt_ctx ctx[1])
  101. { int nb = len >> 4;
  102. if(len & (AES_BLOCK_SIZE - 1))
  103. return EXIT_FAILURE;
  104. #if defined( USE_VIA_ACE_IF_PRESENT )
  105. if(ctx->inf.b[1] == 0xff)
  106. { uint_8t *ksp = (uint_8t*)(ctx->ks);
  107. via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
  108. if(addr_offset( ctx, 16 ))
  109. return EXIT_FAILURE;
  110. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ))
  111. {
  112. via_ecb_op5(ksp,cwd,ibuf,obuf,nb);
  113. }
  114. else
  115. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  116. uint_8t *ip, *op;
  117. while(nb)
  118. {
  119. int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
  120. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  121. op = (addr_offset( obuf, 16 ) ? buf : obuf);
  122. if(ip != ibuf)
  123. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  124. via_ecb_op5(ksp,cwd,ip,op,m);
  125. if(op != obuf)
  126. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  127. ibuf += m * AES_BLOCK_SIZE;
  128. obuf += m * AES_BLOCK_SIZE;
  129. nb -= m;
  130. }
  131. }
  132. return EXIT_SUCCESS;
  133. }
  134. #endif
  135. #if !defined( ASSUME_VIA_ACE_PRESENT )
  136. while(nb--)
  137. {
  138. zrtp_bg_aes_encrypt(ibuf, obuf, ctx);
  139. ibuf += AES_BLOCK_SIZE;
  140. obuf += AES_BLOCK_SIZE;
  141. }
  142. #endif
  143. return EXIT_SUCCESS;
  144. }
  145. AES_RETURN zrtp_bg_aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
  146. int len, const aes_decrypt_ctx ctx[1])
  147. { int nb = len >> 4;
  148. if(len & (AES_BLOCK_SIZE - 1))
  149. return EXIT_FAILURE;
  150. #if defined( USE_VIA_ACE_IF_PRESENT )
  151. if(ctx->inf.b[1] == 0xff)
  152. { uint_8t *ksp = kd_adr(ctx);
  153. via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);
  154. if(addr_offset( ctx, 16 ))
  155. return EXIT_FAILURE;
  156. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ))
  157. {
  158. via_ecb_op5(ksp,cwd,ibuf,obuf,nb);
  159. }
  160. else
  161. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  162. uint_8t *ip, *op;
  163. while(nb)
  164. {
  165. int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
  166. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  167. op = (addr_offset( obuf, 16 ) ? buf : obuf);
  168. if(ip != ibuf)
  169. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  170. via_ecb_op5(ksp,cwd,ip,op,m);
  171. if(op != obuf)
  172. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  173. ibuf += m * AES_BLOCK_SIZE;
  174. obuf += m * AES_BLOCK_SIZE;
  175. nb -= m;
  176. }
  177. }
  178. return EXIT_SUCCESS;
  179. }
  180. #endif
  181. #if !defined( ASSUME_VIA_ACE_PRESENT )
  182. while(nb--)
  183. {
  184. zrtp_bg_aes_decrypt(ibuf, obuf, ctx);
  185. ibuf += AES_BLOCK_SIZE;
  186. obuf += AES_BLOCK_SIZE;
  187. }
  188. #endif
  189. return EXIT_SUCCESS;
  190. }
  191. #ifndef ZRTP_RESTRICT
  192. AES_RETURN zrtp_bg_aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
  193. int len, unsigned char *iv, const aes_encrypt_ctx ctx[1])
  194. { int nb = len >> 4;
  195. if(len & (AES_BLOCK_SIZE - 1))
  196. return EXIT_FAILURE;
  197. #if defined( USE_VIA_ACE_IF_PRESENT )
  198. if(ctx->inf.b[1] == 0xff)
  199. { uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;
  200. aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);
  201. via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
  202. if(addr_offset( ctx, 16 ))
  203. return EXIT_FAILURE;
  204. if(addr_offset( iv, 16 )) /* ensure an aligned iv */
  205. {
  206. ivp = liv;
  207. memcpy(liv, iv, AES_BLOCK_SIZE);
  208. }
  209. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ) && !addr_offset( iv, 16 ))
  210. {
  211. via_cbc_op7(ksp,cwd,ibuf,obuf,nb,ivp,ivp);
  212. }
  213. else
  214. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  215. uint_8t *ip, *op;
  216. while(nb)
  217. {
  218. int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
  219. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  220. op = (addr_offset( obuf, 16 ) ? buf : obuf);
  221. if(ip != ibuf)
  222. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  223. via_cbc_op7(ksp,cwd,ip,op,m,ivp,ivp);
  224. if(op != obuf)
  225. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  226. ibuf += m * AES_BLOCK_SIZE;
  227. obuf += m * AES_BLOCK_SIZE;
  228. nb -= m;
  229. }
  230. }
  231. if(iv != ivp)
  232. memcpy(iv, ivp, AES_BLOCK_SIZE);
  233. return EXIT_SUCCESS;
  234. }
  235. #endif
  236. #if !defined( ASSUME_VIA_ACE_PRESENT )
  237. # ifdef FAST_BUFFER_OPERATIONS
  238. if(!addr_offset( ibuf, 4 ) && !addr_offset( iv, 4 ))
  239. while(nb--)
  240. {
  241. lp32(iv)[0] ^= lp32(ibuf)[0];
  242. lp32(iv)[1] ^= lp32(ibuf)[1];
  243. lp32(iv)[2] ^= lp32(ibuf)[2];
  244. lp32(iv)[3] ^= lp32(ibuf)[3];
  245. zrtp_bg_aes_encrypt(iv, iv, ctx);
  246. memcpy(obuf, iv, AES_BLOCK_SIZE);
  247. ibuf += AES_BLOCK_SIZE;
  248. obuf += AES_BLOCK_SIZE;
  249. }
  250. else
  251. # endif
  252. while(nb--)
  253. {
  254. iv[ 0] ^= ibuf[ 0]; iv[ 1] ^= ibuf[ 1];
  255. iv[ 2] ^= ibuf[ 2]; iv[ 3] ^= ibuf[ 3];
  256. iv[ 4] ^= ibuf[ 4]; iv[ 5] ^= ibuf[ 5];
  257. iv[ 6] ^= ibuf[ 6]; iv[ 7] ^= ibuf[ 7];
  258. iv[ 8] ^= ibuf[ 8]; iv[ 9] ^= ibuf[ 9];
  259. iv[10] ^= ibuf[10]; iv[11] ^= ibuf[11];
  260. iv[12] ^= ibuf[12]; iv[13] ^= ibuf[13];
  261. iv[14] ^= ibuf[14]; iv[15] ^= ibuf[15];
  262. zrtp_bg_aes_encrypt(iv, iv, ctx);
  263. memcpy(obuf, iv, AES_BLOCK_SIZE);
  264. ibuf += AES_BLOCK_SIZE;
  265. obuf += AES_BLOCK_SIZE;
  266. }
  267. #endif
  268. return EXIT_SUCCESS;
  269. }
  270. AES_RETURN zrtp_bg_aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
  271. int len, unsigned char *iv, const aes_decrypt_ctx ctx[1])
  272. { unsigned char tmp[AES_BLOCK_SIZE];
  273. int nb = len >> 4;
  274. if(len & (AES_BLOCK_SIZE - 1))
  275. return EXIT_FAILURE;
  276. #if defined( USE_VIA_ACE_IF_PRESENT )
  277. if(ctx->inf.b[1] == 0xff)
  278. { uint_8t *ksp = kd_adr(ctx), *ivp = iv;
  279. aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);
  280. via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);
  281. if(addr_offset( ctx, 16 ))
  282. return EXIT_FAILURE;
  283. if(addr_offset( iv, 16 )) /* ensure an aligned iv */
  284. {
  285. ivp = liv;
  286. memcpy(liv, iv, AES_BLOCK_SIZE);
  287. }
  288. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ) && !addr_offset( iv, 16 ))
  289. {
  290. via_cbc_op6(ksp,cwd,ibuf,obuf,nb,ivp);
  291. }
  292. else
  293. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  294. uint_8t *ip, *op;
  295. while(nb)
  296. {
  297. int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
  298. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  299. op = (addr_offset( obuf, 16 ) ? buf : obuf);
  300. if(ip != ibuf)
  301. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  302. via_cbc_op6(ksp,cwd,ip,op,m,ivp);
  303. if(op != obuf)
  304. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  305. ibuf += m * AES_BLOCK_SIZE;
  306. obuf += m * AES_BLOCK_SIZE;
  307. nb -= m;
  308. }
  309. }
  310. if(iv != ivp)
  311. memcpy(iv, ivp, AES_BLOCK_SIZE);
  312. return EXIT_SUCCESS;
  313. }
  314. #endif
  315. #if !defined( ASSUME_VIA_ACE_PRESENT )
  316. # ifdef FAST_BUFFER_OPERATIONS
  317. if(!addr_offset( obuf, 4 ) && !addr_offset( iv, 4 ))
  318. while(nb--)
  319. {
  320. memcpy(tmp, ibuf, AES_BLOCK_SIZE);
  321. zrtp_bg_aes_decrypt(ibuf, obuf, ctx);
  322. lp32(obuf)[0] ^= lp32(iv)[0];
  323. lp32(obuf)[1] ^= lp32(iv)[1];
  324. lp32(obuf)[2] ^= lp32(iv)[2];
  325. lp32(obuf)[3] ^= lp32(iv)[3];
  326. memcpy(iv, tmp, AES_BLOCK_SIZE);
  327. ibuf += AES_BLOCK_SIZE;
  328. obuf += AES_BLOCK_SIZE;
  329. }
  330. else
  331. # endif
  332. while(nb--)
  333. {
  334. memcpy(tmp, ibuf, AES_BLOCK_SIZE);
  335. zrtp_bg_aes_decrypt(ibuf, obuf, ctx);
  336. obuf[ 0] ^= iv[ 0]; obuf[ 1] ^= iv[ 1];
  337. obuf[ 2] ^= iv[ 2]; obuf[ 3] ^= iv[ 3];
  338. obuf[ 4] ^= iv[ 4]; obuf[ 5] ^= iv[ 5];
  339. obuf[ 6] ^= iv[ 6]; obuf[ 7] ^= iv[ 7];
  340. obuf[ 8] ^= iv[ 8]; obuf[ 9] ^= iv[ 9];
  341. obuf[10] ^= iv[10]; obuf[11] ^= iv[11];
  342. obuf[12] ^= iv[12]; obuf[13] ^= iv[13];
  343. obuf[14] ^= iv[14]; obuf[15] ^= iv[15];
  344. memcpy(iv, tmp, AES_BLOCK_SIZE);
  345. ibuf += AES_BLOCK_SIZE;
  346. obuf += AES_BLOCK_SIZE;
  347. }
  348. #endif
  349. return EXIT_SUCCESS;
  350. }
  351. #endif //ZRTP_RESTRICT
  352. AES_RETURN zrtp_bg_aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
  353. int len, unsigned char *iv, aes_encrypt_ctx ctx[1])
  354. { int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;
  355. if(b_pos) /* complete any partial block */
  356. {
  357. while(b_pos < AES_BLOCK_SIZE && cnt < len)
  358. *obuf++ = iv[b_pos++] ^= *ibuf++, cnt++;
  359. b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
  360. }
  361. if((nb = (len - cnt) >> 4) != 0) /* process whole blocks */
  362. {
  363. #if defined( USE_VIA_ACE_IF_PRESENT )
  364. if(ctx->inf.b[1] == 0xff)
  365. { int m;
  366. uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;
  367. aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);
  368. via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
  369. if(addr_offset( ctx, 16 ))
  370. return EXIT_FAILURE;
  371. if(addr_offset( iv, 16 )) /* ensure an aligned iv */
  372. {
  373. ivp = liv;
  374. memcpy(liv, iv, AES_BLOCK_SIZE);
  375. }
  376. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ))
  377. {
  378. via_cfb_op7(ksp, cwd, ibuf, obuf, nb, ivp, ivp);
  379. ibuf += nb * AES_BLOCK_SIZE;
  380. obuf += nb * AES_BLOCK_SIZE;
  381. cnt += nb * AES_BLOCK_SIZE;
  382. }
  383. else /* input, output or both are unaligned */
  384. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  385. uint_8t *ip, *op;
  386. while(nb)
  387. {
  388. m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
  389. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  390. op = (addr_offset( obuf, 16 ) ? buf : obuf);
  391. if(ip != ibuf)
  392. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  393. via_cfb_op7(ksp, cwd, ip, op, m, ivp, ivp);
  394. if(op != obuf)
  395. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  396. ibuf += m * AES_BLOCK_SIZE;
  397. obuf += m * AES_BLOCK_SIZE;
  398. cnt += m * AES_BLOCK_SIZE;
  399. }
  400. }
  401. if(ivp != iv)
  402. memcpy(iv, ivp, AES_BLOCK_SIZE);
  403. }
  404. #else
  405. # ifdef FAST_BUFFER_OPERATIONS
  406. if(!addr_offset( ibuf, 4 ) && !addr_offset( obuf, 4 ) && !addr_offset( iv, 4 ))
  407. while(cnt + AES_BLOCK_SIZE <= len)
  408. {
  409. assert(b_pos == 0);
  410. zrtp_bg_aes_encrypt(iv, iv, ctx);
  411. lp32(obuf)[0] = lp32(iv)[0] ^= lp32(ibuf)[0];
  412. lp32(obuf)[1] = lp32(iv)[1] ^= lp32(ibuf)[1];
  413. lp32(obuf)[2] = lp32(iv)[2] ^= lp32(ibuf)[2];
  414. lp32(obuf)[3] = lp32(iv)[3] ^= lp32(ibuf)[3];
  415. ibuf += AES_BLOCK_SIZE;
  416. obuf += AES_BLOCK_SIZE;
  417. cnt += AES_BLOCK_SIZE;
  418. }
  419. else
  420. # endif
  421. while(cnt + AES_BLOCK_SIZE <= len)
  422. {
  423. assert(b_pos == 0);
  424. zrtp_bg_aes_encrypt(iv, iv, ctx);
  425. obuf[ 0] = iv[ 0] ^= ibuf[ 0]; obuf[ 1] = iv[ 1] ^= ibuf[ 1];
  426. obuf[ 2] = iv[ 2] ^= ibuf[ 2]; obuf[ 3] = iv[ 3] ^= ibuf[ 3];
  427. obuf[ 4] = iv[ 4] ^= ibuf[ 4]; obuf[ 5] = iv[ 5] ^= ibuf[ 5];
  428. obuf[ 6] = iv[ 6] ^= ibuf[ 6]; obuf[ 7] = iv[ 7] ^= ibuf[ 7];
  429. obuf[ 8] = iv[ 8] ^= ibuf[ 8]; obuf[ 9] = iv[ 9] ^= ibuf[ 9];
  430. obuf[10] = iv[10] ^= ibuf[10]; obuf[11] = iv[11] ^= ibuf[11];
  431. obuf[12] = iv[12] ^= ibuf[12]; obuf[13] = iv[13] ^= ibuf[13];
  432. obuf[14] = iv[14] ^= ibuf[14]; obuf[15] = iv[15] ^= ibuf[15];
  433. ibuf += AES_BLOCK_SIZE;
  434. obuf += AES_BLOCK_SIZE;
  435. cnt += AES_BLOCK_SIZE;
  436. }
  437. #endif
  438. }
  439. while(cnt < len)
  440. {
  441. if(!b_pos)
  442. zrtp_bg_aes_ecb_encrypt(iv, iv, AES_BLOCK_SIZE, ctx);
  443. while(cnt < len && b_pos < AES_BLOCK_SIZE)
  444. *obuf++ = iv[b_pos++] ^= *ibuf++, cnt++;
  445. b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
  446. }
  447. ctx->inf.b[2] = (uint_8t)b_pos;
  448. return EXIT_SUCCESS;
  449. }
  450. AES_RETURN zrtp_bg_aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
  451. int len, unsigned char *iv, aes_encrypt_ctx ctx[1])
  452. { int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;
  453. if(b_pos) /* complete any partial block */
  454. { uint_8t t;
  455. while(b_pos < AES_BLOCK_SIZE && cnt < len)
  456. t = *ibuf++, *obuf++ = t ^ iv[b_pos], iv[b_pos++] = t, cnt++;
  457. b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
  458. }
  459. if((nb = (len - cnt) >> 4) != 0) /* process whole blocks */
  460. {
  461. #if defined( USE_VIA_ACE_IF_PRESENT )
  462. if(ctx->inf.b[1] == 0xff)
  463. { int m;
  464. uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;
  465. aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);
  466. via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);
  467. if(addr_offset( ctx, 16 ))
  468. return EXIT_FAILURE;
  469. if(addr_offset( iv, 16 )) /* ensure an aligned iv */
  470. {
  471. ivp = liv;
  472. memcpy(liv, iv, AES_BLOCK_SIZE);
  473. }
  474. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ))
  475. {
  476. via_cfb_op6(ksp, cwd, ibuf, obuf, nb, ivp);
  477. ibuf += nb * AES_BLOCK_SIZE;
  478. obuf += nb * AES_BLOCK_SIZE;
  479. cnt += nb * AES_BLOCK_SIZE;
  480. }
  481. else /* input, output or both are unaligned */
  482. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  483. uint_8t *ip, *op;
  484. while(nb)
  485. {
  486. m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
  487. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  488. op = (addr_offset( obuf, 16 ) ? buf : op);
  489. if(ip != ibuf)
  490. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  491. via_cfb_op6(ksp, cwd, ip, op, m, ivp);
  492. if(op != obuf)
  493. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  494. ibuf += m * AES_BLOCK_SIZE;
  495. obuf += m * AES_BLOCK_SIZE;
  496. cnt += m * AES_BLOCK_SIZE;
  497. }
  498. }
  499. if(ivp != iv)
  500. memcpy(iv, ivp, AES_BLOCK_SIZE);
  501. }
  502. #else
  503. # ifdef FAST_BUFFER_OPERATIONS
  504. if(!addr_offset( ibuf, 4 ) && !addr_offset( obuf, 4 ) &&!addr_offset( iv, 4 ))
  505. while(cnt + AES_BLOCK_SIZE <= len)
  506. { uint_32t t;
  507. assert(b_pos == 0);
  508. zrtp_bg_aes_encrypt(iv, iv, ctx);
  509. t = lp32(ibuf)[0], lp32(obuf)[0] = t ^ lp32(iv)[0], lp32(iv)[0] = t;
  510. t = lp32(ibuf)[1], lp32(obuf)[1] = t ^ lp32(iv)[1], lp32(iv)[1] = t;
  511. t = lp32(ibuf)[2], lp32(obuf)[2] = t ^ lp32(iv)[2], lp32(iv)[2] = t;
  512. t = lp32(ibuf)[3], lp32(obuf)[3] = t ^ lp32(iv)[3], lp32(iv)[3] = t;
  513. ibuf += AES_BLOCK_SIZE;
  514. obuf += AES_BLOCK_SIZE;
  515. cnt += AES_BLOCK_SIZE;
  516. }
  517. else
  518. # endif
  519. while(cnt + AES_BLOCK_SIZE <= len)
  520. { uint_8t t;
  521. assert(b_pos == 0);
  522. zrtp_bg_aes_encrypt(iv, iv, ctx);
  523. t = ibuf[ 0], obuf[ 0] = t ^ iv[ 0], iv[ 0] = t;
  524. t = ibuf[ 1], obuf[ 1] = t ^ iv[ 1], iv[ 1] = t;
  525. t = ibuf[ 2], obuf[ 2] = t ^ iv[ 2], iv[ 2] = t;
  526. t = ibuf[ 3], obuf[ 3] = t ^ iv[ 3], iv[ 3] = t;
  527. t = ibuf[ 4], obuf[ 4] = t ^ iv[ 4], iv[ 4] = t;
  528. t = ibuf[ 5], obuf[ 5] = t ^ iv[ 5], iv[ 5] = t;
  529. t = ibuf[ 6], obuf[ 6] = t ^ iv[ 6], iv[ 6] = t;
  530. t = ibuf[ 7], obuf[ 7] = t ^ iv[ 7], iv[ 7] = t;
  531. t = ibuf[ 8], obuf[ 8] = t ^ iv[ 8], iv[ 8] = t;
  532. t = ibuf[ 9], obuf[ 9] = t ^ iv[ 9], iv[ 9] = t;
  533. t = ibuf[10], obuf[10] = t ^ iv[10], iv[10] = t;
  534. t = ibuf[11], obuf[11] = t ^ iv[11], iv[11] = t;
  535. t = ibuf[12], obuf[12] = t ^ iv[12], iv[12] = t;
  536. t = ibuf[13], obuf[13] = t ^ iv[13], iv[13] = t;
  537. t = ibuf[14], obuf[14] = t ^ iv[14], iv[14] = t;
  538. t = ibuf[15], obuf[15] = t ^ iv[15], iv[15] = t;
  539. ibuf += AES_BLOCK_SIZE;
  540. obuf += AES_BLOCK_SIZE;
  541. cnt += AES_BLOCK_SIZE;
  542. }
  543. #endif
  544. }
  545. while(cnt < len)
  546. { uint_8t t;
  547. if(!b_pos)
  548. zrtp_bg_aes_ecb_encrypt(iv, iv, AES_BLOCK_SIZE, ctx);
  549. while(cnt < len && b_pos < AES_BLOCK_SIZE)
  550. t = *ibuf++, *obuf++ = t ^ iv[b_pos], iv[b_pos++] = t, cnt++;
  551. b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
  552. }
  553. ctx->inf.b[2] = (uint_8t)b_pos;
  554. return EXIT_SUCCESS;
  555. }
  556. #ifndef ZRTP_RESTRICT
  557. AES_RETURN zrtp_bg_aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
  558. int len, unsigned char *iv, aes_encrypt_ctx ctx[1])
  559. { int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;
  560. if(b_pos) /* complete any partial block */
  561. {
  562. while(b_pos < AES_BLOCK_SIZE && cnt < len)
  563. *obuf++ = iv[b_pos++] ^ *ibuf++, cnt++;
  564. b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
  565. }
  566. if((nb = (len - cnt) >> 4) != 0) /* process whole blocks */
  567. {
  568. #if defined( USE_VIA_ACE_IF_PRESENT )
  569. if(ctx->inf.b[1] == 0xff)
  570. { int m;
  571. uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;
  572. aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);
  573. via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
  574. if(addr_offset( ctx, 16 ))
  575. return EXIT_FAILURE;
  576. if(addr_offset( iv, 16 )) /* ensure an aligned iv */
  577. {
  578. ivp = liv;
  579. memcpy(liv, iv, AES_BLOCK_SIZE);
  580. }
  581. if(!addr_offset( ibuf, 16 ) && !addr_offset( obuf, 16 ))
  582. {
  583. via_ofb_op6(ksp, cwd, ibuf, obuf, nb, ivp);
  584. ibuf += nb * AES_BLOCK_SIZE;
  585. obuf += nb * AES_BLOCK_SIZE;
  586. cnt += nb * AES_BLOCK_SIZE;
  587. }
  588. else /* input, output or both are unaligned */
  589. { aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
  590. uint_8t *ip, *op;
  591. while(nb)
  592. {
  593. m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
  594. ip = (addr_offset( ibuf, 16 ) ? buf : (uint_8t*)ibuf);
  595. op = (addr_offset( obuf, 16 ) ? buf : obuf);
  596. if(ip != ibuf)
  597. memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
  598. via_ofb_op6(ksp, cwd, ip, op, m, ivp);
  599. if(op != obuf)
  600. memcpy(obuf, buf, m * AES_BLOCK_SIZE);
  601. ibuf += m * AES_BLOCK_SIZE;
  602. obuf += m * AES_BLOCK_SIZE;
  603. cnt += m * AES_BLOCK_SIZE;
  604. }
  605. }
  606. if(ivp != iv)
  607. memcpy(iv, ivp, AES_BLOCK_SIZE);
  608. }
  609. #else
  610. # ifdef FAST_BUFFER_OPERATIONS
  611. if(!addr_offset( ibuf, 4 ) && !addr_offset( obuf, 4 ) && !addr_offset( iv, 4 ))
  612. while(cnt + AES_BLOCK_SIZE <= len)
  613. {
  614. assert(b_pos == 0);
  615. zrtp_bg_aes_encrypt(iv, iv, ctx);
  616. lp32(obuf)[0] = lp32(iv)[0] ^ lp32(ibuf)[0];
  617. lp32(obuf)[1] = lp32(iv)[1] ^ lp32(ibuf)[1];
  618. lp32(obuf)[2] = lp32(iv)[2] ^ lp32(ibuf)[2];
  619. lp32(obuf)[3] = lp32(iv)[3] ^ lp32(ibuf)[3];
  620. ibuf += AES_BLOCK_SIZE;
  621. obuf += AES_BLOCK_SIZE;
  622. cnt += AES_BLOCK_SIZE;
  623. }
  624. else
  625. # endif
  626. while(cnt + AES_BLOCK_SIZE <= len)
  627. {
  628. assert(b_pos == 0);
  629. zrtp_bg_aes_encrypt(iv, iv, ctx);
  630. obuf[ 0] = iv[ 0] ^ ibuf[ 0]; obuf[ 1] = iv[ 1] ^ ibuf[ 1];
  631. obuf[ 2] = iv[ 2] ^ ibuf[ 2]; obuf[ 3] = iv[ 3] ^ ibuf[ 3];
  632. obuf[ 4] = iv[ 4] ^ ibuf[ 4]; obuf[ 5] = iv[ 5] ^ ibuf[ 5];
  633. obuf[ 6] = iv[ 6] ^ ibuf[ 6]; obuf[ 7] = iv[ 7] ^ ibuf[ 7];
  634. obuf[ 8] = iv[ 8] ^ ibuf[ 8]; obuf[ 9] = iv[ 9] ^ ibuf[ 9];
  635. obuf[10] = iv[10] ^ ibuf[10]; obuf[11] = iv[11] ^ ibuf[11];
  636. obuf[12] = iv[12] ^ ibuf[12]; obuf[13] = iv[13] ^ ibuf[13];
  637. obuf[14] = iv[14] ^ ibuf[14]; obuf[15] = iv[15] ^ ibuf[15];
  638. ibuf += AES_BLOCK_SIZE;
  639. obuf += AES_BLOCK_SIZE;
  640. cnt += AES_BLOCK_SIZE;
  641. }
  642. #endif
  643. }
  644. while(cnt < len)
  645. {
  646. if(!b_pos)
  647. zrtp_bg_aes_ecb_encrypt(iv, iv, AES_BLOCK_SIZE, ctx);
  648. while(cnt < len && b_pos < AES_BLOCK_SIZE)
  649. *obuf++ = iv[b_pos++] ^ *ibuf++, cnt++;
  650. b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
  651. }
  652. ctx->inf.b[2] = (uint_8t)b_pos;
  653. return EXIT_SUCCESS;
  654. }
  655. #endif //ZRTP_RESTRICT
  656. #define BFR_LENGTH (BFR_BLOCKS * AES_BLOCK_SIZE)
  657. AES_RETURN zrtp_bg_aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
  658. int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx ctx[1])
  659. { uint_8t *ip;
  660. int i, blen, b_pos = (int)(ctx->inf.b[2]);
  661. #if defined( USE_VIA_ACE_IF_PRESENT )
  662. aligned_auto(uint_8t, buf, BFR_LENGTH, 16);
  663. if(ctx->inf.b[1] == 0xff && addr_offset( ctx, 16 ))
  664. return EXIT_FAILURE;
  665. #else
  666. uint_8t buf[BFR_LENGTH];
  667. #endif
  668. if(b_pos)
  669. {
  670. memcpy(buf, cbuf, AES_BLOCK_SIZE);
  671. zrtp_bg_aes_ecb_encrypt(buf, buf, AES_BLOCK_SIZE, ctx);
  672. while(b_pos < AES_BLOCK_SIZE && len)
  673. *obuf++ = *ibuf++ ^ buf[b_pos++], --len;
  674. if(len)
  675. ctr_inc(cbuf), b_pos = 0;
  676. }
  677. while(len)
  678. {
  679. blen = (len > BFR_LENGTH ? BFR_LENGTH : len), len -= blen;
  680. for(i = 0, ip = buf; i < (blen >> 4); ++i)
  681. {
  682. memcpy(ip, cbuf, AES_BLOCK_SIZE);
  683. ctr_inc(cbuf);
  684. ip += AES_BLOCK_SIZE;
  685. }
  686. if(blen & (AES_BLOCK_SIZE - 1))
  687. memcpy(ip, cbuf, AES_BLOCK_SIZE), i++;
  688. #if defined( USE_VIA_ACE_IF_PRESENT )
  689. if(ctx->inf.b[1] == 0xff)
  690. {
  691. via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
  692. via_ecb_op5((ctx->ks),cwd,buf,buf,i);
  693. }
  694. else
  695. #endif
  696. zrtp_bg_aes_ecb_encrypt(buf, buf, i * AES_BLOCK_SIZE, ctx);
  697. i = 0; ip = buf;
  698. # ifdef FAST_BUFFER_OPERATIONS
  699. if(!addr_offset( ibuf, 4 ) && !addr_offset( obuf, 4 ) && !addr_offset( ip, 4 ))
  700. while(i + AES_BLOCK_SIZE <= blen)
  701. {
  702. lp32(obuf)[0] = lp32(ibuf)[0] ^ lp32(ip)[0];
  703. lp32(obuf)[1] = lp32(ibuf)[1] ^ lp32(ip)[1];
  704. lp32(obuf)[2] = lp32(ibuf)[2] ^ lp32(ip)[2];
  705. lp32(obuf)[3] = lp32(ibuf)[3] ^ lp32(ip)[3];
  706. i += AES_BLOCK_SIZE;
  707. ip += AES_BLOCK_SIZE;
  708. ibuf += AES_BLOCK_SIZE;
  709. obuf += AES_BLOCK_SIZE;
  710. }
  711. else
  712. #endif
  713. while(i + AES_BLOCK_SIZE <= blen)
  714. {
  715. obuf[ 0] = ibuf[ 0] ^ ip[ 0]; obuf[ 1] = ibuf[ 1] ^ ip[ 1];
  716. obuf[ 2] = ibuf[ 2] ^ ip[ 2]; obuf[ 3] = ibuf[ 3] ^ ip[ 3];
  717. obuf[ 4] = ibuf[ 4] ^ ip[ 4]; obuf[ 5] = ibuf[ 5] ^ ip[ 5];
  718. obuf[ 6] = ibuf[ 6] ^ ip[ 6]; obuf[ 7] = ibuf[ 7] ^ ip[ 7];
  719. obuf[ 8] = ibuf[ 8] ^ ip[ 8]; obuf[ 9] = ibuf[ 9] ^ ip[ 9];
  720. obuf[10] = ibuf[10] ^ ip[10]; obuf[11] = ibuf[11] ^ ip[11];
  721. obuf[12] = ibuf[12] ^ ip[12]; obuf[13] = ibuf[13] ^ ip[13];
  722. obuf[14] = ibuf[14] ^ ip[14]; obuf[15] = ibuf[15] ^ ip[15];
  723. i += AES_BLOCK_SIZE;
  724. ip += AES_BLOCK_SIZE;
  725. ibuf += AES_BLOCK_SIZE;
  726. obuf += AES_BLOCK_SIZE;
  727. }
  728. while(i++ < blen)
  729. *obuf++ = *ibuf++ ^ ip[b_pos++];
  730. }
  731. ctx->inf.b[2] = (uint_8t)b_pos;
  732. return EXIT_SUCCESS;
  733. }
  734. #if defined(__cplusplus)
  735. }
  736. #endif
  737. #endif