luajitos

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

easycrypto_demo.c (8320B)


      1 /*
      2  * EasyCrypto Demo
      3  * Shows simple encryption API usage
      4  */
      5 
      6 #include "EasyCrypto.h"
      7 #include "PBKDF2.h"
      8 #include <stdio.h>
      9 
     10 int main(void) {
     11     printf("╔════════════════════════════════════════════════════╗\n");
     12     printf("║   EasyCrypto - Simple Encryption API              ║\n");
     13     printf("╚════════════════════════════════════════════════════╝\n\n");
     14 
     15     printf("Features:\n");
     16     printf("• Simple ENCRYPT(key, data, len) macro\n");
     17     printf("• Automatic nonce generation\n");
     18     printf("• Authenticated encryption (AEAD)\n");
     19     printf("• Default: AES-256-GCM (hardware accelerated)\n");
     20     printf("• Alternative: ChaCha20-Poly1305\n\n");
     21 
     22     // Initialize global CSPRNG
     23     csprng_global_init();
     24 
     25     printf("════════════════════════════════════════════════════\n");
     26     printf("Example 1: Encrypt with Generated Key\n");
     27     printf("════════════════════════════════════════════════════\n\n");
     28 
     29     // Generate a random key
     30     uint8_t key[32];
     31     random_bytes(key, 32);
     32 
     33     printf("Generated key: ");
     34     for (int i = 0; i < 16; i++) printf("%02x", key[i]);
     35     printf("...\n\n");
     36 
     37     // Message to encrypt
     38     const char *message = "Hello, EasyCrypto! This is a secret message.";
     39     printf("Message: \"%s\"\n\n", message);
     40 
     41     // Encrypt (uses AES-256-GCM by default)
     42     encrypted_data *enc = ENCRYPT(key, (uint8_t*)message, strlen(message));
     43     if (!enc) {
     44         fprintf(stderr, "✗ Encryption failed\n");
     45         return 1;
     46     }
     47 
     48     printf("✓ Encrypted successfully\n");
     49     printf("  Algorithm: AES-256-GCM\n");
     50     printf("  Nonce: ");
     51     for (size_t i = 0; i < enc->nonce_len; i++) {
     52         printf("%02x", enc->nonce[i]);
     53     }
     54     printf("\n");
     55     printf("  Ciphertext (%zu bytes): ", enc->ciphertext_len);
     56     for (size_t i = 0; i < (enc->ciphertext_len < 32 ? enc->ciphertext_len : 32); i++) {
     57         printf("%02x", enc->ciphertext[i]);
     58     }
     59     if (enc->ciphertext_len > 32) printf("...");
     60     printf("\n");
     61     printf("  Tag: ");
     62     for (size_t i = 0; i < enc->tag_len; i++) {
     63         printf("%02x", enc->tag[i]);
     64     }
     65     printf("\n\n");
     66 
     67     // Decrypt
     68     size_t dec_len;
     69     uint8_t *decrypted = DECRYPT(key, enc, &dec_len);
     70     if (!decrypted) {
     71         fprintf(stderr, "✗ Decryption failed\n");
     72         encrypted_data_free(enc);
     73         return 1;
     74     }
     75 
     76     printf("✓ Decrypted successfully\n");
     77     printf("  Message: \"");
     78     fwrite(decrypted, 1, dec_len, stdout);
     79     printf("\"\n\n");
     80 
     81     // Verify
     82     if (dec_len == strlen(message) && memcmp(decrypted, message, dec_len) == 0) {
     83         printf("✓ Encryption/Decryption verified!\n\n");
     84     }
     85 
     86     // Cleanup
     87     encrypted_data_free(enc);
     88     free(decrypted);
     89 
     90     printf("════════════════════════════════════════════════════\n");
     91     printf("Example 2: Encrypt with Password (PBKDF2)\n");
     92     printf("════════════════════════════════════════════════════\n\n");
     93 
     94     // Derive key from password
     95     const char *password = "super secret password";
     96     uint8_t salt[16];
     97     random_bytes(salt, 16);
     98 
     99     uint8_t derived_key[32];
    100 
    101     printf("Password: \"%s\"\n", password);
    102     printf("Salt: ");
    103     for (int i = 0; i < 16; i++) printf("%02x", salt[i]);
    104     printf("\n");
    105     printf("Deriving key with PBKDF2 (100,000 iterations)...\n");
    106 
    107     if (pbkdf2_hmac_sha256((uint8_t*)password, strlen(password),
    108                           salt, 16, PBKDF2_ITERATIONS_MIN,
    109                           derived_key, 32) != 0) {
    110         fprintf(stderr, "✗ Key derivation failed\n");
    111         return 1;
    112     }
    113 
    114     printf("Derived key: ");
    115     for (int i = 0; i < 16; i++) printf("%02x", derived_key[i]);
    116     printf("...\n\n");
    117 
    118     // Encrypt with derived key
    119     const char *secret = "This data is protected by a password!";
    120     printf("Secret data: \"%s\"\n\n", secret);
    121 
    122     encrypted_data *enc2 = ENCRYPT(derived_key, (uint8_t*)secret, strlen(secret));
    123     if (!enc2) {
    124         fprintf(stderr, "✗ Encryption failed\n");
    125         return 1;
    126     }
    127 
    128     printf("✓ Encrypted with password-derived key\n");
    129     printf("  Ciphertext: ");
    130     for (size_t i = 0; i < (enc2->ciphertext_len < 32 ? enc2->ciphertext_len : 32); i++) {
    131         printf("%02x", enc2->ciphertext[i]);
    132     }
    133     if (enc2->ciphertext_len > 32) printf("...");
    134     printf("\n\n");
    135 
    136     // Decrypt
    137     uint8_t *dec2 = DECRYPT(derived_key, enc2, &dec_len);
    138     if (!dec2) {
    139         fprintf(stderr, "✗ Decryption failed\n");
    140         encrypted_data_free(enc2);
    141         return 1;
    142     }
    143 
    144     printf("✓ Decrypted: \"");
    145     fwrite(dec2, 1, dec_len, stdout);
    146     printf("\"\n\n");
    147 
    148     encrypted_data_free(enc2);
    149     free(dec2);
    150 
    151     printf("════════════════════════════════════════════════════\n");
    152     printf("Example 3: Algorithm Selection\n");
    153     printf("════════════════════════════════════════════════════\n\n");
    154 
    155     const char *data = "Test message";
    156 
    157     // AES (default)
    158     encrypted_data *enc_aes = ENCRYPT_AES(key, (uint8_t*)data, strlen(data));
    159     printf("✓ AES-256-GCM:      Encrypted %zu bytes → %zu bytes + 16-byte tag\n",
    160            strlen(data), enc_aes->ciphertext_len);
    161 
    162     // ChaCha20
    163     encrypted_data *enc_chacha = ENCRYPT_CHACHA(key, (uint8_t*)data, strlen(data));
    164     printf("✓ ChaCha20-Poly1305: Encrypted %zu bytes → %zu bytes + 16-byte tag\n",
    165            strlen(data), enc_chacha->ciphertext_len);
    166 
    167     // Decrypt both
    168     uint8_t *dec_aes = DECRYPT_AES(key, enc_aes, &dec_len);
    169     uint8_t *dec_chacha = DECRYPT_CHACHA(key, enc_chacha, &dec_len);
    170 
    171     printf("\n✓ Both algorithms work correctly\n\n");
    172 
    173     encrypted_data_free(enc_aes);
    174     encrypted_data_free(enc_chacha);
    175     free(dec_aes);
    176     free(dec_chacha);
    177 
    178     printf("════════════════════════════════════════════════════\n");
    179     printf("Example 4: Authentication Verification\n");
    180     printf("════════════════════════════════════════════════════\n\n");
    181 
    182     encrypted_data *enc3 = ENCRYPT(key, (uint8_t*)message, strlen(message));
    183 
    184     printf("Original tag: ");
    185     for (size_t i = 0; i < 8; i++) printf("%02x", enc3->tag[i]);
    186     printf("...\n");
    187 
    188     // Corrupt the tag
    189     enc3->tag[0] ^= 0x01;
    190     printf("Corrupted tag: ");
    191     for (size_t i = 0; i < 8; i++) printf("%02x", enc3->tag[i]);
    192     printf("...\n\n");
    193 
    194     uint8_t *dec3 = DECRYPT(key, enc3, &dec_len);
    195     if (dec3) {
    196         printf("✗ Should have rejected corrupted data!\n");
    197         free(dec3);
    198     } else {
    199         printf("✓ Correctly rejected corrupted data\n");
    200     }
    201 
    202     encrypted_data_free(enc3);
    203 
    204     printf("\n════════════════════════════════════════════════════\n");
    205     printf("Summary:\n");
    206     printf("════════════════════════════════════════════════════\n");
    207     printf("• ENCRYPT/DECRYPT - Simple, secure API\n");
    208     printf("• AES-256-GCM default (hardware accelerated)\n");
    209     printf("• Automatic nonce generation\n");
    210     printf("• Authentication included (AEAD)\n");
    211     printf("• Works with PBKDF2 for password-based encryption\n");
    212     printf("• Production-ready\n");
    213 
    214     // Zero sensitive data
    215     memset(key, 0, 32);
    216     memset(derived_key, 0, 32);
    217 
    218     return 0;
    219 }