aes.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. aes.c
  5. Abstract:
  6. This module implements the AES encryption and decryption routines.
  7. Author:
  8. Evan Green 13-Jan-2015
  9. Environment:
  10. Any
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "cryptop.h"
  16. //
  17. // --------------------------------------------------------------------- Macros
  18. //
  19. //
  20. // This macro performs a simple byte swap on a 32 bit value.
  21. //
  22. #define AES_BYTE_SWAP32(_Value) \
  23. ((((ULONG)(_Value) & 0xFF000000) >> 24) | \
  24. (((ULONG)(_Value) & 0x00FF0000) >> 8) | \
  25. (((ULONG)(_Value) & 0x0000FF00) << 8) | \
  26. (((ULONG)(_Value) & 0x000000FF) << 24))
  27. //
  28. // Define rotation methods that rotate a 32 bit value by either 1 byte, 2 bytes,
  29. // or 3 bytes.
  30. //
  31. #define AES_ROTATE1(_Value) (((_Value) << 24) | ((_Value) >> 8))
  32. #define AES_ROTATE2(_Value) (((_Value) << 16) | ((_Value) >> 16))
  33. #define AES_ROTATE3(_Value) (((_Value) << 8) | ((_Value) >> 24))
  34. //
  35. // This macro does 4 multiplies by 2 in a finite field.
  36. //
  37. #define AES_FINITE_MULTIPLY_2(_Value, _TemporaryVariable) \
  38. ((_TemporaryVariable) = ((_Value) & 0x80808080), \
  39. ((((_Value) + (_Value)) & 0xFEFEFEFE) ^ \
  40. (((_TemporaryVariable) - ((_TemporaryVariable) >> 7)) & 0x1B1B1B1B)))
  41. //
  42. // This macro does the inverse mix columns operation.
  43. //
  44. #define AES_INVERSE_MIX_COLUMNS(_Value, _F2, _F4, _F8, _F9) \
  45. ((_F2) = AES_FINITE_MULTIPLY_2(_Value, _F2), \
  46. (_F4) = AES_FINITE_MULTIPLY_2(_F2, _F4), \
  47. (_F8) = AES_FINITE_MULTIPLY_2(_F4, _F8), \
  48. (_F9) = (_Value) ^ (_F8), \
  49. (_F8) = ((_F2) ^ (_F4) ^ (_F8)), \
  50. (_F2) ^= (_F9), \
  51. (_F4) ^= (_F9), \
  52. (_F8) ^= AES_ROTATE3(_F2), \
  53. (_F8) ^= AES_ROTATE2(_F4), \
  54. (_F8) ^ AES_ROTATE1(_F9))
  55. //
  56. // ---------------------------------------------------------------- Definitions
  57. //
  58. //
  59. // ------------------------------------------------------ Data Type Definitions
  60. //
  61. //
  62. // ----------------------------------------------- Internal Function Prototypes
  63. //
  64. VOID
  65. CypAesEncryptBlock (
  66. PAES_CONTEXT Context,
  67. PULONG Block
  68. );
  69. VOID
  70. CypAesDecryptBlock (
  71. PAES_CONTEXT Context,
  72. PULONG Block
  73. );
  74. UCHAR
  75. CypAesXtime (
  76. UCHAR Value
  77. );
  78. //
  79. // -------------------------------------------------------------------- Globals
  80. //
  81. //
  82. // Define the S-Box and Inverse S-Box values.
  83. //
  84. static const UCHAR CyAesSbox[256] = {
  85. 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
  86. 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  87. 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
  88. 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  89. 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC,
  90. 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  91. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A,
  92. 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  93. 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
  94. 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  95. 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B,
  96. 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  97. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85,
  98. 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  99. 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
  100. 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  101. 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17,
  102. 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  103. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88,
  104. 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  105. 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
  106. 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  107. 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9,
  108. 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  109. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6,
  110. 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  111. 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
  112. 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  113. 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94,
  114. 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  115. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68,
  116. 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16,
  117. };
  118. static const UCHAR CyAesInvertedSbox[256] = {
  119. 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38,
  120. 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  121. 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
  122. 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  123. 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D,
  124. 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  125. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2,
  126. 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  127. 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
  128. 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  129. 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA,
  130. 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  131. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A,
  132. 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  133. 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
  134. 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  135. 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA,
  136. 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  137. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85,
  138. 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  139. 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
  140. 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  141. 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20,
  142. 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  143. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31,
  144. 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  145. 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
  146. 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  147. 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0,
  148. 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  149. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26,
  150. 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
  151. };
  152. static const UCHAR CyAesRcon[30]= {
  153. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  154. 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F,
  155. 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A, 0xD4,
  156. 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91,
  157. };
  158. //
  159. // ------------------------------------------------------------------ Functions
  160. //
  161. CRYPTO_API
  162. VOID
  163. CyAesInitialize (
  164. PAES_CONTEXT Context,
  165. AES_CIPHER_MODE Mode,
  166. PUCHAR Key,
  167. PUCHAR InitializationVector
  168. )
  169. /*++
  170. Routine Description:
  171. This routine initializes an AES context structure, making it ready to
  172. encrypt and decrypt data.
  173. Arguments:
  174. Context - Supplies a pointer to the AES state.
  175. Mode - Supplies the mode of AES to use.
  176. Key - Supplies the encryption/decryption key to use.
  177. InitializationVector - Supplies the initialization vector to start with.
  178. This doubles as the initial counter value for counter mode, which
  179. should be provided in big-endian byte order.
  180. Return Value:
  181. None. The AES context will be initialized and ready for operation.
  182. --*/
  183. {
  184. PUCHAR CurrentRcon;
  185. INT ExpandedKeyRange;
  186. INT Index;
  187. ULONG KeyValue;
  188. PULONG LongPointer;
  189. INT Words;
  190. ULONG WorkingKey;
  191. switch (Mode) {
  192. case AesModeCbc128:
  193. case AesModeEcb128:
  194. case AesModeCtr128:
  195. Context->Rounds = 10;
  196. Context->KeySize = AES_CBC128_KEY_SIZE;
  197. break;
  198. case AesModeCbc256:
  199. case AesModeEcb256:
  200. case AesModeCtr256:
  201. Context->Rounds = 14;
  202. Context->KeySize = AES_CBC256_KEY_SIZE;
  203. break;
  204. default:
  205. return;
  206. }
  207. //
  208. // Copy the initial key, performing byte swapping.
  209. //
  210. Words = Context->KeySize / sizeof(ULONG);
  211. LongPointer = (PULONG)(&(Context->Keys[0]));
  212. for (Index = 0; Index < Words; Index += 1) {
  213. LongPointer[Index] = ((ULONG)(Key[0]) << 24) |
  214. ((ULONG)(Key[1]) << 16) |
  215. ((ULONG)(Key[2]) << 8) |
  216. Key[3];
  217. Key += 4;
  218. }
  219. //
  220. // Create the round keys.
  221. //
  222. CurrentRcon = (PUCHAR)CyAesRcon;
  223. ExpandedKeyRange = (Context->Rounds + 1) * 4;
  224. for (Index = Words; Index < ExpandedKeyRange; Index += 1) {
  225. KeyValue = LongPointer[Index - 1];
  226. if ((Index % Words) == 0) {
  227. WorkingKey = ((ULONG)CyAesSbox[KeyValue & 0xFF] << 8) |
  228. ((ULONG)CyAesSbox[(KeyValue >> 8) & 0xFF] << 16) |
  229. ((ULONG)CyAesSbox[(KeyValue >> 16) & 0xFF] << 24) |
  230. ((ULONG)CyAesSbox[(KeyValue >> 24) & 0xFF]);
  231. KeyValue = WorkingKey ^ (((ULONG)*CurrentRcon) << 24);
  232. CurrentRcon += 1;
  233. }
  234. if ((Words == 8) && ((Index % Words) == 4)) {
  235. WorkingKey = ((ULONG)CyAesSbox[KeyValue & 0xFF]) |
  236. ((ULONG)CyAesSbox[(KeyValue >> 8) & 0xFF] << 8) |
  237. ((ULONG)CyAesSbox[(KeyValue >> 16) & 0xFF] << 16) |
  238. ((ULONG)CyAesSbox[(KeyValue >> 24) & 0xFF]) << 24;
  239. KeyValue = WorkingKey;
  240. }
  241. LongPointer[Index] = LongPointer[Index - Words] ^ KeyValue;
  242. }
  243. //
  244. // Just copy the initialization vector straight over, ignoring it for ECB
  245. // modes.
  246. //
  247. if ((Mode != AesModeEcb128) && (Mode != AesModeEcb256)) {
  248. if (InitializationVector != NULL) {
  249. RtlCopyMemory(Context->InitializationVector,
  250. InitializationVector,
  251. AES_INITIALIZATION_VECTOR_SIZE);
  252. } else {
  253. RtlZeroMemory(Context->InitializationVector,
  254. AES_INITIALIZATION_VECTOR_SIZE);
  255. }
  256. }
  257. return;
  258. }
  259. CRYPTO_API
  260. VOID
  261. CyAesConvertKeyForDecryption (
  262. PAES_CONTEXT Context
  263. )
  264. /*++
  265. Routine Description:
  266. This routine prepares the context for decryption by performing the
  267. necessary transformations on the round keys.
  268. Arguments:
  269. Context - Supplies a pointer to the AES context.
  270. Return Value:
  271. None.
  272. --*/
  273. {
  274. INT Index;
  275. PULONG KeyLong;
  276. ULONG KeyValue;
  277. ULONG Temporary1;
  278. ULONG Temporary2;
  279. ULONG Temporary3;
  280. ULONG Temporary4;
  281. KeyLong = (PULONG)&(Context->Keys[0]);
  282. KeyLong += 4;
  283. for (Index = Context->Rounds * 4; Index > 4; Index -= 1) {
  284. KeyValue = *KeyLong;
  285. KeyValue = AES_INVERSE_MIX_COLUMNS(KeyValue,
  286. Temporary1,
  287. Temporary2,
  288. Temporary3,
  289. Temporary4);
  290. *KeyLong = KeyValue;
  291. KeyLong += 1;
  292. }
  293. return;
  294. }
  295. CRYPTO_API
  296. VOID
  297. CyAesCbcEncrypt (
  298. PAES_CONTEXT Context,
  299. PUCHAR Plaintext,
  300. PUCHAR Ciphertext,
  301. INT Length
  302. )
  303. /*++
  304. Routine Description:
  305. This routine encrypts a byte sequence (with a block size of 16) using the
  306. AES cipher.
  307. Arguments:
  308. Context - Supplies a pointer to the AES context.
  309. Plaintext - Supplies a pointer to the plaintext buffer.
  310. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  311. Length - Supplies the length of the plaintext and ciphertext buffers, in
  312. bytes. This length must be a multiple of 16 bytes.
  313. Return Value:
  314. None.
  315. --*/
  316. {
  317. ULONG BlockIn[AES_BLOCK_SIZE / sizeof(ULONG)];
  318. ULONG BlockOut[AES_BLOCK_SIZE / sizeof(ULONG)];
  319. ULONG InitializationVector[AES_INITIALIZATION_VECTOR_SIZE / sizeof(ULONG)];
  320. PULONG InLong;
  321. PULONG OutLong;
  322. INT TextIndex;
  323. INT WordIndex;
  324. ASSERT((Length % AES_BLOCK_SIZE) == 0);
  325. RtlCopyMemory(InitializationVector,
  326. Context->InitializationVector,
  327. AES_INITIALIZATION_VECTOR_SIZE);
  328. //
  329. // Copy the initialization vector into the initial output block.
  330. //
  331. for (WordIndex = 0;
  332. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  333. WordIndex += 1) {
  334. BlockOut[WordIndex] = AES_BYTE_SWAP32(InitializationVector[WordIndex]);
  335. }
  336. //
  337. // Loop over and encrypt each block.
  338. //
  339. for (TextIndex = Length - AES_BLOCK_SIZE;
  340. TextIndex >= 0;
  341. TextIndex -= AES_BLOCK_SIZE) {
  342. InLong = (PULONG)Plaintext;
  343. OutLong = (PULONG)Ciphertext;
  344. for (WordIndex = 0;
  345. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  346. WordIndex += 1) {
  347. BlockIn[WordIndex] = AES_BYTE_SWAP32(InLong[WordIndex]) ^
  348. BlockOut[WordIndex];
  349. }
  350. CypAesEncryptBlock(Context, BlockIn);
  351. for (WordIndex = 0;
  352. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  353. WordIndex += 1) {
  354. BlockOut[WordIndex] = BlockIn[WordIndex];
  355. OutLong[WordIndex] = AES_BYTE_SWAP32(BlockIn[WordIndex]);
  356. }
  357. Plaintext += AES_BLOCK_SIZE;
  358. Ciphertext += AES_BLOCK_SIZE;
  359. }
  360. //
  361. // Copy the initialization vector back in to the context.
  362. //
  363. for (WordIndex = 0;
  364. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  365. WordIndex += 1) {
  366. InitializationVector[WordIndex] = AES_BYTE_SWAP32(BlockOut[WordIndex]);
  367. }
  368. RtlCopyMemory(Context->InitializationVector,
  369. InitializationVector,
  370. AES_INITIALIZATION_VECTOR_SIZE);
  371. return;
  372. }
  373. CRYPTO_API
  374. VOID
  375. CyAesCbcDecrypt (
  376. PAES_CONTEXT Context,
  377. PUCHAR Ciphertext,
  378. PUCHAR Plaintext,
  379. INT Length
  380. )
  381. /*++
  382. Routine Description:
  383. This routine decrypts a byte sequence (with a block size of 16) using the
  384. AES cipher.
  385. Arguments:
  386. Context - Supplies a pointer to the AES context.
  387. Ciphertext - Supplies a pointer to the ciphertext buffer.
  388. Plaintext - Supplies a pointer where the plaintext will be returned.
  389. Length - Supplies the length of the plaintext and ciphertext buffers, in
  390. bytes. This length must be a multiple of 16 bytes.
  391. Return Value:
  392. None.
  393. --*/
  394. {
  395. ULONG BlockIn[AES_BLOCK_SIZE / sizeof(ULONG)];
  396. ULONG BlockOut[AES_BLOCK_SIZE / sizeof(ULONG)];
  397. ULONG InitializationVector[AES_INITIALIZATION_VECTOR_SIZE / sizeof(ULONG)];
  398. PULONG InLong;
  399. PULONG OutLong;
  400. INT TextIndex;
  401. INT WordIndex;
  402. ULONG WorkingBlock[AES_BLOCK_SIZE / sizeof(ULONG)];
  403. ULONG XorValue[AES_BLOCK_SIZE / sizeof(ULONG)];
  404. ASSERT((Length % AES_BLOCK_SIZE) == 0);
  405. RtlCopyMemory(InitializationVector,
  406. Context->InitializationVector,
  407. AES_INITIALIZATION_VECTOR_SIZE);
  408. //
  409. // Copy the initialization vector into the initial XOR values.
  410. //
  411. for (WordIndex = 0;
  412. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  413. WordIndex += 1) {
  414. XorValue[WordIndex] = AES_BYTE_SWAP32(InitializationVector[WordIndex]);
  415. }
  416. //
  417. // Decrypt each block.
  418. //
  419. for (TextIndex = Length - AES_BLOCK_SIZE;
  420. TextIndex >= 0;
  421. TextIndex -= AES_BLOCK_SIZE) {
  422. InLong = (PULONG)Ciphertext;
  423. OutLong = (PULONG)Plaintext;
  424. for (WordIndex = 0;
  425. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  426. WordIndex += 1) {
  427. BlockIn[WordIndex] = AES_BYTE_SWAP32(InLong[WordIndex]);
  428. WorkingBlock[WordIndex] = BlockIn[WordIndex];
  429. }
  430. CypAesDecryptBlock(Context, WorkingBlock);
  431. for (WordIndex = 0;
  432. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  433. WordIndex += 1) {
  434. BlockOut[WordIndex] = WorkingBlock[WordIndex] ^ XorValue[WordIndex];
  435. XorValue[WordIndex] = BlockIn[WordIndex];
  436. OutLong[WordIndex] = AES_BYTE_SWAP32(BlockOut[WordIndex]);
  437. }
  438. Ciphertext += AES_BLOCK_SIZE;
  439. Plaintext += AES_BLOCK_SIZE;
  440. }
  441. //
  442. // Copy the initialization vector back in to the context.
  443. //
  444. for (WordIndex = 0;
  445. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  446. WordIndex += 1) {
  447. InitializationVector[WordIndex] = AES_BYTE_SWAP32(XorValue[WordIndex]);
  448. }
  449. RtlCopyMemory(Context->InitializationVector,
  450. InitializationVector,
  451. AES_INITIALIZATION_VECTOR_SIZE);
  452. return;
  453. }
  454. CRYPTO_API
  455. VOID
  456. CyAesEcbEncrypt (
  457. PAES_CONTEXT Context,
  458. PUCHAR Plaintext,
  459. PUCHAR Ciphertext,
  460. INT Length
  461. )
  462. /*++
  463. Routine Description:
  464. This routine encrypts a byte sequence (with a block size of 16) using the
  465. AES codebook.
  466. Arguments:
  467. Context - Supplies a pointer to the AES context.
  468. Plaintext - Supplies a pointer to the plaintext buffer.
  469. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  470. Length - Supplies the length of the plaintext and ciphertext buffers, in
  471. bytes. This length must be a multiple of 16 bytes.
  472. Return Value:
  473. None.
  474. --*/
  475. {
  476. ULONG BlockIn[AES_BLOCK_SIZE / sizeof(ULONG)];
  477. PULONG InLong;
  478. PULONG OutLong;
  479. INT TextIndex;
  480. INT WordIndex;
  481. ASSERT((Length % AES_BLOCK_SIZE) == 0);
  482. //
  483. // Loop over and encrypt each block.
  484. //
  485. for (TextIndex = Length - AES_BLOCK_SIZE;
  486. TextIndex >= 0;
  487. TextIndex -= AES_BLOCK_SIZE) {
  488. InLong = (PULONG)Plaintext;
  489. OutLong = (PULONG)Ciphertext;
  490. for (WordIndex = 0;
  491. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  492. WordIndex += 1) {
  493. BlockIn[WordIndex] = AES_BYTE_SWAP32(InLong[WordIndex]);
  494. }
  495. CypAesEncryptBlock(Context, BlockIn);
  496. for (WordIndex = 0;
  497. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  498. WordIndex += 1) {
  499. OutLong[WordIndex] = AES_BYTE_SWAP32(BlockIn[WordIndex]);
  500. }
  501. Plaintext += AES_BLOCK_SIZE;
  502. Ciphertext += AES_BLOCK_SIZE;
  503. }
  504. return;
  505. }
  506. CRYPTO_API
  507. VOID
  508. CyAesEcbDecrypt (
  509. PAES_CONTEXT Context,
  510. PUCHAR Ciphertext,
  511. PUCHAR Plaintext,
  512. INT Length
  513. )
  514. /*++
  515. Routine Description:
  516. This routine decrypts a byte sequence (with a block size of 16) using the
  517. AES codebook.
  518. Arguments:
  519. Context - Supplies a pointer to the AES context.
  520. Ciphertext - Supplies a pointer to the ciphertext buffer.
  521. Plaintext - Supplies a pointer where the plaintext will be returned.
  522. Length - Supplies the length of the plaintext and ciphertext buffers, in
  523. bytes. This length must be a multiple of 16 bytes.
  524. Return Value:
  525. None.
  526. --*/
  527. {
  528. ULONG BlockIn[AES_BLOCK_SIZE / sizeof(ULONG)];
  529. PULONG InLong;
  530. PULONG OutLong;
  531. INT TextIndex;
  532. INT WordIndex;
  533. ASSERT((Length % AES_BLOCK_SIZE) == 0);
  534. //
  535. // Decrypt each block.
  536. //
  537. for (TextIndex = Length - AES_BLOCK_SIZE;
  538. TextIndex >= 0;
  539. TextIndex -= AES_BLOCK_SIZE) {
  540. InLong = (PULONG)Ciphertext;
  541. OutLong = (PULONG)Plaintext;
  542. for (WordIndex = 0;
  543. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  544. WordIndex += 1) {
  545. BlockIn[WordIndex] = AES_BYTE_SWAP32(InLong[WordIndex]);
  546. }
  547. CypAesDecryptBlock(Context, BlockIn);
  548. for (WordIndex = 0;
  549. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  550. WordIndex += 1) {
  551. OutLong[WordIndex] = AES_BYTE_SWAP32(BlockIn[WordIndex]);
  552. }
  553. Ciphertext += AES_BLOCK_SIZE;
  554. Plaintext += AES_BLOCK_SIZE;
  555. }
  556. return;
  557. }
  558. CRYPTO_API
  559. VOID
  560. CyAesCtrEncrypt (
  561. PAES_CONTEXT Context,
  562. PUCHAR Plaintext,
  563. PUCHAR Ciphertext,
  564. INT Length
  565. )
  566. /*++
  567. Routine Description:
  568. This routine encrypts a byte sequence (with a block size of 16) using AES
  569. counter mode.
  570. Arguments:
  571. Context - Supplies a pointer to the AES context.
  572. Plaintext - Supplies a pointer to the plaintext buffer.
  573. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  574. Length - Supplies the length of the plaintext and ciphertext buffers, in
  575. bytes. This length must be a multiple of 16 bytes.
  576. Return Value:
  577. None.
  578. --*/
  579. {
  580. ULONG BlockIn[AES_BLOCK_SIZE / sizeof(ULONG)];
  581. INT ByteIndex;
  582. ULONG Counter[AES_INITIALIZATION_VECTOR_SIZE / sizeof(ULONG)];
  583. PUCHAR CounterBytes;
  584. PULONG InLong;
  585. PULONG OutLong;
  586. INT TextIndex;
  587. INT WordIndex;
  588. ASSERT((Length % AES_BLOCK_SIZE) == 0);
  589. RtlCopyMemory(Counter,
  590. Context->InitializationVector,
  591. AES_INITIALIZATION_VECTOR_SIZE);
  592. //
  593. // Encrypt the incrementing counter each iteration and XOR it with the next
  594. // block of input.
  595. //
  596. for (TextIndex = Length - AES_BLOCK_SIZE;
  597. TextIndex >= 0;
  598. TextIndex -= AES_BLOCK_SIZE) {
  599. //
  600. // Copy the counter into the input block.
  601. //
  602. for (WordIndex = 0;
  603. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  604. WordIndex += 1) {
  605. BlockIn[WordIndex] = AES_BYTE_SWAP32(Counter[WordIndex]);
  606. }
  607. InLong = (PULONG)Plaintext;
  608. OutLong = (PULONG)Ciphertext;
  609. CypAesEncryptBlock(Context, BlockIn);
  610. for (WordIndex = 0;
  611. WordIndex < (AES_BLOCK_SIZE / sizeof(ULONG));
  612. WordIndex += 1) {
  613. OutLong[WordIndex] = AES_BYTE_SWAP32(BlockIn[WordIndex]) ^
  614. InLong[WordIndex];
  615. }
  616. Plaintext += AES_BLOCK_SIZE;
  617. Ciphertext += AES_BLOCK_SIZE;
  618. //
  619. // Increment the counter. Remember that this is big-endian.
  620. //
  621. CounterBytes = (PUCHAR)Counter;
  622. for (ByteIndex = AES_BLOCK_SIZE - 1;
  623. ByteIndex >= 0;
  624. ByteIndex -= 1) {
  625. CounterBytes[ByteIndex] += 1;
  626. if (CounterBytes[ByteIndex] != 0) {
  627. break;
  628. }
  629. }
  630. }
  631. //
  632. // Copy the counter back in to the context.
  633. //
  634. RtlCopyMemory(Context->InitializationVector,
  635. Counter,
  636. AES_INITIALIZATION_VECTOR_SIZE);
  637. return;
  638. }
  639. CRYPTO_API
  640. VOID
  641. CyAesCtrDecrypt (
  642. PAES_CONTEXT Context,
  643. PUCHAR Ciphertext,
  644. PUCHAR Plaintext,
  645. INT Length
  646. )
  647. /*++
  648. Routine Description:
  649. This routine decrypts a byte sequence (with a block size of 16) using AES
  650. counter mode.
  651. Arguments:
  652. Context - Supplies a pointer to the AES context.
  653. Ciphertext - Supplies a pointer to the ciphertext buffer.
  654. Plaintext - Supplies a pointer where the plaintext will be returned.
  655. Length - Supplies the length of the plaintext and ciphertext buffers, in
  656. bytes. This length must be a multiple of 16 bytes.
  657. Return Value:
  658. None.
  659. --*/
  660. {
  661. //
  662. // Counter mode always uses AES encryption to derive a value from the
  663. // counter and then XOR's that value with the input. Thus, decryption is
  664. // the same as encryption except the ciphertext is the input and the
  665. // plaintext is the output.
  666. //
  667. CyAesCtrEncrypt(Context, Ciphertext, Plaintext, Length);
  668. return;
  669. }
  670. //
  671. // --------------------------------------------------------- Internal Functions
  672. //
  673. VOID
  674. CypAesEncryptBlock (
  675. PAES_CONTEXT Context,
  676. PULONG Block
  677. )
  678. /*++
  679. Routine Description:
  680. This routine encrypts a single block of data using the AES cipher.
  681. Arguments:
  682. Context - Supplies a pointer to the AES context.
  683. Block - Supplies a pointer to the block to encrypt.
  684. Return Value:
  685. None. The encrypted data will be returned inline.
  686. --*/
  687. {
  688. PULONG Key;
  689. UCHAR OriginalVector0;
  690. INT Round;
  691. INT Row;
  692. UCHAR SboxIndex;
  693. UCHAR Vector[4];
  694. ULONG WorkingBlock[4];
  695. ULONG XorAll;
  696. Key = (PULONG)(Context->Keys);
  697. //
  698. // Perform pre-round key addition.
  699. //
  700. for (Row = 0; Row < 4; Row += 1) {
  701. Block[Row] ^= *Key;
  702. Key += 1;
  703. }
  704. //
  705. // Loop through and encrypt the block.
  706. //
  707. for (Round = 0; Round < Context->Rounds; Round += 1) {
  708. for (Row = 0; Row < 4; Row += 1) {
  709. //
  710. // Perform the byte substitution and row shift operations together.
  711. //
  712. SboxIndex = (UCHAR)(Block[Row % 4] >> 24);
  713. Vector[0] = CyAesSbox[SboxIndex];
  714. SboxIndex = (UCHAR)(Block[(Row + 1) % 4] >> 16);
  715. Vector[1] = CyAesSbox[SboxIndex];
  716. SboxIndex = (UCHAR)(Block[(Row + 2) % 4] >> 8);
  717. Vector[2] = CyAesSbox[SboxIndex];
  718. SboxIndex = (UCHAR)(Block[(Row + 3) % 4]);
  719. Vector[3] = CyAesSbox[SboxIndex];
  720. //
  721. // If this is not the last round, perform the mix columns operation.
  722. //
  723. if (Round != Context->Rounds - 1) {
  724. XorAll = Vector[0] ^ Vector[1] ^ Vector[2] ^ Vector[3];
  725. OriginalVector0 = Vector[0];
  726. Vector[0] ^= XorAll ^ CypAesXtime(Vector[0] ^ Vector[1]);
  727. Vector[1] ^= XorAll ^ CypAesXtime(Vector[1] ^ Vector[2]);
  728. Vector[2] ^= XorAll ^ CypAesXtime(Vector[2] ^ Vector[3]);
  729. Vector[3] ^= XorAll ^ CypAesXtime(Vector[3] ^ OriginalVector0);
  730. }
  731. WorkingBlock[Row] = ((ULONG)Vector[0] << 24) |
  732. ((ULONG)Vector[1] << 16) |
  733. ((ULONG)Vector[2] << 8) |
  734. Vector[3];
  735. }
  736. //
  737. // Perform key addition now that the mix column operation is complete.
  738. //
  739. for (Row = 0; Row < 4; Row += 1) {
  740. Block[Row] = WorkingBlock[Row] ^ *Key;
  741. Key += 1;
  742. }
  743. }
  744. return;
  745. }
  746. VOID
  747. CypAesDecryptBlock (
  748. PAES_CONTEXT Context,
  749. PULONG Block
  750. )
  751. /*++
  752. Routine Description:
  753. This routine decrypts a single block of data using the AES cipher.
  754. Arguments:
  755. Context - Supplies a pointer to the AES context.
  756. Block - Supplies a pointer to the block to decrypt.
  757. Return Value:
  758. None. The decrypted data will be returned inline.
  759. --*/
  760. {
  761. PULONG Key;
  762. INT Round;
  763. INT Row;
  764. UCHAR SboxIndex;
  765. UCHAR Vector[4];
  766. ULONG WorkingBlock[4];
  767. UCHAR XorValue0;
  768. UCHAR XorValue1;
  769. UCHAR XorValue2;
  770. UCHAR XorValue3;
  771. UCHAR XorValue4;
  772. UCHAR XorValue5;
  773. UCHAR XorValue6;
  774. Key = Context->Keys + ((Context->Rounds + 1) * 4);
  775. //
  776. // Perform pre-round key addition.
  777. //
  778. for (Row = 4; Row > 0; Row -= 1) {
  779. Key -= 1;
  780. Block[Row - 1] ^= *Key;
  781. }
  782. //
  783. // Loop through and decrypt the block.
  784. //
  785. for (Round = 0; Round < Context->Rounds; Round += 1) {
  786. for (Row = 4; Row > 0; Row -= 1) {
  787. //
  788. // Perform the byte substitution and row shift operations together.
  789. //
  790. SboxIndex = (UCHAR)(Block[(Row + 3) % 4] >> 24);
  791. Vector[0] = CyAesInvertedSbox[SboxIndex];
  792. SboxIndex = (UCHAR)(Block[(Row + 2) % 4] >> 16);
  793. Vector[1] = CyAesInvertedSbox[SboxIndex];
  794. SboxIndex = (UCHAR)(Block[(Row + 1) % 4] >> 8);
  795. Vector[2] = CyAesInvertedSbox[SboxIndex];
  796. SboxIndex = (UCHAR)(Block[Row % 4]);
  797. Vector[3] = CyAesInvertedSbox[SboxIndex];
  798. //
  799. // Perform the Mix Column operation if this is not the last round.
  800. //
  801. if (Round < (Context->Rounds - 1)) {
  802. XorValue0 = CypAesXtime(Vector[0] ^ Vector[1]);
  803. XorValue1 = CypAesXtime(Vector[1] ^ Vector[2]);
  804. XorValue2 = CypAesXtime(Vector[2] ^ Vector[3]);
  805. XorValue3 = CypAesXtime(Vector[3] ^ Vector[0]);
  806. XorValue4 = CypAesXtime(XorValue0 ^ XorValue1);
  807. XorValue5 = CypAesXtime(XorValue1 ^ XorValue2);
  808. XorValue6 = CypAesXtime(XorValue4 ^ XorValue5);
  809. XorValue0 ^= Vector[1] ^ Vector[2] ^ Vector[3] ^ XorValue4 ^
  810. XorValue6;
  811. XorValue1 ^= Vector[0] ^ Vector[2] ^ Vector[3] ^ XorValue5 ^
  812. XorValue6;
  813. XorValue2 ^= Vector[0] ^ Vector[1] ^ Vector[3] ^ XorValue4 ^
  814. XorValue6;
  815. XorValue3 ^= Vector[0] ^ Vector[1] ^ Vector[2] ^ XorValue5 ^
  816. XorValue6;
  817. WorkingBlock[Row - 1] = ((ULONG)XorValue0 << 24) |
  818. ((ULONG)XorValue1 << 16) |
  819. ((ULONG)XorValue2 << 8) |
  820. XorValue3;
  821. } else {
  822. WorkingBlock[Row - 1] = ((ULONG)Vector[0] << 24) |
  823. ((ULONG)Vector[1] << 16) |
  824. ((ULONG)Vector[2] << 8) |
  825. Vector[3];
  826. }
  827. }
  828. for (Row = 4; Row > 0; Row -= 1) {
  829. Key -= 1;
  830. Block[Row - 1] = WorkingBlock[Row - 1] ^ *Key;
  831. }
  832. }
  833. return;
  834. }
  835. UCHAR
  836. CypAesXtime (
  837. UCHAR Value
  838. )
  839. /*++
  840. Routine Description:
  841. This routine performs doubling of an 8 bit value in a Galois Field GF(2^8)
  842. using the irreducible polynomial x^8 + x^4 + x^3 + x + 1. This basically
  843. means multiply by 2 and exclusive OR with 0x1B if it rolls over.
  844. Arguments:
  845. Value - Supplies the value to multiply by 2 in the finite field.
  846. Return Value:
  847. Returns the value multiplied by 2 in the finite field.
  848. --*/
  849. {
  850. if ((Value & 0x80) != 0) {
  851. return (Value << 1) ^ 0x1B;
  852. }
  853. return Value << 1;
  854. }