예제 #1
0
파일: mytunetsvc.c 프로젝트: alick/mytunet
INT DecodePasswordByUsername(CHAR *username, CHAR *encodedstr, CHAR *decoded)
{
    BYTE md5[16];
    des3_context ctx3;
    BYTE output[16];
    BYTE encoded[16];
	
    CHAR vn[200];
	
#ifdef _WIN32
	DWORD sn;
	GetVolumeInformation("C:\\", vn, sizeof(vn), &sn, NULL, NULL, NULL, 0);
	snprintf(vn, sizeof(vn), "%s%x", username, sn);
#else
	snprintf(vn, sizeof(vn), "%s", username);
#endif
	
    //MD5Buffer(username, strlen(username), md5);
    MD5Buffer(vn, strlen(vn), md5);
	
	
    hex2buf(encodedstr, encoded, NULL);
	
    des3_set_3keys( &ctx3, md5, md5 + 8, md5);
    des3_decrypt( &ctx3, (BYTE *)encoded, output);
	
    des3_set_3keys( &ctx3, md5, md5 + 8, md5);
    des3_decrypt( &ctx3, (BYTE *)encoded + 8, output + 8);
	
    buf2hex(output, 16, decoded);
	
    return OK;
}
예제 #2
0
/**
 * krb5_decrypt_compare                                             // {{{
 *
 */
int krb5_decrypt_compare() {
/* TGT_SIZE is not a multiple of DES block size; add space for one extra
 * DES block to make sure the OpenSSL routines will not overwrite stack
 * space beyond the end of plain[] when they operate on whole DES blocks. */
    char plain[TGT_SIZE + 8];
    int i;

    memset(krb5key->key, 0x00, DES3_KEY_SIZE);
    memset(krb5key->schedule, 0x00, DES3_KEY_SCHED_SIZE);

/* NUL padding is intentional */
    strncpy(username, psalt->user, MAX_USER_LEN);
    strncpy(realm, psalt->realm, MAX_REALM_LEN);
    strncpy(password, skey.passwd, MAX_PASS_LEN);

    // do str2key
    str2key(username, realm, password, krb5key);

/* Possible optimization: we might not have to decrypt the entire thing */
    des3_decrypt(krb5key, psalt->tgt_ebin, plain, TGT_SIZE);

    for(i=0;i<TGT_SIZE;++i)
        if (plain[i] == 'k')
            if (strncmp(plain + i, KRBTGT, strlen(KRBTGT)) == 0) {
/* NUL padding is intentional */
                strncpy(psalt->passwd, skey.passwd, MAX_PASS_LEN);
                return 1;
            }
    return 0;
}
예제 #3
0
int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
                          u8 *plain, size_t len)
{
    size_t i, j, blocks;
    u8 tmp[32];

    switch (ctx->alg) {
    case CRYPTO_CIPHER_ALG_RC4:
        if (plain != crypt)
            os_memcpy(plain, crypt, len);
        rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
                 ctx->u.rc4.used_bytes, plain, len);
        ctx->u.rc4.used_bytes += len;
        break;
    case CRYPTO_CIPHER_ALG_AES:
        if (len % ctx->u.aes.block_size)
            return -1;
        blocks = len / ctx->u.aes.block_size;
        for (i = 0; i < blocks; i++) {
            os_memcpy(tmp, crypt, ctx->u.aes.block_size);
            aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
            for (j = 0; j < ctx->u.aes.block_size; j++)
                plain[j] ^= ctx->u.aes.cbc[j];
            os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
            plain += ctx->u.aes.block_size;
            crypt += ctx->u.aes.block_size;
        }
        break;
    case CRYPTO_CIPHER_ALG_3DES:
        if (len % 8)
            return -1;
        blocks = len / 8;
        for (i = 0; i < blocks; i++) {
            os_memcpy(tmp, crypt, 8);
            des3_decrypt(crypt, &ctx->u.des3.key, plain);
            for (j = 0; j < 8; j++)
                plain[j] ^= ctx->u.des3.cbc[j];
            os_memcpy(ctx->u.des3.cbc, tmp, 8);
            plain += 8;
            crypt += 8;
        }
        break;
    default:
        return -1;
    }

    return 0;
}
예제 #4
0
파일: crypto.c 프로젝트: wfmdyh/wecan
/*****************************************************************************
 函 数 名  : pboc_des3_decrypt
 功能描述  : 以3DES-ECB(EDE)模式解密数据。
 输入参数  : const uint8_t dekey[DOUBLE_KEY_SIZE] 双长度(16字节)解密密钥
 const uint8_t *inbuf 输入密文
 int len 输入密文长度,注意应为8的整数倍
 输出参数  : uint8_t *outbuf 输出明文
 返 回 值  : int  返回 0
 修改历史  :
 *****************************************************************************/
