コード例 #1
0
ファイル: libcrypto.c プロジェクト: codinn/libssh
static void des3_1_encrypt(struct ssh_cipher_struct *cipher, void *in,
    void *out, unsigned long len) {
#ifdef DEBUG_CRYPTO
  ssh_print_hexa("Encrypt IV before", cipher->des3_key->ivs.c, 24);
#endif
  DES_ncbc_encrypt(in, out, len, &cipher->des3_key->keys[0], &cipher->des3_key->ivs.v[0], 1);
  DES_ncbc_encrypt(out, in, len, &cipher->des3_key->keys[1], &cipher->des3_key->ivs.v[1], 0);
  DES_ncbc_encrypt(in, out, len, &cipher->des3_key->keys[2], &cipher->des3_key->ivs.v[2], 1);
#ifdef DEBUG_CRYPTO
  ssh_print_hexa("Encrypt IV after", cipher->des3_key->ivs.c, 24);
#endif
}
コード例 #2
0
ファイル: ssh.c プロジェクト: SylvestreG/bitrig
static void
des3_decrypt(u_char *src, u_char *dst, int len, void *state)
{
	struct des3_state *dstate;
	
	dstate = (struct des3_state *)state;
	memcpy(dstate->iv1, dstate->iv2, 8);
	
	DES_ncbc_encrypt(src, dst, len, &dstate->k3, &dstate->iv3, DES_DECRYPT);
	DES_ncbc_encrypt(dst, dst, len, &dstate->k2, &dstate->iv2, DES_ENCRYPT);
	DES_ncbc_encrypt(dst, dst, len, &dstate->k1, &dstate->iv1, DES_DECRYPT);
}
コード例 #3
0
ファイル: wrapper.c プロジェクト: BackupTheBerlios/libssh-svn
static void des3_1_encrypt(struct crypto_struct *cipher, void *in,
    void *out, unsigned long len, void *IV) {
#ifdef DEBUG_CRYPTO
  ssh_print_hexa("Encrypt IV before", IV, 24);
#endif
  DES_ncbc_encrypt(in, out, len, cipher->key, IV, 1);
  DES_ncbc_encrypt(out, in, len, cipher->key + sizeof(DES_key_schedule),
      IV + 8, 0);
  DES_ncbc_encrypt(in, out, len, cipher->key + 2 * sizeof(DES_key_schedule),
      IV + 16, 1);
#ifdef DEBUG_CRYPTO
  ssh_print_hexa("Encrypt IV after", IV, 24);
#endif
}
コード例 #4
0
ファイル: libcrypto.c プロジェクト: SHLD/node-libssh
static void des3_1_encrypt(struct ssh_cipher_struct *cipher, void *in,
    void *out, unsigned long len) {
#ifdef DEBUG_CRYPTO
  ssh_print_hexa("Encrypt IV before", cipher->IV, 24);
#endif
  DES_ncbc_encrypt(in, out, len, cipher->key, cipher->IV, 1);
  DES_ncbc_encrypt(out, in, len, (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)),
      (void*)((uint8_t*)cipher->IV + 8), 0);
  DES_ncbc_encrypt(in, out, len, (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)),
      (void*)((uint8_t*)cipher->IV + 16), 1);
#ifdef DEBUG_CRYPTO
  ssh_print_hexa("Encrypt IV after", cipher->IV, 24);
