www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Encryption in Phobos?

reply kinghajj <kinghajj_member pathlink.com> writes:
Does anyone else agree with me that Phobos should contain encryption functions?
Many applications today encrypt their data, and it would be nice if Rijndael
could be implemented in D. Even something simple like XTEA would suffice, in my
opinion.
Mar 08 2006
next sibling parent Brad Anderson <brad dsource.dot.org> writes:
kinghajj wrote:
 Does anyone else agree with me that Phobos should contain encryption functions?
 Many applications today encrypt their data, and it would be nice if Rijndael
 could be implemented in D. Even something simple like XTEA would suffice, in my
 opinion.
 
 

I'm working on blowfish encryption ... will make public somewhere on dsource.org when finished. BA
Mar 08 2006
prev sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
On Thu, 09 Mar 2006 00:30:49 -0500, kinghajj  
<kinghajj_member pathlink.com> wrote:

 Does anyone else agree with me that Phobos should contain encryption  
 functions?
 Many applications today encrypt their data, and it would be nice if  
 Rijndael
 could be implemented in D. Even something simple like XTEA would  
 suffice, in my
 opinion.

I guess it'd be ok to add encryption. I wrote a RC4 module in D a long time ago but didn't release it mainly becuase I'm not sure how legal it is. Like http://en.wikipedia.org/wiki/Rc4 says `The name "RC4" is trademarked, however. The current status seems to be that "unofficial" implementations are legal, but cannot use the RC4 name. RC4 is often referred to as "ARCFOUR" (Alleged-RC4, because RSA has never officially released the algorithm), to avoid possible trademark problems.` and `RC4 (or ARCFOUR) is the most widely-used software stream cipher and is used in popular protocols`.
Mar 08 2006
parent reply kinghajj <kinghajj_member pathlink.com> writes:
In article <op.s54vc4mspo9bzi moe>, Chris Miller says...
I guess it'd be ok to add encryption.

