|
| 1 | +/*- |
| 2 | + * Copyright (c) 2021-2024 Ribose Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 16 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS |
| 18 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | + * POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "crypto.h" |
| 28 | +#include "config.h" |
| 29 | +#include "defaults.h" |
| 30 | +#include "dsa_common.h" |
| 31 | + |
| 32 | +pgp_hash_alg_t |
| 33 | +dsa_get_min_hash(size_t qsize) |
| 34 | +{ |
| 35 | + /* |
| 36 | + * I'm using _broken_ SHA1 here only because |
| 37 | + * some old implementations may not understand keys created |
| 38 | + * with other hashes. If you're sure we don't have to support |
| 39 | + * such implementations, please be my guest and remove it. |
| 40 | + */ |
| 41 | + return (qsize < 160) ? PGP_HASH_UNKNOWN : |
| 42 | + (qsize == 160) ? PGP_HASH_SHA1 : |
| 43 | + (qsize <= 224) ? PGP_HASH_SHA224 : |
| 44 | + (qsize <= 256) ? PGP_HASH_SHA256 : |
| 45 | + (qsize <= 384) ? PGP_HASH_SHA384 : |
| 46 | + (qsize <= 512) ? PGP_HASH_SHA512 |
| 47 | + /*(qsize>512)*/ : |
| 48 | + PGP_HASH_UNKNOWN; |
| 49 | +} |
| 50 | + |
| 51 | +size_t |
| 52 | +dsa_choose_qsize_by_psize(size_t psize) |
| 53 | +{ |
| 54 | + return (psize == 1024) ? 160 : |
| 55 | + (psize <= 2047) ? 224 : |
| 56 | + (psize <= 3072) ? DSA_MAX_Q_BITLEN : |
| 57 | + 0; |
| 58 | +} |
| 59 | + |
| 60 | +pgp_hash_alg_t |
| 61 | +ecdsa_get_min_hash(pgp_curve_t curve) |
| 62 | +{ |
| 63 | + switch (curve) { |
| 64 | + case PGP_CURVE_NIST_P_256: |
| 65 | + case PGP_CURVE_BP256: |
| 66 | + case PGP_CURVE_P256K1: |
| 67 | + return PGP_HASH_SHA256; |
| 68 | + case PGP_CURVE_NIST_P_384: |
| 69 | + case PGP_CURVE_BP384: |
| 70 | + return PGP_HASH_SHA384; |
| 71 | + case PGP_CURVE_NIST_P_521: |
| 72 | + case PGP_CURVE_BP512: |
| 73 | + return PGP_HASH_SHA512; |
| 74 | + default: |
| 75 | + return PGP_HASH_UNKNOWN; |
| 76 | + } |
| 77 | +} |
0 commit comments