开发者

kernel project des encryption

In a kernel level project I want to encrypt a packet using des algorithm , need to use the 开发者_运维技巧function

static int des_setkey(void *ctx,const u8 *key,
                         unsigned int keylen, 
                         u32 *flags) 

I want to know how the key to be passed to the function , which value to be used as the to the key , its a pointer type so will be an address value


http://lxr.kelp.or.kr/ident?v=2.6.28;i=des_setkey

* Cryptographic API.
*
* s390 implementation of the DES Cipher Algorithm.
*
* Copyright IBM Corp. 2003,2007
* Author(s): Thomas Spatzier
*            Jan Glauber (jan.glauber@de.ibm.com)

typeA A=xy;
functionname(typeA* a, typeA b)
--> functionname(&A,A);



crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags)
{
        u32 n, w;

        n  = parity[key[0]]; n <<= 4;
        n |= parity[key[1]]; n <<= 4;
        n |= parity[key[2]]; n <<= 4;
        n |= parity[key[3]]; n <<= 4;
        n |= parity[key[4]]; n <<= 4;
        n |= parity[key[5]]; n <<= 4;
        n |= parity[key[6]]; n <<= 4;
        n |= parity[key[7]];
        w = 0x88888888L;

        if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY)
            && !((n - (w >> 3)) & w)) {  /* 1 in 



-->CRYPTO_TFM_REQ_WEAK_KEY is a flag --> source/include/linux/crypto.h

/*
* Transform masks and values (for crt_flags).
*/
#define CRYPTO_TFM_REQ_MASK             0x000fff00
#define CRYPTO_TFM_RES_MASK             0xfff00000

#define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
#define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
#define CRYPTO_TFM_REQ_MAY_BACKLOG      0x00000400
#define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
#define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
#define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
#define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
#define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000



source/arch/s390/crypto/des_s390.c

#define DES_EXPKEY_WORDS        32
typedef unsigned char           u8;
typedef unsigned char           u8;
typedef unsigned short          u16;
typedef unsigned int            u32;
typedef unsigned long long      u64;
typedef signed char             s8;
typedef short                   s16;
typedef int                     s32;
typedef long long               s64;



static int des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
{
         return setkey(((struct des_ctx *)ctx)->expkey, key, keylen, flags);
}



struct des_ctx {
        u32 expkey[DES_EXPKEY_WORDS];
};


This looks like it is from the 2.6.16 (ish) linux kernel. Later kernels have seen the crypto system api re-written slightly.

for this kernel this might help:-

  • *key is a pointer to a 16 byte array, ( unsigned char[16] ).
  • *ctx is a pointer to a struct des_ctx structure;
  • *flags is a pointer to a 32bit unsigned int that will have CRYPTO_TFM_RES_WEAK_KEY set if you have supplied a weak key ( one that fails FIPS 74 - see the ekey() function )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