[xmlsec] separate KW DES implementation



commit fdc6473e743642c3c2591f8b37b4aebf90cf3047
Author: Aleksey Sanin <aleksey aleksey com>
Date:   Sat May 8 22:55:12 2010 -0700

    separate KW DES implementation

 src/kw_aes_des.c     |  255 ++++++++++++++++++++++++++++++-
 src/kw_aes_des.h     |   74 +++++++++-
 src/openssl/kw_des.c |  415 +++++++++++++++++++++-----------------------------
 win32/mycfg.bat      |    2 +-
 4 files changed, 499 insertions(+), 247 deletions(-)
---
diff --git a/src/kw_aes_des.c b/src/kw_aes_des.c
index 924e5e5..7164eb7 100644
--- a/src/kw_aes_des.c
+++ b/src/kw_aes_des.c
@@ -20,15 +20,262 @@
 
 #include "kw_aes_des.h"
 
+#ifndef XMLSEC_NO_DES
+
+static int      xmlSecKWDes3BufferReverse                       (xmlSecByte *buf, 
+                                                                 xmlSecSize size);
 
-#ifndef XMLSEC_NO_AES
 /********************************************************************
  *
- * KT AES
+ * CMS Triple DES Key Wrap
+ *
+ * http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
+ *
+ * The following algorithm wraps (encrypts) a key (the wrapped key, WK)
+ * under a TRIPLEDES key-encryption-key (KEK) as specified in [CMS-Algorithms]:
+ *
+ * 1. Represent the key being wrapped as an octet sequence. If it is a
+ *    TRIPLEDES key, this is 24 octets (192 bits) with odd parity bit as
+ *    the bottom bit of each octet.
+ * 2. Compute the CMS key checksum (section 5.6.1) call this CKS.
+ * 3. Let WKCKS = WK || CKS, where || is concatenation.
+ * 4. Generate 8 random octets [RANDOM] and call this IV.
+ * 5. Encrypt WKCKS in CBC mode using KEK as the key and IV as the
+ *    initialization vector. Call the results TEMP1.
+ * 6. Left TEMP2 = IV || TEMP1.
+ * 7. Reverse the order of the octets in TEMP2 and call the result TEMP3.
+ * 8. Encrypt TEMP3 in CBC mode using the KEK and an initialization vector
+ *    of 0x4adda22c79e82105. The resulting cipher text is the desired result.
+ *    It is 40 octets long if a 168 bit key is being wrapped.
+ *
+ * The following algorithm unwraps (decrypts) a key as specified in
+ * [CMS-Algorithms]:
+ *
+ * 1. Check if the length of the cipher text is reasonable given the key type.
+ *    It must be 40 bytes for a 168 bit key and either 32, 40, or 48 bytes for
+ *    a 128, 192, or 256 bit key. If the length is not supported or inconsistent
+ *    with the algorithm for which the key is intended, return error.
+ * 2. Decrypt the cipher text with TRIPLEDES in CBC mode using the KEK and
+ *    an initialization vector (IV) of 0x4adda22c79e82105. Call the output TEMP3.
+ * 3. Reverse the order of the octets in TEMP3 and call the result TEMP2.
+ * 4. Decompose TEMP2 into IV, the first 8 octets, and TEMP1, the remaining
+ *    octets.
+ * 5. Decrypt TEMP1 using TRIPLEDES in CBC mode using the KEK and the IV found
+ *    in the previous step. Call the result WKCKS.
+ * 6. Decompose WKCKS. CKS is the last 8 octets and WK, the wrapped key, are
+ *    those octets before the CKS.
+ * 7. Calculate a CMS key checksum (section 5.6.1) over the WK and compare
+ *    with the CKS extracted in the above step. If they are not equal, return
+ *    error.
+ * 8. WK is the wrapped key, now extracted for use in data decryption.
  *
  ********************************************************************/
+static xmlSecByte xmlSecKWDes3Iv[XMLSEC_KW_DES3_IV_LENGTH] = {
+    0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
+};
 
