chiark / gitweb /
@@ -1,10 +1,10 @@
[userv-utils.git] / ipif / mech-pkcs5.c
1 /*
2  * PKCS#5 padding mechanism for udp tunnel
3  *
4  * arguments: block size to pad to, must be power of 2
5  *
6  * encoding: append between 1 and n bytes, all of the same value being
7  *           the number of bytes appended
8  */
9 /*
10  * Copyright (C) 2000 Ian Jackson
11  *
12  * This is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with userv-utils; if not, write to the Free Software
24  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #include "forwarder.h"
28
29 struct mechdata {
30   unsigned mask;
31 };
32
33 static unsigned long setup(struct mechdata **md_r) {
34   struct mechdata *md;
35   unsigned long blocksize;
36
37   XMALLOC(md);
38
39   blocksize= getarg_ulong();
40   md->mask= blocksize - 1;
41   arg_assert(!(md->mask & blocksize));
42   arg_assert(blocksize <= 255);
43
44   *md_r= md;
45   return blocksize;
46 }
47
48 static void mes_pkcs5(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) {
49   unsigned long blocksize;
50   
51   blocksize= setup(md_r);
52   *maxsuffix_io += blocksize + 1;
53 }
54
55 static void mds_pkcs5(struct mechdata **md_r) {
56   setup(md_r);
57 }
58
59 static void menc_pkcs5(struct mechdata *md, struct buffer *buf) {
60   unsigned char *pad;
61   int padlen;
62
63                                   /* eg with blocksize=4 mask=3 mask+2=5   */
64                                   /* msgsize    20    21    22    23   24  */
65   padlen= md->mask - buf->size;   /*           -17   -18   -19   -16  -17  */
66   padlen &= md->mask;             /*             3     2     1     0    3  */
67   padlen++;                       /*             4     3     2     1    4  */
68
69   pad= buf_append(buf,padlen);
70   memset(pad,padlen,padlen);
71 }
72
73 static const char *mdec_pkcs5(struct mechdata *md, struct buffer *buf) {
74   unsigned char *padp;
75   unsigned padlen;
76   int i;
77
78   BUF_UNAPPEND(padp,buf,1);
79   padlen= *padp;
80   if (!padlen || (padlen > md->mask+1)) return "invalid length";
81
82   BUF_UNAPPEND(padp,buf,padlen-1);
83   for (i=0; i<padlen-1; i++)
84     if (*++padp != padlen) return "corrupted padding";
85
86   return 0;
87 }
88
89 STANDARD_MECHANISMLIST("pkcs5",pkcs5)