From cd6c3eeba0c396300391d99e8a17558dca5305fd Mon Sep 17 00:00:00 2001 From: mdw Date: Sat, 1 Jul 2000 11:17:38 +0000 Subject: [PATCH] New support for PKCS#1 message encoding. --- pkcs1.c | 249 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkcs1.h | 138 +++++++++++++++++++++++++++++++++++ 2 files changed, 387 insertions(+) create mode 100644 pkcs1.c create mode 100644 pkcs1.h diff --git a/pkcs1.c b/pkcs1.c new file mode 100644 index 0000000..2dbf6de --- /dev/null +++ b/pkcs1.c @@ -0,0 +1,249 @@ +/* -*-c-*- + * + * $Id: pkcs1.c,v 1.1 2000/07/01 11:17:38 mdw Exp $ + * + * PKCS#1 1.5 packing + * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb 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 Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: pkcs1.c,v $ + * Revision 1.1 2000/07/01 11:17:38 mdw + * New support for PKCS#1 message encoding. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include + +#include +#include + +#include "grand.h" +#include "pkcs1.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @pkcs1_cryptencode@ --- * + * + * Arguments: @const void *msg@ = pointer to message data + * @size_t msz@ = size of message data + * @void *buf@ = pointer to output buffer + * @size_t sz@ = size of the output buffer + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: Zero if all went well, negative on failure. + * + * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +int pkcs1_cryptencode(const void *msg, size_t msz, void *buf, size_t sz, + void *p) +{ + pkcs1 *pp = p; + grand *r = pp->r; + octet *q, *qq; + size_t i, n; + + /* --- Ensure that the buffer is sensibly sized --- */ + + if (pp->epsz + msz + 11 > sz) + return (-1); + + /* --- Fill in the buffer --- */ + + q = buf; + qq = q + sz; + *q++ = 0; + *q++ = 2; + n = sz - msz - pp->epsz - 3; + r->ops->fill(r, q, n); + for (i = 0; i < n; i++) { + if (*q == 0) + *q = r->ops->range(r, 255) + 1; + q++; + } + *q++ = 0; + memcpy(q, pp->ep, pp->epsz); + q += pp->epsz; + memcpy(q, msg, msz); + return (0); +} + +/* --- @pkcs1_cryptdecode@ --- * + * + * Arguments: @const void *buf@ = pointer to encoded buffer + * @size_t sz@ = size of the encoded buffer + * @dstr *d@ = pointer to destination string + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: The length of the output string if successful, negative on + * failure. + * + * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +int pkcs1_cryptdecode(const void *buf, size_t sz, dstr *d, void *p) +{ + pkcs1 *pp = p; + const octet *q, *qq; + size_t n, i; + + /* --- Check the size of the block looks sane --- */ + + if (pp->epsz + 11 > sz) + return (-1); + q = buf; + qq = buf + sz; + + /* --- Ensure that the block looks OK --- */ + + if (*q++ != 0 || *q++ != 2) + return (-1); + + /* --- Check the nonzero padding --- */ + + i = 0; + while (*q != 0 && q < qq) + i++, q++; + if (i < 8 || q == qq) + return (-1); + q++; + + /* --- Check the encoding parameters --- */ + + if (memcmp(q, pp->ep, pp->epsz) != 0) + return (-1); + q += pp->epsz; + + /* --- Done --- */ + + n = qq - q; + dstr_putm(d, q, n); + return (n); +} + +/* --- @pkcs1_sigencode@ --- * + * + * Arguments: @const void *msg@ = pointer to message data + * @size_t msz@ = size of message data + * @void *buf@ = pointer to output buffer + * @size_t sz@ = size of the output buffer + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: Zero if all went well, negative on failure. + * + * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +int pkcs1_sigencode(const void *msg, size_t msz, void *buf, size_t sz, + void *p) +{ + pkcs1 *pp = p; + octet *q, *qq; + size_t n; + + /* --- Ensure that the buffer is sensibly sized --- */ + + if (pp->epsz + msz + 11 > sz) + return (-1); + + /* --- Fill in the buffer --- */ + + q = buf; + qq = q + sz; + *q++ = 0; + *q++ = 1; + n = sz - msz - pp->epsz - 3; + memset(q, 0xff, n); + q += n; + *q++ = 0; + memcpy(q, pp->ep, pp->epsz); + q += pp->epsz; + memcpy(q, msg, msz); + return (0); +} + +/* --- @pkcs1_sigdecode@ --- * + * + * Arguments: @const void *buf@ = pointer to encoded buffer + * @size_t sz@ = size of the encoded buffer + * @dstr *d@ = pointer to destination string + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: The length of the output string if successful, negative on + * failure. + * + * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +int pkcs1_sigdecode(const void *buf, size_t sz, dstr *d, void *p) +{ + pkcs1 *pp = p; + const octet *q, *qq; + size_t i, n; + + /* --- Check the size of the block looks sane --- */ + + if (pp->epsz + 10 > sz) + return (-1); + q = buf; + qq = buf + sz; + + /* --- Ensure that the block looks OK --- */ + + if (*q++ != 0 || *q++ != 1) + return (-1); + + /* --- Check the padding --- */ + + i = 0; + while (*q == 0xff && q < qq) + i++, q++; + if (i < 8 || q == qq) + return (-1); + if (*q != 0) + return (-1); + q++; + + /* --- Check the encoding parameters --- */ + + if (memcmp(q, pp->ep, pp->epsz) != 0) + return (-1); + q += pp->epsz; + + /* --- Done --- */ + + n = qq - q; + dstr_putm(d, q, n); + return (n); +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/pkcs1.h b/pkcs1.h new file mode 100644 index 0000000..ff5123f --- /dev/null +++ b/pkcs1.h @@ -0,0 +1,138 @@ +/* -*-c-*- + * + * $Id: pkcs1.h,v 1.1 2000/07/01 11:17:38 mdw Exp $ + * + * PKCS#1 1.5 packing + * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb 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 Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: pkcs1.h,v $ + * Revision 1.1 2000/07/01 11:17:38 mdw + * New support for PKCS#1 message encoding. + * + */ + +#ifndef CATACOMB_PKCS1_H +#define CATACOMB_PKCS1_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include +#include + +#ifndef CATACOMB_GRAND_H +# include "grand.h" +#endif + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct pkcs1 { + grand *r; /* Random number source */ + const void *ep; /* Encoding parameters block */ + size_t epsz; /* Size of the parameter block */ +} pkcs1; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @pkcs1_cryptencode@ --- * + * + * Arguments: @const void *msg@ = pointer to message data + * @size_t msz@ = size of message data + * @void *buf@ = pointer to output buffer + * @size_t sz@ = size of the output buffer + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: Zero if all went well, negative on failure. + * + * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +extern int pkcs1_cryptencode(const void */*msg*/, size_t /*msz*/, + void */*buf*/, size_t /*sz*/, void */*p*/); + +/* --- @pkcs1_cryptdecode@ --- * + * + * Arguments: @const void *buf@ = pointer to encoded buffer) + * @size_t sz@ = size of the encoded buffer + * @dstr *d@ = pointer to destination string + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: The length of the output string if successful, negative on + * failure. + * + * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +extern int pkcs1_cryptdecode(const void */*buf*/, size_t /*sz*/, + dstr */*d*/, void */*p*/); + +/* --- @pkcs1_sigencode@ --- * + * + * Arguments: @const void *msg@ = pointer to message data + * @size_t msz@ = size of message data + * @void *buf@ = pointer to output buffer + * @size_t sz@ = size of the output buffer + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: Zero if all went well, negative on failure. + * + * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +extern int pkcs1_sigencode(const void */*msg*/, size_t /*msz*/, + void */*buf*/, size_t /*sz*/, void */*p*/); + +/* --- @pkcs1_sigdecode@ --- * + * + * Arguments: @const void *buf@ = pointer to encoded buffer + * @size_t sz@ = size of the encoded buffer + * @dstr *d@ = pointer to destination string + * @void *p@ = pointer to PKCS1 parameter block + * + * Returns: The length of the output string if successful, negative on + * failure. + * + * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined + * in PKCS#1 v. 2.0 (RFC2437). + */ + +extern int pkcs1_sigdecode(const void */*buf*/, size_t /*sz*/, + dstr */*d*/, void */*p*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif -- 2.11.0