Ejemplo n.º 1
0
Archivo: sshdes.c Proyecto: rdebath/sgt
static void des3_sckey(unsigned char *key) {
    des_key_setup(GET_32BIT_MSB_FIRST(key),
                  GET_32BIT_MSB_FIRST(key+4), &sckeys[0]);
    des_key_setup(GET_32BIT_MSB_FIRST(key+8),
                  GET_32BIT_MSB_FIRST(key+12), &sckeys[1]);
    des_key_setup(GET_32BIT_MSB_FIRST(key+16),
                  GET_32BIT_MSB_FIRST(key+20), &sckeys[2]);
    logevent("Initialised triple-DES server->client encryption");
}
Ejemplo n.º 2
0
Archivo: sshdes.c Proyecto: rdebath/sgt
void des3_encrypt_pubkey(unsigned char *key,
                         unsigned char *blk, int len) {
    DESContext ourkeys[3];
    des_key_setup(GET_32BIT_MSB_FIRST(key),
                  GET_32BIT_MSB_FIRST(key+4), &ourkeys[0]);
    des_key_setup(GET_32BIT_MSB_FIRST(key+8),
                  GET_32BIT_MSB_FIRST(key+12), &ourkeys[1]);
    des_key_setup(GET_32BIT_MSB_FIRST(key),
                  GET_32BIT_MSB_FIRST(key+4), &ourkeys[2]);
    des_3cbc_encrypt(blk, blk, len, ourkeys);
}
Ejemplo n.º 3
0
void des3_set_2keys(des3_context *ctx, CHAR8 k1, CHAR8 k2)
{
    DESContext *sched = ctx->sched;
    word32 kL, kR;
    kL = GET_32BIT_MSB_FIRST(k1);
    kR = GET_32BIT_MSB_FIRST(k1+4);
    des_key_setup(kL, kR, &sched[0]);
    des_key_setup(kL, kR, &sched[2]);
    kL = GET_32BIT_MSB_FIRST(k2);
    kR = GET_32BIT_MSB_FIRST(k2+4);
    des_key_setup(kL, kR, &sched[1]);
}
Ejemplo n.º 4
0
bool Des::Encrypt(const char *szInput, char *szOutput)
{
	int i = 0, j = 0;
	int inlen = strlen(szInput);
	int outlen = 0;

	srand((unsigned)time(NULL));

    des_key_setup(GET_32BIT_MSB_FIRST(key8),
		  GET_32BIT_MSB_FIRST(key8 + 4), &crkey);

	unsigned char dest[MAX_LEN] = {0};
	des_cbc_encrypt(dest, (unsigned char *)szInput, inlen, &crkey);
	outlen = inlen % 8 ? inlen + 8 - inlen % 8 : inlen;

	for(i = 0;i < outlen;i ++)
	{
		sprintf(szOutput, "%s%c", szOutput, map[dest[i] / 0x10][0]);
		for(j = 0;j < (int)strlen(map[dest[i] / 0x10]) - 1;j ++)
			sprintf(szOutput, "%s%c", szOutput, rand() % 26 + 0x41);

		sprintf(szOutput, "%s%c", szOutput, map[dest[i] % 0x10][0]);
		for(j = 0;j < (int)strlen(map[dest[i] % 0x10]) - 1;j ++)
			sprintf(szOutput, "%s%c", szOutput, rand() % 26 + 0x41);
	}

	//memset(key8, 0, 8);
	//memset(map, 0, 256);

	return true;

}
Ejemplo n.º 5
0
/* Functions called by dyncrypt */
void des_set_key(des_context *ctx, CHAR8 key)
{
    DESContext *sched = ctx->sched;
    word32 kL, kR;
    kL = GET_32BIT_MSB_FIRST(key);
    kR = GET_32BIT_MSB_FIRST(key+4);
    des_key_setup(kL, kR, &sched[0]);
}
struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
					  const u8 *iv, const u8 *key,
					  size_t key_len)
{
	struct crypto_cipher *ctx;

	ctx = os_zalloc(sizeof(*ctx));
	if (ctx == NULL)
		return NULL;

	ctx->alg = alg;

	switch (alg) {
	case CRYPTO_CIPHER_ALG_RC4:
		if (key_len > sizeof(ctx->u.rc4.key)) {
			os_free(ctx);
			return NULL;
		}
		ctx->u.rc4.keylen = key_len;
		os_memcpy(ctx->u.rc4.key, key, key_len);
		break;
	case CRYPTO_CIPHER_ALG_AES:
		ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
		if (ctx->u.aes.ctx_enc == NULL) {
			os_free(ctx);
			return NULL;
		}
		ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
		if (ctx->u.aes.ctx_dec == NULL) {
			aes_encrypt_deinit(ctx->u.aes.ctx_enc);
			os_free(ctx);
			return NULL;
		}
		os_memcpy(ctx->u.aes.cbc, iv, AES_BLOCK_SIZE);
		break;
	case CRYPTO_CIPHER_ALG_3DES:
		if (key_len != 24) {
			os_free(ctx);
			return NULL;
		}
		des3_key_setup(key, &ctx->u.des3.key);
		os_memcpy(ctx->u.des3.cbc, iv, 8);
		break;
	case CRYPTO_CIPHER_ALG_DES:
		if (key_len != 8) {
			os_free(ctx);
			return NULL;
		}
		des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
		os_memcpy(ctx->u.des.cbc, iv, 8);
		break;
	default:
		os_free(ctx);
		return NULL;
	}

	return ctx;
}
struct crypto_cipher *  fast_crypto_cipher_init(enum crypto_cipher_alg alg,
					  const uint8_t *iv, const uint8_t *key,
					  size_t key_len)
{
    struct fast_crypto_cipher *ctx;

    ctx = (struct fast_crypto_cipher *)os_zalloc(sizeof(*ctx));
    if (ctx == NULL) {
        return NULL;
    }
    
    ctx->alg = alg;

    switch (alg) {
        case CRYPTO_CIPHER_ALG_RC4:
            if (key_len > sizeof(ctx->u.rc4.key)) {
	        os_free(ctx);
	        return NULL;
            }
            ctx->u.rc4.keylen = key_len;
            os_memcpy(ctx->u.rc4.key, key, key_len);
            break;
        case CRYPTO_CIPHER_ALG_AES:                
            mbedtls_aes_init(&(ctx->u.aes.ctx_enc));
            mbedtls_aes_setkey_enc(&(ctx->u.aes.ctx_enc), key, 256);
            mbedtls_aes_init(&(ctx->u.aes.ctx_dec));
            mbedtls_aes_setkey_dec(&(ctx->u.aes.ctx_dec), key, 256);               
            os_memcpy(ctx->u.aes.cbc, iv, AES_BLOCK_SIZE);
            break;
#ifdef CONFIG_DES3
        case CRYPTO_CIPHER_ALG_3DES:
            if (key_len != 24) {
	        os_free(ctx);
	        return NULL;
            }
            des3_key_setup(key, &ctx->u.des3.key);
            os_memcpy(ctx->u.des3.cbc, iv, 8);
            break;
#endif
#ifdef CONFIG_DES
        case CRYPTO_CIPHER_ALG_DES:
            if (key_len != 8) {
	        os_free(ctx);
	        return NULL;
            }
            des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
            os_memcpy(ctx->u.des.cbc, iv, 8);
            break;
#endif
        default:
            os_free(ctx);
            return NULL;
    }

    return (struct crypto_cipher *)ctx;
}
Ejemplo n.º 8
0
void desPerformOn(uint8_t *plainData) {
    uint8_t desSchedule_buffer[16][6];
    uint8_t encryptedDataBuffer_ptr[DES_BLOCK_SIZE];

    //set crypto mode
    CryptoMode_enum crypto_mode = ((g_Des_mode == DES_ENCRYPTION) ? ENCRYPT : DECRYPT);
    //setup buffer for keys
    des_key_setup(g_DES_KEY_ptr, desSchedule_buffer, crypto_mode);
    //apply to data
    des_crypt(plainData, encryptedDataBuffer_ptr, desSchedule_buffer);

    //replace plain data by encrypted data
    memcpy(plainData, encryptedDataBuffer_ptr, DES_BLOCK_SIZE);

    //debug print
    //for (int i=0; i<DES_BLOCK_SIZE;i++) printf("%02X ",plainData[i]);
}
Ejemplo n.º 9
0
bool Des::Decrypt(const char *szInput, char *szOutput)
{
	int i = 0;
	int inlen = strlen(szInput);
	int datalen = 0;

	if(inlen <= 8 || inlen > 512)
		return false;

	i = 0;
	char src[MAX_LEN] = {0};
	while(i < inlen)
	{
		int len = 0;
		int hi = map_index(szInput[i], len);
		i += len;
		int lo = map_index(szInput[i], len);
		i += len;

		if(hi == 99 || lo == 99)
			return false;

		src[datalen++] = hi * 0x10 + lo;
	}
	
    des_key_setup(GET_32BIT_MSB_FIRST(key8),
		  GET_32BIT_MSB_FIRST(key8 + 4), &crkey);

	unsigned char dest[256] = {0};
	des_cbc_decrypt(dest, (unsigned char *)src, datalen, &crkey);

	sprintf(szOutput, "%s", dest);

	//memset(key, 0, 8);
	//memset(map, 0, 256);

	return true;

}
Ejemplo n.º 10
0
Archivo: sshdes.c Proyecto: rdebath/sgt
static void des_sesskey(unsigned char *key) {
    des_key_setup(GET_32BIT_MSB_FIRST(key),
                  GET_32BIT_MSB_FIRST(key+4), &cskeys[0]);
    logevent("Initialised single-DES encryption");
}