xref: /aosp_15_r20/external/boringssl/src/util/fipstools/acvp/modulewrapper/modulewrapper.h (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 <openssl/base.h>
16 
17 #include <functional>
18 #include <memory>
19 #include <vector>
20 
21 #include <openssl/span.h>
22 
23 
24 namespace bssl {
25 namespace acvp {
26 
27 // kMaxArgs is the maximum number of arguments (including the function name)
28 // that an ACVP request can contain.
29 constexpr size_t kMaxArgs = 9;
30 // kMaxNameLength is the maximum length of a function name in an ACVP request.
31 constexpr size_t kMaxNameLength = 30;
32 
33 // RequestBuffer holds various buffers needed for parsing an ACVP request. It
34 // can be reused between requests.
35 class RequestBuffer {
36  public:
37   virtual ~RequestBuffer();
38 
39   static std::unique_ptr<RequestBuffer> New();
40 };
41 
42 // ParseArgsFromFd returns a span of arguments, the first of which is the name
43 // of the requested function, from |fd|. The return values point into |buffer|
44 // and so must not be used after |buffer| has been freed or reused for a
45 // subsequent call. It returns an empty span on error, because std::optional
46 // is still too new.
47 Span<const Span<const uint8_t>> ParseArgsFromFd(int fd, RequestBuffer *buffer);
48 
49 // WriteReplyToFd writes a reply to the given file descriptor.
50 bool WriteReplyToFd(int fd, const std::vector<Span<const uint8_t>> &spans);
51 
52 // WriteReplyToBuffer writes a reply to an internal buffer that may be flushed
53 // with |FlushBuffer|.
54 bool WriteReplyToBuffer(const std::vector<Span<const uint8_t>> &spans);
55 
56 // FlushBuffer writes the buffer that |WriteReplyToBuffer| fills, to |fd|.
57 bool FlushBuffer(int fd);
58 
59 // ReplyCallback is the type of a callback that writes a reply to an ACVP
60 // request.
61 typedef std::function<bool(const std::vector<Span<const uint8_t>> &)>
62     ReplyCallback;
63 
64 // Handler is the type of a function that handles a specific ACVP request. If
65 // successful it will call |write_reply| with the response arguments and return
66 // |write_reply|'s return value. Otherwise it will return false. The given args
67 // must not include the name at the beginning.
68 typedef bool (*Handler)(const Span<const uint8_t> args[],
69                         ReplyCallback write_reply);
70 
71 // FindHandler returns a |Handler| that can process the given arguments, or logs
72 // a reason and returns |nullptr| if none is found.
73 Handler FindHandler(Span<const Span<const uint8_t>> args);
74 
75 }  // namespace acvp
76 }  // namespace bssl
77