Path: blob/master/libs/tomcrypt/src/pk/dh/dh_check_pubkey.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89#include "tomcrypt.h"1011#ifdef LTC_MDH1213/**14Check DH public key (INTERNAL ONLY, not part of public API)15@param key The key you wish to test16@return CRYPT_OK if successful17*/18int dh_check_pubkey(dh_key *key)19{20void *p_minus1;21ltc_mp_digit digit;22int i, digit_count, bits_set = 0, err;2324LTC_ARGCHK(key != NULL);2526if ((err = mp_init(&p_minus1)) != CRYPT_OK) {27return err;28}2930/* avoid: y <= 1 OR y >= p-1 */31if ((err = mp_sub_d(key->prime, 1, p_minus1)) != CRYPT_OK) {32goto error;33}34if (mp_cmp(key->y, p_minus1) != LTC_MP_LT || mp_cmp_d(key->y, 1) != LTC_MP_GT) {35err = CRYPT_INVALID_ARG;36goto error;37}3839/* public key must have more than one bit set */40digit_count = mp_get_digit_count(key->y);41for (i = 0; i < digit_count && bits_set < 2; i++) {42digit = mp_get_digit(key->y, i);43while (digit > 0) {44if (digit & 1) bits_set++;45digit >>= 1;46}47}48if (bits_set > 1) {49err = CRYPT_OK;50}51else {52err = CRYPT_INVALID_ARG;53}5455error:56mp_clear(p_minus1);57return err;58}5960#endif /* LTC_MDH */616263