X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/ba6e6b64033b1f9de49feccb5c9cd438354481f7..0f00dc4c8eb47e67bc0f148c2dd109f73a451e0a:/pub/dsa-misc.c diff --git a/pub/dsa-misc.c b/pub/dsa-misc.c new file mode 100644 index 0000000..4d8bc2a --- /dev/null +++ b/pub/dsa-misc.c @@ -0,0 +1,64 @@ +/* -*-c-*- + * + * Useful functions for doing DSA + * + * (c) 2008 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Header files ------------------------------------------------------*/ + +#include "dsa.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @dsa_h2n@ --- * + * + * Arguments: @mp *d@ = destination integer + * @mp *r@ = order of the DSA group + * @const void *h@ = pointer to message hash + * @size_t hsz@ = size (in bytes) of the hash output + * + * Returns: Resulting integer. + * + * Use: Converts a hash to an integer in the demented way necessary + * for DSA/ECDSA. This is, of course, completely insane, but + * there you go. + */ + +mp *dsa_h2n(mp *d, mp *r, const void *h, size_t hsz) +{ + size_t n = mp_bits(r); + size_t l = hsz; + size_t s = 0; + + if (n < 8*l) { + l = (n + 7)/8; + s = 8*l - n; + } + d = mp_loadb(d, h, l); + if (s) + d = mp_lsr(d, d, s); + return (d); +} + +/*----- That's all, folks -------------------------------------------------*/