X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=rsa.c;h=28a4c1b3225c3caefbb5a296f7582a149b63d245;hb=4a6ee8b677fd7addc97fc0d245f95ddde3b5f8e4;hp=3c82a40cfdc7f57876991073adbdbcc7cf2c765f;hpb=3b83c93292fbf6c4e859ce513bdf54ad90733f96;p=secnet.git diff --git a/rsa.c b/rsa.c index 3c82a40..28a4c1b 100644 --- a/rsa.c +++ b/rsa.c @@ -1,25 +1,56 @@ -/* 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 #include #include "secnet.h" #include "util.h" +#include "unaligned.h" #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n" #define mpp(s,n) do { char *p = mpz_get_str(NULL,16,n); printf("%s 0x%sL\n", s, p); free(p); } while (0) +struct rsacommon { + struct hash_if *hashi; + uint8_t *hashbuf; +}; + struct rsapriv { closure_t cl; - struct rsaprivkey_if ops; + struct sigprivkey_if ops; struct cloc loc; + struct rsacommon common; MP_INT n; MP_INT p, dp; MP_INT q, dq; @@ -27,25 +58,48 @@ struct rsapriv { }; struct rsapub { closure_t cl; - struct rsapubkey_if ops; + struct sigpubkey_if ops; struct cloc loc; + struct rsacommon common; MP_INT e; MP_INT n; }; /* 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 string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen) +static void rsa_sethash(struct rsacommon *c, struct hash_if *hash) +{ + free(c->hashbuf); + c->hashbuf=safe_malloc(hash->hlen, "generate_msg"); + c->hashi=hash; +} +static void rsa_pub_sethash(void *sst, struct hash_if *hash) +{ + struct rsapub *st=sst; + rsa_sethash(&st->common, hash); +} +static void rsa_priv_sethash(void *sst, struct hash_if *hash) { struct rsapriv *st=sst; - MP_INT a, b, u, v, tmp, tmp2; - char buff[2048]; - int msize, i; - string_t signature; + rsa_sethash(&st->common, hash); +} +static void rsa_hash(struct rsacommon *c, const uint8_t *buf, int32_t len) +{ + hash_hash(c->hashi,buf,len,c->hashbuf); +} - mpz_init(&a); - mpz_init(&b); +static void emsa_pkcs1(MP_INT *n, MP_INT *m, + const uint8_t *data, int32_t datalen) +{ + char buff[2*RSA_MAX_MODBYTES + 1]; + int msize, i; /* RSA PKCS#1 v1.5 signature padding: * @@ -65,7 +119,7 @@ static string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen) * -iwj 17.9.2002 */ - msize=mpz_sizeinbase(&st->n, 16); + msize=mpz_sizeinbase(n, 16); if (datalen*2+6>=msize) { fatal("rsa_sign: message too big"); @@ -86,7 +140,23 @@ static string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen) buff[msize]=0; - mpz_set_str(&a, buff, 16); + mpz_set_str(m, buff, 16); +} + +static bool_t rsa_sign(void *sst, uint8_t *data, int32_t datalen, + struct buffer_if *msg) +{ + struct rsapriv *st=sst; + MP_INT a, b, u, v, tmp, tmp2; + string_t signature = 0; + bool_t ok; + + mpz_init(&a); + mpz_init(&b); + + rsa_hash(&st->common,data,datalen); + /* Construct the message representative. */ + emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->common.hashi->hlen); /* * Produce an RSA signature (a^d mod n) using the Chinese @@ -110,8 +180,8 @@ static string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen) mpz_init(&u); mpz_init(&v); - mpz_powm(&u, &a, &st->dp, &st->p); - mpz_powm(&v, &a, &st->dq, &st->q); + mpz_powm_sec(&u, &a, &st->dp, &st->p); + mpz_powm_sec(&v, &a, &st->dq, &st->q); mpz_sub(&tmp, &u, &v); mpz_mul(&tmp2, &tmp, &st->w); mpz_add(&tmp, &tmp2, &v); @@ -124,45 +194,62 @@ static string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen) signature=write_mpstring(&b); + uint8_t *op = buf_append(msg,2); + if (!op) { ok=False; goto out; } + size_t l = strlen(signature); + assert(l < 65536); + put_uint16(op, l); + op = buf_append(msg,l); + if (!op) { ok=False; goto out; } + memcpy(op, signature, l); + + ok = True; + + out: + free(signature); mpz_clear(&b); mpz_clear(&a); - return signature; + return ok; } -static rsa_checksig_fn rsa_sig_check; -static bool_t rsa_sig_check(void *sst, uint8_t *data, uint32_t datalen, - cstring_t signature) +static bool_t rsa_sig_unpick(void *sst, struct buffer_if *msg, + struct alg_msg_data *sig) +{ + uint8_t *lp = buf_unprepend(msg, 2); + if (!lp) return False; + sig->len = get_uint16(lp); + sig->start = buf_unprepend(msg, sig->len); + if (!sig->start) return False; + + /* In `rsa_sig_check' below, we assume that we can write a nul + * terminator following the signature. Make sure there's enough space. + */ + if (msg->start >= msg->base + msg->alloclen) + return False; + + return True; +} + +static sig_checksig_fn rsa_sig_check; +static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen, + const struct alg_msg_data *sig) { struct rsapub *st=sst; MP_INT a, b, c; - char buff[2048]; - int msize, i; bool_t ok; mpz_init(&a); mpz_init(&b); mpz_init(&c); - msize=mpz_sizeinbase(&st->n, 16); - - strcpy(buff,"0001"); + rsa_hash(&st->common,data,datalen); + emsa_pkcs1(&st->n, &a, st->common.hashbuf, st->common.hashi->hlen); - for (i=0; i>4]; - buff[msize+(-datalen+i)*2+1]=hexchars[data[i]&0xf]; - } - - buff[msize-datalen*2-2]= '0'; - buff[msize-datalen*2-1]= '0'; - - for (i=4; istart[sig->len]; + sig->start[sig->len] = 0; + mpz_set_str(&b, sig->start, 16); + sig->start[sig->len] = save; mpz_powm(&c, &b, &st->e, &st->n); @@ -182,19 +269,22 @@ 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.type=CL_SIGPUBKEY; st->cl.apply=NULL; st->cl.interface=&st->ops; st->ops.st=st; + st->ops.sethash=rsa_pub_sethash; + st->common.hashbuf=NULL; + st->ops.unpick=rsa_sig_unpick; st->ops.check=rsa_sig_check; st->loc=loc; i=list_elem(args,0); if (i) { if (i->type!=t_string) { - cfgfatal(i->loc,"rsa-public","first argument must be a string"); + cfgfatal(i->loc,"rsa-public","first argument must be a string\n"); } e=i->data.string; if (mpz_init_set_str(&st->e,e,10)!=0) { @@ -204,11 +294,14 @@ 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) { if (i->type!=t_string) { - cfgfatal(i->loc,"rsa-public","second argument must be a string"); + cfgfatal(i->loc,"rsa-public","second argument must be a string\n"); } n=i->data.string; if (mpz_init_set_str(&st->n,n,10)!=0) { @@ -218,6 +311,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); } @@ -254,12 +350,14 @@ 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.type=CL_SIGPRIVKEY; st->cl.apply=NULL; st->cl.interface=&st->ops; st->ops.st=st; + st->ops.sethash=rsa_priv_sethash; + st->common.hashbuf=NULL; st->ops.sign=rsa_sign; st->loc=loc; @@ -267,7 +365,7 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, i=list_elem(args,0); if (i) { if (i->type!=t_string) { - cfgfatal(i->loc,"rsa-public","first argument must be a string"); + cfgfatal(i->loc,"rsa-private","first argument must be a string\n"); } filename=i->data.string; } else { @@ -307,19 +405,19 @@ 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); } b=safe_malloc(length,"rsapriv_apply"); if (fread(b,length,1,f) != 1) { - cfgfatal_maybefile(f,loc,"rsa-private","error reading modulus"); + cfgfatal_maybefile(f,loc,"rsa-private","error reading modulus\n"); } mpz_init(&st->n); 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"); @@ -350,7 +448,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); } @@ -364,7 +462,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); } @@ -378,7 +476,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); } @@ -392,7 +490,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); } @@ -430,8 +528,9 @@ static list_t *rsapriv_apply(closure_t *self, struct cloc loc, dict_t *context, /* * Verify that d*e is congruent to 1 mod (p-1), and mod * (q-1). This is equivalent to it being congruent to 1 mod - * lcm(p-1,q-1), i.e. congruent to 1 mod phi(n). Note that - * phi(n) is _not_ simply (p-1)*(q-1). + * lambda(n) = lcm(p-1,q-1). The usual `textbook' condition, + * that d e == 1 (mod (p-1)(q-1)) is sufficient, but not + * actually necessary. */ mpz_mul(&tmp, &d, &e); mpz_sub_ui(&tmp2, &st->p, 1); @@ -487,7 +586,6 @@ assume_valid: return new_closure(&st->cl); } -init_module rsa_module; void rsa_module(dict_t *dict) { add_closure(dict,"rsa-private",rsapriv_apply);