libDwmAuth-0.3.6
DwmAuthSymCryptoMessage.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwmAuth/tags/libDwmAuth-0.3.6/include/DwmAuthSymCryptoMessage.hh 11162 $
3 // @(#) $Id: DwmAuthSymCryptoMessage.hh 11162 2020-09-08 06:46:41Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMAUTHSYMCRYPTOMESSAGE_HH_
43 #define _DWMAUTHSYMCRYPTOMESSAGE_HH_
44 
45 #include <string>
46 
47 #include <cryptopp/aes.h>
48 
49 #include "DwmSocket.hh"
50 #include "DwmDescriptorIOCapable.hh"
51 #include "DwmFileIOCapable.hh"
52 #include "DwmStreamIOCapable.hh"
53 #include "DwmStreamedLengthCapable.hh"
54 
55 namespace Dwm {
56 
57  namespace Auth {
58 
59  namespace SymCrypto {
60 
61  //----------------------------------------------------------------------
63  //----------------------------------------------------------------------
64  class Message
65  : public DescriptorIOCapable, public FileIOCapable,
66  public StreamIOCapable, public StreamedLengthCapable
67  {
68  public:
69  //--------------------------------------------------------------------
71  //--------------------------------------------------------------------
72  Message(const std::string & secret, const std::string & value = "");
73 
74  //--------------------------------------------------------------------
76  //--------------------------------------------------------------------
77  ~Message();
78 
79  //--------------------------------------------------------------------
81  //--------------------------------------------------------------------
82  std::string Value() const;
83 
84  //--------------------------------------------------------------------
86  //--------------------------------------------------------------------
87  const std::string & Value(const std::string & value);
88 
89  //--------------------------------------------------------------------
91  //--------------------------------------------------------------------
92  std::istream & Read(std::istream & is) override;
93 
94  //--------------------------------------------------------------------
96  //--------------------------------------------------------------------
97  std::ostream & Write(std::ostream & os) const override;
98 
99  //--------------------------------------------------------------------
102  //--------------------------------------------------------------------
103  ssize_t Read(int fd) override;
104 
105  //--------------------------------------------------------------------
108  //--------------------------------------------------------------------
109  ssize_t Write(int fd) const override;
110 
111  //--------------------------------------------------------------------
114  //--------------------------------------------------------------------
115  size_t Read(FILE *f) override;
116 
117  //--------------------------------------------------------------------
120  //--------------------------------------------------------------------
121  size_t Write(FILE *f) const override;
122 
123  //--------------------------------------------------------------------
126  //--------------------------------------------------------------------
127  uint32_t StreamedLength() const override;
128 
129  ssize_t SendTo(Socket & s, int flags,
130  const Ipv4Address & dstAddr, uint16_t dstPort);
131  ssize_t RecvFrom(Socket & s, int flags,
132  Ipv4Address & srcAddr, uint16_t & srcPort);
133 
134  private:
135  std::string _secret;
136  uint8_t _iv[CryptoPP::AES::BLOCKSIZE];
137  std::string _msg;
138 
139  ssize_t ReadMsgValue(int fd);
140  ssize_t WriteMsgValue(int fd) const;
141  std::istream & ReadMsgValue(std::istream & is);
142  std::ostream & WriteMsgValue(std::ostream & os) const;
143  size_t ReadMsgValue(FILE *f);
144  size_t WriteMsgValue(FILE *f) const;
145  };
146 
147  } // namespace SymCrypto
148 
149  } // namespace Auth
150 
151 } // namespace Dwm
152 
153 #endif // _DWMAUTHSYMCRYPTOMESSAGE_HH_
uint32_t StreamedLength() const override
Returns the number of bytes that would be written if one of the Write() members were called.
Message(const std::string &secret, const std::string &value="")
Constructor with the given secret and value.
std::string Value() const
Returns the decrypted value.
std::ostream & Write(std::ostream &os) const override
Writes the message to an ostream. Returns the ostream.
Encapsulates an encrypted message.
Definition: DwmAuthSymCryptoMessage.hh:64
std::istream & Read(std::istream &is) override
Reads the message from an istream. Returns the istream.