一种SM4加密算法的软件优化实现方法与流程

文档序号:18601752发布日期:2019-09-03 22:43阅读:387来源:国知局

本发明涉及计算机安全技术领域,特别是一种sm4软件优化实现方法。



背景技术:

目前的各种数据加密体制可分为对称密码加密体制和公钥密码加密体制。常用的对称密码算法有des、aes、sm4等。

sm4分组密码算法是我国自主设计的分组对称密码,明文长度、密钥长度和密文长度都是128比特,加密和解密密钥相同。加解密通过32次循环的非线性迭代轮函数来实现,其中包括非线性变换s盒及由循环移位异或构成的线性变换。加密时,首先把128比特密钥按照32比特一组分成4组,然后根据密钥扩展算法,生成32组32比特轮密钥;同时把输入的128比特数据按照32比特一组分成4组进行循环运算。

切片实现是密码算法的快速实现方法之一,以若干比特为基本单位进行并行加解密,其中单比特切片与宽泛意义上的切片仍有较大不同。

切片实现中单比特的切片是最早的切片方式,切片粒度为比特,算法中的非线性部分s盒视为逻辑电路实现。使用1比特切片时,将处理器视为并行的1比特simd处理器。线性移位操作只需要改变索引,不需要任何实际指令去实现,所以没有时间消耗。



技术实现要素:

本发明用单比特粒度的切片方法实现sm4算法,在原有切片技术的基础上提出了如下改进的技术方案。

一种sm4加密算法的软件优化实现方法,以1比特为单位切片,在x86架构下一次并行加解/密256组,包括:

作为数据预处理的数据编排和密钥编排步骤,包括:

数据编排步骤,从常规256组128比特数据组织方式到单比特切片要求的256组的同一比特组织在一起的组织方式之间,存在比特矩阵转置变换trans(),特征在于,输入为256*128比特,输出为128*256比特。若将输入第i比特表示为二维数组x[256][128]的x[i/128][imod128]项,则输出为二位数组x[128][256]的x[imod128][i/128]项。

密钥编排步骤,对于第i轮加密的128比特密钥rki,变换得到表示将rki每比特重复256次得到的结果,即

迭代计算步骤,将256组128比特的数据表示为32轮的加密密钥记为进行32次迭代运算:

其中^表示异或运算;

反序计算步骤,令则输出的128比特的加密数据表示为

其中,合成置换t的输入和输出都是256组32比特,由非线性变换τ和线性变换l复合而成t(·)=l(τ(·))。

上述256组的同1比特存储在一起(ymm拓展寄存器或一块内存)。表示二维数组x[128][256]的x[0]项,其长度为256比特。

更进一步地,将256组32比特输入数据表示为:其中,均为8*256比特,则

更进一步地,合成变换t中的非线性变换τ中的四个s()为,

s0~s7在逻辑函数表达形式基础上用逻辑运算实现。逻辑表达式采用与或式的表达如下:

s0=~a~b~d~e~g~h+~b~c~d~eh+~b~c~d~fh+~a~c~e~fg~h+~a~b~d~e~fgh+~a~c~d~ef~g~h+~a~b~df~gh+~a~bef~gh+~a~b~cdfh+~a~bde~f~g~h+~a~bc~df~h+~bc~ef~h+~acd~e~g~h+~a~bcd~fh+~a~bcdgh+~a~bcde~f+~acdegh+~ab~c~egh+~ab~c~de~g~h+~ab~c~def~g+b~defgh+~ab~cdfg~h+~ab~cde~f~g+~abc~d~efg+~abcdf~g+~abcdef+bcdef~g+a~b~c~d~f+a~b~e~f~gh+a~d~e~fg~h+a~b~c~dgh+a~c~e~fgh+a~b~ce~g~h+a~b~de~f~g~h+a~b~de~fgh+a~bd~e~g~h+a~bd~f~gh+a~cdef+a~bcd~e~g+acd~f~gh+ab~c~d~e~h+ab~d~e~f~h+ab~c~ef~g+ab~ef~gh+ab~de~f~gh+ab~cd~eh+abd~e~fh+ab~cdeg~h+abc~d~fg~h+abc~def~h