-/**
+int
+xmlSecKWDes3Encode(xmlSecKWDes3Id kwDes3Id, void *context,
+                  const xmlSecByte *in, xmlSecSize inSize,
+                  xmlSecByte *out, xmlSecSize outSize) {
+    xmlSecByte sha1[XMLSEC_KW_DES3_SHA_DIGEST_LENGTH];
+    xmlSecByte iv[XMLSEC_KW_DES3_IV_LENGTH];
+    xmlSecSize s;
+    int ret;
+
+    xmlSecAssert2(xmlSecKWDes3CheckId(kwDes3Id), -1);
+    xmlSecAssert2(context != NULL, -1);
+    xmlSecAssert2(in != NULL, -1);
+    xmlSecAssert2(inSize > 0, -1);
+    xmlSecAssert2(out != NULL, -1);
+    xmlSecAssert2(outSize >= inSize + XMLSEC_KW_DES3_BLOCK_LENGTH + XMLSEC_KW_DES3_IV_LENGTH, -1);
+
+    /* step 2: calculate sha1 and CMS */
+    ret = kwDes3Id->sha1(context, in, inSize, sha1, sizeof(sha1));
+    if((ret < 0) || (ret != sizeof(sha1))) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->sha1",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    /* step 3: construct WKCKS as WK || CKS */
+    memcpy(out, in, inSize);
+    memcpy(out + inSize, sha1, XMLSEC_KW_DES3_BLOCK_LENGTH);
+
+    /* step 4: generate random iv */
+    ret = kwDes3Id->generateRandom(context, iv, sizeof(iv));
+    if((ret < 0) || (ret != sizeof(iv))) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->generateRandom",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    /* step 5: first encryption, result is TEMP1 */
+    ret = kwDes3Id->encrypt(context, 
+                           iv, sizeof(iv),
+                           out, inSize + XMLSEC_KW_DES3_BLOCK_LENGTH,
+                           out, outSize);
+    if((ret < 0) || (ret != inSize + XMLSEC_KW_DES3_BLOCK_LENGTH)) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->encrypt",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    /* step 6: construct TEMP2=IV || TEMP1 */
+    memmove(out + XMLSEC_KW_DES3_IV_LENGTH, out, inSize + XMLSEC_KW_DES3_BLOCK_LENGTH);
+    memcpy(out, iv, XMLSEC_KW_DES3_IV_LENGTH); 
+    s = inSize + XMLSEC_KW_DES3_BLOCK_LENGTH + XMLSEC_KW_DES3_IV_LENGTH;
+
+    /* step 7: reverse octets order, result is TEMP3 */
+    ret = xmlSecKWDes3BufferReverse(out, s);
+    if(ret < 0) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "xmlSecKWDes3BufferReverse",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    /* step 8: second encryption with static IV */
+    ret = kwDes3Id->encrypt(context, 
+                           xmlSecKWDes3Iv, sizeof(xmlSecKWDes3Iv),
+                           out, s, 
+                           out, outSize);
+    if((ret < 0) || (ret != s)) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->encrypt",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    s = ret;
+    return(s);
+}
+
+int
+xmlSecKWDes3Decode(xmlSecKWDes3Id kwDes3Id, void *context,
+                  const xmlSecByte *in, xmlSecSize inSize,
+                  xmlSecByte *out, xmlSecSize outSize) 
+{
+    xmlSecByte sha1[XMLSEC_KW_DES3_SHA_DIGEST_LENGTH];
+    xmlSecSize s;
+    int ret;
+
+    xmlSecAssert2(xmlSecKWDes3CheckId(kwDes3Id), -1);
+    xmlSecAssert2(context != NULL, -1);
+    xmlSecAssert2(in != NULL, -1);
+    xmlSecAssert2(inSize > 0, -1);
+    xmlSecAssert2(out != NULL, -1);
+    xmlSecAssert2(outSize >= inSize, -1);
+
+
+    /* step 2: first decryption with static IV, result is TEMP3 */
+    ret = kwDes3Id->decrypt(context,
+                           xmlSecKWDes3Iv, sizeof(xmlSecKWDes3Iv),
+                           in, inSize, 
+                           out, outSize);
+    if((ret < 0) || (ret < XMLSEC_KW_DES3_IV_LENGTH)) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->decrypt",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+    s = ret;
+
+    /* step 3: reverse octets order in TEMP3, result is TEMP2 */
+    ret = xmlSecKWDes3BufferReverse(out, s);
+    if(ret < 0) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "xmlSecKWDes3BufferReverse",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    /* steps 4 and 5: get IV and decrypt second time, result is WKCKS */
+    ret = kwDes3Id->decrypt(context,
+                           out, XMLSEC_KW_DES3_IV_LENGTH,
+                           out + XMLSEC_KW_DES3_IV_LENGTH, s - XMLSEC_KW_DES3_IV_LENGTH,
+                           out, outSize);
+    if((ret < 0) || (ret < XMLSEC_KW_DES3_BLOCK_LENGTH)) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->decrypt",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+    s = ret - XMLSEC_KW_DES3_BLOCK_LENGTH;
+
+    /* steps 6 and 7: calculate SHA1 and validate it */
+    ret = kwDes3Id->sha1(context,
+                        out, s,
+                        sha1, sizeof(sha1));
+    if((ret < 0) || (ret != sizeof(sha1))) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    "kwDes3Id->sha1",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    "ret=%d", ret);
+        return(-1);
+    }
+
+    /* check sha1 */
+    xmlSecAssert2(XMLSEC_KW_DES3_BLOCK_LENGTH <= sizeof(sha1), -1);
+    if(memcmp(sha1, out + s, XMLSEC_KW_DES3_BLOCK_LENGTH) != 0) {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    NULL,
+                    NULL,
+                    XMLSEC_ERRORS_R_INVALID_DATA,
+                    "SHA1 does not match");
+        return(-1);
+    }
+
+    /* done */
+    return(s);
+}
+
+static int
+xmlSecKWDes3BufferReverse(xmlSecByte *buf, xmlSecSize size) 
+{
+    xmlSecByte * p;
+    xmlSecByte ch;
+
+    xmlSecAssert2(buf != NULL, -1);
+    xmlSecAssert2(size > 0, -1);
+
+    for(p = buf + size - 1; p >= buf; ++buf, --p) {
+        ch = (*p);
+        (*p) = (*buf);
+        (*buf) = ch;
+    }
+    return (0);
+}
+
+#endif /* XMLSEC_NO_DES */
+
+
+
+#ifndef XMLSEC_NO_AES
+/********************************************************************
+ *
+ * KT AES
+ *
  * http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap:
  *
  * Assume that the data to be wrapped consists of N 64-bit data blocks
@@ -96,7 +343,7 @@
  *            P(i)=R(i)
  *          * If A is 0xA6A6A6A6A6A6A6A6, return success. Otherwise, return
  *            an integrity check failure error.
- */
+ ********************************************************************/
 static const xmlSecByte xmlSecKWAesMagicBlock[XMLSEC_KW_AES_MAGIC_BLOCK_SIZE] = {
     0xA6,  0xA6,  0xA6,  0xA6,  0xA6,  0xA6,  0xA6,  0xA6
 };
