summaryrefslogtreecommitdiff
path: root/src/util/fipstools/cavp/cavp_keywrap_test.cc
blob: a4abccd4f5025acaf7b72fb577bfd68f0579e4fe (plain)
1
2
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
/* Copyright (c) 2017, Google Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

// cavp_keywrap_test processes a NIST CAVP AES test vector request file and
// emits the corresponding response.

#include <stdlib.h>

#include <openssl/aes.h>
#include <openssl/crypto.h>

#include "../crypto/test/file_test.h"
#include "cavp_test_util.h"


namespace {

struct TestCtx {
  bool encrypt;
  bool padding;
};

}  // namespace

static bool AESKeyWrap(std::vector<uint8_t> *out, bool encrypt,
                       const std::vector<uint8_t> &key,
                       const std::vector<uint8_t> &in) {
  size_t key_bits = key.size() * 8;
  if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
    return false;
  }
  AES_KEY aes_key;

  if (encrypt) {
    out->resize(in.size() + 8);
    if (AES_set_encrypt_key(key.data(), key_bits, &aes_key) ||
        AES_wrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
            -1) {
      return false;
    }
  } else {
    out->resize(in.size() - 8);
    if (AES_set_decrypt_key(key.data(), key_bits, &aes_key) ||
        AES_unwrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
            -1) {
      return false;
    }
  }

  return true;
}

static bool AESKeyWrapWithPadding(std::vector<uint8_t> *out, bool encrypt,
                                  const std::vector<uint8_t> &key,
                                  const std::vector<uint8_t> &in) {
  const size_t key_bits = key.size() * 8;
  if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
    return false;
  }
  AES_KEY aes_key;

  size_t out_len;
  if (encrypt) {
    out->resize(in.size() + 15);
    if (AES_set_encrypt_key(key.data(), key_bits, &aes_key) ||
        !AES_wrap_key_padded(&aes_key, out->data(), &out_len, out->size(),
                             in.data(), in.size())) {
      return false;
    }
  } else {
    out->resize(in.size());
    if (AES_set_decrypt_key(key.data(), key_bits, &aes_key) ||
        !AES_unwrap_key_padded(&aes_key, out->data(), &out_len, out->size(),
                               in.data(), in.size())) {
      return false;
    }
  }

  out->resize(out_len);
  return true;
}

static bool TestCipher(FileTest *t, void *arg) {
  TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);

  std::string count, unused, in_label = ctx->encrypt ? "P" : "C",
                             result_label = ctx->encrypt ? "C" : "P";
  std::vector<uint8_t> key, in, result;
  // clang-format off
  if (!t->GetInstruction(&unused, "PLAINTEXT LENGTH") ||
      !t->GetAttribute(&count, "COUNT") ||
      !t->GetBytes(&key, "K") ||
      !t->GetBytes(&in, in_label)) {
    return false;
  }
  // clang-format on

  auto wrap_function = AESKeyWrap;
  if (ctx->padding) {
    wrap_function = AESKeyWrapWithPadding;
  }

  printf("%s", t->CurrentTestToString().c_str());
  if (!wrap_function(&result, ctx->encrypt, key, in)) {
    if (ctx->encrypt) {
      return false;
    } else {
      printf("FAIL\r\n\r\n");
    }
  } else {
    printf("%s = %s\r\n\r\n", result_label.c_str(),
           EncodeHex(result.data(), result.size()).c_str());
  }

  return true;
}

static int usage(char *arg) {
  fprintf(
      stderr,
      "usage: %s (enc|dec|enc-pad|dec-pad) (128|192|256) <test file>\n",
      arg);
  return 1;
}

int cavp_keywrap_test_main(int argc, char **argv) {
  if (argc != 4) {
    return usage(argv[0]);
  }

  const std::string op(argv[1]);
  bool encrypt = false;
  bool padding = false;
  if (op == "enc") {
    encrypt = true;
  } else if (op == "dec") {
  } else if (op == "enc-pad") {
    encrypt = true;
    padding = true;
  } else if (op == "dec-pad") {
    padding = true;
  } else {
    return usage(argv[0]);
  }

  TestCtx ctx = {encrypt, padding};

  FileTest::Options opts;
  opts.path = argv[3];
  opts.callback = TestCipher;
  opts.arg = &ctx;
  opts.silent = true;
  opts.comment_callback = EchoComment;
  return FileTestMain(opts);
}