forked from antonscheffer/as_crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_as_crypto.sql
155 lines (155 loc) · 6.43 KB
/
test_as_crypto.sql
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
declare
procedure test( p_secret raw, p_type pls_integer, p_key raw, p_iv raw, p_txt varchar2 )
is
t_encr raw(32767);
begin
t_encr := as_crypto.encrypt( p_secret, p_type, p_key, p_iv );
if t_encr != dbms_crypto.encrypt( p_secret, p_type, p_key, p_iv )
then
dbms_output.put_line( 'Difference encrypting ' || p_txt );
end if;
if p_secret != as_crypto.decrypt( t_encr, p_type, p_key, p_iv )
then
dbms_output.put_line( 'Difference decrypting ' || p_txt );
end if;
end;
--
procedure test_mac( p_src raw, p_type pls_integer, p_key raw, p_txt varchar2 )
is
t_mac raw(3999);
begin
t_mac := as_crypto.mac( p_src, p_type, p_key );
if t_mac != dbms_crypto.mac( p_src, p_type, p_key )
then
dbms_output.put_line( 'Difference for mac ' || p_txt );
end if;
end;
begin
for i in 1 .. 18
loop
test( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.ENCRYPT_DES + as_crypto.CHAIN_CFB + as_crypto.PAD_NONE
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, null
, 'DES + CFB + NONE'
);
test( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.ENCRYPT_DES + as_crypto.CHAIN_CFB + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, null
, 'DES + CFB + PKCS5'
);
test( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.ENCRYPT_DES + as_crypto.CHAIN_CFB + as_crypto.PAD_ZERO
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, null
, 'DES + CFB + ZERO'
);
test( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.ENCRYPT_DES + as_crypto.CHAIN_CFB + as_crypto.PAD_ORCL
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, null
, 'DES + CFB + ORCL'
);
end loop;
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_DES + as_crypto.CHAIN_CFB + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, null
, 'DES + CBC + PKCS5'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_3DES_2KEY + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678') -- 16 bytes
, null
, '3DES_2KEY + CBC + PKCS5'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_AES + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678') -- 16 bytes
, null
, 'AES + CBC + PKCS5, keylen 16'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_AES + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('123456781234567812345678') -- 24 bytes
, null
, 'AES + CBC + PKCS5, keylen 24'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_AES + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('12345678123456781234567812345678') -- 32 bytes
, null
, 'AES + CBC + PKCS5, keylen 32'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_AES128 + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678') -- 16 bytes
, null
, 'AES128 + CBC + PKCS5'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_AES192 + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('123456781234567812345678') -- 24 bytes
, null
, 'AES192 + CBC + PKCS5'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_AES256 + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('12345678123456781234567812345678') -- 32 bytes
, null
, 'AES256 + CBC + PKCS5'
);
test( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ) -- 19 bytes
, as_crypto.ENCRYPT_3DES + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678ABCDEFGH') -- 24 bytes
, null
, '3DES + CBC + PKCS5'
);
test( utl_raw.copies( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ), 15 ) -- 19 bytes * 15
, as_crypto.ENCRYPT_3DES + as_crypto.CHAIN_CBC + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678ABCDEFGH') -- 24 bytes
, '567812345678ABCD' -- 8 bytes
, '3DES + CBC + PKCS5 + IV'
);
test( utl_raw.copies( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ), 15 ) -- 19 bytes * 15
, as_crypto.ENCRYPT_3DES + as_crypto.CHAIN_CFB + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678ABCDEFGH') -- 24 bytes
, '567812345678ABCD' -- 8 bytes
, '3DES + CFB + PKCS5 + IV'
);
test( utl_raw.copies( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ), 15 ) -- 19 bytes * 15
, as_crypto.ENCRYPT_3DES + as_crypto.CHAIN_ECB + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678ABCDEFGH') -- 24 bytes
, '567812345678ABCD' -- 8 bytes
, '3DES + ECB + PKCS5 + IV'
);
test( utl_raw.copies( utl_raw.cast_to_raw( '12345678ABCDEFGHIJ' ), 15 ) -- 19 bytes * 15
, as_crypto.ENCRYPT_3DES + as_crypto.CHAIN_OFB + as_crypto.PAD_PKCS5
, utl_raw.cast_to_raw('1234567812345678ABCDEFGH') -- 24 bytes
, '567812345678ABCD' -- 8 bytes
, '3DES + OFB + PKCS5 + IV'
);
--
for i in 1 .. 18
loop
test_mac( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.HMAC_MD5
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, 'MD5, size ' || i
);
test_mac( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.HMAC_SH1
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, 'SH1, size ' || i
);
$IF NOT DBMS_DB_VERSION.VER_LE_11 $THEN
test_mac( utl_raw.substr( utl_raw.cast_to_raw( '0123456789ABCDEFGH987654321' ), 1, i )
, as_crypto.HMAC_SH256
, utl_raw.cast_to_raw('12345678') -- 8 bytes
, 'SH256, size ' || i
);
$END
end loop;
dbms_output.put_line( 'Done' );
end;