| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 
 | /* Copyright Akamai Technologies, Inc.
 * SPDX-License-Identifier: Apache-2.0
 */
/** @file
 * Demo program for libaes-siv
 */
#define _POSIX_C_SOURCE 200112L
#define _ISOC99_SOURCE 1
#include "aes_siv.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
 * Load a file into memory.
 * @param[in] filename pathname of the file to load.
 * @param[out] the buf pointer will be set to a new malloc buffer which contains the file data.
 * @param[out] the len pointer will be set to the size of buf in bytes, which is the same as the file size in bytes.
 * @return 0 upon success, the file could be read and the buf,len output parameters have been set.
 * @return -1 on error; errno will be set.
 */
static int load_file(const char *filename, unsigned char **buf, size_t *len)
{
  FILE *f = NULL;
  unsigned char *buf_ = NULL;
  size_t len_;
  long ftell_len;
  int saved_errno;
  /* Get file size */
  f = fopen(filename, "rb");
  if (f == NULL) {
    goto fail;
  }
  if (fseek(f, 0, SEEK_END) < 0) {
    goto fail;
  }
  ftell_len = ftell(f);
  if(ftell_len < 0) {
    goto fail;
  }
  len_ = (size_t)ftell_len;
  if(fseek(f, 0, SEEK_SET) < 0) {
    goto fail;
  }
  /* allocate memory and read */
  buf_ = (unsigned char*) malloc(len_);
  if (buf_ == NULL) {
    goto fail;
  }
  if (fread(buf_, 1, len_, f) != len_) {
    assert(errno);
    goto fail;
  }
  fclose(f);
  f = NULL;
  *buf = buf_;
  *len = len_;
  return 0;
  fail:
  saved_errno = errno;
  if(f != NULL) {
    fclose(f);
  }
  free(buf_);
  errno = saved_errno;
  return -1;
}
void help(void)
{
  fprintf(stderr, "Usage: aes_siv_test [-d] <key file> <ad file> [nonce file]\n");
  fprintf(stderr, "This program encrypts or decrypts STDIN to STDOUT using the AES-SIV algorithm.\n");
  fprintf(stderr, "-d           Decrypt STDIN, by default STDIN is encrypted\n");
  fprintf(stderr, "<key file>   Filename which is read for key data, must have a size of 32, 48, 64 bytes.\n");
  fprintf(stderr, "<ad file>    Filename which is used for associate data. Can have any size.\n");
  fprintf(stderr, "[nonce file] Optional filename which is used for nonce data. Can have any size.\n");
}
int main(int argc, char const* argv[])
{
  int arg = 1;
  int decrypt_mode = 0;
  const char *key_file = NULL;
  const char *nonce_file = NULL;
  const char *ad_file = NULL;
  unsigned char *key = NULL;
  size_t key_len = 0;
  unsigned char *nonce = NULL;
  size_t nonce_len = 0;
  unsigned char *ad = NULL;
  size_t ad_len = 0;
  unsigned char *out = NULL;
  size_t out_len = 0;
  size_t plaintext_allocated = 1024;
  unsigned char *plaintext = malloc(plaintext_allocated);
  size_t plaintext_len = 0;
  AES_SIV_CTX *ctx = NULL;
  if(plaintext == NULL) {
    perror("malloc");
    goto fail;
  }
  /* Parse command line */
  arg = 1;
  if (arg < argc && strcmp(argv[arg], "-d") == 0)
  {
    decrypt_mode = 1;
    ++arg;
  }
  if(arg >= argc)
  {
    fprintf(stderr, "Missing key filename\n\n");
    help();
    goto fail;
  }
  key_file = argv[arg++];
  if(arg >= argc)
  {
    fprintf(stderr, "Missing associate data filename\n\n");
    help();
    goto fail;
  }
  ad_file = argv[arg++];
  if(arg < argc)
  {
    nonce_file = argv[arg++];
  }
  if(arg < argc)
  {
    fprintf(stderr, "Unknown command line argument: %s\n\n", argv[arg]);
    help();
    goto fail;
  }
  /* Load files */
  if(load_file(key_file, &key, &key_len) < 0)
  {
    fprintf(stderr, "Could not load key file %s : %s\n", key_file, strerror(errno));
    goto fail;
  }
  assert(key != NULL);
  if(! (key_len == 32 ||
        key_len == 48 ||
        key_len == 64))
  {
    fprintf(stderr, "Invalid key length %zu bytes, must be one of 32, 48, or 64\n", key_len);
    goto fail;
  }
  if(load_file(ad_file, &ad, &ad_len) < 0)
  {
    fprintf(stderr, "Could not load associated data file %s : %s\n", ad_file, strerror(errno));
    goto fail;
  }
  assert(ad);
  assert(ad_len > 0);
  if(load_file(nonce_file, &nonce, &nonce_len) < 0)
  {
    fprintf(stderr, "could not load nonce file %s : %s\n", nonce_file, strerror(errno));
    goto fail;
  }
  /* Read all of STDIN */
  while(!feof(stdin))
  {
    unsigned char buf[1024];
    unsigned char *plaintext_new;
    size_t r = fread(buf, 1, sizeof(buf), stdin);
    if(r > 0)
    {
      if(plaintext_len + r > plaintext_allocated)
      {
        plaintext_allocated *= 2;
        plaintext_new = realloc(plaintext, plaintext_allocated);
        if(plaintext_new == NULL)
        {
          perror("realloc");
          goto fail;
        }
        plaintext = plaintext_new;
      }
      assert(plaintext_len + r <= plaintext_allocated);
      memcpy(plaintext + plaintext_len, buf, r);
      plaintext_len += r;
    } else if(ferror(stdin)) {
      perror("fread");
      goto fail;
    }
  }
  /* allocate output buffer */
  out_len = plaintext_len + 16;
  out = (unsigned char*) malloc(out_len);
  if (out == NULL)
  {
    perror("malloc");
    goto fail;
  }
  /* do AES-SIV */
  ctx = AES_SIV_CTX_new();
  if (ctx == NULL)
  {
    perror("AES_SIV_CTX_new");
    goto fail;
  }
  if (decrypt_mode)
  {
    if (AES_SIV_Decrypt(ctx, out, &out_len, key, key_len, nonce, nonce_len, plaintext, plaintext_len, ad, ad_len) != 1)
    {
      fprintf(stderr, "Could not decrypt AES-SIV\n");
      goto fail;
    }
  }
  else
  {
    if (AES_SIV_Encrypt(ctx, out, &out_len, key, key_len, nonce, nonce_len, plaintext, plaintext_len, ad, ad_len) != 1)
    {
      fprintf(stderr, "Could not encrypt AES-SIV\n");
      goto fail;
    }
  }
  AES_SIV_CTX_free(ctx);
  /* write to stdout */
  if(fwrite(out, 1, out_len, stdout) < out_len) {
    perror("fwrite");
    goto fail;
  }
  free(plaintext);
  free(key);
  free(ad);
  free(nonce);
  free(out);
  return EXIT_SUCCESS;
 fail:
  free(plaintext);
  free(key);
  free(ad);
  free(nonce);
  free(out);
  return EXIT_FAILURE;
}
 |