3 * $Id: pss.c,v 1.1 2000/07/20 20:13:38 mdw Exp $
5 * Probabistic signature scheme
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/20 20:13:38 mdw
34 * Added Bellare and Rogaway's PSS encoding for RSA signatures.
38 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/alloc.h>
43 #include <mLib/bits.h>
44 #include <mLib/dstr.h>
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @pss_presign@ --- *
55 * Arguments: @pss *pp@ = pointer to PSS parameter block
57 * Returns: An initialized generic hash context.
59 * Use: Initializes a hash function for signing with PSS. A salt is
60 * chosen and written into the parameter block.
63 ghash
*pss_presign(pss
*pp
)
65 size_t hsz
= pp
->ch
->hashsz
;
66 octet
*salt
= xmalloc(hsz
);
69 pp
->r
->ops
->fill(pp
->r
, salt
, hsz
);
72 h
->ops
->hash(h
, salt
, hsz
);
76 /* --- @pss_encode@ --- *
78 * Arguments: @const void *msg@ = pointer to message (hash) data
79 * @size_t msz@ = size of message data
80 * @void *buf@ = pointer to output buffer
81 * @size_t sz@ = size of the output buffer
82 * @void *p@ = pointer to PSS parameter block
84 * Returns: Zero of all went well, negative on failure.
86 * Use: Implements the operation @EMSA-PSS-ENCODE@, as defined in
87 * PKCS#1 v. 2.1 draft 1.
90 int pss_encode(const void *msg
, size_t msz
, void *buf
, size_t sz
, void *p
)
95 size_t hsz
= pp
->ch
->hashsz
;
98 /* --- Ensure that everything is sensibly sized --- */
100 if (hsz
+ msz
+ 1 > sz
)
103 /* --- Fill in the initial buffer --- */
112 memcpy(mq
, pp
->salt
, hsz
);
115 memset(mq
+ hsz
, 0, n
- hsz
);
117 /* --- Do the encryption --- */
119 c
= pp
->cc
->init(msg
, msz
);
120 c
->ops
->encrypt(c
, mq
, mq
, n
);
128 /* --- @pss_decode@ --- *
130 * Arguments: @const void *buf@ = pointer to encoded buffer
131 * @size_t sz@ = size of the encoded byffer
132 * @dstr *d@ = pointer to destination string
133 * @void *p@ = pointer to PSS parameter block
135 * Returns: The length of the output string (hash) if successful,
136 * negative on failure.
138 * Use: Implements most of the operation @EMSA_PSS_VERIFY@, as
139 * defined in PCSK#1 v. 2.1 draft 1. The salt value is filled
140 * in ready for hashing of the data to start.
143 int pss_decode(const void *buf
, size_t sz
, dstr
*d
, void *p
)
150 size_t hsz
= pp
->ch
->hashsz
;
153 /* --- Ensure that the block is large enough --- */
155 if (sz
< 2 * hsz
+ 1)
158 q
= x_alloc(d
->a
, sz
);
161 /* --- Recover the salt --- */
169 c
= pp
->cc
->init(q
, hsz
);
170 c
->ops
->decrypt(c
, mq
, mq
, n
);
173 /* --- Now check the recovery --- */
185 if (memcmp(pp
->salt
, mq
, hsz
) != 0)
192 dstr_putm(d
, q
, hsz
);
200 /* --- @pss_preverify@ --- *
202 * Arguments: @pss *pp@ = pointer to PSS parameter block
204 * Returns: An initialized generic hash context.
206 * Use: Initializes a hash function for use with PSS. A salt is
207 * read from the parameter block, where @pss_decode@ should have
211 ghash
*pss_preverify(pss
*pp
)
213 size_t hsz
= pp
->ch
->hashsz
;
214 ghash
*h
= pp
->ch
->init();
215 h
->ops
->hash(h
, pp
->salt
, hsz
);
219 /* --- @pss_done@ --- *
221 * Arguments: @pss *pp@ = pointer to PSS parameter block
225 * Use: Disposes of a PSS parameter block once it's finished with.
228 void pss_done(pss
*pp
)
236 /*----- That's all, folks -------------------------------------------------*/