void twofish_xts_crypt(struct twofish_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv, u_int do_encrypt) { u_int8_t block[TWOFISH_XTS_BLOCK_LEN]; u_int i, carry_in, carry_out; for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++) block[i] = data[i] ^ iv[i]; if (do_encrypt) twofish_encrypt(&ctx->key1, block, data); else twofish_decrypt(&ctx->key1, block, data); for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++) data[i] ^= iv[i]; /* Exponentiate tweak */ carry_in = 0; for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++) { carry_out = iv[i] & 0x80; iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0); carry_in = carry_out; } if (carry_in) iv[0] ^= AES_XTS_ALPHA; bzero(block, sizeof(block)); }
void EncipherBlock(int cipher, void *data, void *ks) { #ifdef GST_WINDOWS_BOOT_AES if (IsAesHwCpuSupported()) aes_hw_cpu_encrypt ((byte *) ks, data); else aes_encrypt (data, data, ks); #elif defined (GST_WINDOWS_BOOT_SERPENT) serpent_encrypt (data, data, ks); #elif defined (GST_WINDOWS_BOOT_TWOFISH) twofish_encrypt (ks, data, data); #endif }
void EncipherBlock(int cipher, void *data, void *ks) { switch (cipher) { case AES: // In 32-bit kernel mode, due to KeSaveFloatingPointState() overhead, AES instructions can be used only when processing the whole data unit. #if (defined (_WIN64) || !defined (TC_WINDOWS_DRIVER)) && !defined (TC_WINDOWS_BOOT) if (IsAesHwCpuSupported()) aes_hw_cpu_encrypt (ks, data); else #endif aes_encrypt (data, data, ks); break; case TWOFISH: twofish_encrypt (ks, data, data); break; case SERPENT: serpent_encrypt (data, data, ks); break; default: TC_THROW_FATAL_EXCEPTION; // Unknown/wrong ID } }
void twofish_xts_reinit(caddr_t key, u_int8_t *iv) { struct twofish_xts_ctx *ctx = (struct twofish_xts_ctx *)key; #if 0 u_int64_t blocknum; #endif #if 0 /* * Prepare tweak as E_k2(IV). IV is specified as LE representation * of a 64-bit block number which we allow to be passed in directly. */ /* XXX: possibly use htole64? */ #endif /* Last 64 bits of IV are always zero */ bzero(iv + TWOFISH_XTS_IV_LEN, TWOFISH_XTS_IV_LEN); twofish_encrypt(&ctx->key2, iv, iv); }
void CipherTwofish::Encrypt (byte *data) const { twofish_encrypt ((TwofishInstance *) ScheduledKey.Ptr(), (unsigned int *)data, (unsigned int *)data); }
/* function to import keyfile to encrypt, to decrypt or both */ int import ( config_t* config, char * path) { if (!(config->get_status == STATUS_ENC_KEY || config->get_status == STATUS_DEC_KEY )) { fprintf(stderr, "Not a valid key status.\n"); return 1; } /* Open keyfile path to read and write */ FILE * f = fopen(path,"r+"); if (!f) { fprintf(stderr, "Could not open keyfile %s", path); return 1; } /* Create new header */ header_t keyheader; /* Read header from keyfile */ if (fread(&keyheader,1,sizeof(header_t),f) != sizeof(header_t) ) { fprintf(stderr, "Could not read from keyfile %s\n", path); fclose(f); return 1; } /* key deriation */ char * key; void * salt = NULL; getkey(&key,&salt,"Please enter encryption passphrase: "); /* Set pos and status of the new header */ keyheader.pos = 0; keyheader.status = config->get_status; keyheader.salt = *((uint64_t * ) salt); /* write new header to file */ fseek(f,0,SEEK_SET); if (fwrite(&keyheader, 1, sizeof(header_t), f) != sizeof(header_t)) { fprintf(stderr, "Could not write to keyfile %s", path); fclose(f); return 1; } /* allocate memory for buffer */ uint8_t * buf; buf = malloc(keyheader.size * sizeof(uint8_t)); if (!buf) { fprintf(stderr, "\nCould not allocate memory\n"); fclose(f); return 1; } /* read random bytes */ fseek(f,sizeof(header_t),SEEK_SET); if (fread( buf, 1, keyheader.size, f) != keyheader.size ) { fprintf(stderr, "Could not read from keyfile %s\n", path); fclose(f); free(buf); return 1; } /* encryption */ if (twofish_encrypt((char *)buf,keyheader.size,key)) { fprintf(stderr, "Could not encrypt keyfile %s\n", path); free(buf); fclose(f); return 1; } /* write encrypted random bytes to keyfile */ fseek(f,sizeof(header_t),SEEK_SET); if (fwrite(buf, 1, keyheader.size, f) != keyheader.size ) { fprintf(stderr, "Could not write to public keyfile %s\n", path); free(buf); fclose(f); return 1; } /* close keyfile, free buf */ free(buf); fclose(f); /* If keyfile is for encryption */ if (keyheader.status == STATUS_ENC_KEY) { /* rename to .public */ char * pubpath = malloc(strlen(path) + 8 * sizeof(char)); sprintf(pubpath, "%s.public", path); rename(path,pubpath); /* error is missing */ /* free all and return success */ free(pubpath); } return 0; }
int main() { TWOFISH_context ctx; /* Expanded key. */ int i, j; /* Loop counters. */ const char *encrypt_msg; /* Message to print regarding encryption test; * the printf is done outside the loop to avoid * stuffing up the timing. */ clock_t timer; /* For computing elapsed time. */ /* Test buffer. */ byte buffer[4][16] = { {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}, {0x0F, 0x1E, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0x87, 0x96, 0xA5, 0xB4, 0xC3, 0xD2 ,0xE1, 0xF0}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54 ,0x32, 0x10}, {0x01, 0x23, 0x45, 0x67, 0x76, 0x54 ,0x32, 0x10, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98} }; /* Expected outputs for the million-operation test */ static const byte test_encrypt[4][16] = { {0xC8, 0x23, 0xB8, 0xB7, 0x6B, 0xFE, 0x91, 0x13, 0x2F, 0xA7, 0x5E, 0xE6, 0x94, 0x77, 0x6F, 0x6B}, {0x90, 0x36, 0xD8, 0x29, 0xD5, 0x96, 0xC2, 0x8E, 0xE4, 0xFF, 0x76, 0xBC, 0xE5, 0x77, 0x88, 0x27}, {0xB8, 0x78, 0x69, 0xAF, 0x42, 0x8B, 0x48, 0x64, 0xF7, 0xE9, 0xF3, 0x9C, 0x42, 0x18, 0x7B, 0x73}, {0x7A, 0x88, 0xFB, 0xEB, 0x90, 0xA4, 0xB4, 0xA8, 0x43, 0xA3, 0x1D, 0xF1, 0x26, 0xC4, 0x53, 0x57} }; static const byte test_decrypt[4][16] = { {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}, {0x0F, 0x1E, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0x87, 0x96, 0xA5, 0xB4, 0xC3, 0xD2 ,0xE1, 0xF0}, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54 ,0x32, 0x10}, {0x01, 0x23, 0x45, 0x67, 0x76, 0x54 ,0x32, 0x10, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98} }; /* Start the timer ticking. */ timer = clock (); /* Encryption test. */ for (i = 0; i < 125; i++) { twofish_setkey (&ctx, buffer[0], sizeof (buffer[0])); for (j = 0; j < 1000; j++) twofish_encrypt (&ctx, buffer[2], buffer[2]); twofish_setkey (&ctx, buffer[1], sizeof (buffer[1])); for (j = 0; j < 1000; j++) twofish_encrypt (&ctx, buffer[3], buffer[3]); twofish_setkey (&ctx, buffer[2], sizeof (buffer[2])*2); for (j = 0; j < 1000; j++) { twofish_encrypt (&ctx, buffer[0], buffer[0]); twofish_encrypt (&ctx, buffer[1], buffer[1]); } } encrypt_msg = memcmp (buffer, test_encrypt, sizeof (test_encrypt)) ? "encryption failure!\n" : "encryption OK!\n"; /* Decryption test. */ for (i = 0; i < 125; i++) { twofish_setkey (&ctx, buffer[2], sizeof (buffer[2])*2); for (j = 0; j < 1000; j++) { twofish_decrypt (&ctx, buffer[0], buffer[0]); twofish_decrypt (&ctx, buffer[1], buffer[1]); } twofish_setkey (&ctx, buffer[1], sizeof (buffer[1])); for (j = 0; j < 1000; j++) twofish_decrypt (&ctx, buffer[3], buffer[3]); twofish_setkey (&ctx, buffer[0], sizeof (buffer[0])); for (j = 0; j < 1000; j++) twofish_decrypt (&ctx, buffer[2], buffer[2]); } /* Stop the timer, and print results. */ timer = clock () - timer; printf (encrypt_msg); printf (memcmp (buffer, test_decrypt, sizeof (test_decrypt)) ? "decryption failure!\n" : "decryption OK!\n"); printf ("elapsed time: %.1f s.\n", (float) timer / CLOCKS_PER_SEC); return 0; }
static void twofish128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv) { twofish_encrypt((twofish_ctx *) key, blk, blk); }