示例#1
0
int
main(int argc, char **argv)
{
    RC2_KEY key;
    unsigned char t[8];
    unsigned char out[40];
    int i;

    for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
	RC2_set_key(&key, tests[i].keylen, tests[i].key, tests[i].bitsize);

	memcpy(t, tests[i].plain, 8);
	RC2_encryptc(t, t, &key);
	if (memcmp(t, tests[i].cipher, 8) != 0) {
	    printf("encrypt %d\n", i);
	    exit(1);
	}
	RC2_decryptc(t, t, &key);
	if (memcmp(t, tests[i].plain, 8) != 0) {
	    printf("decrypt: %d\n", i);
	    exit(1);
	}
    }

    /* cbc test */

    RC2_set_key(&key, 16, cbc_key, 0);
    memcpy(t, cbc_iv, 8);
    RC2_cbc_encrypt(cbc_in_data, out, 32, &key, t, 1);

    if (memcmp(out_iv, t, 8) != 0)
	abort();

    if (memcmp(out, cbc_out_data, 32) != 0) {
	printf("cbc test encrypt\n");
	exit(1);
    }

    memcpy(t, cbc_iv, 8);
    RC2_cbc_encrypt(out, out, 32, &key, t, 0);

    if (memcmp(cbc_in_data, out, 32) != 0) {
	printf("cbc test decrypt \n");
	exit(1);
    }

    return 0;
}
示例#2
0
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
			const unsigned char *iv, int enc)
	{
	RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
		    key,data(ctx)->key_bits);
	return 1;
	}
示例#3
0
/* 
 * Standard CSPContext init, called from CSPFullPluginSession::init().
 * Reusable, e.g., query followed by en/decrypt.
 */
