• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 The BoringSSL Authors
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 <inttypes.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <string>
20 
21 #include <openssl/crypto.h>
22 #include <openssl/span.h>
23 
24 #include "modulewrapper.h"
25 
26 
EqString(bssl::Span<const uint8_t> cmd,const char * str)27 static bool EqString(bssl::Span<const uint8_t> cmd, const char *str) {
28   return cmd.size() == strlen(str) && memcmp(str, cmd.data(), cmd.size()) == 0;
29 }
30 
main(int argc,char ** argv)31 int main(int argc, char **argv) {
32   if (argc == 2 && strcmp(argv[1], "--version") == 0) {
33     printf("Built for architecture: ");
34 
35 #if defined(OPENSSL_X86_64)
36     puts("x86-64 (64-bit)");
37 #elif defined(OPENSSL_ARM)
38     puts("ARM (32-bit)");
39 #elif defined(OPENSSL_AARCH64)
40     puts("aarch64 (64-bit)");
41 #else
42 #error "FIPS build not supported on this architecture"
43 #endif
44 
45     if (!FIPS_mode()) {
46       printf("Module not in FIPS mode\n");
47       abort();
48     }
49     printf("Module is in FIPS mode\n");
50 
51     const uint32_t module_version = FIPS_version();
52     if (module_version == 0) {
53       printf("No module version set\n");
54       abort();
55     }
56     printf("Module: '%s', version: %" PRIu32 " hash:\n", FIPS_module_name(),
57            module_version);
58 
59 #if !defined(BORINGSSL_FIPS)
60     // |module_version| will be zero, so the non-FIPS build will never get
61     // this far.
62     printf("Non zero module version in non-FIPS build - should not happen!\n");
63     abort();
64 #elif defined(OPENSSL_ASAN)
65     printf("(not available when compiled for ASAN)");
66 #else
67     const uint8_t *module_hash = FIPS_module_hash();
68     for (size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
69       printf("%02x", module_hash[i]);
70     }
71     printf("\n");
72 #endif
73     printf("Hardware acceleration enabled: %s\n",
74            CRYPTO_has_asm() ? "yes" : "no");
75 
76     return 0;
77   } else if (argc != 1) {
78     fprintf(stderr, "Usage: %s [--version]\n", argv[0]);
79     return 4;
80   }
81 
82   // modulewrapper buffers responses to the greatest degree allowed in order to
83   // fully exercise the async handling in acvptool.
84   std::unique_ptr<bssl::acvp::RequestBuffer> buffer =
85       bssl::acvp::RequestBuffer::New();
86   const bssl::acvp::ReplyCallback write_reply = std::bind(
87       bssl::acvp::WriteReplyToFd, STDOUT_FILENO, std::placeholders::_1);
88   const bssl::acvp::ReplyCallback buffer_reply =
89       std::bind(bssl::acvp::WriteReplyToBuffer, std::placeholders::_1);
90 
91   for (;;) {
92     const bssl::Span<const bssl::Span<const uint8_t>> args =
93         ParseArgsFromFd(STDIN_FILENO, buffer.get());
94     if (args.empty()) {
95       return 1;
96     }
97 
98     if (EqString(args[0], "flush")) {
99       if (!bssl::acvp::FlushBuffer(STDOUT_FILENO)) {
100         abort();
101       }
102       continue;
103     }
104 
105     const bssl::acvp::Handler handler = bssl::acvp::FindHandler(args);
106     if (!handler) {
107       return 2;
108     }
109 
110     auto &reply_callback =
111         EqString(args[0], "getConfig") ? write_reply : buffer_reply;
112     if (!handler(args.subspan(1).data(), reply_callback)) {
113       const std::string name(reinterpret_cast<const char *>(args[0].data()),
114                              args[0].size());
115       fprintf(stderr, "\'%s\' operation failed.\n", name.c_str());
116       return 3;
117     }
118   }
119 }
120