static int camellia256_init(pgp_crypt_t *crypt) { if (crypt->encrypt_key) { free(crypt->encrypt_key); } if ((crypt->encrypt_key = calloc(1, sizeof(CAMELLIA_KEY))) == NULL) { (void) fprintf(stderr, "camellia256_init: alloc failure\n"); return 0; } if (Camellia_set_key(crypt->key, KEYBITS_CAMELLIA256, crypt->encrypt_key)) { fprintf(stderr, "camellia256_init: Error setting encrypt_key\n"); } if (crypt->decrypt_key) { free(crypt->decrypt_key); } if ((crypt->decrypt_key = calloc(1, sizeof(CAMELLIA_KEY))) == NULL) { (void) fprintf(stderr, "camellia256_init: alloc failure\n"); return 0; } if (Camellia_set_key(crypt->key, KEYBITS_CAMELLIA256, crypt->decrypt_key)) { fprintf(stderr, "camellia256_init: Error setting decrypt_key\n"); } return 1; }
static void camellia256_init(ops_crypt_t *crypt) { if (crypt->encrypt_key) free(crypt->encrypt_key); crypt->encrypt_key=malloc(sizeof(CAMELLIA_KEY)); if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA256,crypt->encrypt_key)) fprintf(stderr,"camellia256_init: Error setting encrypt_key\n"); if (crypt->decrypt_key) free(crypt->decrypt_key); crypt->decrypt_key=malloc(sizeof(CAMELLIA_KEY)); if (Camellia_set_key(crypt->key,KEYBITS_CAMELLIA256,crypt->decrypt_key)) fprintf(stderr,"camellia256_init: Error setting decrypt_key\n"); }
/* The subkey for Camellia is generated. */ static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc) { int ret, mode; EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data; ret = Camellia_set_key(key, ctx->key_len * 8, &dat->ks); if (ret < 0) { EVPerr(EVP_F_CAMELLIA_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED); return 0; } mode = ctx->cipher->flags & EVP_CIPH_MODE; if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { dat->block = (block128_f) Camellia_decrypt; dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f) Camellia_cbc_encrypt : NULL; } else { dat->block = (block128_f) Camellia_encrypt; dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f) Camellia_cbc_encrypt : NULL; } return 1; }
static krb5_error_code cts_decr(krb5_key key, const krb5_data *ivec, krb5_crypto_iov *data, size_t num_data, size_t dlen) { int ret = 0; size_t size = 0; unsigned char *oblock = NULL; unsigned char *dbuf = NULL; unsigned char iv_cts[IV_CTS_BUF_SIZE]; struct iov_block_state input_pos, output_pos; CAMELLIA_KEY deck; memset(iv_cts,0,sizeof(iv_cts)); if (ivec && ivec->data){ if (ivec->length != sizeof(iv_cts)) return KRB5_CRYPTO_INTERNAL; memcpy(iv_cts, ivec->data,ivec->length); } IOV_BLOCK_STATE_INIT(&input_pos); IOV_BLOCK_STATE_INIT(&output_pos); oblock = OPENSSL_malloc(dlen); if (!oblock) return ENOMEM; dbuf = OPENSSL_malloc(dlen); if (!dbuf){ OPENSSL_free(oblock); return ENOMEM; } Camellia_set_key(key->keyblock.contents, NUM_BITS * key->keyblock.length, &deck); krb5int_c_iov_get_block(dbuf, dlen, data, num_data, &input_pos); size = CRYPTO_cts128_decrypt((unsigned char *)dbuf, oblock, dlen, &deck, iv_cts, (cbc128_f)Camellia_cbc_encrypt); if (size <= 0) ret = KRB5_CRYPTO_INTERNAL; else { krb5int_c_iov_put_block(data, num_data, oblock, dlen, &output_pos); } if (!ret && ivec && ivec->data) memcpy(ivec->data, iv_cts, sizeof(iv_cts)); zap(oblock, dlen); zap(dbuf, dlen); OPENSSL_free(oblock); OPENSSL_free(dbuf); return ret; }
/* The subkey for Camellia is generated. */ static int camellia_init_key (EVP_CIPHER_CTX * ctx, const unsigned char *key, const unsigned char *iv, int enc) { int ret; ret = Camellia_set_key (key, ctx->key_len * 8, ctx->cipher_data); if (ret < 0) { EVPerr (EVP_F_CAMELLIA_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED); return 0; } return 1; }
static int ssh_camellia_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc) { struct ssh_camellia_ctr_ctx *c; if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { c = malloc(sizeof(*c)); EVP_CIPHER_CTX_set_app_data(ctx, c); } if (key != NULL) Camellia_set_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, &c->camellia_ctx); if (iv != NULL) memcpy(c->camellia_counter, iv, CAMELLIA_BLOCK_SIZE); return (1); }
int crypto_stream_xor(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *n, const unsigned char *k) { unsigned char tmp[CAMELLIA_BLOCK_SIZE] = {0, }; unsigned char iv[CAMELLIA_BLOCK_SIZE]; CAMELLIA_KEY ctx; unsigned int num = 0; Camellia_set_key(k, CRYPTO_KEYBYTES * 8, &ctx); memcpy(iv, n, sizeof(iv)); Camellia_ctr128_encrypt(in, out, inlen, &ctx, iv, tmp, &num); return 0; }
static void crypt_all(int count) { int index = 0; #ifdef _OPENMP #pragma omp parallel for for (index = 0; index < count; index++) #endif { CAMELLIA_KEY st_key; unsigned char in[16] = {0}; unsigned char key[32] = {0}; memcpy(key, saved_key[index], strlen(saved_key[index])); Camellia_set_key(key, 256, &st_key); Camellia_encrypt(in, crypt_out[index], &st_key); } }
int crypto_stream(unsigned char *out, unsigned long long outlen, const unsigned char *n, const unsigned char *k) { static const uint64_t zero[(1024 * 1024) / sizeof(uint64_t)]; unsigned char tmp[CAMELLIA_BLOCK_SIZE] = {0, }; unsigned char iv[CAMELLIA_BLOCK_SIZE]; CAMELLIA_KEY ctx; unsigned int num = 0; assert(outlen <= sizeof(zero)); Camellia_set_key(k, CRYPTO_KEYBYTES * 8, &ctx); memcpy(iv, n, sizeof(iv)); Camellia_ctr128_encrypt((void *)zero, out, outlen, &ctx, iv, tmp, &num); return 0; }
krb5_error_code krb5int_camellia_cbc_mac(krb5_key key, const krb5_crypto_iov *data, size_t num_data, const krb5_data *iv, krb5_data *output) { CAMELLIA_KEY enck; unsigned char blockY[CAMELLIA_BLOCK_SIZE]; struct iov_block_state iov_state; if (output->length < CAMELLIA_BLOCK_SIZE) return KRB5_BAD_MSIZE; Camellia_set_key(key->keyblock.contents, NUM_BITS * key->keyblock.length, &enck); if (iv != NULL) memcpy(blockY, iv->data, CAMELLIA_BLOCK_SIZE); else memset(blockY, 0, CAMELLIA_BLOCK_SIZE); IOV_BLOCK_STATE_INIT(&iov_state); for (;;) { unsigned char blockB[CAMELLIA_BLOCK_SIZE]; if (!krb5int_c_iov_get_block(blockB, CAMELLIA_BLOCK_SIZE, data, num_data, &iov_state)) break; xorblock(blockB, blockY); Camellia_ecb_encrypt(blockB, blockY, &enck, 1); } output->length = CAMELLIA_BLOCK_SIZE; memcpy(output->data, blockY, CAMELLIA_BLOCK_SIZE); return 0; }
int main(void) { int i; CAMELLIA_KEY key; BIO* bio_out; unsigned char key_data[KEY_SIZE] = { 0x23, 0x33, 0xa1, 0x19, 0xd2, 0x4b, 0x98, 0x75, 0x77, 0x29, 0xd2, 0x9d, 0xfe, 0x39, 0x36, 0x80 }; unsigned char iv[CAMELLIA_BLOCK_SIZE]; unsigned char const iv_data[CAMELLIA_BLOCK_SIZE] = { 0xcc, 0xae, 0xcd, 0x1e, 0xf1, 0x7e, 0x12, 0x51, 0x32, 0x15, 0x33, 0x55, 0x58, 0x24, 0x02, 0x39 }; 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 a multiple of 16 (i.e. the blocksize) */ int length = (int)(strlen(data) + (CAMELLIA_BLOCK_SIZE - 1)) & ~(CAMELLIA_BLOCK_SIZE - 1); /* Input pointer to OpenSSL's Camelia CBC method must be a multiple of 16. */ /* Hence, use the length calculated above and fill the extra bytes with 0's */ char* data_to_encrypt = (char*) calloc(length, sizeof(char)); /* The output of OpenSSL's Camelia CBC method is a multiple of 16 */ /* 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 */ memcpy(iv, iv_data, CAMELLIA_BLOCK_SIZE); /* Copy the data to the padded array created earlier */ memcpy(data_to_encrypt, data, strlen(data)); /* Set the key structure using the predefined key */ Camellia_set_key(key_data, KEY_SIZE * 8, &key); /* Carry out the encryption */ Camellia_cbc_encrypt(data_to_encrypt, ciphertext, length, &key, iv, CAMELLIA_ENCRYPT); /* Setup output */ 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"); /* Start the decryption process */ /* First, copy the original IV data back to the IV array - as it was overwritten * during the encryption process */ memcpy(iv, iv_data, CAMELLIA_BLOCK_SIZE); /* Carry out the decryption */ Camellia_cbc_encrypt(ciphertext, plaintext, length, &key, iv, CAMELLIA_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(ciphertext); free(plaintext); return 0; }
static int cuda_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc) { switch ((ctx->cipher)->nid) { case NID_des_ecb: case NID_des_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating DES key schedule..."); DES_key_schedule des_key_schedule; DES_set_key((const_DES_cblock *)key,&des_key_schedule); DES_cuda_transfer_key_schedule(&des_key_schedule); if(iv) DES_cuda_transfer_iv(iv); break; case NID_bf_ecb: case NID_bf_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating Blowfish key schedule...\n"); BF_KEY key_schedule; BF_set_key(&key_schedule,ctx->key_len,key); BF_cuda_transfer_key_schedule(&key_schedule); if(iv) BF_cuda_transfer_iv(iv); break; case NID_cast5_ecb: case NID_cast5_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating CAST5 key schedule...\n"); CAST_KEY cast_key_schedule; CAST_set_key(&cast_key_schedule,ctx->key_len*8,key); CAST_cuda_transfer_key_schedule(&cast_key_schedule); if(iv) CAST_cuda_transfer_iv(iv); break; case NID_camellia_128_ecb: case NID_camellia_128_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating Camellia key schedule...\n"); CAMELLIA_KEY cmll_key_schedule; Camellia_set_key(key,ctx->key_len*8,&cmll_key_schedule); CMLL_cuda_transfer_key_schedule(&cmll_key_schedule); if(iv) CMLL_cuda_transfer_iv(iv); break; case NID_idea_ecb: case NID_idea_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating IDEA key schedule...\n"); { IDEA_KEY_SCHEDULE idea_key_schedule, idea_dec_key_schedule; idea_set_encrypt_key(key,&idea_key_schedule); if(!(ctx->encrypt)) { idea_set_decrypt_key(&idea_key_schedule,&idea_dec_key_schedule); IDEA_cuda_transfer_key_schedule(&idea_dec_key_schedule); } else { IDEA_cuda_transfer_key_schedule(&idea_key_schedule); } } if(iv) IDEA_cuda_transfer_iv(iv); break; case NID_aes_128_ecb: case NID_aes_128_cbc: { if (!quiet && verbose) fprintf(stdout,"Start calculating AES-128 key schedule...\n"); AES_KEY aes_key_schedule; if(ctx->encrypt) AES_cuda_set_encrypt_key(key,128,&aes_key_schedule); else AES_cuda_set_decrypt_key(key,128,&aes_key_schedule); AES_cuda_transfer_key_schedule(&aes_key_schedule); } if(iv) AES_cuda_transfer_iv(iv); break; case NID_aes_192_ecb: case NID_aes_192_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating AES-192 key schedule...\n"); { AES_KEY aes_key_schedule; if(ctx->encrypt) AES_cuda_set_encrypt_key(key,192,&aes_key_schedule); else AES_cuda_set_decrypt_key(key,192,&aes_key_schedule); AES_cuda_transfer_key_schedule(&aes_key_schedule); if(iv) AES_cuda_transfer_iv(iv); } break; case NID_aes_256_ecb: case NID_aes_256_cbc: if (!quiet && verbose) fprintf(stdout,"Start calculating AES-256 key schedule...\n"); { AES_KEY aes_key_schedule; if(ctx->encrypt) AES_cuda_set_encrypt_key(key,256,&aes_key_schedule); else AES_cuda_set_decrypt_key(key,256,&aes_key_schedule); AES_cuda_transfer_key_schedule(&aes_key_schedule); if(iv) AES_cuda_transfer_iv(iv); } break; default: return 0; } if (!quiet && verbose) fprintf(stdout,"DONE!\n"); return 1; }