ssor family.
aboutsummaryrefslogblamecommitdiffstats
path: root/crypto/tcrypt.c
blob: 28a45a1e6f423603a563ef40c821736a88ef266b (plain) (tree)
1
2
3
4
5
6
7
8
9
  






                                                                
                                            


                                                                             
                                                                            

                     

   
                        
                      

                         
                       
                              
                         
                              
                          

                            


                   
                                                          
   
                         

  
                             


                 
 


                              
                        
 
                
                              

                        


                                                                           
                                                                          

                                                                              

  

                                                                         
 



                                 


                                                                 
                                                                           
                    
                                                                           









                                                           

                                                                   
 



                                 





                                 
                                                                           
                    
                                                                           










                                     
                                                                           
                    
                                                                           


















                                                                

                                                          
                                                                          
                                                                     
                                                               
 
                                       
                                 


                                     
                    




                                 
 
                                                      
 
                                                                
 


                                                                      

                       

                       
 

              
 

                                     
                                                         
 



                                                                           

                                         
 


                                                                           
                                                          

                                                        
                                       




                                                                   
                         
 





                                                                                 
 







                                                                        




                                                                         
 
                                

                                                                         
                            

                                                                        
 








                                                                                

    
                                   

 

                                                                     

                                                       





                                                                 










                                                              

                                                                            
 

                                 


                         
                                                                          
 

                                                                 


                                             
                                                                 


                                                                 

                                                                             


                                                   




                                                          


                 

                                                                               
 








                                 










                                                              



















                                                              

 

                                                                           
 

                                 


                         
                                                                    
 




                                 


                                             
                                                                 


                                                                 
                 
                                                   

                                 





                                    

                                     


                                             
                                                                 


                                                                 
                 


                                                   





                                      
    


                           


                           


                                                           
                 

 

                                                               
 
                                         

                                

                          
                


                                                
                                                           
 


                                                                      


                       



                                                           
                                                              
                                                                    


                         





                                                        
                                              


                                                                         





                                                                                       
                        
                                                                         
                                                                            
                    
                                                                        





                                                                      


         
                              

 
                                

                            
 

                                         
                                                    
                                                  
                       
         

 









                                              
               

                                         


                      
                                   


                      
                                    


                      

                                        


                      

                                             


                      
                                   
                      
 
               
                                      
                      
 
               

                                             


                      

                                            
                      
 
               
                                            


                      




                                                 


                      
                                      
                      
 
                
                                      


                      
                                       


                      
                                          


                      
                                          


                      
                                         


                      
                                           


                      
                                      


                      
                                        


                      
                                         


                      
                                           


                      
                                     


                      
                                     


                      
                                     


                      
                                            


                      

                                           


                      
                                      



                      
                                      


                      
                                      
                      
 
                
                                         
                      
 
                
                                            

                      
                

                                             
                      
                
                                      
                      
 
                
                                       

                      
                
                                        

                      
                
                                   

                      
                
                                        

                      
                
                                             

                      
                
                                      


                      
                                      

                      
                
                                      


                      




                                         

                      
                 
                                         
                      
 
                 
                                          
                      
 
                 
                                            

                      
                 
                                            


                      
                                            
                      
 
                 
                                            
                      
 
                 
                                         

                      
                 
                                            


                      
                                            

                      
                 
                                                                    
                                                         
                                                                    
                                                         
                                                                    
                                                         
                                                                    
                                                         
                                                                    
                                                         
                                                                    
                                                         
                                                                    
                                                         
                                                                    
                                                         


                      
                                                                
                                                                        
                                                   
                                                                
                                                                        
                                                   
                                                                
                                                                        
                                                   
                                                                
                                                                        
                                                   


                      
                                                                        
                                                         
                                                                        
                                                         
                                                                        
                                                         
                                                                        
                                                         


                      
                                                                         
                                                       
                                                                         
                                                       
                                                                         
                                                       
                                                                         
                                                       


                      
                                                                    
                                                    
                                                                    
                                                    
                                                                    
                                                    
                                                                    
                                                    

                      

                                                                         
                                                         
                                                                         
                                                         
                                                                         
                                                         
                                                                         
                                                         

                      

                                                                   
                                                        

                      



                                  
                                                                         


                                                    
                                                                         


                                                    
                                                                          


                                                    
                                                                            


                                                    
                                                                            


                                                    
                                                                            


                                                    
                                                                           


                                                    
                                                                           


                                                    
                                                                           


                                                    
                                                                            


                                                    
                                                                            


                                                    
                                                                            

                                                    



                                                                            







                                                                            







                                                                            


                      


                                 


         
                                       
 
                          
              
 




                                                               
 
                      






                                                            

                      


                                                   

                   





                                                                          
                                            
 

                             

                           
                           

                                                                          



                                                          
/*
 * Quick & dirty crypto testing module.
 *
 * This will only exist until we have a better testing mechanism
 * (e.g. a char device).
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
 * Copyright (c) 2007 Nokia Siemens Networks
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

#include <crypto/hash.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/scatterlist.h>
#include <linux/string.h>
#include <linux/moduleparam.h>
#include <linux/jiffies.h>
#include <linux/timex.h>
#include <linux/interrupt.h>
#include "tcrypt.h"

/*
 * Need slab memory for testing (size in number of pages).
 */
