1 /* Copyright (c) 2021, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdio.h>
16 #include <string>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <openssl/crypto.h>
21 #include <openssl/span.h>
22
23 #include "modulewrapper.h"
24
25
main(int argc,char ** argv)26 int main(int argc, char **argv) {
27 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
28 printf("Built for architecture: ");
29
30 #if defined(OPENSSL_X86_64)
31 puts("x86-64 (64-bit)");
32 #elif defined(OPENSSL_ARM)
33 puts("ARM (32-bit)");
34 #elif defined(OPENSSL_AARCH64)
35 puts("aarch64 (64-bit)");
36 #elif defined(OPENSSL_PPC64LE)
37 puts("PPC64LE (64-bit)");
38 #else
39 #error "FIPS build not supported on this architecture"
40 #endif
41
42 printf("Hardware acceleration enabled: %s\n",
43 CRYPTO_has_asm() ? "yes" : "no");
44
45 return 0;
46 } else if (argc != 1) {
47 fprintf(stderr, "Usage: %s [--version]\n", argv[0]);
48 return 4;
49 }
50
51 std::unique_ptr<bssl::acvp::RequestBuffer> buffer =
52 bssl::acvp::RequestBuffer::New();
53 const bssl::acvp::ReplyCallback write_reply = std::bind(
54 bssl::acvp::WriteReplyToFd, STDOUT_FILENO, std::placeholders::_1);
55
56 for (;;) {
57 const bssl::Span<const bssl::Span<const uint8_t>> args =
58 ParseArgsFromFd(STDIN_FILENO, buffer.get());
59 if (args.empty()) {
60 return 1;
61 }
62
63 const bssl::acvp::Handler handler = bssl::acvp::FindHandler(args);
64 if (!handler) {
65 return 2;
66 }
67
68 if (!handler(args.subspan(1).data(), write_reply)) {
69 const std::string name(reinterpret_cast<const char *>(args[0].data()),
70 args[0].size());
71 fprintf(stderr, "\'%s\' operation failed.\n", name.c_str());
72 return 3;
73 }
74 }
75 };
76