libDwmAuth-0.3.6
DwmAuthEd25519Keys.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwmAuth/tags/libDwmAuth-0.3.6/include/DwmAuthEd25519Keys.hh 11162 $
3 // @(#) $Id: DwmAuthEd25519Keys.hh 11162 2020-09-08 06:46:41Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2018, 2020
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 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
42 
43 #ifndef _DWMAUTHED25519KEYS_HH_
44 #define _DWMAUTHED25519KEYS_HH_
45 
46 #include <string>
47 #include <cryptopp/cryptlib.h>
48 #include <cryptopp/xed25519.h>
49 #include <cryptopp/base64.h>
50 #include <cryptopp/filters.h>
51 #include <cryptopp/osrng.h>
52 
53 #include "DwmDescriptorIOCapable.hh"
54 #include "DwmFileIOCapable.hh"
55 #include "DwmStreamIOCapable.hh"
56 #include "DwmStreamedLengthCapable.hh"
57 
58 namespace Dwm {
59 
60  namespace Auth {
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  class Ed25519
66  {
67  public:
68  //----------------------------------------------------------------------
70  //----------------------------------------------------------------------
71  class PublicKey
72  : public CryptoPP::ed25519PublicKey,
73  public Dwm::DescriptorIOCapable, public Dwm::FileIOCapable,
74  public Dwm::StreamIOCapable, public Dwm::StreamedLengthCapable
75  {
76  public:
77  PublicKey()
78  : CryptoPP::ed25519PublicKey()
79  {}
80 
81  PublicKey(const CryptoPP::ed25519PublicKey & key)
82  : CryptoPP::ed25519PublicKey(key)
83  {}
84 
85  bool IsValid() const;
86 
87  std::string ToString() const;
88  std::string ToBase64String() const;
89  bool FromString(const std::string & s);
90  bool FromBase64String(const std::string & s);
91 
92  std::istream & Read(std::istream & is) override;
93  std::ostream & Write(std::ostream & os) const override;
94  size_t Read(FILE *f) override;
95  size_t Write(FILE *f) const override;
96  ssize_t Read(int fd) override;
97  ssize_t Write(int fd) const override;
98  uint32_t StreamedLength() const override;
99 
100  //--------------------------------------------------------------------
102  //--------------------------------------------------------------------
103  bool operator == (const PublicKey & pk) const;
104  };
105 
106  //----------------------------------------------------------------------
108  //----------------------------------------------------------------------
110  : public CryptoPP::ed25519PrivateKey,
111  public Dwm::DescriptorIOCapable, public Dwm::FileIOCapable,
112  public Dwm::StreamIOCapable, public Dwm::StreamedLengthCapable
113  {
114  public:
115  PrivateKey()
116  : CryptoPP::ed25519PrivateKey()
117  {}
118 
119  PrivateKey(const CryptoPP::ed25519PrivateKey & key)
120  : CryptoPP::ed25519PrivateKey(key)
121  {}
122 
123  bool IsValid() const;
124 
125  std::string ToString() const;
126  std::string ToBase64String() const;
127  bool FromString(const std::string & s);
128  bool FromBase64String(const std::string & s);
129 
130  std::istream & Read(std::istream & is) override;
131  std::ostream & Write(std::ostream & os) const override;
132  size_t Read(FILE *f) override;
133  size_t Write(FILE *f) const override;
134  ssize_t Read(int fd) override;
135  ssize_t Write(int fd) const override;
136  uint32_t StreamedLength() const override;
137 
138  //--------------------------------------------------------------------
140  //--------------------------------------------------------------------
141  bool operator == (const PrivateKey & pk) const;
142  };
143 
144  //----------------------------------------------------------------------
146  //----------------------------------------------------------------------
147  class KeyPair
148  : public std::pair<PrivateKey,PublicKey>
149  {
150  public:
151  //--------------------------------------------------------------------
153  //--------------------------------------------------------------------
155  : std::pair<PrivateKey,PublicKey>()
156  {}
157 
158  //--------------------------------------------------------------------
160  //--------------------------------------------------------------------
161  KeyPair(const PrivateKey & privKey, const PublicKey & publicKey)
162  : std::pair<PrivateKey,PublicKey>(privKey,publicKey)
163  {}
164 
165  //--------------------------------------------------------------------
167  //--------------------------------------------------------------------
168  KeyPair & operator = (const KeyPair & keys)
169  {
170  if (this != &keys) {
171  this->first = keys.first;
172  this->second = keys.second;
173  }
174  return *this;
175  }
176 
177  //--------------------------------------------------------------------
179  //--------------------------------------------------------------------
180  const PrivateKey & Private() const
181  {
182  return first;
183  }
184 
185  //--------------------------------------------------------------------
187  //--------------------------------------------------------------------
189  {
190  return first;
191  }
192 
193  //--------------------------------------------------------------------
195  //--------------------------------------------------------------------
196  const PublicKey & Public() const
197  {
198  return second;
199  }
200 
201  //--------------------------------------------------------------------
203  //--------------------------------------------------------------------
204  PublicKey & Public()
205  {
206  return second;
207  }
208 
209  bool Load(const std::string & privKeyPath, std::string & name);
210  };
211 
212  //----------------------------------------------------------------------
215  //----------------------------------------------------------------------
216  static KeyPair CreateKeyPair();
217  };
218 
219  } // namespace Auth
220 
221 } // namespace Dwm
222 
223 #endif // _DWMAUTHED25519KEYS_HH_
Encapsulates a readable and writable private key.
Definition: DwmAuthEd25519Keys.hh:109
const PublicKey & Public() const
Returns a const reference to the public key.
Definition: DwmAuthEd25519Keys.hh:196
Encapsulates a private/public key pair.
Definition: DwmAuthEd25519Keys.hh:147
PrivateKey & Private()
Returns a mutable reference to the private key.
Definition: DwmAuthEd25519Keys.hh:188
KeyPair()
Default constructor.
Definition: DwmAuthEd25519Keys.hh:154
const PrivateKey & Private() const
Returns a const reference to the private key.
Definition: DwmAuthEd25519Keys.hh:180
KeyPair & operator=(const KeyPair &keys)
operator =
Definition: DwmAuthEd25519Keys.hh:168
KeyPair(const PrivateKey &privKey, const PublicKey &publicKey)
Construct from a private and public key.
Definition: DwmAuthEd25519Keys.hh:161
PublicKey & Public()
Returns a mutable reference to the public key.
Definition: DwmAuthEd25519Keys.hh:204
bool operator==(const PrivateKey &pk) const
operator ==