Add.
[shishi.git] / tests / crypto-ctx.c
blob66cdd9a8cb673f8bf41b029ffb0c415d21eb77c5
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
22 #include "utils.c"
24 static const char rnd[] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123456789";
27 static const char iv[] =
28 "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
30 const char *in =
31 "abcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 struct tv
35 int etype;
36 size_t start;
37 size_t step;
38 size_t len;
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},
53 {0}
56 void
57 test (Shishi * handle)
59 Shishi_crypto *ctx, *ctx2;
60 Shishi_key *key;
61 char *out, *out2;
62 size_t i, j;
63 size_t len, len2;
64 const struct tv *tvp;
65 int err;
67 if (debug)
68 shishi_cfg (handle, strdup ("verbose-crypto"));
70 for (i = 0; tvp = &tv[i], tvp->etype; i++)
72 len = shishi_cipher_randomlen (tvp->etype);
73 if (len < 0)
75 fail ("shishi_cipher_randomlen(%d) failed: %d\n", tvp->etype, len);
76 continue;
79 err = shishi_key_from_random (handle, tvp->etype, rnd, len, &key);
80 if (err)
82 fail ("shishi_key_from_random(%d) failed (%d)\n", tvp->etype, err);
83 continue;
86 len = shishi_cipher_blocksize (tvp->etype);
87 if (len < 0)
89 fail ("shishi_cipher_blocksize (%d) failed: %d\n", tvp->etype, len);
90 continue;
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,
99 tvp->etype, NULL, 0);
100 ctx2 = shishi_crypto (handle, key, SHISHI_KEYUSAGE_ENCASREPPART,
101 tvp->etype, NULL, 0);
103 else
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);
110 if (!ctx)
112 fail ("shishi_crypto(%d) failed\n", tvp->etype);
113 continue;
115 if (!ctx2)
117 fail ("shishi_crypto(%d) failed (2)\n", tvp->etype);
118 continue;
121 for (j = tvp->start; j < tvp->len; j += tvp->step)
123 int ok;
125 err = shishi_crypto_encrypt (ctx, in, j, &out, &len);
126 if (err)
128 fail ("shishi_crypto_encrypt(etype=%d, len=%d) failed (%d)\n",
129 tvp->etype, j, err);
130 continue;
133 err = shishi_crypto_decrypt (ctx2, out, len, &out2, &len2);
134 if (err)
136 fail ("shishi_crypto_decrypt(etype=%d, len=%d) failed (%d)\n",
137 tvp->etype, j, err);
138 continue;
141 free (out);
143 ok = len2 != j || memcmp (out2, in, len2) != 0;
145 free (out2);
147 if (ok)
149 puts ("expected");
150 hexprint (in, j);
151 puts ("computed");
152 hexprint (out2, len2);
153 fail ("shishi_crypto_encrypt (in1, %d) failed\n", tvp->etype);
154 continue;
156 success ("shishi_crypto_encrypt/decrypt(etype=%d, len=%d) OK\n",
157 tvp->etype, j);
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);