00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 const char *XrdCryptoLite_bf32CVSID = "$Id: XrdCryptoLite_bf32.cc 30949 2009-11-02 16:37:58Z ganis $";
00015
00016 #include "XrdCrypto/XrdCryptoLite.hh"
00017
00018 #ifdef R__SSL
00019
00020 #include <errno.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <sys/types.h>
00024 #include <netinet/in.h>
00025 #include <inttypes.h>
00026
00027 #include <openssl/blowfish.h>
00028
00029 #include "XrdOuc/XrdOucCRC.hh"
00030 #include "XrdSys/XrdSysHeaders.hh"
00031
00032
00033
00034
00035
00036 class XrdCryptoLite_bf32 : public XrdCryptoLite
00037 {
00038 public:
00039
00040 virtual int Decrypt(const char *key,
00041 int keyLen,
00042 const char *src,
00043 int srcLen,
00044 char *dst,
00045 int dstLen);
00046
00047 virtual int Encrypt(const char *key,
00048 int keyLen,
00049 const char *src,
00050 int srcLen,
00051 char *dst,
00052 int dstLen);
00053
00054 XrdCryptoLite_bf32(const char deType) : XrdCryptoLite(deType, 4) {}
00055 ~XrdCryptoLite_bf32() {}
00056 };
00057
00058
00059
00060
00061
00062 int XrdCryptoLite_bf32::Decrypt(const char *key,
00063 int keyLen,
00064 const char *src,
00065 int srcLen,
00066 char *dst,
00067 int dstLen)
00068 {
00069 BF_KEY decKey;
00070 unsigned char ivec[8] = {0,0,0,0,0,0,0,0};
00071 unsigned int crc32;
00072 int ivnum = 0, dLen = srcLen-sizeof(crc32);
00073
00074
00075
00076 if (dstLen <= (int)sizeof(crc32) || dstLen < srcLen) return -EINVAL;
00077
00078
00079
00080 BF_set_key(&decKey, keyLen, (const unsigned char *)key);
00081
00082
00083
00084 BF_cfb64_encrypt((const unsigned char *)src, (unsigned char *)dst, srcLen,
00085 &decKey, ivec, &ivnum, BF_DECRYPT);
00086
00087
00088
00089 memcpy(&crc32, dst+dLen, sizeof(crc32));
00090 crc32 = ntohl(crc32);
00091 if (crc32 != XrdOucCRC::CRC32((const unsigned char *)dst, dLen))
00092 return -EPROTO;
00093
00094
00095
00096 return dLen;
00097 }
00098
00099
00100
00101
00102
00103 int XrdCryptoLite_bf32::Encrypt(const char *key,
00104 int keyLen,
00105 const char *src,
00106 int srcLen,
00107 char *dst,
00108 int dstLen)
00109 {
00110 BF_KEY encKey;
00111 unsigned char buff[4096], *bP, *mP = 0, ivec[8] = {0,0,0,0,0,0,0,0};
00112 unsigned int crc32;
00113 int ivnum = 0, dLen = srcLen+sizeof(crc32);
00114
00115
00116
00117 if (dstLen-srcLen < (int)sizeof(crc32) || srcLen <= 0) return -EINVAL;
00118
00119
00120
00121 if (dLen <= (int)sizeof(buff)) bP = buff;
00122 else {if (!(mP = (unsigned char *)malloc(dLen))) return -ENOMEM;
00123 else bP = mP;
00124 }
00125
00126
00127
00128 memcpy(bP, src, srcLen);
00129 crc32 = XrdOucCRC::CRC32(bP, srcLen);
00130 crc32 = htonl(crc32);
00131 memcpy((bP+srcLen), &crc32, sizeof(crc32));
00132
00133
00134
00135 BF_set_key(&encKey, keyLen, (const unsigned char *)key);
00136
00137
00138
00139 BF_cfb64_encrypt(bP, (unsigned char *)dst, dLen,
00140 &encKey, ivec, &ivnum, BF_ENCRYPT);
00141
00142
00143
00144 if (mP) free(mP);
00145 return dLen;
00146 }
00147 #endif
00148
00149
00150
00151
00152
00153 XrdCryptoLite *XrdCryptoLite_New_bf32(const char Type)
00154 {
00155 #ifdef R__SSL
00156 return (XrdCryptoLite *)(new XrdCryptoLite_bf32(Type));
00157 #else
00158 return (XrdCryptoLite *)0;
00159 #endif
00160 }