#endif
}
コード例 #5
0
ファイル: oracle_fmt.c プロジェクト: earthquake/GI-John
static void oracle_crypt_all(int count)
{
	unsigned char buf[sizeof(cur_salt)];
	unsigned int l;

	l = salt_length + key_length;
	crypt_key[0] = 0;
	crypt_key[1] = 0;
	memcpy((char *)cur_salt + salt_length, cur_key, key_length);
	DES_ncbc_encrypt((unsigned char *)cur_salt, buf, l, &desschedule1, (DES_cblock *) crypt_key, DES_ENCRYPT);
	DES_set_key((DES_cblock *)crypt_key, &desschedule2);
	crypt_key[0] = 0;
	crypt_key[1] = 0;
	DES_ncbc_encrypt((unsigned char *)cur_salt, buf, l, &desschedule2, (DES_cblock *) crypt_key, DES_ENCRYPT);
}
コード例 #6
0
ファイル: parse_url.c プロジェクト: taek007/libwebsocket
int parse_url(const char *encodeUrl){
	unsigned char *keystring = "newtv.beijing!";
	DES_cblock key;
	DES_key_schedule key_schedule;
	//IV
    DES_cblock ivec;

	memset((char*)&ivec, 0, sizeof(ivec));
    memcpy((char*)&ivec,"12345678",sizeof("12345678"));

	// DES_string_to_key(keystring, &key);

	// if (DES_set_key_checked(&key, &key_schedule) != 0) {
	// 	printf("convert to key_schedule failed.\n");
	// 	return -1;
	// }

	memcpy(key, keystring, 8);  
      DES_set_key_unchecked(&key, &key_schedule); 

	
	printf("the url is %s\n", encodeUrl);
	char  *decode = base64_decode((char*)encodeUrl, strlen(encodeUrl)); 
	printf("the decode is %s, the length is %d\n",decode, strlen(decode));

	//解密
	char in[1000];
	DES_ncbc_encrypt(decode, in, strlen(decode), &key_schedule, &ivec, 0);

	printf("%s\n", in);


}
コード例 #7
0
ファイル: e_des.c プロジェクト: niubl/camera_project
static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
			  const unsigned char *in, unsigned int inl)
{
	DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data,
			 (DES_cblock *)ctx->iv, ctx->encrypt);
	return 1;
}
コード例 #8
0
ファイル: des_cbc.c プロジェクト: SeTriones/openssl
int mdes_cbc_encrypt(unsigned char key[8], unsigned char* src, unsigned char* des, int src_len) {
	// pkcs5 padding
	int padding = 8 - src_len % 8; 
	int new_len = src_len + padding + 1;
	unsigned char* after_padding = (unsigned char*)malloc(new_len);
	memcpy(after_padding, src, src_len);
	int i = 0;
	for (i = 0; i < padding; i++) {
		after_padding[src_len + i] = padding;
	}
	after_padding[new_len] = 0;

	DES_cblock dkey;
	memcpy(&dkey, key, sizeof(DES_cblock));
	DES_key_schedule key_schedule;
	DES_set_key_unchecked(&dkey, &key_schedule);

	DES_cblock ivec;
	memcpy(&ivec, iv, sizeof(ivec));

	DES_ncbc_encrypt(after_padding, des, new_len - 1, &key_schedule, &ivec, 1);
	des[new_len] = 0;
	fprintf(stderr, "new_len=%d\n", new_len - 1);	
	for (i = 0; i < new_len - 1; i++) {
		fprintf(stderr, "%02X ", des[i]);
	}
	fprintf(stderr, "\n");
	return new_len - 1;
}
コード例 #9
0
ファイル: RsaDesManager.cpp プロジェクト: furen2013/testwork
//des解密函数
bool CRsaDesManager::DESDecode(DES_key_schedule& key_schedule, unsigned char *pcInData, unsigned long ulInLen, unsigned char *pcOutData)
{
	DES_cblock ivec;

	// 加密
	memset((char*)&ivec, 0, sizeof(ivec));//ivec清0
	DES_ncbc_encrypt(pcInData, pcOutData, ulInLen, &key_schedule, &ivec, 0);
	return true;
}
コード例 #10
0
ファイル: e_des.c プロジェクト: google/boringssl
static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
                          size_t in_len) {
  EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data;

  DES_ncbc_encrypt(in, out, in_len, &dat->ks.ks, (DES_cblock *)ctx->iv,
                   ctx->encrypt);

  return 1;
}
コード例 #11
0
ファイル: openssl.c プロジェクト: Epictetus/postgres
static int
ossl_des_cbc_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen,
					 uint8 *res)
{
	ossldata   *od = c->ptr;

	DES_ncbc_encrypt(data, res, dlen, &od->u.des.key_schedule,
					 (DES_cblock *) od->iv, 0);
	return 0;
}
コード例 #12
0
ファイル: tt.c プロジェクト: helight/helight_code
static void desdecode(char *content_in, char *content_out)
{
	char de_content[1024] = {0};
	Base64Decode(content_in, strlen(content_in), de_content, sizeof(de_content));
	DES_cblock key_cblock;
	DES_string_to_key(key, &key_cblock);
	DES_key_schedule schedule;
	DES_set_key_checked(&key_cblock, &schedule);
	printf("key_cblock= %s\n", key_cblock);
	DES_ncbc_encrypt(de_content, content_out, strlen(de_content), &schedule, &key_cblock, DES_DECRYPT );
	printf("key_cblock= %s\n", key_cblock);

}
コード例 #13
0
ファイル: des_R.c プロジェクト: kgadek/kpfp
void cbc(FILE* inf, FILE* keyf, DES_key_schedule klucz, int tryb) {
	printf("\n--- dzialania w trybie CBC...\n");
	FILE* outf = fopen("plik.out", "w");
	int i=0;
	DES_cblock ivec, in, out;
	for(i=0; i<8; i++)
		ivec[i]='0';
	if(tryb)	{
		fwrite((void*)&klucz, sizeof(unsigned char), sizeof(DES_key_schedule), keyf);
		while((i=fread(&in, sizeof(unsigned char), 8, inf))==8) {
			DES_ncbc_encrypt(in, out, 8, &klucz, &ivec, 1);
			fwrite(&out, sizeof(unsigned char), 8, outf);
		}
	} else {
		fread((void*)&klucz, sizeof(unsigned char), sizeof(DES_key_schedule), keyf);
		while((i=fread(&in, sizeof(unsigned char), 8, inf))==8) {
			DES_ncbc_encrypt(in, out, 8, &klucz, &ivec, 0);
			fwrite(&out, sizeof(unsigned char), 8, outf);
		}
	}
	fclose(outf);
}
コード例 #14
0
ファイル: des_cbc.c プロジェクト: SeTriones/openssl
int mdes_cbc_decrypt(unsigned char key[8], unsigned char* src, unsigned char* des, int src_len) {
	DES_cblock dkey;
	memcpy(&dkey, key, sizeof(DES_cblock));
	DES_key_schedule key_schedule;
	DES_set_key_unchecked(&dkey, &key_schedule);

	DES_cblock ivec;
	memcpy(&ivec, iv, sizeof(ivec));

	DES_ncbc_encrypt(src, des, src_len, &key_schedule, &ivec, 0);
	unsigned char padding = des[src_len - 1];
	des[src_len - padding] = 0;
	return src_len - padding;
}
コード例 #15
0
std::string PbeMd5AndDesEncryptor::decrypt(const std::string& encryptedtext, const std::string& password,
        const std::string salt, long iterations) {
    std::string data = Base64Util::decode(encryptedtext);
    PbeMd5AndDesKey key = generateKey(password, salt, iterations);

    int decryptedReturnedLength = static_cast<int>(data.length());
    unsigned char decryptedReturned[decryptedReturnedLength];
    DES_ncbc_encrypt(reinterpret_cast<const unsigned char*>(data.data()), decryptedReturned,
            decryptedReturnedLength, &key.schedule, &key.ivec, DES_DECRYPT);

    std::string decrypted(reinterpret_cast<char *>(decryptedReturned), decryptedReturnedLength);
    int padValue = static_cast<int>(decrypted[decryptedReturnedLength - 1]);
    return decrypted.substr(0, decryptedReturnedLength - padValue);
}
コード例 #16
0
std::string PbeMd5AndDesEncryptor::encrypt(const std::string& plaintext, const std::string& password,
        const std::string salt, long iterations) {
    int padding = ALGO_BLOCK_SIZE - (static_cast<int>(plaintext.length()) % ALGO_BLOCK_SIZE);
    std::string pad(padding, static_cast<char>(padding));
    std::string toEncrypt = plaintext + pad;

    PbeMd5AndDesKey key = generateKey(password, salt, iterations);

    int encryptedReturnedLength = static_cast<int>(toEncrypt.length());
    unsigned char encryptedReturned[encryptedReturnedLength];
    DES_ncbc_encrypt(reinterpret_cast<const unsigned char*>(toEncrypt.data()), encryptedReturned,
            encryptedReturnedLength, &key.schedule, &key.ivec, DES_ENCRYPT);

    return Base64Util::encode(encryptedReturned, encryptedReturnedLength);
}
コード例 #17
0
ファイル: rpc_enc.c プロジェクト: 1234-/openssl
int _des_crypt(char *buf, int len, struct desparams *desp)
{
    DES_key_schedule ks;
    int enc;

    DES_set_key_unchecked(&desp->des_key, &ks);
    enc = (desp->des_dir == ENCRYPT) ? DES_ENCRYPT : DES_DECRYPT;

    if (desp->des_mode == CBC)
        DES_ecb_encrypt((const_DES_cblock *)desp->UDES.UDES_buf,
                        (DES_cblock *)desp->UDES.UDES_buf, &ks, enc);
    else {
        DES_ncbc_encrypt(desp->UDES.UDES_buf, desp->UDES.UDES_buf,
                         len, &ks, &desp->des_ivec, enc);
    }
    return (1);
}
コード例 #18
0
ファイル: Encrypt.cpp プロジェクト: gexiannan1/newfriut
void EncryptDecryptUtil::Des_Cbc_Decrypt(char* srcBytes, uint32 srcLen, char* destBytes, const char* encryptKey)
{
	DES_key_schedule schedule;
	DES_set_key_unchecked((const_DES_cblock*)encryptKey, &schedule);

	unsigned char padChar = '\0';
	uint32 alignLen = (srcLen / 8 + (srcLen % 8 ? 1 : 0)) * 8;
	memset(srcBytes + srcLen, padChar, 8 - srcLen % 8);

	uint32 count = srcLen / 8;
	uint32 idx = 0;
	for (idx = 0; idx < count; idx++)
	{
		DES_ncbc_encrypt((const unsigned char*)(srcBytes + 8 * idx), (unsigned char*)(destBytes + 8 * idx), 8, &schedule, &sIVec, DES_DECRYPT);
	}

	memcpy(srcBytes + 8 * idx, destBytes + 8 * idx, 8 - srcLen % 8);
}
コード例 #19
0
ファイル: transport.cpp プロジェクト: adaptee/qterm-hack
QByteArray SSH1Encryption::crypt(const QByteArray & src)
{
    QByteArray dest;
    if (src.size() % 8)
        qDebug("============Bad data len: %d", src.size());
    dest.resize(src.size());
    if (m_method == Encryption) {
#ifdef SSH_DEBUG
        qDebug() << "Encryption";
#endif
        DES_ncbc_encrypt((uint8_t *)src.data(), (uint8_t *)dest.data(), dest.size(), &m_key1, &m_IV1, DES_ENCRYPT);
        DES_ncbc_encrypt((uint8_t *)dest.data(), (uint8_t *)dest.data(), dest.size(), &m_key2, &m_IV2, DES_DECRYPT);
        DES_ncbc_encrypt((uint8_t *)dest.data(), (uint8_t *)dest.data(), dest.size(), &m_key3, &m_IV3, DES_ENCRYPT);
    } else {
#ifdef SSH_DEBUG
        qDebug() << "Decryption";
#endif
        DES_ncbc_encrypt((uint8_t *)src.data(), (uint8_t *)dest.data(), dest.size(), &m_key3, &m_IV3, DES_DECRYPT);
        DES_ncbc_encrypt((uint8_t *)dest.data(), (uint8_t *)dest.data(), dest.size(), &m_key2, &m_IV2, DES_ENCRYPT);
        DES_ncbc_encrypt((uint8_t *)dest.data(), (uint8_t *)dest.data(), dest.size(), &m_key1, &m_IV1, DES_DECRYPT);
    }
    return dest;
}
コード例 #20
0
ファイル: libcrypto.c プロジェクト: SHLD/node-libssh
static void des1_1_decrypt(struct ssh_cipher_struct *cipher, void *in, void *out,
                           unsigned long len){

  DES_ncbc_encrypt(in,out,len, cipher->key, cipher->IV, 0);
}
コード例 #21
0
ファイル: des_old.c プロジェクト: yyyyyao/Slicer3-lib-mirrors
void _ossl_old_des_ncbc_encrypt(_ossl_old_des_cblock *input,_ossl_old_des_cblock *output,long length,
  des_key_schedule schedule,_ossl_old_des_cblock *ivec,int enc)
  {
  DES_ncbc_encrypt((unsigned char *)input, (unsigned char *)output,
    length, (DES_key_schedule *)schedule, ivec, enc);
  }
