C++实践(五)C++实现认证算法:基于SHA-512的HMAC

基于SHA-512的HMAC算法

SHA是使用最广泛的Hash函数。其家族有SHA-1,SHA-2(包括SHA-256/SHA-384/SHA-512)。SHA1与SHA2都使用了同样的迭代结构和模算术与二元逻辑操作。在本实验中,我们采用SHA-512。

SHA-512算法

SHA-512的逻辑

算法的输入为长度小于128位的信息(实际实现中我们假定输入的信息长度小于64位),
输出时512位的消息摘要。输入信息以1024位的小组为单位处理。过程如图:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第1张图片
该过程包含如下步骤:
①附加填充位
在输入消息后面填充一个1,再填充若干个0使其长度模1024与896同余。不管输入消息长度如何,都要进行填充。
②附加长度
在消息后面附加一个128bit的块表示原始消息的长度。本实验中假设所有输入的消息长度都小于2^64-1,所以我们只处理最后附加块的低64bit。
③初始化Hash缓冲区
Hash函数的中间结果和最终结果都保存在512bit的缓冲区中。该缓冲区可以用8个64bit的寄存器表示。初始值如下:
mState[0] = 0x6a09e667f3bcc908;
mState[1] = 0xbb67ae8584caa73b;
mState[2] = 0x3c6ef372fe94f82b;
mState[3] = 0xa54ff53a5f1d36f1;
mState[4] = 0x510e527fade682d1;
mState[5] = 0x9b05688c2b3e6c1f;
mState[6] = 0x1f83d9abfb41bd6b;
mState[7] = 0x5be0cd19137e2179;
注意这些值以高位在前的格式存储。由于我们采用的是Intel x86系列CPU,该型CPU为小端存储,满足要求,所以为简便起见这里我们不再作任何处理。
④以1024为单位处理信息,共有80轮,如下图所示:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第2张图片
⑤输出
所有N个1024分组处理完毕后,从缓冲区输出512bit的消息摘要。

SHA-512的轮函数

每一轮的过程由如下函数定义:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第3张图片
ROTRn(x)=对64位变量循环右移n位。
轮消息Wt的产生如下图所示:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第4张图片
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第5张图片
其中SHRn(x)对64比特变量x向左移动n位,右边填充0。

基于SHA-512的HMAC算法

算法描述如图:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第6张图片
首先,判断输入的密钥K的长度是否符合要求:若K的长度大于1024bits,则我们用SHA-512对K作一次Hash,得到的输出长度将为512bits,作为新的密钥;接着将密钥的长度扩展到1024bits(在密钥的左边填入0)。
然后按照下式计算:
这里写图片描述
其中,opad[i]=0x5c,ipad[i]=0x36,opad与ipad的长度与密钥的长度相同,即1024bits.

SHA-512测试

编写SHA512类

该类主要有四个成员函数,其中void init()进行必要的初始化工作,即初始化静态常量(轮常数等);void process(const unsigned char *msg, unsigned long inlen)从输入的消息产生消息摘要;void hash(unsigned char *out)获取消息摘要;void calc()进行每轮的必要计算。

测试SHA512类

参考FIPS Publication 180-2中给出的两个例子,比对消息分组、每轮运算时的缓冲区的值,结果符合预期。
①测试数据message=”abc”,
hash= 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f

实验结果如下:
消息扩展过程中,将数据块各字分别赋值给W0~W15:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第7张图片
第46、47轮时缓冲区的值如下,经比对与FIPS Publication 180-2中给出的结果一致。
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第8张图片
最后得到消息摘要,在main函数中我们检查产生的消息摘要,当该消息摘要与预先准备的hash值一致时会输出“true!”。
这里写图片描述

②测试数据:
message=”abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu”
hash= 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
这个测试数据的长度刚好是896bits,非常有代表性。
测试结果如下,符合预期。
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第9张图片
经过这两步测试,我们的SHA512类基本正确了。
利用网站http://www.atool.org/hash.php 我们可以进一步测试SHA512类。

基于SHA-512的Hmac测试

编写HMAC类

