各种加密模式在TLS协议中的运用 1 (Mac-then-Encrypt)

11 篇文章 8 订阅
11 篇文章 4 订阅

Mac-then-Encrypt

老的算法譬如:

TLS_RSA_WITH_AES_128_CBC_SHA
TLS_RSA_WITH_RC4_128_MD5
都是mac-then-encrypt模式。

加密流程图:

这里写图片描述

上面涉及到2个操作(算上padding计算的话是3个操作)

HMAC

操作1:计算HMAC,这个HMAC操作就是“MAC-then-encrypt”中的MAC操作,作用是计算摘要,校验完整性用。

HMAC实现:

(1):RFC 2104 上HMAC的描述

   We define two fixed and different strings ipad and opad as follows
   (the 'i' and 'o' are mnemonics for inner and outer):

                  ipad = the byte 0x36 repeated B times
                  opad = the byte 0x5C repeated B times.

   To compute HMAC over the data `text' we perform

                    H(K XOR opad, H(K XOR ipad, text))

H就是某个MD操作,MD5、SHA1、SHA2、SM3等。
B是摘要算法的blocksize,不同摘要算法的的blocksize不同。

专业的写法,理解起来较复杂。

(2):伪代码

s32 hmac(struct md *op, u8 *key, s32 key_len,u8 *in, s32 in_len,
            u8 *out)
{
    u32 i;
    u8 *p;
    u8 tmp1[64],tmp2[64];
    //64 is the max size of blocksize of all digest alg

    //unlikely
    if(key_len > op->block_size)
    {
        op>do_digest(op, key, key_len, tmp2);
        key_len = op->md_len;
    }
    else
    {
        memcpy(tmp2, key, key_len);
    }

    p = malloc(64 + (in_len > op->md_len ?(in_len):(op->md_len)));

    memcpy(p, tmp2, key_len);
    memset(p + key_len,0,op->block_size - key_len);

    for(i=0; i<op->block_size; i++)
    {
        p[i]^=0x36;
    }

    memcpy(p + op->block_size, in, in_len);

    //save result to tmp1
    op->do_digest(op ,p, op->block_size + in_len, tmp1);

    memcpy(p, tmp2, key_len);
    memset(p + key_len, 0, op->block_size - key_len);   

    for(i=0; i < op->block_size; i++)
    {
        p[i]^=0x5c;
    }

    memcpy(p + op->block_size, tmp1, op->md_len);
    op->do_digest(op, p, op->block_size + op->md_len, out);

    return 0;
}
HMAC文字描述:

(1):key和0x36亦或,结果为k1
(2):k1 + in 送入摘要函数计算一个结果,结果为tmp1
(3):key和0x35亦或,结果为k2
(4):k2 + tmp1送入摘要函数计算一个结果,这个结果就是最终的hmac值。

HMAC入参:

(1):具体摘要算法,例如是MD5还是SHA1还是其他。
(2):key。
(3):待处理的plaintext。

TLS协议中使用HMAC

加密流程图中,我们知道,数据在被加密前,需要先使用HMAC计算mac值
TLS协议中,HMAC的入参
(1):key,是通过master key计算后,key derive得到的mac key。
(2):摘要算法,是握手中协商的摘要算法。
(3):header+plain text

第3个入参就是本篇着重要介绍的。
header:
counter(8) + type(1) + version(2) + length(2)
共13字节。

(1):counter是计数器,TLS 一个端维护两个counter,一个用于read,一个用于write,目的是为了防止重放攻击,可以理解为salt。每计算一个密文,counter++。
(2):type是指数据类型,即其handshake类型。对于明文数据,type是0x17(比如http的get,response),对于握手数据,type是0x16,比如finished报文,或者renegotiation时,所有握手报文被加密时的这个type都是0x16。
(3):version是TLS类型,0x0301, 0x0302,0x0303等。。
(4):length是后面plain text的长度。
例如我们这个例子buf[]={ 0x11, 0x12, 0x13, 0x14 },
header为
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x1 0x17 0x03 0x03 0x00 0x04
hmac的入参就是
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x1 0x17 0x03 0x03 0x00 0x04 0x11 0x12 0x13 0x14

Padding

对于流加密,比如rc4算法,ChaCha20-Poly算法都是流加密算法,不需要padding。
对于块加密,如果Plain + mac 的长度不是块加密算法的整数倍,则需要填充,填充到送入块加密函数的入参是块的整数倍。


padding
    Padding that is added to force the length of the plaintext to be
    an integral multiple of the block cipher's block length.  The
    padding MAY be any length up to 255 bytes, as long as it results
    in the TLSCiphertext.length being an integral multiple of the
    block length.  Lengths longer than necessary might be desirable to
    frustrate attacks on a protocol that are based on analysis of the
    lengths of exchanged messages.  Each uint8 in the padding data
    vector MUST be filled with the padding length value.  The receiver
    MUST check this padding and MUST use the bad_record_mac alert to
    indicate padding errors.

   padding_length
      The padding length MUST be such that the total size of the
      GenericBlockCipher structure is a multiple of the cipher's block
      length.  Legal values range from zero to 255, inclusive.  This
      length specifies the length of the padding field exclusive of the
      padding_length field itself.

举个例子,假设块大小为16,摘要算法为md5,明文数据4字节,明文数据加上摘要,总长度为20字节,要满足块大小整数倍,缺少12字节,则padding为12个11,padding value = padding len - 1。

如果明文数据是16字节,算上摘要16字节,正好是32字节,为了解密方便,也需要添加padding,padding为16个15。

Block cipher

加密,这里不是本文关心的流程。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值