コード例 #22
0
int main(int argc, char **argv)
	{
	long count;
	static unsigned char buf[BUFSIZE];
	static DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
	static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
	static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
	DES_key_schedule sch,sch2,sch3;
	double a,b,c,d,e;
#ifndef SIGALRM
	long ca,cb,cc,cd,ce;
#endif

#ifndef TIMES
	printf("To get the most accurate results, try to run this\n");
	printf("program when this computer is idle.\n");
#endif

	DES_set_key_unchecked(&key2,&sch2);
	DES_set_key_unchecked(&key3,&sch3);

#ifndef SIGALRM
	printf("First we calculate the approximate speed ...\n");
	DES_set_key_unchecked(&key,&sch);
	count=10;
	do	{
		long i;
		DES_LONG data[2];

		count*=2;
		Time_F(START);
		for (i=count; i; i--)
			DES_encrypt1(data,&sch,DES_ENCRYPT);
		d=Time_F(STOP);
		} while (d < 3.0);
	ca=count;
	cb=count*3;
	cc=count*3*8/BUFSIZE+1;
	cd=count*8/BUFSIZE+1;
	ce=count/20+1;
	printf("Doing set_key %ld times\n",ca);
#define COND(d)	(count != (d))
#define COUNT(d) (d)
#else
#define COND(c)	(run)
#define COUNT(d) (count)
	signal(SIGALRM,sig_done);
	printf("Doing set_key for 10 seconds\n");
	alarm(10);
#endif

	Time_F(START);
	for (count=0,run=1; COND(ca); count++)
		DES_set_key_unchecked(&key,&sch);
	d=Time_F(STOP);
	printf("%ld set_key's in %.2f seconds\n",count,d);
	a=((double)COUNT(ca))/d;

#ifdef SIGALRM
	printf("Doing DES_encrypt's for 10 seconds\n");
	alarm(10);
#else
	printf("Doing DES_encrypt %ld times\n",cb);
#endif
	Time_F(START);
	for (count=0,run=1; COND(cb); count++)
		{
		DES_LONG data[2];

		DES_encrypt1(data,&sch,DES_ENCRYPT);
		}
	d=Time_F(STOP);
	printf("%ld DES_encrypt's in %.2f second\n",count,d);
	b=((double)COUNT(cb)*8)/d;

#ifdef SIGALRM
	printf("Doing DES_cbc_encrypt on %ld byte blocks for 10 seconds\n",
		BUFSIZE);
	alarm(10);
#else
	printf("Doing DES_cbc_encrypt %ld times on %ld byte blocks\n",cc,
		BUFSIZE);
#endif
	Time_F(START);
	for (count=0,run=1; COND(cc); count++)
		DES_ncbc_encrypt(buf,buf,BUFSIZE,&sch,
			&key,DES_ENCRYPT);
	d=Time_F(STOP);
	printf("%ld DES_cbc_encrypt's of %ld byte blocks in %.2f second\n",
		count,BUFSIZE,d);
	c=((double)COUNT(cc)*BUFSIZE)/d;

#ifdef SIGALRM
	printf("Doing DES_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
		BUFSIZE);
	alarm(10);
#else
	printf("Doing DES_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,
		BUFSIZE);
#endif
	Time_F(START);
	for (count=0,run=1; COND(cd); count++)
		DES_ede3_cbc_encrypt(buf,buf,BUFSIZE,
			&sch,
			&sch2,
			&sch3,
			&key,
			DES_ENCRYPT);
	d=Time_F(STOP);
	printf("%ld DES_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
		count,BUFSIZE,d);
	d=((double)COUNT(cd)*BUFSIZE)/d;

#ifdef SIGALRM
	printf("Doing crypt for 10 seconds\n");
	alarm(10);
#else
	printf("Doing crypt %ld times\n",ce);
#endif
	Time_F(START);
	for (count=0,run=1; COND(ce); count++)
		crypt("testing1","ef");
	e=Time_F(STOP);
	printf("%ld crypts in %.2f second\n",count,e);
	e=((double)COUNT(ce))/e;

	printf("set_key            per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
	printf("DES raw ecb bytes  per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
	printf("DES cbc bytes      per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
	printf("DES ede cbc bytes  per sec = %12.2f (%9.3fuS)\n",d,8.0e6/d);
	printf("crypt              per sec = %12.2f (%9.3fuS)\n",e,1.0e6/e);
	exit(0);
#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
	return(0);
#endif
	}
コード例 #23
0
ファイル: libcrypto.c プロジェクト: codinn/libssh
static void des1_1_decrypt(struct ssh_cipher_struct *cipher, void *in, void *out,
        unsigned long len){
    DES_ncbc_encrypt(in,out,len, &cipher->des3_key->keys[0], &cipher->des3_key->ivs.v[0], 0);
}
コード例 #24
0
static crypto_error_t crypto_cipher(bytestring_t *dst,
                                    const bytestring_t *ctx,
                                    const bytestring_t *src,
                                    const bytestring_t *iv,
                                    int enc)
{
    unsigned u;
    unsigned char alg;
    DES_key_schedule key_schedule;
    DES_key_schedule key_schedule2;

    if (bytestring_get_element(&alg,ctx,0)!=BYTESTRING_OK)
        return CRYPTO_ERROR_UNKNOWN;

    if ((bytestring_get_size(src)&0x7)!=0)
        return CRYPTO_ERROR_BAD_CLEARTEXT_LENGTH;

    switch (alg)
    {
        case CRYPTO_ALG_DES_ECB:
            bytestring_resize(dst,bytestring_get_size(src));

            memcpy(&key_schedule,ctx->data+KS_HEADER_SIZE,DES_KS_SIZE);

            for (u=0; u<bytestring_get_size(src)/8; u++)
                DES_ecb_encrypt((const_DES_cblock *)(src->data+u*8),
                                (DES_cblock *)(dst->data+u*8),
                                &key_schedule,
                                enc);
            break;
        case CRYPTO_ALG_DES_CBC:
            if (iv==NULL || bytestring_get_size(iv)!=8)
                return CRYPTO_ERROR_BAD_IV_LENGTH;

            bytestring_resize(dst,bytestring_get_size(src));

            memcpy(&key_schedule,ctx->data+KS_HEADER_SIZE,DES_KS_SIZE);
            
            DES_ncbc_encrypt(src->data,
                             dst->data,
                             bytestring_get_size(src),
                             &key_schedule,
                             (DES_cblock *)(iv->data),
                             enc);
            break;
        case CRYPTO_ALG_DES2_EDE_ECB:
            bytestring_resize(dst,bytestring_get_size(src));

            memcpy(&key_schedule,ctx->data+KS_HEADER_SIZE,DES_KS_SIZE);
            memcpy(&key_schedule2,ctx->data+KS_HEADER_SIZE+DES_KS_SIZE,DES_KS_SIZE);
            
            for (u=0; u<bytestring_get_size(src)/8; u++)
                DES_ecb2_encrypt((const_DES_cblock *)(src->data+u*8),
                                 (DES_cblock *)(dst->data+u*8),
                                 &key_schedule,
                                 &key_schedule2,
                                 enc);
            break;
        case CRYPTO_ALG_DES2_EDE_CBC:
            if (iv==NULL || bytestring_get_size(iv)!=8)
                return CRYPTO_ERROR_BAD_IV_LENGTH;

            bytestring_resize(dst,bytestring_get_size(src));


            memcpy(&key_schedule,ctx->data+KS_HEADER_SIZE,DES_KS_SIZE);
            memcpy(&key_schedule2,ctx->data+KS_HEADER_SIZE+DES_KS_SIZE,DES_KS_SIZE);

            DES_ede2_cbc_encrypt(src->data,
                                 dst->data,
                                 bytestring_get_size(src),
                                 &key_schedule,
                                 &key_schedule2,
                                 (DES_cblock *)(iv->data),
                                 enc);
            break;
        default:
            return CRYPTO_ERROR_UNKNOWN_ALGORITHM;
    }
    return CRYPTO_OK;
}
コード例 #25
0
ファイル: destest.c プロジェクト: 277800076/openssl
int main(int argc, char *argv[])
{
    int j, err = 0;
    unsigned int i;
    DES_cblock in, out, outin, iv3;
    DES_key_schedule ks, ks2, ks3;
    unsigned char cbc_in[40];
    unsigned char cbc_out[40];
    DES_LONG cs;
    unsigned char cret[8];
    DES_LONG lqret[4];
    int num;
    char *str;

    printf("Doing ecb\n");
    for (i = 0; i < NUM_TESTS; i++) {
        DES_set_key_unchecked(&key_data[i], &ks);
        memcpy(in, plain_data[i], 8);
        memset(out, 0, 8);
        memset(outin, 0, 8);
        DES_ecb_encrypt(&in, &out, &ks, DES_ENCRYPT);
        DES_ecb_encrypt(&out, &outin, &ks, DES_DECRYPT);

        if (memcmp(out, cipher_data[i], 8) != 0) {
            printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(in), pt(cipher_data[i]),
                   pt(out));
            err = 1;
        }
        if (memcmp(in, outin, 8) != 0) {
            printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(out), pt(in), pt(outin));
            err = 1;
        }
    }

# ifndef LIBDES_LIT
    printf("Doing ede ecb\n");
    for (i = 0; i < (NUM_TESTS - 2); i++) {
        DES_set_key_unchecked(&key_data[i], &ks);
        DES_set_key_unchecked(&key_data[i + 1], &ks2);
        DES_set_key_unchecked(&key_data[i + 2], &ks3);
        memcpy(in, plain_data[i], 8);
        memset(out, 0, 8);
        memset(outin, 0, 8);
        DES_ecb3_encrypt(&in,&out,&ks,&ks2,&ks,DES_ENCRYPT);
        DES_ecb3_encrypt(&out,&outin,&ks,&ks2,&ks,DES_DECRYPT);

        if (memcmp(out, cipher_ecb2[i], 8) != 0) {
            printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(in), pt(cipher_ecb2[i]),
                   pt(out));
            err = 1;
        }
        if (memcmp(in, outin, 8) != 0) {
            printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
                   i + 1, pt(key_data[i]), pt(out), pt(in), pt(outin));
            err = 1;
        }
    }