HMAC类继承了SHA512类。在构造函数中我们进行初始化、对密钥长度的处理、计算HMAC值。
构造函数:HMAC(const unsigned char *in, uint64_t len, const unsigned char *key, uint64_t keyLen, unsigned char *out);
注意:为了通用,我们传入的是unsigned char 的指针。所以还必须传入消息的长度及密钥的长度,因为输入的消息、密钥都是unsigned char类型的指针,我们必须要知道该指针指向的数据块的长度。产生消息并使用HMAC类的对象应该知道消息、密钥的长度。
void keyPadding(const unsigned char *key, uint64_t keyLen)进行密钥长度预处理。

测试

测试数据为:message=”abc”,key=”abc”。测试结果如下:
C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第10张图片
测试数据为:
message=”abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu”
key=”abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmno
pqklmnopqrlmnopqrsmnopqrstnopqrstu”
结果如下:

C++实践(五)C++实现认证算法:基于SHA-512的HMAC_第11张图片
由于没有找到HMAC-SHA-512的测试数据集,所以实验结果是否正确还有待进一步考量。但从结果看(例子见参考文献),密钥预处理步骤基本正确。

sha512.h

/************************************************************************/
/*SHA512:       
/*Author:       chenweiliang
/*Version:      1.0
/*Note:         To see detail information please uncomment macro DEBUG
/*Reference:    library Tomcrypt
/************************************************************************/
#include 
#include 

#ifndef __SHA_512_H
#define __SHA_512_H
#endif // !__SHA_512_H

#ifndef DEBUG
//#define DEBUG
#endif

#define STORE64H(x, y)                                                                          \
{  (y)[0] = (unsigned char)(((x) >> 56) & 255); (y)[1] = (unsigned char)(((x) >> 48) & 255);     \
    (y)[2] = (unsigned char)(((x) >> 40) & 255); (y)[3] = (unsigned char)(((x) >> 32) & 255);     \
    (y)[4] = (unsigned char)(((x) >> 24) & 255); (y)[5] = (unsigned char)(((x) >> 16) & 255);     \
    (y)[6] = (unsigned char)(((x) >> 8) & 255); (y)[7] = (unsigned char)((x)& 255); }
#define LOAD64H(x, y)                                                               \
   { x = (((uint64_t)((y)[0] & 255)) << 56) | (((uint64_t)((y)[1] & 255)) << 48) | \
   (((uint64_t)((y)[2] & 255)) << 40) | (((uint64_t)((y)[3] & 255)) << 32) | \
   (((uint64_t)((y)[4] & 255)) << 24) | (((uint64_t)((y)[5] & 255)) << 16) | \
   (((uint64_t)((y)[6] & 255)) << 8) | (((uint64_t)((y)[7] & 255))); }

typedef unsigned long long uint64_t;

class SHA512{
public:
    SHA512();
    virtual ~SHA512();
    void hash(unsigned char *out);
    void process(const unsigned char *in, unsigned long inlen);
protected:
    //Length of the hashcode:64bytes
    static const int    HASHCODE_SIZE = 64;

    //Length of the block size:128bytes
    static const int    MSG_BLOCK_SIZE = 128;
private:
    static const uint64_t const_K[80];

    uint64_t mState[8];

    //length of the message
    uint64_t mLength;

    //length of the current block
    uint64_t mCurLen;

    //current block of the message
    unsigned char mBlock[128];


    uint64_t ch(uint64_t x, uint64_t y, uint64_t z);
    uint64_t maj(uint64_t x, uint64_t y, uint64_t z);
    uint64_t rotr(uint64_t x, int n);
    uint64_t shr(uint64_t x, int n);

    uint64_t sigma0(uint64_t x);
    uint64_t sigma1(uint64_t x);
    uint64_t gamma0(uint64_t x);
    uint64_t gamma1(uint64_t x);

    void init();
    void calc();
public:
    void print1024(unsigned char *data);
    void print64(unsigned char data[]);
};

sha512.cpp

/************************************************************************/
/*SHA512:       
/*Author:       chenweiliang
/*Version:      1.0
/*Note:         To see detail information please uncomment macro DEBUG
/*Reference:    library Tomcrypt
/************************************************************************/
#ifndef __SHA_512_H
#include "sha_512.h"
#endif // !__SHA_512_H


