コード例 #1
0
static void
ebc3_test(char key1[8], char key2[8], char key3[8], char in[8], char out[8])
{
    unsigned char k1[8], k2[8], k3[8],
	indata[8], outdata[8], outdata2[8], ansdata[8];
    DES_key_schedule s1, s2, s3;

    memcpy(k1, key1, 8);
    memcpy(k2, key2, 8);
    memcpy(k3, key3, 8);
    memcpy(indata, in, 8);
    memcpy(ansdata, out, 8);
    DES_set_odd_parity(&k1);
    DES_set_odd_parity(&k2);
    DES_set_odd_parity(&k3);
    DES_set_key_unchecked(&k1, &s1);
    DES_set_key_unchecked(&k2, &s2);
    DES_set_key_unchecked(&k3, &s3);
    DES_ecb3_encrypt(&indata, &outdata, &s1, &s2, &s3, 1);
    if (memcmp(outdata, ansdata, sizeof(ansdata)) != 0)
	errx(1, "des3: encrypt");
    DES_ecb3_encrypt(&outdata, &outdata2, &s1, &s2, &s3, 0);
    if (memcmp(indata, outdata2, sizeof(outdata2)) != 0)
	errx(1, "des3: decrypt");
}
コード例 #2
0
ファイル: symmetric.c プロジェクト: MrKID/RetroShare
static void tripledes_block_decrypt(ops_crypt_t *crypt,void *out,
				    const void *in)
    {
    DES_key_schedule *keys=crypt->encrypt_key;

    DES_ecb3_encrypt((void *)in,out,&keys[0],&keys[1],&keys[2],DES_DECRYPT);
    }
コード例 #3
0
ファイル: symmetric.c プロジェクト: Henauxg/minix
static void 
tripledes_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
	DES_key_schedule *keys = crypt->encrypt_key;

	DES_ecb3_encrypt(__UNCONST(in), out, &keys[0], &keys[1], &keys[2],
			DES_DECRYPT);
}
コード例 #4
0
ファイル: des_old.c プロジェクト: yyyyyao/Slicer3-lib-mirrors
void _ossl_old_des_ecb3_encrypt(_ossl_old_des_cblock *input,_ossl_old_des_cblock *output,
  des_key_schedule ks1,des_key_schedule ks2,
  des_key_schedule ks3, int enc)
  {
  DES_ecb3_encrypt((const_DES_cblock *)input, output,
    (DES_key_schedule *)ks1, (DES_key_schedule *)ks2,
    (DES_key_schedule *)ks3, enc);
  }