# endif

    printf("Doing cbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ncbc_encrypt(cbc_data, cbc_out, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, DES_ENCRYPT);
    if (memcmp(cbc_out, cbc_ok, 32) != 0) {
        printf("cbc_encrypt encrypt error\n");
        err = 1;
    }

    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ncbc_encrypt(cbc_out, cbc_in, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data)) != 0) {
        printf("cbc_encrypt decrypt error\n");
        err = 1;
    }
# ifndef LIBDES_LIT
    printf("Doing desx cbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_xcbc_encrypt(cbc_data, cbc_out, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, &cbc2_key, &cbc3_key, DES_ENCRYPT);
    if (memcmp(cbc_out, xcbc_ok, 32) != 0) {
        printf("des_xcbc_encrypt encrypt error\n");
        err = 1;
    }
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_xcbc_encrypt(cbc_out, cbc_in, strlen((char *)cbc_data) + 1, &ks,
                     &iv3, &cbc2_key, &cbc3_key, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data) + 1) != 0) {
        printf("des_xcbc_encrypt decrypt error\n");
        err = 1;
    }
# endif

    printf("Doing ede cbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    if ((j = DES_set_key_checked(&cbc2_key, &ks2)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    if ((j = DES_set_key_checked(&cbc3_key, &ks3)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    i = strlen((char *)cbc_data) + 1;
    /* i=((i+7)/8)*8; */
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));

    DES_ede3_cbc_encrypt(cbc_data, cbc_out, 16L, &ks, &ks2, &ks3, &iv3,
                         DES_ENCRYPT);
    DES_ede3_cbc_encrypt(&(cbc_data[16]), &(cbc_out[16]), i - 16, &ks, &ks2,
                         &ks3, &iv3, DES_ENCRYPT);
    if (memcmp
        (cbc_out, cbc3_ok,
         (unsigned int)(strlen((char *)cbc_data) + 1 + 7) / 8 * 8) != 0) {
        unsigned int n;

        printf("des_ede3_cbc_encrypt encrypt error\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc_out[n]);
        printf("\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc3_ok[n]);
        printf("\n");
        err = 1;
    }

    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ede3_cbc_encrypt(cbc_out, cbc_in, i, &ks, &ks2, &ks3, &iv3, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data) + 1) != 0) {
        unsigned int n;

        printf("DES_ede3_cbc_encrypt decrypt error\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc_data[n]);
        printf("\n");
        for (n = 0; n < i; ++n)
            printf(" %02x", cbc_in[n]);
        printf("\n");
        err = 1;
    }
# ifndef LIBDES_LIT
    printf("Doing pcbc\n");
    if ((j = DES_set_key_checked(&cbc_key, &ks)) != 0) {
        printf("Key error %d\n", j);
        err = 1;
    }
    memset(cbc_out, 0, 40);
    memset(cbc_in, 0, 40);
    DES_pcbc_encrypt(cbc_data, cbc_out, strlen((char *)cbc_data) + 1, &ks,
                     &cbc_iv, DES_ENCRYPT);
    if (memcmp(cbc_out, pcbc_ok, 32) != 0) {
        printf("pcbc_encrypt encrypt error\n");
        err = 1;
    }
    DES_pcbc_encrypt(cbc_out, cbc_in, strlen((char *)cbc_data) + 1, &ks,
                     &cbc_iv, DES_DECRYPT);
    if (memcmp(cbc_in, cbc_data, strlen((char *)cbc_data) + 1) != 0) {
        printf("pcbc_encrypt decrypt error\n");
        err = 1;
    }

    printf("Doing ");
    printf("cfb8 ");
    err += cfb_test(8, cfb_cipher8);
    printf("cfb16 ");
    err += cfb_test(16, cfb_cipher16);
    printf("cfb32 ");
    err += cfb_test(32, cfb_cipher32);
    printf("cfb48 ");
    err += cfb_test(48, cfb_cipher48);
    printf("cfb64 ");
    err += cfb_test(64, cfb_cipher64);

    printf("cfb64() ");
    err += cfb64_test(cfb_cipher64);

    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    for (i = 0; i < sizeof(plain); i++)
        DES_cfb_encrypt(&(plain[i]), &(cfb_buf1[i]),
                        8, 1, &ks, &cfb_tmp, DES_ENCRYPT);
    if (memcmp(cfb_cipher8, cfb_buf1, sizeof(plain)) != 0) {
        printf("cfb_encrypt small encrypt error\n");
        err = 1;
    }

    memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));
    for (i = 0; i < sizeof(plain); i++)
        DES_cfb_encrypt(&(cfb_buf1[i]), &(cfb_buf2[i]),
                        8, 1, &ks, &cfb_tmp, DES_DECRYPT);
    if (memcmp(plain, cfb_buf2, sizeof(plain)) != 0) {
        printf("cfb_encrypt small decrypt error\n");
        err = 1;
    }

    printf("ede_cfb64() ");
    err += ede_cfb64_test(cfb_cipher64);

    printf("done\n");

    printf("Doing ofb\n");
    DES_set_key_checked(&ofb_key, &ks);
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    DES_ofb_encrypt(plain, ofb_buf1, 64, sizeof(plain) / 8, &ks, &ofb_tmp);
    if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
        printf("ofb_encrypt encrypt error\n");
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
               ofb_buf1[8 + 0], ofb_buf1[8 + 1], ofb_buf1[8 + 2],
               ofb_buf1[8 + 3], ofb_buf1[8 + 4], ofb_buf1[8 + 5],
               ofb_buf1[8 + 6], ofb_buf1[8 + 7]);
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", ofb_buf1[8 + 0],
               ofb_cipher[8 + 1], ofb_cipher[8 + 2], ofb_cipher[8 + 3],
               ofb_buf1[8 + 4], ofb_cipher[8 + 5], ofb_cipher[8 + 6],
               ofb_cipher[8 + 7]);
        err = 1;
    }
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    DES_ofb_encrypt(ofb_buf1, ofb_buf2, 64, sizeof(ofb_buf1) / 8, &ks,
                    &ofb_tmp);
    if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
        printf("ofb_encrypt decrypt error\n");
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
               ofb_buf2[8 + 0], ofb_buf2[8 + 1], ofb_buf2[8 + 2],
               ofb_buf2[8 + 3], ofb_buf2[8 + 4], ofb_buf2[8 + 5],
               ofb_buf2[8 + 6], ofb_buf2[8 + 7]);
        printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", plain[8 + 0],
               plain[8 + 1], plain[8 + 2], plain[8 + 3], plain[8 + 4],
               plain[8 + 5], plain[8 + 6], plain[8 + 7]);
        err = 1;
    }

    printf("Doing ofb64\n");
    DES_set_key_checked(&ofb_key, &ks);
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    memset(ofb_buf1, 0, sizeof(ofb_buf1));
    memset(ofb_buf2, 0, sizeof(ofb_buf1));
    num = 0;
    for (i = 0; i < sizeof(plain); i++) {
        DES_ofb64_encrypt(&(plain[i]), &(ofb_buf1[i]), 1, &ks, &ofb_tmp, &num);
    }
    if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
        printf("ofb64_encrypt encrypt error\n");
        err = 1;
    }
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    num = 0;
    DES_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), &ks, &ofb_tmp,
                      &num);
    if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
        printf("ofb64_encrypt decrypt error\n");
        err = 1;
    }

    printf("Doing ede_ofb64\n");
    DES_set_key_checked(&ofb_key, &ks);
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    memset(ofb_buf1, 0, sizeof(ofb_buf1));
    memset(ofb_buf2, 0, sizeof(ofb_buf1));
    num = 0;
    for (i = 0; i < sizeof(plain); i++) {
        DES_ede3_ofb64_encrypt(&(plain[i]), &(ofb_buf1[i]), 1, &ks, &ks,
                               &ks, &ofb_tmp, &num);
    }
    if (memcmp(ofb_cipher, ofb_buf1, sizeof(ofb_buf1)) != 0) {
        printf("ede_ofb64_encrypt encrypt error\n");
        err = 1;
    }
    memcpy(ofb_tmp, ofb_iv, sizeof(ofb_iv));
    num = 0;
    DES_ede3_ofb64_encrypt(ofb_buf1, ofb_buf2, sizeof(ofb_buf1), &ks, &ks, &ks,
                           &ofb_tmp, &num);
    if (memcmp(plain, ofb_buf2, sizeof(ofb_buf2)) != 0) {
        printf("ede_ofb64_encrypt decrypt error\n");
        err = 1;
    }

    printf("Doing cbc_cksum\n");
    DES_set_key_checked(&cbc_key, &ks);
    cs = DES_cbc_cksum(cbc_data, &cret, strlen((char *)cbc_data), &ks,
                       &cbc_iv);
    if (cs != cbc_cksum_ret) {
        printf("bad return value (%08lX), should be %08lX\n",
               (unsigned long)cs, (unsigned long)cbc_cksum_ret);
        err = 1;
    }
    if (memcmp(cret, cbc_cksum_data, 8) != 0) {
        printf("bad cbc_cksum block returned\n");
        err = 1;
    }

    printf("Doing quad_cksum\n");
    cs = DES_quad_cksum(cbc_data, (DES_cblock *)lqret,
                        (long)strlen((char *)cbc_data), 2,
                        (DES_cblock *)cbc_iv);
    if (cs != 0x70d7a63aL) {
        printf("quad_cksum error, ret %08lx should be 70d7a63a\n",
               (unsigned long)cs);
        err = 1;
    }
    if (lqret[0] != 0x327eba8dL) {
        printf("quad_cksum error, out[0] %08lx is not %08lx\n",
               (unsigned long)lqret[0], 0x327eba8dUL);
        err = 1;
    }
    if (lqret[1] != 0x201a49ccL) {
        printf("quad_cksum error, out[1] %08lx is not %08lx\n",
               (unsigned long)lqret[1], 0x201a49ccUL);
        err = 1;
    }
    if (lqret[2] != 0x70d7a63aL) {
        printf("quad_cksum error, out[2] %08lx is not %08lx\n",
               (unsigned long)lqret[2], 0x70d7a63aUL);
        err = 1;
    }
    if (lqret[3] != 0x501c2c26L) {
        printf("quad_cksum error, out[3] %08lx is not %08lx\n",
               (unsigned long)lqret[3], 0x501c2c26UL);
        err = 1;
    }
