📄 ssx31b_cipher_digest.c
字号:
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); /* Add the working vars back into context.state[] */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; /* Wipe variables */ a = b = c = d = e = 0; memset (block32, 0x00, sizeof block32);}/* SHA1Init - Initialize new context */static inline voidSHA1Init(sha1_ctx_t* ctx){ /* SHA1 initialization constants */ const static sha1_ctx_t initstate = { 0, { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 }, { 0, } }; *ctx = initstate;}/* Run your data through this. */static voidSHA1Update(sha1_ctx_t* ctx, const u8* data, unsigned len){ unsigned i, j; j = (ctx->count >> 3) & 0x3f; ctx->count += len << 3; if ((j + len) > 63) { memcpy(&ctx->buffer[j], data, (i = 64-j)); SHA1Transform(ctx->state, ctx->buffer); for ( ; i + 63 < len; i += 64) { SHA1Transform(ctx->state, &data[i]); } j = 0; } else i = 0; memcpy(&ctx->buffer[j], &data[i], len - i);}/* Add padding and return the message digest. */static voidSHA1Final(sha1_ctx_t* ctx, u8 digest[20]){ const static u8 padding[64] = { 0x80, }; u32 i, j, index, padLen; u64 t; u8 bits[8] = { 0, }; t = ctx->count; bits[7] = 0xff & t; t>>=8; bits[6] = 0xff & t; t>>=8; bits[5] = 0xff & t; t>>=8; bits[4] = 0xff & t; t>>=8; bits[3] = 0xff & t; t>>=8; bits[2] = 0xff & t; t>>=8; bits[1] = 0xff & t; t>>=8; bits[0] = 0xff & t; /* Pad out to 56 mod 64 */ index = (ctx->count >> 3) & 0x3f; padLen = (index < 56) ? (56 - index) : ((64+56) - index); SHA1Update(ctx, padding, padLen); /* Append length */ SHA1Update(ctx, bits, sizeof bits); /* Store state in digest */ for (i = j = 0; i < 5; i++, j += 4) { u32 t2 = ctx->state[i]; digest[j+3] = t2 & 0xff; t2>>=8; digest[j+2] = t2 & 0xff; t2>>=8; digest[j+1] = t2 & 0xff; t2>>=8; digest[j ] = t2 & 0xff; } /* Wipe context */ memset(ctx, 0, sizeof *ctx);}/* * */static intsha1_open(struct digest_context *cx, int atomic){ sha1_ctx_t *const ctx = (sha1_ctx_t *) cx->digest_info; SHA1Init (ctx); return 0;}static intsha1_update(struct digest_context *cx, const u8 *in, int size, int atomic){ sha1_ctx_t *const ctx = (sha1_ctx_t *) cx->digest_info; SHA1Update (ctx, in, size); return 0;}static intsha1_digest(struct digest_context *cx, u8 *out, int atomic){ sha1_ctx_t *const ctx = (sha1_ctx_t *) cx->digest_info; sha1_ctx_t *const ctx_tmp = kmalloc (sizeof *ctx_tmp, GFP_KERNEL); if (!ctx_tmp) return -ENOMEM; *ctx_tmp = *ctx; SHA1Final (ctx_tmp, out); kfree (ctx_tmp); return 0;}static intsha1_close(struct digest_context *cx, u8 *out, int atomic){ sha1_ctx_t *const ctx = (sha1_ctx_t *) cx->digest_info; static u8 tmp[20]; SHA1Final(ctx, out ? out : tmp); return 0;}#else /*NOT_ONLY_HMAC*/static int ssx31b_default_open (struct digest_context *cx, int atomic) {return 0;}static int ssx31b_default_update(struct digest_context *cx, const u8 *in, int size, int atomic) {return 0;}static int ssx31b_default_digest(struct digest_context *cx, u8 *out, int atomic) {return 0;}static int ssx31b_default_close(struct digest_context *cx, u8 *out, int atomic) {return 0;}#endif /*NOT_ONLY_HMAC*/struct digest_context *ssx31b_default_realloc_digest_context(struct digest_context *old_cx, struct digest_implementation *di){ struct digest_context *cx; if (old_cx) di->free_context (old_cx); cx = kmalloc(sizeof (struct digest_context) + di->working_size, GFP_KERNEL); if (!cx) return NULL; cx->di = di; /* let digest_info point behind the context */ cx->digest_info = (void *)((char *)cx) + sizeof(struct digest_context); if(ssx31b_ctx_mapping_set(cx)<0){ kfree(cx); return NULL; } return cx;}voidssx31b_default_free_digest_context(struct digest_context * cx){ if(cx==NULL) return; ssx31b_ctx_mapping_clear(cx); kfree(cx); return;}#define SSX31B_DIGEST_MD5_ID md5#define SSX31B_DIGEST_MD5_BLOCKSIZE 16 /* sizeof (md5_ctx_t.hash) */int md5_hmac(struct digest_context *cx, const u8 *key, int key_len, const u8 *in, int size, u8 *hmac, int atomic){ int status; int saNum; unsigned long OutDatLen; unsigned short usOpt=0; /*000 0000 00: ?? MD5*/ unsigned char ucOpt=0x0; /*0x10 0: hash only*/ if (key_len != 16) { return -EINVAL; } saNum=ssx31b_ctx_mapping_getsanum(cx); if(saNum<0) return -1; status=ssx31b_sa_register( usOpt, NULL, (unsigned char*)key, saNum); if(status<0) return -1; status=ssx31b_crypto_perform(ucOpt, NULL, 8, saNum, in, size, hmac, &OutDatLen); if(status!=0) return -1; return 0;}#define SSX31B_DIGEST_SHA1_ID sha1#define SSX31B_DIGEST_SHA1_BLOCKSIZE 20int sha1_hmac(struct digest_context *cx, const u8 *key, int key_len, const u8 *in, int size, u8 *hmac, int atomic){ int status; int saNum; unsigned long OutDatLen; unsigned short usOpt=1; /*000 0000 00: ?? SHA1*/ unsigned char ucOpt=0x0; /*0x10 0: hash only*/ if (key_len != 20) { return -EINVAL; } saNum=ssx31b_ctx_mapping_getsanum(cx); if(saNum<0) return -1; status=ssx31b_sa_register( usOpt, NULL, (unsigned char*)key, saNum); if(status<0) return -1; status=ssx31b_crypto_perform(ucOpt, NULL, 8, saNum, in, size, hmac, &OutDatLen); if(status!=0) return -1; return 0;}#ifdef MODULE_WITH_TESTstruct cipher_context* setup_cipher (const char *ciphername, const u8 *key, u32 keylen){ struct cipher_context *cx = NULL; struct cipher_implementation* ci = NULL; ci = find_cipher_by_name (ciphername, 1 /* == atomic - i.e. cipher must not sleep */); if (!ci) /* cipher not found */ return NULL; ci->lock (); cx = ci->realloc_context (NULL, ci, /* max expected */ keylen); if (!cx) /* some error */ { ci->unlock (); return NULL; } if (ci->set_key (cx, key, keylen) < 0) { /* error while setting key */ ci->wipe_context(cx); ci->free_context(cx); ci->unlock (); return NULL; } return cx; /* everything ok so far */}int test_cipher (struct cipher_context *cx, u8 *data, u32 datalen){ int error = 0; int i; unsigned short blklen=cx->ci->blocksize;u32 datalenAligned=(datalen+blklen-1)/blklen*blklen; /* * operation when passing IV as argument */ //u8 iv[cx->ci->ivsize] = { 0, }; u8 iv[cx->ci->ivsize]; //u8 iv[32]; #if 0for(i=0;i<cx->ci->ivsize;i++) iv[i]=(i+0x21)&0xff; if ((error = cx->ci->encrypt_iv (cx, data, data, datalen, iv)) < 0) return error; /* *data should contain ciphertext now */#if 0 printk("After EN_iv:\n"); for(i=0;i<datalenAligned;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif if ((error = cx->ci->decrypt_iv ( cx, data, data, datalenAligned, iv)) < 0) return error; /* *data should contain plaintext again */ /* * same stuff done w/ IV stored in context */#if 0 printk("After DE_iv:\n"); for(i=0;i<datalen;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif#endif#if 1 //memset (cx->iv, 0, cx->ci->ivsize); /* clear IV */ if ((error = cx->ci->encrypt (cx, data, data, datalen)) < 0) return error; /* *data should contain ciphertext now */#if 1 printk("After EN:\n"); for(i=0;i<datalenAligned;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif if ((error = cx->ci->decrypt ( cx, data, data, datalenAligned)) < 0) return error;#if 1 printk("After DE:\n"); for(i=0;i<datalen;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif#endif return 0;}free_cipher (struct cipher_context* cx){ /* assert (cx != NULL); */ struct cipher_implementation* ci = cx->ci; ci->wipe_context(cx); ci->free_context(cx); ci->unlock();}void ssx31b_crypto_test_cipher(){ struct cipher_context * cx; unsigned char ciphername[32]; unsigned long keylen,datalen; unsigned char *data=NULL; int i; unsigned long keyL[8]={ 0x33334444, 0x31323334, 0xc4a7d8a2, 0xbec44af2, 0x33334444, 0x31323334, 0xaabbccdd, 0x89ab4567 }; unsigned char* key= (unsigned char*)keyL; unsigned short blklen; keylen=16; /*des:8, 3des:24, aes:16,24,32,scb2:16 */ //strcpy(ciphername,"3des-cbc"); //strcpy(ciphername,"aes-cbc"); strcpy(ciphername,"scb2-cbc"); datalen=256; cx=setup_cipher(ciphername, key, keylen); if(cx==NULL){ printk("ssx31b_crypto_test_cipher: setup_cipher return err\n"); return; } blklen=cx->ci->blocksize; data=kmalloc((datalen+blklen-1)/blklen*blklen,GFP_KERNEL); for(i=0;i<datalen;i++) data[i]=i&0xff;#if 0 printk("Orig data:\n"); for(i=0;i<(datalen+blklen-1)/blklen*blklen;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif if(0!=test_cipher( cx, data, datalen)){ printk("ssx31b_crypto_test_cipher: test_cipher return err\n"); kfree(data); free_cipher(cx); return; }#if 0 printk("Final data:\n"); for(i=0;i<datalen;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif kfree(data); free_cipher(cx); return; }struct digest_context* setup_digest(const char *digestname) { int error = 0; struct digest_context *dx = NULL; struct digest_implementation* di = NULL; di = find_digest_by_name(digestname, 1); /* atomic */ if (!di) /* unknown digest */ return NULL; di->lock(); dx = di->realloc_context(NULL, di); /* allocate a context */ if (!dx) { /* error */ di->unlock(); return NULL; } return dx; /* return the context, ready for use */ }int test_digest(struct digest_context *dx, u8 *output, u8 *data, u32 datalen, u8* key, u32 keylen) { int error = 0; /* Initialize the digest context */ if ((error = dx->di->open(dx)) < 0) return error; /* update the digest context with the data */ if ((error = dx->di->update(dx, data, datalen)) < 0) return error; if ((error = dx->di->close(dx, output)) < 0) /* output digest */ return error; if ((error = dx->di->hmac(dx, key,keylen,data,datalen,data)) < 0) /* hmac */ return error; /* try printing the digest */ printk("digest: %s\n", output); return 0; }void free_digest(struct digest_context* dx) { /* assert (dx != NULL); */ struct digest_implementation* di = dx->di; di->free_context(dx); di->unlock(); } void ssx31b_crypto_test_hmac(){ struct digest_context * dx; unsigned char hmacname[32]; unsigned long keylen,datalen; unsigned char *data=NULL; int i; unsigned long keyL[10]={ 0x33334444, 0x31323334, 0xc4a7d8a2, 0xbec44af2, 0x11223344, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; unsigned char* key= (unsigned char*)keyL; unsigned short blklen; keylen=16; /*md5:16, sha1:20*/ strcpy(hmacname,"md5"); datalen=32; dx=setup_digest(hmacname); if(dx==NULL){ printk("ssx31b_crypto_test_hmac: setup_digest return err\n"); return; } blklen=dx->di->blocksize; data=kmalloc((datalen+blklen-1)/blklen*blklen,GFP_KERNEL); for(i=0;i<datalen;i++) data[i]=i&0xff;#if 0 printk("Orig data:\n"); for(i=0;i<(datalen+blklen-1)/blklen*blklen;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif if(0!=test_digest(dx, data, data, datalen, key, keylen)){ printk("ssx31b_crypto_test_hmac: test_digest return err\n"); kfree(data); free_digest(dx); return; }#if 0 printk("Final data:\n"); for(i=0;i<datalen;i++){ printk("%02x",data[i]); if((i+1)%4==0) printk(" "); if((i+1)%32==0) printk("\n"); }#endif kfree(data); free_digest(dx); return; }#endif /*MODULE_WITH_TEST*/#include "./ssx31b_cipher_digest.h"EXPORT_NO_SYMBOLS;/* eof */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -