libDwmAuth-0.1.2
DwmAuthSymCryptoMessage.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwmAuth/tags/libDwmAuth-0.1.2/include/DwmAuthSymCryptoMessage.hh 8630 $
3 // @(#) $Id: DwmAuthSymCryptoMessage.hh 8630 2016-06-02 15:46:17Z 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 "DwmReadable.hh"
50 #include "DwmSocket.hh"
51 #include "DwmWritable.hh"
52 
53 namespace Dwm {
54 
55  namespace Auth {
56 
57  namespace SymCrypto {
58 
59  //----------------------------------------------------------------------
61  //----------------------------------------------------------------------
62  class Message
63  : public Readable, public Writable
64  {
65  public:
66  //--------------------------------------------------------------------
68  //--------------------------------------------------------------------
69  Message(const std::string & secret, const std::string & value = "");
70 
71  //--------------------------------------------------------------------
73  //--------------------------------------------------------------------
74  ~Message();
75 
76  //--------------------------------------------------------------------
78  //--------------------------------------------------------------------
79  std::string Value() const;
80 
81  //--------------------------------------------------------------------
83  //--------------------------------------------------------------------
84  const std::string & Value(const std::string & value);
85 
86  //--------------------------------------------------------------------
88  //--------------------------------------------------------------------
89  std::istream & Read(std::istream & is);
90 
91  //--------------------------------------------------------------------
93  //--------------------------------------------------------------------
94  std::ostream & Write(std::ostream & os) const;
95 
96  //--------------------------------------------------------------------
99  //--------------------------------------------------------------------
100  ssize_t Read(int fd);
101 
102  //--------------------------------------------------------------------
105  //--------------------------------------------------------------------
106  ssize_t Write(int fd) const;
107 
108  //--------------------------------------------------------------------
111  //--------------------------------------------------------------------
112  size_t Read(FILE *f);
113 
114  //--------------------------------------------------------------------
117  //--------------------------------------------------------------------
118  size_t Write(FILE *f) const;
119 
120  //--------------------------------------------------------------------
123  //--------------------------------------------------------------------
124  uint32_t StreamedLength() const;
125 
126  ssize_t SendTo(Socket & s, int flags,
127  const Ipv4Address & dstAddr, uint16_t dstPort);
128  ssize_t RecvFrom(Socket & s, int flags,
129  Ipv4Address & srcAddr, uint16_t & srcPort);
130 
131  private:
132  std::string _secret;
133  byte _iv[CryptoPP::AES::BLOCKSIZE];
134  std::string _msg;
135 
136  ssize_t ReadMsgValue(int fd);
137  ssize_t WriteMsgValue(int fd) const;
138  std::istream & ReadMsgValue(std::istream & is);
139  std::ostream & WriteMsgValue(std::ostream & os) const;
140  size_t ReadMsgValue(FILE *f);
141  size_t WriteMsgValue(FILE *f) const;
142  };
143 
144  } // namespace SymCrypto
145 
146  } // namespace Auth
147 
148 } // namespace Dwm
149 
150 #endif // _DWMAUTHSYMCRYPTOMESSAGE_HH_
Message(const std::string &secret, const std::string &value="")
Constructor with the given secret and value.
std::string Value() const
Returns the decrypted value.
Encapsulates an encrypted message.
Definition: DwmAuthSymCryptoMessage.hh:62
uint32_t StreamedLength() const
Returns the number of bytes that would be written if one of the Write() members were called...
Definition: DwmAuth.hh:48
std::istream & Read(std::istream &is)
Reads the message from an istream. Returns the istream.
std::ostream & Write(std::ostream &os) const
Writes the message to an ostream. Returns the ostream.