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