Twofish in C#

As one of the five AES competition finalists, Twofish algorithm is well known and appreciated. Yes, Rijndael ended up being AES at the end but, unlike other finalists, Twofish is still widely used.

As I tried to read some Twofish-encrypted files I’ve noticed one sad fact - C# support is severely lacking. Yes, there is one reasonably good free implementation on CodeProject but it doesn’t really play nicely with CryptoStream due to lack of any padding support. So, since I had some free time on my hands, I’ve decided to roll my own implementation.

I won’t go too much into the details - do check the code yourself. Suffice to say it can be inserted wherever SymmetricAlgorithm can be used. It supports CBC and ECB encryption modes with no, zero, or PKCS#7 padding. You can chose if you are going to deal with encryption yourself or decide to be at the mercy of CryptoStream.

Basis for this code was Twofish reference implementation in C. Yes, I am aware recommendation is to use the optimized version instead but I find reference code much more readable and it made porting so much easier. I did however use MDS table lookup from the optimized version as it pretty much doubles the speed. Some time in the future I might consider optimizing it further but, as I have no pressing need, it might be a while.

It should be reasonably easy to follow Twofish code and only confusion will probably arise from the use of DWord structure with the overlapping fields. Yes, you can have the exactly same functionality with bitwise operations but overlapping structure does help performance quite a bit (around 25%). As I am using unoptimized reference code, one might argue implementation is not a speed champ to start with. However I believe this actually makes code even a bit more readable so I saw no reason to resist the temptation.

Probably the same amount of time I’ve spent on code was spent on setting up the test environment. While the basic test cases for full block transformations were covered by the official test vectors, the more expansive test cases with padding were pretty much non-existent. And let’s not even get into the Monte Carlo tests and all the insanity brewing within.

In any case, download is available (or check a GitHub link). Those a bit more interested in validity can check tests too.

QText 4.10

QText 4.00

On the last day of 2015, a new version of QText saw the light of day. It is just a minor version bump but with a few interesting twists.

First of all is the final solution for the bug annoying all Asian users for years - QText simply could not properly handle that character set thanks to restrictions of the underlying RichText control. While issue is still there, a simple workaround made it irrelevant for character entry.

Windows 10 functionality was improved by adding basic support for tablet mode and long awaited spell-checker. Spell-checker uses system service so it is not available on anything lower than Windows 8 but hopefully most users are either on Windows 10 or on the way toward them.

Couple other changes can all be bundled into a general improvement category so I’m not going to go over each one. If you are curious, source is always available.

Either upgrade from the application or get the new version here.

In the Year 2015

Illustration

As in the previous years, first post of 2016 is reserved for some stats. And yes, since I publish my blog posts in UTC times, I am aware it might not be a new year everywhere yet. So what? :)

This year saw 58 posts - slightly more than the last year but not nearly as much as the years before. What can I say, I got lazy. I did manage to keep it roughly at a one post a week which is my goal for the next year too.

As it became usual, there is around 20% posts about programming, Windows and Linux got 12% each, and electronics got slightly less at 10%. I did make a few videos too but I am definitely sticking with the textual blog for now.

Traffic decreased slightly as compared with the last year and pretty much all decrease can be traced back to a single post going away due to DMCA complaint toward Google. Regardless of post only describing the officially supported functionality, some smartass decided that it must be bad with the title like that. I’ve tried complaining and challenging takedown but pretty much never got a human to answer me. And yes, despite Microsoft being behind removal, Bing still happily returns the page. Too bad nobody is using it.

Visitor browser stats are almost the same as in the previous year: 44% comes via Chrome, 24% chose Firefox, and 21% is still using Internet Explorer. Microsoft Edge is as successful as Windows Mobile at about 1%. Newcomer is Maxthon at 0.2%, something I didn’t even know existed. And guess what, same as last year, only 5% visitors decided to squint at their mobile phone.

There was a slight change to visitor’s locations. First is still USA at 21% and second is Germany at 11%. The newcomers are UK and Japan, both at 5%. India has dropped of the radar to only 4% compared to 10% it held previous year. Either I lost a whole country or they just started to hide their location. Total of 207 countries visited the site with Caribbean Netherlands being my favorite single-visit country. Before seeing it in stats, I had no idea it even existed. Again, as the vast majority of traffic comes from an unknown location, I cannot really tell how accurate stats actually are.

That’s it for year 2015 - have fun and drink beer.

Dual Boot With Bitlocker for a Nitpicker

Illustration

