chiark / gitweb /
sig: Move marshalling responsibility into sign function
[secnet.git] / rsa.c
diff --git a/rsa.c b/rsa.c
index 2db03c9bbdf45453cc37e2e7b3cb910c1cde4448..dfbc2372f605670842382c5cbe8c1e9af742c916 100644 (file)
--- a/rsa.c
+++ b/rsa.c
@@ -1,16 +1,41 @@
-/* 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 <stdio.h>
 #include <string.h>
 #include <gmp.h>
 #include "secnet.h"
 #include "util.h"
+#include "unaligned.h"
 
 #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
 
@@ -18,7 +43,7 @@
 
 struct rsapriv {
     closure_t cl;
-    struct rsaprivkey_if ops;
+    struct sigprivkey_if ops;
     struct cloc loc;
     MP_INT n;
     MP_INT p, dp;
@@ -27,7 +52,7 @@ struct rsapriv {
 };
 struct rsapub {
     closure_t cl;
-    struct rsapubkey_if ops;
+    struct sigpubkey_if ops;
     struct cloc loc;
     MP_INT e;
     MP_INT n;
@@ -90,11 +115,13 @@ static void emsa_pkcs1(MP_INT *n, MP_INT *m,
     mpz_set_str(m, buff, 16);
 }
 
-static string_t rsa_sign(void *sst, uint8_t *data, int32_t datalen)
+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;
+    string_t signature = 0;
+    bool_t ok;
 
     mpz_init(&a);
     mpz_init(&b);
@@ -124,8 +151,8 @@ static string_t rsa_sign(void *sst, uint8_t *data, int32_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);
@@ -138,12 +165,25 @@ static string_t rsa_sign(void *sst, uint8_t *data, int32_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 sig_checksig_fn rsa_sig_check;
 static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen,
                            cstring_t signature)
 {
@@ -177,9 +217,9 @@ 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;
@@ -199,6 +239,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) {
@@ -213,6 +256,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);
 }
 
@@ -249,9 +295,9 @@ 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;
@@ -262,7 +308,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\n");
+           cfgfatal(i->loc,"rsa-private","first argument must be a string\n");
        }
        filename=i->data.string;
     } else {