diff --git a/src/kw_aes_des.h b/src/kw_aes_des.h
index 5e80606..46e8527 100644
--- a/src/kw_aes_des.h
+++ b/src/kw_aes_des.h
@@ -22,6 +22,76 @@
 extern "C" {
 #endif /* __cplusplus */
 
+#ifndef XMLSEC_NO_DES
+/********************************************************************
+ *
+ * KT DES
+ *
+ ********************************************************************/
+#define XMLSEC_KW_DES3_KEY_LENGTH                   24
+#define XMLSEC_KW_DES3_IV_LENGTH                    8
+#define XMLSEC_KW_DES3_BLOCK_LENGTH                 8
+#define XMLSEC_KW_DES3_SHA_DIGEST_LENGTH            20
+
+
+typedef int  (*xmlSecKWDes3Sha1Method)              (void * context,
+                                                     const xmlSecByte * in,
+                                                     xmlSecSize inSize,
+                                                     xmlSecByte * out,
+                                                     xmlSecSize outSize);
+typedef int  (*xmlSecKWDes3GenerateRandomMethod)    (void * context,
+                                                     xmlSecByte * out,
+                                                     xmlSecSize outSize);
+typedef int  (*xmlSecKWDes3BlockEncryptMethod)      (void * context,
+                                                     const xmlSecByte * iv,
+                                                     xmlSecSize ivSize,
+                                                     const xmlSecByte * in,
+                                                     xmlSecSize inSize,
+                                                     xmlSecByte * out,
+                                                     xmlSecSize outSize);
+typedef int  (*xmlSecKWDes3BlockDecryptMethod)      (void * context,
+                                                     const xmlSecByte * iv,
+                                                     xmlSecSize ivSize,
+                                                     const xmlSecByte * in,
+                                                     xmlSecSize inSize,
+                                                     xmlSecByte * out,
+                                                     xmlSecSize outSize);
+
+
+struct _xmlSecKWDes3Klass {
+    /* callbacks */
+    xmlSecKWDes3GenerateRandomMethod    generateRandom;
+    xmlSecKWDes3Sha1Method              sha1;
+    xmlSecKWDes3BlockEncryptMethod      encrypt;
+    xmlSecKWDes3BlockDecryptMethod      decrypt;
+
+    /* for the future */
+    void*                               reserved0;
+    void*                               reserved1;
+}; 
+typedef const struct _xmlSecKWDes3Klass              xmlSecKWDes3Klass,
+                                                    *xmlSecKWDes3Id;
+
+#define xmlSecKWDes3CheckId(id) \
+    ( \
+     ((id) != NULL) && \
+     ((id)->generateRandom != NULL) && \
+     ((id)->sha1 != NULL) && \
+     ((id)->encrypt != NULL) && \
+     ((id)->decrypt != NULL) \
+    )
+
+XMLSEC_EXPORT int
+xmlSecKWDes3Encode(xmlSecKWDes3Id kwDes3Id, void *context,
+                  const xmlSecByte *in, xmlSecSize inSize,
+                  xmlSecByte *out, xmlSecSize outSize);
+
+XMLSEC_EXPORT int
+xmlSecKWDes3Decode(xmlSecKWDes3Id kwDes3Id, void *context,
+                  const xmlSecByte *in, xmlSecSize inSize,
+                  xmlSecByte *out, xmlSecSize outSize);
+#endif /* XMLSEC_NO_DES */
+
 #ifndef XMLSEC_NO_AES
 /********************************************************************
  *
@@ -59,12 +129,12 @@ typedef const struct _xmlSecKWAesKlass              xmlSecKWAesKlass,
                                                     *xmlSecKWAesId;
 
 XMLSEC_EXPORT int
-xmlSecKWAesEncode(xmlSecKWAesId kwAesId, void *key,
+xmlSecKWAesEncode(xmlSecKWAesId kwAesId, void *context,
                   const xmlSecByte *in, xmlSecSize inSize,
                   xmlSecByte *out, xmlSecSize outSize);
 
 XMLSEC_EXPORT int
-xmlSecKWAesDecode(xmlSecKWAesId kwAesId, void *key,
+xmlSecKWAesDecode(xmlSecKWAesId kwAesId, void *context,
                   const xmlSecByte *in, xmlSecSize inSize,
                   xmlSecByte *out, xmlSecSize outSize);
 
diff --git a/src/openssl/kw_des.c b/src/openssl/kw_des.c
index e98304a..63af05a 100644
--- a/src/openssl/kw_des.c
+++ b/src/openssl/kw_des.c
@@ -28,9 +28,58 @@
 
 #include <xmlsec/openssl/crypto.h>
 
-#define XMLSEC_OPENSSL_DES3_KEY_LENGTH                          24
-#define XMLSEC_OPENSSL_DES3_IV_LENGTH                           8
-#define XMLSEC_OPENSSL_DES3_BLOCK_LENGTH                        8
+#include "../kw_aes_des.h"
+
+/*********************************************************************
+ *
+ * DES KW implementation
+ *
+ *********************************************************************/
+static int       xmlSecOpenSSLKWDes3GenerateRandom               (void * context,
+                                                                 xmlSecByte * out, 
+                                                                 xmlSecSize outSize);
+static int       xmlSecOpenSSLKWDes3Sha1                         (void * context,
+                                                                 const xmlSecByte * in, 
+                                                                 xmlSecSize inSize, 
+                                                                 xmlSecByte * out, 
+                                                                 xmlSecSize outSize);
+static int      xmlSecOpenSSLKWDes3BlockEncrypt                  (void * context,
+                                                                 const xmlSecByte * iv, 
+                                                                 xmlSecSize ivSize,
+                                                                 const xmlSecByte * in, 
+                                                                 xmlSecSize inSize,
+                                                                 xmlSecByte * out, 
+                                                                 xmlSecSize outSize);
+static int      xmlSecOpenSSLKWDes3BlockDecrypt                  (void * context,
+                                                                 const xmlSecByte * iv, 
+                                                                 xmlSecSize ivSize,
+                                                                 const xmlSecByte * in, 
+                                                                 xmlSecSize inSize,
+                                                                 xmlSecByte * out, 
+                                                                 xmlSecSize outSize);
+
+static xmlSecKWDes3Klass xmlSecOpenSSLKWDes3ImplKlass = {
+    /* callbacks */
+    xmlSecOpenSSLKWDes3GenerateRandom,       /* xmlSecKWDes3GenerateRandomMethod     generateRandom; */
+    xmlSecOpenSSLKWDes3Sha1,                 /* xmlSecKWDes3Sha1Method               sha1; */
+    xmlSecOpenSSLKWDes3BlockEncrypt,         /* xmlSecKWDes3BlockEncryptMethod       encrypt; */
+    xmlSecOpenSSLKWDes3BlockDecrypt,         /* xmlSecKWDes3BlockDecryptMethod       decrypt; */
+
+    /* for the future */
+    NULL,                                   /* void*                               reserved0; */
+    NULL,                                   /* void*                               reserved1; */
+}; 
+
+static int      xmlSecOpenSSLKWDes3Encrypt                      (const xmlSecByte *key, 
+                                                                 xmlSecSize keySize,
+                                                                 const xmlSecByte *iv, 
+                                                                 xmlSecSize ivSize,
+                                                                 const xmlSecByte *in, 
+                                                                 xmlSecSize inSize,
+                                                                 xmlSecByte *out, 
+                                                                 xmlSecSize outSize, 
+                                                                 int enc);
+
 
 /*********************************************************************
  *
@@ -39,10 +88,15 @@
  * key (xmlSecBuffer) is located after xmlSecTransform structure
  *
  ********************************************************************/
-#define xmlSecOpenSSLKWDes3GetKey(transform) \
-    ((xmlSecBufferPtr)(((xmlSecByte*)(transform)) + sizeof(xmlSecTransform)))
-#define xmlSecOpenSSLKWDes3Size \
-    (sizeof(xmlSecTransform) + sizeof(xmlSecBuffer))
+typedef struct _xmlSecOpenSSLKWDes3Ctx              xmlSecOpenSSLKWDes3Ctx,
+                                                  *xmlSecOpenSSLKWDes3CtxPtr;
+struct _xmlSecOpenSSLKWDes3Ctx {
+    xmlSecBuffer        keyBuffer;
+};
+#define xmlSecOpenSSLKWDes3Size     \
+    (sizeof(xmlSecTransform) + sizeof(xmlSecOpenSSLKWDes3Ctx))
+#define xmlSecOpenSSLKWDes3GetCtx(transform) \
+    ((xmlSecOpenSSLKWDes3CtxPtr)(((xmlSecByte*)(transform)) + sizeof(xmlSecTransform)))
 
 static int      xmlSecOpenSSLKWDes3Initialize                   (xmlSecTransformPtr transform);
 static void     xmlSecOpenSSLKWDes3Finalize                     (xmlSecTransformPtr transform);
@@ -53,30 +107,6 @@ static int      xmlSecOpenSSLKWDes3SetKey                       (xmlSecTransform
 static int      xmlSecOpenSSLKWDes3Execute                      (xmlSecTransformPtr transform,
                                                                  int last,
                                                                  xmlSecTransformCtxPtr transformCtx);
-static int      xmlSecOpenSSLKWDes3Encode                       (const xmlSecByte *key,
-                                                                 xmlSecSize keySize,
-                                                                 const xmlSecByte *in,
-                                                                 xmlSecSize inSize,
-                                                                 xmlSecByte *out,
-                                                                 xmlSecSize outSize);
-static int      xmlSecOpenSSLKWDes3Decode                       (const xmlSecByte *key,
-                                                                 xmlSecSize keySize,
-                                                                 const xmlSecByte *in,
-                                                                 xmlSecSize inSize,
-                                                                 xmlSecByte *out,
-                                                                 xmlSecSize outSize);
-static int      xmlSecOpenSSLKWDes3Encrypt                      (const xmlSecByte *key,
-                                                                 xmlSecSize keySize,
-                                                                 const xmlSecByte *iv,
-                                                                 xmlSecSize ivSize,
-                                                                 const xmlSecByte *in,
-                                                                 xmlSecSize inSize,
-                                                                 xmlSecByte *out,
-                                                                 xmlSecSize outSize,
-                                                                 int enc);
-static int      xmlSecOpenSSLKWDes3BufferReverse                (xmlSecByte *buf,
-                                                                 xmlSecSize size);
-
 static xmlSecTransformKlass xmlSecOpenSSLKWDes3Klass = {
     /* klass/object sizes */
     sizeof(xmlSecTransformKlass),               /* xmlSecSize klassSize */
@@ -118,12 +148,16 @@ xmlSecOpenSSLTransformKWDes3GetKlass(void) {
 
 static int
 xmlSecOpenSSLKWDes3Initialize(xmlSecTransformPtr transform) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx;
     int ret;
 
     xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformKWDes3Id), -1);
     xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLKWDes3Size), -1);
 
