前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >密码库LibTomCrypt学习记录——(2.12)分组密码算法的工作模式——OMAC认证模式

密码库LibTomCrypt学习记录——(2.12)分组密码算法的工作模式——OMAC认证模式

作者头像
全栈程序员站长
发布2022-09-14 09:44:35
7100
发布2022-09-14 09:44:35
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

  1. OMAC

OMAC是一种认证模式,LibTomCrypt中涉及的OMAC,而NIST中提到的是CMAC。它们之间的关系是这样的:

为避免基本的MAC算法CBC-MAC存在的安全缺陷,Black和Rogaway对其进行了改进,提出了避免CBC-MAC安全缺陷的XCBC算法。Iwata和Kurosawa对XCBC进一步改进,提出了One-Key CBC-MAC(OMAC),接着又精益求精地提出了OMAC1。而NIST SP 800-38B 中涉及的CMAC其实就是OMAC1。因此,OMAC采用的基本模式是用改进的CBC模式得到MAC值,主要调整是子密钥的生成和最后一个分组的padding上。

参考文献

  1. NIST SP 800-38B.
  2. Black, P. Rogaway, A Suggestion for Handling Arbitrary-Length Messages with the CBC MAC, Natl. Inst. Stand. Technol. [Web page], http://csrc.nist.gov/CryptoToolkit/modes/workshop1/.
  3. J. Black, P. Rogaway, CBC MACs for arbitrary-length messages: The three-key constructions, in Advances in Cryptology—Crypto 2000, Lecture Notes in Computer Science, Vol. 1880, Mihir Bellare, ed., Springer-Verlag (2000), pp. 197–215.
  4. T. Iwata, K. Kurosawa, OMAC: One-Key CBC MAC, Natl. Inst. Stand. Technol. [Web page], http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/.
  5. T. Iwata, K. Kurosawa, OMAC: One-Key CBC MAC, in Fast Software Encryption, 10th International Workshop, FSE 2003, Lecture Notes in Computer Science, Vol. 2887, Thomas Johansson, ed., Springer-Verlag (2003), p.p. 129–153.
  6. T. Iwata, K. Kurosawa, OMAC: One-Key CBC MAC—Addendum, Natl. Inst. Stand. Technol. [Web page], http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/.
  7. T. Iwata, K. Kurosawa, Stronger Security Bounds for OMAC, TMAC, and XCBC, Natl. Inst. Stand. Technol. [Web page], http://csrc.nist.gov/CryptoToolkit/modes/comments/.
  8. A. Menezes, P. van Oorschot, S. Vanstone, Handbook of Applied Cryptography, CRC Press, Inc., Boca Raton (1996).
  9. 子密钥生成

记加密算法为CIPH,其分组大小为b比特;密钥记为K;输出子密钥为K1和K2。子密钥生成步骤如下:

step 1. L = CIPHK(0b).

step 2. If MSB1(L) = 0, then K1 = L << 1;

Else K1 = (L << 1) ⊕ Rb;

step 3. If MSB1(K1) = 0, then K2 = K1 << 1;

Else K2 = (K1 << 1) ⊕ Rb.

step 4. Return K1, K2.

其中:Rb在b为128bit和64bit时的值为

R128 = 012010000111,R64 = 05911011。

012010000111就是前面120bit的0再在低位接10000111,其余类似。

其实,对步骤2和步骤3更准确的理解是,这是在做有限域GF(2b)上的乘以2的乘法,Rb是生成多项式的尾项,即生成多项式为f(x)=x128+Rb。此有限域上的b = a2的运算过程就是:。这样一来,两个子密钥的来历就是:

密码库LibTomCrypt学习记录——(2.12)分组密码算法的工作模式——OMAC认证模式
密码库LibTomCrypt学习记录——(2.12)分组密码算法的工作模式——OMAC认证模式

子密钥生成的流程图(生成子密钥K1和K2)

  1. MAC生成

记加密算法为CIPH,其分组大小为b比特;密钥记为K;输入消息为Mlen比特长的M,输出MAC值为Tlen比特长的T。

MAC生成CMAC(K, M, Tlen)(也可以记为CMAC(K, M))步骤如下:

step 1. 使用子密钥生产算法生成子密钥K1和K2;

step 2. If Mlen = 0, n = 1; else, n = 向上取整(Mlen/b).

step 3. 将消息M按照分组大小b进行划分

M =M1 || M2 || … || Mn-1 || Mn*,

这里的M1, M2,…, Mn-1是完整分组,而最后一个分组Mn*可能完整也可能不完整。

step 4. 若Mn*是完整分组,Mn = K1 ⊕ Mn*; 否则,Mn = K2 ⊕ (Mn*||10j),j = nb-Mlen-1。

step 5. Let C0 = 0b.