# endif

    printf("input word alignment test");
    for (i = 0; i < 4; i++) {
        printf(" %d", i);
        DES_ncbc_encrypt(&(cbc_out[i]), cbc_in,
                         strlen((char *)cbc_data) + 1, &ks,
                         &cbc_iv, DES_ENCRYPT);
    }
    printf("\noutput word alignment test");
    for (i = 0; i < 4; i++) {
        printf(" %d", i);
        DES_ncbc_encrypt(cbc_out, &(cbc_in[i]),
                         strlen((char *)cbc_data) + 1, &ks,
                         &cbc_iv, DES_ENCRYPT);
    }
    printf("\n");
    printf("fast crypt test ");
    str = crypt("testing", "ef");
    if (strcmp("efGnQx2725bI2", str) != 0) {
        printf("fast crypt error, %s should be efGnQx2725bI2\n", str);
        err = 1;
    }
    str = crypt("bca76;23", "yA");
    if (strcmp("yA1Rp/1hZXIJk", str) != 0) {
        printf("fast crypt error, %s should be yA1Rp/1hZXIJk\n", str);
        err = 1;
    }
    printf("\n");
    return (err);
}
コード例 #26
0
ファイル: test.c プロジェクト: GreenLunar/smaFS
int openssl_test()
{
    EVP_MD_CTX md_ctx;
    testVector a, b, c;
    byte       hash[SHA_DIGEST_SIZE];

    a.input  = "1234567890123456789012345678901234567890123456789012345678"
               "9012345678901234567890";
    a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
               "\x7a";
    a.inLen  = strlen(a.input);
    a.outLen = strlen(a.output);

    EVP_MD_CTX_init(&md_ctx);
    EVP_DigestInit(&md_ctx, EVP_md5());

    EVP_DigestUpdate(&md_ctx, a.input, a.inLen);
    EVP_DigestFinal(&md_ctx, hash, 0);

    if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0)
        return -71;

    b.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
               "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
               "aaaaaaaaaa";
    b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
               "\x53\x99\x5E\x26\xA0";
    b.inLen  = strlen(b.input);
    b.outLen = strlen(b.output);

    EVP_MD_CTX_init(&md_ctx);
    EVP_DigestInit(&md_ctx, EVP_sha1());

    EVP_DigestUpdate(&md_ctx, b.input, b.inLen);
    EVP_DigestFinal(&md_ctx, hash, 0);

    if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0)
        return -72;

    if (RAND_bytes(hash, sizeof(hash)) != 1)
        return -73;
            
    c.input  = "what do ya want for nothing?";
    c.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
               "\x38";
    c.inLen  = strlen(c.input);
    c.outLen = strlen(c.output);

    HMAC(EVP_md5(), "Jefe", 4, (byte*)c.input, (int)c.inLen, hash, 0);

    if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0)
        return -74;

    { /* des test */
    const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
        0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
        0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
        0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
    };

    byte plain[24];
    byte cipher[24];

    const_DES_cblock key = 
    {
        0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
    };

    DES_cblock iv = 
    {
        0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
    };

    DES_key_schedule sched;

    const byte verify[] = 
    {
        0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
        0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
        0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
    };

    DES_key_sched(&key, &sched);

    DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
    DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);

    if (memcmp(plain, vector, sizeof(vector)) != 0)
        return -75;

    if (memcmp(cipher, verify, sizeof(verify)) != 0)
        return -76;

        /* test changing iv */
    DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
    DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);

    if (memcmp(cipher, verify, sizeof(verify)) != 0)
        return -77;

    }  /* end des test */

    return 0;
}
コード例 #27
0
_SCAPI_NOT_CONFIGURED
#endif                          /* NETSNMP_USE_INTERNAL_MD5 */
/*******************************************************************-o-******
 * sc_encrypt
 *
 * Parameters:
 *	 privtype	Type of privacy cryptographic transform.
 *	*key		Key bits for crypting.
 *	 keylen		Length of key (buffer) in bytes.
 *	*iv		IV bits for crypting.
 *	 ivlen		Length of iv (buffer) in bytes.
 *	*plaintext	Plaintext to crypt.
 *	 ptlen		Length of plaintext.
 *	*ciphertext	Ciphertext to crypt.
 *	*ctlen		Length of ciphertext.
 *      
 * Returns:
 *	SNMPERR_SUCCESS			Success.
 *	SNMPERR_SC_NOT_CONFIGURED	Encryption is not supported.
 *	SNMPERR_SC_GENERAL_FAILURE	Any other error
 *
 *
 * Encrypt plaintext into ciphertext using key and iv.
 *
 * ctlen contains actual number of crypted bytes in ciphertext upon
 * successful return.
 */
