/
XrdCryptoCipher.cc
211 lines (178 loc) · 7.2 KB
/
XrdCryptoCipher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/******************************************************************************/
/* */
/* X r d C r y p t o C i p h e r . c c */
/* */
/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Gerri Ganis for CERN */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
/* ************************************************************************** */
/* */
/* Generic interface to a cipher class */
/* Allows to plug-in modules based on different crypto implementation */
/* (OpenSSL, Botan, ...) */
/* */
/* ************************************************************************** */
#include <string.h>
#include "XrdCrypto/XrdCryptoAux.hh"
#include "XrdCrypto/XrdCryptoCipher.hh"
//_____________________________________________________________________________
bool XrdCryptoCipher::Finalize(bool, char *, int, const char *)
{
// Finalize key computation (key agreement)
ABSTRACTMETHOD("XrdCryptoCipher::Finalize");
return 0;
}
//_____________________________________________________________________________
bool XrdCryptoCipher::IsValid()
{
// Check key validity
ABSTRACTMETHOD("XrdCryptoCipher::IsValid");
return 0;
}
//____________________________________________________________________________
void XrdCryptoCipher::SetIV(int l, const char *iv)
{
// Set IV from l bytes at iv
ABSTRACTMETHOD("XrdCryptoCipher::SetIV");
}
//____________________________________________________________________________
char *XrdCryptoCipher::RefreshIV(int &l)
{
// Regenerate IV and return it
ABSTRACTMETHOD("XrdCryptoCipher::RefreshIV");
return 0;
}
//____________________________________________________________________________
char *XrdCryptoCipher::IV(int &l) const
{
// Get IV
ABSTRACTMETHOD("XrdCryptoCipher::IV");
return 0;
}
//____________________________________________________________________________
char *XrdCryptoCipher::Public(int &lpub)
{
// Getter for public part during key agreement
ABSTRACTMETHOD("XrdCryptoCipher::Public");
return 0;
}
//_____________________________________________________________________________
XrdSutBucket *XrdCryptoCipher::AsBucket()
{
// Return pointer to a bucket created using the internal information
// serialized
ABSTRACTMETHOD("XrdCryptoCipher::AsBucket");
return 0;
}
//____________________________________________________________________________
int XrdCryptoCipher::Encrypt(const char *, int, char *)
{
// Encrypt lin bytes at in with local cipher.
ABSTRACTMETHOD("XrdCryptoCipher::Encrypt");
return 0;
}
//____________________________________________________________________________
int XrdCryptoCipher::Decrypt(const char *, int, char *)
{
// Decrypt lin bytes at in with local cipher.
ABSTRACTMETHOD("XrdCryptoCipher::Decrypt");
return 0;
}
//____________________________________________________________________________
int XrdCryptoCipher::EncOutLength(int)
{
// Required buffer size for encrypting l bytes
ABSTRACTMETHOD("XrdCryptoCipher::EncOutLength");
return 0;
}
//____________________________________________________________________________
int XrdCryptoCipher::DecOutLength(int)
{
// Required buffer size for decrypting l bytes
ABSTRACTMETHOD("XrdCryptoCipher::DecOutLength");
return 0;
}
//____________________________________________________________________________
bool XrdCryptoCipher::IsDefaultLength() const
{
// Test if cipher length is the default one
ABSTRACTMETHOD("XrdCryptoCipher::IsDefaultLength");
return 0;
}
//____________________________________________________________________________
int XrdCryptoCipher::MaxIVLength() const
{
// Return the max cipher IV length
ABSTRACTMETHOD("XrdCryptoCipher::MaxIVLength");
return 0;
}
//____________________________________________________________________________
int XrdCryptoCipher::Encrypt(XrdSutBucket &bck, bool useiv)
{
// Encrypt bucket bck with local cipher
// Return size of encoded bucket or -1 in case of error
int snew = -1;
int liv = 0;
char *iv = 0;
if (useiv) {
iv = RefreshIV(liv);
if (!iv) return snew;
}
int sz = EncOutLength(bck.size) + liv;
char *newbck = new char[sz];
if (newbck) {
memset(newbck, 0, sz);
if (liv > 0) memcpy(newbck, iv, liv);
snew = Encrypt(bck.buffer,bck.size,newbck+liv);
if (snew > -1)
bck.Update(newbck,snew + liv);
}
return snew;
}
//____________________________________________________________________________
int XrdCryptoCipher::Decrypt(XrdSutBucket &bck, bool useiv)
{
// Decrypt bucket bck with local cipher
// Return size of encoded bucket or -1 in case of error
int snew = -1;
int liv = (useiv) ? MaxIVLength() : 0;
int sz = DecOutLength(bck.size - liv);
char *newbck = new char[sz];
if (newbck) {
if (useiv) {
char *iv = new char[liv];
if (iv) {
memcpy(iv,bck.buffer,liv);
SetIV(liv, iv);
delete[] iv;
} else {
return snew;
}
}
memset(newbck, 0, sz);
snew = Decrypt(bck.buffer + liv, bck.size - liv, newbck);
if (snew > -1)
bck.Update(newbck,snew);
}
return snew;
}