1st try to convert cifs to use CryptoAPI
RFC PATCH 1/1 1st try to convert cifs to use CryptoAPI
Hi,
I'm trying to learn a thing or two about the new CryptoAPI, and I thought a good way to learn was to try to accomplish one of the minor items on the Crypto-TODO list. So:
CIFS currently use it's own MD4/MD5 code. I've started to convert it to using the CryptoAPI, but I'm far from an CIFS/CryptoAPI expert, so my question is: is this patch totally bogus, or am I doing something right?
I haven't tested it, it's not beautiful, it's probably broken, but I hope I'm using the CryptoAPI correct... (yup, I know the extern things are ugly, should create a cifsencrypt.h header or something...)
(If there is some meaning in this patch, I'd like to improve it and try to continue to convert the rest of CIFS to the CryptoAPI)
Here it is:
fs/cifs/cifsencrypt.c +74 -17
@@ -20,10 +20,11 @@*/ #include <linux/fs.h> +#include <linux/crypto.h> +#include <asm/scattelist.h> #include "cifspdu.h" #include "cifsglob.h" #include "cifs_debug.h" -#include "md5.h" #include "cifs_unicode.h" /* Calculate and return the CIFS signature based on the mac key and the smb pdu */
@@ -32,20 +33,37 @@/* Note that the smb header signature field on input contains the sequence number before this function is called */ -extern void mdfour (unsigned char *out,unsigned char *in,int n);extern void E_md4hash (const unsigned char *passwd,unsigned char *p16); - +/* smbencrypt.c */ +extern void SMBencrypt_prepare_keys (u8 *ikey,u8 *okey,u8 *in,int len); +extern void SMBencrypt_finish_md5 (u8 *key,u8 *digest); + +/* cifsfs.c */ +extern spinlock_t md5_tfm_lock; +extern spinlock_t md4_tfm_lock; +extern struct crypto_tfm *md5_tfm; +extern struct crypto_tfm *md4_tfm; +static int cifs_calculate_signature (const struct smb_hdr * cifs_pdu,const char * key,char * signature) { -struct MD5Context context; +struct scatterlist sg[2 ];if ((cifs_pdu ==NULL ) || (signature ==NULL ))return -EINVAL ; -MD5Init (&context); -MD5Update (&context,key,CIFS_SESSION_KEY_SIZE +16 ); -MD5Update (&context,cifs_pdu->Protocol,cifs_pdu->smb_buf_length); -MD5Final (signature,&context); + sg[0 ].page =virt_to_page (key); + sg[0 ].offset =offset_in_page (key); + sg[0 ].length =CIFS_SESSION_KEY_SIZE +16 ; + sg[1 ].page =virt_to_page (cifs_pdu->Protocol); + sg[1 ].offset =offset_in_page (cifs_pdu->Protocol); + sg[1 ].length = cifs_pdu->smb_buf_length; + +spin_lock (&md5_tfm_lock); +crypto_digest_init (md5_tfm); +crypto_digest_update (md5_tfm, sg,2 ); +crypto_digest_final (md5_tfm, signature); +spin_unlock (&md5_tfm_lock); +return 0 ; }
@@ -136,29 +154,42 @@int cifs_calculate_mac_key (char * key,const char * rn,const char * password) {char temp_key[16 ]; +struct scatterlist sg; +if ((key ==NULL ) || (rn ==NULL ) || (password ==NULL ))return -EINVAL ; + sg.page =virt_to_page (temp_key); + sg.offset =offset_in_page (temp_key); + sg.length =16 ; +E_md4hash (password, temp_key); -mdfour (key,temp_key,16 ); + +spin_lock (&md4_tfm_lock); +crypto_digest_init (md4_tfm); +crypto_digest_update (md4_tfm, sg,1 ); +crypto_digest_final (md4_tfm, key); +spin_unlock (&md4_tfm_lock);memcpy (key+16 ,rn,CIFS_SESSION_KEY_SIZE ); +return 0 ; }int CalcNTLMv2_partial_mac_key (struct cifsSesInfo * ses,struct nls_table * nls_info) {char temp_hash[16 ]; -struct HMACMD5Context ctx;char * ucase_buf;wchar_t * unicode_buf;unsigned int i,user_name_len,dom_name_len; +u8 ikey[65 ], okey[65 ]; +struct scatterlist sg[2 ];if (ses)return -EINVAL ;E_md4hash (ses->password_with_pad, temp_hash); -hmac_md5_init_limK_to_64 (temp_hash,16 , &ctx); +SMBencrypt_prepare_keys (ikey, okey, temp_hash,16 ); user_name_len =strlen (ses->userName);if (user_name_len >MAX_USERNAME_SIZE )return -EINVAL ;
@@ -183,24 +214,50 @@ dom_name_len =cifs_strtoUCS (unicode_buf+user_name_len, ucase_buf,MAX_USERNAME_SIZE *2 , nls_info); unicode_buf[user_name_len + dom_name_len] =0 ; -hmac_md5_update ((const unsigned char *) unicode_buf, - (user_name_len+dom_name_len)*2 ,&ctx); -hmac_md5_final (ses->mac_signing_key,&ctx); + sg[0 ].page =virt_to_page (ikey); + sg[0 ].offset =offset_in_page (ikey); + sg[0 ].length =64 ; + sg[1 ].page =virt_to_page (unicode_buf); + sg[1 ].offset =offset_in_page (unicode_buf); + sg[1 ].length = (user_name_len+dom_name_len)*2 ; + +spin_lock (&md5_tfm_lock); +crypto_digest_init (md5_tfm); +crypto_digest_update (md5_tfm, sg,2 ); +crypto_digest_final (md5_tfm, ses->mac_signing_key); +spin_unlock (&md5_tfm_lock); + +SMBencrypt_finish_md5 (okey, ses->mac_signing_key); +kfree (ucase_buf);kfree (unicode_buf);return 0 ; }void CalcNTLMv2_response (const struct cifsSesInfo * ses,char * v2_session_response) { +u8 ikey[65 ], okey[65 ]; +struct scatterlist sg[2 ];struct HMACMD5Context context; +memcpy (v2_session_response +8 , ses->server->cryptKey,8 );/* gen_blob(v2_session_response + 16); */ -hmac_md5_init_limK_to_64 (ses->mac_signing_key,16 , &context); -hmac_md5_update (ses->server->cryptKey,8 ,&context); +SMBencrypt_prepare_keys (ikey, okey, ses->mac_signing_key,16 ); + + sg[0 ].page =virt_to_page (ikey); + sg[0 ].offset =offset_in_page (ikey); + sg[0 ].length =64 ; + sg[1 ].page =virt_to_page (ses->server->cryptKey); + sg[1 ].offset =offset_in_page (ses->server->cryptKey); + sg[1 ].length =8 ;/* hmac_md5_update(v2_session_response+16)client thing,8,&context); */ /* BB fix */ +spin_lock (&md5_tfm_lock); +crypto_digest_init (md5_tfm); +crypto_digest_update (md5_tfm, sg,2 ); +crypto_digest_final (md5_tfm, v2_session_response); +spin_unlock (&md5_tfm_lock); -hmac_md5_final (v2_session_response,&context); +SMBencrypt_finish_md5 (okey, v2_session_response); }
fs/cifs/cifsfs.c +26 -0
@@ -32,6 +32,7 @@#include <linux/list.h> #include <linux/seq_file.h> #include <linux/vfs.h> +#include <linux/crypto.h> #include "cifsfs.h" #include "cifspdu.h" #define DECLARE_GLOBALS_HERE
@@ -60,6 +61,10 @@unsigned int sign_CIFS_PDUs =1 ;unsigned int CIFSMaximumBufferSize =CIFS_MAX_MSGSIZE ;struct task_struct * oplockThread =NULL ; +static struct crypto_tfm *md5_tfm; +static struct crypto_tfm *md4_tfm; +static spinlock_t md5_tfm_lock =SPIN_LOCK_UNLOCKED ; +static spinlock_t md4_tfm_lock =SPIN_LOCK_UNLOCKED ;extern int cifs_mount (struct super_block *,struct cifs_sb_info *,char *,const char *);
@@ -643,6 +648,18 @@ rc =cifs_init_inodecache ();if (!rc) { + md5_tfm =crypto_alloc_tfm ("md5" ,0 ); +if (unlikely (md5_tfm ==NULL )) { +printk (KERN_ERROR "failed to load transform for md5\n" ); + rc =0 ; + } + md4_tfm =crypto_alloc_tfm ("md4" ,0 ); +if (unlikely (md4_tfm ==NULL )) { +printk (KERN_ERROR "failed to load transform for md4\n" ); + rc =0 ; + } + } +if (!rc) { rc =cifs_init_mids ();if (!rc) { rc =cifs_init_request_bufs ();
@@ -659,6 +676,7 @@ }cifs_destroy_inodecache (); } +#ifdef CONFIG_PROC_FS cifs_proc_clean ();#endif
@@ -680,6 +698,14 @@send_sig (SIGTERM , oplockThread,1 );wait_for_completion (&cifs_oplock_exited); } +if (likely (md5_tfm !=NULL )) { +crypto_free_tfm (md5_tfm); + md5_tfm =NULL ; + } +if (likely (md4_tfm !=NULL )) { +crypto_free_tfm (md4_tfm); + md4_tfm =NULL ; + } }MODULE_AUTHOR ("Steve French <[email protected]>" );
fs/cifs/smbencrypt.c +128 -22
230 lines not on this page · raw message