|
| 1 | +/* aesgcm-file-minimal.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2026 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 | +#ifndef WOLFSSL_USER_SETTINGS |
| 23 | + #include <wolfssl/options.h> |
| 24 | +#endif |
| 25 | +#include <wolfssl/wolfcrypt/aes.h> |
| 26 | +#include <wolfssl/wolfcrypt/random.h> |
| 27 | + |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#define KEY_SZ AES_256_KEY_SIZE |
| 33 | +#define NONCE_SZ GCM_NONCE_MID_SZ |
| 34 | +#define TAG_SZ AES_BLOCK_SIZE |
| 35 | + |
| 36 | +static int Encrypt(const byte* key, const byte* iv, const byte* in, |
| 37 | + word32 inSz, byte* out, byte* tag) |
| 38 | +{ |
| 39 | + Aes aes; |
| 40 | + int ret; |
| 41 | + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); |
| 42 | + if (ret == 0) { |
| 43 | + ret = wc_AesGcmSetKey(&aes, key, KEY_SZ); |
| 44 | + } |
| 45 | + if (ret == 0) { |
| 46 | + ret = wc_AesGcmEncrypt(&aes, out, in, inSz, iv, NONCE_SZ, |
| 47 | + tag, TAG_SZ, NULL, 0); |
| 48 | + } |
| 49 | + wc_AesFree(&aes); |
| 50 | + return ret; |
| 51 | +} |
| 52 | + |
| 53 | +static int Decrypt(const byte* key, const byte* iv, const byte* in, |
| 54 | + word32 inSz, byte* out, const byte* tag) |
| 55 | +{ |
| 56 | + Aes aes; |
| 57 | + int ret; |
| 58 | + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); |
| 59 | + if (ret == 0) { |
| 60 | + ret = wc_AesGcmSetKey(&aes, key, KEY_SZ); |
| 61 | + } |
| 62 | + if (ret == 0) { |
| 63 | + ret = wc_AesGcmDecrypt(&aes, out, in, inSz, iv, NONCE_SZ, |
| 64 | + tag, TAG_SZ, NULL, 0); |
| 65 | + } |
| 66 | + wc_AesFree(&aes); |
| 67 | + return ret; |
| 68 | +} |
| 69 | + |
| 70 | +static int ReadFile(const char* path, byte** data, word32* sz) |
| 71 | +{ |
| 72 | + FILE* file; |
| 73 | + long fileSz; |
| 74 | + byte* buf; |
| 75 | + |
| 76 | + *data = NULL; |
| 77 | + *sz = 0; |
| 78 | + file = fopen(path, "rb"); |
| 79 | + if (file == NULL) { |
| 80 | + return -1; |
| 81 | + } |
| 82 | + if (fseek(file, 0, SEEK_END) != 0) { |
| 83 | + fclose(file); |
| 84 | + return -1; |
| 85 | + } |
| 86 | + fileSz = ftell(file); |
| 87 | + if (fileSz < 0) { |
| 88 | + fclose(file); |
| 89 | + return -1; |
| 90 | + } |
| 91 | + if (fseek(file, 0, SEEK_SET) != 0) { |
| 92 | + fclose(file); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + buf = (byte*)malloc((size_t)fileSz); |
| 96 | + if (buf == NULL && fileSz != 0) { |
| 97 | + fclose(file); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + if (fileSz != 0 && fread(buf, 1, (size_t)fileSz, file) != (size_t)fileSz) { |
| 101 | + free(buf); |
| 102 | + fclose(file); |
| 103 | + return -1; |
| 104 | + } |
| 105 | + fclose(file); |
| 106 | + *data = buf; |
| 107 | + *sz = fileSz; |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +static int WriteFile(const char* path, const byte* data, long dataSz) |
| 112 | +{ |
| 113 | + FILE* file = fopen(path, "wb"); |
| 114 | + if (file == NULL) { |
| 115 | + return -1; |
| 116 | + } |
| 117 | + if (dataSz != 0 && |
| 118 | + fwrite(data, 1, (size_t)dataSz, file) != (size_t)dataSz) { |
| 119 | + fclose(file); |
| 120 | + return -1; |
| 121 | + } |
| 122 | + fclose(file); |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +static void print_hex(const char* label, const byte* data, word32 sz) |
| 127 | +{ |
| 128 | + word32 i; |
| 129 | + printf("%s: ", label); |
| 130 | + for (i = 0; i < sz; i++) { |
| 131 | + printf("%02x", data[i]); |
| 132 | + } |
| 133 | + printf("\n"); |
| 134 | +} |
| 135 | + |
| 136 | +int main(int argc, char** argv) |
| 137 | +{ |
| 138 | + byte key[KEY_SZ], iv[NONCE_SZ], tag[TAG_SZ]; |
| 139 | + byte* plaintext = NULL; |
| 140 | + byte* ciphertext = NULL; |
| 141 | + byte* decrypted = NULL; |
| 142 | + word32 plaintextSz = 0; |
| 143 | + WC_RNG rng; |
| 144 | + int ret; |
| 145 | + |
| 146 | + if (argc != 4) { |
| 147 | + printf("Usage: %s <input-file> <encrypted-file> <decrypted-file>\n", |
| 148 | + argv[0]); |
| 149 | + return 1; |
| 150 | + } |
| 151 | + |
| 152 | + if (ReadFile(argv[1], &plaintext, &plaintextSz) != 0) { |
| 153 | + printf("Failed to read: %s\n", argv[1]); |
| 154 | + return 1; |
| 155 | + } |
| 156 | + |
| 157 | + ciphertext = (byte*)malloc((size_t)plaintextSz); |
| 158 | + decrypted = (byte*)malloc((size_t)plaintextSz); |
| 159 | + if ((ciphertext == NULL || decrypted == NULL) && plaintextSz != 0) { |
| 160 | + printf("alloc failed\n"); |
| 161 | + ret = 1; |
| 162 | + goto exit; |
| 163 | + } |
| 164 | + |
| 165 | + ret = wc_InitRng(&rng); |
| 166 | + if (ret == 0) { |
| 167 | + ret = wc_RNG_GenerateBlock(&rng, key, KEY_SZ); |
| 168 | + if (ret == 0) { |
| 169 | + ret = wc_RNG_GenerateBlock(&rng, iv, NONCE_SZ); |
| 170 | + } |
| 171 | + wc_FreeRng(&rng); |
| 172 | + } |
| 173 | + if (ret != 0) { |
| 174 | + printf("Key/IV generation failed: %d\n", ret); |
| 175 | + goto exit; |
| 176 | + } |
| 177 | + |
| 178 | + ret = Encrypt(key, iv, plaintext, plaintextSz, ciphertext, tag); |
| 179 | + if (ret != 0) { |
| 180 | + printf("Encryption failed: %d\n", ret); |
| 181 | + goto exit; |
| 182 | + } |
| 183 | + |
| 184 | + if (WriteFile(argv[2], ciphertext, plaintextSz) != 0) { |
| 185 | + printf("Failed to write: %s\n", argv[2]); |
| 186 | + ret = 1; |
| 187 | + goto exit; |
| 188 | + } |
| 189 | + |
| 190 | + ret = Decrypt(key, iv, ciphertext, plaintextSz, decrypted, tag); |
| 191 | + if (ret != 0) { |
| 192 | + printf("Decryption failed: %d\n", ret); |
| 193 | + goto exit; |
| 194 | + } |
| 195 | + |
| 196 | + if (WriteFile(argv[3], decrypted, plaintextSz) != 0) { |
| 197 | + printf("Failed to write: %s\n", argv[3]); |
| 198 | + ret = 1; |
| 199 | + goto exit; |
| 200 | + } |
| 201 | + |
| 202 | + print_hex("Key", key, KEY_SZ); |
| 203 | + print_hex("IV", iv, NONCE_SZ); |
| 204 | + print_hex("Tag", tag, TAG_SZ); |
| 205 | + printf("Encrypted %u bytes to %s\n", plaintextSz, argv[2]); |
| 206 | + printf("Decrypted %u bytes to %s\n", plaintextSz, argv[3]); |
| 207 | + |
| 208 | + if (memcmp(plaintext, decrypted, (size_t)plaintextSz) == 0) { |
| 209 | + printf("Round-trip OK: decrypted output matches original input\n"); |
| 210 | + } |
| 211 | + else { |
| 212 | + printf("Round-trip FAILED: mismatch\n"); |
| 213 | + ret = 1; |
| 214 | + } |
| 215 | + |
| 216 | +exit: |
| 217 | + free(decrypted); |
| 218 | + free(ciphertext); |
| 219 | + free(plaintext); |
| 220 | + return ret; |
| 221 | +} |
0 commit comments