📄 turingref.cpp
字号:
/* @(#)TuringRef.c 1.9 (QUALCOMM Turing) 03/02/24 *//* * Reference implementation of Turing * * Copyright C 2002, Qualcomm Inc. Written by Greg Rose *//*This software is free for commercial and non-commercial use subject tothe following conditions:1. Copyright remains vested in QUALCOMM Incorporated, and Copyrightnotices in the code are not to be removed. If this package is used ina product, QUALCOMM should be given attribution as the author of theTuring encryption algorithm. This can be in the form of a textualmessage at program startup or in documentation (online or textual)provided with the package.2. Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:a. Redistributions of source code must retain the copyright notice, this list of conditions and the following disclaimer.b. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.c. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by QUALCOMM Incorporated.3. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OFMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND AGAINSTINFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR ORCONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.4. The license and distribution terms for any publically available versionor derivative of this code cannot be changed, that is, this code cannotsimply be copied and put under another distribution license includingthe GNU Public License.5. The Turing family of encryption algorithms are covered by patents inthe United States of America and other countries. A free andirrevocable license is hereby granted for the use of such patents tothe extent required to utilize the Turing family of encryptionalgorithms for any purpose, subject to the condition that anycommercial product utilising any of the Turing family of encryptionalgorithms should show the words "Encryption by QUALCOMM" either on theproduct or in the associated documentation.*/#include <stdlib.h>#include "Turing.h" #include "TuringAPI.h" /* interface definitions */#include "TuringSbox.h"#include "QUTsbox.h"#include "TuringMultab.h"/* two variants of the Pseudo-Hadamard Transform *//* Mix 5 words in place */#define PHT(A,B,C,D,E) { \ (E) += (A) + (B) + (C) + (D); \ (A) += (E); \ (B) += (E); \ (C) += (E); \ (D) += (E); \}/* * This does a reversible transformation of a word, based on the S-boxes. * The reversibility isn't used, but it guarantees no loss of information, * and hence no equivalent keys or IVs. */static TURING_WORD fixedS(TURING_WORD w){ TURING_WORD b; b = Sbox[B(w, 0)]; w = ((w ^ Qbox[b]) & 0x00FFFFFF) | (b << 24); b = Sbox[B(w, 1)]; w = ((w ^ ROTL(Qbox[b],8)) & 0xFF00FFFF) | (b << 16); b = Sbox[B(w, 2)]; w = ((w ^ ROTL(Qbox[b],16)) & 0xFFFF00FF) | (b << 8); b = Sbox[B(w, 3)]; w = ((w ^ ROTL(Qbox[b],24)) & 0xFFFFFF00) | b; return w;}/* General word-wide n-PHT */void mixwords(TURING_WORD w[], int n){ register TURING_WORD sum; register int i; for (sum = i = 0; i < n-1; ++i) sum += w[i]; w[n-1] += sum; sum = w[n-1]; for (i = 0; i < n-1; ++i) w[i] += sum;}TuringCipher::TuringCipher(){ keylen = 0; block_index = 0; block_offset = 0;}/* step the LFSR */void TuringCipher::step(void){ register int i; register TURING_WORD w; w = R[15] ^ R[4] ^ (R[0] << 8) ^ Multab[(R[0] >> 24) & 0xFF]; for (i = 1; i < LFSRLEN; ++i) R[i-1] = R[i]; R[LFSRLEN-1] = w;}/* * Push a word through the keyed S-boxes. * As the bytes bounce around the permutation table, they are used * to build up words from the Qbox entries. Then the byte position * corresponding to the input byte is replaced with the result of * the S-box, which is a permutation of the input and guarantees * a balanced function. * Also added a rotation of the input word, to combat a differential * trail allowed by the PHT. */TURING_WORD TuringCipher::S(TURING_WORD w, int r){ register int i; TURING_BYTE b[4]; TURING_WORD ws[4]; w = ROTL(w, r); WORD2BYTE(w, b); ws[0] = ws[1] = ws[2] = ws[3] = 0; for (i = 0; i < keylen; ++i) { b[0] = Sbox[B(K[i], 0) ^ b[0]]; ws[0] ^= ROTL(Qbox[b[0]], i + 0); b[1] = Sbox[B(K[i], 1) ^ b[1]]; ws[1] ^= ROTL(Qbox[b[1]], i + 8); b[2] = Sbox[B(K[i], 2) ^ b[2]]; ws[2] ^= ROTL(Qbox[b[2]], i + 16); b[3] = Sbox[B(K[i], 3) ^ b[3]]; ws[3] ^= ROTL(Qbox[b[3]], i + 24); } w = (ws[0] & 0x00FFFFFFUL) | (b[0] << 24); w ^= (ws[1] & 0xFF00FFFFUL) | (b[1] << 16); w ^= (ws[2] & 0xFFFF00FFUL) | (b[2] << 8); w ^= (ws[3] & 0xFFFFFF00UL) | b[3]; return w;}/* * Key the cipher. * Reference version; merely gathers words, mixes them, saves them. */void TuringCipher::TuringKey(const TURING_BYTE key[], const int keylength){ register int i; if ((keylength & 0x03) != 0 || keylength > MAXKEY) abort(); keylen = 0; for (i = 0; i < keylength; i += 4) K[keylen++] = fixedS(BYTE2WORD(&key[i])); mixwords(K, keylen); }/* * Load the Initialization Vector. * Actually, this fills the LFSR, with IV, key, length, and more. * IV goes through the fixed S-box, key is premixed, the rest go through * the keyed S-boxes. The reason for this is to avoid nasty interactions * between the mixed key and the S-boxes that depend on them, and also * to avoid any "chosen-IV" interactions with the keyed S-boxes, not that I * can think of any. */void TuringCipher::TuringIV(const TURING_BYTE iv[], const int ivlength){ register int i, j; /* check args */ if ((ivlength & 0x03) != 0 || (ivlength + 4*keylen) > MAXKIV) abort(); /* first copy in the IV, mixing as we go */ for (i = j = 0; j < ivlength; j +=4) R[i++] = fixedS(BYTE2WORD(&iv[j])); /* now continue with the premixed key */ for (j = 0 /* i continues */; j < keylen; ++j) R[i++] = K[j]; /* now the length-dependent word */ R[i++] = (keylen << 4) | (ivlength >> 2) | 0x01020300UL; /* ... and fill the rest of the register */ for (j = 0 /* i continues */; i < LFSRLEN; ++i, ++j) R[i] = S(R[j] + R[i-1], 0); /* finally mix all the words */ mixwords(R, LFSRLEN);}/* * Generate a 5-word block of output. * Buffering issues are outside the scope of this implementation. * Returns the number of bytes of stream generated. */int TuringCipher::TuringGen(TURING_BYTE *buf){ register TURING_WORD A, B, C, D, E; step(); A = R[16]; B = R[13]; C = R[6]; D = R[1]; E = R[0]; PHT(A, B, C, D, E); A = S(A,0); B = S(B,8); C = S(C,16); D = S(D,24); E = S(E,0); PHT(A, B, C, D, E); step(); step(); step(); A += R[14]; B += R[12]; C += R[8]; D += R[1]; E += R[0]; WORD2BYTE(A, buf); WORD2BYTE(B, buf+4); WORD2BYTE(C, buf+8); WORD2BYTE(D, buf+12); WORD2BYTE(E, buf+16); step(); block_offset += 20; return 20;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -