~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/mod/sec/tst/SEC0009.als

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ---------------------------------------------------------------------------
 
2
# - SEC0009.als                                                             -
 
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.                  -
 
7
# -                                                                         -
 
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
# ---------------------------------------------------------------------------
 
16
 
 
17
# @info   rc2 block cipher test unit
 
18
# @author amaury darsch
 
19
 
 
20
# get the module
 
21
interp:library "afnix-sec"
 
22
 
 
23
# this procedure test the rc2 cipher
 
24
const test-rc2-cipher-no-padding (kbuf ivec ovec ekey) {
 
25
  # create the key
 
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
 
33
  # bind the buffers
 
34
  trans ib   (Buffer ivec)
 
35
  trans ob   (Buffer)
 
36
  # encode the vector
 
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)
 
42
  }
 
43
  # reset in decode mode
 
44
  trans vb (Buffer ivec)
 
45
  rc2:set-reverse true
 
46
  # decode and verify
 
47
  trans blen (rc2:stream ib ob)
 
48
  loop (trans i 0) (< i blen) (i:++) {
 
49
    assert (vb:get i) (ib:get i)
 
50
  }
 
51
}
 
52
 
 
53
# this procedure test the rc2 cipher with padding
 
54
const test-rc2-cipher-with-padding (kbuf ivec ekey pmod cmod) {
 
55
  # create the key
 
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
 
63
 
 
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)
 
69
 
 
70
  # bind the buffer
 
71
  trans ib (Buffer ivec)
 
72
  # encode the buffer
 
73
  trans ob (Buffer)
 
74
  rc2:stream ob ib
 
75
  # decode the buffer
 
76
  trans vb (Buffer)
 
77
  rc2:set-reverse true
 
78
  rc2:stream vb ob
 
79
  
 
80
  # rebuild the input buffer
 
81
  assert 0 (ib:length)
 
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)
 
87
  }
 
88
}
 
89
 
 
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
 
96
  
 
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
 
101
  
 
102
  # test without padding
 
103
  test-rc2-cipher-no-padding kbuf ivec ovec ekey
 
104
  
 
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
 
110
 
 
111
  # test in ansi mode
 
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
 
116
 
 
117
  # test in nist mode
 
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
 
122
}
 
123
 
 
124
# test 0 
 
125
trans kbuf "0000000000000000"
 
126
trans ivec (Vector
 
127
  0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
 
128
)
 
129
trans ovec (Vector
 
130
  0xeb 0xb7 0x73 0xf9 0x93 0x27 0x8e 0xff
 
131
)
 
132
test-rc2-cipher kbuf ivec ovec 63
 
133
 
 
134
# test 1
 
135
trans kbuf "ffffffffffffffff"
 
136
trans ivec (Vector
 
137
  0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 
 
138
)
 
139
trans ovec (Vector
 
140
  0x27 0x8b 0x27 0xe4 0x2e 0x2f 0x0d 0x49
 
141
)
 
142
test-rc2-cipher-no-padding kbuf ivec ovec 64
 
143
 
 
144
# test 2
 
145
trans kbuf "3000000000000000"
 
146
trans ivec (Vector
 
147
  0x10 0x00 0x00 0x00 0x00 0x00 0x00 0x01 
 
148
)
 
149
trans ovec (Vector
 
150
  0x30 0x64 0x9e 0xdf 0x9b 0xe7 0xd2 0xc2
 
151
)
 
152
test-rc2-cipher-no-padding kbuf ivec ovec 64
 
153
 
 
154
# test 3
 
155
trans kbuf "88"
 
156
trans ivec (Vector
 
157
  0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
 
158
)
 
159
trans ovec (Vector
 
160
  0x61 0xa8 0xa2 0x44 0xad 0xac 0xcc 0xf0
 
161
)
 
162
test-rc2-cipher-no-padding kbuf ivec ovec 64
 
163
 
 
164
# test 4
 
165
trans kbuf "88bca90e90875a"
 
166
trans ivec (Vector
 
167
  0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
 
168
)
 
169
trans ovec (Vector
 
170
  0x6c 0xcf 0x43 0x08 0x97 0x4c 0x26 0x7f
 
171
)
 
172
test-rc2-cipher-no-padding kbuf ivec ovec 64
 
173
 
 
174
# test 5
 
175
trans kbuf "88bca90e90875a7f0f79c384627bafb2"
 
176
trans ivec (Vector
 
177
  0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
 
178
)
 
179
trans ovec (Vector
 
180
  0x1a 0x80 0x7d 0x27 0x2b 0xbe 0x5d 0xb1
 
181
)
 
182
test-rc2-cipher-no-padding kbuf ivec ovec 64
 
183
 
 
184
# test 6
 
185
trans kbuf "88bca90e90875a7f0f79c384627bafb2"
 
186
trans ivec (Vector
 
187
  0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
 
188
)
 
189
trans ovec (Vector
 
190
  0x22 0x69 0x55 0x2a 0xb0 0xf8 0x5c 0xa6
 
191
)
 
192
test-rc2-cipher-no-padding kbuf ivec ovec 128