int
sc_encrypt(const oid * privtype, size_t privtypelen,
           u_char * key, u_int keylen,
           u_char * iv, u_int ivlen,
           u_char * plaintext, u_int ptlen,
           u_char * ciphertext, size_t * ctlen)
#if defined(NETSNMP_USE_OPENSSL)
{
    int             rval = SNMPERR_SUCCESS;
    u_int           properlength = 0, properlength_iv = 0;
    u_char          pad_block[128];      /* bigger than anything I need */
    u_char          my_iv[128];  /* ditto */
    int             pad, plast, pad_size = 0;
    int             have_trans;
#ifndef NETSNMP_DISABLE_DES
#ifdef OLD_DES
    DES_key_schedule key_sch;
#else
    DES_key_schedule key_sched_store;
    DES_key_schedule *key_sch = &key_sched_store;
#endif
    DES_cblock       key_struct;
#endif
#ifdef HAVE_AES
    AES_KEY aes_key;
    int new_ivlen = 0;
#endif

    DEBUGTRACE;

    /*
     * Sanity check.
     */
#if	!defined(NETSNMP_ENABLE_SCAPI_AUTHPRIV)
    snmp_log(LOG_ERR, "Encryption support not enabled.\n");
    return SNMPERR_SC_NOT_CONFIGURED;
#endif

    if (!privtype || !key || !iv || !plaintext || !ciphertext || !ctlen
        || (keylen <= 0) || (ivlen <= 0) || (ptlen <= 0) || (*ctlen <= 0)
        || (privtypelen != USM_LENGTH_OID_TRANSFORM)) {
        QUITFUN(SNMPERR_GENERR, sc_encrypt_quit);
    } else if (ptlen > *ctlen) {
        QUITFUN(SNMPERR_GENERR, sc_encrypt_quit);
    }
#ifdef NETSNMP_ENABLE_TESTING_CODE
    {
        size_t          buf_len = 128, out_len = 0;
        u_char         *buf = (u_char *) malloc(buf_len);

        if (buf != NULL) {
            if (sprint_realloc_hexstring(&buf, &buf_len, &out_len, 1,
                                         iv, ivlen)) {
                DEBUGMSGTL(("scapi", "encrypt: IV: %s/", buf));
            } else {
                DEBUGMSGTL(("scapi", "encrypt: IV: %s [TRUNCATED]/", buf));
            }
            out_len = 0;
            if (sprint_realloc_hexstring(&buf, &buf_len, &out_len, 1,
                                         key, keylen)) {
                DEBUGMSG(("scapi", "%s\n", buf));
            } else {
                DEBUGMSG(("scapi", "%s [TRUNCATED]\n", buf));
            }
            out_len = 0;
            if (sprint_realloc_hexstring(&buf, &buf_len, &out_len, 1,
                                         plaintext, 16)) {
                DEBUGMSGTL(("scapi", "encrypt: string: %s\n", buf));
            } else {
                DEBUGMSGTL(("scapi", "encrypt: string: %s [TRUNCATED]\n",
                            buf));
            }
            free(buf);
        } else {
            DEBUGMSGTL(("scapi",
                        "encrypt: malloc fail for debug output\n"));
        }
    }
#endif                          /* NETSNMP_ENABLE_TESTING_CODE */


    /*
     * Determine privacy transform.
     */
    have_trans = 0;
#ifndef NETSNMP_DISABLE_DES
    if (ISTRANSFORM(privtype, DESPriv)) {
        properlength = BYTESIZE(SNMP_TRANS_PRIVLEN_1DES);
        properlength_iv = BYTESIZE(SNMP_TRANS_PRIVLEN_1DES_IV);
        pad_size = properlength;
        have_trans = 1;
    }
#endif
#ifdef HAVE_AES
    if (ISTRANSFORM(privtype, AESPriv)) {
        properlength = BYTESIZE(SNMP_TRANS_PRIVLEN_AES);
        properlength_iv = BYTESIZE(SNMP_TRANS_PRIVLEN_AES_IV);
        have_trans = 1;
    }
#endif
    if (!have_trans) {
        QUITFUN(SNMPERR_GENERR, sc_encrypt_quit);
    }

    if ((keylen < properlength) || (ivlen < properlength_iv)) {
        QUITFUN(SNMPERR_GENERR, sc_encrypt_quit);
    }

    memset(my_iv, 0, sizeof(my_iv));

#ifndef NETSNMP_DISABLE_DES
    if (ISTRANSFORM(privtype, DESPriv)) {

        /*
         * now calculate the padding needed 
         */
        pad = pad_size - (ptlen % pad_size);
        plast = (int) ptlen - (pad_size - pad);
        if (pad == pad_size)
            pad = 0;
        if (ptlen + pad > *ctlen) {
            QUITFUN(SNMPERR_GENERR, sc_encrypt_quit);    /* not enough space */
        }
        if (pad > 0) {              /* copy data into pad block if needed */
            memcpy(pad_block, plaintext + plast, pad_size - pad);
            memset(&pad_block[pad_size - pad], pad, pad);   /* filling in padblock */
        }

        memcpy(key_struct, key, sizeof(key_struct));
        (void) DES_key_sched(&key_struct, key_sch);

        memcpy(my_iv, iv, ivlen);
        /*
         * encrypt the data 
         */
        DES_ncbc_encrypt(plaintext, ciphertext, plast, key_sch,
                         (DES_cblock *) my_iv, DES_ENCRYPT);
        if (pad > 0) {
            /*
             * then encrypt the pad block 
             */
            DES_ncbc_encrypt(pad_block, ciphertext + plast, pad_size,
                             key_sch, (DES_cblock *) my_iv, DES_ENCRYPT);
            *ctlen = plast + pad_size;
        } else {
            *ctlen = plast;
        }
    }
#endif
#ifdef HAVE_AES
    if (ISTRANSFORM(privtype, AESPriv)) {
        (void) AES_set_encrypt_key(key, properlength*8, &aes_key);

        memcpy(my_iv, iv, ivlen);
        /*
         * encrypt the data 
         */
        AES_cfb128_encrypt(plaintext, ciphertext, ptlen,
                           &aes_key, my_iv, &new_ivlen, AES_ENCRYPT);
        *ctlen = ptlen;
    }
#endif
  sc_encrypt_quit:
    /*
     * clear memory just in case 
     */
    memset(my_iv, 0, sizeof(my_iv));
    memset(pad_block, 0, sizeof(pad_block));
#ifndef NETSNMP_DISABLE_DES
    memset(key_struct, 0, sizeof(key_struct));
#ifdef OLD_DES
    memset(&key_sch, 0, sizeof(key_sch));
#else
    memset(&key_sched_store, 0, sizeof(key_sched_store));
#endif
#endif
#ifdef HAVE_AES
    memset(&aes_key,0,sizeof(aes_key));
#endif
    return rval;

}                               /* end sc_encrypt() */