5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/bits.h>
33 #include <mLib/dstr.h>
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @pkcs1_cryptencode@ --- *
43 * Arguments: @mp *d@ = where to put the answer
44 * @const void *m@ = pointer to message data
45 * @size_t msz@ = size of message data
46 * @octet *b@ = spare buffer
47 * @size_t sz@ = size of the buffer (big enough)
48 * @unsigned long nbits@ = length of bits of @n@
49 * @void *p@ = pointer to PKCS1 parameter block
51 * Returns: The encoded result, or null.
53 * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined
54 * in PKCS#1 v. 2.0 (RFC2437).
57 mp
*pkcs1_cryptencode(mp
*d
, const void *m
, size_t msz
, octet
*b
, size_t sz
,
58 unsigned long nbits
, void *p
)
65 /* --- Ensure that the buffer is sensibly sized --- */
67 if (pp
->epsz
+ msz
+ 11 > sz
)
70 /* --- Allocate the buffer and fill it in --- */
75 n
= sz
- msz
- pp
->epsz
- 3;
77 for (i
= 0; i
< n
; i
++) {
79 *q
= r
->ops
->range(r
, 255) + 1;
84 memcpy(q
, pp
->ep
, pp
->epsz
);
91 /* --- Collect the result --- */
93 return (mp_loadb(d
, b
, sz
));
96 /* --- @pkcs1_cryptdecode@ --- *
98 * Arguments: @mp *m@ = the decrypted message
99 * @octet *b@ = pointer to a buffer to work in
100 * @size_t sz@ = the size of the buffer (big enough)
101 * @unsigned long nbits@ = the number of bits in @n@
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(mp
*m
, octet
*b
, size_t sz
,
112 unsigned long nbits
, void *p
)
119 /* --- Check the size of the block looks sane --- */
121 if (pp
->epsz
+ 11 > sz
) /* OK: independent of ciphertext */
127 /* --- Ensure that the block looks OK --- */
129 goodp
&= ct_inteq(*q
++, 0);
130 goodp
&= ct_inteq(*q
++, 2);
132 /* --- Check the nonzero padding --- */
135 while (*q
!= 0 && q
< qq
)
137 goodp
&= ct_intle(8, i
);
138 goodp
&= ~ct_intle(qq
- q
, pp
->epsz
+ 1);
141 /* --- Check the encoding parameters --- */
144 goodp
&= ct_memeq(b
+ ct_pick(goodp
, 0, q
- b
), pp
->ep
, pp
->epsz
);
150 memmove(b
, b
+ ct_pick(goodp
, 1, q
- b
), n
);
151 return (goodp ? n
: -1);
154 /* --- @pkcs1_sigencode@ --- *
156 * Arguments: @mp *d@ = where to put the answer
157 * @const void *m@ = pointer to message data
158 * @size_t msz@ = size of message data
159 * @octet *b@ = spare buffer
160 * @size_t sz@ = size of the buffer (big enough)
161 * @unsigned long nbits@ = length of bits of @n@
162 * @void *p@ = pointer to PKCS1 parameter block
164 * Returns: The encoded message representative, or null.
166 * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined
167 * in PKCS#1 v. 2.0 (RFC2437).
170 mp
*pkcs1_sigencode(mp
*d
, const void *m
, size_t msz
, octet
*b
, size_t sz
,
171 unsigned long nbits
, void *p
)
177 /* --- Ensure that the buffer is sensibly sized --- */
179 if (pp
->epsz
+ msz
+ 11 > sz
)
182 /* --- Fill in the buffer --- */
187 n
= sz
- msz
- pp
->epsz
- 3;
192 memcpy(q
, pp
->ep
, pp
->epsz
);
198 return (mp_loadb(d
, b
, sz
));
201 /* --- @pkcs1_sigdecode@ --- *
203 * Arguments: @mp *s@ = the message representative
204 * @const void *m@ = the original message, or null (ignored)
205 * @size_t msz@ = the message size (ignored)
206 * @octet *b@ = a scratch buffer
207 * @size_t sz@ = size of the buffer (large enough)
208 * @unsigned long nbits@ = number of bits in @n@
209 * @void *p@ = pointer to PKCS1 parameters
211 * Returns: The length of the output string if successful, negative on
214 * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined
215 * in PKCS#1 v. 2.0 (RFC2437).
218 int pkcs1_sigdecode(mp
*s
, const void *m
, size_t msz
, octet
*b
, size_t sz
,
219 unsigned long nbits
, void *p
)
225 /* --- Check the size of the block looks sane --- */
227 if (pp
->epsz
+ 10 > sz
)
233 /* --- Ensure that the block looks OK --- */
235 if (*q
++ != 0x00 || *q
++ != 0x01)
238 /* --- Check the padding --- */
241 while (*q
== 0xff && q
< qq
)
243 if (i
< 8 || qq
- q
< pp
->epsz
+ 1 || *q
++ != 0)
246 /* --- Check the encoding parameters --- */
248 if (pp
->ep
&& memcmp(q
, pp
->ep
, pp
->epsz
) != 0)
259 /*----- That's all, folks -------------------------------------------------*/