Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/dh/dh_check_pubkey.c
4396 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
10
#include "tomcrypt.h"
11
12
#ifdef LTC_MDH
13
14
/**
15
Check DH public key (INTERNAL ONLY, not part of public API)
16
@param key The key you wish to test
17
@return CRYPT_OK if successful
18
*/
19
int dh_check_pubkey(dh_key *key)
20
{
21
void *p_minus1;
22
ltc_mp_digit digit;
23
int i, digit_count, bits_set = 0, err;
24
25
LTC_ARGCHK(key != NULL);
26
27
if ((err = mp_init(&p_minus1)) != CRYPT_OK) {
28
return err;
29
}
30
31
/* avoid: y <= 1 OR y >= p-1 */
32
if ((err = mp_sub_d(key->prime, 1, p_minus1)) != CRYPT_OK) {
33
goto error;
34
}
35
if (mp_cmp(key->y, p_minus1) != LTC_MP_LT || mp_cmp_d(key->y, 1) != LTC_MP_GT) {
36
err = CRYPT_INVALID_ARG;
37
goto error;
38
}
39
40
/* public key must have more than one bit set */
41
digit_count = mp_get_digit_count(key->y);
42
for (i = 0; i < digit_count && bits_set < 2; i++) {
43
digit = mp_get_digit(key->y, i);
44
while (digit > 0) {
45
if (digit & 1) bits_set++;
46
digit >>= 1;
47
}
48
}
49
if (bits_set > 1) {
50
err = CRYPT_OK;
51
}
52
else {
53
err = CRYPT_INVALID_ARG;
54
}
55
56
error:
57
mp_clear(p_minus1);
58
return err;
59
}
60
61
#endif /* LTC_MDH */
62
63