s1=~a~c~d~e~g~h+~b~c~d~e~g~h+~a~b~d~e~fg+~a~d~e~fgh+~b~c~d~fgh+~b~df~gh+~a~cd~f~gh+~a~b~cd~egh+~b~cd~efgh+~bdef~g~h+~a~bc~dh+~bc~d~e~gh+~bc~d~e~fg~h+~a~bc~def~g+cd~e~f~g~h+~a~bcd~ef~h+~bcde~fh+b~ef~gh+b~ce~f~gh+~ab~c~def~h+~ab~cd~g+~abd~f~g~h+b~cd~gh+~abdf~gh+b~cd~efg~h+~abde~fgh+bc~e~fgh+~abc~d~ef~h+bc~deg~h+bcefg~h+~abcd~e~f~h+bcd~efh+a~b~c~df~g+a~c~de~fh+a~c~de~fg+a~b~cd~e~f~h+adefgh+a~bc~dfg+ac~de~f~g~h+a~bcd~e~g+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~de~fh+~a~bef~gh+~a~b~cd~e~f+~a~b~cd~e~g+~a~cde~fg+~a~bdefg~h+~bc~e~f~gh+~a~bc~dfh+~bc~de~fg+~acegh+cde~fh+~ab~c~d~e~g+~ab~d~e~f~h+~ab~d~e~g~h+b~c~df~g~h+~ab~c~efg+b~d~efgh+b~c~def~h+~ab~cd~fh+~ab~cde~gh+bde~fgh+~abc~e~fg+~abce~fh+~abcd~ef+bcde~fg+a~b~c~d~f+a~b~c~f~gh+a~b~d~e~gh+a~c~d~e~fh+a~d~e~fg+a~be~fg~h+a~bd~ef~h+a~cd~efg~h+a~b~cdefgh+a~bcg~h+a~bc~ef~h+ac~df~gh+ac~dfg~h+a~bc~deg+a~bcd~efg+a~bcde~f+ab~cd~e~f~g~h+abcdf~g~h+abcdef~g

s7=~a~c~d~eg~h+~b~c~dfh+~b~cfgh+~a~def~gh+~a~b~cd~e~f~g+~a~cdfgh+~a~cde~fg~h+~bdef~g~h+~a~bc~df~g+~a~bcf~g~h+~ac~de~g~h+~bc~de~fg+~bcd~e~fh+~bcd~egh+~a~bcde~gh+~bcdef~h+b~c~d~e~g+b~c~d~f~g+b~c~e~f~gh+b~c~e~fg~h+~ab~ef~g~h+~ab~c~dfg~h+~ab~ce~fh+b~de~fgh+~ab~cdeg+bc~efg~h+~abc~defh+~abcd~fgh+~abcd~ef~g+a~b~c~d~gh+a~c~d~efh+a~b~ce~f~g+a~ce~f~g~h+a~b~cef~h+a~def~g~h+ad~e~fg~h+a~b~cdf~g~h+a~bdefg+a~bc~e~f~g~h+a~bc~deh+a~bcd~f~g+a~bcd~eh+ab~d~f~g+ab~cd~ef+abdef~gh+abc~d~f~h+abcde~h

以上s0~s7式中,a,b,c,d,e,f,g,h为顺序排列的8位256比特输入,~为非运算,+为或运算,与运算*省略,运算优先级为~>*>+。

s0~s7的逻辑表达式,特征在于,8个表达式中的输入a~h是相同的。

对于s0~s7中任意一个逻辑函数,可以表达为“选择函数”形式。

选择函数形式为:

fo=(f1andsel)or(f2andnotsel)

其中fo为n输入单输出逻辑函数,sel为a~h中任一位输入,f1、f2为与sel位无关的n-1位输入单输出的逻辑函数。

还有以下选择函数形式:

fo=(f3andsel)xorf2

fo=(f3andnotsel)xorf1

fo=(f4orsel)xornotf1

fo=(f4ornotsel)xornotf2

fo=(f4nandsel)xorf2

fo=(f4nandnotsel)xorf1

fo=(f3norsel)xornotf1

fo=(f3nornotsel)xornotf2

其中f3=f1xorf2,f4=notf3,anandb=notaandb,anorb=notaorb.

