kabeor 7月 25, 2019

# 逆向分析加解密之TwoFish算法

## 算法流程

http://www.schneier.com/twofish-analysis-shiho.pdf

### 流程图 TwoFish的意思应该就是这样交叉运算的形状吧

### 算法分析

TwoFish加密需要明文(plain)和密钥(key)

1. Input whitening
2. 16次循环
3. Output whitening

#### Input whitening

1. 拓展密钥 `GF(2^8) ≡ GF(2)(x)/v(x)，其中v(x) = x^8 + x^6 + x^5 + x^3 + 1`

`````` y(k,j) = x(j)                     j = 0, ... ,3

y(3,0) = q1[y(4,0)] xor l(3,0)
y(3,1) = q0[y(4,1)] xor l(3,1)
y(3,2) = q0[y(4,2)] xor l(3,2)
y(3,3) = q1[y(4,3)] xor l(3,3)

y(2,0) = q1[y(3,0)] xor l(2,0)
y(2,1) = q1[y(3,1)] xor l(2,1)
y(2,2) = q0[y(3,2)] xor l(2,2)
y(2,3) = q0[y(3,3)] xor l(3,3)

y0 = q1[q0[q0[y(2,0)] xor l(1,0)] xor l(0,0)]
y1 = q0[q0[q1[y(2,1)] xor l(1,1)] xor l(0,1)]
y2 = q1[q1[q0[y(2,2)] xor l(1,2)] xor l(0,2)]
y3 = q0[q1[q1[y(2,3)] xor l(1,3)] xor l(0,3)]
``````

1. 输入白化

`P(i) = ∑p(4i+j)2^(8j)，其中i,j = 0, ... ,3`

#### 16次运算

``````(F(r,0), F(r,1)) = F(R(r,0), R(r,1), r)
R(r+1,0) = ROR(R(r,2) xor F(r,0), 1)
R(r+1,1) = ROL(R(r,3), 1) xor F(r,1)
R(r+1,2) = R(r,0)
R(r+1,3) = R(r,1)
``````

``````t0 = g(r0)
t1 = rol(r1, 8)
t1 = g(t1)
o = 2*r
F0 = (T0 +  T1 + K(2r+8)) mod 2^32
F1 = (T0 + 2T1 + K(2r+9)) mod 2^32
``````

``````x(i) = [X/2^(8i)] mod 2^8  其中i = 0, ... ,3
y(i) = s(i)(x(i))       其中i = 0, ... ,3
`````` ``````Z = ∑z(i)2^(8i)，其中i = 0, ... ,3
``````

#### 输出白化

``````C(i) = R(16,(i+2) mod 4) xor K(i+4)，其中i = 0, ... ,3
``````

``````c(i) = [C(i/4) / 2^(8(i mod 4))] mod 2^8，其中i = 0, ... ,15
``````

## 逆向分析 IDA分析一下，进入主函数看到流程 c实现 rsm函数定义为

k生成 h函数内部，可以看出，IDA将二维数组直接一维化 q0,q1都是256大小的数组 MDS矩阵运算 S-box生成 c实现 f函数 twofish.h

tables.h

twofish.c

## CTF出题变化分析

TwoFish算法共有三处可发生变化以提高出题难度

1. rsm函数，0x14d可替换为其他数字
2. Twofish_generate_ext_s_keys函数中gf的参数0x166可替换
3. Twofish_mds_mul函数中gf的参数0x166可替换

This blog is under a CC BY-NC-SA 4.0 Unported License
{"ft_lang_label":"__label__en","ft_lang_prob":0.5012342,"math_prob":1.000009,"size":16510,"snap":"2019-51-2020-05","text_gpt3_token_len":9266,"char_repetition_ratio":0.16909003,"word_repetition_ratio":0.21929824,"special_character_ratio":0.5226529,"punctuation_ratio":0.2853012,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999026,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"im_url_duplicate_count":[null,null,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-05T15:02:26Z\",\"WARC-Record-ID\":\"<urn:uuid:a2bb1c39-f724-48b8-83c9-302b6305612e>\",\"Content-Length\":\"139566\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0263ad57-c0be-4361-86e6-95b0aa31dd7a>\",\"WARC-Concurrent-To\":\"<urn:uuid:eac3a1e5-8422-4a71-945a-12e4a218d582>\",\"WARC-IP-Address\":\"104.27.161.86\",\"WARC-Target-URI\":\"https://kabeor.cn/%E9%80%86%E5%90%91%E5%88%86%E6%9E%90%E5%8A%A0%E8%A7%A3%E5%AF%86%E4%B9%8BTwoFish%E7%AE%97%E6%B3%95/\",\"WARC-Payload-Digest\":\"sha1:MKAMG6KWHMCHXYMLWXJMOJ4VCVNI5WHV\",\"WARC-Block-Digest\":\"sha1:PFMDFYCSAPQSCNSBY67NNDUKGBTQZHOE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540481076.11_warc_CC-MAIN-20191205141605-20191205165605-00340.warc.gz\"}"}