コード例 #5
0
ファイル: e_des3.c プロジェクト: niubl/camera_project
static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
			      const unsigned char *in, unsigned int inl)
{
	BLOCK_CIPHER_ecb_loop()
		DES_ecb3_encrypt(in + i,out + i, 
				 &data(ctx)->ks1, &data(ctx)->ks2,
				 &data(ctx)->ks3,
				 ctx->encrypt);
	return 1;
}
コード例 #6
0
ファイル: openssl.c プロジェクト: Epictetus/postgres
static int
ossl_des3_ecb_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen,
					  uint8 *res)
{
	unsigned	bs = gen_ossl_block_size(c);
	unsigned	i;
	ossldata   *od = c->ptr;

	for (i = 0; i < dlen / bs; i++)
		DES_ecb3_encrypt((void *) (data + i * bs), (void *) (res + i * bs),
						 &od->u.des3.k1, &od->u.des3.k2, &od->u.des3.k3, 0);
	return 0;
}
コード例 #7
0
ファイル: e_des.c プロジェクト: google/boringssl
static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
                              const uint8_t *in, size_t in_len) {
  if (in_len < ctx->cipher->block_size) {
    return 1;
  }
  in_len -= ctx->cipher->block_size;

  DES_EDE_KEY *dat = (DES_EDE_KEY *) ctx->cipher_data;
  for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) {
    DES_ecb3_encrypt((DES_cblock *) (in + i), (DES_cblock *) (out + i),
                     &dat->ks.ks[0], &dat->ks.ks[1], &dat->ks.ks[2],
                     ctx->encrypt);
  }
  return 1;
}
コード例 #8
0
ファイル: tdesecb.c プロジェクト: jackpelf/store
void tdes(int choice)
{
	unsigned char input_data[100] = "iamflag";
	int len = strlen(input_data);
	DES_cblock Key1 = { 0, 0, 0, 0, 0, 0, 0, 0 };
	DES_cblock Key2 = { 0, 0, 0, 0, 0, 0, 0, 0 };
	DES_cblock Key3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
	unsigned char *text = malloc(len);


	
	if ( -2 == (DES_set_key_checked(&Key1, &SchKey1) || DES_set_key_checked(&Key2, &SchKey2) || DES_set_key_checked(&Key3, &SchKey3)))
	{
		printf(" Weak key ....\n");
		exit(0);
	}

	if(choice == EN)
	{
		int i;
		for(i=0; i<len + 4; i += 8)
		{
			DES_ecb3_encrypt( (C_Block *)(input_data + i), (C_Block *)(text + i), &SchKey1, &SchKey2, &SchKey3, DES_ENCRYPT);
		}
		print_data("\n Encrypted",text,8*((len + 4) / 8));
	}
	else if(choice == DE)
	{
		int i;
		for(i=0; i<len + 4; i += 8)
		{
			DES_ecb3_encrypt( (C_Block *)(input_data + i), (C_Block *)(text + i), &SchKey1, &SchKey2, &SchKey3, DES_DECRYPT);
		}
		print_data("\n Decrypted",text,8*((len + 4) / 8));
	}
}
コード例 #9
0
ファイル: CryptTdes.c プロジェクト: Acidburn0zzz/shim
/**
  Performs TDES decryption on a data buffer of the specified size in ECB mode.

  This function performs TDES decryption on data buffer pointed by Input, of specified
  size of InputSize, in ECB mode.
  InputSize must be multiple of block size (8 bytes). This function does not perform
  padding. Caller must perform padding, if necessary, to ensure valid input data size.
  TdesContext should be already correctly initialized by TdesInit(). Behavior with
  invalid TDES context is undefined.

  If TdesContext is NULL, then return FALSE.
  If Input is NULL, then return FALSE.
  If InputSize is not multiple of block size (8 bytes), then return FALSE.
  If Output is NULL, then return FALSE.

  @param[in]   TdesContext  Pointer to the TDES context.
  @param[in]   Input        Pointer to the buffer containing the data to be decrypted.
  @param[in]   InputSize    Size of the Input buffer in bytes.
  @param[out]  Output       Pointer to a buffer that receives the TDES decryption output.

  @retval TRUE   TDES decryption succeeded.
  @retval FALSE  TDES decryption failed.

**/
BOOLEAN
EFIAPI
TdesEcbDecrypt (
  IN   VOID         *TdesContext,
  IN   CONST UINT8  *Input,
  IN   UINTN        InputSize,
  OUT  UINT8        *Output
  )
{
  DES_key_schedule  *KeySchedule;

  //
  // Check input parameters.
  //
  if (TdesContext == NULL || Input == NULL || (InputSize % TDES_BLOCK_SIZE) != 0 || Output == NULL) {
    return FALSE;
  }

  KeySchedule = (DES_key_schedule *) TdesContext;

  while (InputSize > 0) {
    DES_ecb3_encrypt (
      (const_DES_cblock *) Input,
      (DES_cblock *) Output,
      KeySchedule,
      KeySchedule + 1,
      KeySchedule + 2,
      DES_DECRYPT
      );
    Input     += TDES_BLOCK_SIZE;
    Output    += TDES_BLOCK_SIZE;
    InputSize -= TDES_BLOCK_SIZE;
  }

  return TRUE;
}
コード例 #10
0
ファイル: des-demo.c プロジェクト: mooosu/c-demo
int main(void)
{
    int docontinue = 1;
    char *data = "abcdefgh"; /* 明文 */
    int data_len;
    int data_rest;
    unsigned char ch;
    unsigned char *src = NULL; /* 补齐后的明文 */
    unsigned char *dst = NULL; /* 解密后的明文 */
    unsigned char *encryption= NULL; /* 解密后的明文 */
    int len;
    unsigned char tmp[8];
    unsigned char in[8];
    unsigned char out[8];
    char *k = "1234567890abcdefghijklmn"; /* 原始密钥 */
    int key_len;
    #define LEN_OF_KEY 24
    unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */
    unsigned char block_key[9];
    DES_key_schedule ks,ks2,ks3;
    /* 构造补齐后的密钥 */
    key_len = strlen(k);
    memcpy(key, k, key_len);
    memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
    /* 分析补齐明文所需空间及补齐填充数据 */
    data_len = strlen(data);
    data_rest = data_len % 8;
    len = data_len + (8 - data_rest);
    ch = 8 - data_rest;
    src = (unsigned char *)malloc(len);
    dst = (unsigned char *)malloc(len);
    encryption = (unsigned char *)malloc(len);
    if (NULL == src || NULL == dst)
    {
        docontinue = 0;
    }
    if (docontinue)
    {
        int count;
        int i;
        /* 构造补齐后的加密内容 */
        memset(src, 0, len);
        memcpy(src, data, data_len);
        memset(src + data_len, ch, 8 - data_rest);
        /* 密钥置换 */
        memset(block_key, 0, sizeof(block_key));
        memcpy(block_key, key + 0, 8);
        DES_set_key_unchecked((const_DES_cblock*)block_key, &ks);
        memcpy(block_key, key + 8, 8);
        DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2);
        memcpy(block_key, key + 16, 8);
        DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3);
        printf("before encrypt:\n%s\n",data);
        /* 循环加密/解密,每8字节一次 */
        count = len / 8;
        for (i = 0; i < count; i++)
        {
            memset(tmp, 0, 8);
            memset(in, 0, 8);
            memset(out, 0, 8);
            memcpy(tmp, src + 8 * i, 8);
            /* 加密 */
            DES_ecb3_encrypt((const_DES_cblock*)tmp, (DES_cblock*)in, &ks, &ks2, &ks3, DES_ENCRYPT);
            memcpy(encryption + 8 * i, in, 8);
            /* 解密 */
            DES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, &ks, &ks2, &ks3, DES_DECRYPT);
            /* 将解密的内容拷贝到解密后的明文 */
            memcpy(dst + 8 * i, out, 8);
        }
        printf("encryption :\n");
        for (i = 0; i < len; i++)
        {
            printf("0x%.2X ", *(encryption + i));
        }
	printf("\n%s\n",base64(encryption,len+1));
    }
    if (NULL != src)
    {
        free(src);
        src = NULL;
    }
    if (NULL != dst)
    {
        free(dst);
        dst = NULL;
    }
    return 0;
}
コード例 #11
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);
}
コード例 #12
0
int FIPS_selftest_des()
    {
    int n;

    /* Encrypt/decrypt with DES and compare to known answers */
    for(n=0 ; n < 2 ; ++n)
	{
	DES_key_schedule key;
	DES_cblock buf;

	DES_set_key(&tests[n].key,&key);
	DES_ecb_encrypt(&tests[n].plaintext,&buf,&key,1);
	if(memcmp(buf,tests[n].ciphertext,sizeof buf))
	    {
	    FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED);
	    return 0;
	    }
	DES_ecb_encrypt(&tests[n].ciphertext,&buf,&key,0);
	if(memcmp(buf,tests[n].plaintext,sizeof buf))
	    {
	    FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED);
	    return 0;
	    }
	}

    /* Encrypt/decrypt with 2-key 3DES and compare to known answers */
    for(n=0 ; n < 2 ; ++n)
	{
	DES_key_schedule key1, key2;
	unsigned char buf[8];

	DES_set_key(&tests2[n].key1,&key1);
	DES_set_key(&tests2[n].key2,&key2);
	DES_ecb2_encrypt(tests2[n].plaintext,buf,&key1,&key2,1);
	if(memcmp(buf,tests2[n].ciphertext,sizeof buf))
	    {
	    FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED);
	    return 0;
	    }
	DES_ecb2_encrypt(tests2[n].ciphertext,buf,&key1,&key2,0);
	if(memcmp(buf,tests2[n].plaintext,sizeof buf))
	    {
	    FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED);
	    return 0;
	    }
	}

    /* Encrypt/decrypt with 3DES and compare to known answers */
    for(n=0 ; n < 2 ; ++n)
	{
	DES_key_schedule key1, key2, key3;
	unsigned char buf[8];

	DES_set_key(&tests3[n].key1,&key1);
	DES_set_key(&tests3[n].key2,&key2);
	DES_set_key(&tests3[n].key3,&key3);
	DES_ecb3_encrypt(tests3[n].plaintext,buf,&key1,&key2,&key3,1);
	if(memcmp(buf,tests3[n].ciphertext,sizeof buf))
	    {
	    FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED);
	    return 0;
	    }
	DES_ecb3_encrypt(tests3[n].ciphertext,buf,&key1,&key2,&key3,0);
	if(memcmp(buf,tests3[n].plaintext,sizeof buf))
	    {
	    FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED);
	    return 0;
	    }
	}

    return 1;
    }
