tls_aes128.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /******************************************************************************
  2. *
  3. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  4. *
  5. * This is a simple and straightforward implementation of the AES Rijndael
  6. * 128-bit block cipher designed by Vincent Rijmen and Joan Daemen. The focus
  7. * of this work was correctness & accuracy. It is written in 'C' without any
  8. * particular focus upon optimization or speed. It should be endian (memory
  9. * byte order) neutral since the few places that care are handled explicitly.
  10. *
  11. * This implementation of Rijndael was created by Steven M. Gibson of GRC.com.
  12. *
  13. * It is intended for general purpose use, but was written in support of GRC's
  14. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  15. *
  16. * See: http://csrc.nist.gov/archive/aes/rijndael/wsdindex.html
  17. *
  18. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  19. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  20. *
  21. *******************************************************************************/
  22. #include "tls_aes128.h"
  23. #include "tls.h"
  24. #if MG_TLS == MG_TLS_BUILTIN
  25. static int aes_tables_inited = 0; // run-once flag for performing key
  26. // expasion table generation (see below)
  27. /*
  28. * The following static local tables must be filled-in before the first use of
  29. * the GCM or AES ciphers. They are used for the AES key expansion/scheduling
  30. * and once built are read-only and thread safe. The "gcm_initialize" function
  31. * must be called once during system initialization to populate these arrays
  32. * for subsequent use by the AES key scheduler. If they have not been built
  33. * before attempted use, an error will be returned to the caller.
  34. *
  35. * NOTE: GCM Encryption/Decryption does NOT REQUIRE AES decryption. Since
  36. * GCM uses AES in counter-mode, where the AES cipher output is XORed with
  37. * the GCM input, we ONLY NEED AES encryption. Thus, to save space AES
  38. * decryption is typically disabled by setting AES_DECRYPTION to 0 in aes.h.
  39. */
  40. // We always need our forward tables
  41. static uchar FSb[256]; // Forward substitution box (FSb)
  42. static uint32_t FT0[256]; // Forward key schedule assembly tables
  43. static uint32_t FT1[256];
  44. static uint32_t FT2[256];
  45. static uint32_t FT3[256];
  46. #if AES_DECRYPTION // We ONLY need reverse for decryption
  47. static uchar RSb[256]; // Reverse substitution box (RSb)
  48. static uint32_t RT0[256]; // Reverse key schedule assembly tables
  49. static uint32_t RT1[256];
  50. static uint32_t RT2[256];
  51. static uint32_t RT3[256];
  52. #endif /* AES_DECRYPTION */
  53. static uint32_t RCON[10]; // AES round constants
  54. /*
  55. * Platform Endianness Neutralizing Load and Store Macro definitions
  56. * AES wants platform-neutral Little Endian (LE) byte ordering
  57. */
  58. #define GET_UINT32_LE(n, b, i) \
  59. { \
  60. (n) = ((uint32_t) (b)[(i)]) | ((uint32_t) (b)[(i) + 1] << 8) | \
  61. ((uint32_t) (b)[(i) + 2] << 16) | ((uint32_t) (b)[(i) + 3] << 24); \
  62. }
  63. #define PUT_UINT32_LE(n, b, i) \
  64. { \
  65. (b)[(i)] = (uchar) ((n)); \
  66. (b)[(i) + 1] = (uchar) ((n) >> 8); \
  67. (b)[(i) + 2] = (uchar) ((n) >> 16); \
  68. (b)[(i) + 3] = (uchar) ((n) >> 24); \
  69. }
  70. /*
  71. * AES forward and reverse encryption round processing macros
  72. */
  73. #define AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3) \
  74. { \
  75. X0 = *RK++ ^ FT0[(Y0) &0xFF] ^ FT1[(Y1 >> 8) & 0xFF] ^ \
  76. FT2[(Y2 >> 16) & 0xFF] ^ FT3[(Y3 >> 24) & 0xFF]; \
  77. \
  78. X1 = *RK++ ^ FT0[(Y1) &0xFF] ^ FT1[(Y2 >> 8) & 0xFF] ^ \
  79. FT2[(Y3 >> 16) & 0xFF] ^ FT3[(Y0 >> 24) & 0xFF]; \
  80. \
  81. X2 = *RK++ ^ FT0[(Y2) &0xFF] ^ FT1[(Y3 >> 8) & 0xFF] ^ \
  82. FT2[(Y0 >> 16) & 0xFF] ^ FT3[(Y1 >> 24) & 0xFF]; \
  83. \
  84. X3 = *RK++ ^ FT0[(Y3) &0xFF] ^ FT1[(Y0 >> 8) & 0xFF] ^ \
  85. FT2[(Y1 >> 16) & 0xFF] ^ FT3[(Y2 >> 24) & 0xFF]; \
  86. }
  87. #define AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3) \
  88. { \
  89. X0 = *RK++ ^ RT0[(Y0) &0xFF] ^ RT1[(Y3 >> 8) & 0xFF] ^ \
  90. RT2[(Y2 >> 16) & 0xFF] ^ RT3[(Y1 >> 24) & 0xFF]; \
  91. \
  92. X1 = *RK++ ^ RT0[(Y1) &0xFF] ^ RT1[(Y0 >> 8) & 0xFF] ^ \
  93. RT2[(Y3 >> 16) & 0xFF] ^ RT3[(Y2 >> 24) & 0xFF]; \
  94. \
  95. X2 = *RK++ ^ RT0[(Y2) &0xFF] ^ RT1[(Y1 >> 8) & 0xFF] ^ \
  96. RT2[(Y0 >> 16) & 0xFF] ^ RT3[(Y3 >> 24) & 0xFF]; \
  97. \
  98. X3 = *RK++ ^ RT0[(Y3) &0xFF] ^ RT1[(Y2 >> 8) & 0xFF] ^ \
  99. RT2[(Y1 >> 16) & 0xFF] ^ RT3[(Y0 >> 24) & 0xFF]; \
  100. }
  101. /*
  102. * These macros improve the readability of the key
  103. * generation initialization code by collapsing
  104. * repetitive common operations into logical pieces.
  105. */
  106. #define ROTL8(x) ((x << 8) & 0xFFFFFFFF) | (x >> 24)
  107. #define XTIME(x) ((x << 1) ^ ((x & 0x80) ? 0x1B : 0x00))
  108. #define MUL(x, y) ((x && y) ? pow[(log[x] + log[y]) % 255] : 0)
  109. #define MIX(x, y) \
  110. { \
  111. y = ((y << 1) | (y >> 7)) & 0xFF; \
  112. x ^= y; \
  113. }
  114. #define CPY128 \
  115. { \
  116. *RK++ = *SK++; \
  117. *RK++ = *SK++; \
  118. *RK++ = *SK++; \
  119. *RK++ = *SK++; \
  120. }
  121. /******************************************************************************
  122. *
  123. * AES_INIT_KEYGEN_TABLES
  124. *
  125. * Fills the AES key expansion tables allocated above with their static
  126. * data. This is not "per key" data, but static system-wide read-only
  127. * table data. THIS FUNCTION IS NOT THREAD SAFE. It must be called once
  128. * at system initialization to setup the tables for all subsequent use.
  129. *
  130. ******************************************************************************/
  131. void aes_init_keygen_tables(void) {
  132. int i, x, y, z; // general purpose iteration and computation locals
  133. int pow[256];
  134. int log[256];
  135. if (aes_tables_inited) return;
  136. // fill the 'pow' and 'log' tables over GF(2^8)
  137. for (i = 0, x = 1; i < 256; i++) {
  138. pow[i] = x;
  139. log[x] = i;
  140. x = (x ^ XTIME(x)) & 0xFF;
  141. }
  142. // compute the round constants
  143. for (i = 0, x = 1; i < 10; i++) {
  144. RCON[i] = (uint32_t) x;
  145. x = XTIME(x) & 0xFF;
  146. }
  147. // fill the forward and reverse substitution boxes
  148. FSb[0x00] = 0x63;
  149. #if AES_DECRYPTION // whether AES decryption is supported
  150. RSb[0x63] = 0x00;
  151. #endif /* AES_DECRYPTION */
  152. for (i = 1; i < 256; i++) {
  153. x = y = pow[255 - log[i]];
  154. MIX(x, y);
  155. MIX(x, y);
  156. MIX(x, y);
  157. MIX(x, y);
  158. FSb[i] = (uchar) (x ^= 0x63);
  159. #if AES_DECRYPTION // whether AES decryption is supported
  160. RSb[x] = (uchar) i;
  161. #endif /* AES_DECRYPTION */
  162. }
  163. // generate the forward and reverse key expansion tables
  164. for (i = 0; i < 256; i++) {
  165. x = FSb[i];
  166. y = XTIME(x) & 0xFF;
  167. z = (y ^ x) & 0xFF;
  168. FT0[i] = ((uint32_t) y) ^ ((uint32_t) x << 8) ^ ((uint32_t) x << 16) ^
  169. ((uint32_t) z << 24);
  170. FT1[i] = ROTL8(FT0[i]);
  171. FT2[i] = ROTL8(FT1[i]);
  172. FT3[i] = ROTL8(FT2[i]);
  173. #if AES_DECRYPTION // whether AES decryption is supported
  174. x = RSb[i];
  175. RT0[i] = ((uint32_t) MUL(0x0E, x)) ^ ((uint32_t) MUL(0x09, x) << 8) ^
  176. ((uint32_t) MUL(0x0D, x) << 16) ^ ((uint32_t) MUL(0x0B, x) << 24);
  177. RT1[i] = ROTL8(RT0[i]);
  178. RT2[i] = ROTL8(RT1[i]);
  179. RT3[i] = ROTL8(RT2[i]);
  180. #endif /* AES_DECRYPTION */
  181. }
  182. aes_tables_inited = 1; // flag that the tables have been generated
  183. } // to permit subsequent use of the AES cipher
  184. /******************************************************************************
  185. *
  186. * AES_SET_ENCRYPTION_KEY
  187. *
  188. * This is called by 'aes_setkey' when we're establishing a key for
  189. * subsequent encryption. We give it a pointer to the encryption
  190. * context, a pointer to the key, and the key's length in bytes.
  191. * Valid lengths are: 16, 24 or 32 bytes (128, 192, 256 bits).
  192. *
  193. ******************************************************************************/
  194. static int aes_set_encryption_key(aes_context *ctx, const uchar *key, uint keysize) {
  195. uint i; // general purpose iteration local
  196. uint32_t *RK = ctx->rk; // initialize our RoundKey buffer pointer
  197. for (i = 0; i < (keysize >> 2); i++) {
  198. GET_UINT32_LE(RK[i], key, i << 2);
  199. }
  200. switch (ctx->rounds) {
  201. case 10:
  202. for (i = 0; i < 10; i++, RK += 4) {
  203. RK[4] = RK[0] ^ RCON[i] ^ ((uint32_t) FSb[(RK[3] >> 8) & 0xFF]) ^
  204. ((uint32_t) FSb[(RK[3] >> 16) & 0xFF] << 8) ^
  205. ((uint32_t) FSb[(RK[3] >> 24) & 0xFF] << 16) ^
  206. ((uint32_t) FSb[(RK[3]) & 0xFF] << 24);
  207. RK[5] = RK[1] ^ RK[4];
  208. RK[6] = RK[2] ^ RK[5];
  209. RK[7] = RK[3] ^ RK[6];
  210. }
  211. break;
  212. case 12:
  213. for (i = 0; i < 8; i++, RK += 6) {
  214. RK[6] = RK[0] ^ RCON[i] ^ ((uint32_t) FSb[(RK[5] >> 8) & 0xFF]) ^
  215. ((uint32_t) FSb[(RK[5] >> 16) & 0xFF] << 8) ^
  216. ((uint32_t) FSb[(RK[5] >> 24) & 0xFF] << 16) ^
  217. ((uint32_t) FSb[(RK[5]) & 0xFF] << 24);
  218. RK[7] = RK[1] ^ RK[6];
  219. RK[8] = RK[2] ^ RK[7];
  220. RK[9] = RK[3] ^ RK[8];
  221. RK[10] = RK[4] ^ RK[9];
  222. RK[11] = RK[5] ^ RK[10];
  223. }
  224. break;
  225. case 14:
  226. for (i = 0; i < 7; i++, RK += 8) {
  227. RK[8] = RK[0] ^ RCON[i] ^ ((uint32_t) FSb[(RK[7] >> 8) & 0xFF]) ^
  228. ((uint32_t) FSb[(RK[7] >> 16) & 0xFF] << 8) ^
  229. ((uint32_t) FSb[(RK[7] >> 24) & 0xFF] << 16) ^
  230. ((uint32_t) FSb[(RK[7]) & 0xFF] << 24);
  231. RK[9] = RK[1] ^ RK[8];
  232. RK[10] = RK[2] ^ RK[9];
  233. RK[11] = RK[3] ^ RK[10];
  234. RK[12] = RK[4] ^ ((uint32_t) FSb[(RK[11]) & 0xFF]) ^
  235. ((uint32_t) FSb[(RK[11] >> 8) & 0xFF] << 8) ^
  236. ((uint32_t) FSb[(RK[11] >> 16) & 0xFF] << 16) ^
  237. ((uint32_t) FSb[(RK[11] >> 24) & 0xFF] << 24);
  238. RK[13] = RK[5] ^ RK[12];
  239. RK[14] = RK[6] ^ RK[13];
  240. RK[15] = RK[7] ^ RK[14];
  241. }
  242. break;
  243. default:
  244. return -1;
  245. }
  246. return (0);
  247. }
  248. #if AES_DECRYPTION // whether AES decryption is supported
  249. /******************************************************************************
  250. *
  251. * AES_SET_DECRYPTION_KEY
  252. *
  253. * This is called by 'aes_setkey' when we're establishing a
  254. * key for subsequent decryption. We give it a pointer to
  255. * the encryption context, a pointer to the key, and the key's
  256. * length in bits. Valid lengths are: 128, 192, or 256 bits.
  257. *
  258. ******************************************************************************/
  259. static int aes_set_decryption_key(aes_context *ctx, const uchar *key, uint keysize) {
  260. int i, j;
  261. aes_context cty; // a calling aes context for set_encryption_key
  262. uint32_t *RK = ctx->rk; // initialize our RoundKey buffer pointer
  263. uint32_t *SK;
  264. int ret;
  265. cty.rounds = ctx->rounds; // initialize our local aes context
  266. cty.rk = cty.buf; // round count and key buf pointer
  267. if ((ret = aes_set_encryption_key(&cty, key, keysize)) != 0) return (ret);
  268. SK = cty.rk + cty.rounds * 4;
  269. CPY128 // copy a 128-bit block from *SK to *RK
  270. for (i = ctx->rounds - 1, SK -= 8; i > 0; i--, SK -= 8) {
  271. for (j = 0; j < 4; j++, SK++) {
  272. *RK++ = RT0[FSb[(*SK) & 0xFF]] ^ RT1[FSb[(*SK >> 8) & 0xFF]] ^
  273. RT2[FSb[(*SK >> 16) & 0xFF]] ^ RT3[FSb[(*SK >> 24) & 0xFF]];
  274. }
  275. }
  276. CPY128 // copy a 128-bit block from *SK to *RK
  277. memset(&cty, 0, sizeof(aes_context)); // clear local aes context
  278. return (0);
  279. }
  280. #endif /* AES_DECRYPTION */
  281. /******************************************************************************
  282. *
  283. * AES_SETKEY
  284. *
  285. * Invoked to establish the key schedule for subsequent encryption/decryption
  286. *
  287. ******************************************************************************/
  288. int aes_setkey(aes_context *ctx, // AES context provided by our caller
  289. int mode, // ENCRYPT or DECRYPT flag
  290. const uchar *key, // pointer to the key
  291. uint keysize) // key length in bytes
  292. {
  293. // since table initialization is not thread safe, we could either add
  294. // system-specific mutexes and init the AES key generation tables on
  295. // demand, or ask the developer to simply call "gcm_initialize" once during
  296. // application startup before threading begins. That's what we choose.
  297. if (!aes_tables_inited) return (-1); // fail the call when not inited.
  298. ctx->mode = mode; // capture the key type we're creating
  299. ctx->rk = ctx->buf; // initialize our round key pointer
  300. switch (keysize) // set the rounds count based upon the keysize
  301. {
  302. case 16:
  303. ctx->rounds = 10;
  304. break; // 16-byte, 128-bit key
  305. case 24:
  306. ctx->rounds = 12;
  307. break; // 24-byte, 192-bit key
  308. case 32:
  309. ctx->rounds = 14;
  310. break; // 32-byte, 256-bit key
  311. default:
  312. return (-1);
  313. }
  314. #if AES_DECRYPTION
  315. if (mode == DECRYPT) // expand our key for encryption or decryption
  316. return (aes_set_decryption_key(ctx, key, keysize));
  317. else /* ENCRYPT */
  318. #endif /* AES_DECRYPTION */
  319. return (aes_set_encryption_key(ctx, key, keysize));
  320. }
  321. /******************************************************************************
  322. *
  323. * AES_CIPHER
  324. *
  325. * Perform AES encryption and decryption.
  326. * The AES context will have been setup with the encryption mode
  327. * and all keying information appropriate for the task.
  328. *
  329. ******************************************************************************/
  330. int aes_cipher(aes_context *ctx, const uchar input[16], uchar output[16]) {
  331. int i;
  332. uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; // general purpose locals
  333. RK = ctx->rk;
  334. GET_UINT32_LE(X0, input, 0);
  335. X0 ^= *RK++; // load our 128-bit
  336. GET_UINT32_LE(X1, input, 4);
  337. X1 ^= *RK++; // input buffer in a storage
  338. GET_UINT32_LE(X2, input, 8);
  339. X2 ^= *RK++; // memory endian-neutral way
  340. GET_UINT32_LE(X3, input, 12);
  341. X3 ^= *RK++;
  342. #if AES_DECRYPTION // whether AES decryption is supported
  343. if (ctx->mode == DECRYPT) {
  344. for (i = (ctx->rounds >> 1) - 1; i > 0; i--) {
  345. AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  346. AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
  347. }
  348. AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  349. X0 = *RK++ ^ ((uint32_t) RSb[(Y0) &0xFF]) ^
  350. ((uint32_t) RSb[(Y3 >> 8) & 0xFF] << 8) ^
  351. ((uint32_t) RSb[(Y2 >> 16) & 0xFF] << 16) ^
  352. ((uint32_t) RSb[(Y1 >> 24) & 0xFF] << 24);
  353. X1 = *RK++ ^ ((uint32_t) RSb[(Y1) &0xFF]) ^
  354. ((uint32_t) RSb[(Y0 >> 8) & 0xFF] << 8) ^
  355. ((uint32_t) RSb[(Y3 >> 16) & 0xFF] << 16) ^
  356. ((uint32_t) RSb[(Y2 >> 24) & 0xFF] << 24);
  357. X2 = *RK++ ^ ((uint32_t) RSb[(Y2) &0xFF]) ^
  358. ((uint32_t) RSb[(Y1 >> 8) & 0xFF] << 8) ^
  359. ((uint32_t) RSb[(Y0 >> 16) & 0xFF] << 16) ^
  360. ((uint32_t) RSb[(Y3 >> 24) & 0xFF] << 24);
  361. X3 = *RK++ ^ ((uint32_t) RSb[(Y3) &0xFF]) ^
  362. ((uint32_t) RSb[(Y2 >> 8) & 0xFF] << 8) ^
  363. ((uint32_t) RSb[(Y1 >> 16) & 0xFF] << 16) ^
  364. ((uint32_t) RSb[(Y0 >> 24) & 0xFF] << 24);
  365. } else /* ENCRYPT */
  366. {
  367. #endif /* AES_DECRYPTION */
  368. for (i = (ctx->rounds >> 1) - 1; i > 0; i--) {
  369. AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  370. AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
  371. }
  372. AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
  373. X0 = *RK++ ^ ((uint32_t) FSb[(Y0) &0xFF]) ^
  374. ((uint32_t) FSb[(Y1 >> 8) & 0xFF] << 8) ^
  375. ((uint32_t) FSb[(Y2 >> 16) & 0xFF] << 16) ^
  376. ((uint32_t) FSb[(Y3 >> 24) & 0xFF] << 24);
  377. X1 = *RK++ ^ ((uint32_t) FSb[(Y1) &0xFF]) ^
  378. ((uint32_t) FSb[(Y2 >> 8) & 0xFF] << 8) ^
  379. ((uint32_t) FSb[(Y3 >> 16) & 0xFF] << 16) ^
  380. ((uint32_t) FSb[(Y0 >> 24) & 0xFF] << 24);
  381. X2 = *RK++ ^ ((uint32_t) FSb[(Y2) &0xFF]) ^
  382. ((uint32_t) FSb[(Y3 >> 8) & 0xFF] << 8) ^
  383. ((uint32_t) FSb[(Y0 >> 16) & 0xFF] << 16) ^
  384. ((uint32_t) FSb[(Y1 >> 24) & 0xFF] << 24);
  385. X3 = *RK++ ^ ((uint32_t) FSb[(Y3) &0xFF]) ^
  386. ((uint32_t) FSb[(Y0 >> 8) & 0xFF] << 8) ^
  387. ((uint32_t) FSb[(Y1 >> 16) & 0xFF] << 16) ^
  388. ((uint32_t) FSb[(Y2 >> 24) & 0xFF] << 24);
  389. #if AES_DECRYPTION // whether AES decryption is supported
  390. }
  391. #endif /* AES_DECRYPTION */
  392. PUT_UINT32_LE(X0, output, 0);
  393. PUT_UINT32_LE(X1, output, 4);
  394. PUT_UINT32_LE(X2, output, 8);
  395. PUT_UINT32_LE(X3, output, 12);
  396. return (0);
  397. }
  398. /* end of aes.c */
  399. /******************************************************************************
  400. *
  401. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  402. *
  403. * This is a simple and straightforward implementation of AES-GCM authenticated
  404. * encryption. The focus of this work was correctness & accuracy. It is written
  405. * in straight 'C' without any particular focus upon optimization or speed. It
  406. * should be endian (memory byte order) neutral since the few places that care
  407. * are handled explicitly.
  408. *
  409. * This implementation of AES-GCM was created by Steven M. Gibson of GRC.com.
  410. *
  411. * It is intended for general purpose use, but was written in support of GRC's
  412. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  413. *
  414. * See: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
  415. * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/
  416. * gcm/gcm-revised-spec.pdf
  417. *
  418. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  419. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  420. *
  421. *******************************************************************************/
  422. /******************************************************************************
  423. * ==== IMPLEMENTATION WARNING ====
  424. *
  425. * This code was developed for use within SQRL's fixed environmnent. Thus, it
  426. * is somewhat less "general purpose" than it would be if it were designed as
  427. * a general purpose AES-GCM library. Specifically, it bothers with almost NO
  428. * error checking on parameter limits, buffer bounds, etc. It assumes that it
  429. * is being invoked by its author or by someone who understands the values it
  430. * expects to receive. Its behavior will be undefined otherwise.
  431. *
  432. * All functions that might fail are defined to return 'ints' to indicate a
  433. * problem. Most do not do so now. But this allows for error propagation out
  434. * of internal functions if robust error checking should ever be desired.
  435. *
  436. ******************************************************************************/
  437. /* Calculating the "GHASH"
  438. *
  439. * There are many ways of calculating the so-called GHASH in software, each with
  440. * a traditional size vs performance tradeoff. The GHASH (Galois field hash) is
  441. * an intriguing construction which takes two 128-bit strings (also the cipher's
  442. * block size and the fundamental operation size for the system) and hashes them
  443. * into a third 128-bit result.
  444. *
  445. * Many implementation solutions have been worked out that use large precomputed
  446. * table lookups in place of more time consuming bit fiddling, and this approach
  447. * can be scaled easily upward or downward as needed to change the time/space
  448. * tradeoff. It's been studied extensively and there's a solid body of theory
  449. * and practice. For example, without using any lookup tables an implementation
  450. * might obtain 119 cycles per byte throughput, whereas using a simple, though
  451. * large, key-specific 64 kbyte 8-bit lookup table the performance jumps to 13
  452. * cycles per byte.
  453. *
  454. * And Intel's processors have, since 2010, included an instruction which does
  455. * the entire 128x128->128 bit job in just several 64x64->128 bit pieces.
  456. *
  457. * Since SQRL is interactive, and only processing a few 128-bit blocks, I've
  458. * settled upon a relatively slower but appealing small-table compromise which
  459. * folds a bunch of not only time consuming but also bit twiddling into a simple
  460. * 16-entry table which is attributed to Victor Shoup's 1996 work while at
  461. * Bellcore: "On Fast and Provably Secure MessageAuthentication Based on
  462. * Universal Hashing." See: http://www.shoup.net/papers/macs.pdf
  463. * See, also section 4.1 of the "gcm-revised-spec" cited above.
  464. */
  465. /*
  466. * This 16-entry table of pre-computed constants is used by the
  467. * GHASH multiplier to improve over a strictly table-free but
  468. * significantly slower 128x128 bit multiple within GF(2^128).
  469. */
  470. static const uint64_t last4[16] = {
  471. 0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
  472. 0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0};
  473. /*
  474. * Platform Endianness Neutralizing Load and Store Macro definitions
  475. * GCM wants platform-neutral Big Endian (BE) byte ordering
  476. */
  477. #define GET_UINT32_BE(n, b, i) \
  478. { \
  479. (n) = ((uint32_t) (b)[(i)] << 24) | ((uint32_t) (b)[(i) + 1] << 16) | \
  480. ((uint32_t) (b)[(i) + 2] << 8) | ((uint32_t) (b)[(i) + 3]); \
  481. }
  482. #define PUT_UINT32_BE(n, b, i) \
  483. { \
  484. (b)[(i)] = (uchar) ((n) >> 24); \
  485. (b)[(i) + 1] = (uchar) ((n) >> 16); \
  486. (b)[(i) + 2] = (uchar) ((n) >> 8); \
  487. (b)[(i) + 3] = (uchar) ((n)); \
  488. }
  489. /******************************************************************************
  490. *
  491. * GCM_INITIALIZE
  492. *
  493. * Must be called once to initialize the GCM library.
  494. *
  495. * At present, this only calls the AES keygen table generator, which expands
  496. * the AES keying tables for use. This is NOT A THREAD-SAFE function, so it
  497. * MUST be called during system initialization before a multi-threading
  498. * environment is running.
  499. *
  500. ******************************************************************************/
  501. int gcm_initialize(void) {
  502. aes_init_keygen_tables();
  503. return (0);
  504. }
  505. /******************************************************************************
  506. *
  507. * GCM_MULT
  508. *
  509. * Performs a GHASH operation on the 128-bit input vector 'x', setting
  510. * the 128-bit output vector to 'x' times H using our precomputed tables.
  511. * 'x' and 'output' are seen as elements of GCM's GF(2^128) Galois field.
  512. *
  513. ******************************************************************************/
  514. static void gcm_mult(gcm_context *ctx, // pointer to established context
  515. const uchar x[16], // pointer to 128-bit input vector
  516. uchar output[16]) // pointer to 128-bit output vector
  517. {
  518. int i;
  519. uchar lo, hi, rem;
  520. uint64_t zh, zl;
  521. lo = (uchar) (x[15] & 0x0f);
  522. hi = (uchar) (x[15] >> 4);
  523. zh = ctx->HH[lo];
  524. zl = ctx->HL[lo];
  525. for (i = 15; i >= 0; i--) {
  526. lo = (uchar) (x[i] & 0x0f);
  527. hi = (uchar) (x[i] >> 4);
  528. if (i != 15) {
  529. rem = (uchar) (zl & 0x0f);
  530. zl = (zh << 60) | (zl >> 4);
  531. zh = (zh >> 4);
  532. zh ^= (uint64_t) last4[rem] << 48;
  533. zh ^= ctx->HH[lo];
  534. zl ^= ctx->HL[lo];
  535. }
  536. rem = (uchar) (zl & 0x0f);
  537. zl = (zh << 60) | (zl >> 4);
  538. zh = (zh >> 4);
  539. zh ^= (uint64_t) last4[rem] << 48;
  540. zh ^= ctx->HH[hi];
  541. zl ^= ctx->HL[hi];
  542. }
  543. PUT_UINT32_BE(zh >> 32, output, 0);
  544. PUT_UINT32_BE(zh, output, 4);
  545. PUT_UINT32_BE(zl >> 32, output, 8);
  546. PUT_UINT32_BE(zl, output, 12);
  547. }
  548. /******************************************************************************
  549. *
  550. * GCM_SETKEY
  551. *
  552. * This is called to set the AES-GCM key. It initializes the AES key
  553. * and populates the gcm context's pre-calculated HTables.
  554. *
  555. ******************************************************************************/
  556. int gcm_setkey(gcm_context *ctx, // pointer to caller-provided gcm context
  557. const uchar *key, // pointer to the AES encryption key
  558. const uint keysize) // size in bytes (must be 16, 24, 32 for
  559. // 128, 192 or 256-bit keys respectively)
  560. {
  561. int ret, i, j;
  562. uint64_t hi, lo;
  563. uint64_t vl, vh;
  564. unsigned char h[16];
  565. memset(ctx, 0, sizeof(gcm_context)); // zero caller-provided GCM context
  566. memset(h, 0, 16); // initialize the block to encrypt
  567. // encrypt the null 128-bit block to generate a key-based value
  568. // which is then used to initialize our GHASH lookup tables
  569. if ((ret = aes_setkey(&ctx->aes_ctx, ENCRYPT, key, keysize)) != 0)
  570. return (ret);
  571. if ((ret = aes_cipher(&ctx->aes_ctx, h, h)) != 0) return (ret);
  572. GET_UINT32_BE(hi, h, 0); // pack h as two 64-bit ints, big-endian
  573. GET_UINT32_BE(lo, h, 4);
  574. vh = (uint64_t) hi << 32 | lo;
  575. GET_UINT32_BE(hi, h, 8);
  576. GET_UINT32_BE(lo, h, 12);
  577. vl = (uint64_t) hi << 32 | lo;
  578. ctx->HL[8] = vl; // 8 = 1000 corresponds to 1 in GF(2^128)
  579. ctx->HH[8] = vh;
  580. ctx->HH[0] = 0; // 0 corresponds to 0 in GF(2^128)
  581. ctx->HL[0] = 0;
  582. for (i = 4; i > 0; i >>= 1) {
  583. uint32_t T = (uint32_t) (vl & 1) * 0xe1000000U;
  584. vl = (vh << 63) | (vl >> 1);
  585. vh = (vh >> 1) ^ ((uint64_t) T << 32);
  586. ctx->HL[i] = vl;
  587. ctx->HH[i] = vh;
  588. }
  589. for (i = 2; i < 16; i <<= 1) {
  590. uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
  591. vh = *HiH;
  592. vl = *HiL;
  593. for (j = 1; j < i; j++) {
  594. HiH[j] = vh ^ ctx->HH[j];
  595. HiL[j] = vl ^ ctx->HL[j];
  596. }
  597. }
  598. return (0);
  599. }
  600. /******************************************************************************
  601. *
  602. * GCM processing occurs four phases: SETKEY, START, UPDATE and FINISH.
  603. *
  604. * SETKEY:
  605. *
  606. * START: Sets the Encryption/Decryption mode.
  607. * Accepts the initialization vector and additional data.
  608. *
  609. * UPDATE: Encrypts or decrypts the plaintext or ciphertext.
  610. *
  611. * FINISH: Performs a final GHASH to generate the authentication tag.
  612. *
  613. ******************************************************************************
  614. *
  615. * GCM_START
  616. *
  617. * Given a user-provided GCM context, this initializes it, sets the encryption
  618. * mode, and preprocesses the initialization vector and additional AEAD data.
  619. *
  620. ******************************************************************************/
  621. int gcm_start(gcm_context *ctx, // pointer to user-provided GCM context
  622. int mode, // GCM_ENCRYPT or GCM_DECRYPT
  623. const uchar *iv, // pointer to initialization vector
  624. size_t iv_len, // IV length in bytes (should == 12)
  625. const uchar *add, // ptr to additional AEAD data (NULL if none)
  626. size_t add_len) // length of additional AEAD data (bytes)
  627. {
  628. int ret; // our error return if the AES encrypt fails
  629. uchar work_buf[16]; // XOR source built from provided IV if len != 16
  630. const uchar *p; // general purpose array pointer
  631. size_t use_len; // byte count to process, up to 16 bytes
  632. size_t i; // local loop iterator
  633. // since the context might be reused under the same key
  634. // we zero the working buffers for this next new process
  635. memset(ctx->y, 0x00, sizeof(ctx->y));
  636. memset(ctx->buf, 0x00, sizeof(ctx->buf));
  637. ctx->len = 0;
  638. ctx->add_len = 0;
  639. ctx->mode = mode; // set the GCM encryption/decryption mode
  640. ctx->aes_ctx.mode = ENCRYPT; // GCM *always* runs AES in ENCRYPTION mode
  641. if (iv_len == 12) { // GCM natively uses a 12-byte, 96-bit IV
  642. memcpy(ctx->y, iv, iv_len); // copy the IV to the top of the 'y' buff
  643. ctx->y[15] = 1; // start "counting" from 1 (not 0)
  644. } else // if we don't have a 12-byte IV, we GHASH whatever we've been given
  645. {
  646. memset(work_buf, 0x00, 16); // clear the working buffer
  647. PUT_UINT32_BE(iv_len * 8, work_buf, 12); // place the IV into buffer
  648. p = iv;
  649. while (iv_len > 0) {
  650. use_len = (iv_len < 16) ? iv_len : 16;
  651. for (i = 0; i < use_len; i++) ctx->y[i] ^= p[i];
  652. gcm_mult(ctx, ctx->y, ctx->y);
  653. iv_len -= use_len;
  654. p += use_len;
  655. }
  656. for (i = 0; i < 16; i++) ctx->y[i] ^= work_buf[i];
  657. gcm_mult(ctx, ctx->y, ctx->y);
  658. }
  659. if ((ret = aes_cipher(&ctx->aes_ctx, ctx->y, ctx->base_ectr)) != 0)
  660. return (ret);
  661. ctx->add_len = add_len;
  662. p = add;
  663. while (add_len > 0) {
  664. use_len = (add_len < 16) ? add_len : 16;
  665. for (i = 0; i < use_len; i++) ctx->buf[i] ^= p[i];
  666. gcm_mult(ctx, ctx->buf, ctx->buf);
  667. add_len -= use_len;
  668. p += use_len;
  669. }
  670. return (0);
  671. }
  672. /******************************************************************************
  673. *
  674. * GCM_UPDATE
  675. *
  676. * This is called once or more to process bulk plaintext or ciphertext data.
  677. * We give this some number of bytes of input and it returns the same number
  678. * of output bytes. If called multiple times (which is fine) all but the final
  679. * invocation MUST be called with length mod 16 == 0. (Only the final call can
  680. * have a partial block length of < 128 bits.)
  681. *
  682. ******************************************************************************/
  683. int gcm_update(gcm_context *ctx, // pointer to user-provided GCM context
  684. size_t length, // length, in bytes, of data to process
  685. const uchar *input, // pointer to source data
  686. uchar *output) // pointer to destination data
  687. {
  688. int ret; // our error return if the AES encrypt fails
  689. uchar ectr[16]; // counter-mode cipher output for XORing
  690. size_t use_len; // byte count to process, up to 16 bytes
  691. size_t i; // local loop iterator
  692. ctx->len += length; // bump the GCM context's running length count
  693. while (length > 0) {
  694. // clamp the length to process at 16 bytes
  695. use_len = (length < 16) ? length : 16;
  696. // increment the context's 128-bit IV||Counter 'y' vector
  697. for (i = 16; i > 12; i--)
  698. if (++ctx->y[i - 1] != 0) break;
  699. // encrypt the context's 'y' vector under the established key
  700. if ((ret = aes_cipher(&ctx->aes_ctx, ctx->y, ectr)) != 0) return (ret);
  701. // encrypt or decrypt the input to the output
  702. if (ctx->mode == ENCRYPT) {
  703. for (i = 0; i < use_len; i++) {
  704. // XOR the cipher's ouptut vector (ectr) with our input
  705. output[i] = (uchar) (ectr[i] ^ input[i]);
  706. // now we mix in our data into the authentication hash.
  707. // if we're ENcrypting we XOR in the post-XOR (output)
  708. // results, but if we're DEcrypting we XOR in the input
  709. // data
  710. ctx->buf[i] ^= output[i];
  711. }
  712. } else {
  713. for (i = 0; i < use_len; i++) {
  714. // but if we're DEcrypting we XOR in the input data first,
  715. // i.e. before saving to ouput data, otherwise if the input
  716. // and output buffer are the same (inplace decryption) we
  717. // would not get the correct auth tag
  718. ctx->buf[i] ^= input[i];
  719. // XOR the cipher's ouptut vector (ectr) with our input
  720. output[i] = (uchar) (ectr[i] ^ input[i]);
  721. }
  722. }
  723. gcm_mult(ctx, ctx->buf, ctx->buf); // perform a GHASH operation
  724. length -= use_len; // drop the remaining byte count to process
  725. input += use_len; // bump our input pointer forward
  726. output += use_len; // bump our output pointer forward
  727. }
  728. return (0);
  729. }
  730. /******************************************************************************
  731. *
  732. * GCM_FINISH
  733. *
  734. * This is called once after all calls to GCM_UPDATE to finalize the GCM.
  735. * It performs the final GHASH to produce the resulting authentication TAG.
  736. *
  737. ******************************************************************************/
  738. int gcm_finish(gcm_context *ctx, // pointer to user-provided GCM context
  739. uchar *tag, // pointer to buffer which receives the tag
  740. size_t tag_len) // length, in bytes, of the tag-receiving buf
  741. {
  742. uchar work_buf[16];
  743. uint64_t orig_len = ctx->len * 8;
  744. uint64_t orig_add_len = ctx->add_len * 8;
  745. size_t i;
  746. if (tag_len != 0) memcpy(tag, ctx->base_ectr, tag_len);
  747. if (orig_len || orig_add_len) {
  748. memset(work_buf, 0x00, 16);
  749. PUT_UINT32_BE((orig_add_len >> 32), work_buf, 0);
  750. PUT_UINT32_BE((orig_add_len), work_buf, 4);
  751. PUT_UINT32_BE((orig_len >> 32), work_buf, 8);
  752. PUT_UINT32_BE((orig_len), work_buf, 12);
  753. for (i = 0; i < 16; i++) ctx->buf[i] ^= work_buf[i];
  754. gcm_mult(ctx, ctx->buf, ctx->buf);
  755. for (i = 0; i < tag_len; i++) tag[i] ^= ctx->buf[i];
  756. }
  757. return (0);
  758. }
  759. /******************************************************************************
  760. *
  761. * GCM_CRYPT_AND_TAG
  762. *
  763. * This either encrypts or decrypts the user-provided data and, either
  764. * way, generates an authentication tag of the requested length. It must be
  765. * called with a GCM context whose key has already been set with GCM_SETKEY.
  766. *
  767. * The user would typically call this explicitly to ENCRYPT a buffer of data
  768. * and optional associated data, and produce its an authentication tag.
  769. *
  770. * To reverse the process the user would typically call the companion
  771. * GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
  772. * authentication tag. The GCM_AUTH_DECRYPT function calls this function
  773. * to perform its decryption and tag generation, which it then compares.
  774. *
  775. ******************************************************************************/
  776. int gcm_crypt_and_tag(
  777. gcm_context *ctx, // gcm context with key already setup
  778. int mode, // cipher direction: GCM_ENCRYPT or GCM_DECRYPT
  779. const uchar *iv, // pointer to the 12-byte initialization vector
  780. size_t iv_len, // byte length if the IV. should always be 12
  781. const uchar *add, // pointer to the non-ciphered additional data
  782. size_t add_len, // byte length of the additional AEAD data
  783. const uchar *input, // pointer to the cipher data source
  784. uchar *output, // pointer to the cipher data destination
  785. size_t length, // byte length of the cipher data
  786. uchar *tag, // pointer to the tag to be generated
  787. size_t tag_len) // byte length of the tag to be generated
  788. { /*
  789. assuming that the caller has already invoked gcm_setkey to
  790. prepare the gcm context with the keying material, we simply
  791. invoke each of the three GCM sub-functions in turn...
  792. */
  793. gcm_start(ctx, mode, iv, iv_len, add, add_len);
  794. gcm_update(ctx, length, input, output);
  795. gcm_finish(ctx, tag, tag_len);
  796. return (0);
  797. }
  798. /******************************************************************************
  799. *
  800. * GCM_AUTH_DECRYPT
  801. *
  802. * This DECRYPTS a user-provided data buffer with optional associated data.
  803. * It then verifies a user-supplied authentication tag against the tag just
  804. * re-created during decryption to verify that the data has not been altered.
  805. *
  806. * This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
  807. * and authentication tag generation.
  808. *
  809. ******************************************************************************/
  810. int gcm_auth_decrypt(
  811. gcm_context *ctx, // gcm context with key already setup
  812. const uchar *iv, // pointer to the 12-byte initialization vector
  813. size_t iv_len, // byte length if the IV. should always be 12
  814. const uchar *add, // pointer to the non-ciphered additional data
  815. size_t add_len, // byte length of the additional AEAD data
  816. const uchar *input, // pointer to the cipher data source
  817. uchar *output, // pointer to the cipher data destination
  818. size_t length, // byte length of the cipher data
  819. const uchar *tag, // pointer to the tag to be authenticated
  820. size_t tag_len) // byte length of the tag <= 16
  821. {
  822. uchar check_tag[16]; // the tag generated and returned by decryption
  823. int diff; // an ORed flag to detect authentication errors
  824. size_t i; // our local iterator
  825. /*
  826. we use GCM_DECRYPT_AND_TAG (above) to perform our decryption
  827. (which is an identical XORing to reverse the previous one)
  828. and also to re-generate the matching authentication tag
  829. */
  830. gcm_crypt_and_tag(ctx, DECRYPT, iv, iv_len, add, add_len, input, output,
  831. length, check_tag, tag_len);
  832. // now we verify the authentication tag in 'constant time'
  833. for (diff = 0, i = 0; i < tag_len; i++) diff |= tag[i] ^ check_tag[i];
  834. if (diff != 0) { // see whether any bits differed?
  835. memset(output, 0, length); // if so... wipe the output data
  836. return (GCM_AUTH_FAILURE); // return GCM_AUTH_FAILURE
  837. }
  838. return (0);
  839. }
  840. /******************************************************************************
  841. *
  842. * GCM_ZERO_CTX
  843. *
  844. * The GCM context contains both the GCM context and the AES context.
  845. * This includes keying and key-related material which is security-
  846. * sensitive, so it MUST be zeroed after use. This function does that.
  847. *
  848. ******************************************************************************/
  849. void gcm_zero_ctx(gcm_context *ctx) {
  850. // zero the context originally provided to us
  851. memset(ctx, 0, sizeof(gcm_context));
  852. }
  853. //
  854. // aes-gcm.c
  855. // Pods
  856. //
  857. // Created by Markus Kosmal on 20/11/14.
  858. //
  859. //
  860. int aes_gcm_encrypt(unsigned char *output, //
  861. const unsigned char *input, size_t input_length,
  862. const unsigned char *key, const size_t key_len,
  863. const unsigned char *iv, const size_t iv_len,
  864. unsigned char *aead, size_t aead_len, unsigned char *tag,
  865. const size_t tag_len) {
  866. int ret = 0; // our return value
  867. gcm_context ctx; // includes the AES context structure
  868. gcm_setkey(&ctx, key, (const uint) key_len);
  869. ret = gcm_crypt_and_tag(&ctx, ENCRYPT, iv, iv_len, aead, aead_len, input, output,
  870. input_length, tag, tag_len);
  871. gcm_zero_ctx(&ctx);
  872. return (ret);
  873. }
  874. int aes_gcm_decrypt(unsigned char *output, const unsigned char *input,
  875. size_t input_length, const unsigned char *key,
  876. const size_t key_len, const unsigned char *iv,
  877. const size_t iv_len) {
  878. int ret = 0; // our return value
  879. gcm_context ctx; // includes the AES context structure
  880. size_t tag_len = 0;
  881. unsigned char *tag_buf = NULL;
  882. gcm_setkey(&ctx, key, (const uint) key_len);
  883. ret = gcm_crypt_and_tag(&ctx, DECRYPT, iv, iv_len, NULL, 0, input, output,
  884. input_length, tag_buf, tag_len);
  885. gcm_zero_ctx(&ctx);
  886. return (ret);
  887. }
  888. #endif
  889. // End of aes128 PD