step 6. For i = 1 to n, Ci = CIPHK(Ci-1 ⊕ Mi).

step 7. T = MSBTlen(Cn).

step 8. Return T.

密码库LibTomCrypt学习记录——(2.12)分组密码算法的工作模式——OMAC认证模式
密码库LibTomCrypt学习记录——(2.12)分组密码算法的工作模式——OMAC认证模式

MAC生成的流程图(MAC值为T)

  1. LibTomCrypt与OMAC

LibTomCrypt中与OMAC相关的信息如下:

typedef struct {

int cipher_idx, //密码算法索引值

buflen, // block中数据的长度

blklen; // 分块大小,即密码算法分组大小

unsigned char block[MAXBLOCKSIZE], //缓存尾巴上的数据

prev[MAXBLOCKSIZE], //上一块加密数据

Lu[2][MAXBLOCKSIZE];// Lu[0] is Subkey K1, Lu[1] is Subkey K2

symmetric_key key; // 扩展密钥

} omac_state;

其中

  1. block[MAXBLOCKSIZE]缓存尾巴上的数据,当数据长len为blklen的倍数时,block存储最后一个blklen的数据;否则,存储len mod blklen 个字节的末尾数据

在LibTomCrypt中omac主要有以下函数

int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);

int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen);

int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen);

int omac_memory(int cipher, const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen);

int omac_memory_multi(int cipher, const unsigned char *key, unsigned long keylen, unsigned char *out, unsigned long *outlen, const unsigned char *in, unsigned long inlen, …);

int omac_file(int cipher, const unsigned char *key, unsigned long keylen, const char *filename, unsigned char *out, unsigned long *outlen);

int omac_test(void);

──────────────────────────────────────

int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);

// [功能] 初始化

  1. omac // [输入/输出] OMAC状态
  2. cipher // [输入] 初始化向量
  3. key // [输入] 密钥
  4. keylen // [输入] 密钥长度

──────────────────────────────────────

──────────────────────────────────────

int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen);

// [功能] 处理输入信息

  1. omac // [输入/输出] OMAC状态
  2. in // [输入] 消息
  3. inlen // [输入] 消息长度

//[备注] 消息长度可以不是分组长度的倍数。可多次调用此函数输入消息

──────────────────────────────────────

──────────────────────────────────────

int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen);

// [功能] 结束并输出mac值

  1. omac // [输入/输出] OMAC状态
  2. out // [输出] mac值
  3. outlen // [输出] mac值长度

//[备注] 实际输出长度 = min ( 输入的outlen长度, 分组长度 )

OMAC通常的执行流程是:

omac_init(***);

while( want_send_message )

{

omac_ process (***);//每次送入的消息长度可以为任意值

}

omac_ done (***);

──────────────────────────────────────

──────────────────────────────────────

int omac_memory(int cipher, const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen);

// [功能] 计算一段数据的mac值

  1. cipher // [输入] 密码算法
  2. key // [输入] 密钥
  3. keylen // [输入] 密钥长度
  4. in // [输入] 消息
  5. inlen // [输入] 消息长度
  6. out // [输出] mac值
  7. outlen // [输出] mac值长度

//[备注] 适合消息不太长的场合。实际处理过程为

omac_init(***);

omac_ process (***);

omac_ done (***);

──────────────────────────────────────

──────────────────────────────────────

int omac_memory_multi(int cipher, const unsigned char *key, unsigned long keylen, unsigned char *out, unsigned long *outlen, const unsigned char *in, unsigned long inlen, …);

// [功能] 计算多段数据的mac值

  1. cipher // [输入] 密码算法
  2. key // [输入] 密钥
  3. keylen // [输入] 密钥长度
  4. out // [输出] mac值
  5. outlen // [输出] mac值长度
  6. in // [输入] 消息
  7. inlen // [输入] 消息长度
  8. … // [输入] 可选参数,先跟消息地址,再跟消息长度,如此反复

//[备注] 类似omac_memory,只不过处理的是多段数据

──────────────────────────────────────

──────────────────────────────────────

int omac_file(int cipher, const unsigned char *key, unsigned long keylen, const char *filename, unsigned char *out, unsigned long *outlen);

// [功能] 计算文件的mac值

  1. cipher // [输入] 密码算法
  2. key // [输入] 密钥
  3. keylen // [输入] 密钥长度
  4. filename // [输入] 文件名
  5. out // [输出] mac值
  6. outlen // [输出] mac值长度

//[备注] 适合消息不太长的场合。实际处理过程为

omac_init(***);

while ( file_not end )

{

fread(***);

omac_ process (***);

}

omac_ done (***);

──────────────────────────────────────

──────────────────────────────────────

int omac_test(void);

// [功能] 测试函数

──────────────────────────────────────

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159192.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档