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>
34 #include <mLib/macros.h>
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @pkcs1_cryptencode@ --- *
44 * Arguments: @mp *d@ = where to put the answer
45 * @const void *m@ = pointer to message data
46 * @size_t msz@ = size of message data
47 * @octet *b@ = spare buffer
48 * @size_t sz@ = size of the buffer (big enough)
49 * @unsigned long nbits@ = length of bits of @n@
50 * @void *p@ = pointer to PKCS1 parameter block
52 * Returns: The encoded result, or null.
54 * Use: Implements the operation @EME-PKCS1-V1_5-ENCODE@, as defined
55 * in PKCS#1 v. 2.0 (RFC2437).
58 mp
*pkcs1_cryptencode(mp
*d
, const void *m
, size_t msz
, octet
*b
, size_t sz
,
59 unsigned long nbits
, void *p
)
66 /* --- Ensure that the buffer is sensibly sized --- */
68 if (pp
->epsz
+ msz
+ 11 > sz
)
71 /* --- Allocate the buffer and fill it in --- */
76 n
= sz
- msz
- pp
->epsz
- 3;
78 for (i
= 0; i
< n
; i
++) {
80 *q
= r
->ops
->range(r
, 255) + 1;
85 memcpy(q
, pp
->ep
, pp
->epsz
);
92 /* --- Collect the result --- */
94 return (mp_loadb(d
, b
, sz
));
97 /* --- @pkcs1_cryptdecode@ --- *
99 * Arguments: @mp *m@ = the decrypted message
100 * @octet *b@ = pointer to a buffer to work in
101 * @size_t sz@ = the size of the buffer (big enough)
102 * @unsigned long nbits@ = the number of bits in @n@
103 * @void *p@ = pointer to PKCS1 parameter block
105 * Returns: The length of the output string if successful, negative on
108 * Use: Implements the operation @EME-PKCS1-V1_5-DECODE@, as defined
109 * in PKCS#1 v. 2.0 (RFC2437).
112 int pkcs1_cryptdecode(mp
*m
, octet
*b
, size_t sz
,
113 unsigned long nbits
, void *p
)
120 /* --- Check the size of the block looks sane --- */
122 if (pp
->epsz
+ 11 > sz
) /* OK: independent of ciphertext */
128 /* --- Ensure that the block looks OK --- */
130 goodp
&= ct_inteq(*q
++, 0);
131 goodp
&= ct_inteq(*q
++, 2);
133 /* --- Check the nonzero padding --- */
136 while (*q
!= 0 && q
< qq
)
138 goodp
&= ct_intle(8, i
);
139 goodp
&= ~ct_intle(qq
- q
, pp
->epsz
+ 1);
142 /* --- Check the encoding parameters --- */
145 goodp
&= ct_memeq(b
+ ct_pick(goodp
, 0, q
- b
), pp
->ep
, pp
->epsz
);
151 memmove(b
, b
+ ct_pick(goodp
, 1, q
- b
), n
);
152 return (goodp ? n
: -1);
155 /* --- @pkcs1_sigencode@ --- *
157 * Arguments: @mp *d@ = where to put the answer
158 * @const void *m@ = pointer to message data
159 * @size_t msz@ = size of message data
160 * @octet *b@ = spare buffer
161 * @size_t sz@ = size of the buffer (big enough)
162 * @unsigned long nbits@ = length of bits of @n@
163 * @void *p@ = pointer to PKCS1 parameter block
165 * Returns: The encoded message representative, or null.
167 * Use: Implements the operation @EMSA-PKCS1-V1_5-ENCODE@, as defined
168 * in PKCS#1 v. 2.0 (RFC2437).
171 mp
*pkcs1_sigencode(mp
*d
, const void *m
, size_t msz
, octet
*b
, size_t sz
,
172 unsigned long nbits
, void *p
)
178 /* --- Ensure that the buffer is sensibly sized --- */
180 if (pp
->epsz
+ msz
+ 11 > sz
)
183 /* --- Fill in the buffer --- */
188 n
= sz
- msz
- pp
->epsz
- 3;
193 memcpy(q
, pp
->ep
, pp
->epsz
);
199 return (mp_loadb(d
, b
, sz
));
202 /* --- @pkcs1_sigdecode@ --- *
204 * Arguments: @mp *s@ = the message representative
205 * @const void *m@ = the original message, or null (ignored)
206 * @size_t msz@ = the message size (ignored)
207 * @octet *b@ = a scratch buffer
208 * @size_t sz@ = size of the buffer (large enough)
209 * @unsigned long nbits@ = number of bits in @n@
210 * @void *p@ = pointer to PKCS1 parameters
212 * Returns: The length of the output string if successful, negative on
215 * Use: Implements the operation @EMSA-PKCS1-V1_5-DECODE@, as defined
216 * in PKCS#1 v. 2.0 (RFC2437).
219 int pkcs1_sigdecode(mp
*s
, const void *m
, size_t msz
, octet
*b
, size_t sz
,
220 unsigned long nbits
, void *p
)
226 /* --- Check the size of the block looks sane --- */
228 if (pp
->epsz
+ 10 > sz
)
234 /* --- Ensure that the block looks OK --- */
236 if (*q
++ != 0x00 || *q
++ != 0x01)
239 /* --- Check the padding --- */
242 while (*q
== 0xff && q
< qq
)
244 if (i
< 8 || qq
- q
< pp
->epsz
+ 1 || *q
++ != 0)
247 /* --- Check the encoding parameters --- */
249 if (pp
->ep
&& MEMCMP(q
, !=, pp
->ep
, pp
->epsz
))
260 /*----- That's all, folks -------------------------------------------------*/