1 /* crypto-ctx.c --- Shishi crypto context self tests.
2 * Copyright (C) 2002, 2003, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 static const char rnd
[] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123456789";
27 static const char iv
[] =
28 "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
31 "abcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
40 const struct tv tv
[] = {
41 {SHISHI_DES_CBC_CRC
, 4, 8, 68},
42 {SHISHI_DES_CBC_MD4
, 0, 8, 72},
43 {SHISHI_DES_CBC_MD5
, 0, 8, 72},
44 {SHISHI_DES_CBC_NONE
, 8, 8, 72},
45 {SHISHI_DES3_CBC_NONE
, 8, 8, 72},
46 /* XXX following three doesn't use start=0 because of a weird
47 realloc(0,0) problem */
48 {SHISHI_DES3_CBC_HMAC_SHA1_KD
, 8, 8, 72},
49 {SHISHI_AES128_CTS_HMAC_SHA1_96
, 1, 1, 72},
50 {SHISHI_AES256_CTS_HMAC_SHA1_96
, 1, 1, 72},
51 {SHISHI_ARCFOUR_HMAC
, 0, 1, 72},
52 {SHISHI_ARCFOUR_HMAC_EXP
, 0, 1, 72},
57 test (Shishi
* handle
)
59 Shishi_crypto
*ctx
, *ctx2
;
68 shishi_cfg (handle
, strdup ("verbose-crypto"));
70 for (i
= 0; tvp
= &tv
[i
], tvp
->etype
; i
++)
72 len
= shishi_cipher_randomlen (tvp
->etype
);
75 fail ("shishi_cipher_randomlen(%d) failed: %d\n", tvp
->etype
, len
);
79 err
= shishi_key_from_random (handle
, tvp
->etype
, rnd
, len
, &key
);
82 fail ("shishi_key_from_random(%d) failed (%d)\n", tvp
->etype
, err
);
86 len
= shishi_cipher_blocksize (tvp
->etype
);
89 fail ("shishi_cipher_blocksize (%d) failed: %d\n", tvp
->etype
, len
);
93 if (tvp
->etype
== SHISHI_ARCFOUR_HMAC
||
94 tvp
->etype
== SHISHI_ARCFOUR_HMAC_EXP
)
96 /* For ARCFOUR, IV is internal S-BOX, not of blocksize length.
97 We probably should clean this up somehow... */
98 ctx
= shishi_crypto (handle
, key
, SHISHI_KEYUSAGE_ENCASREPPART
,
100 ctx2
= shishi_crypto (handle
, key
, SHISHI_KEYUSAGE_ENCASREPPART
,
101 tvp
->etype
, NULL
, 0);
105 ctx
= shishi_crypto (handle
, key
, SHISHI_KEYUSAGE_ENCASREPPART
,
106 tvp
->etype
, iv
, len
);
107 ctx2
= shishi_crypto (handle
, key
, SHISHI_KEYUSAGE_ENCASREPPART
,
108 tvp
->etype
, iv
, len
);
112 fail ("shishi_crypto(%d) failed\n", tvp
->etype
);
117 fail ("shishi_crypto(%d) failed (2)\n", tvp
->etype
);
121 for (j
= tvp
->start
; j
< tvp
->len
; j
+= tvp
->step
)
125 err
= shishi_crypto_encrypt (ctx
, in
, j
, &out
, &len
);
128 fail ("shishi_crypto_encrypt(etype=%d, len=%d) failed (%d)\n",
133 err
= shishi_crypto_decrypt (ctx2
, out
, len
, &out2
, &len2
);
136 fail ("shishi_crypto_decrypt(etype=%d, len=%d) failed (%d)\n",
143 ok
= len2
!= j
|| memcmp (out2
, in
, len2
) != 0;
152 hexprint (out2
, len2
);
153 fail ("shishi_crypto_encrypt (in1, %d) failed\n", tvp
->etype
);
156 success ("shishi_crypto_encrypt/decrypt(etype=%d, len=%d) OK\n",
160 shishi_crypto_close (ctx
);
161 shishi_crypto_close (ctx2
);
162 shishi_key_done (key
);
164 success ("shishi_crypto_encrypt/decrypt(etype=%d) OK\n", tvp
->etype
);