コード例 #13
0
ファイル: 3des.c プロジェクト: Sword2G/waf-anti-tamper
/**
 * msc_tripleDes:3des加/解密
 * @mptmp:用于分配内存的内存池
 * @data:明文
 * @kkey:密钥
 * @iv:明文向量 
 * @decode_or_encode_flag:加密/解密开关
 *
 * 返回值:失败返回NULL,成功返回vector字符串
 */
unsigned char *tripleDes(apr_pool_t *mptmp, const unsigned char *data,
                const unsigned char *kkey, const unsigned char *iv, int decode_or_encode_flag)
{   
    int data_rest;
    int data_len;
    int count, i;
    unsigned char ch;

    unsigned char *tmp;
    unsigned char *data_real;
    unsigned char *src; /* 补齐后的明文 */
    unsigned char *dst; /* 解密后的明文 */
    int len; 
    
    unsigned char in[LEN_OF_STEP];
    unsigned char out[LEN_OF_STEP];

    int key_len;
    unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */
    unsigned char block_key[LEN_OF_STEP + 1];
    DES_key_schedule ks,ks2,ks3;
    
    /* 入参检查 */
    if (mptmp == NULL || data == NULL || kkey == NULL || iv == NULL) {
        return NULL;
    }
    if (decode_or_encode_flag != DES_ENCRYPT && decode_or_encode_flag != DES_DECRYPT) {
        return NULL;
    }

    /* 构造补齐后的密钥 */
    key_len = strlen((char *)kkey);
    memcpy(key, (char *)kkey, (key_len < LEN_OF_KEY)?key_len:LEN_OF_KEY);
    if (key_len < LEN_OF_KEY) {
        memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
    }

    /* 拷贝一份原始数据 */
    data_len = strlen((char *)data);
    data_real = apr_pmemdup(mptmp, data, data_len + 1);    
    
    /* 加密/解密前处理 */
    ch = '\0';
    len = 0;
    data_rest = 0;
    if (decode_or_encode_flag == DES_ENCRYPT) {
        data_real = (unsigned char *)apr_pstrcat(mptmp, (char *)data_real, "|", (char *)iv, NULL);
        
        /* 分析补齐明文所需空间及补齐填充数据 */
        data_len = strlen((char *)data_real);
        data_rest = data_len % LEN_OF_STEP;
        if (data_rest != 0) {
            len = data_len + (LEN_OF_STEP - data_rest);
            ch = LEN_OF_STEP - data_rest;
        } else {
            len = data_len;
        }
        
    } else {
        /* 解密前要将16进制字符串转换成字节流 */
        data_len = hex2bytes_inplace_3des(data_real, data_len);
        len = data_len;
    }

    src = (unsigned char *)apr_palloc(mptmp, len + 1);
    dst = (unsigned char *)apr_palloc(mptmp, len + 1);
    if (src != NULL && dst != NULL) {

        /* 构造补齐后的加密内容 */
        memset(src, 0, len + 1);
        memcpy(src, data_real, data_len);
        
        if (data_rest != 0) {
            memset(src + data_len, ch, LEN_OF_STEP - data_rest);
        }        
        
        /* 密钥置换 */
        memset(block_key, 0, sizeof(block_key));
        memcpy(block_key, key + 0, LEN_OF_STEP);
        DES_set_key_unchecked((const_DES_cblock *)block_key, &ks);
        memcpy(block_key, key + LEN_OF_STEP, LEN_OF_STEP);
        DES_set_key_unchecked((const_DES_cblock *)block_key, &ks2);
        memcpy(block_key, key + LEN_OF_STEP * 2, LEN_OF_STEP);
        DES_set_key_unchecked((const_DES_cblock *)block_key, &ks3);

        /* 循环加密/解密,每8字节一次 */
        count = len / LEN_OF_STEP;
        for (i = 0; i < count; i++) {
            memset(in, 0, LEN_OF_STEP);
            memset(out, 0, LEN_OF_STEP);
            memcpy(in, src + LEN_OF_STEP * i, LEN_OF_STEP);

            DES_ecb3_encrypt((const_DES_cblock *)in, (DES_cblock *)out, &ks, &ks2, &ks3, decode_or_encode_flag);
            /* 将解密的内容拷贝到解密后的明文 */
            memcpy(dst + LEN_OF_STEP * i, out, LEN_OF_STEP);
        }
        *(dst + len) = 0; 
        
        if (decode_or_encode_flag == DES_DECRYPT) {
            /* 解密需要检验明文后面的向量,最后去除向量 */
            tmp = (unsigned char *)strrchr((const char *)dst, '|');
            if (tmp != NULL) {
                *tmp++ = 0;
                if (strncasecmp((const char *)tmp, (const char *)iv, strlen((char *)iv)) == 0) {
                    return dst;
                }
            }
        } else {
            /* 加密需要将密文编码成16进制字符串 */
            return (unsigned char *)bytes2hex(mptmp, dst, len);
        }
        
    }
    
    return NULL;
}
コード例 #14
0
ファイル: Utils.cpp プロジェクト: mrG7/VoIPserver
bool ServerConf::isEligible(){
	mysqlpp::Connection con = mysqlpp::Connection(false);
	mysqlpp::Query q = NULL;
	mysqlpp::StoreQueryResult qr;

	char *dbKeyBytes;
	char *sharedKey;
	char buffer[25];

	int b64decodedSize;

	char yyyy[5], mm[3], dd[3], fdd[5];

	DES_cblock cb1, cb2, cb3;
	DES_key_schedule ks1, ks2, ks3;

	time_t now = time(0);
	time_t future;

	struct tm expire_time;
	struct tm creation_time;

	memset(buffer, 0x00, 25);
	memset(yyyy, 0x00, 5);
	memset(mm, 0x00, 3);
	memset(dd, 0x00, 3);
	memset(fdd, 0x00, 5);

	memset(&expire_time, 0x00, sizeof(struct tm));

	if( con.connect(mysql_sentry_dbname, mysql_sentry_ip, mysql_sentry_user, mysql_sentry_password, 3306) ){
		q = con.query("SELECT activationkey FROM commonconfiguration");
		//q = con.query("select 'et6hyYTfpfhqZqlPP6WRJRjI09MNuOSJ' as 'activationkey'");
		qr = q.store();
		if(qr.size() == 0){
			std::cerr << "Key info not found" << std::endl;
		}else{
			std::cout << "Base64 Encoded data is " << (*qr.data())[0] << std::endl;

			Base64Decode((char *)((*qr.data())[0]).c_str(), &dbKeyBytes, &b64decodedSize);

			if(b64decodedSize < 0x0A)
				std::cerr << "Invalid Key" << std::endl;
			else{
				sharedKey = getKey();



				memcpy((DES_cblock *)cb1, sharedKey,8);
				memcpy((DES_cblock *)cb2, sharedKey+8,8);
				memcpy((DES_cblock *)cb3, sharedKey+16,8);

				/*DES_set_key(&cb1,&ks1);
				DES_set_key(&cb2,&ks2);
				DES_set_key(&cb3,&ks3);*/

				des_set_odd_parity(&cb1);
				des_set_odd_parity(&cb2);
				des_set_odd_parity(&cb3);


				if(DES_set_key_checked(&cb1, &ks1) || DES_set_key_checked(&cb2, &ks2) || DES_set_key_checked(&cb3, &ks3) ){
					std::cerr << "3DES key scheduling error" << std::endl;
				}else{

					DES_ecb3_encrypt((C_Block *)dbKeyBytes,(C_Block *)buffer, &ks1, &ks2, &ks3, DES_DECRYPT);
					DES_ecb3_encrypt((C_Block *)dbKeyBytes+1,(C_Block *)buffer+1, &ks1, &ks2, &ks3, DES_DECRYPT);
					DES_ecb3_encrypt((C_Block *)dbKeyBytes+2,(C_Block *)buffer+2, &ks1, &ks2, &ks3, DES_DECRYPT);

					free(sharedKey);
					free(dbKeyBytes);

					//std::cout << "Buffer " << buffer << std::endl;

					strncpy(yyyy, buffer+8, 4);
					strncpy(mm, buffer+12, 2);
					strncpy(dd, buffer+14, 2);
					strncpy(fdd, buffer+16, 4);

					expire_time.tm_mday = atoi(dd);
					expire_time.tm_mon = atoi(mm)-1;
					expire_time.tm_year = atoi(yyyy) - 1900;

					creation_time = expire_time;

					expire_time.tm_mday +=   atoi(fdd);

					future = mktime(&creation_time);

					if(now > future)
					{
						future = mktime(&expire_time);

						if(future >= now)
							return true;
					}

				}
			}
		}
	}else{
		std::cerr << "Unable to check eligibility, Connection failed..." << std::endl;
	}

	if(con.connected())
		con.disconnect();

	return false;

}