void bench_camellia(void) { Camellia cam; double start, total, persec; int i, ret; ret = CamelliaSetKey(&cam, key, 16, iv); if (ret != 0) { printf("CamelliaSetKey failed, ret = %d\n", ret); return; } start = current_time(1); for(i = 0; i < numBlocks; i++) CamelliaCbcEncrypt(&cam, plain, cipher, sizeof(plain)); total = current_time(0) - start; persec = 1 / total * numBlocks; #ifdef BENCH_EMBEDDED /* since using kB, convert to MB/s */ persec = persec / 1024; #endif printf("Camellia %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks, blockType, total, persec); }
/* * Encrypts a file using Camellia */ int CamelliaEncrypt(Camellia* cam, byte* key, int size, FILE* inFile, FILE* outFile) { RNG rng; byte iv[CAMELLIA_BLOCK_SIZE]; byte* input; byte* output; byte salt[SALT_SIZE] = {0}; int i = 0; int ret = 0; int inputLength; int length; int padCounter = 0; fseek(inFile, 0, SEEK_END); inputLength = ftell(inFile); fseek(inFile, 0, SEEK_SET); length = inputLength; /* pads the length until it evenly matches a block / increases pad number*/ while (length % CAMELLIA_BLOCK_SIZE != 0) { length++; padCounter++; } input = malloc(length); output = malloc(length); ret = InitRng(&rng); if (ret != 0) { printf("Failed to initialize random number generator\n"); return -1030; } /* reads from inFile and wrties whatever is there to the input array */ ret = fread(input, 1, inputLength, inFile); if (ret == 0) { printf("Input file does not exist.\n"); return -1010; } for (i = inputLength; i < length; i++) { /* padds the added characters with the number of pads */ input[i] = padCounter; } ret = RNG_GenerateBlock(&rng, iv, CAMELLIA_BLOCK_SIZE); if (ret != 0) return -1020; /* stretches key to fit size */ ret = GenerateKey(&rng, key, size, salt, padCounter); if (ret != 0) return -1040; /* sets key */ ret = CamelliaSetKey(cam, key, CAMELLIA_BLOCK_SIZE, iv); if (ret != 0) return -1001; /* encrypts the message to the ouput based on input length + padding */ CamelliaCbcEncrypt(cam, output, input, length); /* writes to outFile */ fwrite(salt, 1, SALT_SIZE, outFile); fwrite(iv, 1, CAMELLIA_BLOCK_SIZE, outFile); fwrite(output, 1, length, outFile); /* closes the opened files and frees the memory*/ memset(input, 0, length); memset(output, 0, length); memset(key, 0, size); free(input); free(output); free(key); fclose(inFile); fclose(outFile); return 0; }