3 * $Id: pkcs1.c,v 1.3 2000/10/08 12:07:04 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.3 2000/10/08 12:07:04 mdw
34 * Don't do arithmetic on @void *@ pointers.
36 * Revision 1.2 2000/07/05 17:49:48 mdw
37 * Fix decoding functions, so that they don't run off the end of the
40 * Revision 1.1 2000/07/01 11:17:38 mdw
41 * New support for PKCS#1 message encoding.
45 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/bits.h>
50 #include <mLib/dstr.h>
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @pkcs1_cryptencode@ --- *
59 * Arguments: @const void *msg@ = pointer to message data
60 * @size_t msz@ = size of message data
61 * @void *buf@ = pointer to output buffer
62 * @size_t sz@ = size of the output buffer
63 * @void *p@ = pointer to PKCS1 parameter block
65 * Returns: Zero if all went well, negative on failure.
67 * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined
68 * in PKCS#1 v. 2.0 (RFC2437).
71 int pkcs1_cryptencode(const void *msg
, size_t msz
, void *buf
, size_t sz
,
79 /* --- Ensure that the buffer is sensibly sized --- */
81 if (pp
->epsz
+ msz
+ 11 > sz
)
84 /* --- Fill in the buffer --- */
90 n
= sz
- msz
- pp
->epsz
- 3;
91 r
->ops
->fill(r
, q
, n
);
92 for (i
= 0; i
< n
; i
++) {
94 *q
= r
->ops
->range(r
, 255) + 1;
98 memcpy(q
, pp
->ep
, pp
->epsz
);
104 /* --- @pkcs1_cryptdecode@ --- *
106 * Arguments: @const void *buf@ = pointer to encoded buffer
107 * @size_t sz@ = size of the encoded buffer
108 * @dstr *d@ = pointer to destination string
109 * @void *p@ = pointer to PKCS1 parameter block
111 * Returns: The length of the output string if successful, negative on
114 * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined
115 * in PKCS#1 v. 2.0 (RFC2437).
118 int pkcs1_cryptdecode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
124 /* --- Check the size of the block looks sane --- */
126 if (pp
->epsz
+ 11 > sz
)
131 /* --- Ensure that the block looks OK --- */
133 if (*q
++ != 0 || *q
++ != 2)
136 /* --- Check the nonzero padding --- */
139 while (*q
!= 0 && q
< qq
)
141 if (i
< 8 || qq
- q
< pp
->epsz
+ 1)
145 /* --- Check the encoding parameters --- */
147 if (memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
158 /* --- @pkcs1_sigencode@ --- *
160 * Arguments: @const void *msg@ = pointer to message data
161 * @size_t msz@ = size of message data
162 * @void *buf@ = pointer to output buffer
163 * @size_t sz@ = size of the output buffer
164 * @void *p@ = pointer to PKCS1 parameter block
166 * Returns: Zero if all went well, negative on failure.
168 * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined
169 * in PKCS#1 v. 2.0 (RFC2437).
172 int pkcs1_sigencode(const void *msg
, size_t msz
, void *buf
, size_t sz
,
179 /* --- Ensure that the buffer is sensibly sized --- */
181 if (pp
->epsz
+ msz
+ 11 > sz
)
184 /* --- Fill in the buffer --- */
190 n
= sz
- msz
- pp
->epsz
- 3;
194 memcpy(q
, pp
->ep
, pp
->epsz
);
200 /* --- @pkcs1_sigdecode@ --- *
202 * Arguments: @const void *buf@ = pointer to encoded buffer
203 * @size_t sz@ = size of the encoded buffer
204 * @dstr *d@ = pointer to destination string
205 * @void *p@ = pointer to PKCS1 parameter block
207 * Returns: The length of the output string if successful, negative on
210 * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined
211 * in PKCS#1 v. 2.0 (RFC2437).
214 int pkcs1_sigdecode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
220 /* --- Check the size of the block looks sane --- */
222 if (pp
->epsz
+ 10 > sz
)
227 /* --- Ensure that the block looks OK --- */
229 if (*q
++ != 0 || *q
++ != 1)
232 /* --- Check the padding --- */
235 while (*q
== 0xff && q
< qq
)
237 if (i
< 8 || qq
- q
< pp
->epsz
+ 1 || *q
++ != 0)
240 /* --- Check the encoding parameters --- */
242 if (memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
253 /*----- That's all, folks -------------------------------------------------*/