2 * PKCS#5 padding mechanism for udp tunnel
4 * arguments: block size to pad to, must be power of 2
6 * encoding: append between 1 and n bytes, all of the same value being
7 * the number of bytes appended
10 * Copyright (C) 2000 Ian Jackson
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.
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.
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.
27 #include "forwarder.h"
33 static unsigned long setup(struct mechdata
**md_r
) {
35 unsigned long blocksize
;
39 blocksize
= getarg_ulong();
40 md
->mask
= blocksize
- 1;
41 arg_assert(!(md
->mask
& blocksize
));
42 arg_assert(blocksize
<= 255);
48 static void mes_pkcs5(struct mechdata
**md_r
, int *maxprefix_io
, int *maxsuffix_io
) {
49 unsigned long blocksize
;
51 blocksize
= setup(md_r
);
52 *maxsuffix_io
+= blocksize
+ 1;
55 static void mds_pkcs5(struct mechdata
**md_r
) {
59 static void menc_pkcs5(struct mechdata
*md
, struct buffer
*buf
) {
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 */
69 pad
= buf_append(buf
,padlen
);
70 memset(pad
,padlen
,padlen
);
73 static const char *mdec_pkcs5(struct mechdata
*md
, struct buffer
*buf
) {
78 BUF_UNAPPEND(padp
,buf
,1);
80 if (!padlen
|| (padlen
> md
->mask
+1)) return "invalid length";
82 BUF_UNAPPEND(padp
,buf
,padlen
-1);
83 for (i
=0; i
<padlen
-1; i
++)
84 if (*++padp
!= padlen
) return "corrupted padding";
89 STANDARD_MECHANISMLIST("pkcs5",pkcs5
)