限定只与n-1位输入有关后,f1、f2是唯一确定的。有这样的关系:2n=2n-1×2n-1,对于n比特输入逻辑函数,恰好需要2n比特进行表示;f1、f2至少需要2n-1比特进行表示,但实际上仍以2n比特进行表示。以f1为例进行求解:对于(foandsel),将输入项为ab…notsel…h的输出由0改为与输入ab…sel…h相同即可。对于2输入单输出逻辑函数,共有16种,除去0、1和两个输入仍有12种不平凡的,可以用12个逻辑门实现。

对于n输入的逻辑函数,递归地使用“选择函数”生成表达式,若选择上面包含两个逻辑门的“选择函数”表达式,可以用最多12+21+22+...2n-2个逻辑门实现。

对于s0~s7,使用选择函数表达,可以用不超过12+8*(21+22+...26)=1020个逻辑门实现。

s0~s78个逻辑函数以任意顺序先后生成“选择函数”形式的表达式;选择函数表达式中sel位按任意顺序选择输入a~h中某一位;后生成的表达式可以借助前面已经生成的结果;上面给出的多个“选择函数”表达式都可以被尝试从而选择逻辑门数最少的表达式。充分尝试上述各种生成表达式的顺序,混合使用多种“选择函数”表达式,可以找到很少门数的s0~s7表达式。

s0~s7的“选择函数”表达式存在互相借用中间结果,实现了s0~s7并行运算。可以找到逻辑门数为514的“选择函数”表达式实现了s0~s7。

x86平台上使用avx2指令集,具体为vpand、vpnand、vpor、vpxor、vmovdqa指令即可实现上述表达式。汇编语句除去vmovdqa进行数据存取,其余为逻辑运算,需要语句数与上述表达式的逻辑门数相同。

数据反编排步骤,从切片后的128组256比特数据组织方式恢复到正常256组128比特数据,需要进行比特矩阵转置变换inv_trans(),特征在于,输入为128*256比特,输出为256*128比特。若将输入第i比特表示为二维数组x[128][256]的x[i/256][imod256]项,则输出为二位数组x[256][128]的x[imod256][i/256]项。

具体实施方式

作为数据预处理的数据编排和密钥编排步骤,包括:

数据编排步骤,从常规256组128比特数据组织方式到单比特切片要求的256组的同一比特组织在一起的组织方式之间,存在比特矩阵转置变换trans(),特征在于,输入为256*128比特,输出为128*256比特。若将输入第i比特表示为二维数组x[256][128]的x[i/128][imod128]项,则输出为二位数组x[128][256]的x[imod128][i/128]项。

密钥编排步骤,对于第i轮加密的128比特密钥rki,变换得到表示将rki每比特重复256次得到的结果,即

迭代计算步骤,将256组128比特的数据表示为32轮的加密密钥记为进行32次迭代运算:其中^表示异或运算;

反序计算步骤,令则输出的128比特的加密数据表示为

其中,合成置换t的输入和输出都是256组32比特,由非线性变换τ和线性变换l复合而成t(·)=l(τ(·))。

上述256组的同1比特存储在一起(ymm拓展寄存器或一块内存)。表示二维数组x[128][256]的x0]项,其长度为256比特。

更进一步地,将256组32比特输入数据表示为:其中,均为8*256比特,则

更进一步地,合成变换t中的非线性变换τ中的四个s()为,

s0~s7在逻辑函数表达形式基础上用逻辑运算实现。逻辑表达式采用与或式的表达如下:

