3 * Probabistic signature scheme
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/alloc.h>
33 #include <mLib/bits.h>
34 #include <mLib/dstr.h>
35 #include <mLib/macros.h>
42 /*----- Magic statics -----------------------------------------------------*/
44 static const octet z8[8] = { 0 };
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @pss_encode@ --- *
50 * Arguments: @mp *d@ = where to put the answer
51 * @const void *m@ = pointer to the message hash
52 * @size_t msz@ = the size of the message hash
53 * @octet *b@ = scratch buffer
54 * @size_t sz@ = sizeo of the buffer (large enough)
55 * @unsigned long nbits@ = size in bits of @n@
56 * @void *p@ = pointer to the PSS parameters
58 * Returns: Encoded message representative, or null on error.
60 * Use: Implements the operation @EMSA-PSS-ENCODE@, as defined in
61 * PKCS#1 v. 2.1 (RFC3447).
64 mp *pss_encode(mp *d, const void *m, size_t msz, octet *b, size_t sz,
65 unsigned long nbits, void *p)
72 size_t pssz, hsz = pp->ch->hashsz;
74 /* --- Check the message length --- */
78 mask = (1 << nbits%8) - 1;
79 if (!mask) mask = 0xff;
80 if (hsz + pp->ssz + 2 > sz)
83 /* --- Generate a random salt --- */
85 pssz = sz - pp->ssz - hsz - 2;
90 GR_FILL(pp->r, s, pp->ssz);
92 /* --- Compute the salted hash --- */
97 GH_HASH(h, s, pp->ssz);
101 /* --- Do the masking --- */
103 c = GC_INIT(pp->cc, r, hsz);
104 GC_ENCRYPT(c, b, b, pssz + pp->ssz + 1);
107 return (mp_loadb(d, b, sz));
110 /* --- @pss_decode@ --- *
112 * Arguments: @mp *s@ = the message representative
113 * @const void *m@ = the original message
114 * @size_t msz@ = the message size
115 * @octet *b@ = a scratch buffer
116 * @size_t sz@ = size of the buffer (large enough)
117 * @unsigned long nbits@ = number of bits in @n@
118 * @void *p@ = pointer to PKCS1 parameters
120 * Returns: The length of the output string if successful, negative on
123 * Use: Implements the operation @EMSA_PSS_VERIFY@, as defined in
124 * PCSK#1 v. 2.1 (RFC3447).
127 int pss_decode(mp *mi, const void *m, size_t msz, octet *b, size_t sz,
128 unsigned long nbits, void *p)
135 size_t pssz, hsz = pp->ch->hashsz, i;
138 /* --- Check the message length --- */
142 if (mp_octets(mi) > sz)
144 mask = (1 << nbits%8) - 1;
145 if (!mask) mask = 0xff;
146 if (hsz + pp->ssz + 2 > sz)
148 mp_storeb(mi, b, sz);
150 /* --- Split up the buffer --- */
152 pssz = sz - hsz - pp->ssz - 2;
158 /* --- Decode the seed --- */
162 c = GC_INIT(pp->cc, r, hsz);
163 GC_DECRYPT(c, b, b, pssz + pp->ssz + 1);
166 for (i = 0; i < pssz; i++)
167 if (b[i]) return (-1);
171 /* --- Hash the message --- */
176 GH_HASH(h, s, pp->ssz);
178 rc = MEMCMP(s, ==, r, hsz);
180 if (!rc) return (-1);
187 /*----- That's all, folks -------------------------------------------------*/