luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

example_easycrypto.c (6324B)


      1 /*
      2  * Example program demonstrating EasyCrypto API
      3  */
      4 
      5 #include "EasyCrypto.h"
      6 #include <stdio.h>
      7 #include <string.h>
      8 
      9 int main(void) {
     10     printf("EasyCrypto Demo\n");
     11     printf("===============\n\n");
     12 
     13     // Initialize the library
     14     easycrypto_init();
     15 
     16     // Generate a random key
     17     uint8_t key[32];
     18     easycrypto_generate_key(key);
     19     easycrypto_set_key(key);
     20 
     21     printf("Generated 256-bit key:\n");
     22     easycrypto_print_hex("Key", key, 32);
     23     printf("\n");
     24 
     25     // Test data
     26     const char *message = "Hello, EasyCrypto! This is a secret message.";
     27     printf("Original message: %s\n\n", message);
     28 
     29     // ==========================================
     30     // Test 1: AES-256-GCM (default - 2 args)
     31     // ==========================================
     32     printf("Test 1: AES-256-GCM (default)\n");
     33     printf("------------------------------\n");
     34 
     35     CryptoResult encrypted_aes = ENCRYPT((uint8_t*)message, strlen(message));
     36 
     37     if (encrypted_aes.success) {
     38         printf("Encryption successful!\n");
     39         printf("Encrypted size: %zu bytes\n", encrypted_aes.length);
     40         easycrypto_print_hex("Encrypted", encrypted_aes.data, encrypted_aes.length);
     41         printf("\n");
     42 
     43         // Decrypt
     44         CryptoResult decrypted_aes = DECRYPT(encrypted_aes.data, encrypted_aes.length);
     45 
     46         if (decrypted_aes.success) {
     47             printf("Decryption successful!\n");
     48             printf("Decrypted message: %.*s\n\n", (int)decrypted_aes.length, decrypted_aes.data);
     49 
     50             // Verify
     51             if (memcmp(message, decrypted_aes.data, strlen(message)) == 0) {
     52                 printf("✓ AES-256-GCM: Message verified!\n\n");
     53             } else {
     54                 printf("✗ AES-256-GCM: Verification failed!\n\n");
     55             }
     56 
     57             easycrypto_free(&decrypted_aes);
     58         } else {
     59             printf("✗ Decryption failed!\n\n");
     60         }
     61 
     62         easycrypto_free(&encrypted_aes);
     63     } else {
     64         printf("✗ Encryption failed!\n\n");
     65     }
     66 
     67     // ==========================================
     68     // Test 2: Serpent-256-GCM (explicit - 3 args)
     69     // ==========================================
     70     printf("Test 2: Serpent-256-GCM (explicit)\n");
     71     printf("----------------------------------\n");
     72 
     73     CryptoResult encrypted_serpent = ENCRYPT(SERPENT_256_GCM, (uint8_t*)message, strlen(message));
     74 
     75     if (encrypted_serpent.success) {
     76         printf("Encryption successful!\n");
     77         printf("Encrypted size: %zu bytes\n", encrypted_serpent.length);
     78         easycrypto_print_hex("Encrypted", encrypted_serpent.data, encrypted_serpent.length);
     79         printf("\n");
     80 
     81         // Decrypt
     82         CryptoResult decrypted_serpent = DECRYPT(encrypted_serpent.data, encrypted_serpent.length);
     83 
     84         if (decrypted_serpent.success) {
     85             printf("Decryption successful!\n");
     86             printf("Decrypted message: %.*s\n\n", (int)decrypted_serpent.length, decrypted_serpent.data);
     87 
     88             // Verify
     89             if (memcmp(message, decrypted_serpent.data, strlen(message)) == 0) {
     90                 printf("✓ Serpent-256-GCM: Message verified!\n\n");
     91             } else {
     92                 printf("✗ Serpent-256-GCM: Verification failed!\n\n");
     93             }
     94 
     95             easycrypto_free(&decrypted_serpent);
     96         } else {
     97             printf("✗ Decryption failed!\n\n");
     98         }
     99 
    100         easycrypto_free(&encrypted_serpent);
    101     } else {
    102         printf("✗ Encryption failed!\n\n");
    103     }
    104 
    105     // ==========================================
    106     // Test 3: With AAD (Additional Authenticated Data)
    107     // ==========================================
    108     printf("Test 3: AES-256-GCM with AAD\n");
    109     printf("-----------------------------\n");
    110 
    111     const char *aad_data = "metadata:user=alice,timestamp=123456";
    112     printf("AAD: %s\n", aad_data);
    113 
    114     CryptoResult encrypted_with_aad = easycrypto_encrypt(
    115         AES_256_GCM,
    116         (uint8_t*)message, strlen(message),
    117         (uint8_t*)aad_data, strlen(aad_data)
    118     );
    119 
    120     if (encrypted_with_aad.success) {
    121         printf("Encryption with AAD successful!\n");
    122 
    123         // Decrypt with AAD
    124         CryptoResult decrypted_with_aad = easycrypto_decrypt(
    125             encrypted_with_aad.data, encrypted_with_aad.length,
    126             (uint8_t*)aad_data, strlen(aad_data)
    127         );
    128 
    129         if (decrypted_with_aad.success) {
    130             printf("Decryption with AAD successful!\n");
    131             printf("Decrypted message: %.*s\n", (int)decrypted_with_aad.length, decrypted_with_aad.data);
    132             printf("✓ AAD authentication passed!\n\n");
    133             easycrypto_free(&decrypted_with_aad);
    134         } else {
    135             printf("✗ Decryption with AAD failed!\n\n");
    136         }
    137 
    138         // Try to decrypt with wrong AAD (should fail)
    139         printf("Testing with wrong AAD...\n");
    140         const char *wrong_aad = "wrong metadata";
    141         CryptoResult decrypted_wrong_aad = easycrypto_decrypt(
    142             encrypted_with_aad.data, encrypted_with_aad.length,
    143             (uint8_t*)wrong_aad, strlen(wrong_aad)
    144         );
    145 
    146         if (!decrypted_wrong_aad.success) {
    147             printf("✓ Correctly rejected wrong AAD!\n\n");
    148         } else {
    149             printf("✗ Should have rejected wrong AAD!\n\n");
    150             easycrypto_free(&decrypted_wrong_aad);
    151         }
    152 
    153         easycrypto_free(&encrypted_with_aad);
    154     } else {
    155         printf("✗ Encryption with AAD failed!\n\n");
    156     }
    157 
    158     // ==========================================
    159     // Test 4: Tamper detection
    160     // ==========================================
    161     printf("Test 4: Tamper detection\n");
    162     printf("------------------------\n");
    163 
    164     CryptoResult encrypted_tamper = ENCRYPT((uint8_t*)message, strlen(message));
    165 
    166     if (encrypted_tamper.success) {
    167         // Corrupt a byte in the ciphertext
    168         encrypted_tamper.data[20] ^= 0xFF;
    169 
    170         printf("Corrupted ciphertext, attempting decrypt...\n");
    171         CryptoResult decrypted_tamper = DECRYPT(encrypted_tamper.data, encrypted_tamper.length);
    172 
    173         if (!decrypted_tamper.success) {
    174             printf("✓ Correctly detected tampering!\n\n");
    175         } else {
    176             printf("✗ Should have detected tampering!\n\n");
    177             easycrypto_free(&decrypted_tamper);
    178         }
    179 
    180         easycrypto_free(&encrypted_tamper);
    181     }
    182 
    183     printf("All tests completed!\n");
    184 
    185     return 0;
    186 }