I wrote a RC4 module in D a long time ago but didn't release it mainly  
becuase I'm not sure how legal it is. Like  
http://en.wikipedia.org/wiki/Rc4 says `The name "RC4" is trademarked,  
however. The current status seems to be that "unofficial" implementations  
are legal, but cannot use the RC4 name. RC4 is often referred to as  
"ARCFOUR" (Alleged-RC4, because RSA has never officially released the  
algorithm), to avoid possible trademark problems.` and `RC4 (or ARCFOUR)  
is the most widely-used software stream cipher and is used in popular  
protocols`.

Here's an XTEA implementation I made. /* Implementation of the XTEA encryption algorithm in D. By Samuel Fredrickson. Based on public domain C code by David Wheeler and Roger Needham. See: http://en.wikipedia.org/wiki/XTEA */ /* This module is part of the public domain. Enjoy :) */ /* Encrypts a block of data. */ private void encipherBlock(uint[2] v, uint[4] k) { uint v0 = v[0], v1 = v[1], i; uint sum = 0, delta = 0x9E3779B9; for(i = 0; i < 32; i++) { v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); sum += delta; v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); } v[0] = v0; v[1] = v1; } /* Decrypts a block of data. */ private void decipherBlock(uint[2] v, uint[4] k) { uint v0 = v[0], v1 = v[1], i; uint sum = 0xC6EF3720, delta = 0x9E3779B9; for(i = 0; i < 32; i++) { v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); sum -= delta; v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); } v[0] = v0; v[1] = v1; } /* Encrypts a block of data. Unlike encipherBlock, this function takes char[] arrays, much more "D-like." */ void encryptBlock(char[8] data, char[16] key) { uint[2] v; uint[4] k; // place data into v v[0] = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]); v[1] = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | (data[7]); // place key into k k[0] = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) | (key[3]); k[1] = (key[4] << 24) | (key[5] << 16) | (key[6] << 8) | (key[7]); k[2] = (key[8] << 24) | (key[9] << 16) | (key[10] << 8) | (key[11]); k[3] = (key[12] << 24) | (key[13] << 16) | (key[14] << 8) | (key[15]); // encrypt encipherBlock(v, k); data[0] = v[0]; data[1] = v[0] >> 8; data[2] = v[0] >> 16; data[3] = v[0] >> 24; data[4] = v[1]; data[5] = v[1] >> 8; data[6] = v[1] >> 16; data[7] = v[1] >> 24; } /* Decrypts a block of data. Unlike decipherBlock, this function takes char[] arrays, much more "D-like." */ void decryptBlock(char[8] data, char[16] key) { uint[2] v; uint[4] k; // place data into v v[0] = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]); v[1] = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | (data[7]); // place key into k k[0] = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) | (key[3]); k[1] = (key[4] << 24) | (key[5] << 16) | (key[6] << 8) | (key[7]); k[2] = (key[8] << 24) | (key[9] << 16) | (key[10] << 8) | (key[11]); k[3] = (key[12] << 24) | (key[13] << 16) | (key[14] << 8) | (key[15]); // encrypt decipherBlock(v, k); data[3] = v[0]; data[2] = v[0] >> 8; data[1] = v[0] >> 16; data[0] = v[0] >> 24; data[7] = v[1]; data[6] = v[1] >> 8; data[5] = v[1] >> 16; data[4] = v[1] >> 24; }
Mar 09 2006
next sibling parent Brad Anderson <brad dsource.dot.org> writes:
kinghajj wrote:
 In article <op.s54vc4mspo9bzi moe>, Chris Miller says...
 I guess it'd be ok to add encryption.

 I wrote a RC4 module in D a long time ago but didn't release it mainly  
 becuase I'm not sure how legal it is. Like  
 http://en.wikipedia.org/wiki/Rc4 says `The name "RC4" is trademarked,  
 however. The current status seems to be that "unofficial" implementations  
 are legal, but cannot use the RC4 name. RC4 is often referred to as  
 "ARCFOUR" (Alleged-RC4, because RSA has never officially released the  
 algorithm), to avoid possible trademark problems.` and `RC4 (or ARCFOUR)  
 is the most widely-used software stream cipher and is used in popular  
 protocols`.

Here's an XTEA implementation I made. /* Implementation of the XTEA encryption algorithm in D. By Samuel Fredrickson. Based on public domain C code by David Wheeler and Roger Needham. See: http://en.wikipedia.org/wiki/XTEA */ /* This module is part of the public domain. Enjoy :) */ /* Encrypts a block of data. */ private void encipherBlock(uint[2] v, uint[4] k) { uint v0 = v[0], v1 = v[1], i; uint sum = 0, delta = 0x9E3779B9; for(i = 0; i < 32; i++) { v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); sum += delta; v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); } v[0] = v0; v[1] = v1; } /* Decrypts a block of data. */ private void decipherBlock(uint[2] v, uint[4] k) { uint v0 = v[0], v1 = v[1], i; uint sum = 0xC6EF3720, delta = 0x9E3779B9; for(i = 0; i < 32; i++) { v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); sum -= delta; v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); } v[0] = v0; v[1] = v1; } /* Encrypts a block of data. Unlike encipherBlock, this function takes char[] arrays, much more "D-like." */ void encryptBlock(char[8] data, char[16] key) { uint[2] v; uint[4] k; // place data into v v[0] = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]); v[1] = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | (data[7]); // place key into k k[0] = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) | (key[3]); k[1] = (key[4] << 24) | (key[5] << 16) | (key[6] << 8) | (key[7]); k[2] = (key[8] << 24) | (key[9] << 16) | (key[10] << 8) | (key[11]); k[3] = (key[12] << 24) | (key[13] << 16) | (key[14] << 8) | (key[15]); // encrypt encipherBlock(v, k); data[0] = v[0]; data[1] = v[0] >> 8; data[2] = v[0] >> 16; data[3] = v[0] >> 24; data[4] = v[1]; data[5] = v[1] >> 8; data[6] = v[1] >> 16; data[7] = v[1] >> 24; } /* Decrypts a block of data. Unlike decipherBlock, this function takes char[] arrays, much more "D-like." */ void decryptBlock(char[8] data, char[16] key) { uint[2] v; uint[4] k; // place data into v v[0] = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]); v[1] = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | (data[7]); // place key into k k[0] = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) | (key[3]); k[1] = (key[4] << 24) | (key[5] << 16) | (key[6] << 8) | (key[7]); k[2] = (key[8] << 24) | (key[9] << 16) | (key[10] << 8) | (key[11]); k[3] = (key[12] << 24) | (key[13] << 16) | (key[14] << 8) | (key[15]); // encrypt decipherBlock(v, k); data[3] = v[0]; data[2] = v[0] >> 8; data[1] = v[0] >> 16; data[0] = v[0] >> 24; data[7] = v[1]; data[6] = v[1] >> 8; data[5] = v[1] >> 16; data[4] = v[1] >> 24; }

What would be nice is a consistent interface for all the encryption algorithms. I don't have encryptBlock() or decipherBlock() in my blowfish implementation and don't know what the correct ones are, but if they were consistent in one unified D encryption lib, that'd be cool. A good starting point might be what Regan did in Deimos. http://svn.dsource.org/projects/deimos/trunk/etc/crypto/hash BA
Mar 09 2006
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 10 Mar 2006 03:16:01 +0000 (UTC), kinghajj wrote:


 Here's an XTEA implementation I made.

I have a XTEA with extensions coded in Euphoria so I might port that to D too. Would be interesting to compare notes. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 10/03/2006 2:30:14 PM
Mar 09 2006
parent kinghajj <kinghajj_member pathlink.com> writes:
On Fri, 10 Mar 2006 03:16:01 +0000 (UTC), kinghajj wrote:
 Here's an XTEA implementation I made.

Oops. Just tested my code again and encryptBlock/decryptBlock don't work correctly. encipherBlock/decipherBlock do work, however.
Mar 09 2006