3 * $Id: pkcs1.c,v 1.2 2000/07/05 17:49:48 mdw Exp $
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/07/05 17:49:48 mdw
34 * Fix decoding functions, so that they don't run off the end of the
37 * Revision 1.1 2000/07/01 11:17:38 mdw
38 * New support for PKCS#1 message encoding.
42 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/bits.h>
47 #include <mLib/dstr.h>
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @pkcs1_cryptencode@ --- *
56 * Arguments: @const void *msg@ = pointer to message data
57 * @size_t msz@ = size of message data
58 * @void *buf@ = pointer to output buffer
59 * @size_t sz@ = size of the output buffer
60 * @void *p@ = pointer to PKCS1 parameter block
62 * Returns: Zero if all went well, negative on failure.
64 * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined
65 * in PKCS#1 v. 2.0 (RFC2437).
68 int pkcs1_cryptencode(const void *msg
, size_t msz
, void *buf
, size_t sz
,
76 /* --- Ensure that the buffer is sensibly sized --- */
78 if (pp
->epsz
+ msz
+ 11 > sz
)
81 /* --- Fill in the buffer --- */
87 n
= sz
- msz
- pp
->epsz
- 3;
88 r
->ops
->fill(r
, q
, n
);
89 for (i
= 0; i
< n
; i
++) {
91 *q
= r
->ops
->range(r
, 255) + 1;
95 memcpy(q
, pp
->ep
, pp
->epsz
);
101 /* --- @pkcs1_cryptdecode@ --- *
103 * Arguments: @const void *buf@ = pointer to encoded buffer
104 * @size_t sz@ = size of the encoded buffer
105 * @dstr *d@ = pointer to destination string
106 * @void *p@ = pointer to PKCS1 parameter block
108 * Returns: The length of the output string if successful, negative on
111 * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined
112 * in PKCS#1 v. 2.0 (RFC2437).
115 int pkcs1_cryptdecode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
121 /* --- Check the size of the block looks sane --- */
123 if (pp
->epsz
+ 11 > sz
)
128 /* --- Ensure that the block looks OK --- */
130 if (*q
++ != 0 || *q
++ != 2)
133 /* --- Check the nonzero padding --- */
136 while (*q
!= 0 && q
< qq
)
138 if (i
< 8 || qq
- q
< pp
->epsz
+ 1)
142 /* --- Check the encoding parameters --- */
144 if (memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
155 /* --- @pkcs1_sigencode@ --- *
157 * Arguments: @const void *msg@ = pointer to message data
158 * @size_t msz@ = size of message data
159 * @void *buf@ = pointer to output buffer
160 * @size_t sz@ = size of the output buffer
161 * @void *p@ = pointer to PKCS1 parameter block
163 * Returns: Zero if all went well, negative on failure.
165 * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined
166 * in PKCS#1 v. 2.0 (RFC2437).
169 int pkcs1_sigencode(const void *msg
, size_t msz
, void *buf
, size_t sz
,
176 /* --- Ensure that the buffer is sensibly sized --- */
178 if (pp
->epsz
+ msz
+ 11 > sz
)
181 /* --- Fill in the buffer --- */
187 n
= sz
- msz
- pp
->epsz
- 3;
191 memcpy(q
, pp
->ep
, pp
->epsz
);
197 /* --- @pkcs1_sigdecode@ --- *
199 * Arguments: @const void *buf@ = pointer to encoded buffer
200 * @size_t sz@ = size of the encoded buffer
201 * @dstr *d@ = pointer to destination string
202 * @void *p@ = pointer to PKCS1 parameter block
204 * Returns: The length of the output string if successful, negative on
207 * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined
208 * in PKCS#1 v. 2.0 (RFC2437).
211 int pkcs1_sigdecode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
217 /* --- Check the size of the block looks sane --- */
219 if (pp
->epsz
+ 10 > sz
)
224 /* --- Ensure that the block looks OK --- */
226 if (*q
++ != 0 || *q
++ != 1)
229 /* --- Check the padding --- */
232 while (*q
== 0xff && q
< qq
)
234 if (i
< 8 || qq
- q
< pp
->epsz
+ 1 || *q
++ != 0)
237 /* --- Check the encoding parameters --- */
239 if (memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
250 /*----- That's all, folks -------------------------------------------------*/