int pboc_des3_decrypt(const UINT8 dekey[DOUBLE_KEY_SIZE], const UINT8 *inbuf,
		int len, UINT8 *outbuf) {
	des3_context ctx = { { 0 } };
	int i;

	des3_set_2keys(&ctx, (UINT8 *) dekey, (UINT8 *) (dekey + DES_KEY_SIZE));

	/* decrypt data in blocks */
	for (i = 0; i < len / DES_BLOCK_SIZE; i++) {
		des3_decrypt(&ctx, (UINT8 *) (inbuf + i * DES_BLOCK_SIZE),
		outbuf + i*DES_BLOCK_SIZE);
	}

	memset(&ctx, 0, sizeof(des3_context));
	return 0;
}
int  fast_crypto_cipher_decrypt(struct crypto_cipher *ctx, const uint8_t *crypt,
			  uint8_t *plain, size_t len)
{
    size_t i, j, blocks;
    uint8_t tmp[32];
    struct fast_crypto_cipher *fast_ctx;

    fast_ctx = (struct fast_crypto_cipher *)ctx;
    
    switch (fast_ctx->alg) {
        case CRYPTO_CIPHER_ALG_RC4:
            if (plain != crypt) {
                os_memcpy(plain, crypt, len);
            }
            rc4_skip(fast_ctx->u.rc4.key, fast_ctx->u.rc4.keylen,
            fast_ctx->u.rc4.used_bytes, plain, len);
            fast_ctx->u.rc4.used_bytes += len;
            break;
        case CRYPTO_CIPHER_ALG_AES:
            if (len % AES_BLOCK_SIZE) {
                return -1;
            }
            blocks = len / AES_BLOCK_SIZE;
            for (i = 0; i < blocks; i++) {
                os_memcpy(tmp, crypt, AES_BLOCK_SIZE);
                mbedtls_aes_decrypt(&(fast_ctx->u.aes.ctx_dec), crypt, plain);
                for (j = 0; j < AES_BLOCK_SIZE; j++)
                    plain[j] ^= fast_ctx->u.aes.cbc[j];
                os_memcpy(fast_ctx->u.aes.cbc, tmp, AES_BLOCK_SIZE);
                plain += AES_BLOCK_SIZE;
                crypt += AES_BLOCK_SIZE;
            }
            break;
#ifdef CONFIG_DES3
        case CRYPTO_CIPHER_ALG_3DES:
            if (len % 8) {
                return -1;
            }
            blocks = len / 8;
            for (i = 0; i < blocks; i++) {
                os_memcpy(tmp, crypt, 8);
                des3_decrypt(crypt, &fast_ctx->u.des3.key, plain);
                for (j = 0; j < 8; j++) {
                    plain[j] ^= fast_ctx->u.des3.cbc[j];
                }
                os_memcpy(fast_ctx->u.des3.cbc, tmp, 8);
                plain += 8;
                crypt += 8;
            }
            break;
#endif
#ifdef CONFIG_DES
        case CRYPTO_CIPHER_ALG_DES:
            if (len % 8) {
                return -1;
            }
            blocks = len / 8;
            for (i = 0; i < blocks; i++) {
                os_memcpy(tmp, crypt, 8);
                des_block_decrypt(crypt, fast_ctx->u.des.dk, plain);
                for (j = 0; j < 8; j++) {
                    plain[j] ^= fast_ctx->u.des.cbc[j];
                }
                os_memcpy(fast_ctx->u.des.cbc, tmp, 8);
                plain += 8;
                crypt += 8;
            }
            break;
#endif
        default:
            return -1;
}

return 0;
}
예제 #6
0
int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
		       const u8 *crypt, u8 *plain, size_t len)
{
	struct crypto_cipher *cipher;
	int encr_alg;

#ifdef CCNS_PL
	if (alg == ENCR_3DES) {
		struct des3_key_s des3key;
		size_t i, blocks;

		/* ECB mode is used incorrectly for 3DES!? */
		if (key_len != 24) {
			wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
			return -1;
		}
		des3_key_setup(key, &des3key);

		if (len % 8) {
			wpa_printf(MSG_INFO, "IKEV2: Invalid encrypted "
				   "length");
			return -1;
		}
		blocks = len / 8;
		for (i = 0; i < blocks; i++) {
			des3_decrypt(crypt, &des3key, plain);
			plain += 8;
			crypt += 8;
		}
	} else {
#endif /* CCNS_PL */
	switch (alg) {
	case ENCR_3DES:
		encr_alg = CRYPTO_CIPHER_ALG_3DES;
		break;
	case ENCR_AES_CBC:
		encr_alg = CRYPTO_CIPHER_ALG_AES;
		break;
	default:
		wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
		return -1;
	}

	cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
	if (cipher == NULL) {
		wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
		return -1;
	}

	if (crypto_cipher_decrypt(cipher, crypt, plain, len) < 0) {
		wpa_printf(MSG_INFO, "IKEV2: Decryption failed");
		crypto_cipher_deinit(cipher);
		return -1;
	}
	crypto_cipher_deinit(cipher);
#ifdef CCNS_PL
	}
#endif /* CCNS_PL */

	return 0;
}
예제 #7
0
파일: ssh.c 프로젝트: SylvestreG/bitrig
static int
load_ssh1_private(RSA *rsa, struct iovec *iov)
{
	BN_CTX *ctx;
	BIGNUM *aux;
	MD5_CTX md;
	char pass[128], comment[BUFSIZ];
	u_char *p, cipher_type, digest[16];
	void *dstate;
	int i;

	i = strlen(SSH1_MAGIC) + 1;

	/* Make sure it begins with the id string. */
	if (iov->iov_len < i || memcmp(iov->iov_base, SSH1_MAGIC, i) != 0)
		return (-1);
	
	p = (u_char *)iov->iov_base + i;
	i = iov->iov_len - i;
	
	/* Skip cipher_type, reserved data, bits. */
	cipher_type = *p;
	p += 1 + 4 + 4;
	i -= 1 + 4 + 4;

	/* Read public key. */
	if (get_bn(rsa->n, &p, &i) < 0 || get_bn(rsa->e, &p, &i) < 0)
		return (-1);
	
	/* Read comment. */
	if (get_string(comment, sizeof(comment), &p, &i) < 0)
		return (-1);
	
	/* Decrypt private key. */
	if (cipher_type != 0) {
		sign_passwd_cb(pass, sizeof(pass), 0, NULL);

		MD5_Init(&md);
		MD5_Update(&md, (const u_char *)pass, strlen(pass));
		MD5_Final(digest, &md);
		
		memset(pass, 0, strlen(pass));
		
		if ((dstate = des3_init(digest, sizeof(digest))) == NULL)
			return (-1);
		
		des3_decrypt(p, p, i, dstate);

		if (p[0] != p[2] || p[1] != p[3]) {
			fprintf(stderr, "Bad passphrase for %s\n", comment);
			return (-1);
		}
	}
	else if (p[0] != p[2] || p[1] != p[3])
		return (-1);
	
	p += 4;
	i -= 4;
	
	/* Read the private key. */
	if (get_bn(rsa->d, &p, &i) < 0 ||
	    get_bn(rsa->iqmp, &p, &i) < 0)
		return (-1);
	
	/* In SSL and SSH v1 p and q are exchanged. */
	if (get_bn(rsa->q, &p, &i) < 0 ||
	    get_bn(rsa->p, &p, &i) < 0)
		return (-1);
	
	/* Calculate p-1 and q-1. */
	ctx = BN_CTX_new();
	aux = BN_new();

	BN_sub(aux, rsa->q, BN_value_one());
	BN_mod(rsa->dmq1, rsa->d, aux, ctx);

	BN_sub(aux, rsa->p, BN_value_one());
	BN_mod(rsa->dmp1, rsa->d, aux, ctx);

	BN_clear_free(aux);
	BN_CTX_free(ctx);
	
	return (0);
}