xref: /aosp_15_r20/external/boringssl/src/util/fipstools/acvp/modulewrapper/main.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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 
EqString(bssl::Span<const uint8_t> cmd,const char * str)26 static bool EqString(bssl::Span<const uint8_t> cmd, const char *str) {
27   return cmd.size() == strlen(str) && memcmp(str, cmd.data(), cmd.size()) == 0;
28 }
29 
main(int argc,char ** argv)30 int main(int argc, char **argv) {
31   if (argc == 2 && strcmp(argv[1], "--version") == 0) {
32     printf("Built for architecture: ");
33 
34 #if defined(OPENSSL_X86_64)
35     puts("x86-64 (64-bit)");
36 #elif defined(OPENSSL_ARM)
37     puts("ARM (32-bit)");
38 #elif defined(OPENSSL_AARCH64)
39     puts("aarch64 (64-bit)");
40 #else
41 #error "FIPS build not supported on this architecture"
42 #endif
43 
44     printf("Hardware acceleration enabled: %s\n",
45            CRYPTO_has_asm() ? "yes" : "no");
46 
47     return 0;
48   } else if (argc != 1) {
49     fprintf(stderr, "Usage: %s [--version]\n", argv[0]);
50     return 4;
51   }
52 
53   // modulewrapper buffers responses to the greatest degree allowed in order to
54   // fully exercise the async handling in acvptool.
55   std::unique_ptr<bssl::acvp::RequestBuffer> buffer =
56       bssl::acvp::RequestBuffer::New();
57   const bssl::acvp::ReplyCallback write_reply = std::bind(
58       bssl::acvp::WriteReplyToFd, STDOUT_FILENO, std::placeholders::_1);
59   const bssl::acvp::ReplyCallback buffer_reply =
60       std::bind(bssl::acvp::WriteReplyToBuffer, std::placeholders::_1);
61 
62   for (;;) {
63     const bssl::Span<const bssl::Span<const uint8_t>> args =
64         ParseArgsFromFd(STDIN_FILENO, buffer.get());
65     if (args.empty()) {
66       return 1;
67     }
68 
69     if (EqString(args[0], "flush")) {
70       if (!bssl::acvp::FlushBuffer(STDOUT_FILENO)) {
71         abort();
72       }
73       continue;
74     }
75 
76     const bssl::acvp::Handler handler = bssl::acvp::FindHandler(args);
77     if (!handler) {
78       return 2;
79     }
80 
81     auto &reply_callback =
82         EqString(args[0], "getConfig") ? write_reply : buffer_reply;
83     if (!handler(args.subspan(1).data(), reply_callback)) {
84       const std::string name(reinterpret_cast<const char *>(args[0].data()),
85                              args[0].size());
86       fprintf(stderr, "\'%s\' operation failed.\n", name.c_str());
87       return 3;
88     }
89   }
90 };
91