5 * DES Algorithm support
7 * This is free software; see Copyright file in the source
8 * distribution for preciese wording.
10 * Copyrigth (C) 2003 Cordys R&D BV, All rights reserved.
21 #include <xmlsec/xmlsec.h>
22 #include <xmlsec/xmltree.h>
23 #include <xmlsec/keys.h>
24 #include <xmlsec/keyinfo.h>
25 #include <xmlsec/transforms.h>
26 #include <xmlsec/errors.h>
28 #include <xmlsec/mscrypto/crypto.h>
30 /*****************************************************************************
32 * Symmetic (binary) keys - just a wrapper for xmlSecKeyDataBinary
34 ****************************************************************************/
35 static int xmlSecMSCryptoSymKeyDataInitialize (xmlSecKeyDataPtr data);
36 static int xmlSecMSCryptoSymKeyDataDuplicate (xmlSecKeyDataPtr dst,
37 xmlSecKeyDataPtr src);
38 static void xmlSecMSCryptoSymKeyDataFinalize (xmlSecKeyDataPtr data);
39 static int xmlSecMSCryptoSymKeyDataXmlRead (xmlSecKeyDataId id,
42 xmlSecKeyInfoCtxPtr keyInfoCtx);
43 static int xmlSecMSCryptoSymKeyDataXmlWrite (xmlSecKeyDataId id,
46 xmlSecKeyInfoCtxPtr keyInfoCtx);
47 static int xmlSecMSCryptoSymKeyDataBinRead (xmlSecKeyDataId id,
49 const unsigned char* buf,
51 xmlSecKeyInfoCtxPtr keyInfoCtx);
52 static int xmlSecMSCryptoSymKeyDataBinWrite (xmlSecKeyDataId id,
56 xmlSecKeyInfoCtxPtr keyInfoCtx);
57 static int xmlSecMSCryptoSymKeyDataGenerate (xmlSecKeyDataPtr data,
59 xmlSecKeyDataType type);
61 static xmlSecKeyDataType xmlSecMSCryptoSymKeyDataGetType(xmlSecKeyDataPtr data);
62 static size_t xmlSecMSCryptoSymKeyDataGetSize (xmlSecKeyDataPtr data);
63 static void xmlSecMSCryptoSymKeyDataDebugDump (xmlSecKeyDataPtr data,
65 static void xmlSecMSCryptoSymKeyDataDebugXmlDump (xmlSecKeyDataPtr data,
67 static int xmlSecMSCryptoSymKeyDataKlassCheck (xmlSecKeyDataKlass* klass);
70 /**************************************************************************
72 * <xmlsec:AESKeyValue> processing
74 *************************************************************************/
75 static xmlSecKeyDataKlass xmlSecMSCryptoKeyDataAesKlass = {
76 sizeof(xmlSecKeyDataKlass),
77 xmlSecKeyDataBinarySize,
80 xmlSecNameAESKeyValue,
81 xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml,
82 /* xmlSecKeyDataUsage usage; */
83 xmlSecHrefAESKeyValue, /* const xmlChar* href; */
84 xmlSecNodeAESKeyValue, /* const xmlChar* dataNodeName; */
85 xmlSecNs, /* const xmlChar* dataNodeNs; */
87 /* constructors/destructor */
88 xmlSecMSCryptoSymKeyDataInitialize, /* xmlSecKeyDataInitializeMethod initialize; */
89 xmlSecMSCryptoSymKeyDataDuplicate, /* xmlSecKeyDataDuplicateMethod duplicate; */
90 xmlSecMSCryptoSymKeyDataFinalize, /* xmlSecKeyDataFinalizeMethod finalize; */
91 xmlSecMSCryptoSymKeyDataGenerate, /* xmlSecKeyDataGenerateMethod generate; */
94 xmlSecMSCryptoSymKeyDataGetType, /* xmlSecKeyDataGetTypeMethod getType; */
95 xmlSecMSCryptoSymKeyDataGetSize, /* xmlSecKeyDataGetSizeMethod getSize; */
96 NULL, /* xmlSecKeyDataGetIdentifier getIdentifier; */
99 xmlSecMSCryptoSymKeyDataXmlRead, /* xmlSecKeyDataXmlReadMethod xmlRead; */
100 xmlSecMSCryptoSymKeyDataXmlWrite, /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
101 xmlSecMSCryptoSymKeyDataBinRead, /* xmlSecKeyDataBinReadMethod binRead; */
102 xmlSecMSCryptoSymKeyDataBinWrite, /* xmlSecKeyDataBinWriteMethod binWrite; */
105 xmlSecMSCryptoSymKeyDataDebugDump, /* xmlSecKeyDataDebugDumpMethod debugDump; */
106 xmlSecMSCryptoSymKeyDataDebugXmlDump, /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
108 /* reserved for the future */
109 NULL, /* void* reserved0; */
110 NULL, /* void* reserved1; */
114 * xmlSecMSCryptoKeyDataAesGetKlass:
116 * The AES key data klass.
118 * Returns: AES key data klass.
121 xmlSecMSCryptoKeyDataAesGetKlass(void) {
122 return(&xmlSecMSCryptoKeyDataAesKlass);
126 * xmlSecMSCryptoKeyDataAesSet:
127 * @data: the pointer to AES key data.
128 * @buf: the pointer to key value.
129 * @bufSize: the key value size (in bytes).
131 * Sets the value of AES key data.
133 * Returns: 0 on success or a negative value if an error occurs.
136 xmlSecMSCryptoKeyDataAesSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
137 xmlSecBufferPtr buffer;
139 xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecMSCryptoKeyDataAesId), -1);
140 xmlSecAssert2(buf != NULL, -1);
141 xmlSecAssert2(bufSize > 0, -1);
143 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
144 xmlSecAssert2(buffer != NULL, -1);
146 return(xmlSecBufferSetData(buffer, buf, bufSize));
148 #endif /* XMLSEC_NO_AES */
150 #ifndef XMLSEC_NO_DES
151 /**************************************************************************
153 * <xmlsec:DESKeyValue> processing
155 *************************************************************************/
156 static xmlSecKeyDataKlass xmlSecMSCryptoKeyDataDesKlass = {
157 sizeof(xmlSecKeyDataKlass),
158 xmlSecKeyDataBinarySize,
161 xmlSecNameDESKeyValue,
162 xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml,
163 /* xmlSecKeyDataUsage usage; */
164 xmlSecHrefDESKeyValue, /* const xmlChar* href; */
165 xmlSecNodeDESKeyValue, /* const xmlChar* dataNodeName; */
166 xmlSecNs, /* const xmlChar* dataNodeNs; */
168 /* constructors/destructor */
169 xmlSecMSCryptoSymKeyDataInitialize, /* xmlSecKeyDataInitializeMethod initialize; */
170 xmlSecMSCryptoSymKeyDataDuplicate, /* xmlSecKeyDataDuplicateMethod duplicate; */
171 xmlSecMSCryptoSymKeyDataFinalize, /* xmlSecKeyDataFinalizeMethod finalize; */
172 xmlSecMSCryptoSymKeyDataGenerate, /* xmlSecKeyDataGenerateMethod generate; */
175 xmlSecMSCryptoSymKeyDataGetType, /* xmlSecKeyDataGetTypeMethod getType; */
176 xmlSecMSCryptoSymKeyDataGetSize, /* xmlSecKeyDataGetSizeMethod getSize; */
177 NULL, /* xmlSecKeyDataGetIdentifier getIdentifier; */
180 xmlSecMSCryptoSymKeyDataXmlRead, /* xmlSecKeyDataXmlReadMethod xmlRead; */
181 xmlSecMSCryptoSymKeyDataXmlWrite, /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
182 xmlSecMSCryptoSymKeyDataBinRead, /* xmlSecKeyDataBinReadMethod binRead; */
183 xmlSecMSCryptoSymKeyDataBinWrite, /* xmlSecKeyDataBinWriteMethod binWrite; */
186 xmlSecMSCryptoSymKeyDataDebugDump, /* xmlSecKeyDataDebugDumpMethod debugDump; */
187 xmlSecMSCryptoSymKeyDataDebugXmlDump, /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
189 /* reserved for the future */
190 NULL, /* void* reserved0; */
191 NULL, /* void* reserved1; */
195 * xmlSecMSCryptoKeyDataDesGetKlass:
197 * The DES key data klass.
199 * Returns: DES key data klass.
202 xmlSecMSCryptoKeyDataDesGetKlass(void) {
203 return(&xmlSecMSCryptoKeyDataDesKlass);
205 #endif /* XMLSEC_NO_DES */
208 * GENERIC HELPER FUNCTIONS
211 #define xmlSecMSCryptoSymKeyDataCheckId(data) \
212 (xmlSecKeyDataIsValid((data)) && \
213 xmlSecMSCryptoSymKeyDataKlassCheck((data)->id))
216 xmlSecMSCryptoSymKeyDataInitialize(xmlSecKeyDataPtr data) {
217 xmlSecAssert2(xmlSecMSCryptoSymKeyDataCheckId(data), -1);
219 return(xmlSecKeyDataBinaryValueInitialize(data));
223 xmlSecMSCryptoSymKeyDataDuplicate(xmlSecKeyDataPtr dst, xmlSecKeyDataPtr src) {
224 xmlSecAssert2(xmlSecMSCryptoSymKeyDataCheckId(dst), -1);
225 xmlSecAssert2(xmlSecMSCryptoSymKeyDataCheckId(src), -1);
226 xmlSecAssert2(dst->id == src->id, -1);
228 return(xmlSecKeyDataBinaryValueDuplicate(dst, src));
232 xmlSecMSCryptoSymKeyDataFinalize(xmlSecKeyDataPtr data) {
233 xmlSecAssert(xmlSecMSCryptoSymKeyDataCheckId(data));
235 xmlSecKeyDataBinaryValueFinalize(data);
239 xmlSecMSCryptoSymKeyDataXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
240 xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
241 xmlSecAssert2(xmlSecMSCryptoSymKeyDataKlassCheck(id), -1);
243 return(xmlSecKeyDataBinaryValueXmlRead(id, key, node, keyInfoCtx));
247 xmlSecMSCryptoSymKeyDataXmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
248 xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
249 xmlSecAssert2(xmlSecMSCryptoSymKeyDataKlassCheck(id), -1);
251 return(xmlSecKeyDataBinaryValueXmlWrite(id, key, node, keyInfoCtx));
255 xmlSecMSCryptoSymKeyDataBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
256 const unsigned char* buf, size_t bufSize,
257 xmlSecKeyInfoCtxPtr keyInfoCtx) {
258 xmlSecAssert2(xmlSecMSCryptoSymKeyDataKlassCheck(id), -1);
260 return(xmlSecKeyDataBinaryValueBinRead(id, key, buf, bufSize, keyInfoCtx));
264 xmlSecMSCryptoSymKeyDataBinWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
265 unsigned char** buf, size_t* bufSize,
266 xmlSecKeyInfoCtxPtr keyInfoCtx) {
267 xmlSecAssert2(xmlSecMSCryptoSymKeyDataKlassCheck(id), -1);
269 return(xmlSecKeyDataBinaryValueBinWrite(id, key, buf, bufSize, keyInfoCtx));
273 xmlSecMSCryptoSymKeyDataGenerate(xmlSecKeyDataPtr data, size_t sizeBits, xmlSecKeyDataType type ATTRIBUTE_UNUSED) {
274 xmlSecBufferPtr buffer;
276 xmlSecAssert2(xmlSecMSCryptoSymKeyDataCheckId(data), -1);
277 xmlSecAssert2(sizeBits > 0, -1);
279 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
280 xmlSecAssert2(buffer != NULL, -1);
282 return(xmlSecMSCryptoGenerateRandom(buffer, (sizeBits + 7) / 8));
285 static xmlSecKeyDataType
286 xmlSecMSCryptoSymKeyDataGetType(xmlSecKeyDataPtr data) {
287 xmlSecBufferPtr buffer;
289 xmlSecAssert2(xmlSecMSCryptoSymKeyDataCheckId(data), xmlSecKeyDataTypeUnknown);
291 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
292 xmlSecAssert2(buffer != NULL, xmlSecKeyDataTypeUnknown);
294 return((xmlSecBufferGetSize(buffer) > 0) ? xmlSecKeyDataTypeSymmetric : xmlSecKeyDataTypeUnknown);
298 xmlSecMSCryptoSymKeyDataGetSize(xmlSecKeyDataPtr data) {
299 xmlSecAssert2(xmlSecMSCryptoSymKeyDataCheckId(data), 0);
301 return(xmlSecKeyDataBinaryValueGetSize(data));
305 xmlSecMSCryptoSymKeyDataDebugDump(xmlSecKeyDataPtr data, FILE* output) {
306 xmlSecAssert(xmlSecMSCryptoSymKeyDataCheckId(data));
308 xmlSecKeyDataBinaryValueDebugDump(data, output);
312 xmlSecMSCryptoSymKeyDataDebugXmlDump(xmlSecKeyDataPtr data, FILE* output) {
313 xmlSecAssert(xmlSecMSCryptoSymKeyDataCheckId(data));
315 xmlSecKeyDataBinaryValueDebugXmlDump(data, output);
319 xmlSecMSCryptoSymKeyDataKlassCheck(xmlSecKeyDataKlass* klass) {
320 #ifndef XMLSEC_NO_DES
321 if(klass == xmlSecMSCryptoKeyDataDesId) {
324 #endif /* XMLSEC_NO_DES */
326 #ifndef XMLSEC_NO_AES
327 if(klass == xmlSecMSCryptoKeyDataAesId) {
330 #endif /* XMLSEC_NO_AES */