Advertisement
AnthonyCagliano

Untitled

Apr 9th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. /*
  2. *--------------------------------------
  3. * Program Name:
  4. * Author:
  5. * License:
  6. * Description:
  7. *--------------------------------------
  8. */
  9.  
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <stdbool.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <encrypt.h>
  16. #include <alloca.h>
  17.  
  18. #define CEMU_CONSOLE ((char*)0xFB0000)
  19. char *msg = "The lazy fox jumped over the dog!";
  20. char *secondmsg = "The dog got angry and barked";
  21. #define KEYSIZE (256>>3) // 256 bits converted to bytes
  22.  
  23. // we will reuse the key and IV for the sake of demo
  24. // in an actual use-case, both key and IV should be generated by a secure RNG
  25. // and also probably shouldn't be globals
  26. uint8_t key[KEYSIZE] = {
  27. 0xEE,0x89,0x19,0xC3,0x8D,0x53,0x7A,0xD6,0x04,0x19,0x9E,0x77,0x0B,0xE0,0xE0,0x4C,0x4C,0x70,0xDB,0xE1,0x22,0x79,0xE1,0x90,0x06,0x1B,0xAF,0x99,0x49,0x8E,0x66,0x73
  28. };
  29. uint8_t iv[CRYPTX_AES_IV_SIZE] = {
  30. 0x79,0xA6,0xDE,0xDF,0xF0,0xA2,0x7C,0x7F,0xEE,0x0B,0x8E,0xF5,0x12,0x63,0xA4,0x8A
  31. };
  32. aes_error_t error;
  33. struct cryptx_aes_ctx ctx;
  34.  
  35. void hexdump(uint8_t *addr, size_t len, char *label){
  36. if(label) sprintf(CEMU_CONSOLE, "\n%s\n", label);
  37. else sprintf(CEMU_CONSOLE, "\n");
  38. for(size_t rem_len = len, ct=1; rem_len>0; rem_len--, addr++, ct++){
  39. sprintf(CEMU_CONSOLE, "\\x%02X", *addr);
  40. if(!(ct%CRYPTX_AES_BLOCK_SIZE)) sprintf(CEMU_CONSOLE, "\n");
  41. }
  42. sprintf(CEMU_CONSOLE, "\n");
  43. }
  44.  
  45.  
  46. void demo_aes_cbc(void){
  47.  
  48. // declare buffers
  49. size_t ctlen = strlen(msg)/CRYPTX_AES_BLOCK_SIZE*CRYPTX_AES_BLOCK_SIZE+1;
  50. uint8_t *ebuf = alloca(ctlen);
  51. uint8_t *dbuf = alloca(ctlen);
  52.  
  53. sprintf(CEMU_CONSOLE, "\n-----------------------------------\nCyclic Block Chain (CBC) mode\n\n");
  54.  
  55. // init AES context and echo status
  56. error = cryptx_aes_init(&ctx, key, KEYSIZE, iv, CRYPTX_AES_IV_SIZE, CRYPTX_AES_CBC_FLAGS(0));
  57. sprintf(CEMU_CONSOLE, "cbc init complete, exit code %u\n", error);
  58.  
  59. // encrypt string and echo status
  60. memset(ebuf, 0, ctlen);
  61. error = cryptx_aes_encrypt(&ctx, msg, strlen(msg), ebuf);
  62. sprintf(CEMU_CONSOLE, "cbc encryption done, exit code %u\n", error);
  63. hexdump(ebuf, ctlen, "-- encrypted msg --");
  64.  
  65. // reset context for decryption and echo status
  66. error = cryptx_aes_init(&ctx, key, KEYSIZE, iv, CRYPTX_AES_IV_SIZE, CRYPTX_AES_CBC_FLAGS(0));
  67. sprintf(CEMU_CONSOLE, "cbc init complete, exit code %u\n", error);
  68.  
  69. // encrypt string and echo status
  70. memset(dbuf, 0, ctlen);
  71. error = cryptx_aes_decrypt(&ctx, ebuf, ctlen, dbuf);
  72. sprintf(CEMU_CONSOLE, "cbc decryption done, exit code %u\n", error);
  73. sprintf(CEMU_CONSOLE, "%s\n", dbuf);
  74. }
  75.  
  76. void demo_aes_ctr(void){
  77.  
  78. // declare buffers
  79. size_t ctlen = strlen(msg);
  80. uint8_t *ebuf = alloca(ctlen);
  81. uint8_t *dbuf = alloca(ctlen);
  82.  
  83. sprintf(CEMU_CONSOLE, "\n-----------------------------------\nCounter (CTR) mode\n\n");
  84.  
  85. // init AES context and echo status
  86. error = cryptx_aes_init(&ctx, key, KEYSIZE, iv, CRYPTX_AES_IV_SIZE, CRYPTX_AES_CTR_FLAGS(8,8));
  87. sprintf(CEMU_CONSOLE, "ctr init complete, exit code %u\n", error);
  88.  
  89. // encrypt string and echo status
  90. memset(ebuf, 0, ctlen);
  91. error = cryptx_aes_encrypt(&ctx, msg, strlen(msg), ebuf);
  92. sprintf(CEMU_CONSOLE, "ctr encryption done, exit code %u\n", error);
  93. hexdump(ebuf, ctlen, "-- encrypted msg --");
  94.  
  95. // reset context for decryption and echo status
  96. error = cryptx_aes_init(&ctx, key, KEYSIZE, iv, CRYPTX_AES_IV_SIZE, CRYPTX_AES_CTR_FLAGS(8,8));
  97. sprintf(CEMU_CONSOLE, "ctr init complete, exit code %u\n", error);
  98.  
  99. // encrypt string and echo status
  100. memset(dbuf, 0, ctlen);
  101. error = cryptx_aes_decrypt(&ctx, ebuf, ctlen, dbuf);
  102. sprintf(CEMU_CONSOLE, "ctr decryption done, exit code %u\n", error);
  103. sprintf(CEMU_CONSOLE, "%s\n", dbuf);
  104. }
  105.  
  106. void demo_aes_gcm(void){
  107.  
  108. // declare buffers
  109. size_t ctlen = strlen(msg);
  110. char *associated = "Some random header";
  111. uint8_t *ebuf = alloca(ctlen);
  112. uint8_t *dbuf = alloca(ctlen);
  113. uint8_t odigest[CRYPTX_AES_BLOCK_SIZE], vdigest[CRYPTX_AES_BLOCK_SIZE];
  114.  
  115. sprintf(CEMU_CONSOLE, "\n-----------------------------------\nGalois Counter (GCM) mode\n\n");
  116.  
  117. // init AES context and echo status
  118. error = cryptx_aes_init(&ctx, key, KEYSIZE, iv, 13, CRYPTX_AES_GCM_FLAGS);
  119. sprintf(CEMU_CONSOLE, "gcm init complete, exit code %u\n", error);
  120.  
  121. // update context for associated data
  122. cryptx_aes_update_aad(&ctx, associated, strlen(associated));
  123.  
  124. // encrypt string and echo status
  125. memset(ebuf, 0, ctlen);
  126. error = cryptx_aes_encrypt(&ctx, msg, strlen(msg), ebuf);
  127. sprintf(CEMU_CONSOLE, "gcm encryption done, exit code %u\n", error);
  128. hexdump(ebuf, ctlen, "-- encrypted msg --");
  129.  
  130. // now echo digest/auth tag
  131. error = cryptx_aes_digest(&ctx, odigest);
  132. sprintf(CEMU_CONSOLE, "gcm digest return done, exit code %u\n", error);
  133. hexdump(odigest, CRYPTX_AES_BLOCK_SIZE, "-- digest of aad + ciphertext --");
  134.  
  135. // reset context for decryption and echo status
  136. error = cryptx_aes_init(&ctx, key, KEYSIZE, iv, 13, CRYPTX_AES_GCM_FLAGS);
  137. sprintf(CEMU_CONSOLE, "gcm init complete, exit code %u\n", error);
  138.  
  139. // #######################################################
  140. // to test an invalid ciphertext uncomment the line below
  141. // ebuf[10] ^= 0xff;
  142.  
  143. // this is a macro to do a few things. See the header file
  144. if(!cryptx_aes_verify(&ctx, associated, strlen(associated), ebuf, ctlen, odigest)){
  145. sprintf(CEMU_CONSOLE, "auth tag invalid. not decrypting.\n");
  146. return;
  147. }
  148. else
  149. sprintf(CEMU_CONSOLE, "auth tag valid. proceeding.\n");
  150.  
  151. // update context for associated data
  152. cryptx_aes_update_aad(&ctx, associated, strlen(associated));
  153.  
  154. // decrypt string and echo status
  155. memset(dbuf, 0, ctlen);
  156. error = cryptx_aes_decrypt(&ctx, ebuf, ctlen, dbuf);
  157. sprintf(CEMU_CONSOLE, "gcm decryption done, exit code %u\n", error);
  158. sprintf(CEMU_CONSOLE, "%s\n", dbuf);
  159.  
  160.  
  161.  
  162. // now echo digest/auth tag
  163. error = cryptx_aes_digest(&ctx, vdigest);
  164. sprintf(CEMU_CONSOLE, "gcm digest return done, exit code %u\n", error);
  165. hexdump(vdigest, CRYPTX_AES_BLOCK_SIZE, "-- digest of aad + ciphertext --");
  166. }
  167.  
  168.  
  169.  
  170. int main(void)
  171. {
  172. sprintf(CEMU_CONSOLE, "\n------------------------------\nCryptX AES Demo\n------------------------------\n");
  173.  
  174. // sprintf(CEMU_CONSOLE, "\n----- CTR Mode -----\n");
  175.  
  176. // generate random key and IV
  177. if(!cryptx_csrand_init(SAMPLING_FAST)) return 1; // <<<----- DONT FORGET THIS
  178. // !!!! NEVER PROCEED WITH ANYTHING CRYPTOGRAPHIC !!!!
  179. // !!!! IF THE CSRNG FAILS TO INIT !!!!
  180.  
  181.  
  182. // show the IV and key for testing purposes
  183. sprintf(CEMU_CONSOLE, "\n-----------------------------------\nSecrets for Testing\n\n");
  184. hexdump(key, KEYSIZE, "-- aes key --");
  185. hexdump(iv, CRYPTX_AES_IV_SIZE, "-- initialization vector --");
  186.  
  187. demo_aes_cbc();
  188. demo_aes_ctr();
  189. demo_aes_gcm();
  190.  
  191. }
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement