X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/2cd2cf055c0c424f60751f7ac4007771e377f262..86420bb75f19f628ffd2d8ff9964e59ed99e3187:/rsa.c diff --git a/rsa.c b/rsa.c index 0ca5d19..51a382e 100644 --- a/rsa.c +++ b/rsa.c @@ -1,10 +1,34 @@ -/* This file is part of secnet, and is distributed under the terms of - the GNU General Public License version 2 or later. +/* + * rsa.c: implementation of RSA with PKCS#1 padding + */ +/* + * This file is Free Software. It was originally written for secnet. + * + * Copyright 1995-2003 Stephen Early + * Copyright 2002-2014 Ian Jackson + * Copyright 2001 Simon Tatham + * Copyright 2013 Mark Wooding + * + * You may redistribute secnet as a whole and/or modify it under the + * terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3, or (at your option) any + * later version. + * + * You may redistribute this file and/or modify it under the terms of + * the GNU General Public License as published by the Free Software + * Foundation; either version 2, or (at your option) any later + * version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, see + * https://www.gnu.org/licenses/gpl.html. + */ - Copyright (C) 1995-2002 Stephen Early - Copyright (C) 2001 Simon Tatham - Copyright (C) 2002 Ian Jackson - */ #include #include @@ -34,12 +58,18 @@ struct rsapub { }; /* Sign data. NB data must be smaller than modulus */ +#define RSA_MAX_MODBYTES 2048 +/* The largest modulus I've seen is 15360 bits, which works out at 1920 + * bytes. Using keys this big is quite implausible, but it doesn't cost us + * much to support them. + */ + static const char *hexchars="0123456789abcdef"; static void emsa_pkcs1(MP_INT *n, MP_INT *m, const uint8_t *data, int32_t datalen) { - char buff[2048]; + char buff[2*RSA_MAX_MODBYTES + 1]; int msize, i; /* RSA PKCS#1 v1.5 signature padding: @@ -171,7 +201,7 @@ static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context, item_t *i; string_t e,n; - st=safe_malloc(sizeof(*st),"rsapub_apply"); + NEW(st); st->cl.description="rsapub"; st->cl.type=CL_RSAPUBKEY; st->cl.apply=NULL; @@ -193,6 +223,9 @@ static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context, } else { cfgfatal(loc,"rsa-public","you must provide an encryption key\n"); } + if (mpz_sizeinbase(&st->e, 256) > RSA_MAX_MODBYTES) { + cfgfatal(loc, "rsa-public", "implausibly large public exponent\n"); + } i=list_elem(args,1); if (i) { @@ -207,6 +240,9 @@ static list_t *rsapub_apply(closure_t *self, struct cloc loc, dict_t *context, } else { cfgfatal(loc,"rsa-public","you must provide a modulus\n"); } + if (mpz_sizeinbase(&st->n, 256) > RSA_MAX_MODBYTES) { + cfgfatal(loc, "rsa-public", "implausibly large modulus\n"); + } return new_closure(&st->cl); } @@ -243,7 +279,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, MP_INT e,d,iqmp,tmp,tmp2,tmp3; bool_t valid; - st=safe_malloc(sizeof(*st),"rsapriv_apply"); + NEW(st); st->cl.description="rsapriv"; st->cl.type=CL_RSAPRIVKEY; st->cl.apply=NULL; @@ -296,7 +332,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, /* Read the public key */ keyfile_get_int(loc,f); /* Not sure what this is */ length=(keyfile_get_short(loc,f)+7)/8; - if (length>1024) { + if (length>RSA_MAX_MODBYTES) { cfgfatal(loc,"rsa-private","implausible length %ld for modulus\n", length); } @@ -308,7 +344,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, read_mpbin(&st->n,b,length); free(b); length=(keyfile_get_short(loc,f)+7)/8; - if (length>1024) { + if (length>RSA_MAX_MODBYTES) { cfgfatal(loc,"rsa-private","implausible length %ld for e\n",length); } b=safe_malloc(length,"rsapriv_apply"); @@ -339,7 +375,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, /* Read d */ length=(keyfile_get_short(loc,f)+7)/8; - if (length>1024) { + if (length>RSA_MAX_MODBYTES) { cfgfatal(loc,"rsa-private","implausibly long (%ld) decryption key\n", length); } @@ -353,7 +389,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, free(b); /* Read iqmp (inverse of q mod p) */ length=(keyfile_get_short(loc,f)+7)/8; - if (length>1024) { + if (length>RSA_MAX_MODBYTES) { cfgfatal(loc,"rsa-private","implausibly long (%ld)" " iqmp auxiliary value\n", length); } @@ -367,7 +403,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, free(b); /* Read q (the smaller of the two primes) */ length=(keyfile_get_short(loc,f)+7)/8; - if (length>1024) { + if (length>RSA_MAX_MODBYTES) { cfgfatal(loc,"rsa-private","implausibly long (%ld) q value\n", length); } @@ -381,7 +417,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, free(b); /* Read p (the larger of the two primes) */ length=(keyfile_get_short(loc,f)+7)/8; - if (length>1024) { + if (length>RSA_MAX_MODBYTES) { cfgfatal(loc,"rsa-private","implausibly long (%ld) p value\n", length); }