void RC2Context::init( 
	const Context &context, 
	bool encrypting)
{
	CSSM_SIZE	keyLen;
	uint8 		*keyData 	= NULL;
	uint32		effectiveBits;
	
	/* obtain key from context */
	symmetricKeyBits(context, session(), CSSM_ALGID_RC2, 
		encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
		keyData, keyLen);
	if((keyLen < RC2_MIN_KEY_SIZE_BYTES) || (keyLen > RC2_MAX_KEY_SIZE_BYTES)) {
		CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
	}
	
	/* 
	 * Optional effective key size in bits - either from Context,
	 * or the key
	 */
	effectiveBits = context.getInt(CSSM_ATTRIBUTE_EFFECTIVE_BITS);
	if(effectiveBits == 0) {
		CssmKey &key = context.get<CssmKey>(CSSM_ATTRIBUTE_KEY, 
			CSSMERR_CSP_MISSING_ATTR_KEY);
		effectiveBits = key.KeyHeader.LogicalKeySizeInBits;
	}

	/* init the low-level state */
	RC2_set_key(&rc2Key, keyLen, keyData, effectiveBits);

	/* Finally, have BlockCryptor do its setup */
	setup(RC2_BLOCK_SIZE_BYTES, context);
}	
示例#4
0
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                        const unsigned char *iv, int enc)
{
    RC2_set_key(&(ctx->c.rc2.ks),EVP_CIPHER_CTX_key_length(ctx),
                key,ctx->c.rc2.key_bits);
    return 1;
}
示例#5
0
static int
rc2_init(EVP_CIPHER_CTX *ctx,
	 const unsigned char * key,
	 const unsigned char * iv,
	 int encp)
{
    struct rc2_cbc *k = ctx->cipher_data;
    k->maximum_effective_key = EVP_CIPHER_CTX_key_length(ctx) * 8;
    RC2_set_key(&k->key,
		EVP_CIPHER_CTX_key_length(ctx),
		key,
		k->maximum_effective_key);
    return 1;
}
示例#6
0
QVariantMap jsBridge::encrypt(QString data)
{
    QVariantMap passJson;
    passJson["ok"] = false;

    PasswordDialog pwd(qApp->activeWindow(), true);
    if (pwd.exec() != QDialog::Accepted)
        return passJson;

    passJson["hint"] = pwd.getHint();

    RC2_KEY key;
    QByteArray r = QCryptographicHash::hash(pwd.getPassword().toUtf8(), QCryptographicHash::Md5);
    RC2_set_key(&key, r.size(), (const unsigned char*)r.data(), 64);

    QByteArray b = data.toUtf8();

    char null_ = 0;
    while (((b.size() + 4) % 8) != 0)
        b.append(null_);

    ulong crc = crc32(0, NULL, 0);
    crc = crc32(crc, (const Bytef *)b.data(), b.size());
    QString crc2 = QString(QByteArray::number(~(uint)crc, 16).left(4)).toUpper();

    b = crc2.toLatin1() + b;

    unsigned char * buf2 = new unsigned char[8];

    QByteArray result;

    while (b.size()>0) {
        QByteArray x = b.left(8);
        RC2_ecb_encrypt((const unsigned char*)x.data(),buf2,&key,RC2_ENCRYPT);
        result += QByteArray((const char *)buf2, 8);
        b.remove(0, 8);
    }

    if (!result.isEmpty()){
        passJson["data"] = QString::fromLatin1(result.toBase64());
        passJson["ok"] = true;
    }

    return passJson;
}
示例#7
0
QString jsBridge::decrypt(QString data, QString hint)
{
    bool error = false;
    while (true) {
        PasswordDialog pwd(qApp->activeWindow(), false);
        pwd.setHint(hint);
        pwd.setError(error);
        if (pwd.exec() == QDialog::Accepted){

            RC2_KEY key;
            QByteArray b = QByteArray::fromBase64(data.toLatin1());

            QByteArray r = QCryptographicHash::hash(pwd.getPassword().toUtf8(), QCryptographicHash::Md5);
            RC2_set_key(&key, r.size(), (const unsigned char*)r.data(), 64);

            unsigned char * buf2 = new unsigned char[8];

            QByteArray result;

            while (b.size()>0) {
                QByteArray x = b.left(8);
                RC2_ecb_encrypt((const unsigned char*)x.data(),buf2,&key,RC2_DECRYPT);
                result += QByteArray((const char *)buf2, x.size());
                b.remove(0, 8);
            }

            QString crc1 = QString::fromUtf8(result.left(4));
            result.remove(0, 4);

            ulong crc = crc32(0, NULL, 0);
            crc = crc32(crc, (const Bytef *)result.data(), result.size());
            QString crc2 = QString(QByteArray::number(~(uint)crc, 16).left(4));

            if (QString::compare(crc1, crc2, Qt::CaseInsensitive) != 0){
                error = true;
                continue;
            }

            return result;
        }
        else
            return QString();
    }
    return QString();
}
示例#8
0
文件: rc2test.c 项目: Lukasa/openssl
static int test_rc2(const int n)
{
    int testresult = 1;
    RC2_KEY key;
    unsigned char buf[8], buf2[8];

    RC2_set_key(&key, 16, &(RC2key[n][0]), 0 /* or 1024 */ );

    RC2_ecb_encrypt(&RC2plain[n][0], buf, &key, RC2_ENCRYPT);
    if (!TEST_mem_eq(&RC2cipher[n][0], 8, buf, 8))
        testresult = 0;

    RC2_ecb_encrypt(buf, buf2, &key, RC2_DECRYPT);
    if (!TEST_mem_eq(&RC2plain[n][0], 8, buf2, 8))
        testresult = 0;

    return testresult;
}
示例#9
0
/* {{{ CI_Ceay_DecryptInit */
CK_DEFINE_FUNCTION(CK_RV, CI_Ceay_DecryptInit)(
  CK_I_SESSION_DATA_PTR  session_data,
  CK_MECHANISM_PTR  pMechanism,     /* the decryption mechanism */
  CK_I_OBJ_PTR      key_obj         /* handle of decryption key */
)
{
  CK_RV rv = CKR_OK;

  CI_LogEntry("C_DecryptInit", "starting...", rv , 0);	  

  switch(pMechanism->mechanism)
    {

      /* {{{ CKM_RSA_PKCS */
    case CKM_RSA_PKCS:
      {
	RSA CK_PTR internal_key_obj = NULL_PTR;

	/* check that object is a private key */
	if((CI_ObjLookup(key_obj,CK_IA_CLASS) == NULL_PTR) || 
	   (*((CK_OBJECT_CLASS CK_PTR)CI_ObjLookup(key_obj,CK_IA_CLASS)->pValue) != CKO_PRIVATE_KEY))
	  return CKR_KEY_TYPE_INCONSISTENT;

	CI_LogEntry("C_Ceay_DecryptInit", "RSA PKCS starting", rv, 2);	  

	internal_key_obj = CI_Ceay_Obj2RSA(key_obj);
	if(internal_key_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;
      
	session_data->decrypt_state = (CK_VOID_PTR)internal_key_obj;
	session_data->decrypt_mechanism = CKM_RSA_PKCS;
      }
      break;

      /* }}} */
      /* {{{ CKM_RSA_X_509 */
    case CKM_RSA_X_509:
      {
	RSA CK_PTR internal_key_obj = NULL_PTR;

	/* check that object is a private key */
	if((CI_ObjLookup(key_obj,CK_IA_CLASS) == NULL_PTR) || 
	   (*((CK_OBJECT_CLASS CK_PTR)CI_ObjLookup(key_obj,CK_IA_CLASS)->pValue) != CKO_PRIVATE_KEY))
	  return CKR_KEY_TYPE_INCONSISTENT;

	CI_LogEntry("C_Ceay_DecryptInit", "RSA X509 starting", rv, 2);

	internal_key_obj = CI_Ceay_Obj2RSA(key_obj);
	if(internal_key_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;
      
	session_data->decrypt_state = (CK_VOID_PTR)internal_key_obj;
	session_data->decrypt_mechanism = CKM_RSA_X_509;
      }
      break;
      /* }}} */
      /* {{{ CKM_RC4 */
    case CKM_RC4:
      {
	RC4_KEY CK_PTR internal_obj = NULL_PTR;
	CK_ULONG key_len;

	CI_LogEntry("C_Ceay_DecryptInit", "RC4 starting", rv, 2);	  

	internal_obj = CI_RC4Key_new();
	if(internal_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;

	if(CI_ObjLookup(key_obj,CK_IA_VALUE_LEN) != NULL_PTR)
	  key_len = *((CK_ULONG_PTR)(CI_ObjLookup(key_obj,CK_IA_VALUE_LEN)->pValue));
	else
	  key_len = (CI_ObjLookup(key_obj,CK_IA_VALUE)->ulValueLen);

	RC4_set_key(internal_obj,
		    key_len,
		    CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue);

	session_data->decrypt_state = (CK_VOID_PTR)internal_obj;
	session_data->decrypt_mechanism = CKM_RC4;

      }
      break;
      /* }}} */
      /* {{{ CKM_RC2_ECB */
    case CKM_RC2_ECB:
      {
	RC2_KEY CK_PTR internal_obj = NULL_PTR;
	CK_ULONG key_len;

	/* check correct parameter */
	if((pMechanism->pParameter == NULL_PTR) ||
	   (pMechanism->ulParameterLen != sizeof(CK_RC2_PARAMS)))
	  return CKR_MECHANISM_PARAM_INVALID;

	CI_LogEntry("C_CL_DecryptInit", "RC2 ECB starting", rv, 2);	  

	internal_obj = CI_RC2Key_new();
	if(internal_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;

	if(CI_ObjLookup(key_obj,CK_IA_VALUE_LEN) != NULL_PTR)
	  key_len = *((CK_ULONG_PTR)(CI_ObjLookup(key_obj,CK_IA_VALUE_LEN)->pValue));
	else
	  key_len = CI_ObjLookup(key_obj,CK_IA_VALUE)->ulValueLen;

	RC2_set_key(internal_obj,
		    key_len,
		    CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue,
		    (int)pMechanism->pParameter);

	session_data->decrypt_state = (CK_VOID_PTR)internal_obj;
	session_data->decrypt_mechanism = CKM_RC2_ECB;
      }
      break;
      /* }}} */
      /* {{{ CKM_RC2_CBC */
    case CKM_RC2_CBC:
      {
	CK_I_CEAY_RC2_INFO_PTR internal_obj = NULL_PTR;
	CK_ULONG key_len;
	
	CK_RC2_CBC_PARAMS_PTR para = pMechanism->pParameter; 

	rv = CKR_OK;

	internal_obj= CI_RC2_INFO_new();
	if(internal_obj == NULL_PTR)
	  {
	    rv = CKR_HOST_MEMORY;
	    goto decrypt_init_rc2_error;
	  }

	CI_LogEntry("C_CL_DecryptInit", "RC2 CBC starting", rv, 2);

	/* Ok, alles alloziert, jetzt die wirklichen Werte eintragen */
	/* Mechanism zuerst, denn da kann ja der Parameter fehlen */
	if((pMechanism->pParameter == NULL_PTR) ||
	   (pMechanism->ulParameterLen != sizeof(CK_RC2_CBC_PARAMS)))
	  {
	    rv = CKR_MECHANISM_PARAM_INVALID;
	    goto decrypt_init_rc2_error;
	  }
	/* Na da wolle wir mal besser sicher gehen das ceay und pkcs11 vom gleichen reden! */
	assert(sizeof(CK_BYTE) == sizeof(unsigned char));
	memcpy(internal_obj->ivec,para->iv, sizeof(CK_BYTE)*8);
	{
	  CK_BYTE_PTR tmp_str = NULL_PTR;
	  
	  CI_VarLogEntry("CI_Ceay_EncryptInit", "RC2 CBC ivec: %s", rv, 2,
			 tmp_str = CI_PrintableByteStream(internal_obj->ivec,
							  sizeof(CK_BYTE)*8));
	  TC_free(tmp_str);
	}

	if(CI_ObjLookup(key_obj,CK_IA_VALUE_LEN) != NULL_PTR)
	  key_len = *((CK_ULONG_PTR)(CI_ObjLookup(key_obj,CK_IA_VALUE_LEN)->pValue));
	else
	  key_len = CI_ObjLookup(key_obj,CK_IA_VALUE)->ulValueLen;

	/* TODO: check that the size of the effective Bits are valid */
	RC2_set_key(internal_obj->key,
		    key_len,
		    (unsigned char*)CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue,
		    (int)para->ulEffectiveBits);

	session_data->decrypt_state = internal_obj;
	session_data->decrypt_mechanism = CKM_RC2_CBC;


      decrypt_init_rc2_error:
	if(rv != CKR_OK)
	  {
	    if(internal_obj->key) TC_free(internal_obj->key);
	    if(internal_obj) TC_free(internal_obj);
	  }
      }
      break;
      /* }}} */
      /* {{{ CKM_DES_ECB */
    case CKM_DES_ECB:
      {
	des_key_schedule CK_PTR internal_obj = NULL_PTR;
	CK_BYTE_PTR tmp_key_data;

	internal_obj = TC_malloc(sizeof(des_key_schedule));
	if(internal_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;

	CI_LogEntry("C_CL_DecryptInit", "DES ECB starting", rv, 2);	  

	tmp_key_data = CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue;
	if(tmp_key_data == NULL_PTR)
	  return CKR_KEY_TYPE_INCONSISTENT;

	des_set_key((des_cblock*)tmp_key_data,
		    *((des_key_schedule CK_PTR)internal_obj));

	session_data->decrypt_state = (CK_VOID_PTR)internal_obj;
	session_data->decrypt_mechanism = CKM_DES_ECB;
      }
      break;
      /* }}} */
      /* {{{ CKM_DES_CBC */
    case CKM_DES_CBC:
      {
	CK_I_CEAY_DES_INFO_PTR internal_obj = NULL_PTR;
	CK_BYTE_PTR tmp_key_data;

	rv = CKR_OK; /* positiv denken */

	CI_LogEntry("C_CL_DecryptInit", "DES CBC starting", rv, 2);	  

	internal_obj= CI_DES_INFO_new();
	if(internal_obj == NULL_PTR)
	  {
	    rv = CKR_HOST_MEMORY;
	    goto decrypt_init_des_error;
	  }

	/* Ok, alles alloziert, jetzt die wirklichen Werte eintragen */
	/* Mechanism zuerst, denn da kann ja der Parameter fehlen */
	if((pMechanism->pParameter == NULL_PTR) ||
	   (pMechanism->ulParameterLen != sizeof(des_cblock)))
	  {
	    rv = CKR_MECHANISM_PARAM_INVALID;
	    goto decrypt_init_des_error;
	  }
	memcpy(internal_obj->ivec,pMechanism->pParameter, sizeof(des_cblock));
	{
	  CK_BYTE_PTR tmp_str = NULL_PTR;
	  
	  CI_VarLogEntry("CI_Ceay_EncryptInit", "DES CBC ivec: %s", rv, 2,
			 tmp_str = CI_PrintableByteStream((CK_BYTE_PTR)internal_obj->ivec,
							  sizeof(des_cblock)));
	  TC_free(tmp_str);
	}

	tmp_key_data = CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue;
	if(tmp_key_data == NULL_PTR) return CKR_KEY_TYPE_INCONSISTENT;

	des_set_key((des_cblock*)tmp_key_data,internal_obj->sched);

	session_data->decrypt_state = internal_obj;
	session_data->decrypt_mechanism = CKM_DES_CBC;

	/* ugly global value from the ceay lib */
	des_rw_mode = DES_CBC_MODE;

      decrypt_init_des_error:
	if(rv != CKR_OK)
	  CI_DES_INFO_delete(internal_obj);
      }
      break;
      /* }}} */
      /* {{{ CKM_DES_CBC_PAD */
    case CKM_DES_CBC_PAD:
      {
	CK_I_CEAY_DES_INFO_PTR internal_obj = NULL_PTR;
	CK_BYTE_PTR tmp_key_data;

	rv = CKR_OK; /* positiv denken */

	internal_obj= CI_DES_INFO_new();
	if(internal_obj == NULL_PTR)
	  {
	    rv = CKR_HOST_MEMORY;
	    goto decrypt_init_des_error;
	  }

	/* Ok, alles alloziert, jetzt die wirklichen Werte eintragen */
	/* Mechanism zuerst, denn da kann ja der Parameter fehlen */
	if((pMechanism->pParameter == NULL_PTR) ||
	   (pMechanism->ulParameterLen != sizeof(des_cblock)))
	  {
	    rv = CKR_MECHANISM_PARAM_INVALID;
	    goto decrypt_init_des_pad_error;
	  }
	memcpy(internal_obj->ivec,pMechanism->pParameter, sizeof(des_cblock));
	{
	  CK_BYTE_PTR tmp_str = NULL_PTR;
	  
	  CI_VarLogEntry("CI_Ceay_EncryptInit", "DES CBC PAD ivec: %s", rv, 2,
			 tmp_str = CI_PrintableByteStream((CK_BYTE_PTR)internal_obj->ivec,
							  sizeof(des_cblock)));
	  TC_free(tmp_str);
	}

	tmp_key_data = CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue;
	if(tmp_key_data == NULL_PTR) return CKR_KEY_TYPE_INCONSISTENT;

	des_set_key((des_cblock*)tmp_key_data,internal_obj->sched);

	session_data->decrypt_state = internal_obj;
	session_data->decrypt_mechanism = CKM_DES_CBC_PAD;
	((CK_I_CEAY_DES_INFO_PTR)session_data->decrypt_state)->pad = 0;
	memset(((CK_I_CEAY_DES_INFO_PTR)session_data->decrypt_state)->lastblock, 0, 8);
	/* ugly global value from the ceay lib */
	des_rw_mode = DES_CBC_MODE;

      decrypt_init_des_pad_error:
	if(rv != CKR_OK)
	  CI_DES_INFO_delete(internal_obj);
      }
      break;
      /* }}} */
      /* {{{ CKM_DES3_ECB */
    case CKM_DES3_ECB:
      {
	CK_I_CEAY_DES3_INFO_PTR internal_obj = NULL_PTR;
	
	CI_LogEntry("C_CL_DecryptInit", "DES3 ECB starting", rv, 2);	  

	/* TODO: change this into des_cblock[3] */
	internal_obj = CI_DES3_INFO_new(CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue);
	if(internal_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;


	session_data->decrypt_state = (CK_VOID_PTR)internal_obj;
	session_data->decrypt_mechanism = CKM_DES3_ECB;
      }
      break;
      /* }}} */
      /* {{{ CKM_DES3_CBC */
    case CKM_DES3_CBC:
      {
	CK_I_CEAY_DES3_INFO_PTR internal_obj = NULL_PTR;

	rv = CKR_OK; /* positiv denken */

	/* Mechanism zuerst prüfen, denn da kann ja der Parameter fehlen */
	if((pMechanism->pParameter == NULL_PTR) ||
	   (pMechanism->ulParameterLen != sizeof(des_cblock)))
	  return CKR_MECHANISM_PARAM_INVALID;

	CI_LogEntry("C_CL_DecryptInit", "DES3 CBC/PAD starting", rv, 2);	  

	internal_obj= CI_DES3_INFO_new(CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue);
	if(internal_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;

	/* Ok, alles alloziert, jetzt die wirklichen Werte eintragen */
	memcpy(internal_obj->ivec,pMechanism->pParameter, sizeof(des_cblock));
	{
	  CK_BYTE_PTR tmp_str = NULL_PTR;
	  
	  CI_VarLogEntry("CI_Ceay_EncryptInit", "DES3 CBC ivec: %s", rv, 2,
			 tmp_str = CI_PrintableByteStream((CK_BYTE_PTR)internal_obj->ivec,
							  sizeof(des_cblock)));
	  TC_free(tmp_str);
	}

	session_data->decrypt_state = internal_obj;
	session_data->decrypt_mechanism = CKM_DES3_CBC;

	/* ugly global value from the ceay lib */
	des_rw_mode = DES_CBC_MODE;

      }
      break;
      /* }}} */
      /* {{{ CKM_IDEA_ECB */
    case CKM_IDEA_ECB:
      {
	IDEA_KEY_SCHEDULE CK_PTR internal_obj = NULL_PTR;
	IDEA_KEY_SCHEDULE temp_sched;

	CI_LogEntry("C_CL_DecryptInit", "IDEA ECB starting", rv, 2);	  

	internal_obj = CI_IDEA_KEY_SCHEDULE_new();
	if(internal_obj == NULL_PTR)
	  return CKR_HOST_MEMORY;

	/* 
	 * This is the IDEA way: generate an encryption key and then convert
	 * it to a decrypt key 
	 */
	idea_set_encrypt_key(CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue,
			     &temp_sched);
	idea_set_decrypt_key(&temp_sched,internal_obj);

	session_data->decrypt_state = (CK_VOID_PTR)internal_obj;
	session_data->decrypt_mechanism = CKM_IDEA_ECB;

	rv = CKR_OK;

      }
      break;
      /* }}} */
      /* {{{ CKM_IDEA_CBC */
    case CKM_IDEA_CBC:
      {
	CK_I_CEAY_IDEA_INFO_PTR internal_obj = NULL_PTR;
	IDEA_KEY_SCHEDULE temp_sched;

	rv = CKR_OK; /* positiv denken */

	/* check Mechanism first, there might be a parameter missing */
	if((pMechanism->pParameter == NULL_PTR) ||
	   (pMechanism->ulParameterLen != sizeof(des_cblock)))
	    return CKR_MECHANISM_PARAM_INVALID;

	CI_LogEntry("C_CL_DecryptInit", "IDEA CBC starting", rv, 2);	  

	internal_obj= CI_IDEA_INFO_new();
	if(internal_obj == NULL_PTR)
	    return CKR_HOST_MEMORY;

	/* Ok, alles alloziert, jetzt die wirklichen Werte eintragen */
	memcpy(internal_obj->ivec,pMechanism->pParameter, sizeof(des_cblock));
	{
	  CK_BYTE_PTR tmp_str = NULL_PTR;
	  
	  CI_VarLogEntry("CI_Ceay_EncryptInit", "IDEA CBC ivec: %s", rv, 2,
			 tmp_str = CI_PrintableByteStream(internal_obj->ivec,
							  sizeof(des_cblock)));
	  TC_free(tmp_str);
	}

	idea_set_encrypt_key(CI_ObjLookup(key_obj,CK_IA_VALUE)->pValue,&temp_sched);
	idea_set_decrypt_key(&temp_sched,&(internal_obj->sched));

	session_data->decrypt_state = internal_obj;
	session_data->decrypt_mechanism = CKM_IDEA_CBC;
      }
      break;
      /* }}} */
    default:
      rv = CKR_MECHANISM_INVALID;

      CI_VarLogEntry("C_DecryptInit", "algorithm specified: %s", rv, 0, 
		     CI_MechanismStr(pMechanism->mechanism));

    }
  
  return rv;
}
示例#10
0
文件: rc2speed.c 项目: 0b10011/node
int main(int argc, char **argv)
	{
	long count;
	static unsigned char buf[BUFSIZE];
	static unsigned char key[] ={
			0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
			0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
			};
	RC2_KEY sch;
	double a,b,c,d;
#ifndef SIGALRM
	long ca,cb,cc;
#endif

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

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

		count*=2;
		Time_F(START);
		for (i=count; i; i--)
			RC2_encrypt(data,&sch);
		d=Time_F(STOP);
		} while (d < 3.0);
	ca=count/512;
	cb=count;
	cc=count*8/BUFSIZE+1;
	printf("Doing RC2_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 RC2_set_key for 10 seconds\n");
	alarm(10);
#endif

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

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

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

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

	printf("RC2 set_key       per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
	printf("RC2 raw ecb bytes per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
	printf("RC2 cbc     bytes per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
	exit(0);
#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
	return(0);
#endif
	}
示例#11
0
int ssl_test_rc2(int argc, char *argv[])
	{
	int i,n,err=0;
	RC2_KEY key; 
	unsigned char buf[8],buf2[8];

	for (n=0; n<4; n++)
		{
		RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */);

		RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT);
		if (TINYCLR_SSL_MEMCMP(&(RC2cipher[n][0]),buf,8) != 0)
			{
			TINYCLR_SSL_PRINTF("ecb rc2 error encrypting\n");
			TINYCLR_SSL_PRINTF("got     :");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",buf[i]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expected:");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",RC2cipher[n][i]);
			err=20;
			TINYCLR_SSL_PRINTF("\n");
			}

		RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT);
		if (TINYCLR_SSL_MEMCMP(&(RC2plain[n][0]),buf2,8) != 0)
			{
			TINYCLR_SSL_PRINTF("ecb RC2 error decrypting\n");
			TINYCLR_SSL_PRINTF("got     :");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",buf[i]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expected:");
			for (i=0; i<8; i++)
				TINYCLR_SSL_PRINTF("%02X ",RC2plain[n][i]);
			TINYCLR_SSL_PRINTF("\n");
			err=3;
			}
		}

	if (err == 0) TINYCLR_SSL_PRINTF("ecb RC2 ok\n");
#ifdef undef
	TINYCLR_SSL_MEMCPY(iv,k,8);
	idea_cbc_encrypt((unsigned char *)text,out,TINYCLR_SSL_STRLEN(text)+1,&key,iv,1);
	TINYCLR_SSL_MEMCPY(iv,k,8);
	idea_cbc_encrypt(out,out,8,&dkey,iv,0);
	idea_cbc_encrypt(&(out[8]),&(out[8]),TINYCLR_SSL_STRLEN(text)+1-8,&dkey,iv,0);
	if (TINYCLR_SSL_MEMCMP(text,out,TINYCLR_SSL_STRLEN(text)+1) != 0)
		{
		TINYCLR_SSL_PRINTF("cbc idea bad\n");
		err=4;
		}
	else
		TINYCLR_SSL_PRINTF("cbc idea ok\n");

	TINYCLR_SSL_PRINTF("cfb64 idea ");
	if (cfb64_test(cfb_cipher64))
		{
		TINYCLR_SSL_PRINTF("bad\n");
		err=5;
		}
	else
		TINYCLR_SSL_PRINTF("ok\n");
#endif

#ifdef OPENSSL_SYS_NETWARE
    if (err) TINYCLR_SSL_PRINTF("ERROR: %d\n", err);
#endif
	return(err);
	}
示例#12
0
文件: rc2test.c 项目: aosm/OpenSSL096
int main(int argc, char *argv[])
	{
	int i,n,err=0;
	RC2_KEY key; 
	unsigned char buf[8],buf2[8];

	for (n=0; n<4; n++)
		{
		RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */);

		RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT);
		if (memcmp(&(RC2cipher[n][0]),buf,8) != 0)
			{
			printf("ecb rc2 error encrypting\n");
			printf("got     :");
			for (i=0; i<8; i++)
				printf("%02X ",buf[i]);
			printf("\n");
			printf("expected:");
			for (i=0; i<8; i++)
				printf("%02X ",RC2cipher[n][i]);
			err=20;
			printf("\n");
			}

		RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT);
		if (memcmp(&(RC2plain[n][0]),buf2,8) != 0)
			{
			printf("ecb RC2 error decrypting\n");
			printf("got     :");
			for (i=0; i<8; i++)
				printf("%02X ",buf[i]);
			printf("\n");
			printf("expected:");
			for (i=0; i<8; i++)
				printf("%02X ",RC2plain[n][i]);
			printf("\n");
			err=3;
			}
		}

	if (err == 0) printf("ecb RC2 ok\n");
#ifdef undef
	memcpy(iv,k,8);
	idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
	memcpy(iv,k,8);
	idea_cbc_encrypt(out,out,8,&dkey,iv,0);
	idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
	if (memcmp(text,out,strlen(text)+1) != 0)
		{
		printf("cbc idea bad\n");
		err=4;
		}
	else
		printf("cbc idea ok\n");

	printf("cfb64 idea ");
	if (cfb64_test(cfb_cipher64))
		{
		printf("bad\n");
		err=5;
		}
	else
		printf("ok\n");
#endif

	EXIT(err);
	return(err);
	}
示例#13
0
int main(void)
{
    int     i;
    RC2_KEY key;
    BIO*    bio_out;

    unsigned char const iv_data[RC2_BLOCK] = {
        0xcc, 0xfe, 0xcd, 0x3e, 0x21, 0xde, 0x1c, 0x31
    };

    static unsigned const char RC2Key[RC2_KEY_LENGTH] = {
        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
        0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    };

    unsigned char   iv[RC2_BLOCK]; 

    char*   data  = "The worthwhile problems are the ones you can"
                    "really solve or help solve, the ones you can"
                    "really contribute something to. No "
                    "problem is too small or too trivial if we "
                    "can really do something about it."
                    "- Richard Feynman";

    /* Round up the length to the higher multiple of 8 */
    int     length = (strlen(data) + (RC2_BLOCK - 1)) & ~(RC2_BLOCK - 1);    


    /* Input pointer to OpenSSL's RC2 CBC method must be a multiple of 8. */
    /* Hence, use the length calcualted above and fill the extra bytes with 0's */
    char*   data_to_encrypt = (char*) calloc(length, sizeof(char)); 

    /* Output pointer to OpenSSL's RC2 CBC method must be a multiple of 8. */
    /* Hence, use the length calcualted above. */
    char*	ciphertext = (char*) malloc(sizeof(char) * length); 
    char*	plaintext  = (char*) malloc(sizeof(char) * length); 

    /* Copy the IV data to the IV array. The IV array will be updated by the RC2_cbc_encrypt call */
    memcpy(iv, iv_data, RC2_BLOCK);

    /* Copy the data to the padded array created earlier */
    memcpy(data_to_encrypt, data, strlen(data));

    /* set the key structure using the (unmodified) predefined key */
    RC2_set_key(&key, RC2_KEY_LENGTH, RC2Key, 1024);
    RC2_cbc_encrypt(data_to_encrypt, ciphertext, length, &key, iv, RC2_ENCRYPT);

    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    BIO_printf(bio_out, "Original plaintext: %s\n\n", data_to_encrypt);

    BIO_printf(bio_out, "Ciphertext: ");

    /* print out the ciphertext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);

    BIO_printf(bio_out, "\n\n");

    /* Copy the original IV data back to the IV array - as it was overwritten during the encryption process */
    memcpy(iv, iv_data, RC2_BLOCK);

    /* start the decryption process */
    RC2_cbc_encrypt(ciphertext, plaintext, length, &key, iv, RC2_DECRYPT);

    BIO_printf(bio_out, "Recovered plaintext: ");

    /* print out the plaintext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);

    BIO_printf(bio_out, "\n\n");

    BIO_free(bio_out);

    free(data_to_encrypt);
    free(ciphertext);
    free(plaintext);

    return 0;

}
示例#14
0
int main(void)
{
    int     i;
    RC2_KEY key;
    BIO*    bio_out;
    int     num_bytes;

    unsigned char const iv_data[RC2_BLOCK] = {
        0xcc, 0xfe, 0xcd, 0x3e, 0x21, 0xde, 0x1c, 0x31
    };

    static unsigned const char RC2Key[RC2_KEY_LENGTH] = {
        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
        0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    };

    unsigned char   iv[RC2_BLOCK]; 

    char*   data  = "The worthwhile problems are the ones you can"
                    "really solve or help solve, the ones you can"
                    "really contribute something to. No "
                    "problem is too small or too trivial if we "
                    "can really do something about it."
                    "- Richard Feynman";

    int     length = strlen(data);    
    
    /* Allocate some memory for the ciphertext */
    unsigned char*  ciphertext = (unsigned char*) malloc(sizeof(char) * length); 
    /* Allocate some memory for the decrypted ciphertext (plaintext) */
    unsigned char*  plaintext  = (unsigned char*) malloc(sizeof(char) * length);
    /* Allocate some memory for the bit-stream */
    unsigned char*  bitstream  = (unsigned char*) malloc(sizeof(char) * length); 

    /* Copy the IV data to the IV array. The IV array will be updated by the RC2_cfb64_encrypt call */
    memcpy(iv, iv_data, RC2_BLOCK);

    /* set the key structure using the (unmodified) predefined key */
    RC2_set_key(&key, RC2_KEY_LENGTH, RC2Key, 1024);
    RC2_ofb64_encrypt(data, ciphertext, length, &key, iv, &num_bytes);

    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    BIO_printf(bio_out, "Original plaintext: %s\n\n", data);

    BIO_printf(bio_out, "Ciphertext: ");

    /* print out the ciphertext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);

    BIO_printf(bio_out, "\n\n");

    /* The decryption process */

    /* Reset the number of bytes used */
    num_bytes = 0;

    /* Copy the original IV data back to the IV array - as it was overwritten during the encryption process */
    memcpy(iv, iv_data, RC2_BLOCK);
    RC2_set_key(&key, RC2_KEY_LENGTH, RC2Key, 1024);

    RC2_ofb64_encrypt(ciphertext, plaintext, length, &key, iv, &num_bytes);

    BIO_printf(bio_out, "Recovered plaintext: ");

    /* print out the plaintext */
    for (i = 0; i < length; i++)
        BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);

    BIO_printf(bio_out, "\n\n");

    BIO_free(bio_out);

    free(ciphertext);
    free(plaintext);

    return 0;


}