• 沒有找到結果。

Round function

Chapter 4 Experimental Results and Analysis

4.1.3. Round function

DES needs 16 times of round function to complete encryption and decryption.

Generally we make a DES function and recursive call this function to complete 3-DES encryption and decryption. In fact, this may waste lots of CPU time to move data between CPU memory and GPU memory. The solution to reduce the unnecessary memory access is to do 64 times of round function continuously. Thus program only needs two memory accesses to finish one 3-DES encryption or decryption.

The main important things to rewrite the Round function are to care about the on

25

chip memory access. We use a constant unsigned long memory to store our spbox.

Constant memory’s speed is usually faster than global memory and would not occupy the space of shared memory. After assign the accuracy values to the spbox, we do the round function for 64 times just like the following Fig. 4-2.

Fig. 4-2 Round function details

4.2. 3-DES Encode implementation

Since we want to design a 3-DES encryption program, we need to consider all kinds of file type. For example in the word files like PowerPoint or WordPad, it uses

‘\0’ to be its end symbol. In this situation that we just need to determine what the input word is. But in streaming data like music or movie files, the ‘\0’ is not the end symbol. This means we need to adjust our program to fit all the file types.

Moreover, we input 64 * n bits plaintext once rather than 64 bits plaintext. This While (I < 64)

{

//phase i

work = ((right >> 4) | (right << 28)) ^ ks[i];

left ^= Spbox[6][work & 0x3f];

left ^= Spbox[4][(work >> 8) & 0x3f];

left ^= Spbox[2][(work >> 16) & 0x3f];

left ^= Spbox[0][(work >> 24) & 0x3f];

work = right ^ ks[i+1];

left ^= Spbox[7][work & 0x3f];

left ^= Spbox[5][(work >> 8) & 0x3f];

left ^= Spbox[3][(work >> 16) & 0x3f];

left ^= Spbox[1][(work >> 24) & 0x3f];

}

26

may make a mistake for us to determine where the end symbol is. For instance, if we input 1000 words of plaintext once and the end symbol ‘\0’ is the 1250th word of the plaintext, we must need to do the input loop for two times to input all the plaintext.

However after we do the loop for two times, we read the 2000th word of the plaintext.

Because the 2000th word is a empty word that compiler could not recognize, the loop would not be stopped.

For the two reasons we describe above, we bring up follow pseudo codes to explain how we solve these problems. First is the pseudo code of 3-DES encryption in Fig. 4-3.

27

Fig. 4-3 3-DES Encryption Pseudo code

In the figure above, it is obviously that only the function named __global__3DES_encryption () would be executed in GPU. This is because of parallel factor. Besides the __global__3DES_encryption function, only the key_schedule function can be parallelized. But the key_schedule function has few computation that it is not worth executing in CUDA, its memory access time is larger than its computation time.

