From 2ac9e613358cd00a10ce0916a4488473292b76b1 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 24 Nov 2017 18:21:27 -0500 Subject: [PATCH] Switch to rotlConstant and rotrConstant --- sm3.cpp | 36 ++++++++++++++++++++---------------- sm4.cpp | 11 +++++++---- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/sm3.cpp b/sm3.cpp index 55d49f47..bf01dd53 100644 --- a/sm3.cpp +++ b/sm3.cpp @@ -2,6 +2,10 @@ // Based on the specification provided by Sean Shen and Xiaodong Lee. // Based on code by Krzysztof Kwiatkowski and Jack Lloyd. // Also see https://tools.ietf.org/html/draft-shen-sm3-hash. +// +// We understand future ARMv8 enhancements are supposed +// to include SM3 and SM4 related instructions so the function +// is stubbed for an eventual SM3_HashMultipleBlocks_ARMV8. #include "pch.h" #include "config.h" @@ -14,7 +18,7 @@ ANONYMOUS_NAMESPACE_BEGIN using CryptoPP::byte; using CryptoPP::word32; -using CryptoPP::rotlFixed; +using CryptoPP::rotlConstant; using CryptoPP::SM3; using CryptoPP::GetBlock; @@ -22,17 +26,17 @@ using CryptoPP::BigEndian; inline word32 P0(word32 X) { - return X ^ rotlFixed(X, 9) ^ rotlFixed(X, 17); + return X ^ rotlConstant<9>(X) ^ rotlConstant<17>(X); } inline word32 P1(word32 X) { - return X ^ rotlFixed(X, 15) ^ rotlFixed(X, 23); + return X ^ rotlConstant<15>(X) ^ rotlConstant<23>(X); } inline word32 EE(word32 W0, word32 W7, word32 W13, word32 W3, word32 W10) { - return P1(W0 ^ W7 ^ rotlFixed(W13, 15)) ^ rotlFixed(W3, 7) ^ W10; + return P1(W0 ^ W7 ^ rotlConstant<15>(W13)) ^ rotlConstant<7>(W3) ^ W10; } inline word32 FF(word32 X, word32 Y, word32 Z) @@ -48,25 +52,25 @@ inline word32 GG(word32 X, word32 Y, word32 Z) inline void R1(word32 A, word32& B, word32 C, word32& D, word32 E, word32& F, word32 G, word32& H, word32 TJ, word32 Wi, word32 Wj) { - const word32 A12 = rotlFixed(A, 12); - const word32 SS1 = rotlFixed(A12 + E + TJ, 7); - const word32 TT1 = (A ^ B ^ C) + D + (SS1 ^ A12) + Wj; - const word32 TT2 = (E ^ F ^ G) + H + SS1 + Wi; + const word32 A12 = rotlConstant<12>(A); + const word32 TT0 = rotlConstant<7>(A12 + E + TJ); + const word32 TT1 = (A ^ B ^ C) + D + (TT0 ^ A12) + Wj; + const word32 TT2 = (E ^ F ^ G) + H + TT0 + Wi; - B = rotlFixed(B, 9); D = TT1; - F= rotlFixed(F, 19); H = P0(TT2); + B = rotlConstant<9>(B); D = TT1; + F = rotlConstant<19>(F); H = P0(TT2); } inline void R2(word32 A, word32& B, word32 C, word32& D, word32 E, word32& F, word32 G, word32& H, word32 TJ, word32 Wi, word32 Wj) { - const word32 A12 = rotlFixed(A, 12); - const word32 SS1 = rotlFixed(A12 + E + TJ, 7); - const word32 TT1 = FF(A, B, C) + D + (SS1 ^ A12) + Wj; - const word32 TT2 = GG(E, F, G) + H + SS1 + Wi; + const word32 A12 = rotlConstant<12>(A); + const word32 TT0 = rotlConstant<7>(A12 + E + TJ); + const word32 TT1 = FF(A, B, C) + D + (TT0 ^ A12) + Wj; + const word32 TT2 = GG(E, F, G) + H + TT0 + Wi; - B = rotlFixed(B, 9); D = TT1; - F = rotlFixed(F, 19); H = P0(TT2); + B = rotlConstant<9>(B); D = TT1; + F = rotlConstant<19>(F); H = P0(TT2); } // Krzysztof Kwiatkowski did a very nice job with this function. diff --git a/sm4.cpp b/sm4.cpp index 1a157c1d..1aa2a1ff 100644 --- a/sm4.cpp +++ b/sm4.cpp @@ -1,4 +1,8 @@ // sm4.cpp - written and placed in the public domain by Jeffrey Walton and Han Lulu +// +// We understand future ARMv8 enhancements are supposed +// to include SM3 and SM4 related instructions so the function +// is stubbed for an eventual SM4_Round_ARMV8. #include "pch.h" #include "config.h" @@ -11,8 +15,7 @@ ANONYMOUS_NAMESPACE_BEGIN using CryptoPP::byte; using CryptoPP::word32; -using CryptoPP::rotlFixed; -using CryptoPP::rotrFixed; +using CryptoPP::rotlConstant; const byte S[256] = { @@ -54,13 +57,13 @@ inline word32 SM4_H(word32 x) inline word32 SM4_G(word32 x) { const word32 t = SM4_H(x); - return t ^ rotlFixed(t, 13) ^ rotlFixed(t, 23); + return t ^ rotlConstant<13>(t) ^ rotlConstant<23>(t); } inline word32 SM4_F(word32 x) { const word32 t = SM4_H(x); - return t ^ rotlFixed(t, 2) ^ rotlFixed(t, 10) ^ rotlFixed(t, 18) ^ rotlFixed(t, 24); + return t ^ rotlConstant<2>(t) ^ rotlConstant<10>(t) ^ rotlConstant<18>(t) ^ rotlConstant<24>(t); } template