1 /*
2  * Copyright (c) 2010 SURFnet bv
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*****************************************************************************
28  RSAPrivateKey.cpp
29 
30  RSA private key class
31  *****************************************************************************/
32 
33 #include "config.h"
34 #include "log.h"
35 #include "RSAPrivateKey.h"
36 #include <string.h>
37 
38 // Set the type
39 /*static*/ const char* RSAPrivateKey::type = "Abstract RSA private key";
40 
41 // Check if the key is of the given type
isOfType(const char * inType)42 bool RSAPrivateKey::isOfType(const char* inType)
43 {
44 	return !strcmp(type, inType);
45 }
46 
47 // Get the bit length
getBitLength() const48 unsigned long RSAPrivateKey::getBitLength() const
49 {
50 	return getN().bits();
51 }
52 
53 // Get the output length
getOutputLength() const54 unsigned long RSAPrivateKey::getOutputLength() const
55 {
56 	// Also handle odd number of bits (bits % 8 != 0)
57 	return (getBitLength() + 7) / 8;
58 }
59 
60 // Setters for the RSA private key components
setP(const ByteString & inP)61 void RSAPrivateKey::setP(const ByteString& inP)
62 {
63 	p = inP;
64 }
65 
setQ(const ByteString & inQ)66 void RSAPrivateKey::setQ(const ByteString& inQ)
67 {
68 	q = inQ;
69 }
70 
setPQ(const ByteString & inPQ)71 void RSAPrivateKey::setPQ(const ByteString& inPQ)
72 {
73 	pq = inPQ;
74 }
75 
setDP1(const ByteString & inDP1)76 void RSAPrivateKey::setDP1(const ByteString& inDP1)
77 {
78 	dp1 = inDP1;
79 }
80 
setDQ1(const ByteString & inDQ1)81 void RSAPrivateKey::setDQ1(const ByteString& inDQ1)
82 {
83 	dq1 = inDQ1;
84 }
85 
setD(const ByteString & inD)86 void RSAPrivateKey::setD(const ByteString& inD)
87 {
88 	d = inD;
89 }
90 
91 // Setters for the RSA public key components
setN(const ByteString & inN)92 void RSAPrivateKey::setN(const ByteString& inN)
93 {
94 	n = inN;
95 }
96 
setE(const ByteString & inE)97 void RSAPrivateKey::setE(const ByteString& inE)
98 {
99 	e = inE;
100 }
101 
102 // Getters for the RSA private key components
getP() const103 const ByteString& RSAPrivateKey::getP() const
104 {
105 	return p;
106 }
107 
getQ() const108 const ByteString& RSAPrivateKey::getQ() const
109 {
110 	return q;
111 }
112 
getPQ() const113 const ByteString& RSAPrivateKey::getPQ() const
114 {
115 	return pq;
116 }
117 
getDP1() const118 const ByteString& RSAPrivateKey::getDP1() const
119 {
120 	return dp1;
121 }
122 
getDQ1() const123 const ByteString& RSAPrivateKey::getDQ1() const
124 {
125 	return dq1;
126 }
127 
getD() const128 const ByteString& RSAPrivateKey::getD() const
129 {
130 	return d;
131 }
132 
133 // Getters for the RSA public key components
getN() const134 const ByteString& RSAPrivateKey::getN() const
135 {
136 	return n;
137 }
138 
getE() const139 const ByteString& RSAPrivateKey::getE() const
140 {
141 	return e;
142 }
143 
144 // Serialisation
serialise() const145 ByteString RSAPrivateKey::serialise() const
146 {
147 	return p.serialise() +
148 	       q.serialise() +
149 	       pq.serialise() +
150 	       dp1.serialise() +
151 	       dq1.serialise() +
152 	       d.serialise() +
153 	       n.serialise() +
154 	       e.serialise();
155 }
156 
deserialise(ByteString & serialised)157 bool RSAPrivateKey::deserialise(ByteString& serialised)
158 {
159 	ByteString dP = ByteString::chainDeserialise(serialised);
160 	ByteString dQ = ByteString::chainDeserialise(serialised);
161 	ByteString dPQ = ByteString::chainDeserialise(serialised);
162 	ByteString dDP1 = ByteString::chainDeserialise(serialised);
163 	ByteString dDQ1 = ByteString::chainDeserialise(serialised);
164 	ByteString dD = ByteString::chainDeserialise(serialised);
165 	ByteString dN = ByteString::chainDeserialise(serialised);
166 	ByteString dE = ByteString::chainDeserialise(serialised);
167 
168 	if ((dD.size() == 0) ||
169 	    (dN.size() == 0) ||
170 	    (dE.size() == 0))
171 	{
172 		return false;
173 	}
174 
175 	setP(dP);
176 	setQ(dQ);
177 	setPQ(dPQ);
178 	setDP1(dDP1);
179 	setDQ1(dDQ1);
180 	setD(dD);
181 	setN(dN);
182 	setE(dE);
183 
184 	return true;
185 }
186 
187