Another interesting thing deserves to mention is the input function. Since we Algorithm 3-DES Encryption ((plaintext, ciphertext) ;

Input: plaintext (64 * N bits plaintext).

Output: ciphertext (64 * N bits ciphertext).

// N means how many threads we use in device.

begin

declare integer plaintext[64 * N], temptext[64 * N], ciphertext[64 * N];

declare integer key1[64], key2[64], key3[64], key[192] ; declare integer plaintextsize = 64 * N;

While(not reach the end of file) {

input_EN (plaintext, temptext); //put plaintext tinto temp buffer key = key_schedule (key1, key2, key3); // generate subkeys

copy temptext form host memory to device memory __global__3DES_encryption (temptext, key);

copy temptext form device memory to host memory swap (ciphertext , temptext);

output_EN (ciphertext);

} end

28

want to design a mature code to support all the file types, we need to devise new end symbol judgment methods. Our method is to design a new end symbol for all file types. If input 1000 words which equals to 8000 bits of plaintext once, the design details are follows:

I. Design a new end symbol

End symbol is a unique word that only recognized by program, so we cannot choose the common word to be the end symbol. We choose the word “/nend/ab”

for our end symbol. This can distinguish the end symbol from other words.

II. Add end symbol to plaintext every 1000 words

The program will check if the plaintext reaches the end every 1000 words. We use fread() to determine if we had already read in all the plaintext. If fread() return the value of 8000, which means input 8000 bits or 1000 words. This means the file may not reach the end. We should add the end symbol to this point and go on search for the end of the plaintext. So we accurately output 1008 words (1000 words + 8 words end symbol) at one time.

III. Find out the end of the plaintext

While the value returned form fread() is less than 8000, this means we have found the end of the plaintext. Since we process 1000 words once, we need to fill it to reach 1000 words. The end symbol “/nend/ab” is only 8 words. After the end symbol, we fill the vacancy with “\0” to reach 1008 words.

IV. The stop condition

After we find out the end of the plaintext, next round the fread() would return the value of 0. 0 means nothing left in the plaintext, so the program will break the loop to stop 3-DES encryption.

29

Fig. 4-4 Input Details of 3-DES Encryption Pseudo Code

4.3. 3-DES Decode implementation

3-DES decode implementation is similar to 3-DES encode implementation. But 3-DES decryption needs more decision operation, which means that needs more CPU

Algorithm 3-DES input_EN (plaintext, *temptext) ; Input: plaintext (64 * N bits ciphertext).

Output: ciphertext (64 * N bits plaintext).

// N means how many threads we use in device.

begin

int trueinputsize = inputsize -8;

int len = fread(cp , sizeof(unsigned char), trueinputsize, fileinput);

if(len equals to the size of input file) { // add end symbol after the end

if( len is less than the size of input file and len doesn’t equals to 0) { // add end symbol after the end and fill 1008 words with ‘\0’

30

time to determine when to stop. Unlike 3-DES encode implementation, the input of 3-DES decryption is all cipher texts. We need to decode the cipher texts before we analysis it.

Fig. 4-5 3-DES Decryption Pseudo code

The main implementation difference between encryption and decryption is the decision condition. In 3-DES encryption program, system can use feof() to see if the file reaches the end. But in 3-DES decryption, all inputs are cipher texts. If we just

Algorithm 3-DES Decryption (plaintext, *temptext) ; Input: ciphertext (64 * N bits ciphertext).

Output: plaintext (64 * N bits plaintext).

// N means how many threads we use in device.

begin

declare integer plaintext[64 * N], temptext[64 * N], ciphertext[64 * N];

declare integer key1[64], key2[64], key3[64], key[192] ; declare integer ciphertextsize = 64 * N;

While(not reach the end of file) {

Input_DE (ciphertext, temptext, ciphertextsize); //put ciphertext into temp buffer

key = key_schedule (key3, key2, key1); // generate subkeys

copy temptext form host memory to device memory __global__3DES_encryption (temptext, key);

copy temptext form device memory to host memory swap (plaintext , temptext);

output_DE (plaintext);

} end

31

decode and output, we would get the plaintext with many end symbols inside. The following figure is our implement details.

I. Input 1008 words and decode

While this program starts, it would input 1008 words form cipher text. After inputting 1008 words, the program will send the cipher text to GPU for decoding.

This phase will continue until there are no words of cipher text to input.

II. Analysis after decoding

After decoding, the program would analysis the last eight words of the plaintext.

If the eight words is “/nend/ab”, it means the input file does not reach the end.

The program will output the result without “/nend/ab” and return to stage one.

Else if the eight words is not “/nend/ab”, it means the input file reaches the end.

The program should trace the 1008 words to find out where the end symbol is.

When the program finds out the position of the end symbol, it would send the result without “/nend/ab\0\0\0……” to plaintext. After that, the program will break from the loop and finish all the jobs.

32

Fig. 4-6 Output function of 3-DES Decryption Pseudo code

Algorithm 3-DES output_DE ((plaintext, *temptext) ; Input: ciphertext (64 * N bits ciphertext).

Output: plainrtext (64 * N bits plaintext).

// N means how many threads we use in device.

begin

int countertemp = 0;

if (there is nothing inputs form file) return; // reach the file end

if( (temptext[len-8] = ‘/’ ) && ( temptext [len-7] = 'n' ) &&

(temptext [len-6] = 'e’) && ( temptext [len-5] = 'n ) &&;

(temptext [len-4] = 'd’) && ( temptext [len-3] = '/' ) &&

(temptext [len-2] = 'a') && ( temptext [len-1] = 'b') }

Countertemp= inputsize - 8; // set the size for output esle

{

unsigned char *temp = temptext;

for( int i = 0; i < inputsize; i ++ )

countertemp ++; // set the size for output }

}

fwrite(temptext, size of (unsigned char), countertemp, Decodeoutput);

// Decodeoutput is a pointer to the output file.

end

33

相關文件