-    ret = xmlSecBufferInitialize(xmlSecOpenSSLKWDes3GetKey(transform), 0);
+    ctx = xmlSecOpenSSLKWDes3GetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
+    ret = xmlSecBufferInitialize(&(ctx->keyBuffer), 0);
     if(ret < 0) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
@@ -138,21 +172,29 @@ xmlSecOpenSSLKWDes3Initialize(xmlSecTransformPtr transform) {
 
 static void
 xmlSecOpenSSLKWDes3Finalize(xmlSecTransformPtr transform) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx;
+
     xmlSecAssert(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformKWDes3Id));
     xmlSecAssert(xmlSecTransformCheckSize(transform, xmlSecOpenSSLKWDes3Size));
 
-    if(xmlSecOpenSSLKWDes3GetKey(transform) != NULL) {
-        xmlSecBufferFinalize(xmlSecOpenSSLKWDes3GetKey(transform));
-    }
+    ctx = xmlSecOpenSSLKWDes3GetCtx(transform);
+    xmlSecAssert(ctx != NULL);
+
+    xmlSecBufferFinalize(&(ctx->keyBuffer));
 }
 
 static int
 xmlSecOpenSSLKWDes3SetKeyReq(xmlSecTransformPtr transform,  xmlSecKeyReqPtr keyReq) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx;
+
     xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformKWDes3Id), -1);
     xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
     xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLKWDes3Size), -1);
     xmlSecAssert2(keyReq != NULL, -1);
 
+    ctx = xmlSecOpenSSLKWDes3GetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
     keyReq->keyId       = xmlSecOpenSSLKeyDataDesId;
     keyReq->keyType     = xmlSecKeyDataTypeSymmetric;
     if(transform->operation == xmlSecTransformOperationEncrypt) {
@@ -160,12 +202,13 @@ xmlSecOpenSSLKWDes3SetKeyReq(xmlSecTransformPtr transform,  xmlSecKeyReqPtr keyR
     } else {
         keyReq->keyUsage= xmlSecKeyUsageDecrypt;
     }
-    keyReq->keyBitsSize = 8 * XMLSEC_OPENSSL_DES3_KEY_LENGTH;
+    keyReq->keyBitsSize = 8 * XMLSEC_KW_DES3_KEY_LENGTH;
     return(0);
 }
 
 static int
 xmlSecOpenSSLKWDes3SetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx;
     xmlSecBufferPtr buffer;
     xmlSecSize keySize;
     int ret;
