|
| 1 | +/* aes-direct.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2025 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +/* AES-DIRECT Example |
| 23 | + * This example demonstrates: |
| 24 | + * - One-shot encryption using wc_AesEncryptDirect() (single block) |
| 25 | + * - One-shot decryption using wc_AesDecryptDirect() (single block) |
| 26 | + * Note: DIRECT mode operates on single 16-byte blocks only |
| 27 | + * No streaming API available - this is raw block cipher access |
| 28 | + */ |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | +#include <stdlib.h> |
| 32 | +#include <string.h> |
| 33 | + |
| 34 | +#ifndef WOLFSSL_USER_SETTINGS |
| 35 | + #include <wolfssl/options.h> |
| 36 | +#endif |
| 37 | +#include <wolfssl/wolfcrypt/settings.h> |
| 38 | +#include <wolfssl/wolfcrypt/aes.h> |
| 39 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 40 | + |
| 41 | +#if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
| 42 | + |
| 43 | +#define AES_KEY_SIZE AES_256_KEY_SIZE |
| 44 | + |
| 45 | +static int read_file(const char* filename, byte** data, word32* dataSz) |
| 46 | +{ |
| 47 | + FILE* fp; |
| 48 | + long fileSz; |
| 49 | + |
| 50 | + fp = fopen(filename, "rb"); |
| 51 | + if (fp == NULL) { |
| 52 | + printf("Error: Cannot open file %s\n", filename); |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + fseek(fp, 0, SEEK_END); |
| 57 | + fileSz = ftell(fp); |
| 58 | + fseek(fp, 0, SEEK_SET); |
| 59 | + |
| 60 | + *data = (byte*)malloc(fileSz); |
| 61 | + if (*data == NULL) { |
| 62 | + fclose(fp); |
| 63 | + printf("Error: Memory allocation failed\n"); |
| 64 | + return -1; |
| 65 | + } |
| 66 | + |
| 67 | + *dataSz = (word32)fread(*data, 1, fileSz, fp); |
| 68 | + fclose(fp); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +static int write_file(const char* filename, const byte* data, word32 dataSz) |
| 74 | +{ |
| 75 | + FILE* fp; |
| 76 | + |
| 77 | + fp = fopen(filename, "wb"); |
| 78 | + if (fp == NULL) { |
| 79 | + printf("Error: Cannot create file %s\n", filename); |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + fwrite(data, 1, dataSz, fp); |
| 84 | + fclose(fp); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +/* One-shot encryption using direct block cipher */ |
| 90 | +static int encrypt_file(const char* inFile, const char* outFile, |
| 91 | + const byte* key, word32 keySz) |
| 92 | +{ |
| 93 | + Aes aes; |
| 94 | + byte* plaintext = NULL; |
| 95 | + byte* ciphertext = NULL; |
| 96 | + word32 plaintextSz; |
| 97 | + word32 paddedSz; |
| 98 | + word32 padLen; |
| 99 | + word32 i; |
| 100 | + int ret; |
| 101 | + |
| 102 | + ret = read_file(inFile, &plaintext, &plaintextSz); |
| 103 | + if (ret != 0) return ret; |
| 104 | + |
| 105 | + /* Calculate padded size (PKCS#7 padding) */ |
| 106 | + padLen = AES_BLOCK_SIZE - (plaintextSz % AES_BLOCK_SIZE); |
| 107 | + paddedSz = plaintextSz + padLen; |
| 108 | + |
| 109 | + /* Store original size at beginning (4 bytes) + ciphertext */ |
| 110 | + ciphertext = (byte*)malloc(4 + paddedSz); |
| 111 | + if (ciphertext == NULL) { |
| 112 | + free(plaintext); |
| 113 | + return -1; |
| 114 | + } |
| 115 | + |
| 116 | + /* Apply PKCS#7 padding */ |
| 117 | + plaintext = (byte*)realloc(plaintext, paddedSz); |
| 118 | + memset(plaintext + plaintextSz, (int)padLen, padLen); |
| 119 | + |
| 120 | + /* Initialize AES */ |
| 121 | + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); |
| 122 | + if (ret != 0) { |
| 123 | + free(plaintext); |
| 124 | + free(ciphertext); |
| 125 | + return ret; |
| 126 | + } |
| 127 | + |
| 128 | + ret = wc_AesSetKeyDirect(&aes, key, keySz, NULL, AES_ENCRYPTION); |
| 129 | + if (ret != 0) { |
| 130 | + wc_AesFree(&aes); |
| 131 | + free(plaintext); |
| 132 | + free(ciphertext); |
| 133 | + return ret; |
| 134 | + } |
| 135 | + |
| 136 | + /* Encrypt block by block using direct API */ |
| 137 | + for (i = 0; i < paddedSz; i += AES_BLOCK_SIZE) { |
| 138 | + ret = wc_AesEncryptDirect(&aes, ciphertext + 4 + i, plaintext + i); |
| 139 | + if (ret != 0) { |
| 140 | + wc_AesFree(&aes); |
| 141 | + free(plaintext); |
| 142 | + free(ciphertext); |
| 143 | + return ret; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + wc_AesFree(&aes); |
| 148 | + |
| 149 | + /* Store original size at beginning */ |
| 150 | + ciphertext[0] = (byte)(plaintextSz >> 24); |
| 151 | + ciphertext[1] = (byte)(plaintextSz >> 16); |
| 152 | + ciphertext[2] = (byte)(plaintextSz >> 8); |
| 153 | + ciphertext[3] = (byte)(plaintextSz); |
| 154 | + |
| 155 | + ret = write_file(outFile, ciphertext, 4 + paddedSz); |
| 156 | + |
| 157 | + free(plaintext); |
| 158 | + free(ciphertext); |
| 159 | + |
| 160 | + printf("AES-DIRECT encryption complete (one-shot, block by block)\n"); |
| 161 | + return ret; |
| 162 | +} |
| 163 | + |
| 164 | +/* One-shot decryption using direct block cipher */ |
| 165 | +static int decrypt_file(const char* inFile, const char* outFile, |
| 166 | + const byte* key, word32 keySz) |
| 167 | +{ |
| 168 | + Aes aes; |
| 169 | + byte* ciphertext = NULL; |
| 170 | + byte* plaintext = NULL; |
| 171 | + word32 ciphertextSz; |
| 172 | + word32 plaintextSz; |
| 173 | + word32 originalSz; |
| 174 | + word32 i; |
| 175 | + int ret; |
| 176 | + |
| 177 | + ret = read_file(inFile, &ciphertext, &ciphertextSz); |
| 178 | + if (ret != 0) return ret; |
| 179 | + |
| 180 | + if (ciphertextSz < 4 + AES_BLOCK_SIZE) { |
| 181 | + free(ciphertext); |
| 182 | + printf("Error: File too small\n"); |
| 183 | + return -1; |
| 184 | + } |
| 185 | + |
| 186 | + /* Extract original size from beginning */ |
| 187 | + originalSz = ((word32)ciphertext[0] << 24) | |
| 188 | + ((word32)ciphertext[1] << 16) | |
| 189 | + ((word32)ciphertext[2] << 8) | |
| 190 | + ((word32)ciphertext[3]); |
| 191 | + |
| 192 | + plaintextSz = ciphertextSz - 4; |
| 193 | + plaintext = (byte*)malloc(plaintextSz); |
| 194 | + if (plaintext == NULL) { |
| 195 | + free(ciphertext); |
| 196 | + return -1; |
| 197 | + } |
| 198 | + |
| 199 | + /* Initialize AES */ |
| 200 | + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); |
| 201 | + if (ret != 0) { |
| 202 | + free(ciphertext); |
| 203 | + free(plaintext); |
| 204 | + return ret; |
| 205 | + } |
| 206 | + |
| 207 | + ret = wc_AesSetKeyDirect(&aes, key, keySz, NULL, AES_DECRYPTION); |
| 208 | + if (ret != 0) { |
| 209 | + wc_AesFree(&aes); |
| 210 | + free(ciphertext); |
| 211 | + free(plaintext); |
| 212 | + return ret; |
| 213 | + } |
| 214 | + |
| 215 | + /* Decrypt block by block using direct API */ |
| 216 | + for (i = 0; i < plaintextSz; i += AES_BLOCK_SIZE) { |
| 217 | + ret = wc_AesDecryptDirect(&aes, plaintext + i, ciphertext + 4 + i); |
| 218 | + if (ret != 0) { |
| 219 | + wc_AesFree(&aes); |
| 220 | + free(ciphertext); |
| 221 | + free(plaintext); |
| 222 | + return ret; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + wc_AesFree(&aes); |
| 227 | + |
| 228 | + /* Use original size to remove padding */ |
| 229 | + ret = write_file(outFile, plaintext, originalSz); |
| 230 | + |
| 231 | + free(ciphertext); |
| 232 | + free(plaintext); |
| 233 | + |
| 234 | + printf("AES-DIRECT decryption complete (one-shot, no streaming API " |
| 235 | + "available)\n"); |
| 236 | + return ret; |
| 237 | +} |
| 238 | + |
| 239 | +int main(int argc, char** argv) |
| 240 | +{ |
| 241 | + byte key[AES_KEY_SIZE]; |
| 242 | + int ret; |
| 243 | + |
| 244 | + if (argc != 3) { |
| 245 | + printf("Usage: %s <input file> <output file>\n", argv[0]); |
| 246 | + printf("Encrypts input file, then decrypts to output file\n"); |
| 247 | + printf("Note: DIRECT mode provides raw block cipher access\n"); |
| 248 | + return 1; |
| 249 | + } |
| 250 | + |
| 251 | + wolfCrypt_Init(); |
| 252 | + |
| 253 | + /* Use a fixed key for demonstration */ |
| 254 | + memset(key, 0x08, AES_KEY_SIZE); |
| 255 | + |
| 256 | + /* Encrypt to temporary file */ |
| 257 | + ret = encrypt_file(argv[1], "temp_encrypted.bin", key, AES_KEY_SIZE); |
| 258 | + if (ret != 0) { |
| 259 | + printf("Encryption failed: %d\n", ret); |
| 260 | + wolfCrypt_Cleanup(); |
| 261 | + return 1; |
| 262 | + } |
| 263 | + |
| 264 | + /* Decrypt to output file */ |
| 265 | + ret = decrypt_file("temp_encrypted.bin", argv[2], key, AES_KEY_SIZE); |
| 266 | + if (ret != 0) { |
| 267 | + printf("Decryption failed: %d\n", ret); |
| 268 | + wolfCrypt_Cleanup(); |
| 269 | + return 1; |
| 270 | + } |
| 271 | + |
| 272 | + /* Clean up temp file */ |
| 273 | + remove("temp_encrypted.bin"); |
| 274 | + |
| 275 | + printf("Success! Decrypted file written to %s\n", argv[2]); |
| 276 | + |
| 277 | + wolfCrypt_Cleanup(); |
| 278 | + return 0; |
| 279 | +} |
| 280 | + |
| 281 | +#else |
| 282 | + |
| 283 | +int main(int argc, char** argv) |
| 284 | +{ |
| 285 | + (void)argc; |
| 286 | + (void)argv; |
| 287 | + printf("AES-DIRECT not compiled in. Enable with WOLFSSL_AES_DIRECT\n"); |
| 288 | + return 0; |
| 289 | +} |
| 290 | + |
| 291 | +#endif |
0 commit comments