⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hmac.c

📁 NIST推荐的素域上的椭圆曲线
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Submited by Dobes Vandermeer  (dobes@smartt.com) */#include "mycrypt.h"/*    (1) append zeros to the end of K to create a B byte string        (e.g., if K is of length 20 bytes and B=64, then K will be         appended with 44 zero bytes 0x00)    (2) XOR (bitwise exclusive-OR) the B byte string computed in step        (1) with ipad (ipad = the byte 0x36 repeated B times)    (3) append the stream of data 'text' to the B byte string resulting        from step (2)    (4) apply H to the stream generated in step (3)    (5) XOR (bitwise exclusive-OR) the B byte string computed in        step (1) with opad (opad = the byte 0x5C repeated B times.)    (6) append the H result from step (4) to the B byte string        resulting from step (5)    (7) apply H to the stream generated in step (6) and output        the result*/#ifdef HMAC#define HMAC_BLOCKSIZE hash_descriptor[hash].blocksizeint hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen){    unsigned char buf[MAXBLOCKSIZE];    unsigned long hashsize;    unsigned long i, z;    int errno;    _ARGCHK(hmac != NULL);    _ARGCHK(key != NULL);    if ((errno = hash_is_valid(hash)) != CRYPT_OK) {        return errno;    }    if(key == NULL || keylen == 0) {        return CRYPT_INVALID_KEYSIZE;    }    hmac->hash = hash;    // (1) make sure we have a large enough key    hmac->hashsize = hashsize = hash_descriptor[hash].hashsize;    if(keylen > HMAC_BLOCKSIZE) {        z = sizeof(hmac->key);        if ((errno = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {           return errno;        }        if(hashsize < HMAC_BLOCKSIZE) {            zeromem(hmac->key+hashsize, HMAC_BLOCKSIZE - hashsize);        }    } else {        memcpy(hmac->key, key, keylen);        if(keylen < HMAC_BLOCKSIZE) {            zeromem(hmac->key + keylen, HMAC_BLOCKSIZE - keylen);        }    }    // Create the initial vector for step (3)    for(i=0; i < keylen;   i++) {       buf[i] = hmac->key[i] ^ 0x36;    }    for(   ; i < HMAC_BLOCKSIZE; i++) {        buf[i] = 0x36;    }    // Pre-pend that to the hash data    hash_descriptor[hash].init(&hmac->md);    hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE);    return CRYPT_OK;}int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len){    int errno;    _ARGCHK(hmac != NULL);    _ARGCHK(buf != NULL);    if ((errno = hash_is_valid(hmac->hash)) != CRYPT_OK) {        return errno;    }    hash_descriptor[hmac->hash].process(&hmac->md, buf, len);    return CRYPT_OK;}int hmac_done(hmac_state *hmac, unsigned char *hashOut){    unsigned char buf[MAXBLOCKSIZE];    unsigned char isha[MAXBLOCKSIZE];    unsigned long hashsize, i;    int hash, errno;    _ARGCHK(hmac != NULL);    _ARGCHK(hashOut != NULL);    hash = hmac->hash;    if((errno = hash_is_valid(hash)) != CRYPT_OK) {        return errno;    }    // Get the hash of the first HMAC vector plus the data    hash_descriptor[hash].done(&hmac->md, isha);    // Create the second HMAC vector vector for step (3)    hashsize = hash_descriptor[hash].hashsize;    for(i=0; i < HMAC_BLOCKSIZE; i++) {        buf[i] = hmac->key[i] ^ 0x5C;    }    // Now calculate the "outer" hash for step (5), (6), and (7)    hash_descriptor[hash].init(&hmac->md);    hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE);    hash_descriptor[hash].process(&hmac->md, isha, hashsize);    hash_descriptor[hash].done(&hmac->md, hashOut);#ifdef CLEAN_STACK    zeromem(hmac->key, sizeof(hmac->key));#endif    return CRYPT_OK;}int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,                const unsigned char *data, unsigned long len, unsigned char *dst){    hmac_state hmac;    int errno;    _ARGCHK(key != NULL);    _ARGCHK(data != NULL);    _ARGCHK(dst != NULL);    if ((errno = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {        return errno;    }      if ((errno = hmac_process(&hmac, data, len)) != CRYPT_OK) {       return errno;    }    if ((errno = hmac_done(&hmac, dst)) != CRYPT_OK) {       return errno;    }    return CRYPT_OK;}/* hmac_file added by Tom St Denis */int hmac_file(int hash, const char *fname, const unsigned char *key,                unsigned long keylen, unsigned char *dst){#ifdef NO_FILE    return CRYPT_ERROR;#else   hmac_state hmac;   FILE *in;   unsigned char buf[512];   int x, errno;   _ARGCHK(fname != NULL);   _ARGCHK(key != NULL);   _ARGCHK(dst != NULL);   if ((errno = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {       return errno;   }   in = fopen(fname, "rb");   if (in == NULL) {      return CRYPT_INVALID_ARG;   }   /* process the file contents */   do {      x = fread(buf, 1, sizeof(buf), in);      if ((errno = hmac_process(&hmac, buf, x)) != CRYPT_OK) {          fclose(in);         return errno;      }   } while (x == sizeof(buf));   fclose(in);   /* get final hmac */   if ((errno = hmac_done(&hmac, dst)) != CRYPT_OK) {      return errno;   }#ifdef CLEAN_STACK   /* clear memory */   zeromem(buf, sizeof(buf));#endif      return CRYPT_OK;#endif}/*    TEST CASES SOURCE:Network Working Group                                          P. ChengRequest for Comments: 2202                                          IBMCategory: Informational                                        R. Glenn                                                                   NIST                                                         September 1997                 Test Cases for HMAC-MD5 and HMAC-SHA-1*/int hmac_test(void){    unsigned char digest[MAXBLOCKSIZE];    int i;    struct hmac_test_case {        int num;        char *algo;        unsigned char key[128];        int keylen;        unsigned char data[128];        int datalen;        unsigned char digest[MAXBLOCKSIZE];    } cases[] = {        /*        3. Test Cases for HMAC-SHA-1        test_case =     1        key =           0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b        key_len =       20        data =          "Hi Ther     20        digest =        0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04        digest-96 =     0x4c1a03424b55e07fe7f27be1        */        { 5, "sha1",            {0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,              0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,              0x0c, 0x0c, 0x0c, 0x0c}, 20,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -