1
# ---------------------------------------------------------------------------
3
# - afnix:sec module test unit -
4
# ---------------------------------------------------------------------------
5
# - This program is free software; you can redistribute it and/or modify -
6
# - it provided that this copyright notice is kept intact. -
8
# - This program is distributed in the hope that it will be useful, but -
9
# - without any warranty; without even the implied warranty of -
10
# - merchantability or fitness for a particular purpose. In no event shall -
11
# - the copyright holder be liable for any direct, indirect, incidental or -
12
# - special damages arising in any way out of the use of this software. -
13
# ---------------------------------------------------------------------------
14
# - copyright (c) 1999-2011 amaury darsch -
15
# ---------------------------------------------------------------------------
17
# @info rc2 block cipher test unit
18
# @author amaury darsch
21
interp:library "afnix-sec"
23
# this procedure test the rc2 cipher
24
const test-rc2-cipher-no-padding (kbuf ivec ovec ekey) {
26
const key (afnix:sec:Key kbuf)
27
# create the cipher and check
28
const rc2 (afnix:sec:Rc2 key)
29
assert "RC2" (rc2:get-name)
30
assert true (afnix:sec:rc2-p rc2)
31
# set the effective key
32
rc2:set-effective-key ekey
34
trans ib (Buffer ivec)
37
trans blen (rc2:stream ob ib)
38
trans vb (Buffer ovec)
39
# verify the output buffer
40
loop (trans i 0) (< i blen) (i:++) {
41
assert (vb:get i) (ob:get i)
43
# reset in decode mode
44
trans vb (Buffer ivec)
47
trans blen (rc2:stream ib ob)
48
loop (trans i 0) (< i blen) (i:++) {
49
assert (vb:get i) (ib:get i)
53
# this procedure test the rc2 cipher with padding
54
const test-rc2-cipher-with-padding (kbuf ivec ekey pmod cmod) {
56
const key (afnix:sec:Key kbuf)
57
# create the cipher and check
58
const rc2 (afnix:sec:Rc2 key)
59
assert "RC2" (rc2:get-name)
60
assert true (afnix:sec:rc2-p rc2)
61
# set the effective key
62
rc2:set-effective-key ekey
64
# set the cipher padding mode
65
rc2:set-padding-mode pmod
66
rc2:set-block-mode cmod
67
assert pmod (rc2:get-padding-mode)
68
assert cmod (rc2:get-block-mode)
71
trans ib (Buffer ivec)
80
# rebuild the input buffer
82
trans ib (Buffer ivec)
83
trans blen (ib:length)
84
assert blen (vb:length)
85
loop (trans i 0) (< i blen) (i:++) {
86
assert (vb:get i) (ib:get i)
90
# this procedure test the rc2 cipher
91
const test-rc2-cipher (kbuf ivec ovec ekey) {
92
const PAD-NONE afnix:sec:BlockCipher:PAD-NONE
93
const PAD-BIT-MODE afnix:sec:BlockCipher:PAD-BIT-MODE
94
const PAD-ANSI-X923 afnix:sec:BlockCipher:PAD-ANSI-X923
95
const PAD-NIST-800 afnix:sec:BlockCipher:PAD-NIST-800
97
const MODE-ECB afnix:sec:BlockCipher:MODE-ECB
98
const MODE-CBC afnix:sec:BlockCipher:MODE-CBC
99
const MODE-CFB afnix:sec:BlockCipher:MODE-CFB
100
const MODE-OFB afnix:sec:BlockCipher:MODE-OFB
102
# test without padding
103
test-rc2-cipher-no-padding kbuf ivec ovec ekey
105
# test in pad bit mode
106
test-rc2-cipher-with-padding kbuf ivec ekey PAD-BIT-MODE MODE-ECB
107
test-rc2-cipher-with-padding kbuf ivec ekey PAD-BIT-MODE MODE-CBC
108
test-rc2-cipher-with-padding kbuf ivec ekey PAD-BIT-MODE MODE-CFB
109
test-rc2-cipher-with-padding kbuf ivec ekey PAD-BIT-MODE MODE-OFB
112
test-rc2-cipher-with-padding kbuf ivec ekey PAD-ANSI-X923 MODE-ECB
113
test-rc2-cipher-with-padding kbuf ivec ekey PAD-ANSI-X923 MODE-CBC
114
test-rc2-cipher-with-padding kbuf ivec ekey PAD-ANSI-X923 MODE-CFB
115
test-rc2-cipher-with-padding kbuf ivec ekey PAD-ANSI-X923 MODE-OFB
118
test-rc2-cipher-with-padding kbuf ivec ekey PAD-NIST-800 MODE-ECB
119
test-rc2-cipher-with-padding kbuf ivec ekey PAD-NIST-800 MODE-CBC
120
test-rc2-cipher-with-padding kbuf ivec ekey PAD-NIST-800 MODE-CFB
121
test-rc2-cipher-with-padding kbuf ivec ekey PAD-NIST-800 MODE-OFB
125
trans kbuf "0000000000000000"
127
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
130
0xeb 0xb7 0x73 0xf9 0x93 0x27 0x8e 0xff
132
test-rc2-cipher kbuf ivec ovec 63
135
trans kbuf "ffffffffffffffff"
137
0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff
140
0x27 0x8b 0x27 0xe4 0x2e 0x2f 0x0d 0x49
142
test-rc2-cipher-no-padding kbuf ivec ovec 64
145
trans kbuf "3000000000000000"
147
0x10 0x00 0x00 0x00 0x00 0x00 0x00 0x01
150
0x30 0x64 0x9e 0xdf 0x9b 0xe7 0xd2 0xc2
152
test-rc2-cipher-no-padding kbuf ivec ovec 64
157
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
160
0x61 0xa8 0xa2 0x44 0xad 0xac 0xcc 0xf0
162
test-rc2-cipher-no-padding kbuf ivec ovec 64
165
trans kbuf "88bca90e90875a"
167
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
170
0x6c 0xcf 0x43 0x08 0x97 0x4c 0x26 0x7f
172
test-rc2-cipher-no-padding kbuf ivec ovec 64
175
trans kbuf "88bca90e90875a7f0f79c384627bafb2"
177
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
180
0x1a 0x80 0x7d 0x27 0x2b 0xbe 0x5d 0xb1
182
test-rc2-cipher-no-padding kbuf ivec ovec 64
185
trans kbuf "88bca90e90875a7f0f79c384627bafb2"
187
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
190
0x22 0x69 0x55 0x2a 0xb0 0xf8 0x5c 0xa6
192
test-rc2-cipher-no-padding kbuf ivec ovec 128