@@ -173,33 +216,33 @@ xmlSecOpenSSLKWDes3SetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) {
     xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecOpenSSLTransformKWDes3Id), -1);
     xmlSecAssert2((transform->operation == xmlSecTransformOperationEncrypt) || (transform->operation == xmlSecTransformOperationDecrypt), -1);
     xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLKWDes3Size), -1);
-    xmlSecAssert2(xmlSecOpenSSLKWDes3GetKey(transform) != NULL, -1);
     xmlSecAssert2(key != NULL, -1);
     xmlSecAssert2(xmlSecKeyDataCheckId(xmlSecKeyGetValue(key), xmlSecOpenSSLKeyDataDesId), -1);
 
+    ctx = xmlSecOpenSSLKWDes3GetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
     buffer = xmlSecKeyDataBinaryValueGetBuffer(xmlSecKeyGetValue(key));
     xmlSecAssert2(buffer != NULL, -1);
 
     keySize = xmlSecBufferGetSize(buffer);
-    if(keySize < XMLSEC_OPENSSL_DES3_KEY_LENGTH) {
+    if(keySize < XMLSEC_KW_DES3_KEY_LENGTH) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
                     NULL,
                     XMLSEC_ERRORS_R_INVALID_KEY_DATA_SIZE,
                     "key length %d is not enough (%d expected)",
-                    keySize, XMLSEC_OPENSSL_DES3_KEY_LENGTH);
+                    keySize, XMLSEC_KW_DES3_KEY_LENGTH);
         return(-1);
     }
 
-    ret = xmlSecBufferSetData(xmlSecOpenSSLKWDes3GetKey(transform),
-                            xmlSecBufferGetData(buffer),
-                            XMLSEC_OPENSSL_DES3_KEY_LENGTH);
+    ret = xmlSecBufferSetData(&(ctx->keyBuffer), xmlSecBufferGetData(buffer), XMLSEC_KW_DES3_KEY_LENGTH);
     if(ret < 0) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
                     "xmlSecBufferSetData",
                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
-                    "size=%d", XMLSEC_OPENSSL_DES3_KEY_LENGTH);
+                    "size=%d", XMLSEC_KW_DES3_KEY_LENGTH);
         return(-1);
     }
 
@@ -208,7 +251,8 @@ xmlSecOpenSSLKWDes3SetKey(xmlSecTransformPtr transform, xmlSecKeyPtr key) {
 
 static int
 xmlSecOpenSSLKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPtr transformCtx) {
-    xmlSecBufferPtr in, out, key;
+    xmlSecOpenSSLKWDes3CtxPtr ctx;
+    xmlSecBufferPtr in, out;
     xmlSecSize inSize, outSize, keySize;
     int ret;
 
@@ -217,11 +261,11 @@ xmlSecOpenSSLKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransfo
     xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecOpenSSLKWDes3Size), -1);
     xmlSecAssert2(transformCtx != NULL, -1);
 
-    key = xmlSecOpenSSLKWDes3GetKey(transform);
-    xmlSecAssert2(key != NULL, -1);
+    ctx = xmlSecOpenSSLKWDes3GetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
 
-    keySize = xmlSecBufferGetSize(key);
-    xmlSecAssert2(keySize == XMLSEC_OPENSSL_DES3_KEY_LENGTH, -1);
+    keySize = xmlSecBufferGetSize(&(ctx->keyBuffer));
+    xmlSecAssert2(keySize == XMLSEC_KW_DES3_KEY_LENGTH, -1);
 
     in = &(transform->inBuf);
     out = &(transform->outBuf);
