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