s0=~a~b~d~e~g~h+~b~c~d~eh+~b~c~d~fh+~a~c~e~fg~h+~a~b~d~e~fgh+~a~c~d~ef~g~h+~a~b~df~gh+~a~bef~gh+~a~b~cdfh+~a~bde~f~g~h+~a~bc~df~h+~bc~ef~h+~acd~e~g~h+~a~bcd~fh+~a~bcdgh+~a~bcde~f+~acdegh+~ab~c~egh+~ab~c~de~g~h+~ab~c~def~g+b~defgh+~ab~cdfg~h+~ab~cde~f~g+~abc~d~efg+~abcdf~g+~abcdef+bcdef~g+a~b~c~d~f+a~b~e~f~gh+a~d~e~fg~h+a~b~c~dgh+a~c~e~fgh+a~b~ce~g~h+a~b~de~f~g~h+a~b~de~fgh+a~bd~e~g~h+a~bd~f~gh+a~cdef+a~bcd~e~g+acd~f~gh+ab~c~d~e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+~bcd~egh+~a~bcde~gh+~bcdef~h+b~c~d~e~g+b~c~d~f~g+b~c~e~f~gh+b~c~e~fg~h+~ab~ef~g~h+~ab~c~dfg~h+~ab~ce~fh+b~de~fgh+~ab~cdeg+bc~efg~h+~abc~defh+~abcd~fgh+~abcd~ef~g+a~b~c~d~gh+a~c~d~efh+a~b~ce~f~g+a~ce~f~g~h+a~b~cef~h+a~def~g~h+ad~e~fg~h+a~b~cdf~g~h+a~bdefg+a~bc~e~f~g~h+a~bc~deh+a~bcd~f~g+a~bcd~eh+ab~d~f~g+ab~cd~ef+abdef~gh+abc~d~f~h+abcde~h

以上s0~s7式中,a,b,c,d,e,f,g,h为顺序排列的8位256比特输入,~为非运算,+为或运算,与运算*省略,运算优先级为~>*>+。

s0~s7的逻辑表达式,特征在于,8个表达式中的输入a~h是相同的。

对于s0~s7中任意一个逻辑函数,可以表达为“选择函数”形式。

选择函数形式为:

fo=(f1andsel)or(f2andnotsel)

其中fo为n输入单输出逻辑函数,sel为a~h中任一位输入,f1、f2为与sel位无关的n-1位输入单输出的逻辑函数。

还有以下选择函数形式:

fo=(f3andsel)xorf2

fo=(f3andnotsel)xorf1

fo=(f4orsel)xornotf1

fo=(f4ornotsel)xornotf2

fo=(f4nandsel)xorf2

fo=(f4nandnotsel)xorf1

fo=(f3norsel)xornotf1

fo=(f3nornotsel)xornotf2

其中f3=f1xorf2,f4=notf3,anandb=notaandb,anorb=notaorb.

对于2输入单输出逻辑函数,共有16种,除去0、1和两个输入仍有12种不平凡的,可以用12个逻辑门实现。

对于n输入的逻辑函数,递归地使用“选择函数”生成表达式,若选择上面包含两个逻辑门的“选择函数”表达式,可以用最多12+21+22+...2n-2个逻辑门实现。

对于s0~s7,使用选择函数表达,可以用不超过12+8*(21+22+...26)=1020个逻辑门实现。

s0~s78个逻辑函数以任意顺序先后生成“选择函数”形式的表达式;选择函数表达式中sel位按任意顺序选择输入a~h中某一位;后生成的表达式可以借助前面已经生成的结果;上面给出的多个“选择函数”表达式都可以被尝试从而选择逻辑门数最少的表达式。

s0~s7生成顺序共有40320种可能,sel选择顺序共有40320种可能,确定一个s进行生成并选择一位输入为sel后可以尝试9个等效的“选择函数”表达式,进行这样的穷举搜索可以找到很少门数的s0~s7表达式。

s0~s7的“选择函数”表达式存在互相借用中间结果,实现了s0~s7并行运算。可以找到逻辑门数为514的“选择函数”表达式实现了s0~s7。

x86平台上使用avx2指令集,具体为vpand、vpnand、vpor、vpxor、vmovdqa指令即可实现上述表达式。汇编语句除去vmovdqa进行数据存取,其余为逻辑运算,需要语句数与上述表达式的逻辑门数相同。

数据反编排步骤,从切片后的128组256比特数据组织方式恢复到正常256组128比特数据,需要进行比特矩阵转置变换inv_trans(),特征在于,输入为128*256比特,输出为256*128比特。若将输入第i比特表示为二维数组x[128][256]的x[i/256][imod256]项,则输出为二位数组x[256][128]的x[imod256][i/256]项。反编排实现与预处理的编排基本一致。

当前第1页1 2 
网友询问留言 已有0条留言
  • 还没有人留言评论。精彩留言会获得点赞!
1