@@ -236,23 +280,24 @@ xmlSecOpenSSLKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransfo
     if((transform->status == xmlSecTransformStatusWorking) && (last == 0)) {
         /* just do nothing */
     } else  if((transform->status == xmlSecTransformStatusWorking) && (last != 0)) {
-        if((inSize % XMLSEC_OPENSSL_DES3_BLOCK_LENGTH) != 0) {
+        if((inSize % XMLSEC_KW_DES3_BLOCK_LENGTH) != 0) {
             xmlSecError(XMLSEC_ERRORS_HERE,
                         xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
                         NULL,
                         XMLSEC_ERRORS_R_INVALID_SIZE,
                         "%d bytes - not %d bytes aligned",
-                        inSize, XMLSEC_OPENSSL_DES3_BLOCK_LENGTH);
+                        inSize, XMLSEC_KW_DES3_BLOCK_LENGTH);
             return(-1);
         }
 
         if(transform->operation == xmlSecTransformOperationEncrypt) {
             /* the encoded key might be 16 bytes longer plus one block just in case */
-            outSize = inSize + XMLSEC_OPENSSL_DES3_IV_LENGTH +
-                               XMLSEC_OPENSSL_DES3_BLOCK_LENGTH +
-                               XMLSEC_OPENSSL_DES3_BLOCK_LENGTH;
+            outSize = inSize + XMLSEC_KW_DES3_IV_LENGTH +
+                               XMLSEC_KW_DES3_BLOCK_LENGTH +
+                               XMLSEC_KW_DES3_BLOCK_LENGTH;
         } else {
-            outSize = inSize + XMLSEC_OPENSSL_DES3_BLOCK_LENGTH;
+            /* just in case, add a block */
+            outSize = inSize + XMLSEC_KW_DES3_BLOCK_LENGTH;
         }
 
         ret = xmlSecBufferSetMaxSize(out, outSize);
@@ -266,13 +311,13 @@ xmlSecOpenSSLKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransfo
         }
 
         if(transform->operation == xmlSecTransformOperationEncrypt) {
-            ret = xmlSecOpenSSLKWDes3Encode(xmlSecBufferGetData(key), keySize,
-                                            xmlSecBufferGetData(in), inSize,
-                                            xmlSecBufferGetData(out), outSize);
+            ret = xmlSecKWDes3Encode(&xmlSecOpenSSLKWDes3ImplKlass, ctx,
+                                    xmlSecBufferGetData(in), inSize,
+                                    xmlSecBufferGetData(out), outSize);
             if(ret < 0) {
                 xmlSecError(XMLSEC_ERRORS_HERE,
                             xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
-                            "xmlSecOpenSSLKWDes3Encode",
+                            "xmlSecKWDes3Encode",
                             XMLSEC_ERRORS_R_XMLSEC_FAILED,
                             "key=%d,in=%d,out=%d",
                             keySize, inSize, outSize);
@@ -280,13 +325,13 @@ xmlSecOpenSSLKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransfo
             }
             outSize = ret;
         } else {
-            ret = xmlSecOpenSSLKWDes3Decode(xmlSecBufferGetData(key), keySize,
-                                            xmlSecBufferGetData(in), inSize,
-                                            xmlSecBufferGetData(out), outSize);
+            ret = xmlSecKWDes3Decode(&xmlSecOpenSSLKWDes3ImplKlass, ctx,
+                                    xmlSecBufferGetData(in), inSize,
+                                    xmlSecBufferGetData(out), outSize);
             if(ret < 0) {
                 xmlSecError(XMLSEC_ERRORS_HERE,
                             xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
-                            "xmlSecOpenSSLKWDes3Decode",
+                            "xmlSecKWDes3Decode",
                             XMLSEC_ERRORS_R_XMLSEC_FAILED,
                             "key=%d,in=%d,out=%d",
                             keySize, inSize, outSize);
@@ -330,50 +375,24 @@ xmlSecOpenSSLKWDes3Execute(xmlSecTransformPtr transform, int last, xmlSecTransfo
     return(0);
 }
 
-static xmlSecByte xmlSecOpenSSLKWDes3Iv[XMLSEC_OPENSSL_DES3_IV_LENGTH] = {
-    0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
-};
-/**
- * CMS Triple DES Key Wrap
- *
- * http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
- *
- * The following algorithm wraps (encrypts) a key (the wrapped key, WK)
- * under a TRIPLEDES key-encryption-key (KEK) as specified in [CMS-Algorithms]:
+/*********************************************************************
  *
- * 1. Represent the key being wrapped as an octet sequence. If it is a
- *    TRIPLEDES key, this is 24 octets (192 bits) with odd parity bit as
- *    the bottom bit of each octet.
- * 2. Compute the CMS key checksum (section 5.6.1) call this CKS.
- * 3. Let WKCKS = WK || CKS, where || is concatenation.
- * 4. Generate 8 random octets [RANDOM] and call this IV.
- * 5. Encrypt WKCKS in CBC mode using KEK as the key and IV as the
- *    initialization vector. Call the results TEMP1.
- * 6. Left TEMP2 = IV || TEMP1.
- * 7. Reverse the order of the octets in TEMP2 and call the result TEMP3.
- * 8. Encrypt TEMP3 in CBC mode using the KEK and an initialization vector
- *    of 0x4adda22c79e82105. The resulting cipher text is the desired result.
- *    It is 40 octets long if a 168 bit key is being wrapped.
+ * DES KW implementation
  *
- */
+ *********************************************************************/
 static int
-xmlSecOpenSSLKWDes3Encode(const xmlSecByte *key, xmlSecSize keySize,
-                        const xmlSecByte *in, xmlSecSize inSize,
-                        xmlSecByte *out, xmlSecSize outSize) {
-    xmlSecByte sha1[SHA_DIGEST_LENGTH];
-    xmlSecByte iv[XMLSEC_OPENSSL_DES3_IV_LENGTH];
-    xmlSecSize s;
-    int ret;
+xmlSecOpenSSLKWDes3Sha1(void * context,
+                       const xmlSecByte * in, xmlSecSize inSize, 
+                       xmlSecByte * out, xmlSecSize outSize) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx = (xmlSecOpenSSLKWDes3CtxPtr)context;
 
-    xmlSecAssert2(key != NULL, -1);
-    xmlSecAssert2(keySize == XMLSEC_OPENSSL_DES3_KEY_LENGTH, -1);
+    xmlSecAssert2(ctx != NULL, -1);
     xmlSecAssert2(in != NULL, -1);
     xmlSecAssert2(inSize > 0, -1);
     xmlSecAssert2(out != NULL, -1);
-    xmlSecAssert2(outSize >= inSize + 16, -1);
+    xmlSecAssert2(outSize >= SHA_DIGEST_LENGTH, -1);
 
-    /* step 2: calculate sha1 and CMS */
-    if(SHA1(in, inSize, sha1) == NULL) {
+    if(SHA1(in, inSize, out) == NULL) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     NULL,
                     "SHA1",
@@ -382,12 +401,20 @@ xmlSecOpenSSLKWDes3Encode(const xmlSecByte *key, xmlSecSize keySize,
         return(-1);
     }
 
-    /* step 3: construct WKCKS */
-    memcpy(out, in, inSize);
-    memcpy(out + inSize, sha1, XMLSEC_OPENSSL_DES3_BLOCK_LENGTH);
+    return(SHA_DIGEST_LENGTH);
+}
+
+static int
+xmlSecOpenSSLKWDes3GenerateRandom(void * context,
+                                 xmlSecByte * out, xmlSecSize outSize) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx = (xmlSecOpenSSLKWDes3CtxPtr)context;
+    int ret;
+
+    xmlSecAssert2(ctx != NULL, -1);
+    xmlSecAssert2(out != NULL, -1);
+    xmlSecAssert2(outSize > 0, -1);
 
-    /* step 4: generate random iv */
-    ret = RAND_bytes(iv, XMLSEC_OPENSSL_DES3_IV_LENGTH);
+    ret = RAND_bytes(out, outSize);
     if(ret != 1) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     NULL,
@@ -397,41 +424,32 @@ xmlSecOpenSSLKWDes3Encode(const xmlSecByte *key, xmlSecSize keySize,
         return(-1);
     }
 
-    /* step 5: first encryption, result is TEMP1 */
-    ret = xmlSecOpenSSLKWDes3Encrypt(key, keySize,
-                                    iv, XMLSEC_OPENSSL_DES3_IV_LENGTH,
-                                    out, inSize + XMLSEC_OPENSSL_DES3_BLOCK_LENGTH,
-                                    out, outSize, 1);
-    if(ret < 0) {
-        xmlSecError(XMLSEC_ERRORS_HERE,
-                    NULL,
-                    "xmlSecOpenSSLKWDes3Encrypt",
-                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
-                    XMLSEC_ERRORS_NO_MESSAGE);
-        return(-1);
-    }
+    return((int)outSize);
+}
 
-    /* step 6: construct TEMP2=IV || TEMP1 */
-    memmove(out + XMLSEC_OPENSSL_DES3_IV_LENGTH, out,
-            inSize + XMLSEC_OPENSSL_DES3_IV_LENGTH);
-    memcpy(out, iv, XMLSEC_OPENSSL_DES3_IV_LENGTH);
-    s = ret + XMLSEC_OPENSSL_DES3_IV_LENGTH;
+static int
+xmlSecOpenSSLKWDes3BlockEncrypt(void * context,
+                               const xmlSecByte * iv, xmlSecSize ivSize,
+                               const xmlSecByte * in, xmlSecSize inSize,
+                               xmlSecByte * out, xmlSecSize outSize) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx = (xmlSecOpenSSLKWDes3CtxPtr)context;
+    int ret;
 
-    /* step 7: reverse octets order, result is TEMP3 */
-    ret = xmlSecOpenSSLKWDes3BufferReverse(out, s);
-    if(ret < 0) {
-        xmlSecError(XMLSEC_ERRORS_HERE,
-                    NULL,
-                    "xmlSecOpenSSLKWDes3BufferReverse",
-                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
-                    XMLSEC_ERRORS_NO_MESSAGE);
-        return(-1);
-    }
+    xmlSecAssert2(ctx != NULL, -1);
+    xmlSecAssert2(xmlSecBufferGetData(&(ctx->keyBuffer)) != NULL, -1);
+    xmlSecAssert2(xmlSecBufferGetSize(&(ctx->keyBuffer)) >= XMLSEC_KW_DES3_KEY_LENGTH, -1);
+    xmlSecAssert2(iv != NULL, -1);
+    xmlSecAssert2(ivSize >= XMLSEC_KW_DES3_IV_LENGTH, -1);
+    xmlSecAssert2(in != NULL, -1);
+    xmlSecAssert2(inSize >= 0, -1);
+    xmlSecAssert2(out != NULL, -1);
+    xmlSecAssert2(outSize >= inSize, -1);
 
-    /* step 8: second encryption with static IV */
-    ret = xmlSecOpenSSLKWDes3Encrypt(key, keySize,
-                                    xmlSecOpenSSLKWDes3Iv, XMLSEC_OPENSSL_DES3_IV_LENGTH,
-                                    out, s, out, outSize, 1);
+    ret = xmlSecOpenSSLKWDes3Encrypt(xmlSecBufferGetData(&(ctx->keyBuffer)), XMLSEC_KW_DES3_KEY_LENGTH,
+                                    iv, XMLSEC_KW_DES3_IV_LENGTH,
+                                    in, inSize,
+                                    out, outSize, 
+                                    1); /* encrypt */
     if(ret < 0) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     NULL,
@@ -440,119 +458,53 @@ xmlSecOpenSSLKWDes3Encode(const xmlSecByte *key, xmlSecSize keySize,
                     XMLSEC_ERRORS_NO_MESSAGE);
         return(-1);
     }
-    s = ret;
-    return(s);
+
+    return(ret);
 }
 