const uint64_t SHA512::const_K[80] =
{
    0x428a2f98d728ae22, 0x7137449123ef65cd,
    0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
    0x3956c25bf348b538, 0x59f111f1b605d019,
    0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
    0xd807aa98a3030242, 0x12835b0145706fbe,
    0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
    0x72be5d74f27b896f, 0x80deb1fe3b1696b1,
    0x9bdc06a725c71235, 0xc19bf174cf692694,
    0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
    0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
    0x2de92c6f592b0275, 0x4a7484aa6ea6e483,
    0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
    0x983e5152ee66dfab, 0xa831c66d2db43210,
    0xb00327c898fb213f, 0xbf597fc7beef0ee4,
    0xc6e00bf33da88fc2, 0xd5a79147930aa725,
    0x06ca6351e003826f, 0x142929670a0e6e70,
    0x27b70a8546d22ffc, 0x2e1b21385c26c926,
    0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
    0x650a73548baf63de, 0x766a0abb3c77b2a8,
    0x81c2c92e47edaee6, 0x92722c851482353b,
    0xa2bfe8a14cf10364, 0xa81a664bbc423001,
    0xc24b8b70d0f89791, 0xc76c51a30654be30,
    0xd192e819d6ef5218, 0xd69906245565a910,
    0xf40e35855771202a, 0x106aa07032bbd1b8,
    0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
    0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
    0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
    0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
    0x748f82ee5defb2fc, 0x78a5636f43172f60,
    0x84c87814a1f0ab72, 0x8cc702081a6439ec,
    0x90befffa23631e28, 0xa4506cebde82bde9,
    0xbef9a3f7b2c67915, 0xc67178f2e372532b,
    0xca273eceea26619c, 0xd186b8c721c0c207,
    0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
    0x06f067aa72176fba, 0x0a637dc5a2c898a6,
    0x113f9804bef90dae, 0x1b710b35131c471b,
    0x28db77f523047d84, 0x32caab7b40c72493,
    0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
    0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
    0x5fcb6fab3ad6faec, 0x6c44198c4a475817
};



SHA512::SHA512()
{
    init();
}

void SHA512::init()
{
    mLength = 0;
    mState[0] = 0x6a09e667f3bcc908;
    mState[1] = 0xbb67ae8584caa73b;
    mState[2] = 0x3c6ef372fe94f82b;
    mState[3] = 0xa54ff53a5f1d36f1;
    mState[4] = 0x510e527fade682d1;
    mState[5] = 0x9b05688c2b3e6c1f;
    mState[6] = 0x1f83d9abfb41bd6b;
    mState[7] = 0x5be0cd19137e2179;
}

void SHA512::process(const unsigned char *in, unsigned long inlen)
{
    init();
    mCurLen = 0;
    for (int j = 0; j < inlen; j++){
        mBlock[mCurLen] = in[j];
        mCurLen++;
        mLength += 8;
        if (mCurLen == 128){
            mCurLen = 0;
#ifdef DEBUG
            printf("Block %d\n", inlen / 128);
            print1024(mBlock);
#endif // DEBUG
            calc();
        }
    }

    //append the '1' bit 
    mBlock[mCurLen] = 0x80;
    mCurLen++;

    //if the length is currently above 112 bytes we append zeros and calc()
    // Then we can fall back to padding zeros and length encoding like normal
    if (mCurLen > 112){
        for (; mCurLen < 128; mCurLen++){
            mBlock[mCurLen] = 0x00;
        }
        calc();
        mCurLen = 0;
    }

    //We assume that the data length is less than 2^64
    //Hence,the length of the message is 64-bit,namely 8 bytes.
    while (mCurLen <= 120){
        mBlock[mCurLen] = 0x00;
        mCurLen++;
    }

    //store length
    for (int i = mCurLen; i < 128; i++){
        mBlock[i] = (unsigned char)(mLength >> (8 * (127 - i)));
    }
#ifdef DEBUG
    printf("last Block:\n");
    print1024(mBlock);
#endif // DEBUG

    calc();


}



void SHA512::hash(unsigned char *out)
{
    for (int i = 0; i < 8; i++){
        STORE64H(mState[i], out + (8 * i));
    }
}

