static const char *hexchars="0123456789abcdef";
-static string_t rsa_sign(void *sst, uint8_t *data, uint32_t datalen)
+static void emsa_pkcs1(MP_INT *n, MP_INT *m,
+ const uint8_t *data, int32_t datalen)
{
- struct rsapriv *st=sst;
- MP_INT a, b, u, v, tmp, tmp2;
char buff[2048];
int msize, i;
- string_t signature;
-
- mpz_init(&a);
- mpz_init(&b);
/* RSA PKCS#1 v1.5 signature padding:
*
* -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");
buff[msize]=0;
- mpz_set_str(&a, buff, 16);
+ mpz_set_str(m, buff, 16);
+}
+
+static string_t rsa_sign(void *sst, uint8_t *data, int32_t datalen)
+{
+ struct rsapriv *st=sst;
+ MP_INT a, b, u, v, tmp, tmp2;
+ string_t signature;
+
+ mpz_init(&a);
+ mpz_init(&b);
+
+ /* Construct the message representative. */
+ emsa_pkcs1(&st->n, &a, data, datalen);
/*
* Produce an RSA signature (a^d mod n) using the Chinese
}
static rsa_checksig_fn rsa_sig_check;
-static bool_t rsa_sig_check(void *sst, uint8_t *data, uint32_t datalen,
+static bool_t rsa_sig_check(void *sst, uint8_t *data, int32_t datalen,
cstring_t signature)
{
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");
-
- for (i=0; i<datalen; i++) {
- buff[msize+(-datalen+i)*2]=hexchars[(data[i]&0xf0)>>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; i<msize-datalen*2-2; i++)
- buff[i]='f';
-
- buff[msize]=0;
-
- mpz_set_str(&a, buff, 16);
+ emsa_pkcs1(&st->n, &a, data, datalen);
mpz_set_str(&b, signature, 16);
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) {
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) {
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");
}
filename=i->data.string;
} else {
}
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);
/*
* 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);