-/**
- * CMS Triple DES Key Wrap
- *
- * http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
- *
- * The following algorithm unwraps (decrypts) a key as specified in
- * [CMS-Algorithms]:
- *
- * 1. Check if the length of the cipher text is reasonable given the key type.
- *    It must be 40 bytes for a 168 bit key and either 32, 40, or 48 bytes for
- *    a 128, 192, or 256 bit key. If the length is not supported or inconsistent
- *    with the algorithm for which the key is intended, return error.
- * 2. Decrypt the cipher text with TRIPLEDES in CBC mode using the KEK and
- *    an initialization vector (IV) of 0x4adda22c79e82105. Call the output TEMP3.
- * 3. Reverse the order of the octets in TEMP3 and call the result TEMP2.
- * 4. Decompose TEMP2 into IV, the first 8 octets, and TEMP1, the remaining
- *    octets.
- * 5. Decrypt TEMP1 using TRIPLEDES in CBC mode using the KEK and the IV found
- *    in the previous step. Call the result WKCKS.
- * 6. Decompose WKCKS. CKS is the last 8 octets and WK, the wrapped key, are
- *    those octets before the CKS.
- * 7. Calculate a CMS key checksum (section 5.6.1) over the WK and compare
- *    with the CKS extracted in the above step. If they are not equal, return
- *    error.
- * 8. WK is the wrapped key, now extracted for use in data decryption.
- */
 static int
