# Custom crypto library in C

I will be making (in the near future) an app that will contain some pretty heavy crypto. As it is possible that my app will be ported to different devices (initially it will be iOs only, but Mac, Android & maybe even Windows ports will eventually exist). The question is how to make the crypto library as portable as possible. I'm thinking about writing it as a C11 header & .c implementation. That way it would be easy to integrate on every platform ( I may use Androids NDK to access the C code from Java). As I understand, Visual C++ can use C headers and functions. Then I will just write a wrapper class in the platforms default language (Objective-C 2.0 for iOs) that will access the cryptographic primitives from the C library...

1. Is this an ok design?

2. I will be using sleep() to make all operations last the same time (anti-timing attacks); does this work the same on all platforms? Should this be done in the wrapper rather than the C library?

3. Should I use open source implementations of the algorithms, or just write my own? (I would like all the code in the crypto library to be 100% C11)

BTW: I'm going to use some non-standard algorithms that most public libraries don't have (Serpent, TwoFish, ThreeFish, as a key derivation function I'm thinking about scrypt) ...

-
I don't understand how the sleep function can help you prevent timing attacks : how do you know how much time you need to sleep ? Also if your using the worst case running time as a reference you need to know what it is precisely on the platform your library is running which requires fine tuning after every install which. May I ask why you're not using and existing library (NaCl, openssl ect) ? –  Alexandre Yamajako Apr 29 '13 at 13:13
Or you could just use existing portable crypto solutions (yes, yes, there are some). By the way, using sleep() won't do anything against side channel attacks, an attacker will be able to distinguish the processor sleeping from the processor actually doing crypto work. –  Thomas Apr 29 '13 at 13:14
@Thomas, not if all they have access to is say network traffic, right? How would they be able to distinguish a sleeping processor vs a proc doing crypto work? I agree that if they have physical access to the device your statement is true. Not sure if that is something the OP is worried about, however. –  mikeazo Apr 29 '13 at 13:17
@mikeazo Yes, with only network access it wouldn't be possible, I agree with you, though for handheld devices I would assume physical access is rather easy. But the question is quite vague on the intended threat model. –  Thomas Apr 29 '13 at 14:34
If you think that adding calls to sleep is a good way of preventing timing attacks against a C library, you have absolutely no business implementing your own crypto. –  Stephen Touset Apr 29 '13 at 16:46

Almost all other languages can call C code, so using C is a safe bet. Serpent, Twofish, Threefish and scrypt all provide C implementations. (See the links.) Some even provide optimized code. Writing objC or C++ wrappers seems unnecessary since both can call C funtions.

The sleep(3) library function is part of POSIX. So you'll find it on linux, mac, *BSD but maybe not MS Windows. POSIX systems generally also have nanosleep(2) which has much better granularity (nanoseconds instead of seconds). But as others mentioned, it is possible to differentiate between a process that is sleeping and one that is actually working. It might be better to do some work to fill up the time. (e.g. check if a large random number is a prime, invert a big random matrix et cetera)

Writing your own implementations is definitely not recommended. Errors in the reference implementations will generally be found, publicly discussed and fixed. Who is going to fix your code if you don't publish it? And I would suggest using the reference implementations as-is, unless your changes pass all the existing tests.

-
Thank you, I will be using Botan or Crypto++. –  user1028028 May 4 '13 at 15:48