void SHA512::calc()
{
    uint64_t S[8];
    uint64_t W[80];
    uint64_t t0, t1;

    //copy state into S
    for (int i = 0; i < 8; i++){
        S[i] = mState[i];
    }
//  for (int i = 0; i < 128; i += 8)
//      W[i >> 3] =
//      ((uint64_t)mBlock[i + 0]) << 56 | ((uint64_t)mBlock[i + 1]) << 48 | ((uint64_t)mBlock[i + 2]) << 40 |
//      ((uint64_t)mBlock[i + 3]) << 32 | ((uint64_t)mBlock[i + 4]) << 24 | ((uint64_t)mBlock[i + 5]) << 16 |
//      ((uint64_t)mBlock[i + 6]) << 8 | ((uint64_t)mBlock[i + 7]);
    for (int i = 0; i < 16; i++) {
        LOAD64H(W[i], mBlock + (8 * i));
    }
#ifdef DEBUG
    unsigned char tw[128];
    for (int i = 0; i < 16; i++){
        STORE64H(W[i], tw + (8 * i));
    }
    for (int i = 0; i < 16; i++){
        printf("W%d=", i);
        print64(tw+i*8);
    }
#endif // DEBUG

    //fill W[16..79]
    for (int i = 16; i < 80; i++){
        W[i] = gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16];
    }

    for (int i = 0; i < 80; i++){
        t0 = S[7] + ch(S[4], S[5], S[6]) + sigma1(S[4]) + W[i] + const_K[i];
        t1 = sigma0(S[0]) + maj(S[0], S[1], S[2]);
        S[7] = S[6];
        S[6] = S[5];
        S[5] = S[4];
        S[4] = S[3] + t0;
        S[3] = S[2];
        S[2] = S[1];
        S[1] = S[0];
        S[0] = t0 + t1;
        unsigned char temp[64];
#ifdef DEBUG
        for (int i = 0; i < 8; i++){
            STORE64H(S[i], temp + (8 * i));
        }
        printf("--------------------------Round %d------------------------------\n",i);
        printf("a:");
        print64(temp);
        printf("b:");
        print64(temp + 8);
        printf("c:");
        print64(temp + 16);
        printf("d:");
        print64(temp + 24);
        printf("e:");
        print64(temp + 32);
        printf("f:");
        print64(temp + 40);
        printf("g:");
        print64(temp + 48);
        printf("h:");
        print64(temp + 56);
#endif // DEBUG

    }

    // feedback 
    for (int i = 0; i < 8; i++){
        mState[i] += S[i];
    }

}

inline uint64_t SHA512::ch(uint64_t x, uint64_t y, uint64_t z)
{
    return ((x& y) ^ ((~x) & z));
}

inline uint64_t SHA512::maj(uint64_t x, uint64_t y, uint64_t z)
{
    return (x&y) ^ (x&z) ^ (y&z);
}

inline uint64_t SHA512::rotr(uint64_t x, int n)
{
    return  ((x >> n) | (x << (64 - n)));
}

inline uint64_t SHA512::shr(uint64_t x, int n)
{
    return x >> n;
}

inline uint64_t SHA512::sigma0(uint64_t x)
{
    return rotr(x, 28) ^ rotr(x, 34) ^ rotr(x, 39);
}

inline uint64_t SHA512::sigma1(uint64_t x)
{
    return rotr(x, 14) ^ rotr(x, 18) ^ rotr(x, 41);
}

inline uint64_t SHA512::gamma0(uint64_t x)
{
    return rotr(x, 1) ^ rotr(x, 8) ^ shr(x, 7);
}

inline uint64_t SHA512::gamma1(uint64_t x)
{
    return rotr(x, 19) ^ rotr(x, 61) ^ shr(x, 6);
}

SHA512::~SHA512()
{

}

void SHA512::print1024(unsigned char *data)
{
    for (int i = 0; i < 128; i++){
        printf("%02hhx", data[i]);
        if (i % 8 == 7){
            printf(" ");
        }
        if (i % 32 == 31){
            printf("\n");
        }
    }
}

void SHA512::print64(unsigned char data[]){
    for (int i = 0; i < 8; i++){
        printf("%02hhx", data[i]);
    }
    printf("\n");
}

hmac.h

/************************************************************************/
/*HMAC:         Use SHA512
/*Author:       chenweiliang
/*Version:      1.0
/*Note:         To see detail information please uncomment macro DEBUG
/*Reference:    javafr_RFC_4493
/************************************************************************/
#ifndef __SHA_512_H
#include "sha_512.h"
#endif // !__SHA_512_H