As I use my laptop for both work and personal stuff, from the get-go I decided to go with the dual boot setup. For security I also decided to have full-disk encryption with the additional benefit of further separating those two installations.

When I first installed my laptop I did it as any sane person would - just install two Windows copies and everything works. However, during these holidays I decided to clean out my machine and redo the installs in a slightly different way.

Basic premise was the same: two BitLocker Windows installations. First one would occupy 400 GB, while the other would get leftovers out of 476 GB drive. What was tricky was my desire to have 400 GB accessible to user after Windows take their share. And I definitely wanted my personal partition to be first so that I can easily kill work partition(s) if I ever want to convert it to personal-only rig.

So I downloaded the latest Windows installation onto my USB and got it started. At the first screen I pressed <Shift>+<F10> and wrote DISKPART on the command prompt. Here I wanted to clean my existing disk content, setup GPT partitioning scheme, and have first 400 GB available for my private install with rest of the disk reserved for the work partition:

SELECT DISK 0
CLEAN
CONVERT GPT
CREATE PARTITION PRIMARY SIZE=410176
CREATE PARTITION PRIMARY
SELECT PARTITION 1
DELETE PARTITION
EXIT

As you can see from commands above, I’ve done this by creating two partitions and then deleting the first one so that Windows can have fun in that area only. Yes, I could’ve create recovery and system partitions myself but I actually prefer not to deal with such a low level details. If you wonder how I came upon 410,176 MB as a figure that will give me 400 GB partition, it is simply 409,600 MB (400 GB, the final desired size) + 450 MB (recovery parition) + 100 MB (system partition) + 16 MB (reserved partition) + 10 MB (to get into next cylinder). After that I’ve continued with Windows installation on unassigned space and my disk C after installation was 400.01 GB - the closest I could get to 400 GB.

As I wanted to have dual boot, I also went into Power Options, Choose what the power buttons do and turned OFF fast startup setting. In the early Windows 10 days omitting this would cause quite a few dual boot issues as each Windows copy left hardware in its own state. With the newer builds Windows seems to handle that better but I see no reason to tempt the fate. Only step left was to start Bitlocker encryption and get onto the next installation once it is done.

After next installation booted (actually of the same USB media), I used <Shift>+<F10> again to delete partition I’ve “reserved” previously. Since Windows added quite a few of its own partitions, it has moved to 5th spot:

SELECT DISK 0
SELECT PARTITION 5
DELETE PARTITION
EXIT

Installation continues as usual with the same adjustments applied (fast startup off + bitlocker on). Note that this leaves second installation as a default startup option. This can be changed in Advanced System Settings under startup and recovery.

Mini Metro and the Runtime Error!

Illustration

When I first got my hands on Mini Metro, it was over at the GOG.com. And I got dreadful Microsoft Visual C++ Runtime Library: Runtime Error!

Going through forums brought nothing useful so I contacted GOGs customer service. They’ve gave me a list of troubleshooting steps and I religiously went through all of them (even a strange suggestion to create a virtual screen?!). As nothing was really solved, it was the time to get out the big guns. In this case it was asking developers at Dinosaur Polo Club for help.

From them I got two steps. One was to install Visual C++ Runtime (different than what GOG guys suggested) and/or to try non-Galaxy build (Galaxy is GOG’s attempt to be Steam). Already fed up with all the runtimes I’ve installed so far, I tried non-Galaxy build first and it worked flawlessly. Problem solved!

After a few hours spent building subway, I was tempted by the other C++ runtime suggested. Would that make my GOG Metro work? Yes, I got another build from Dino guys, but that was just a zip file. Call me spoiled, but I prefer actual installations. And GOG has that. So I ran Visual Studio 2012 runtime and GOG’s Mini Metro started working.

However, that was not without cost - drivers for my Asus touchpad stopped working. As soon as I reinstalled drivers, GOG’s Mini Metro stopped working. Well, since I really needed touchpad drivers so I can have touchpad disabled (if you own Asus N56VJ you’ll understand), I just unpacked Dino’s build over GOG’s and called it a day.

Two things became obvious from this. One is that Dinosaur Polo Club has an excellent support and they didn’t even blink before giving me essentially another copy of a game. Yes, since it is a DRM-free build it doesn’t really matter if I have one or two or million of them - but that is exactly why DRM-free games are awesome - support doesn’t need to go through hoops to give you another copy.

Another is that allowing the use of GOG’s Galaxy client requires a bit of magic in the game and that magic is invasive enough to make game not work. I don’t know any details on what exactly went wrong there but Galaxy seems to need a bit of growing up.