chiark / gitweb /
Padding works.
[chiark-tcl.git] / crypto / crypto.c
1 /*
2  */
3
4 #include <endian.h>
5
6 #include "hbytes.h"
7 #include "tables.h"
8 #include "serpent.h"
9
10 void memxor(Byte *dest, const Byte *src, int l) {
11   while (l--) *dest++ ^= *src++;
12 }
13
14 const PadMethod padmethods[]= {
15   { "un", 0, 0 },
16   { "ua", 0, 1 },
17   { "pn", 1, 0 },
18   { "pa", 1, 1 },
19   { 0 }
20 };
21
22 int do_hbytes_pkcs5(ClientData cd, Tcl_Interp *ip,
23                     const PadMethod *meth, HBytes_Var v, Tcl_Obj *block,
24                     int *ok) {
25   int rc, blocksize, padlen, old_len, i;
26   Byte *padding;
27   const Byte *unpad;
28   
29   if (meth->use_algname) {
30     const BlockCipherAlgInfo *alg;
31     alg= enum_lookup_cached(ip,block,blockcipheralginfos,"cipher alg for pad");
32     if (!alg) return TCL_ERROR;
33     blocksize= alg->blocksize;
34   } else {
35     rc= Tcl_GetIntFromObj(ip, block, &blocksize);  if (rc) return rc;
36     if (blocksize < 1 || blocksize > 255)
37       return staticerr(ip, "block size out of pkcs#5 range 1..255");
38   }
39
40   if (meth->pad) {
41     padlen= blocksize - (hbytes_len(v.hb) % blocksize);
42     padding= hbytes_append(v.hb, padlen);
43     memset(padding, padlen, padlen);
44   } else {
45     old_len= hbytes_len(v.hb);  if (old_len % blocksize) goto bad;
46     unpad= hbytes_unappend(v.hb, 1);  if (!unpad) goto bad;
47     padlen= *unpad;
48     if (padlen < 1 || padlen > blocksize) goto bad;
49     unpad= hbytes_unappend(v.hb, padlen-1);  if (!unpad) goto bad;
50     for (i=0; i<padlen-1; i++, unpad++) if (*unpad != padlen) goto bad;
51   }
52
53   *ok= 1;
54   return TCL_OK;
55
56  bad:
57   *ok= 0;
58   return TCL_OK;
59 }