-xmlSecOpenSSLKWDes3Decode(const xmlSecByte *key, xmlSecSize keySize,
-                        const xmlSecByte *in, xmlSecSize inSize,
-                        xmlSecByte *out, xmlSecSize outSize) {
-    xmlSecByte sha1[SHA_DIGEST_LENGTH];
-    xmlSecSize s;
+xmlSecOpenSSLKWDes3BlockDecrypt(void * context,
+                               const xmlSecByte * iv, xmlSecSize ivSize,
+                               const xmlSecByte * in, xmlSecSize inSize,
+                               xmlSecByte * out, xmlSecSize outSize) {
+    xmlSecOpenSSLKWDes3CtxPtr ctx = (xmlSecOpenSSLKWDes3CtxPtr)context;
     int ret;
 
-    xmlSecAssert2(key != NULL, -1);
-    xmlSecAssert2(keySize == XMLSEC_OPENSSL_DES3_KEY_LENGTH, -1);
+    xmlSecAssert2(ctx != NULL, -1);
+    xmlSecAssert2(xmlSecBufferGetData(&(ctx->keyBuffer)) != NULL, -1);
+    xmlSecAssert2(xmlSecBufferGetSize(&(ctx->keyBuffer)) >= XMLSEC_KW_DES3_KEY_LENGTH, -1);
+    xmlSecAssert2(iv != NULL, -1);
+    xmlSecAssert2(ivSize >= XMLSEC_KW_DES3_IV_LENGTH, -1);
     xmlSecAssert2(in != NULL, -1);
-    xmlSecAssert2(inSize > 0, -1);
+    xmlSecAssert2(inSize >= 0, -1);
     xmlSecAssert2(out != NULL, -1);
     xmlSecAssert2(outSize >= inSize, -1);
 
-    /* step 2: first decryption with static IV, result is TEMP3 */
-    ret = xmlSecOpenSSLKWDes3Encrypt(key, keySize,
-                                    xmlSecOpenSSLKWDes3Iv, XMLSEC_OPENSSL_DES3_IV_LENGTH,
-                                    in, inSize, out, outSize, 0);
-    if((ret < 0) || (ret < XMLSEC_OPENSSL_DES3_IV_LENGTH)) {
-        xmlSecError(XMLSEC_ERRORS_HERE,
-                    NULL,
-                    "xmlSecOpenSSLKWDes3Encrypt",
-                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
-                    XMLSEC_ERRORS_NO_MESSAGE);
-        return(-1);
-    }
-    s = ret;
-
-    /* step 3: reverse octets order in TEMP3, result is TEMP2 */
-    ret = xmlSecOpenSSLKWDes3BufferReverse(out, s);
+    ret = xmlSecOpenSSLKWDes3Encrypt(xmlSecBufferGetData(&(ctx->keyBuffer)), XMLSEC_KW_DES3_KEY_LENGTH,
+                                    iv, XMLSEC_KW_DES3_IV_LENGTH,
+                                    in, inSize,
+                                    out, outSize, 
+                                    0); /* decrypt */
     if(ret < 0) {
         xmlSecError(XMLSEC_ERRORS_HERE,
                     NULL,
-                    "xmlSecOpenSSLKWDes3BufferReverse",
-                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
-                    XMLSEC_ERRORS_NO_MESSAGE);
-        return(-1);
-    }
-
-    /* steps 4 and 5: get IV and decrypt second time, result is WKCKS */
-    ret = xmlSecOpenSSLKWDes3Encrypt(key, keySize,
-                                     out, XMLSEC_OPENSSL_DES3_IV_LENGTH,
-                                     out + XMLSEC_OPENSSL_DES3_IV_LENGTH,
-                                     s - XMLSEC_OPENSSL_DES3_IV_LENGTH,
-                                     out, outSize, 0);
-    if((ret < 0) || (ret < XMLSEC_OPENSSL_DES3_BLOCK_LENGTH)) {
-        xmlSecError(XMLSEC_ERRORS_HERE,
-                    NULL,
                     "xmlSecOpenSSLKWDes3Encrypt",
                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
                     XMLSEC_ERRORS_NO_MESSAGE);
         return(-1);
     }
-    s = ret - XMLSEC_OPENSSL_DES3_BLOCK_LENGTH;
 
-    /* steps 6 and 7: calculate SHA1 and validate it */
-    if(SHA1(out, s, sha1) == NULL) {
-        xmlSecError(XMLSEC_ERRORS_HERE,
-                    NULL,
-                    "SHA1",
-                    XMLSEC_ERRORS_R_CRYPTO_FAILED,
-                    XMLSEC_ERRORS_NO_MESSAGE);
-        return(-1);
-    }
+    return(ret);
+}
 
-    if(memcmp(sha1, out + s, XMLSEC_OPENSSL_DES3_BLOCK_LENGTH) != 0) {
-        xmlSecError(XMLSEC_ERRORS_HERE,
-                    NULL,
-                    NULL,
-                    XMLSEC_ERRORS_R_INVALID_DATA,
-                    "SHA1 does not match");
-        return(-1);
-    }
 
-    return(s);
-}
 
 static int
 xmlSecOpenSSLKWDes3Encrypt(const xmlSecByte *key, xmlSecSize keySize,
                            const xmlSecByte *iv, xmlSecSize ivSize,
                            const xmlSecByte *in, xmlSecSize inSize,
-                           xmlSecByte *out, xmlSecSize outSize, int enc) {
+                           xmlSecByte *out, xmlSecSize outSize, 
+                           int enc) {
     EVP_CIPHER_CTX cipherCtx;
     int updateLen;
     int finalLen;
@@ -606,23 +558,6 @@ xmlSecOpenSSLKWDes3Encrypt(const xmlSecByte *key, xmlSecSize keySize,
     return(updateLen + finalLen);
 }
 
-static int
-xmlSecOpenSSLKWDes3BufferReverse(xmlSecByte *buf, xmlSecSize size) {
-    xmlSecSize s;
-    xmlSecSize i;
-    xmlSecByte c;
-
-    xmlSecAssert2(buf != NULL, -1);
-
-    s = size / 2;
-    --size;
-    for(i = 0; i < s; ++i) {
-        c = buf[i];
-        buf[i] = buf[size - i];
-        buf[size - i] = c;
-    }
-    return(0);
-}
 
 #endif /* XMLSEC_NO_DES */
 
diff --git a/win32/mycfg.bat b/win32/mycfg.bat
index 9af468e..b1ec8ad 100644
--- a/win32/mycfg.bat
+++ b/win32/mycfg.bat
@@ -11,7 +11,7 @@ REM
 SET PREFIX=C:\cygwin\home\local
 SET XMLSEC_INCLUDE=%PREFIX%\include;%PREFIX%\include\mozilla;%PREFIX%\include\mozilla\nspr;%PREFIX%\include\mozilla\nss;%MSSDK_INCLUDE%
 SET XMLSEC_LIB=%PREFIX%\lib;%MSSDK_LIB%
-SET XMLSEC_OPTIONS=static=no iconv=no debug=yes xslt=yes crypto=mscrypto,openssl unicode=yes
+SET XMLSEC_OPTIONS=static=no iconv=no debug=yes xslt=yes crypto=openssl unicode=yes
 
 del /F Makefile configure.txt
 cscript configure.js prefix=%PREFIX% %XMLSEC_OPTIONS% include=%XMLSEC_INCLUDE% lib=%XMLSEC_LIB% 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]