XrdCryptoLite_bf32.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                 X r d C r y p t o L i t e _ b f 3 2 . c c                  */
00004 /*                                                                            */
00005 /*                                                                            */
00006 /* (c) 2008 by the Board of Trustees of the Leland Stanford, Jr., University  */
00007 /*                            All Rights Reserved                             */
00008 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00009 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00010 /******************************************************************************/
00011 
00012 //       $Id: XrdCryptoLite_bf32.cc 30949 2009-11-02 16:37:58Z ganis $
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 /*              C l a s s   X r d C r y p t o L i t e _ b f 3 2               */
00034 /******************************************************************************/
00035   
00036 class XrdCryptoLite_bf32 : public XrdCryptoLite
00037 {
00038 public:
00039 
00040 virtual int  Decrypt(const char *key,      // Decryption key
00041                      int         keyLen,   // Decryption key byte length
00042                      const char *src,      // Buffer to be decrypted
00043                      int         srcLen,   // Bytes length of src  buffer
00044                      char       *dst,      // Buffer to hold decrypted result
00045                      int         dstLen);  // Bytes length of dst  buffer
00046 
00047 virtual int  Encrypt(const char *key,      // Encryption key
00048                      int         keyLen,   // Encryption key byte length
00049                      const char *src,      // Buffer to be encrypted
00050                      int         srcLen,   // Bytes length of src  buffer
00051                      char       *dst,      // Buffer to hold encrypted result
00052                      int         dstLen);  // Bytes length of dst  buffer
00053 
00054          XrdCryptoLite_bf32(const char deType) : XrdCryptoLite(deType, 4) {}
00055         ~XrdCryptoLite_bf32() {}
00056 };
00057 
00058 /******************************************************************************/
00059 /*                               D e c r y p t                                */
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 // Make sure we have data
00075 //
00076    if (dstLen <= (int)sizeof(crc32) || dstLen < srcLen) return -EINVAL;
00077 
00078 // Set the key
00079 //
00080    BF_set_key(&decKey, keyLen, (const unsigned char *)key);
00081 
00082 // Decrypt
00083 //
00084    BF_cfb64_encrypt((const unsigned char *)src, (unsigned char *)dst, srcLen,
00085                     &decKey, ivec, &ivnum, BF_DECRYPT);
00086 
00087 // Perform the CRC check to verify we have valid data here
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 // Return success
00095 //
00096    return dLen;
00097 }
00098   
00099 /******************************************************************************/
00100 /*                               E n c r y p t                                */
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 // Make sure that the destination if at least 4 bytes larger and we have data
00116 //
00117    if (dstLen-srcLen < (int)sizeof(crc32) || srcLen <= 0) return -EINVAL;
00118 
00119 // Normally, the msg is 4k or less but if more, get a new buffer
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 // Append a crc
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 // Set the key
00134 //
00135    BF_set_key(&encKey, keyLen, (const unsigned char *)key);
00136 
00137 // Encrypt
00138 //
00139    BF_cfb64_encrypt(bP, (unsigned char *)dst, dLen,
00140                     &encKey, ivec, &ivnum, BF_ENCRYPT);
00141 
00142 // Free temp buffer and return success
00143 //
00144    if (mP) free(mP);
00145    return dLen;
00146 }
00147 #endif
00148 
00149 /******************************************************************************/
00150 /*                X r d C r y p t o L i t e _ N e w _ b f 3 2                 */
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 }

Generated on Tue Jul 5 14:46:33 2011 for ROOT_528-00b_version by  doxygen 1.5.1