#define TVMEMSIZE	4

/*
* Used by test_cipher_speed()
*/
#define ENCRYPT 1
#define DECRYPT 0

/*
 * Used by test_cipher_speed()
 */
static unsigned int sec;

static int mode;
static char *tvmem[TVMEMSIZE];

static char *check[] = {
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
	"lzo", "cts", NULL
};

static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
			       struct scatterlist *sg, int blen, int sec)
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
		else
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);

		if (ret)
			return ret;
	}

	printk("%d operations in %d seconds (%ld bytes)\n",
	       bcount, sec, (long)bcount * blen);
	return 0;
}

static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
			      struct scatterlist *sg, int blen)
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;

	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
		else
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);

		if (ret)
			goto out;
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();
		if (enc)
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
		else
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
		end = get_cycles();

		if (ret)
			goto out;

		cycles += end - start;
	}

out:
	local_irq_enable();
	local_bh_enable();

	if (ret == 0)
		printk("1 operation in %lu cycles (%d bytes)\n",
		       (cycles + 4) / 8, blen);

	return ret;
}

static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };

static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
			      struct cipher_speed_template *template,
			      unsigned int tcount, u8 *keysize)
{
	unsigned int ret, i, j, iv_len;
	const char *key, iv[128];
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
	u32 *b_size;

	if (enc == ENCRYPT)
	        e = "encryption";
	else
		e = "decryption";

	printk("\ntesting speed of %s %s\n", algo, e);

	tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);

	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
		return;
	}
	desc.tfm = tfm;
	desc.flags = 0;

	i = 0;
	do {

		b_size = block_sizes;
		do {
			struct scatterlist sg[TVMEMSIZE];

			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
				printk("template (%u) too big for "
				       "tvmem (%lu)\n", *keysize + *b_size,
				       TVMEMSIZE * PAGE_SIZE);
				goto out;
			}

			printk("test %u (%d bit key, %d byte blocks): ", i,
					*keysize * 8, *b_size);

			memset(tvmem[0], 0xff, PAGE_SIZE);

			/* set key, plain text and IV */
			key = tvmem[0];
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
			}

			ret = crypto_blkcipher_setkey(tfm, key, *keysize);
			if (ret) {
				printk("setkey() failed flags=%x\n",
						crypto_blkcipher_get_flags(tfm));
				goto out;
			}

			sg_init_table(sg, TVMEMSIZE);
			sg_set_buf(sg, tvmem[0] + *keysize,
				   PAGE_SIZE - *keysize);
			for (j = 1; j < TVMEMSIZE; j++) {
				sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
				memset (tvmem[j], 0xff, PAGE_SIZE);
			}

			iv_len = crypto_blkcipher_ivsize(tfm);
			if (iv_len) {
				memset(&iv, 0xff, iv_len);
				crypto_blkcipher_set_iv(tfm, iv, iv_len);
			}

			if (sec)
				ret = test_cipher_jiffies(&desc, enc, sg,
							  *b_size, sec);
			else
				ret = test_cipher_cycles(&desc, enc, sg,
							 *b_size);

			if (ret) {
				printk("%s() failed flags=%x\n", e, desc.flags);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);

out:
	crypto_free_blkcipher(tfm);
}

static int test_hash_jiffies_digest(struct hash_desc *desc,
				    struct scatterlist *sg, int blen,
				    char *out, int sec)
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		ret = crypto_hash_digest(desc, sg, blen, out);
		if (ret)
			return ret;
	}

	printk("%6u opers/sec, %9lu bytes/sec\n",
	       bcount / sec, ((long)bcount * blen) / sec);

	return 0;
}

static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
			     int blen, int plen, char *out, int sec)
{
	unsigned long start, end;
	int bcount, pcount;
	int ret;

	if (plen == blen)
		return test_hash_jiffies_digest(desc, sg, blen, out, sec);

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
		for (pcount = 0; pcount < blen; pcount += plen) {
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
		}
		/* we assume there is enough space in 'out' for the result */
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
	}

	printk("%6u opers/sec, %9lu bytes/sec\n",
	       bcount / sec, ((long)bcount * blen) / sec);