crypto.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. crypto.h
  9. Abstract:
  10. This header contains definitions for the Minoca cryptographic library.
  11. Author:
  12. Evan Green 13-Jan-2015
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. //
  18. // ---------------------------------------------------------------- Definitions
  19. //
  20. #ifndef CRYPTO_API
  21. #define CRYPTO_API __DLLIMPORT
  22. #endif
  23. //
  24. // Define AES parameters.
  25. //
  26. #define AES_MAX_ROUNDS 14
  27. #define AES_BLOCK_SIZE 16
  28. #define AES_INITIALIZATION_VECTOR_SIZE 16
  29. #define AES_CBC128_KEY_SIZE 16
  30. #define AES_CBC256_KEY_SIZE 32
  31. #define AES_ECB128_KEY_SIZE AES_CBC128_KEY_SIZE
  32. #define AES_ECB256_KEY_SIZE AES_CBC256_KEY_SIZE
  33. #define AES_CTR128_KEY_SIZE AES_CBC128_KEY_SIZE
  34. #define AES_CTR256_KEY_SIZE AES_CBC256_KEY_SIZE
  35. //
  36. // Define SHA-1 parameters.
  37. //
  38. #define SHA1_HASH_SIZE 20
  39. //
  40. // Define SHA-256 parameters.
  41. //
  42. #define SHA256_HASH_SIZE 32
  43. //
  44. // Define SHA-512 parameters.
  45. //
  46. #define SHA512_HASH_SIZE 64
  47. #define SHA512_BLOCK_SIZE 128
  48. #define SHA512_SHORT_BLOCK_SIZE (SHA512_BLOCK_SIZE - 16)
  49. //
  50. // Define MD5 parameters.
  51. //
  52. #define MD5_BLOCK_SIZE 64
  53. #define MD5_HASH_SIZE 16
  54. //
  55. // Define Fortuna PRNG parameters.
  56. //
  57. #define FORTUNA_BLOCK_SIZE 16
  58. #define FORTUNA_HASH_KEY_SIZE 32
  59. #define FORTUNA_POOL_COUNT 23
  60. //
  61. // Define big integer parameters.
  62. //
  63. #define BIG_INTEGER_MODULO_COUNT 3
  64. //
  65. // ------------------------------------------------------ Data Type Definitions
  66. //
  67. typedef enum _AES_CIPHER_MODE {
  68. AesModeInvalid,
  69. AesModeCbc128,
  70. AesModeCbc256,
  71. AesModeEcb128,
  72. AesModeEcb256,
  73. AesModeCtr128,
  74. AesModeCtr256
  75. } AES_CIPHER_MODE, *PAES_CIPHER_MODE;
  76. typedef enum _FORTUNA_INITIALIZATION_STATE {
  77. FortunaNotInitialized,
  78. FortunaInitializationSeeded,
  79. FortunaInitialized
  80. } FORTUNA_INITIALIZATION_STATE, *PFORTUNA_INITIALIZATION_STATE;
  81. typedef
  82. ULONGLONG
  83. (*PCY_GET_TIME_COUNTER) (
  84. VOID
  85. );
  86. /*++
  87. Routine Description:
  88. This routine queries the time counter hardware and returns a 64-bit
  89. monotonically non-decreasing value that represents the number of timer
  90. ticks representing passage of time.
  91. Arguments:
  92. None.
  93. Return Value:
  94. Returns the number of timer ticks that have elapsed since some epoch value.
  95. --*/
  96. /*++
  97. Structure Description:
  98. This structure stores the context used during AES encryption and decryption.
  99. Members:
  100. Rounds - Stores the number of rounds used in this mode.
  101. KeySize - Stores the size of the key.
  102. Keys - Stores the initial key and each of the round keys.
  103. InitializationVector - Stores the initialization vector.
  104. --*/
  105. typedef struct _AES_CONTEXT {
  106. USHORT Rounds;
  107. USHORT KeySize;
  108. ULONG Keys[(AES_MAX_ROUNDS + 1) * 8];
  109. UCHAR InitializationVector[AES_INITIALIZATION_VECTOR_SIZE];
  110. } AES_CONTEXT, *PAES_CONTEXT;
  111. /*++
  112. Structure Description:
  113. This structure stores the context used during computation of a SHA-1 hash.
  114. Members:
  115. IntermediateHash - Stores the running digest.
  116. Length - Stores the length of the message, in bits.
  117. BlockIndex - Stores the current index into the message block array.
  118. MessageBlock - Stores the current block of the message being worked on.
  119. --*/
  120. typedef struct _SHA1_CONTEXT {
  121. ULONG IntermediateHash[SHA1_HASH_SIZE / sizeof(ULONG)];
  122. ULONGLONG Length;
  123. USHORT BlockIndex;
  124. UCHAR MessageBlock[64];
  125. } SHA1_CONTEXT, *PSHA1_CONTEXT;
  126. /*++
  127. Structure Description:
  128. This structure stores the context used during computation of a SHA-256 hash.
  129. Members:
  130. IntermediateHash - Stores the running digest.
  131. Length - Stores the length of the message, in bits.
  132. BlockIndex - Stores the current index into the message block array.
  133. MessageBlock - Stores the current block of the message being worked on.
  134. --*/
  135. typedef struct _SHA256_CONTEXT {
  136. ULONG IntermediateHash[SHA256_HASH_SIZE / sizeof(ULONG)];
  137. ULONGLONG Length;
  138. USHORT BlockIndex;
  139. UCHAR MessageBlock[64];
  140. } SHA256_CONTEXT, *PSHA256_CONTEXT;
  141. /*++
  142. Structure Description:
  143. This structure stores the context used during computation of a SHA-512 hash.
  144. Members:
  145. IntermediateHash - Stores the running digest.
  146. Length - Stores the length of the message, in bits.
  147. MessageBlock - Stores the current block of the message being worked on.
  148. --*/
  149. typedef struct _SHA512_CONTEXT {
  150. ULONGLONG IntermediateHash[SHA512_HASH_SIZE / sizeof(ULONGLONG)];
  151. ULONGLONG Length[2];
  152. UCHAR MessageBlock[SHA512_BLOCK_SIZE];
  153. } SHA512_CONTEXT, *PSHA512_CONTEXT;
  154. /*++
  155. Structure Description:
  156. This structure stores the context used during computation of an MD5 hash.
  157. Members:
  158. State - Stores the running digest.
  159. Length - Stores the length of the message, in bits.
  160. MessageBlock - Stores the current block of the message being worked on.
  161. --*/
  162. typedef struct _MD5_CONTEXT {
  163. ULONG State[4];
  164. ULONGLONG Length;
  165. UCHAR MessageBlock[MD5_BLOCK_SIZE];
  166. } MD5_CONTEXT, *PMD5_CONTEXT;
  167. /*++
  168. Structure Description:
  169. This structure stores the context used by the Fortuna Pseudo-Random Number
  170. Generator.
  171. Members:
  172. Counter - Stores the counter value, padded out to the cipher block size,
  173. for counting cipher blocks.
  174. Result - Stores the ciphertext result.
  175. Key - Stores the encryption key and hash.
  176. Pools - Stores the randomization source pools.
  177. CipherContext - Stores the encryption context.
  178. ReseedCount - Stores whether or not a reseed is needed.
  179. Pool0Bytes - Stores the number of bytes of entropy introduced into pool
  180. zero.
  181. Position - Stores a pool index where entropy is deposited.
  182. Initialized - Stores a state indicating whether the context is
  183. initialized or not.
  184. GetTimeCounter - Stores a pointer to a function used for retrieving the
  185. current time counter value.
  186. TimeCounterFrequency - Stores the frequency of the time counter, in Hertz.
  187. LastReseedTime - Stores the last time a reseed happened.
  188. --*/
  189. typedef struct _FORTUNA_CONTEXT {
  190. UCHAR Counter[FORTUNA_BLOCK_SIZE];
  191. UCHAR Result[FORTUNA_BLOCK_SIZE];
  192. UCHAR Key[FORTUNA_HASH_KEY_SIZE];
  193. SHA256_CONTEXT Pools[FORTUNA_POOL_COUNT];
  194. AES_CONTEXT CipherContext;
  195. UINTN ReseedCount;
  196. UINTN Pool0Bytes;
  197. UINTN Position;
  198. FORTUNA_INITIALIZATION_STATE Initialized;
  199. PCY_GET_TIME_COUNTER GetTimeCounter;
  200. ULONGLONG TimeCounterFrequency;
  201. ULONGLONG LastReseedTime;
  202. } FORTUNA_CONTEXT, *PFORTUNA_CONTEXT;
  203. //
  204. // Define functions called by the big integer library.
  205. //
  206. typedef
  207. PVOID
  208. (*PCY_ALLOCATE_MEMORY) (
  209. UINTN Size
  210. );
  211. /*++
  212. Routine Description:
  213. This routine is called when the crypto library needs to allocate memory.
  214. Arguments:
  215. Size - Supplies the size of the allocation request, in bytes.
  216. Return Value:
  217. Returns a pointer to the allocation if successful, or NULL if the
  218. allocation failed.
  219. --*/
  220. typedef
  221. PVOID
  222. (*PCY_REALLOCATE_MEMORY) (
  223. PVOID Allocation,
  224. UINTN Size
  225. );
  226. /*++
  227. Routine Description:
  228. This routine is called when the crypto library needs to adjust the size of
  229. a previous allocation.
  230. Arguments:
  231. Allocation - Supplies the allocation to resize.
  232. Size - Supplies the size of the allocation request, in bytes.
  233. Return Value:
  234. Returns a pointer to the allocation if successful, or NULL if the
  235. allocation failed.
  236. --*/
  237. typedef
  238. VOID
  239. (*PCY_FREE_MEMORY) (
  240. PVOID Memory
  241. );
  242. /*++
  243. Routine Description:
  244. This routine is called when the crypto library needs to free allocated
  245. memory.
  246. Arguments:
  247. Memory - Supplies the allocation returned by the allocation routine.
  248. Return Value:
  249. None.
  250. --*/
  251. typedef ULONG BIG_INTEGER_COMPONENT, *PBIG_INTEGER_COMPONENT;
  252. typedef ULONGLONG BIG_INTEGER_LONG_COMPONENT, *PBIG_INTEGER_LONG_COMPONENT;
  253. typedef struct _BIG_INTEGER BIG_INTEGER, *PBIG_INTEGER;
  254. /*++
  255. Structure Description:
  256. This structure stores a very large integer indeed.
  257. Members:
  258. Next - Stores an optional pointer to the next big integer if this
  259. integer is on a list.
  260. Size - Stores the number of components in this integer.
  261. Capacity - Stores the number of components this allocation can
  262. sustain before the integer needs to be reallocated.
  263. ReferenceCount - Stores the reference count of the integer.
  264. Components - Stores a pointer to an array of integer components
  265. that make up the big integer.
  266. --*/
  267. struct _BIG_INTEGER {
  268. PBIG_INTEGER Next;
  269. USHORT Size;
  270. USHORT Capacity;
  271. LONG ReferenceCount;
  272. PBIG_INTEGER_COMPONENT Components;
  273. };
  274. /*++
  275. Structure Description:
  276. This structure stores a big integer context, which maintains a
  277. cache of reusable big integers.
  278. Members:
  279. AllocateMemory - Stores a pointer to a function used for heap
  280. allocations when more big integers are needed. This must be
  281. filled in when initialized.
  282. ReallocateMemory - Stores a pointer to a function used to
  283. reallocate memory. This must be filled in before the context
  284. is initialized.
  285. FreeMemory - Stores a pointer to a function used to free
  286. previously allocated memory. This must be filled in before
  287. the context is initialized.
  288. ActiveList - Stores a pointer to the outstanding big integers.
  289. FreeList - Stores a pointer to recently used but currently unused
  290. big integers.
  291. Radix - Stores a pointer to the radix used in the computation.
  292. Modulus - Stores the modulus used in the computation.
  293. Mu - Stores the mu values used in Barrett reduction.
  294. NormalizedMod - Stores the normalized modulo values.
  295. ExponentTable - Stores an array of pointers to integers representing
  296. pre-computed exponentiations of the working value.
  297. WindowSize - Stores the size of the sliding window.
  298. ActiveCount - Stores the number of integers on the active list.
  299. FreeCount - Stores the number of integers on the free list.
  300. ModOffset - Stores the modulo offset in use.
  301. --*/
  302. typedef struct _BIG_INTEGER_CONTEXT {
  303. PCY_ALLOCATE_MEMORY AllocateMemory;
  304. PCY_REALLOCATE_MEMORY ReallocateMemory;
  305. PCY_FREE_MEMORY FreeMemory;
  306. PBIG_INTEGER ActiveList;
  307. PBIG_INTEGER FreeList;
  308. PBIG_INTEGER Radix;
  309. PBIG_INTEGER Modulus[BIG_INTEGER_MODULO_COUNT];
  310. PBIG_INTEGER Mu[BIG_INTEGER_MODULO_COUNT];
  311. PBIG_INTEGER NormalizedMod[BIG_INTEGER_MODULO_COUNT];
  312. PBIG_INTEGER *ExponentTable;
  313. ULONG WindowSize;
  314. INTN ActiveCount;
  315. INTN FreeCount;
  316. UCHAR ModOffset;
  317. } BIG_INTEGER_CONTEXT, *PBIG_INTEGER_CONTEXT;
  318. typedef
  319. PVOID
  320. (*PCY_FILL_RANDOM) (
  321. PVOID Buffer,
  322. UINTN Size
  323. );
  324. /*++
  325. Routine Description:
  326. This routine is called when the crypto library needs to fill a buffer with
  327. random bytes.
  328. Arguments:
  329. Buffer - Supplies a pointer to the buffer to fill with random bytes.
  330. Size - Supplies the number of bytes of random data to return.
  331. Return Value:
  332. None.
  333. --*/
  334. /*++
  335. Structure Description:
  336. This structure stores the context used during encryption or decryption via
  337. RSA.
  338. Members:
  339. BigIntegerContext - Stores the big integer context used to manage the
  340. values used during computation. It is expected that when the context
  341. is initialized the caller will have filled in the allocate, reallocate,
  342. and free memory functions in this structure.
  343. FillRandom - Stores a pointer to a function called to fill a buffer with
  344. random bytes. This function pointer must be filled in to do
  345. encryption with padding.
  346. Modulus - Stores the public modulus, p * q.
  347. PublicExponent - Stores the public exponent e.
  348. PrivateExponent - Stores the private exponent d.
  349. PValue - Stores one of the primes, p.
  350. QValue - Stores one the other prime, q.
  351. DpValue - Stores d mod (p - 1).
  352. DqValue - Stores d mod (q - 1).
  353. QInverse - Stores q^-1 mod p.
  354. ModulusSize - Stores the size of the modulus, in bytes.
  355. --*/
  356. typedef struct _RSA_CONTEXT {
  357. BIG_INTEGER_CONTEXT BigIntegerContext;
  358. PCY_FILL_RANDOM FillRandom;
  359. PBIG_INTEGER Modulus;
  360. PBIG_INTEGER PublicExponent;
  361. PBIG_INTEGER PrivateExponent;
  362. PBIG_INTEGER PValue;
  363. PBIG_INTEGER QValue;
  364. PBIG_INTEGER DpValue;
  365. PBIG_INTEGER DqValue;
  366. PBIG_INTEGER QInverse;
  367. UINTN ModulusSize;
  368. } RSA_CONTEXT, *PRSA_CONTEXT;
  369. /*++
  370. Structure Description:
  371. This structure stores the raw values needed for a public key transfer.
  372. Members:
  373. Modulus - Stores a pointer to the modulus value, the product of the two
  374. primes.
  375. ModulusLength - Stores the length of the modulus value in bytes.
  376. PublicExponent - Stores a pointer to the public key exponent.
  377. PublicExponentLength - Stores the length of the the public key exponent in
  378. bytes.
  379. --*/
  380. typedef struct _RSA_PUBLIC_KEY_COMPONENTS {
  381. PVOID Modulus;
  382. UINTN ModulusLength;
  383. PVOID PublicExponent;
  384. UINTN PublicExponentLength;
  385. } RSA_PUBLIC_KEY_COMPONENTS, *PRSA_PUBLIC_KEY_COMPONENTS;
  386. /*++
  387. Structure Description:
  388. This structure stores the raw values needed for a private key transfer.
  389. Members:
  390. PublicKey - Stores the public key components.
  391. PrivateExponent - Stores a pointer to the private key exponent.
  392. PrivateExponentLength - Stores the length of the private key exponent in
  393. bytes.
  394. PValue - Stores a pointer to one of the primes.
  395. PValueLength - Stores the length of the p value in bytes.
  396. QValue - Stores a pointer to the other prime.
  397. QValueLength - Stores the length of the q value in bytes.
  398. DpValue - Stores a pointer to the value d mod (p - 1).
  399. DpValueLength - Stores the length of the dP value in bytes.
  400. DqValue - Stores a pointer to the value d mod (q - 1).
  401. DqValueLength - Stores the length of the dQ value in bytes.
  402. QInverse - Stores a pointer to the value q^-1 mod p.
  403. QInverseLength - Stores the length of the q inverse value in bytes.
  404. --*/
  405. typedef struct _RSA_PRIVATE_KEY_COMPONENTS {
  406. RSA_PUBLIC_KEY_COMPONENTS PublicKey;
  407. PVOID PrivateExponent;
  408. UINTN PrivateExponentLength;
  409. PVOID PValue;
  410. UINTN PValueLength;
  411. PVOID QValue;
  412. UINTN QValueLength;
  413. PVOID DpValue;
  414. UINTN DpValueLength;
  415. PVOID DqValue;
  416. UINTN DqValueLength;
  417. PVOID QInverse;
  418. UINTN QInverseLength;
  419. } RSA_PRIVATE_KEY_COMPONENTS, *PRSA_PRIVATE_KEY_COMPONENTS;
  420. //
  421. // -------------------------------------------------------------------- Globals
  422. //
  423. CRYPTO_API
  424. VOID
  425. CyAesInitialize (
  426. PAES_CONTEXT Context,
  427. AES_CIPHER_MODE Mode,
  428. PUCHAR Key,
  429. PUCHAR InitializationVector
  430. );
  431. /*++
  432. Routine Description:
  433. This routine initializes an AES context structure, making it ready to
  434. encrypt and decrypt data.
  435. Arguments:
  436. Context - Supplies a pointer to the AES state.
  437. Mode - Supplies the mode of AES to use.
  438. Key - Supplies the encryption/decryption key to use.
  439. InitializationVector - Supplies the initialization vector to start with.
  440. Return Value:
  441. None. The AES context will be initialized and ready for operation.
  442. --*/
  443. CRYPTO_API
  444. VOID
  445. CyAesConvertKeyForDecryption (
  446. PAES_CONTEXT Context
  447. );
  448. /*++
  449. Routine Description:
  450. This routine prepares the context for decryption by performing the
  451. necessary transformations on the round keys.
  452. Arguments:
  453. Context - Supplies a pointer to the AES context.
  454. Return Value:
  455. None.
  456. --*/
  457. CRYPTO_API
  458. VOID
  459. CyAesCbcEncrypt (
  460. PAES_CONTEXT Context,
  461. PUCHAR Plaintext,
  462. PUCHAR Ciphertext,
  463. INT Length
  464. );
  465. /*++
  466. Routine Description:
  467. This routine encrypts a byte sequence (with a block size of 16) using the
  468. AES cipher.
  469. Arguments:
  470. Context - Supplies a pointer to the AES context.
  471. Plaintext - Supplies a pointer to the plaintext buffer.
  472. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  473. Length - Supplies the length of the plaintext and ciphertext buffers, in
  474. bytes. This length must be a multiple of 16 bytes.
  475. Return Value:
  476. None.
  477. --*/
  478. CRYPTO_API
  479. VOID
  480. CyAesCbcDecrypt (
  481. PAES_CONTEXT Context,
  482. PUCHAR Ciphertext,
  483. PUCHAR Plaintext,
  484. INT Length
  485. );
  486. /*++
  487. Routine Description:
  488. This routine decrypts a byte sequence (with a block size of 16) using the
  489. AES cipher.
  490. Arguments:
  491. Context - Supplies a pointer to the AES context.
  492. Ciphertext - Supplies a pointer to the ciphertext buffer.
  493. Plaintext - Supplies a pointer where the plaintext will be returned.
  494. Length - Supplies the length of the plaintext and ciphertext buffers, in
  495. bytes. This length must be a multiple of 16 bytes.
  496. Return Value:
  497. None.
  498. --*/
  499. CRYPTO_API
  500. VOID
  501. CyAesEcbEncrypt (
  502. PAES_CONTEXT Context,
  503. PUCHAR Plaintext,
  504. PUCHAR Ciphertext,
  505. INT Length
  506. );
  507. /*++
  508. Routine Description:
  509. This routine encrypts a byte sequence (with a block size of 16) using the
  510. AES codebook.
  511. Arguments:
  512. Context - Supplies a pointer to the AES context.
  513. Plaintext - Supplies a pointer to the plaintext buffer.
  514. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  515. Length - Supplies the length of the plaintext and ciphertext buffers, in
  516. bytes. This length must be a multiple of 16 bytes.
  517. Return Value:
  518. None.
  519. --*/
  520. CRYPTO_API
  521. VOID
  522. CyAesEcbDecrypt (
  523. PAES_CONTEXT Context,
  524. PUCHAR Ciphertext,
  525. PUCHAR Plaintext,
  526. INT Length
  527. );
  528. /*++
  529. Routine Description:
  530. This routine decrypts a byte sequence (with a block size of 16) using the
  531. AES codebook.
  532. Arguments:
  533. Context - Supplies a pointer to the AES context.
  534. Ciphertext - Supplies a pointer to the ciphertext buffer.
  535. Plaintext - Supplies a pointer where the plaintext will be returned.
  536. Length - Supplies the length of the plaintext and ciphertext buffers, in
  537. bytes. This length must be a multiple of 16 bytes.
  538. Return Value:
  539. None.
  540. --*/
  541. CRYPTO_API
  542. VOID
  543. CyAesCtrEncrypt (
  544. PAES_CONTEXT Context,
  545. PUCHAR Plaintext,
  546. PUCHAR Ciphertext,
  547. INT Length
  548. );
  549. /*++
  550. Routine Description:
  551. This routine encrypts a byte sequence (with a block size of 16) using AES
  552. counter mode.
  553. Arguments:
  554. Context - Supplies a pointer to the AES context.
  555. Plaintext - Supplies a pointer to the plaintext buffer.
  556. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  557. Length - Supplies the length of the plaintext and ciphertext buffers, in
  558. bytes. This length must be a multiple of 16 bytes.
  559. Return Value:
  560. None.
  561. --*/
  562. CRYPTO_API
  563. VOID
  564. CyAesCtrDecrypt (
  565. PAES_CONTEXT Context,
  566. PUCHAR Ciphertext,
  567. PUCHAR Plaintext,
  568. INT Length
  569. );
  570. /*++
  571. Routine Description:
  572. This routine decrypts a byte sequence (with a block size of 16) using AES
  573. counter mode.
  574. Arguments:
  575. Context - Supplies a pointer to the AES context.
  576. Ciphertext - Supplies a pointer to the ciphertext buffer.
  577. Plaintext - Supplies a pointer where the plaintext will be returned.
  578. Length - Supplies the length of the plaintext and ciphertext buffers, in
  579. bytes. This length must be a multiple of 16 bytes.
  580. Return Value:
  581. None.
  582. --*/
  583. CRYPTO_API
  584. VOID
  585. CySha1ComputeHmac (
  586. PUCHAR Message,
  587. ULONG Length,
  588. PUCHAR Key,
  589. ULONG KeyLength,
  590. UCHAR Digest[SHA1_HASH_SIZE]
  591. );
  592. /*++
  593. Routine Description:
  594. This routine obtains a SHA-1 HMAC signature based on the message and key.
  595. Note that the key must be no longer than the hash function's block size.
  596. Arguments:
  597. Message - Supplies a pointer to the data buffer to hash and sign.
  598. Length - Supplies the length of the message, in bytes.
  599. Key - Supplies a pointer to the secret key buffer.
  600. KeyLength - Supplies the length of the secret key, in bytes. This must be
  601. less than or equal to 64 bytes, the block size of the SHA-1 hash
  602. function.
  603. Digest - Supplies a pointer where the HMAC digest will be returned. This
  604. buffer is assumed to be 20 bytes, the size of a SHA-1 hash.
  605. Return Value:
  606. None.
  607. --*/
  608. CRYPTO_API
  609. VOID
  610. CySha256ComputeHmac (
  611. PUCHAR Message,
  612. ULONG Length,
  613. PUCHAR Key,
  614. ULONG KeyLength,
  615. UCHAR Digest[SHA256_HASH_SIZE]
  616. );
  617. /*++
  618. Routine Description:
  619. This routine obtains a SHA-256 HMAC signature based on the message and key.
  620. Note that the key must be no longer than the hash function's block size.
  621. Arguments:
  622. Message - Supplies a pointer to the data buffer to hash and sign.
  623. Length - Supplies the length of the message, in bytes.
  624. Key - Supplies a pointer to the secret key buffer.
  625. KeyLength - Supplies the length of the secret key, in bytes. This must be
  626. less than or equal to 64 bytes, the block size of the SHA-1 hash
  627. function.
  628. Digest - Supplies a pointer where the HMAC digest will be returned. This
  629. buffer is assumed to be 64 bytes, the size of a SHA-256 hash.
  630. Return Value:
  631. None.
  632. --*/
  633. CRYPTO_API
  634. VOID
  635. CyMd5ComputeHmac (
  636. PUCHAR Message,
  637. ULONG Length,
  638. PUCHAR Key,
  639. ULONG KeyLength,
  640. UCHAR Digest[MD5_HASH_SIZE]
  641. );
  642. /*++
  643. Routine Description:
  644. This routine obtains an MD5 HMAC signature based on the message and key.
  645. Note that the key must be no longer than the hash function's block size.
  646. Arguments:
  647. Message - Supplies a pointer to the data buffer to hash and sign.
  648. Length - Supplies the length of the message, in bytes.
  649. Key - Supplies a pointer to the secret key buffer.
  650. KeyLength - Supplies the length of the secret key, in bytes. This must be
  651. less than or equal to 64 bytes, the block size of the SHA-1 hash
  652. function.
  653. Digest - Supplies a pointer where the HMAC digest will be returned. This
  654. buffer is assumed to be 16 bytes, the size of an MD5 hash.
  655. Return Value:
  656. None.
  657. --*/
  658. CRYPTO_API
  659. VOID
  660. CySha1Initialize (
  661. PSHA1_CONTEXT Context
  662. );
  663. /*++
  664. Routine Description:
  665. This routine initializes a SHA-1 context structure, preparing it to accept
  666. and hash data.
  667. Arguments:
  668. Context - Supplies a pointer to the context to initialize.
  669. Return Value:
  670. None.
  671. --*/
  672. CRYPTO_API
  673. VOID
  674. CySha1AddContent (
  675. PSHA1_CONTEXT Context,
  676. PUCHAR Message,
  677. UINTN Length
  678. );
  679. /*++
  680. Routine Description:
  681. This routine adds data to a SHA-1 digest.
  682. Arguments:
  683. Context - Supplies a pointer to the initialized SHA-1 context.
  684. Message - Supplies a pointer to the buffer containing the bytes.
  685. Length - Supplies the length of the message buffer, in bytes.
  686. Return Value:
  687. None.
  688. --*/
  689. CRYPTO_API
  690. VOID
  691. CySha1GetHash (
  692. PSHA1_CONTEXT Context,
  693. UCHAR Hash[SHA1_HASH_SIZE]
  694. );
  695. /*++
  696. Routine Description:
  697. This routine computes and returns the final SHA-1 hash value for the
  698. messages that have been previously entered.
  699. Arguments:
  700. Context - Supplies a pointer to the initialized SHA-1 context.
  701. Hash - Supplies a pointer where the final hash value will be returned. This
  702. buffer must be SHA1_HASH_SIZE length in bytes.
  703. Return Value:
  704. None.
  705. --*/
  706. CRYPTO_API
  707. VOID
  708. CySha256Initialize (
  709. PSHA256_CONTEXT Context
  710. );
  711. /*++
  712. Routine Description:
  713. This routine initializes a SHA-256 context structure, preparing it to
  714. accept and hash data.
  715. Arguments:
  716. Context - Supplies a pointer to the context to initialize.
  717. Return Value:
  718. None.
  719. --*/
  720. CRYPTO_API
  721. VOID
  722. CySha256AddContent (
  723. PSHA256_CONTEXT Context,
  724. PVOID Message,
  725. UINTN Length
  726. );
  727. /*++
  728. Routine Description:
  729. This routine adds data to a SHA-256 digest.
  730. Arguments:
  731. Context - Supplies a pointer to the initialized SHA-256 context.
  732. Message - Supplies a pointer to the buffer containing the bytes.
  733. Length - Supplies the length of the message buffer, in bytes.
  734. Return Value:
  735. None.
  736. --*/
  737. CRYPTO_API
  738. VOID
  739. CySha256GetHash (
  740. PSHA256_CONTEXT Context,
  741. UCHAR Hash[SHA256_HASH_SIZE]
  742. );
  743. /*++
  744. Routine Description:
  745. This routine computes and returns the final SHA-256 hash value for the
  746. messages that have been previously entered.
  747. Arguments:
  748. Context - Supplies a pointer to the initialized SHA-256 context.
  749. Hash - Supplies a pointer where the final hash value will be returned. This
  750. buffer must be SHA256_HASH_SIZE length in bytes.
  751. Return Value:
  752. None.
  753. --*/
  754. CRYPTO_API
  755. VOID
  756. CySha512Initialize (
  757. PSHA512_CONTEXT Context
  758. );
  759. /*++
  760. Routine Description:
  761. This routine initializes a SHA-512 context structure, preparing it to
  762. accept and hash data.
  763. Arguments:
  764. Context - Supplies a pointer to the context to initialize.
  765. Return Value:
  766. None.
  767. --*/
  768. CRYPTO_API
  769. VOID
  770. CySha512AddContent (
  771. PSHA512_CONTEXT Context,
  772. PVOID Message,
  773. UINTN Length
  774. );
  775. /*++
  776. Routine Description:
  777. This routine adds data to a SHA-512 digest.
  778. Arguments:
  779. Context - Supplies a pointer to the initialized SHA-512 context.
  780. Message - Supplies a pointer to the buffer containing the bytes.
  781. Length - Supplies the length of the message buffer, in bytes.
  782. Return Value:
  783. None.
  784. --*/
  785. CRYPTO_API
  786. VOID
  787. CySha512GetHash (
  788. PSHA512_CONTEXT Context,
  789. UCHAR Hash[SHA512_HASH_SIZE]
  790. );
  791. /*++
  792. Routine Description:
  793. This routine computes and returns the final SHA-512 hash value for the
  794. messages that have been previously entered.
  795. Arguments:
  796. Context - Supplies a pointer to the initialized SHA-512 context.
  797. Hash - Supplies a pointer where the final hash value will be returned. This
  798. buffer must be SHA512_HASH_SIZE length in bytes.
  799. Return Value:
  800. None.
  801. --*/
  802. CRYPTO_API
  803. VOID
  804. CyMd5Initialize (
  805. PMD5_CONTEXT Context
  806. );
  807. /*++
  808. Routine Description:
  809. This routine initializes a MD5 context structure, preparing it to accept
  810. and hash data.
  811. Arguments:
  812. Context - Supplies a pointer to the context to initialize.
  813. Return Value:
  814. None.
  815. --*/
  816. CRYPTO_API
  817. VOID
  818. CyMd5AddContent (
  819. PMD5_CONTEXT Context,
  820. PVOID Message,
  821. UINTN Length
  822. );
  823. /*++
  824. Routine Description:
  825. This routine adds data to a MD5 digest.
  826. Arguments:
  827. Context - Supplies a pointer to the initialized MD5 context.
  828. Message - Supplies a pointer to the buffer containing the bytes.
  829. Length - Supplies the length of the message buffer, in bytes.
  830. Return Value:
  831. None.
  832. --*/
  833. CRYPTO_API
  834. VOID
  835. CyMd5GetHash (
  836. PMD5_CONTEXT Context,
  837. UCHAR Hash[MD5_HASH_SIZE]
  838. );
  839. /*++
  840. Routine Description:
  841. This routine computes and returns the final MD5 hash value for the messages
  842. that have been previously entered.
  843. Arguments:
  844. Context - Supplies a pointer to the initialized MD5 context.
  845. Hash - Supplies a pointer where the final hash value will be returned. This
  846. buffer must be MD5_HASH_SIZE length in bytes.
  847. Return Value:
  848. None.
  849. --*/
  850. CRYPTO_API
  851. VOID
  852. CyFortunaInitialize (
  853. PFORTUNA_CONTEXT Context,
  854. PCY_GET_TIME_COUNTER GetTimeCounterFunction,
  855. ULONGLONG TimeCounterFrequency
  856. );
  857. /*++
  858. Routine Description:
  859. This routine initializes a Fortuna PRNG context. It does not seed it with
  860. any values.
  861. Arguments:
  862. Context - Supplies a pointer to the context.
  863. GetTimeCounterFunction - Supplies an optional pointer to a function that
  864. can be used to retrieve a monotonically non-decreasing value
  865. representing the passage of time since some epoch.
  866. TimeCounterFrequency - Supplies the frequency of the time counter in
  867. Hertz.
  868. Return Value:
  869. None.
  870. --*/
  871. CRYPTO_API
  872. VOID
  873. CyFortunaGetRandomBytes (
  874. PFORTUNA_CONTEXT Context,
  875. PUCHAR Data,
  876. UINTN Size
  877. );
  878. /*++
  879. Routine Description:
  880. This routine returns random bytes from a Fortuna instance.
  881. Arguments:
  882. Context - Supplies a pointer to the context.
  883. Data - Supplies a pointer where the random bytes will be returned.
  884. Size - Supplies the number of bytes to return.
  885. Return Value:
  886. None.
  887. --*/
  888. CRYPTO_API
  889. VOID
  890. CyFortunaAddEntropy (
  891. PFORTUNA_CONTEXT Context,
  892. PVOID Data,
  893. UINTN Size
  894. );
  895. /*++
  896. Routine Description:
  897. This routine adds random data into the mix.
  898. Arguments:
  899. Context - Supplies a pointer to the context.
  900. Data - Supplies a pointer to the data to add.
  901. Size - Supplies the number of bytes of randomness in the data buffer.
  902. Return Value:
  903. None.
  904. --*/
  905. CRYPTO_API
  906. KSTATUS
  907. CyRsaInitializeContext (
  908. PRSA_CONTEXT Context
  909. );
  910. /*++
  911. Routine Description:
  912. This routine initializes an RSA context. The caller must have filled out
  913. the allocate, reallocate, and free memory routine pointers in the big
  914. integer context, and zeroed the rest of the structure.
  915. Arguments:
  916. Context - Supplies a pointer to the context to initialize.
  917. Return Value:
  918. Status code.
  919. --*/
  920. CRYPTO_API
  921. VOID
  922. CyRsaDestroyContext (
  923. PRSA_CONTEXT Context
  924. );
  925. /*++
  926. Routine Description:
  927. This routine destroys a previously intialized RSA context.
  928. Arguments:
  929. Context - Supplies a pointer to the context to destroy.
  930. Return Value:
  931. None.
  932. --*/
  933. CRYPTO_API
  934. KSTATUS
  935. CyRsaLoadPrivateKey (
  936. PRSA_CONTEXT Context,
  937. PRSA_PRIVATE_KEY_COMPONENTS PrivateKey
  938. );
  939. /*++
  940. Routine Description:
  941. This routine adds private key information to the given RSA context.
  942. Arguments:
  943. Context - Supplies a pointer to the context.
  944. PrivateKey - Supplies a pointer to the private key information. All fields
  945. are required, including the public key ones.
  946. Return Value:
  947. Status code.
  948. --*/
  949. CRYPTO_API
  950. KSTATUS
  951. CyRsaLoadPublicKey (
  952. PRSA_CONTEXT Context,
  953. PRSA_PUBLIC_KEY_COMPONENTS PublicKey
  954. );
  955. /*++
  956. Routine Description:
  957. This routine adds public key information to the given RSA context.
  958. This routine should not be called if private key information was already
  959. added.
  960. Arguments:
  961. Context - Supplies a pointer to the context.
  962. PublicKey - Supplies a pointer to the public key information. All fields
  963. are required.
  964. Return Value:
  965. Status code.
  966. --*/
  967. CRYPTO_API
  968. INTN
  969. CyRsaDecrypt (
  970. PRSA_CONTEXT Context,
  971. PVOID Ciphertext,
  972. PVOID Plaintext,
  973. BOOL IsDecryption
  974. );
  975. /*++
  976. Routine Description:
  977. This routine performs RSA decryption.
  978. Arguments:
  979. Context - Supplies a pointer to the context.
  980. Ciphertext - Supplies a pointer to the ciphertext, which must be less than
  981. the size of the modulus minus 11.
  982. Plaintext - Supplies a pointer where the plaintext will be returned.
  983. IsDecryption - Supplies a boolean indicating if this is a decryption
  984. operation (TRUE) or a verify operation (FALSE).
  985. Return Value:
  986. Returns the number of bytes that were originally encrypted on success.
  987. -1 on allocation failure.
  988. --*/
  989. CRYPTO_API
  990. INTN
  991. CyRsaEncrypt (
  992. PRSA_CONTEXT Context,
  993. PVOID Plaintext,
  994. UINTN PlaintextLength,
  995. PVOID Ciphertext,
  996. BOOL IsSigning
  997. );
  998. /*++
  999. Routine Description:
  1000. This routine performs RSA encryption.
  1001. Arguments:
  1002. Context - Supplies a pointer to the context.
  1003. Plaintext - Supplies a pointer to the plaintext to encrypt.
  1004. PlaintextLength - Supplies the length of the plaintext buffer in bytes.
  1005. Ciphertext - Supplies a pointer where the ciphertext will be returned.
  1006. This buffer must be the size of the modulus.
  1007. IsSigning - Supplies a boolean indicating whether this is a signing
  1008. operation (TRUE) and should therefore use the private key, or
  1009. whether this is an encryption operation (FALSE) and should use the
  1010. public key.
  1011. Return Value:
  1012. Returns the number of bytes that were originally encrypted on success. This
  1013. is the same as the modulus size.
  1014. -1 on allocation failure.
  1015. --*/
  1016. CRYPTO_API
  1017. KSTATUS
  1018. CyRsaAddPemFile (
  1019. PRSA_CONTEXT RsaContext,
  1020. PVOID PemFile,
  1021. UINTN PemFileLength,
  1022. PSTR Password
  1023. );
  1024. /*++
  1025. Routine Description:
  1026. This routine attempts to add a private key to the given RSA context.
  1027. Arguments:
  1028. RsaContext - Supplies a pointer to the previously initialized RSA context.
  1029. PemFile - Supplies a pointer to the PEM file contents.
  1030. PemFileLength - Supplies the length of the PEM file contents.
  1031. Password - Supplies an optional pointer to a password to decrypt the
  1032. private key if needed.
  1033. Return Value:
  1034. Status code.
  1035. --*/
  1036. CRYPTO_API
  1037. UINTN
  1038. CyBase64GetDecodedLength (
  1039. UINTN EncodedDataLength
  1040. );
  1041. /*++
  1042. Routine Description:
  1043. This routine returns the buffer size needed for a decode buffer with a
  1044. given encoded buffer length. This may not be the actual decoded data size,
  1045. but is a worst-case approximation.
  1046. Arguments:
  1047. EncodedDataLength - Supplies the length of the encoded data, in bytes, not
  1048. including a null terminator.
  1049. Return Value:
  1050. Returns the appropriate size of the decoded data buffer.
  1051. --*/
  1052. CRYPTO_API
  1053. UINTN
  1054. CyBase64GetEncodedLength (
  1055. UINTN DataLength
  1056. );
  1057. /*++
  1058. Routine Description:
  1059. This routine returns the buffer size needed for a Base64 encoded buffer
  1060. given a raw data buffer of the given size. This may not be the actual
  1061. encoded data size, but is a worst-case approximation.
  1062. Arguments:
  1063. DataLength - Supplies the length of the raw data to encode, in bytes.
  1064. Return Value:
  1065. Returns the appropriate size of the encoded data buffer, including space
  1066. for a null terminator.
  1067. --*/
  1068. CRYPTO_API
  1069. BOOL
  1070. CyBase64Decode (
  1071. PSTR EncodedData,
  1072. UINTN EncodedDataLength,
  1073. PUCHAR Data,
  1074. PUINTN DataLength
  1075. );
  1076. /*++
  1077. Routine Description:
  1078. This routine decodes the given Base64 encoded data.
  1079. Arguments:
  1080. EncodedData - Supplies a pointer to the encoded data string.
  1081. EncodedDataLength - Supplies the length of the encoded data in bytes,
  1082. not including a null terminator.
  1083. Data - Supplies a pointer where the decoded data will be returned. It is
  1084. assumed this buffer is big enough.
  1085. DataLength - Supplies a pointer where the final length of the returned data
  1086. will be returned.
  1087. Return Value:
  1088. TRUE on success.
  1089. FALSE if there was a data decoding error at the end.
  1090. --*/
  1091. CRYPTO_API
  1092. VOID
  1093. CyBase64Encode (
  1094. PUCHAR Data,
  1095. UINTN DataLength,
  1096. PSTR EncodedData,
  1097. PUINTN EncodedDataLength
  1098. );
  1099. /*++
  1100. Routine Description:
  1101. This routine encodes the given data in Base64 format.
  1102. Arguments:
  1103. Data - Supplies a pointer to the data to encode.
  1104. DataLength - Supplies the length of the data to encode in bytes.
  1105. EncodedData - Supplies a pointer where the encoded data will be returned.
  1106. It is assumed this buffer is big enough to hold the encoded data.
  1107. EncodedDataLength - Supplies a pointer where the actual length of the
  1108. encoded data, including the null terminator, will be returned on
  1109. success.
  1110. Return Value:
  1111. None.
  1112. --*/
  1113. //
  1114. // -------------------------------------------------------- Function Prototypes
  1115. //