#ifndef __HMAC_H
#define __HMAC_H
#endif

class HMAC : public SHA512
{
public:
    HMAC();
    HMAC(const unsigned char *in, uint64_t len, const unsigned char *key, uint64_t keyLen, unsigned char *out);
    ~HMAC();
protected:
private:
    //
    const static unsigned char IPAD = 0x36;
    const static unsigned char OPAD = 0x5c;

    //The key after padding
    unsigned char mKpadded[MSG_BLOCK_SIZE];

    //Length of the key in bytes
    unsigned long mKeyLen;

    unsigned char mSi[MSG_BLOCK_SIZE];

    unsigned char mSo[MSG_BLOCK_SIZE];

    void keyPadding(const unsigned char *key, uint64_t keyLen);
};

hmac.cpp

/************************************************************************/
/*HMAC:         Use SHA512
/*Author:       chenweiliang
/*Version:      1.0
/*Note:         To see detail information please uncomment macro DEBUG
/*Reference:    javafr_RFC_4493
/************************************************************************/
#ifndef __HMAC_H
#include "hmac.h"
#endif // !__HMAC_H

#include 
#include 
#include 

HMAC::HMAC()
{

}

HMAC::HMAC(const unsigned char *in, uint64_t len, const unsigned char *key, uint64_t keyLen, unsigned char *out)
{
    //out1=H(Si||M)
    unsigned char out1[64];

    keyPadding(key, keyLen);
    //Get Si
    for (int i = 0; i < MSG_BLOCK_SIZE; i++){
        mSi[i] = mKpadded[i] ^ IPAD;
    }
    //Get So
    for (int i = 0; i < MSG_BLOCK_SIZE; i++){
        mSo[i] = mKpadded[i] ^ OPAD;
    }

    unsigned long length1 = len + MSG_BLOCK_SIZE;
    unsigned char *in1 = (unsigned char *)malloc(length1);

    //in1=Si||M
    for (int i = 0; i < length1; i++){
        if (i < MSG_BLOCK_SIZE){
            in1[i] = mKpadded[i];
        }
        else{
            in1[i] = in[i - MSG_BLOCK_SIZE];
        }
    }

    //out1=H(Si||M)
    process(in1, length1);
    free(in1);
    hash(out1);

    //in2=So||H(Si||M)
    unsigned long length2 = MSG_BLOCK_SIZE + HASHCODE_SIZE;
    unsigned char *in2 = (unsigned char *)malloc(length2);
    for (int i = 0; i < length2; i++){
        if (i < MSG_BLOCK_SIZE){
            in2[i] = mSo[i];
        }
        else{
            in2[i] = out1[i - MSG_BLOCK_SIZE];
        }
    }
    //out=H( So || H(Si||M) )
    process(in2, length2);
    free(in2);
    hash(out);
}

void HMAC::keyPadding(const unsigned char *key, uint64_t keyLen){
    mKeyLen = keyLen;

    //Generate a 512-bit-key
    if (mKeyLen > MSG_BLOCK_SIZE){
        process((unsigned char *)key, mKeyLen);
        hash(mKpadded);
        mKeyLen = 64;
    }
    //Fill the left part of the key with 0
    else if (mKeyLen <= MSG_BLOCK_SIZE){
        for (int i = 0; i < MSG_BLOCK_SIZE; i++){
            if (i < MSG_BLOCK_SIZE - mKeyLen){
                mKpadded[i] = 0x00;
            }
            else{
                mKpadded[i] = (unsigned char)key[i - (MSG_BLOCK_SIZE - mKeyLen)];
            }
        }
    }
    printf("Key after padding:\n");
    print1024(mKpadded);
}

HMAC::~HMAC()
{

}

参考文献

[1]FIPS180-2with change notice.
[2] 密码学算法库TomCrypt
[3] javafr_RFC_4493
[4] RSA算法实现与蒙哥马利算法http://www.cnblogs.com/zhtxwd/archive/2012/02/09/2344165.html
[5] 四种加密算法之RSA源代码-C++ http://blog.csdn.net/zmb2011/article/details/6842099
[6] 密码编码学与网络安全:原理与实践

你可能感兴趣的:(编程-C++,C++,sha512,hmac)