static void cbc_test () { int testno; unsigned char tmp[4*B]; printf ("CBC tests:\n"); printf ("initial vector:"); hexdump (ivec, sizeof(ivec)); for (testno = 0; testno < NTESTS; testno++) { unsigned len = (test_case_len[testno] + 15) & ~15; printf ("\ntest %d - %d bytes\n", testno, len); printf ("input:"); hexdump (test_case[testno].input, len); printf ("\n"); cbc_enc (test_case[testno].output, test_case[testno].input, ivec, len); printf ("output:"); hexdump (test_case[testno].output, len); printf ("\n"); cbc_dec (tmp, test_case[testno].output, ivec, len); if (memcmp (tmp, test_case[testno].input, len)) { printf("cbc decrypt failed!!"); hexdump (tmp, len); printf ("\n"); exit(1); } } printf ("\n"); }
krb5_error_code krb5int_aes_encrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data, size_t num_data) { int ret = 0; int nblocks = 0; size_t input_length, i; for (i = 0, input_length = 0; i < num_data; i++){ krb5_crypto_iov *iov = &data[i]; if (ENCRYPT_IOV(iov)) input_length += iov->data.length; } nblocks = (input_length + BLOCK_SIZE - 1) / BLOCK_SIZE; if (nblocks == 1) { if (input_length != BLOCK_SIZE) return KRB5_BAD_MSIZE; ret = cbc_enc(key, ivec, data, num_data); } else if (nblocks > 1) { ret = cts_encr(key, ivec, data, num_data, input_length); } return ret; }
int DES::encrypt(const char key[8], char* data, int blocks, int mode) { if ((!data)||(blocks<1)) return 0; deskey((unsigned char*)key, ENCRYPT); if(mode == 1) { cbc_enc((unsigned char*)data, (unsigned char*)data, blocks); } else { des((unsigned char*)data, (unsigned char*)data, blocks); } return 1; }
krb5_error_code krb5int_aes_encrypt(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data, size_t num_data) { int ret = 0; size_t input_length, nblocks; input_length = iov_total_length(data, num_data, FALSE); nblocks = (input_length + BLOCK_SIZE - 1) / BLOCK_SIZE; if (nblocks == 1) { if (input_length != BLOCK_SIZE) return KRB5_BAD_MSIZE; ret = cbc_enc(key, ivec, data, num_data); } else if (nblocks > 1) { ret = cts_encr(key, ivec, data, num_data, input_length); } return ret; }
static void cts_enc (unsigned char *out, unsigned char *in, unsigned char *iv, unsigned int len) { int r; unsigned int len2; unsigned char pn1[B], pn[B], cn[B], cn1[B]; if (len < B + 1) abort (); len2 = (len - B - 1) & ~(B-1); cbc_enc (out, in, iv, len2); out += len2; in += len2; len -= len2; if (len2) iv = out - B; if (len <= B || len > 2 * B) abort (); printf ("(did CBC mode for %d)\n", len2); D(in); xor (pn1, in, iv); D(pn1); r = camellia_enc_blk (pn1, cn, &ctx); if (!r) fprintf(stderr, "error, line %d\n", __LINE__), exit(1); D(cn); memset (pn, 0, sizeof(pn)); memcpy (pn, in+B, len-B); D(pn); xor (pn, pn, cn); D(pn); r = camellia_enc_blk (pn, cn1, &ctx); if (!r) fprintf(stderr, "error, line %d\n", __LINE__), exit(1); D(cn1); memcpy(out, cn1, B); memcpy(out+B, cn, len-B); }