3 * $Id: pkcs1.c,v 1.1 2000/07/01 11:17:38 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.1 2000/07/01 11:17:38 mdw
34 * New support for PKCS#1 message encoding.
38 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/bits.h>
43 #include <mLib/dstr.h>
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @pkcs1_cryptencode@ --- *
52 * Arguments: @const void *msg@ = pointer to message data
53 * @size_t msz@ = size of message data
54 * @void *buf@ = pointer to output buffer
55 * @size_t sz@ = size of the output buffer
56 * @void *p@ = pointer to PKCS1 parameter block
58 * Returns: Zero if all went well, negative on failure.
60 * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined
61 * in PKCS#1 v. 2.0 (RFC2437).
64 int pkcs1_cryptencode(const void *msg
, size_t msz
, void *buf
, size_t sz
,
72 /* --- Ensure that the buffer is sensibly sized --- */
74 if (pp
->epsz
+ msz
+ 11 > sz
)
77 /* --- Fill in the buffer --- */
83 n
= sz
- msz
- pp
->epsz
- 3;
84 r
->ops
->fill(r
, q
, n
);
85 for (i
= 0; i
< n
; i
++) {
87 *q
= r
->ops
->range(r
, 255) + 1;
91 memcpy(q
, pp
->ep
, pp
->epsz
);
97 /* --- @pkcs1_cryptdecode@ --- *
99 * Arguments: @const void *buf@ = pointer to encoded buffer
100 * @size_t sz@ = size of the encoded buffer
101 * @dstr *d@ = pointer to destination string
102 * @void *p@ = pointer to PKCS1 parameter block
104 * Returns: The length of the output string if successful, negative on
107 * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined
108 * in PKCS#1 v. 2.0 (RFC2437).
111 int pkcs1_cryptdecode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
117 /* --- Check the size of the block looks sane --- */
119 if (pp
->epsz
+ 11 > sz
)
124 /* --- Ensure that the block looks OK --- */
126 if (*q
++ != 0 || *q
++ != 2)
129 /* --- Check the nonzero padding --- */
132 while (*q
!= 0 && q
< qq
)
134 if (i
< 8 || q
== qq
)
138 /* --- Check the encoding parameters --- */
140 if (memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
151 /* --- @pkcs1_sigencode@ --- *
153 * Arguments: @const void *msg@ = pointer to message data
154 * @size_t msz@ = size of message data
155 * @void *buf@ = pointer to output buffer
156 * @size_t sz@ = size of the output buffer
157 * @void *p@ = pointer to PKCS1 parameter block
159 * Returns: Zero if all went well, negative on failure.
161 * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined
162 * in PKCS#1 v. 2.0 (RFC2437).
165 int pkcs1_sigencode(const void *msg
, size_t msz
, void *buf
, size_t sz
,
172 /* --- Ensure that the buffer is sensibly sized --- */
174 if (pp
->epsz
+ msz
+ 11 > sz
)
177 /* --- Fill in the buffer --- */
183 n
= sz
- msz
- pp
->epsz
- 3;
187 memcpy(q
, pp
->ep
, pp
->epsz
);
193 /* --- @pkcs1_sigdecode@ --- *
195 * Arguments: @const void *buf@ = pointer to encoded buffer
196 * @size_t sz@ = size of the encoded buffer
197 * @dstr *d@ = pointer to destination string
198 * @void *p@ = pointer to PKCS1 parameter block
200 * Returns: The length of the output string if successful, negative on
203 * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined
204 * in PKCS#1 v. 2.0 (RFC2437).
207 int pkcs1_sigdecode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
213 /* --- Check the size of the block looks sane --- */
215 if (pp
->epsz
+ 10 > sz
)
220 /* --- Ensure that the block looks OK --- */
222 if (*q
++ != 0 || *q
++ != 1)
225 /* --- Check the padding --- */
228 while (*q
== 0xff && q
< qq
)
230 if (i
< 8 || q
== qq
)
236 /* --- Check the encoding parameters --- */
238 if (memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
249 /*----- That's all, folks -------------------------------------------------*/