Update netpgpverify to 20150901 + rename all the symbols I could fine which could conflict with those in libcrypto. requested by joergdiff -r1.14 -r1.15 pkgsrc/security/netpgpverify/Makefile
(agc)
@@ -1,16 +1,16 @@ | @@ -1,16 +1,16 @@ | |||
1 | # $NetBSD: Makefile,v 1.14 2015/08/17 11:37:55 jperkin Exp $ | 1 | # $NetBSD: Makefile,v 1.15 2015/09/01 19:38:42 agc Exp $ | |
2 | 2 | |||
3 | DISTNAME= netpgpverify-20150205 | 3 | DISTNAME= netpgpverify-20150901 | |
4 | CATEGORIES= security | 4 | CATEGORIES= security | |
5 | MASTER_SITES= # empty | 5 | MASTER_SITES= # empty | |
6 | DISTFILES= # empty | 6 | DISTFILES= # empty | |
7 | 7 | |||
8 | MAINTAINER= agc@NetBSD.org | 8 | MAINTAINER= agc@NetBSD.org | |
9 | HOMEPAGE= http://www.NetBSD.org/ | 9 | HOMEPAGE= http://www.NetBSD.org/ | |
10 | COMMENT= Standalone PGP and ssh signature verification utility | 10 | COMMENT= Standalone PGP and ssh signature verification utility | |
11 | LICENSE= modified-bsd | 11 | LICENSE= modified-bsd | |
12 | 12 | |||
13 | AUTO_MKDIRS= yes | 13 | AUTO_MKDIRS= yes | |
14 | GNU_CONFIGURE= yes | 14 | GNU_CONFIGURE= yes | |
15 | 15 | |||
16 | TEST_TARGET= tst | 16 | TEST_TARGET= tst |
@@ -211,27 +211,27 @@ trim_unused_digits(mp_int * a) | @@ -211,27 +211,27 @@ trim_unused_digits(mp_int * a) | |||
211 | * zero. | 211 | * zero. | |
212 | */ | 212 | */ | |
213 | while (a->used > 0 && a->dp[a->used - 1] == 0) { | 213 | while (a->used > 0 && a->dp[a->used - 1] == 0) { | |
214 | a->used -= 1; | 214 | a->used -= 1; | |
215 | } | 215 | } | |
216 | /* reset the sign flag if used == 0 */ | 216 | /* reset the sign flag if used == 0 */ | |
217 | if (a->used == 0) { | 217 | if (a->used == 0) { | |
218 | a->sign = MP_ZPOS; | 218 | a->sign = MP_ZPOS; | |
219 | } | 219 | } | |
220 | } | 220 | } | |
221 | 221 | |||
222 | /* copy, b = a */ | 222 | /* copy, b = a */ | |
223 | static int | 223 | static int | |
224 | mp_copy(BIGNUM *a, BIGNUM *b) | 224 | mp_copy(PGPV_BIGNUM *a, PGPV_BIGNUM *b) | |
225 | { | 225 | { | |
226 | int res; | 226 | int res; | |
227 | 227 | |||
228 | /* if dst == src do nothing */ | 228 | /* if dst == src do nothing */ | |
229 | if (a == b) { | 229 | if (a == b) { | |
230 | return MP_OKAY; | 230 | return MP_OKAY; | |
231 | } | 231 | } | |
232 | if (a == NULL || b == NULL) { | 232 | if (a == NULL || b == NULL) { | |
233 | return MP_VAL; | 233 | return MP_VAL; | |
234 | } | 234 | } | |
235 | 235 | |||
236 | /* grow dest */ | 236 | /* grow dest */ | |
237 | if (b->alloc < a->used) { | 237 | if (b->alloc < a->used) { | |
@@ -1328,51 +1328,51 @@ fast_modular_inverse(mp_int * a, mp_int | @@ -1328,51 +1328,51 @@ fast_modular_inverse(mp_int * a, mp_int | |||
1328 | } | 1328 | } | |
1329 | 1329 | |||
1330 | /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ | 1330 | /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ | |
1331 | if ((res = mp_copy(&x, &u)) != MP_OKAY) { | 1331 | if ((res = mp_copy(&x, &u)) != MP_OKAY) { | |
1332 | goto LBL_ERR; | 1332 | goto LBL_ERR; | |
1333 | } | 1333 | } | |
1334 | if ((res = mp_copy(&y, &v)) != MP_OKAY) { | 1334 | if ((res = mp_copy(&y, &v)) != MP_OKAY) { | |
1335 | goto LBL_ERR; | 1335 | goto LBL_ERR; | |
1336 | } | 1336 | } | |
1337 | set_word(&D, 1); | 1337 | set_word(&D, 1); | |
1338 | 1338 | |||
1339 | top: | 1339 | top: | |
1340 | /* 4. while u is even do */ | 1340 | /* 4. while u is even do */ | |
1341 | while (BN_is_even(&u) == 1) { | 1341 | while (PGPV_BN_is_even(&u) == 1) { | |
1342 | /* 4.1 u = u/2 */ | 1342 | /* 4.1 u = u/2 */ | |
1343 | if ((res = half(&u, &u)) != MP_OKAY) { | 1343 | if ((res = half(&u, &u)) != MP_OKAY) { | |
1344 | goto LBL_ERR; | 1344 | goto LBL_ERR; | |
1345 | } | 1345 | } | |
1346 | /* 4.2 if B is odd then */ | 1346 | /* 4.2 if B is odd then */ | |
1347 | if (BN_is_odd(&B) == 1) { | 1347 | if (PGPV_BN_is_odd(&B) == 1) { | |
1348 | if ((res = signed_subtract(&B, &x, &B)) != MP_OKAY) { | 1348 | if ((res = signed_subtract(&B, &x, &B)) != MP_OKAY) { | |
1349 | goto LBL_ERR; | 1349 | goto LBL_ERR; | |
1350 | } | 1350 | } | |
1351 | } | 1351 | } | |
1352 | /* B = B/2 */ | 1352 | /* B = B/2 */ | |
1353 | if ((res = half(&B, &B)) != MP_OKAY) { | 1353 | if ((res = half(&B, &B)) != MP_OKAY) { | |
1354 | goto LBL_ERR; | 1354 | goto LBL_ERR; | |
1355 | } | 1355 | } | |
1356 | } | 1356 | } | |
1357 | 1357 | |||
1358 | /* 5. while v is even do */ | 1358 | /* 5. while v is even do */ | |
1359 | while (BN_is_even(&v) == 1) { | 1359 | while (PGPV_BN_is_even(&v) == 1) { | |
1360 | /* 5.1 v = v/2 */ | 1360 | /* 5.1 v = v/2 */ | |
1361 | if ((res = half(&v, &v)) != MP_OKAY) { | 1361 | if ((res = half(&v, &v)) != MP_OKAY) { | |
1362 | goto LBL_ERR; | 1362 | goto LBL_ERR; | |
1363 | } | 1363 | } | |
1364 | /* 5.2 if D is odd then */ | 1364 | /* 5.2 if D is odd then */ | |
1365 | if (BN_is_odd(&D) == 1) { | 1365 | if (PGPV_BN_is_odd(&D) == 1) { | |
1366 | /* D = (D-x)/2 */ | 1366 | /* D = (D-x)/2 */ | |
1367 | if ((res = signed_subtract(&D, &x, &D)) != MP_OKAY) { | 1367 | if ((res = signed_subtract(&D, &x, &D)) != MP_OKAY) { | |
1368 | goto LBL_ERR; | 1368 | goto LBL_ERR; | |
1369 | } | 1369 | } | |
1370 | } | 1370 | } | |
1371 | /* D = D/2 */ | 1371 | /* D = D/2 */ | |
1372 | if ((res = half(&D, &D)) != MP_OKAY) { | 1372 | if ((res = half(&D, &D)) != MP_OKAY) { | |
1373 | goto LBL_ERR; | 1373 | goto LBL_ERR; | |
1374 | } | 1374 | } | |
1375 | } | 1375 | } | |
1376 | 1376 | |||
1377 | /* 6. if u >= v then */ | 1377 | /* 6. if u >= v then */ | |
1378 | if (signed_compare(&u, &v) != MP_LT) { | 1378 | if (signed_compare(&u, &v) != MP_LT) { | |
@@ -1441,75 +1441,75 @@ slow_modular_inverse(mp_int * a, mp_int | @@ -1441,75 +1441,75 @@ slow_modular_inverse(mp_int * a, mp_int | |||
1441 | &A, &B, &C, &D, NULL)) != MP_OKAY) { | 1441 | &A, &B, &C, &D, NULL)) != MP_OKAY) { | |
1442 | return res; | 1442 | return res; | |
1443 | } | 1443 | } | |
1444 | 1444 | |||
1445 | /* x = a, y = b */ | 1445 | /* x = a, y = b */ | |
1446 | if ((res = modulo(a, b, &x)) != MP_OKAY) { | 1446 | if ((res = modulo(a, b, &x)) != MP_OKAY) { | |
1447 | goto LBL_ERR; | 1447 | goto LBL_ERR; | |
1448 | } | 1448 | } | |
1449 | if ((res = mp_copy(b, &y)) != MP_OKAY) { | 1449 | if ((res = mp_copy(b, &y)) != MP_OKAY) { | |
1450 | goto LBL_ERR; | 1450 | goto LBL_ERR; | |
1451 | } | 1451 | } | |
1452 | 1452 | |||
1453 | /* 2. [modified] if x,y are both even then return an error! */ | 1453 | /* 2. [modified] if x,y are both even then return an error! */ | |
1454 | if (BN_is_even(&x) == 1 && BN_is_even(&y) == 1) { | 1454 | if (PGPV_BN_is_even(&x) == 1 && PGPV_BN_is_even(&y) == 1) { | |
1455 | res = MP_VAL; | 1455 | res = MP_VAL; | |
1456 | goto LBL_ERR; | 1456 | goto LBL_ERR; | |
1457 | } | 1457 | } | |
1458 | 1458 | |||
1459 | /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ | 1459 | /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ | |
1460 | if ((res = mp_copy(&x, &u)) != MP_OKAY) { | 1460 | if ((res = mp_copy(&x, &u)) != MP_OKAY) { | |
1461 | goto LBL_ERR; | 1461 | goto LBL_ERR; | |
1462 | } | 1462 | } | |
1463 | if ((res = mp_copy(&y, &v)) != MP_OKAY) { | 1463 | if ((res = mp_copy(&y, &v)) != MP_OKAY) { | |
1464 | goto LBL_ERR; | 1464 | goto LBL_ERR; | |
1465 | } | 1465 | } | |
1466 | set_word(&A, 1); | 1466 | set_word(&A, 1); | |
1467 | set_word(&D, 1); | 1467 | set_word(&D, 1); | |
1468 | 1468 | |||
1469 | top: | 1469 | top: | |
1470 | /* 4. while u is even do */ | 1470 | /* 4. while u is even do */ | |
1471 | while (BN_is_even(&u) == 1) { | 1471 | while (PGPV_BN_is_even(&u) == 1) { | |
1472 | /* 4.1 u = u/2 */ | 1472 | /* 4.1 u = u/2 */ | |
1473 | if ((res = half(&u, &u)) != MP_OKAY) { | 1473 | if ((res = half(&u, &u)) != MP_OKAY) { | |
1474 | goto LBL_ERR; | 1474 | goto LBL_ERR; | |
1475 | } | 1475 | } | |
1476 | /* 4.2 if A or B is odd then */ | 1476 | /* 4.2 if A or B is odd then */ | |
1477 | if (BN_is_odd(&A) == 1 || BN_is_odd(&B) == 1) { | 1477 | if (PGPV_BN_is_odd(&A) == 1 || PGPV_BN_is_odd(&B) == 1) { | |
1478 | /* A = (A+y)/2, B = (B-x)/2 */ | 1478 | /* A = (A+y)/2, B = (B-x)/2 */ | |
1479 | if ((res = signed_add(&A, &y, &A)) != MP_OKAY) { | 1479 | if ((res = signed_add(&A, &y, &A)) != MP_OKAY) { | |
1480 | goto LBL_ERR; | 1480 | goto LBL_ERR; | |
1481 | } | 1481 | } | |
1482 | if ((res = signed_subtract(&B, &x, &B)) != MP_OKAY) { | 1482 | if ((res = signed_subtract(&B, &x, &B)) != MP_OKAY) { | |
1483 | goto LBL_ERR; | 1483 | goto LBL_ERR; | |
1484 | } | 1484 | } | |
1485 | } | 1485 | } | |
1486 | /* A = A/2, B = B/2 */ | 1486 | /* A = A/2, B = B/2 */ | |
1487 | if ((res = half(&A, &A)) != MP_OKAY) { | 1487 | if ((res = half(&A, &A)) != MP_OKAY) { | |
1488 | goto LBL_ERR; | 1488 | goto LBL_ERR; | |
1489 | } | 1489 | } | |
1490 | if ((res = half(&B, &B)) != MP_OKAY) { | 1490 | if ((res = half(&B, &B)) != MP_OKAY) { | |
1491 | goto LBL_ERR; | 1491 | goto LBL_ERR; | |
1492 | } | 1492 | } | |
1493 | } | 1493 | } | |
1494 | 1494 | |||
1495 | /* 5. while v is even do */ | 1495 | /* 5. while v is even do */ | |
1496 | while (BN_is_even(&v) == 1) { | 1496 | while (PGPV_BN_is_even(&v) == 1) { | |
1497 | /* 5.1 v = v/2 */ | 1497 | /* 5.1 v = v/2 */ | |
1498 | if ((res = half(&v, &v)) != MP_OKAY) { | 1498 | if ((res = half(&v, &v)) != MP_OKAY) { | |
1499 | goto LBL_ERR; | 1499 | goto LBL_ERR; | |
1500 | } | 1500 | } | |
1501 | /* 5.2 if C or D is odd then */ | 1501 | /* 5.2 if C or D is odd then */ | |
1502 | if (BN_is_odd(&C) == 1 || BN_is_odd(&D) == 1) { | 1502 | if (PGPV_BN_is_odd(&C) == 1 || PGPV_BN_is_odd(&D) == 1) { | |
1503 | /* C = (C+y)/2, D = (D-x)/2 */ | 1503 | /* C = (C+y)/2, D = (D-x)/2 */ | |
1504 | if ((res = signed_add(&C, &y, &C)) != MP_OKAY) { | 1504 | if ((res = signed_add(&C, &y, &C)) != MP_OKAY) { | |
1505 | goto LBL_ERR; | 1505 | goto LBL_ERR; | |
1506 | } | 1506 | } | |
1507 | if ((res = signed_subtract(&D, &x, &D)) != MP_OKAY) { | 1507 | if ((res = signed_subtract(&D, &x, &D)) != MP_OKAY) { | |
1508 | goto LBL_ERR; | 1508 | goto LBL_ERR; | |
1509 | } | 1509 | } | |
1510 | } | 1510 | } | |
1511 | /* C = C/2, D = D/2 */ | 1511 | /* C = C/2, D = D/2 */ | |
1512 | if ((res = half(&C, &C)) != MP_OKAY) { | 1512 | if ((res = half(&C, &C)) != MP_OKAY) { | |
1513 | goto LBL_ERR; | 1513 | goto LBL_ERR; | |
1514 | } | 1514 | } | |
1515 | if ((res = half(&D, &D)) != MP_OKAY) { | 1515 | if ((res = half(&D, &D)) != MP_OKAY) { | |
@@ -1537,27 +1537,27 @@ top: | @@ -1537,27 +1537,27 @@ top: | |||
1537 | goto LBL_ERR; | 1537 | goto LBL_ERR; | |
1538 | } | 1538 | } | |
1539 | 1539 | |||
1540 | if ((res = signed_subtract(&C, &A, &C)) != MP_OKAY) { | 1540 | if ((res = signed_subtract(&C, &A, &C)) != MP_OKAY) { | |
1541 | goto LBL_ERR; | 1541 | goto LBL_ERR; | |
1542 | } | 1542 | } | |
1543 | 1543 | |||
1544 | if ((res = signed_subtract(&D, &B, &D)) != MP_OKAY) { | 1544 | if ((res = signed_subtract(&D, &B, &D)) != MP_OKAY) { | |
1545 | goto LBL_ERR; | 1545 | goto LBL_ERR; | |
1546 | } | 1546 | } | |
1547 | } | 1547 | } | |
1548 | 1548 | |||
1549 | /* if not zero goto step 4 */ | 1549 | /* if not zero goto step 4 */ | |
1550 | if (BN_is_zero(&u) == 0) { | 1550 | if (PGPV_BN_is_zero(&u) == 0) { | |
1551 | goto top; | 1551 | goto top; | |
1552 | } | 1552 | } | |
1553 | /* now a = C, b = D, gcd == g*v */ | 1553 | /* now a = C, b = D, gcd == g*v */ | |
1554 | 1554 | |||
1555 | /* if v != 1 then there is no inverse */ | 1555 | /* if v != 1 then there is no inverse */ | |
1556 | if (compare_digit(&v, 1) != MP_EQ) { | 1556 | if (compare_digit(&v, 1) != MP_EQ) { | |
1557 | res = MP_VAL; | 1557 | res = MP_VAL; | |
1558 | goto LBL_ERR; | 1558 | goto LBL_ERR; | |
1559 | } | 1559 | } | |
1560 | 1560 | |||
1561 | /* if its too low */ | 1561 | /* if its too low */ | |
1562 | while (compare_digit(&C, 0) == MP_LT) { | 1562 | while (compare_digit(&C, 0) == MP_LT) { | |
1563 | if ((res = signed_add(&C, b, &C)) != MP_OKAY) { | 1563 | if ((res = signed_add(&C, b, &C)) != MP_OKAY) { | |
@@ -1579,27 +1579,27 @@ LBL_ERR: | @@ -1579,27 +1579,27 @@ LBL_ERR: | |||
1579 | mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL); | 1579 | mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL); | |
1580 | return res; | 1580 | return res; | |
1581 | } | 1581 | } | |
1582 | 1582 | |||
1583 | static int | 1583 | static int | |
1584 | modular_inverse(mp_int *c, mp_int *a, mp_int *b) | 1584 | modular_inverse(mp_int *c, mp_int *a, mp_int *b) | |
1585 | { | 1585 | { | |
1586 | /* b cannot be negative */ | 1586 | /* b cannot be negative */ | |
1587 | if (b->sign == MP_NEG || MP_ISZERO(b) == MP_YES) { | 1587 | if (b->sign == MP_NEG || MP_ISZERO(b) == MP_YES) { | |
1588 | return MP_VAL; | 1588 | return MP_VAL; | |
1589 | } | 1589 | } | |
1590 | 1590 | |||
1591 | /* if the modulus is odd we can use a faster routine instead */ | 1591 | /* if the modulus is odd we can use a faster routine instead */ | |
1592 | if (BN_is_odd(b) == 1) { | 1592 | if (PGPV_BN_is_odd(b) == 1) { | |
1593 | return fast_modular_inverse(a, b, c); | 1593 | return fast_modular_inverse(a, b, c); | |
1594 | } | 1594 | } | |
1595 | return slow_modular_inverse(a, b, c); | 1595 | return slow_modular_inverse(a, b, c); | |
1596 | } | 1596 | } | |
1597 | 1597 | |||
1598 | /* b = |a| | 1598 | /* b = |a| | |
1599 | * | 1599 | * | |
1600 | * Simple function copies the input and fixes the sign to positive | 1600 | * Simple function copies the input and fixes the sign to positive | |
1601 | */ | 1601 | */ | |
1602 | static int | 1602 | static int | |
1603 | absolute(mp_int * a, mp_int * b) | 1603 | absolute(mp_int * a, mp_int * b) | |
1604 | { | 1604 | { | |
1605 | int res; | 1605 | int res; | |
@@ -4346,27 +4346,27 @@ exponent_modulo(mp_int * G, mp_int * X, | @@ -4346,27 +4346,27 @@ exponent_modulo(mp_int * G, mp_int * X, | |||
4346 | if (mp_reduce_is_2k_l(P) == MP_YES) { | 4346 | if (mp_reduce_is_2k_l(P) == MP_YES) { | |
4347 | return basic_exponent_mod(G, X, P, Y, 1); | 4347 | return basic_exponent_mod(G, X, P, Y, 1); | |
4348 | } | 4348 | } | |
4349 | 4349 | |||
4350 | /* is it a DR modulus? */ | 4350 | /* is it a DR modulus? */ | |
4351 | diminished_radix = is_diminished_radix_modulus(P); | 4351 | diminished_radix = is_diminished_radix_modulus(P); | |
4352 | 4352 | |||
4353 | /* if not, is it a unrestricted DR modulus? */ | 4353 | /* if not, is it a unrestricted DR modulus? */ | |
4354 | if (!diminished_radix) { | 4354 | if (!diminished_radix) { | |
4355 | diminished_radix = mp_reduce_is_2k(P) << 1; | 4355 | diminished_radix = mp_reduce_is_2k(P) << 1; | |
4356 | } | 4356 | } | |
4357 | 4357 | |||
4358 | /* if the modulus is odd or diminished_radix, use the montgomery method */ | 4358 | /* if the modulus is odd or diminished_radix, use the montgomery method */ | |
4359 | if (BN_is_odd(P) == 1 || diminished_radix) { | 4359 | if (PGPV_BN_is_odd(P) == 1 || diminished_radix) { | |
4360 | return fast_exponent_modulo(G, X, P, Y, diminished_radix); | 4360 | return fast_exponent_modulo(G, X, P, Y, diminished_radix); | |
4361 | } | 4361 | } | |
4362 | /* otherwise use the generic Barrett reduction technique */ | 4362 | /* otherwise use the generic Barrett reduction technique */ | |
4363 | return basic_exponent_mod(G, X, P, Y, 0); | 4363 | return basic_exponent_mod(G, X, P, Y, 0); | |
4364 | } | 4364 | } | |
4365 | 4365 | |||
4366 | /* reverse an array, used for radix code */ | 4366 | /* reverse an array, used for radix code */ | |
4367 | static void | 4367 | static void | |
4368 | bn_reverse(unsigned char *s, int len) | 4368 | bn_reverse(unsigned char *s, int len) | |
4369 | { | 4369 | { | |
4370 | int ix, iy; | 4370 | int ix, iy; | |
4371 | uint8_t t; | 4371 | uint8_t t; | |
4372 | 4372 | |||
@@ -5014,27 +5014,27 @@ mp_toradix_n(mp_int * a, char *str, int | @@ -5014,27 +5014,27 @@ mp_toradix_n(mp_int * a, char *str, int | |||
5014 | /* reverse the digits of the string. In this case _s points | 5014 | /* reverse the digits of the string. In this case _s points | |
5015 | * to the first digit [exluding the sign] of the number | 5015 | * to the first digit [exluding the sign] of the number | |
5016 | */ | 5016 | */ | |
5017 | bn_reverse((unsigned char *)_s, digs); | 5017 | bn_reverse((unsigned char *)_s, digs); | |
5018 | 5018 | |||
5019 | /* append a NULL so the string is properly terminated */ | 5019 | /* append a NULL so the string is properly terminated */ | |
5020 | *str = '\0'; | 5020 | *str = '\0'; | |
5021 | 5021 | |||
5022 | mp_clear(&t); | 5022 | mp_clear(&t); | |
5023 | return MP_OKAY; | 5023 | return MP_OKAY; | |
5024 | } | 5024 | } | |
5025 | 5025 | |||
5026 | static char * | 5026 | static char * | |
5027 | formatbn(const BIGNUM *a, const int radix) | 5027 | formatbn(const PGPV_BIGNUM *a, const int radix) | |
5028 | { | 5028 | { | |
5029 | char *s; | 5029 | char *s; | |
5030 | int len; | 5030 | int len; | |
5031 | 5031 | |||
5032 | if (mp_radix_size(__UNCONST(a), radix, &len) != MP_OKAY) { | 5032 | if (mp_radix_size(__UNCONST(a), radix, &len) != MP_OKAY) { | |
5033 | return NULL; | 5033 | return NULL; | |
5034 | } | 5034 | } | |
5035 | if ((s = allocate(1, (size_t)len)) != NULL) { | 5035 | if ((s = allocate(1, (size_t)len)) != NULL) { | |
5036 | if (mp_toradix_n(__UNCONST(a), s, radix, len) != MP_OKAY) { | 5036 | if (mp_toradix_n(__UNCONST(a), s, radix, len) != MP_OKAY) { | |
5037 | deallocate(s, (size_t)len); | 5037 | deallocate(s, (size_t)len); | |
5038 | return NULL; | 5038 | return NULL; | |
5039 | } | 5039 | } | |
5040 | } | 5040 | } | |
@@ -5076,31 +5076,31 @@ mp_getradix_num(mp_int *a, int radix, ch | @@ -5076,31 +5076,31 @@ mp_getradix_num(mp_int *a, int radix, ch | |||
5076 | return err; | 5076 | return err; | |
5077 | } | 5077 | } | |
5078 | 5078 | |||
5079 | ch = *s++; | 5079 | ch = *s++; | |
5080 | } | 5080 | } | |
5081 | if (compare_digit(a, 0) != MP_EQ) { | 5081 | if (compare_digit(a, 0) != MP_EQ) { | |
5082 | a->sign = neg; | 5082 | a->sign = neg; | |
5083 | } | 5083 | } | |
5084 | 5084 | |||
5085 | return MP_OKAY; | 5085 | return MP_OKAY; | |
5086 | } | 5086 | } | |
5087 | 5087 | |||
5088 | static int | 5088 | static int | |
5089 | getbn(BIGNUM **a, const char *str, int radix) | 5089 | getbn(PGPV_BIGNUM **a, const char *str, int radix) | |
5090 | { | 5090 | { | |
5091 | int len; | 5091 | int len; | |
5092 | 5092 | |||
5093 | if (a == NULL || str == NULL || (*a = BN_new()) == NULL) { | 5093 | if (a == NULL || str == NULL || (*a = PGPV_BN_new()) == NULL) { | |
5094 | return 0; | 5094 | return 0; | |
5095 | } | 5095 | } | |
5096 | if (mp_getradix_num(*a, radix, __UNCONST(str)) != MP_OKAY) { | 5096 | if (mp_getradix_num(*a, radix, __UNCONST(str)) != MP_OKAY) { | |
5097 | return 0; | 5097 | return 0; | |
5098 | } | 5098 | } | |
5099 | mp_radix_size(__UNCONST(*a), radix, &len); | 5099 | mp_radix_size(__UNCONST(*a), radix, &len); | |
5100 | return len - 1; | 5100 | return len - 1; | |
5101 | } | 5101 | } | |
5102 | 5102 | |||
5103 | /* d = a - b (mod c) */ | 5103 | /* d = a - b (mod c) */ | |
5104 | static int | 5104 | static int | |
5105 | subtract_modulo(mp_int *a, mp_int *b, mp_int *c, mp_int *d) | 5105 | subtract_modulo(mp_int *a, mp_int *b, mp_int *c, mp_int *d) | |
5106 | { | 5106 | { | |
@@ -5113,434 +5113,434 @@ subtract_modulo(mp_int *a, mp_int *b, mp | @@ -5113,434 +5113,434 @@ subtract_modulo(mp_int *a, mp_int *b, mp | |||
5113 | } | 5113 | } | |
5114 | 5114 | |||
5115 | if ((res = signed_subtract(a, b, &t)) != MP_OKAY) { | 5115 | if ((res = signed_subtract(a, b, &t)) != MP_OKAY) { | |
5116 | mp_clear(&t); | 5116 | mp_clear(&t); | |
5117 | return res; | 5117 | return res; | |
5118 | } | 5118 | } | |
5119 | res = modulo(&t, c, d); | 5119 | res = modulo(&t, c, d); | |
5120 | mp_clear(&t); | 5120 | mp_clear(&t); | |
5121 | return res; | 5121 | return res; | |
5122 | } | 5122 | } | |
5123 | 5123 | |||
5124 | /**************************************************************************/ | 5124 | /**************************************************************************/ | |
5125 | 5125 | |||
5126 | /* BIGNUM emulation layer */ | 5126 | /* PGPV_BIGNUM emulation layer */ | |
5127 | 5127 | |||
5128 | /* essentiually, these are just wrappers around the libtommath functions */ | 5128 | /* essentiually, these are just wrappers around the libtommath functions */ | |
5129 | /* usually the order of args changes */ | 5129 | /* usually the order of args changes */ | |
5130 | /* the BIGNUM API tends to have more const poisoning */ | 5130 | /* the PGPV_BIGNUM API tends to have more const poisoning */ | |
5131 | /* these wrappers also check the arguments passed for sanity */ | 5131 | /* these wrappers also check the arguments passed for sanity */ | |
5132 | 5132 | |||
5133 | BIGNUM * | 5133 | PGPV_BIGNUM * | |
5134 | BN_bin2bn(const uint8_t *data, int len, BIGNUM *ret) | 5134 | PGPV_BN_bin2bn(const uint8_t *data, int len, PGPV_BIGNUM *ret) | |
5135 | { | 5135 | { | |
5136 | if (data == NULL) { | 5136 | if (data == NULL) { | |
5137 | return BN_new(); | 5137 | return PGPV_BN_new(); | |
5138 | } | 5138 | } | |
5139 | if (ret == NULL) { | 5139 | if (ret == NULL) { | |
5140 | ret = BN_new(); | 5140 | ret = PGPV_BN_new(); | |
5141 | } | 5141 | } | |
5142 | return (mp_read_unsigned_bin(ret, data, len) == MP_OKAY) ? ret : NULL; | 5142 | return (mp_read_unsigned_bin(ret, data, len) == MP_OKAY) ? ret : NULL; | |
5143 | } | 5143 | } | |
5144 | 5144 | |||
5145 | /* store in unsigned [big endian] format */ | 5145 | /* store in unsigned [big endian] format */ | |
5146 | int | 5146 | int | |
5147 | BN_bn2bin(const BIGNUM *a, unsigned char *b) | 5147 | PGPV_BN_bn2bin(const PGPV_BIGNUM *a, unsigned char *b) | |
5148 | { | 5148 | { | |
5149 | BIGNUM t; | 5149 | PGPV_BIGNUM t; | |
5150 | int x; | 5150 | int x; | |
5151 | 5151 | |||
5152 | if (a == NULL || b == NULL) { | 5152 | if (a == NULL || b == NULL) { | |
5153 | return -1; | 5153 | return -1; | |
5154 | } | 5154 | } | |
5155 | if (mp_init_copy (&t, __UNCONST(a)) != MP_OKAY) { | 5155 | if (mp_init_copy (&t, __UNCONST(a)) != MP_OKAY) { | |
5156 | return -1; | 5156 | return -1; | |
5157 | } | 5157 | } | |
5158 | for (x = 0; !BN_is_zero(&t) ; ) { | 5158 | for (x = 0; !PGPV_BN_is_zero(&t) ; ) { | |
5159 | b[x++] = (unsigned char) (t.dp[0] & 0xff); | 5159 | b[x++] = (unsigned char) (t.dp[0] & 0xff); | |
5160 | if (rshift_bits(&t, 8, &t, NULL) != MP_OKAY) { | 5160 | if (rshift_bits(&t, 8, &t, NULL) != MP_OKAY) { | |
5161 | mp_clear(&t); | 5161 | mp_clear(&t); | |
5162 | return -1; | 5162 | return -1; | |
5163 | } | 5163 | } | |
5164 | } | 5164 | } | |
5165 | bn_reverse(b, x); | 5165 | bn_reverse(b, x); | |
5166 | mp_clear(&t); | 5166 | mp_clear(&t); | |
5167 | return x; | 5167 | return x; | |
5168 | } | 5168 | } | |
5169 | 5169 | |||
5170 | void | 5170 | void | |
5171 | BN_init(BIGNUM *a) | 5171 | PGPV_BN_init(PGPV_BIGNUM *a) | |
5172 | { | 5172 | { | |
5173 | if (a != NULL) { | 5173 | if (a != NULL) { | |
5174 | mp_init(a); | 5174 | mp_init(a); | |
5175 | } | 5175 | } | |
5176 | } | 5176 | } | |
5177 | 5177 | |||
5178 | BIGNUM * | 5178 | PGPV_BIGNUM * | |
5179 | BN_new(void) | 5179 | PGPV_BN_new(void) | |
5180 | { | 5180 | { | |
5181 | BIGNUM *a; | 5181 | PGPV_BIGNUM *a; | |
5182 | 5182 | |||
5183 | if ((a = allocate(1, sizeof(*a))) != NULL) { | 5183 | if ((a = allocate(1, sizeof(*a))) != NULL) { | |
5184 | mp_init(a); | 5184 | mp_init(a); | |
5185 | } | 5185 | } | |
5186 | return a; | 5186 | return a; | |
5187 | } | 5187 | } | |
5188 | 5188 | |||
5189 | /* copy, b = a */ | 5189 | /* copy, b = a */ | |
5190 | int | 5190 | int | |
5191 | BN_copy(BIGNUM *b, const BIGNUM *a) | 5191 | PGPV_BN_copy(PGPV_BIGNUM *b, const PGPV_BIGNUM *a) | |
5192 | { | 5192 | { | |
5193 | if (a == NULL || b == NULL) { | 5193 | if (a == NULL || b == NULL) { | |
5194 | return MP_VAL; | 5194 | return MP_VAL; | |
5195 | } | 5195 | } | |
5196 | return mp_copy(__UNCONST(a), b); | 5196 | return mp_copy(__UNCONST(a), b); | |
5197 | } | 5197 | } | |
5198 | 5198 | |||
5199 | BIGNUM * | 5199 | PGPV_BIGNUM * | |
5200 | BN_dup(const BIGNUM *a) | 5200 | PGPV_BN_dup(const PGPV_BIGNUM *a) | |
5201 | { | 5201 | { | |
5202 | BIGNUM *ret; | 5202 | PGPV_BIGNUM *ret; | |
5203 | 5203 | |||
5204 | if (a == NULL) { | 5204 | if (a == NULL) { | |
5205 | return NULL; | 5205 | return NULL; | |
5206 | } | 5206 | } | |
5207 | if ((ret = BN_new()) != NULL) { | 5207 | if ((ret = PGPV_BN_new()) != NULL) { | |
5208 | BN_copy(ret, a); | 5208 | PGPV_BN_copy(ret, a); | |
5209 | } | 5209 | } | |
5210 | return ret; | 5210 | return ret; | |
5211 | } | 5211 | } | |
5212 | 5212 | |||
5213 | void | 5213 | void | |
5214 | BN_swap(BIGNUM *a, BIGNUM *b) | 5214 | PGPV_BN_swap(PGPV_BIGNUM *a, PGPV_BIGNUM *b) | |
5215 | { | 5215 | { | |
5216 | if (a && b) { | 5216 | if (a && b) { | |
5217 | mp_exch(a, b); | 5217 | mp_exch(a, b); | |
5218 | } | 5218 | } | |
5219 | } | 5219 | } | |
5220 | 5220 | |||
5221 | int | 5221 | int | |
5222 | BN_lshift(BIGNUM *r, const BIGNUM *a, int n) | 5222 | PGPV_BN_lshift(PGPV_BIGNUM *r, const PGPV_BIGNUM *a, int n) | |
5223 | { | 5223 | { | |
5224 | if (r == NULL || a == NULL || n < 0) { | 5224 | if (r == NULL || a == NULL || n < 0) { | |
5225 | return 0; | 5225 | return 0; | |
5226 | } | 5226 | } | |
5227 | BN_copy(r, a); | 5227 | PGPV_BN_copy(r, a); | |
5228 | return lshift_digits(r, n) == MP_OKAY; | 5228 | return lshift_digits(r, n) == MP_OKAY; | |
5229 | } | 5229 | } | |
5230 | 5230 | |||
5231 | int | 5231 | int | |
5232 | BN_lshift1(BIGNUM *r, BIGNUM *a) | 5232 | PGPV_BN_lshift1(PGPV_BIGNUM *r, PGPV_BIGNUM *a) | |
5233 | { | 5233 | { | |
5234 | if (r == NULL || a == NULL) { | 5234 | if (r == NULL || a == NULL) { | |
5235 | return 0; | 5235 | return 0; | |
5236 | } | 5236 | } | |
5237 | BN_copy(r, a); | 5237 | PGPV_BN_copy(r, a); | |
5238 | return lshift_digits(r, 1) == MP_OKAY; | 5238 | return lshift_digits(r, 1) == MP_OKAY; | |
5239 | } | 5239 | } | |
5240 | 5240 | |||
5241 | int | 5241 | int | |
5242 | BN_rshift(BIGNUM *r, const BIGNUM *a, int n) | 5242 | PGPV_BN_rshift(PGPV_BIGNUM *r, const PGPV_BIGNUM *a, int n) | |
5243 | { | 5243 | { | |
5244 | if (r == NULL || a == NULL || n < 0) { | 5244 | if (r == NULL || a == NULL || n < 0) { | |
5245 | return MP_VAL; | 5245 | return MP_VAL; | |
5246 | } | 5246 | } | |
5247 | BN_copy(r, a); | 5247 | PGPV_BN_copy(r, a); | |
5248 | return rshift_digits(r, n) == MP_OKAY; | 5248 | return rshift_digits(r, n) == MP_OKAY; | |
5249 | } | 5249 | } | |
5250 | 5250 | |||
5251 | int | 5251 | int | |
5252 | BN_rshift1(BIGNUM *r, BIGNUM *a) | 5252 | PGPV_BN_rshift1(PGPV_BIGNUM *r, PGPV_BIGNUM *a) | |
5253 | { | 5253 | { | |
5254 | if (r == NULL || a == NULL) { | 5254 | if (r == NULL || a == NULL) { | |
5255 | return 0; | 5255 | return 0; | |
5256 | } | 5256 | } | |
5257 | BN_copy(r, a); | 5257 | PGPV_BN_copy(r, a); | |
5258 | return rshift_digits(r, 1) == MP_OKAY; | 5258 | return rshift_digits(r, 1) == MP_OKAY; | |
5259 | } | 5259 | } | |
5260 | 5260 | |||
5261 | int | 5261 | int | |
5262 | BN_set_word(BIGNUM *a, BN_ULONG w) | 5262 | PGPV_BN_set_word(PGPV_BIGNUM *a, PGPV_BN_ULONG w) | |
5263 | { | 5263 | { | |
5264 | if (a == NULL) { | 5264 | if (a == NULL) { | |
5265 | return 0; | 5265 | return 0; | |
5266 | } | 5266 | } | |
5267 | set_word(a, w); | 5267 | set_word(a, w); | |
5268 | return 1; | 5268 | return 1; | |
5269 | } | 5269 | } | |
5270 | 5270 | |||
5271 | int | 5271 | int | |
5272 | BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) | 5272 | PGPV_BN_add(PGPV_BIGNUM *r, const PGPV_BIGNUM *a, const PGPV_BIGNUM *b) | |
5273 | { | 5273 | { | |
5274 | if (a == NULL || b == NULL || r == NULL) { | 5274 | if (a == NULL || b == NULL || r == NULL) { | |
5275 | return 0; | 5275 | return 0; | |
5276 | } | 5276 | } | |
5277 | return signed_add(__UNCONST(a), __UNCONST(b), r) == MP_OKAY; | 5277 | return signed_add(__UNCONST(a), __UNCONST(b), r) == MP_OKAY; | |
5278 | } | 5278 | } | |
5279 | 5279 | |||
5280 | int | 5280 | int | |
5281 | BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) | 5281 | PGPV_BN_sub(PGPV_BIGNUM *r, const PGPV_BIGNUM *a, const PGPV_BIGNUM *b) | |
5282 | { | 5282 | { | |
5283 | if (a == NULL || b == NULL || r == NULL) { | 5283 | if (a == NULL || b == NULL || r == NULL) { | |
5284 | return 0; | 5284 | return 0; | |
5285 | } | 5285 | } | |
5286 | return signed_subtract(__UNCONST(a), __UNCONST(b), r) == MP_OKAY; | 5286 | return signed_subtract(__UNCONST(a), __UNCONST(b), r) == MP_OKAY; | |
5287 | } | 5287 | } | |
5288 | 5288 | |||
5289 | int | 5289 | int | |
5290 | BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) | 5290 | PGPV_BN_mul(PGPV_BIGNUM *r, const PGPV_BIGNUM *a, const PGPV_BIGNUM *b, PGPV_BN_CTX *ctx) | |
5291 | { | 5291 | { | |
5292 | if (a == NULL || b == NULL || r == NULL) { | 5292 | if (a == NULL || b == NULL || r == NULL) { | |
5293 | return 0; | 5293 | return 0; | |
5294 | } | 5294 | } | |
5295 | USE_ARG(ctx); | 5295 | USE_ARG(ctx); | |
5296 | return signed_multiply(__UNCONST(a), __UNCONST(b), r) == MP_OKAY; | 5296 | return signed_multiply(__UNCONST(a), __UNCONST(b), r) == MP_OKAY; | |
5297 | } | 5297 | } | |
5298 | 5298 | |||
5299 | int | 5299 | int | |
5300 | BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d, BN_CTX *ctx) | 5300 | PGPV_BN_div(PGPV_BIGNUM *dv, PGPV_BIGNUM *rem, const PGPV_BIGNUM *a, const PGPV_BIGNUM *d, PGPV_BN_CTX *ctx) | |
5301 | { | 5301 | { | |
5302 | if ((dv == NULL && rem == NULL) || a == NULL || d == NULL) { | 5302 | if ((dv == NULL && rem == NULL) || a == NULL || d == NULL) { | |
5303 | return 0; | 5303 | return 0; | |
5304 | } | 5304 | } | |
5305 | USE_ARG(ctx); | 5305 | USE_ARG(ctx); | |
5306 | return signed_divide(dv, rem, __UNCONST(a), __UNCONST(d)) == MP_OKAY; | 5306 | return signed_divide(dv, rem, __UNCONST(a), __UNCONST(d)) == MP_OKAY; | |
5307 | } | 5307 | } | |
5308 | 5308 | |||
5309 | /* perform a bit operation on the 2 bignums */ | 5309 | /* perform a bit operation on the 2 bignums */ | |
5310 | int | 5310 | int | |
5311 | BN_bitop(BIGNUM *r, const BIGNUM *a, char op, const BIGNUM *b) | 5311 | PGPV_BN_bitop(PGPV_BIGNUM *r, const PGPV_BIGNUM *a, char op, const PGPV_BIGNUM *b) | |
5312 | { | 5312 | { | |
5313 | unsigned ndigits; | 5313 | unsigned ndigits; | |
5314 | mp_digit ad; | 5314 | mp_digit ad; | |
5315 | mp_digit bd; | 5315 | mp_digit bd; | |
5316 | int i; | 5316 | int i; | |
5317 | 5317 | |||
5318 | if (a == NULL || b == NULL || r == NULL) { | 5318 | if (a == NULL || b == NULL || r == NULL) { | |
5319 | return 0; | 5319 | return 0; | |
5320 | } | 5320 | } | |
5321 | if (BN_cmp(__UNCONST(a), __UNCONST(b)) >= 0) { | 5321 | if (PGPV_BN_cmp(__UNCONST(a), __UNCONST(b)) >= 0) { | |
5322 | BN_copy(r, a); | 5322 | PGPV_BN_copy(r, a); | |
5323 | ndigits = a->used; | 5323 | ndigits = a->used; | |
5324 | } else { | 5324 | } else { | |
5325 | BN_copy(r, b); | 5325 | PGPV_BN_copy(r, b); | |
5326 | ndigits = b->used; | 5326 | ndigits = b->used; | |
5327 | } | 5327 | } | |
5328 | for (i = 0 ; i < (int)ndigits ; i++) { | 5328 | for (i = 0 ; i < (int)ndigits ; i++) { | |
5329 | ad = (i > a->used) ? 0 : a->dp[i]; | 5329 | ad = (i > a->used) ? 0 : a->dp[i]; | |
5330 | bd = (i > b->used) ? 0 : b->dp[i]; | 5330 | bd = (i > b->used) ? 0 : b->dp[i]; | |
5331 | switch(op) { | 5331 | switch(op) { | |
5332 | case '&': | 5332 | case '&': | |
5333 | r->dp[i] = (ad & bd); | 5333 | r->dp[i] = (ad & bd); | |
5334 | break; | 5334 | break; | |
5335 | case '|': | 5335 | case '|': | |
5336 | r->dp[i] = (ad | bd); | 5336 | r->dp[i] = (ad | bd); | |
5337 | break; | 5337 | break; | |
5338 | case '^': | 5338 | case '^': | |
5339 | r->dp[i] = (ad ^ bd); | 5339 | r->dp[i] = (ad ^ bd); | |
5340 | break; | 5340 | break; | |
5341 | default: | 5341 | default: | |
5342 | break; | 5342 | break; | |
5343 | } | 5343 | } | |
5344 | } | 5344 | } | |
5345 | return 1; | 5345 | return 1; | |
5346 | } | 5346 | } | |
5347 | 5347 | |||
5348 | void | 5348 | void | |
5349 | BN_free(BIGNUM *a) | 5349 | PGPV_BN_free(PGPV_BIGNUM *a) | |
5350 | { | 5350 | { | |
5351 | if (a) { | 5351 | if (a) { | |
5352 | mp_clear(a); | 5352 | mp_clear(a); | |
5353 | } | 5353 | } | |
5354 | } | 5354 | } | |
5355 | 5355 | |||
5356 | void | 5356 | void | |
5357 | BN_clear(BIGNUM *a) | 5357 | PGPV_BN_clear(PGPV_BIGNUM *a) | |
5358 | { | 5358 | { | |
5359 | if (a) { | 5359 | if (a) { | |
5360 | mp_clear(a); | 5360 | mp_clear(a); | |
5361 | } | 5361 | } | |
5362 | } | 5362 | } | |
5363 | 5363 | |||
5364 | void | 5364 | void | |
5365 | BN_clear_free(BIGNUM *a) | 5365 | PGPV_BN_clear_free(PGPV_BIGNUM *a) | |
5366 | { | 5366 | { | |
5367 | if (a) { | 5367 | if (a) { | |
5368 | mp_clear(a); | 5368 | mp_clear(a); | |
5369 | } | 5369 | } | |
5370 | } | 5370 | } | |
5371 | 5371 | |||
5372 | int | 5372 | int | |
5373 | BN_num_bytes(const BIGNUM *a) | 5373 | PGPV_BN_num_bytes(const PGPV_BIGNUM *a) | |
5374 | { | 5374 | { | |
5375 | if (a == NULL) { | 5375 | if (a == NULL) { | |
5376 | return MP_VAL; | 5376 | return MP_VAL; | |
5377 | } | 5377 | } | |
5378 | return mp_unsigned_bin_size(__UNCONST(a)); | 5378 | return mp_unsigned_bin_size(__UNCONST(a)); | |
5379 | } | 5379 | } | |
5380 | 5380 | |||
5381 | int | 5381 | int | |
5382 | BN_num_bits(const BIGNUM *a) | 5382 | PGPV_BN_num_bits(const PGPV_BIGNUM *a) | |
5383 | { | 5383 | { | |
5384 | if (a == NULL) { | 5384 | if (a == NULL) { | |
5385 | return 0; | 5385 | return 0; | |
5386 | } | 5386 | } | |
5387 | return mp_count_bits(a); | 5387 | return mp_count_bits(a); | |
5388 | } | 5388 | } | |
5389 | 5389 | |||
5390 | void | 5390 | void | |
5391 | BN_set_negative(BIGNUM *a, int n) | 5391 | PGPV_BN_set_negative(PGPV_BIGNUM *a, int n) | |
5392 | { | 5392 | { | |
5393 | if (a) { | 5393 | if (a) { | |
5394 | a->sign = (n) ? MP_NEG : 0; | 5394 | a->sign = (n) ? MP_NEG : 0; | |
5395 | } | 5395 | } | |
5396 | } | 5396 | } | |
5397 | 5397 | |||
5398 | int | 5398 | int | |
5399 | BN_cmp(BIGNUM *a, BIGNUM *b) | 5399 | PGPV_BN_cmp(PGPV_BIGNUM *a, PGPV_BIGNUM *b) | |
5400 | { | 5400 | { | |
5401 | if (a == NULL || b == NULL) { | 5401 | if (a == NULL || b == NULL) { | |
5402 | return MP_VAL; | 5402 | return MP_VAL; | |
5403 | } | 5403 | } | |
5404 | switch(signed_compare(a, b)) { | 5404 | switch(signed_compare(a, b)) { | |
5405 | case MP_LT: | 5405 | case MP_LT: | |
5406 | return -1; | 5406 | return -1; | |
5407 | case MP_GT: | 5407 | case MP_GT: | |
5408 | return 1; | 5408 | return 1; | |
5409 | case MP_EQ: | 5409 | case MP_EQ: | |
5410 | default: | 5410 | default: | |
5411 | return 0; | 5411 | return 0; | |
5412 | } | 5412 | } | |
5413 | } | 5413 | } | |
5414 | 5414 | |||
5415 | int | 5415 | int | |
5416 | BN_mod_exp(BIGNUM *Y, BIGNUM *G, BIGNUM *X, BIGNUM *P, BN_CTX *ctx) | 5416 | PGPV_BN_mod_exp(PGPV_BIGNUM *Y, PGPV_BIGNUM *G, PGPV_BIGNUM *X, PGPV_BIGNUM *P, PGPV_BN_CTX *ctx) | |
5417 | { | 5417 | { | |
5418 | if (Y == NULL || G == NULL || X == NULL || P == NULL) { | 5418 | if (Y == NULL || G == NULL || X == NULL || P == NULL) { | |
5419 | return MP_VAL; | 5419 | return MP_VAL; | |
5420 | } | 5420 | } | |
5421 | USE_ARG(ctx); | 5421 | USE_ARG(ctx); | |
5422 | return exponent_modulo(G, X, P, Y) == MP_OKAY; | 5422 | return exponent_modulo(G, X, P, Y) == MP_OKAY; | |
5423 | } | 5423 | } | |
5424 | 5424 | |||
5425 | BIGNUM * | 5425 | PGPV_BIGNUM * | |
5426 | BN_mod_inverse(BIGNUM *r, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) | 5426 | PGPV_BN_mod_inverse(PGPV_BIGNUM *r, PGPV_BIGNUM *a, const PGPV_BIGNUM *n, PGPV_BN_CTX *ctx) | |
5427 | { | 5427 | { | |
5428 | USE_ARG(ctx); | 5428 | USE_ARG(ctx); | |
5429 | if (r == NULL || a == NULL || n == NULL) { | 5429 | if (r == NULL || a == NULL || n == NULL) { | |
5430 | return NULL; | 5430 | return NULL; | |
5431 | } | 5431 | } | |
5432 | return (modular_inverse(r, a, __UNCONST(n)) == MP_OKAY) ? r : NULL; | 5432 | return (modular_inverse(r, a, __UNCONST(n)) == MP_OKAY) ? r : NULL; | |
5433 | } | 5433 | } | |
5434 | 5434 | |||
5435 | int | 5435 | int | |
5436 | BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx) | 5436 | PGPV_BN_mod_mul(PGPV_BIGNUM *ret, PGPV_BIGNUM *a, PGPV_BIGNUM *b, const PGPV_BIGNUM *m, PGPV_BN_CTX *ctx) | |
5437 | { | 5437 | { | |
5438 | USE_ARG(ctx); | 5438 | USE_ARG(ctx); | |
5439 | if (ret == NULL || a == NULL || b == NULL || m == NULL) { | 5439 | if (ret == NULL || a == NULL || b == NULL || m == NULL) { | |
5440 | return 0; | 5440 | return 0; | |
5441 | } | 5441 | } | |
5442 | return multiply_modulo(ret, a, b, __UNCONST(m)) == MP_OKAY; | 5442 | return multiply_modulo(ret, a, b, __UNCONST(m)) == MP_OKAY; | |
5443 | } | 5443 | } | |
5444 | 5444 | |||
5445 | BN_CTX * | 5445 | PGPV_BN_CTX * | |
5446 | BN_CTX_new(void) | 5446 | PGPV_BN_CTX_new(void) | |
5447 | { | 5447 | { | |
5448 | return allocate(1, sizeof(BN_CTX)); | 5448 | return allocate(1, sizeof(PGPV_BN_CTX)); | |
5449 | } | 5449 | } | |
5450 | 5450 | |||
5451 | void | 5451 | void | |
5452 | BN_CTX_init(BN_CTX *c) | 5452 | PGPV_BN_CTX_init(PGPV_BN_CTX *c) | |
5453 | { | 5453 | { | |
5454 | if (c != NULL) { | 5454 | if (c != NULL) { | |
5455 | c->arraysize = 15; | 5455 | c->arraysize = 15; | |
5456 | if ((c->v = allocate(sizeof(*c->v), c->arraysize)) == NULL) { | 5456 | if ((c->v = allocate(sizeof(*c->v), c->arraysize)) == NULL) { | |
5457 | c->arraysize = 0; | 5457 | c->arraysize = 0; | |
5458 | } | 5458 | } | |
5459 | } | 5459 | } | |
5460 | } | 5460 | } | |
5461 | 5461 | |||
5462 | BIGNUM * | 5462 | PGPV_BIGNUM * | |
5463 | BN_CTX_get(BN_CTX *ctx) | 5463 | PGPV_BN_CTX_get(PGPV_BN_CTX *ctx) | |
5464 | { | 5464 | { | |
5465 | if (ctx == NULL || ctx->v == NULL || ctx->arraysize == 0 || ctx->count == ctx->arraysize - 1) { | 5465 | if (ctx == NULL || ctx->v == NULL || ctx->arraysize == 0 || ctx->count == ctx->arraysize - 1) { | |
5466 | return NULL; | 5466 | return NULL; | |
5467 | } | 5467 | } | |
5468 | return ctx->v[ctx->count++] = BN_new(); | 5468 | return ctx->v[ctx->count++] = PGPV_BN_new(); | |
5469 | } | 5469 | } | |
5470 | 5470 | |||
5471 | void | 5471 | void | |
5472 | BN_CTX_start(BN_CTX *ctx) | 5472 | PGPV_BN_CTX_start(PGPV_BN_CTX *ctx) | |
5473 | { | 5473 | { | |
5474 | BN_CTX_init(ctx); | 5474 | PGPV_BN_CTX_init(ctx); | |
5475 | } | 5475 | } | |
5476 | 5476 | |||
5477 | void | 5477 | void | |
5478 | BN_CTX_free(BN_CTX *c) | 5478 | PGPV_BN_CTX_free(PGPV_BN_CTX *c) | |
5479 | { | 5479 | { | |
5480 | unsigned i; | 5480 | unsigned i; | |
5481 | 5481 | |||
5482 | if (c != NULL && c->v != NULL) { | 5482 | if (c != NULL && c->v != NULL) { | |
5483 | for (i = 0 ; i < c->count ; i++) { | 5483 | for (i = 0 ; i < c->count ; i++) { | |
5484 | BN_clear_free(c->v[i]); | 5484 | PGPV_BN_clear_free(c->v[i]); | |
5485 | } | 5485 | } | |
5486 | deallocate(c->v, sizeof(*c->v) * c->arraysize); | 5486 | deallocate(c->v, sizeof(*c->v) * c->arraysize); | |
5487 | } | 5487 | } | |
5488 | } | 5488 | } | |
5489 | 5489 | |||
5490 | void | 5490 | void | |
5491 | BN_CTX_end(BN_CTX *ctx) | 5491 | PGPV_BN_CTX_end(PGPV_BN_CTX *ctx) | |
5492 | { | 5492 | { | |
5493 | BN_CTX_free(ctx); | 5493 | PGPV_BN_CTX_free(ctx); | |
5494 | } | 5494 | } | |
5495 | 5495 | |||
5496 | char * | 5496 | char * | |
5497 | BN_bn2hex(const BIGNUM *a) | 5497 | PGPV_BN_bn2hex(const PGPV_BIGNUM *a) | |
5498 | { | 5498 | { | |
5499 | return (a == NULL) ? NULL : formatbn(a, 16); | 5499 | return (a == NULL) ? NULL : formatbn(a, 16); | |
5500 | } | 5500 | } | |
5501 | 5501 | |||
5502 | char * | 5502 | char * | |
5503 | BN_bn2dec(const BIGNUM *a) | 5503 | PGPV_BN_bn2dec(const PGPV_BIGNUM *a) | |
5504 | { | 5504 | { | |
5505 | return (a == NULL) ? NULL : formatbn(a, 10); | 5505 | return (a == NULL) ? NULL : formatbn(a, 10); | |
5506 | } | 5506 | } | |
5507 | 5507 | |||
5508 | char * | 5508 | char * | |
5509 | BN_bn2radix(const BIGNUM *a, unsigned radix) | 5509 | PGPV_BN_bn2radix(const PGPV_BIGNUM *a, unsigned radix) | |
5510 | { | 5510 | { | |
5511 | return (a == NULL) ? NULL : formatbn(a, (int)radix); | 5511 | return (a == NULL) ? NULL : formatbn(a, (int)radix); | |
5512 | } | 5512 | } | |
5513 | 5513 | |||
5514 | #ifndef _KERNEL | 5514 | #ifndef _KERNEL | |
5515 | int | 5515 | int | |
5516 | BN_print_fp(FILE *fp, const BIGNUM *a) | 5516 | PGPV_BN_print_fp(FILE *fp, const PGPV_BIGNUM *a) | |
5517 | { | 5517 | { | |
5518 | char *s; | 5518 | char *s; | |
5519 | int ret; | 5519 | int ret; | |
5520 | 5520 | |||
5521 | if (fp == NULL || a == NULL) { | 5521 | if (fp == NULL || a == NULL) { | |
5522 | return 0; | 5522 | return 0; | |
5523 | } | 5523 | } | |
5524 | s = BN_bn2hex(a); | 5524 | s = PGPV_BN_bn2hex(a); | |
5525 | ret = fprintf(fp, "%s", s); | 5525 | ret = fprintf(fp, "%s", s); | |
5526 | deallocate(s, strlen(s) + 1); | 5526 | deallocate(s, strlen(s) + 1); | |
5527 | return ret; | 5527 | return ret; | |
5528 | } | 5528 | } | |
5529 | #endif | 5529 | #endif | |
5530 | 5530 | |||
5531 | #ifdef BN_RAND_NEEDED | 5531 | #ifdef PGPV_BN_RAND_NEEDED | |
5532 | int | 5532 | int | |
5533 | BN_rand(BIGNUM *rnd, int bits, int top, int bottom) | 5533 | PGPV_BN_rand(PGPV_BIGNUM *rnd, int bits, int top, int bottom) | |
5534 | { | 5534 | { | |
5535 | uint64_t r; | 5535 | uint64_t r; | |
5536 | int digits; | 5536 | int digits; | |
5537 | int i; | 5537 | int i; | |
5538 | 5538 | |||
5539 | if (rnd == NULL) { | 5539 | if (rnd == NULL) { | |
5540 | return 0; | 5540 | return 0; | |
5541 | } | 5541 | } | |
5542 | mp_init_size(rnd, digits = howmany(bits, DIGIT_BIT)); | 5542 | mp_init_size(rnd, digits = howmany(bits, DIGIT_BIT)); | |
5543 | for (i = 0 ; i < digits ; i++) { | 5543 | for (i = 0 ; i < digits ; i++) { | |
5544 | r = (uint64_t)arc4random(); | 5544 | r = (uint64_t)arc4random(); | |
5545 | r <<= 32; | 5545 | r <<= 32; | |
5546 | r |= arc4random(); | 5546 | r |= arc4random(); | |
@@ -5550,134 +5550,134 @@ BN_rand(BIGNUM *rnd, int bits, int top, | @@ -5550,134 +5550,134 @@ BN_rand(BIGNUM *rnd, int bits, int top, | |||
5550 | rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)DIGIT_BIT)); | 5550 | rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)DIGIT_BIT)); | |
5551 | } | 5551 | } | |
5552 | if (top == 1) { | 5552 | if (top == 1) { | |
5553 | rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)DIGIT_BIT)); | 5553 | rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)DIGIT_BIT)); | |
5554 | rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)(DIGIT_BIT - 1))); | 5554 | rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)(DIGIT_BIT - 1))); | |
5555 | } | 5555 | } | |
5556 | if (bottom) { | 5556 | if (bottom) { | |
5557 | rnd->dp[0] |= 0x1; | 5557 | rnd->dp[0] |= 0x1; | |
5558 | } | 5558 | } | |
5559 | return 1; | 5559 | return 1; | |
5560 | } | 5560 | } | |
5561 | 5561 | |||
5562 | int | 5562 | int | |
5563 | BN_rand_range(BIGNUM *rnd, BIGNUM *range) | 5563 | PGPV_BN_rand_range(PGPV_BIGNUM *rnd, PGPV_BIGNUM *range) | |
5564 | { | 5564 | { | |
5565 | if (rnd == NULL || range == NULL || BN_is_zero(range)) { | 5565 | if (rnd == NULL || range == NULL || PGPV_BN_is_zero(range)) { | |
5566 | return 0; | 5566 | return 0; | |
5567 | } | 5567 | } | |
5568 | BN_rand(rnd, BN_num_bits(range), 1, 0); | 5568 | PGPV_BN_rand(rnd, PGPV_BN_num_bits(range), 1, 0); | |
5569 | return modulo(rnd, range, rnd) == MP_OKAY; | 5569 | return modulo(rnd, range, rnd) == MP_OKAY; | |
5570 | } | 5570 | } | |
5571 | #endif | 5571 | #endif | |
5572 | 5572 | |||
5573 | int | 5573 | int | |
5574 | BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg) | 5574 | PGPV_BN_is_prime(const PGPV_BIGNUM *a, int checks, void (*callback)(int, int, void *), PGPV_BN_CTX *ctx, void *cb_arg) | |
5575 | { | 5575 | { | |
5576 | int primality; | 5576 | int primality; | |
5577 | 5577 | |||
5578 | if (a == NULL) { | 5578 | if (a == NULL) { | |
5579 | return 0; | 5579 | return 0; | |
5580 | } | 5580 | } | |
5581 | USE_ARG(ctx); | 5581 | USE_ARG(ctx); | |
5582 | USE_ARG(cb_arg); | 5582 | USE_ARG(cb_arg); | |
5583 | USE_ARG(callback); | 5583 | USE_ARG(callback); | |
5584 | return (mp_prime_is_prime(__UNCONST(a), checks, &primality) == MP_OKAY) ? primality : 0; | 5584 | return (mp_prime_is_prime(__UNCONST(a), checks, &primality) == MP_OKAY) ? primality : 0; | |
5585 | } | 5585 | } | |
5586 | 5586 | |||
5587 | const BIGNUM * | 5587 | const PGPV_BIGNUM * | |
5588 | BN_value_one(void) | 5588 | PGPV_BN_value_one(void) | |
5589 | { | 5589 | { | |
5590 | static mp_digit digit = 1UL; | 5590 | static mp_digit digit = 1UL; | |
5591 | static const BIGNUM one = { &digit, 1, 1, 0 }; | 5591 | static const PGPV_BIGNUM one = { &digit, 1, 1, 0 }; | |
5592 | 5592 | |||
5593 | return &one; | 5593 | return &one; | |
5594 | } | 5594 | } | |
5595 | 5595 | |||
5596 | int | 5596 | int | |
5597 | BN_hex2bn(BIGNUM **a, const char *str) | 5597 | PGPV_BN_hex2bn(PGPV_BIGNUM **a, const char *str) | |
5598 | { | 5598 | { | |
5599 | return getbn(a, str, 16); | 5599 | return getbn(a, str, 16); | |
5600 | } | 5600 | } | |
5601 | 5601 | |||
5602 | int | 5602 | int | |
5603 | BN_dec2bn(BIGNUM **a, const char *str) | 5603 | PGPV_BN_dec2bn(PGPV_BIGNUM **a, const char *str) | |
5604 | { | 5604 | { | |
5605 | return getbn(a, str, 10); | 5605 | return getbn(a, str, 10); | |
5606 | } | 5606 | } | |
5607 | 5607 | |||
5608 | int | 5608 | int | |
5609 | BN_radix2bn(BIGNUM **a, const char *str, unsigned radix) | 5609 | PGPV_BN_radix2bn(PGPV_BIGNUM **a, const char *str, unsigned radix) | |
5610 | { | 5610 | { | |
5611 | return getbn(a, str, (int)radix); | 5611 | return getbn(a, str, (int)radix); | |
5612 | } | 5612 | } | |
5613 | 5613 | |||
5614 | int | 5614 | int | |
5615 | BN_mod_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx) | 5615 | PGPV_BN_mod_sub(PGPV_BIGNUM *r, PGPV_BIGNUM *a, PGPV_BIGNUM *b, const PGPV_BIGNUM *m, PGPV_BN_CTX *ctx) | |
5616 | { | 5616 | { | |
5617 | USE_ARG(ctx); | 5617 | USE_ARG(ctx); | |
5618 | if (r == NULL || a == NULL || b == NULL || m == NULL) { | 5618 | if (r == NULL || a == NULL || b == NULL || m == NULL) { | |
5619 | return 0; | 5619 | return 0; | |
5620 | } | 5620 | } | |
5621 | return subtract_modulo(a, b, __UNCONST(m), r) == MP_OKAY; | 5621 | return subtract_modulo(a, b, __UNCONST(m), r) == MP_OKAY; | |
5622 | } | 5622 | } | |
5623 | 5623 | |||
5624 | int | 5624 | int | |
5625 | BN_is_bit_set(const BIGNUM *a, int n) | 5625 | PGPV_BN_is_bit_set(const PGPV_BIGNUM *a, int n) | |
5626 | { | 5626 | { | |
5627 | if (a == NULL || n < 0 || n >= a->used * DIGIT_BIT) { | 5627 | if (a == NULL || n < 0 || n >= a->used * DIGIT_BIT) { | |
5628 | return 0; | 5628 | return 0; | |
5629 | } | 5629 | } | |
5630 | return (a->dp[n / DIGIT_BIT] & (1 << (n % DIGIT_BIT))) ? 1 : 0; | 5630 | return (a->dp[n / DIGIT_BIT] & (1 << (n % DIGIT_BIT))) ? 1 : 0; | |
5631 | } | 5631 | } | |
5632 | 5632 | |||
5633 | /* raise 'a' to power of 'b' */ | 5633 | /* raise 'a' to power of 'b' */ | |
5634 | int | 5634 | int | |
5635 | BN_raise(BIGNUM *res, BIGNUM *a, BIGNUM *b) | 5635 | PGPV_BN_raise(PGPV_BIGNUM *res, PGPV_BIGNUM *a, PGPV_BIGNUM *b) | |
5636 | { | 5636 | { | |
5637 | uint64_t exponent; | 5637 | uint64_t exponent; | |
5638 | BIGNUM *power; | 5638 | PGPV_BIGNUM *power; | |
5639 | BIGNUM *temp; | 5639 | PGPV_BIGNUM *temp; | |
5640 | char *t; | 5640 | char *t; | |
5641 | 5641 | |||
5642 | t = BN_bn2dec(b); | 5642 | t = PGPV_BN_bn2dec(b); | |
5643 | exponent = (uint64_t)strtoull(t, NULL, 10); | 5643 | exponent = (uint64_t)strtoull(t, NULL, 10); | |
5644 | free(t); | 5644 | free(t); | |
5645 | if (exponent == 0) { | 5645 | if (exponent == 0) { | |
5646 | BN_copy(res, BN_value_one()); | 5646 | PGPV_BN_copy(res, PGPV_BN_value_one()); | |
5647 | } else { | 5647 | } else { | |
5648 | power = BN_dup(a); | 5648 | power = PGPV_BN_dup(a); | |
5649 | for ( ; (exponent & 1) == 0 ; exponent >>= 1) { | 5649 | for ( ; (exponent & 1) == 0 ; exponent >>= 1) { | |
5650 | BN_mul(power, power, power, NULL); | 5650 | PGPV_BN_mul(power, power, power, NULL); | |
5651 | } | 5651 | } | |
5652 | temp = BN_dup(power); | 5652 | temp = PGPV_BN_dup(power); | |
5653 | for (exponent >>= 1 ; exponent > 0 ; exponent >>= 1) { | 5653 | for (exponent >>= 1 ; exponent > 0 ; exponent >>= 1) { | |
5654 | BN_mul(power, power, power, NULL); | 5654 | PGPV_BN_mul(power, power, power, NULL); | |
5655 | if (exponent & 1) { | 5655 | if (exponent & 1) { | |
5656 | BN_mul(temp, power, temp, NULL); | 5656 | PGPV_BN_mul(temp, power, temp, NULL); | |
5657 | } | 5657 | } | |
5658 | } | 5658 | } | |
5659 | BN_copy(res, temp); | 5659 | PGPV_BN_copy(res, temp); | |
5660 | BN_free(power); | 5660 | PGPV_BN_free(power); | |
5661 | BN_free(temp); | 5661 | PGPV_BN_free(temp); | |
5662 | } | 5662 | } | |
5663 | return 1; | 5663 | return 1; | |
5664 | } | 5664 | } | |
5665 | 5665 | |||
5666 | /* compute the factorial */ | 5666 | /* compute the factorial */ | |
5667 | int | 5667 | int | |
5668 | BN_factorial(BIGNUM *res, BIGNUM *f) | 5668 | PGPV_BN_factorial(PGPV_BIGNUM *res, PGPV_BIGNUM *f) | |
5669 | { | 5669 | { | |
5670 | BIGNUM *one; | 5670 | PGPV_BIGNUM *one; | |
5671 | BIGNUM *i; | 5671 | PGPV_BIGNUM *i; | |
5672 | 5672 | |||
5673 | i = BN_dup(f); | 5673 | i = PGPV_BN_dup(f); | |
5674 | one = __UNCONST(BN_value_one()); | 5674 | one = __UNCONST(PGPV_BN_value_one()); | |
5675 | BN_sub(i, i, one); | 5675 | PGPV_BN_sub(i, i, one); | |
5676 | BN_copy(res, f); | 5676 | PGPV_BN_copy(res, f); | |
5677 | while (BN_cmp(i, one) > 0) { | 5677 | while (PGPV_BN_cmp(i, one) > 0) { | |
5678 | BN_mul(res, res, i, NULL); | 5678 | PGPV_BN_mul(res, res, i, NULL); | |
5679 | BN_sub(i, i, one); | 5679 | PGPV_BN_sub(i, i, one); | |
5680 | } | 5680 | } | |
5681 | BN_free(i); | 5681 | PGPV_BN_free(i); | |
5682 | return 1; | 5682 | return 1; | |
5683 | } | 5683 | } |
@@ -46,107 +46,107 @@ __BEGIN_DECLS | @@ -46,107 +46,107 @@ __BEGIN_DECLS | |||
46 | 46 | |||
47 | /* should be 32bit on ILP32, 64bit on LP64 */ | 47 | /* should be 32bit on ILP32, 64bit on LP64 */ | |
48 | typedef unsigned long mp_digit; | 48 | typedef unsigned long mp_digit; | |
49 | typedef uint64_t mp_word; | 49 | typedef uint64_t mp_word; | |
50 | 50 | |||
51 | /* multi-precision integer */ | 51 | /* multi-precision integer */ | |
52 | typedef struct mp_int { | 52 | typedef struct mp_int { | |
53 | mp_digit *dp; /* array of digits */ | 53 | mp_digit *dp; /* array of digits */ | |
54 | int used; /* # of digits used */ | 54 | int used; /* # of digits used */ | |
55 | int alloc; /* # of digits allocated */ | 55 | int alloc; /* # of digits allocated */ | |
56 | int sign; /* non-zero if negative */ | 56 | int sign; /* non-zero if negative */ | |
57 | } mp_int; | 57 | } mp_int; | |
58 | 58 | |||
59 | #define BIGNUM mp_int | 59 | #define PGPV_BIGNUM mp_int | |
60 | #define BN_ULONG mp_digit | 60 | #define PGPV_BN_ULONG mp_digit | |
61 | 61 | |||
62 | /* a "context" of mp integers - never really used */ | 62 | /* a "context" of mp integers - never really used */ | |
63 | typedef struct bn_ctx_t { | 63 | typedef struct bn_ctx_t { | |
64 | size_t count; | 64 | size_t count; | |
65 | size_t arraysize; | 65 | size_t arraysize; | |
66 | BIGNUM **v; | 66 | PGPV_BIGNUM **v; | |
67 | } BN_CTX; | 67 | } PGPV_BN_CTX; | |
68 | 68 | |||
69 | #define MP_LT -1 | 69 | #define MP_LT -1 | |
70 | #define MP_EQ 0 | 70 | #define MP_EQ 0 | |
71 | #define MP_GT 1 | 71 | #define MP_GT 1 | |
72 | 72 | |||
73 | #define MP_ZPOS 0 | 73 | #define MP_ZPOS 0 | |
74 | #define MP_NEG 1 | 74 | #define MP_NEG 1 | |
75 | 75 | |||
76 | #define MP_OKAY 0 | 76 | #define MP_OKAY 0 | |
77 | #define MP_MEM -2 | 77 | #define MP_MEM -2 | |
78 | #define MP_VAL -3 | 78 | #define MP_VAL -3 | |
79 | #define MP_RANGE MP_VAL | 79 | #define MP_RANGE MP_VAL | |
80 | 80 | |||
81 | /*********************************/ | 81 | /*********************************/ | |
82 | 82 | |||
83 | #define BN_is_negative(x) ((x)->sign == MP_NEG) | 83 | #define PGPV_BN_is_negative(x) ((x)->sign == MP_NEG) | |
84 | #define BN_is_zero(a) (((a)->used == 0) ? 1 : 0) | 84 | #define PGPV_BN_is_zero(a) (((a)->used == 0) ? 1 : 0) | |
85 | #define BN_is_odd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0) | 85 | #define PGPV_BN_is_odd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0) | |
86 | #define BN_is_even(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? 1 : 0) | 86 | #define PGPV_BN_is_even(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? 1 : 0) | |
87 | 87 | |||
88 | BIGNUM *BN_new(void); | 88 | PGPV_BIGNUM *PGPV_BN_new(void); | |
89 | BIGNUM *BN_dup(const BIGNUM */*a*/); | 89 | PGPV_BIGNUM *PGPV_BN_dup(const PGPV_BIGNUM */*a*/); | |
90 | int BN_copy(BIGNUM */*b*/, const BIGNUM */*a*/); | 90 | int PGPV_BN_copy(PGPV_BIGNUM */*b*/, const PGPV_BIGNUM */*a*/); | |
91 | 91 | |||
92 | void BN_init(BIGNUM */*a*/); | 92 | void PGPV_BN_init(PGPV_BIGNUM */*a*/); | |
93 | void BN_free(BIGNUM */*a*/); | 93 | void PGPV_BN_free(PGPV_BIGNUM */*a*/); | |
94 | void BN_clear(BIGNUM */*a*/); | 94 | void PGPV_BN_clear(PGPV_BIGNUM */*a*/); | |
95 | void BN_clear_free(BIGNUM */*a*/); | 95 | void PGPV_BN_clear_free(PGPV_BIGNUM */*a*/); | |
96 | 96 | |||
97 | int BN_cmp(BIGNUM */*a*/, BIGNUM */*b*/); | 97 | int PGPV_BN_cmp(PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/); | |
98 | 98 | |||
99 | BIGNUM *BN_bin2bn(const uint8_t */*buf*/, int /*size*/, BIGNUM */*bn*/); | 99 | PGPV_BIGNUM *PGPV_BN_bin2bn(const uint8_t */*buf*/, int /*size*/, PGPV_BIGNUM */*bn*/); | |
100 | int BN_bn2bin(const BIGNUM */*a*/, unsigned char */*b*/); | 100 | int PGPV_BN_bn2bin(const PGPV_BIGNUM */*a*/, unsigned char */*b*/); | |
101 | char *BN_bn2hex(const BIGNUM */*a*/); | 101 | char *PGPV_BN_bn2hex(const PGPV_BIGNUM */*a*/); | |
102 | char *BN_bn2dec(const BIGNUM */*a*/); | 102 | char *PGPV_BN_bn2dec(const PGPV_BIGNUM */*a*/); | |
103 | char *BN_bn2radix(const BIGNUM */*a*/, unsigned /*radix*/); | 103 | char *PGPV_BN_bn2radix(const PGPV_BIGNUM */*a*/, unsigned /*radix*/); | |
104 | int BN_hex2bn(BIGNUM **/*a*/, const char */*str*/); | 104 | int PGPV_BN_hex2bn(PGPV_BIGNUM **/*a*/, const char */*str*/); | |
105 | int BN_dec2bn(BIGNUM **/*a*/, const char */*str*/); | 105 | int PGPV_BN_dec2bn(PGPV_BIGNUM **/*a*/, const char */*str*/); | |
106 | int BN_radix2bn(BIGNUM **/*a*/, const char */*str*/, unsigned /*radix*/); | 106 | int PGPV_BN_radix2bn(PGPV_BIGNUM **/*a*/, const char */*str*/, unsigned /*radix*/); | |
107 | #ifndef _KERNEL | 107 | #ifndef _KERNEL | |
108 | int BN_print_fp(FILE */*fp*/, const BIGNUM */*a*/); | 108 | int PGPV_BN_print_fp(FILE */*fp*/, const PGPV_BIGNUM */*a*/); | |
109 | #endif | 109 | #endif | |
110 | 110 | |||
111 | int BN_add(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/); | 111 | int PGPV_BN_add(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/); | |
112 | int BN_sub(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/); | 112 | int PGPV_BN_sub(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/); | |
113 | int BN_mul(BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/, BN_CTX */*ctx*/); | 113 | int PGPV_BN_mul(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/, PGPV_BN_CTX */*ctx*/); | |
114 | int BN_div(BIGNUM */*q*/, BIGNUM */*r*/, const BIGNUM */*a*/, const BIGNUM */*b*/, BN_CTX */*ctx*/); | 114 | int PGPV_BN_div(PGPV_BIGNUM */*q*/, PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*b*/, PGPV_BN_CTX */*ctx*/); | |
115 | void BN_swap(BIGNUM */*a*/, BIGNUM */*b*/); | 115 | void PGPV_BN_swap(PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/); | |
116 | int BN_bitop(BIGNUM */*r*/, const BIGNUM */*a*/, char /*op*/, const BIGNUM */*b*/); | 116 | int PGPV_BN_bitop(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, char /*op*/, const PGPV_BIGNUM */*b*/); | |
117 | int BN_lshift(BIGNUM */*r*/, const BIGNUM */*a*/, int /*n*/); | 117 | int PGPV_BN_lshift(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, int /*n*/); | |
118 | int BN_lshift1(BIGNUM */*r*/, BIGNUM */*a*/); | 118 | int PGPV_BN_lshift1(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/); | |
119 | int BN_rshift(BIGNUM */*r*/, const BIGNUM */*a*/, int /*n*/); | 119 | int PGPV_BN_rshift(PGPV_BIGNUM */*r*/, const PGPV_BIGNUM */*a*/, int /*n*/); | |
120 | int BN_rshift1(BIGNUM */*r*/, BIGNUM */*a*/); | 120 | int PGPV_BN_rshift1(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/); | |
121 | int BN_set_word(BIGNUM */*a*/, BN_ULONG /*w*/); | 121 | int PGPV_BN_set_word(PGPV_BIGNUM */*a*/, PGPV_BN_ULONG /*w*/); | |
122 | void BN_set_negative(BIGNUM */*a*/, int /*n*/); | 122 | void PGPV_BN_set_negative(PGPV_BIGNUM */*a*/, int /*n*/); | |
123 | 123 | |||
124 | int BN_num_bytes(const BIGNUM */*a*/); | 124 | int PGPV_BN_num_bytes(const PGPV_BIGNUM */*a*/); | |
125 | int BN_num_bits(const BIGNUM */*a*/); | 125 | int PGPV_BN_num_bits(const PGPV_BIGNUM */*a*/); | |
126 | 126 | |||
127 | int BN_mod_exp(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*p*/, BIGNUM */*m*/, BN_CTX */*ctx*/); | 127 | int PGPV_BN_mod_exp(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*p*/, PGPV_BIGNUM */*m*/, PGPV_BN_CTX */*ctx*/); | |
128 | BIGNUM *BN_mod_inverse(BIGNUM */*ret*/, BIGNUM */*a*/, const BIGNUM */*n*/, BN_CTX */*ctx*/); | 128 | PGPV_BIGNUM *PGPV_BN_mod_inverse(PGPV_BIGNUM */*ret*/, PGPV_BIGNUM */*a*/, const PGPV_BIGNUM */*n*/, PGPV_BN_CTX */*ctx*/); | |
129 | int BN_mod_mul(BIGNUM */*ret*/, BIGNUM */*a*/, BIGNUM */*b*/, const BIGNUM */*m*/, BN_CTX */*ctx*/); | 129 | int PGPV_BN_mod_mul(PGPV_BIGNUM */*ret*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/, const PGPV_BIGNUM */*m*/, PGPV_BN_CTX */*ctx*/); | |
130 | int BN_mod_sub(BIGNUM */*r*/, BIGNUM */*a*/, BIGNUM */*b*/, const BIGNUM */*m*/, BN_CTX */*ctx*/); | 130 | int PGPV_BN_mod_sub(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/, const PGPV_BIGNUM */*m*/, PGPV_BN_CTX */*ctx*/); | |
131 | 131 | |||
132 | int BN_raise(BIGNUM */*res*/, BIGNUM */*a*/, BIGNUM */*b*/); | 132 | int PGPV_BN_raise(PGPV_BIGNUM */*res*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/); | |
133 | int BN_factorial(BIGNUM */*fact*/, BIGNUM */*f*/); | 133 | int PGPV_BN_factorial(PGPV_BIGNUM */*fact*/, PGPV_BIGNUM */*f*/); | |
134 | 134 | |||
135 | BN_CTX *BN_CTX_new(void); | 135 | PGPV_BN_CTX *PGPV_BN_CTX_new(void); | |
136 | BIGNUM *BN_CTX_get(BN_CTX */*ctx*/); | 136 | PGPV_BIGNUM *PGPV_BN_CTX_get(PGPV_BN_CTX */*ctx*/); | |
137 | void BN_CTX_start(BN_CTX */*ctx*/); | 137 | void PGPV_BN_CTX_start(PGPV_BN_CTX */*ctx*/); | |
138 | void BN_CTX_end(BN_CTX */*ctx*/); | 138 | void PGPV_BN_CTX_end(PGPV_BN_CTX */*ctx*/); | |
139 | void BN_CTX_init(BN_CTX */*c*/); | 139 | void PGPV_BN_CTX_init(PGPV_BN_CTX */*c*/); | |
140 | void BN_CTX_free(BN_CTX */*c*/); | 140 | void PGPV_BN_CTX_free(PGPV_BN_CTX */*c*/); | |
141 | 141 | |||
142 | int BN_rand(BIGNUM */*rnd*/, int /*bits*/, int /*top*/, int /*bottom*/); | 142 | int PGPV_BN_rand(PGPV_BIGNUM */*rnd*/, int /*bits*/, int /*top*/, int /*bottom*/); | |
143 | int BN_rand_range(BIGNUM */*rnd*/, BIGNUM */*range*/); | 143 | int PGPV_BN_rand_range(PGPV_BIGNUM */*rnd*/, PGPV_BIGNUM */*range*/); | |
144 | 144 | |||
145 | int BN_is_prime(const BIGNUM */*a*/, int /*checks*/, void (*callback)(int, int, void *), BN_CTX */*ctx*/, void */*cb_arg*/); | 145 | int PGPV_BN_is_prime(const PGPV_BIGNUM */*a*/, int /*checks*/, void (*callback)(int, int, void *), PGPV_BN_CTX */*ctx*/, void */*cb_arg*/); | |
146 | 146 | |||
147 | const BIGNUM *BN_value_one(void); | 147 | const PGPV_BIGNUM *PGPV_BN_value_one(void); | |
148 | int BN_is_bit_set(const BIGNUM */*a*/, int /*n*/); | 148 | int PGPV_BN_is_bit_set(const PGPV_BIGNUM */*a*/, int /*n*/); | |
149 | 149 | |||
150 | __END_DECLS | 150 | __END_DECLS | |
151 | 151 | |||
152 | #endif | 152 | #endif |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: md5.h,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: md5.h,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest | 4 | * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest | |
5 | * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.org> | 5 | * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.org> | |
6 | * for portability and formatting. | 6 | * for portability and formatting. | |
7 | */ | 7 | */ | |
8 | 8 | |||
9 | /* | 9 | /* | |
10 | * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | 10 | * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | |
11 | * rights reserved. | 11 | * rights reserved. | |
12 | * | 12 | * | |
13 | * License to copy and use this software is granted provided that it | 13 | * License to copy and use this software is granted provided that it | |
14 | * is identified as the "RSA Data Security, Inc. MD5 Message-Digest | 14 | * is identified as the "RSA Data Security, Inc. MD5 Message-Digest | |
@@ -44,27 +44,27 @@ | @@ -44,27 +44,27 @@ | |||
44 | # define __BEGIN_DECLS extern "C" { | 44 | # define __BEGIN_DECLS extern "C" { | |
45 | # define __END_DECLS } | 45 | # define __END_DECLS } | |
46 | # else | 46 | # else | |
47 | # define __BEGIN_DECLS | 47 | # define __BEGIN_DECLS | |
48 | # define __END_DECLS | 48 | # define __END_DECLS | |
49 | # endif | 49 | # endif | |
50 | #endif | 50 | #endif | |
51 | 51 | |||
52 | /* MD5 context. */ | 52 | /* MD5 context. */ | |
53 | typedef struct MD5Context { | 53 | typedef struct MD5Context { | |
54 | uint32_t state[4]; /* state (ABCD) */ | 54 | uint32_t state[4]; /* state (ABCD) */ | |
55 | uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ | 55 | uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ | |
56 | unsigned char buffer[64]; /* input buffer */ | 56 | unsigned char buffer[64]; /* input buffer */ | |
57 | } MD5_CTX; | 57 | } NETPGPV_MD5_CTX; | |
58 | 58 | |||
59 | __BEGIN_DECLS | 59 | __BEGIN_DECLS | |
60 | void MD5Init(MD5_CTX *); | 60 | void netpgpv_MD5Init(NETPGPV_MD5_CTX *); | |
61 | void MD5Update(MD5_CTX *, const unsigned char *, unsigned int); | 61 | void netpgpv_MD5Update(NETPGPV_MD5_CTX *, const unsigned char *, unsigned int); | |
62 | void MD5Final(unsigned char[MD5_DIGEST_LENGTH], MD5_CTX *); | 62 | void netpgpv_MD5Final(unsigned char[MD5_DIGEST_LENGTH], NETPGPV_MD5_CTX *); | |
63 | #ifndef _KERNEL | 63 | #ifndef _KERNEL | |
64 | char *MD5End(MD5_CTX *, char *); | 64 | char *netpgpv_MD5End(NETPGPV_MD5_CTX *, char *); | |
65 | char *MD5File(const char *, char *); | 65 | char *netpgpv_MD5File(const char *, char *); | |
66 | char *MD5Data(const unsigned char *, unsigned int, char *); | 66 | char *netpgpv_MD5Data(const unsigned char *, unsigned int, char *); | |
67 | #endif /* _KERNEL */ | 67 | #endif /* _KERNEL */ | |
68 | __END_DECLS | 68 | __END_DECLS | |
69 | 69 | |||
70 | #endif /* _SYS_MD5_H_ */ | 70 | #endif /* _SYS_MD5_H_ */ |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: md5c.c,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: md5c.c,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest | 4 | * This file is derived from the RSA Data Security, Inc. MD5 Message-Digest | |
5 | * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.org> | 5 | * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.org> | |
6 | * for portability and formatting. | 6 | * for portability and formatting. | |
7 | */ | 7 | */ | |
8 | 8 | |||
9 | /* | 9 | /* | |
10 | * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | 10 | * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | |
11 | * rights reserved. | 11 | * rights reserved. | |
12 | * | 12 | * | |
13 | * License to copy and use this software is granted provided that it | 13 | * License to copy and use this software is granted provided that it | |
14 | * is identified as the "RSA Data Security, Inc. MD5 Message-Digest | 14 | * is identified as the "RSA Data Security, Inc. MD5 Message-Digest | |
@@ -144,45 +144,45 @@ static const unsigned char PADDING[64] = | @@ -144,45 +144,45 @@ static const unsigned char PADDING[64] = | |||
144 | (a) += (b); \ | 144 | (a) += (b); \ | |
145 | } | 145 | } | |
146 | 146 | |||
147 | #define II(a, b, c, d, x, s, ac) { \ | 147 | #define II(a, b, c, d, x, s, ac) { \ | |
148 | (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ | 148 | (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ | |
149 | (a) = ROTATE_LEFT ((a), (s)); \ | 149 | (a) = ROTATE_LEFT ((a), (s)); \ | |
150 | (a) += (b); \ | 150 | (a) += (b); \ | |
151 | } | 151 | } | |
152 | 152 | |||
153 | /* | 153 | /* | |
154 | * MD5 initialization. Begins an MD5 operation, writing a new context. | 154 | * MD5 initialization. Begins an MD5 operation, writing a new context. | |
155 | */ | 155 | */ | |
156 | void | 156 | void | |
157 | MD5Init(MD5_CTX *context) | 157 | netpgpv_MD5Init(NETPGPV_MD5_CTX *context) | |
158 | { | 158 | { | |
159 | 159 | |||
160 | context->count[0] = context->count[1] = 0; | 160 | context->count[0] = context->count[1] = 0; | |
161 | 161 | |||
162 | /* Load magic initialization constants. */ | 162 | /* Load magic initialization constants. */ | |
163 | context->state[0] = 0x67452301; | 163 | context->state[0] = 0x67452301; | |
164 | context->state[1] = 0xefcdab89; | 164 | context->state[1] = 0xefcdab89; | |
165 | context->state[2] = 0x98badcfe; | 165 | context->state[2] = 0x98badcfe; | |
166 | context->state[3] = 0x10325476; | 166 | context->state[3] = 0x10325476; | |
167 | } | 167 | } | |
168 | 168 | |||
169 | /* | 169 | /* | |
170 | * MD5 block update operation. Continues an MD5 message-digest | 170 | * MD5 block update operation. Continues an MD5 message-digest | |
171 | * operation, processing another message block, and updating the | 171 | * operation, processing another message block, and updating the | |
172 | * context. | 172 | * context. | |
173 | */ | 173 | */ | |
174 | void | 174 | void | |
175 | MD5Update(MD5_CTX *context, | 175 | netpgpv_MD5Update(NETPGPV_MD5_CTX *context, | |
176 | const unsigned char *input, /* input block */ | 176 | const unsigned char *input, /* input block */ | |
177 | unsigned int inputLen) /* length of input block */ | 177 | unsigned int inputLen) /* length of input block */ | |
178 | { | 178 | { | |
179 | unsigned int i, idx, partLen; | 179 | unsigned int i, idx, partLen; | |
180 | 180 | |||
181 | /* Compute number of bytes mod 64 */ | 181 | /* Compute number of bytes mod 64 */ | |
182 | idx = (unsigned int)((context->count[0] >> 3) & 0x3F); | 182 | idx = (unsigned int)((context->count[0] >> 3) & 0x3F); | |
183 | 183 | |||
184 | /* Update number of bits */ | 184 | /* Update number of bits */ | |
185 | if ((context->count[0] += ((UINT4)inputLen << 3)) | 185 | if ((context->count[0] += ((UINT4)inputLen << 3)) | |
186 | < ((UINT4)inputLen << 3)) | 186 | < ((UINT4)inputLen << 3)) | |
187 | context->count[1]++; | 187 | context->count[1]++; | |
188 | context->count[1] += ((UINT4)inputLen >> 29); | 188 | context->count[1] += ((UINT4)inputLen >> 29); | |
@@ -200,42 +200,42 @@ MD5Update(MD5_CTX *context, | @@ -200,42 +200,42 @@ MD5Update(MD5_CTX *context, | |||
200 | idx = 0; | 200 | idx = 0; | |
201 | } else | 201 | } else | |
202 | i = 0; | 202 | i = 0; | |
203 | 203 | |||
204 | /* Buffer remaining input */ | 204 | /* Buffer remaining input */ | |
205 | memcpy(&context->buffer[idx], &input[i], inputLen - i); | 205 | memcpy(&context->buffer[idx], &input[i], inputLen - i); | |
206 | } | 206 | } | |
207 | 207 | |||
208 | /* | 208 | /* | |
209 | * MD5 finalization. Ends an MD5 message-digest operation, writing the | 209 | * MD5 finalization. Ends an MD5 message-digest operation, writing the | |
210 | * message digest and zeroing the context. | 210 | * message digest and zeroing the context. | |
211 | */ | 211 | */ | |
212 | void | 212 | void | |
213 | MD5Final(unsigned char digest[16], /* message digest */ | 213 | netpgpv_MD5Final(unsigned char digest[16], /* message digest */ | |
214 | MD5_CTX *context) /* context */ | 214 | NETPGPV_MD5_CTX *context) /* context */ | |
215 | { | 215 | { | |
216 | unsigned char bits[8]; | 216 | unsigned char bits[8]; | |
217 | unsigned int idx, padLen; | 217 | unsigned int idx, padLen; | |
218 | 218 | |||
219 | /* Save number of bits */ | 219 | /* Save number of bits */ | |
220 | Encode(bits, context->count, 8); | 220 | Encode(bits, context->count, 8); | |
221 | 221 | |||
222 | /* Pad out to 56 mod 64. */ | 222 | /* Pad out to 56 mod 64. */ | |
223 | idx = (unsigned int)((context->count[0] >> 3) & 0x3f); | 223 | idx = (unsigned int)((context->count[0] >> 3) & 0x3f); | |
224 | padLen = (idx < 56) ? (56 - idx) : (120 - idx); | 224 | padLen = (idx < 56) ? (56 - idx) : (120 - idx); | |
225 | MD5Update (context, PADDING, padLen); | 225 | netpgpv_MD5Update (context, PADDING, padLen); | |
226 | 226 | |||
227 | /* Append length (before padding) */ | 227 | /* Append length (before padding) */ | |
228 | MD5Update(context, bits, 8); | 228 | netpgpv_MD5Update(context, bits, 8); | |
229 | 229 | |||
230 | /* Store state in digest */ | 230 | /* Store state in digest */ | |
231 | Encode(digest, context->state, 16); | 231 | Encode(digest, context->state, 16); | |
232 | 232 | |||
233 | /* Zeroize sensitive information. */ | 233 | /* Zeroize sensitive information. */ | |
234 | ZEROIZE((POINTER)(void *)context, sizeof(*context)); | 234 | ZEROIZE((POINTER)(void *)context, sizeof(*context)); | |
235 | } | 235 | } | |
236 | 236 | |||
237 | /* | 237 | /* | |
238 | * MD5 basic transformation. Transforms state based on block. | 238 | * MD5 basic transformation. Transforms state based on block. | |
239 | */ | 239 | */ | |
240 | static void | 240 | static void | |
241 | MD5Transform(UINT4 state[4], const unsigned char block[64]) | 241 | MD5Transform(UINT4 state[4], const unsigned char block[64]) |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: rmd160.c,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: rmd160.c,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | /* $KAME: rmd160.c,v 1.2 2003/07/25 09:37:55 itojun Exp $ */ | 2 | /* $KAME: rmd160.c,v 1.2 2003/07/25 09:37:55 itojun Exp $ */ | |
3 | /* $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $ */ | 3 | /* $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $ */ | |
4 | /* | 4 | /* | |
5 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | 5 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | |
6 | * | 6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | 9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. | |
@@ -85,91 +85,91 @@ | @@ -85,91 +85,91 @@ | |||
85 | a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \ | 85 | a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \ | |
86 | c = ROL(10, c); \ | 86 | c = ROL(10, c); \ | |
87 | } while(/*CONSTCOND*/0) | 87 | } while(/*CONSTCOND*/0) | |
88 | 88 | |||
89 | #define X(i) x[i] | 89 | #define X(i) x[i] | |
90 | 90 | |||
91 | static const u_char PADDING[64] = { | 91 | static const u_char PADDING[64] = { | |
92 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 92 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
93 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 93 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
94 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | 94 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
95 | }; | 95 | }; | |
96 | 96 | |||
97 | void | 97 | void | |
98 | RMD160Init(RMD160_CTX *ctx) | 98 | netpgpv_RMD160Init(NETPGPV_RMD160_CTX *ctx) | |
99 | { | 99 | { | |
100 | ctx->count = 0; | 100 | ctx->count = 0; | |
101 | ctx->state[0] = H0; | 101 | ctx->state[0] = H0; | |
102 | ctx->state[1] = H1; | 102 | ctx->state[1] = H1; | |
103 | ctx->state[2] = H2; | 103 | ctx->state[2] = H2; | |
104 | ctx->state[3] = H3; | 104 | ctx->state[3] = H3; | |
105 | ctx->state[4] = H4; | 105 | ctx->state[4] = H4; | |
106 | } | 106 | } | |
107 | 107 | |||
108 | void | 108 | void | |
109 | RMD160Update(RMD160_CTX *ctx, const u_char *input, uint32_t len) | 109 | netpgpv_RMD160Update(NETPGPV_RMD160_CTX *ctx, const u_char *input, uint32_t len) | |
110 | { | 110 | { | |
111 | uint32_t have, off, need; | 111 | uint32_t have, off, need; | |
112 | 112 | |||
113 | have = (uint32_t)((ctx->count/8) % 64); | 113 | have = (uint32_t)((ctx->count/8) % 64); | |
114 | need = 64 - have; | 114 | need = 64 - have; | |
115 | ctx->count += 8 * len; | 115 | ctx->count += 8 * len; | |
116 | off = 0; | 116 | off = 0; | |
117 | 117 | |||
118 | if (len >= need) { | 118 | if (len >= need) { | |
119 | if (have) { | 119 | if (have) { | |
120 | memcpy(ctx->buffer + have, input, (size_t)need); | 120 | memcpy(ctx->buffer + have, input, (size_t)need); | |
121 | RMD160Transform(ctx->state, ctx->buffer); | 121 | netpgpv_RMD160Transform(ctx->state, ctx->buffer); | |
122 | off = need; | 122 | off = need; | |
123 | have = 0; | 123 | have = 0; | |
124 | } | 124 | } | |
125 | /* now the buffer is empty */ | 125 | /* now the buffer is empty */ | |
126 | while (off + 64 <= len) { | 126 | while (off + 64 <= len) { | |
127 | RMD160Transform(ctx->state, input+off); | 127 | netpgpv_RMD160Transform(ctx->state, input+off); | |
128 | off += 64; | 128 | off += 64; | |
129 | } | 129 | } | |
130 | } | 130 | } | |
131 | if (off < len) | 131 | if (off < len) | |
132 | memcpy(ctx->buffer + have, input+off, (size_t)len-off); | 132 | memcpy(ctx->buffer + have, input+off, (size_t)len-off); | |
133 | } | 133 | } | |
134 | 134 | |||
135 | void | 135 | void | |
136 | RMD160Final(u_char digest[20], RMD160_CTX *ctx) | 136 | netpgpv_RMD160Final(u_char digest[20], NETPGPV_RMD160_CTX *ctx) | |
137 | { | 137 | { | |
138 | int i; | 138 | int i; | |
139 | u_char size[8]; | 139 | u_char size[8]; | |
140 | uint32_t padlen; | 140 | uint32_t padlen; | |
141 | 141 | |||
142 | PUT_64BIT_LE(size, ctx->count); | 142 | PUT_64BIT_LE(size, ctx->count); | |
143 | 143 | |||
144 | /* | 144 | /* | |
145 | * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes | 145 | * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes | |
146 | * for the size | 146 | * for the size | |
147 | */ | 147 | */ | |
148 | padlen = (uint32_t)(64 - ((ctx->count/8) % 64)); | 148 | padlen = (uint32_t)(64 - ((ctx->count/8) % 64)); | |
149 | if (padlen < 1 + 8) | 149 | if (padlen < 1 + 8) | |
150 | padlen += 64; | 150 | padlen += 64; | |
151 | RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ | 151 | netpgpv_RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ | |
152 | RMD160Update(ctx, size, 8); | 152 | netpgpv_RMD160Update(ctx, size, 8); | |
153 | 153 | |||
154 | if (digest != NULL) | 154 | if (digest != NULL) | |
155 | for (i = 0; i < 5; i++) | 155 | for (i = 0; i < 5; i++) | |
156 | PUT_32BIT_LE(digest + i*4, ctx->state[i]); | 156 | PUT_32BIT_LE(digest + i*4, ctx->state[i]); | |
157 | 157 | |||
158 | memset(ctx, 0, sizeof (*ctx)); | 158 | memset(ctx, 0, sizeof (*ctx)); | |
159 | } | 159 | } | |
160 | 160 | |||
161 | void | 161 | void | |
162 | RMD160Transform(uint32_t state[5], const u_char block[64]) | 162 | netpgpv_RMD160Transform(uint32_t state[5], const u_char block[64]) | |
163 | { | 163 | { | |
164 | uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16]; | 164 | uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16]; | |
165 | 165 | |||
166 | #if BYTE_ORDER == LITTLE_ENDIAN | 166 | #if BYTE_ORDER == LITTLE_ENDIAN | |
167 | memcpy(x, block, (size_t)64); | 167 | memcpy(x, block, (size_t)64); | |
168 | #else | 168 | #else | |
169 | int i; | 169 | int i; | |
170 | 170 | |||
171 | for (i = 0; i < 16; i++) | 171 | for (i = 0; i < 16; i++) | |
172 | x[i] = le32dec(block+i*4); | 172 | x[i] = le32dec(block+i*4); | |
173 | #endif | 173 | #endif | |
174 | 174 | |||
175 | a = state[0]; | 175 | a = state[0]; |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: rmd160.h,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: rmd160.h,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | /* $KAME: rmd160.h,v 1.2 2003/07/25 09:37:55 itojun Exp $ */ | 2 | /* $KAME: rmd160.h,v 1.2 2003/07/25 09:37:55 itojun Exp $ */ | |
3 | /* $OpenBSD: rmd160.h,v 1.3 2002/03/14 01:26:51 millert Exp $ */ | 3 | /* $OpenBSD: rmd160.h,v 1.3 2002/03/14 01:26:51 millert Exp $ */ | |
4 | /* | 4 | /* | |
5 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | 5 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | |
6 | * | 6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | 9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. | |
@@ -39,29 +39,29 @@ | @@ -39,29 +39,29 @@ | |||
39 | # define __BEGIN_DECLS extern "C" { | 39 | # define __BEGIN_DECLS extern "C" { | |
40 | # define __END_DECLS } | 40 | # define __END_DECLS } | |
41 | # else | 41 | # else | |
42 | # define __BEGIN_DECLS | 42 | # define __BEGIN_DECLS | |
43 | # define __END_DECLS | 43 | # define __END_DECLS | |
44 | # endif | 44 | # endif | |
45 | #endif | 45 | #endif | |
46 | 46 | |||
47 | /* RMD160 context. */ | 47 | /* RMD160 context. */ | |
48 | typedef struct RMD160Context { | 48 | typedef struct RMD160Context { | |
49 | uint32_t state[5]; /* state */ | 49 | uint32_t state[5]; /* state */ | |
50 | uint64_t count; /* number of bits, modulo 2^64 */ | 50 | uint64_t count; /* number of bits, modulo 2^64 */ | |
51 | u_char buffer[64]; /* input buffer */ | 51 | u_char buffer[64]; /* input buffer */ | |
52 | } RMD160_CTX; | 52 | } NETPGPV_RMD160_CTX; | |
53 | 53 | |||
54 | __BEGIN_DECLS | 54 | __BEGIN_DECLS | |
55 | void RMD160Init(RMD160_CTX *); | 55 | void netpgpv_RMD160Init(NETPGPV_RMD160_CTX *); | |
56 | void RMD160Transform(uint32_t [5], const u_char [64]); | 56 | void netpgpv_RMD160Transform(uint32_t [5], const u_char [64]); | |
57 | void RMD160Update(RMD160_CTX *, const u_char *, uint32_t); | 57 | void netpgpv_RMD160Update(NETPGPV_RMD160_CTX *, const u_char *, uint32_t); | |
58 | void RMD160Final(u_char [RMD160_DIGEST_LENGTH], RMD160_CTX *); | 58 | void netpgpv_RMD160Final(u_char [RMD160_DIGEST_LENGTH], NETPGPV_RMD160_CTX *); | |
59 | #ifndef _KERNEL | 59 | #ifndef _KERNEL | |
60 | char *RMD160End(RMD160_CTX *, char *); | 60 | char *netpgpv_RMD160End(NETPGPV_RMD160_CTX *, char *); | |
61 | char *RMD160FileChunk(const char *, char *, off_t, off_t); | 61 | char *netpgpv_RMD160FileChunk(const char *, char *, off_t, off_t); | |
62 | char *RMD160File(const char *, char *); | 62 | char *netpgpv_RMD160File(const char *, char *); | |
63 | char *RMD160Data(const u_char *, size_t, char *); | 63 | char *netpgpv_RMD160Data(const u_char *, size_t, char *); | |
64 | #endif /* _KERNEL */ | 64 | #endif /* _KERNEL */ | |
65 | __END_DECLS | 65 | __END_DECLS | |
66 | 66 | |||
67 | #endif /* _RMD160_H */ | 67 | #endif /* _RMD160_H */ |
@@ -54,204 +54,204 @@ rsa_padding_check_none(uint8_t *to, int | @@ -54,204 +54,204 @@ rsa_padding_check_none(uint8_t *to, int | |||
54 | USE_ARG(num); | 54 | USE_ARG(num); | |
55 | if (flen > tlen) { | 55 | if (flen > tlen) { | |
56 | printf("r too large\n"); | 56 | printf("r too large\n"); | |
57 | return -1; | 57 | return -1; | |
58 | } | 58 | } | |
59 | (void) memset(to, 0x0, tlen - flen); | 59 | (void) memset(to, 0x0, tlen - flen); | |
60 | (void) memcpy(to + tlen - flen, from, flen); | 60 | (void) memcpy(to + tlen - flen, from, flen); | |
61 | return tlen; | 61 | return tlen; | |
62 | } | 62 | } | |
63 | 63 | |||
64 | static int | 64 | static int | |
65 | lowlevel_rsa_private_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa) | 65 | lowlevel_rsa_private_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa) | |
66 | { | 66 | { | |
67 | BIGNUM *decbn; | 67 | PGPV_BIGNUM *decbn; | |
68 | BIGNUM *signedbn; | 68 | PGPV_BIGNUM *signedbn; | |
69 | uint8_t *decbuf; | 69 | uint8_t *decbuf; | |
70 | int nbytes; | 70 | int nbytes; | |
71 | int signc; | 71 | int signc; | |
72 | int signedbytes; | 72 | int signedbytes; | |
73 | int r; | 73 | int r; | |
74 | 74 | |||
75 | decbuf = NULL; | 75 | decbuf = NULL; | |
76 | r = -1; | 76 | r = -1; | |
77 | decbn = BN_new(); | 77 | decbn = PGPV_BN_new(); | |
78 | signedbn = BN_new(); | 78 | signedbn = PGPV_BN_new(); | |
79 | nbytes = BN_num_bytes(rsa->n); | 79 | nbytes = PGPV_BN_num_bytes(rsa->n); | |
80 | decbuf = netpgp_allocate(1, nbytes); | 80 | decbuf = netpgp_allocate(1, nbytes); | |
81 | /* add no padding */ | 81 | /* add no padding */ | |
82 | memcpy(decbuf, plain, plainc); | 82 | memcpy(decbuf, plain, plainc); | |
83 | BN_bin2bn(decbuf, nbytes, decbn); | 83 | PGPV_BN_bin2bn(decbuf, nbytes, decbn); | |
84 | if (BN_cmp(decbn, rsa->n) >= 0) { | 84 | if (PGPV_BN_cmp(decbn, rsa->n) >= 0) { | |
85 | printf("decbn too big\n"); | 85 | printf("decbn too big\n"); | |
86 | goto err; | 86 | goto err; | |
87 | } | 87 | } | |
88 | if (!BN_mod_exp(signedbn, decbn, rsa->d, rsa->n, NULL)) { | 88 | if (!PGPV_BN_mod_exp(signedbn, decbn, rsa->d, rsa->n, NULL)) { | |
89 | printf("bad mod_exp\n"); | 89 | printf("bad mod_exp\n"); | |
90 | goto err; | 90 | goto err; | |
91 | } | 91 | } | |
92 | signedbytes = BN_num_bytes(signedbn); | 92 | signedbytes = PGPV_BN_num_bytes(signedbn); | |
93 | signc = BN_bn2bin(signedbn, &encbuf[nbytes - signedbytes]); | 93 | signc = PGPV_BN_bn2bin(signedbn, &encbuf[nbytes - signedbytes]); | |
94 | memset(encbuf, 0x0, nbytes - signc); | 94 | memset(encbuf, 0x0, nbytes - signc); | |
95 | r = nbytes; | 95 | r = nbytes; | |
96 | err: | 96 | err: | |
97 | netpgp_deallocate(decbuf, nbytes); | 97 | netpgp_deallocate(decbuf, nbytes); | |
98 | BN_clear_free(decbn); | 98 | PGPV_BN_clear_free(decbn); | |
99 | BN_clear_free(signedbn); | 99 | PGPV_BN_clear_free(signedbn); | |
100 | return r; | 100 | return r; | |
101 | } | 101 | } | |
102 | 102 | |||
103 | static int | 103 | static int | |
104 | lowlevel_rsa_public_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa) | 104 | lowlevel_rsa_public_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa) | |
105 | { | 105 | { | |
106 | BIGNUM *decbn; | 106 | PGPV_BIGNUM *decbn; | |
107 | BIGNUM *encbn; | 107 | PGPV_BIGNUM *encbn; | |
108 | uint8_t *decbuf; | 108 | uint8_t *decbuf; | |
109 | int nbytes; | 109 | int nbytes; | |
110 | int encc; | 110 | int encc; | |
111 | int r; | 111 | int r; | |
112 | int i; | 112 | int i; | |
113 | 113 | |||
114 | r = -1; | 114 | r = -1; | |
115 | decbn = BN_new(); | 115 | decbn = PGPV_BN_new(); | |
116 | encbn = BN_new(); | 116 | encbn = PGPV_BN_new(); | |
117 | nbytes = BN_num_bytes(rsa->n); | 117 | nbytes = PGPV_BN_num_bytes(rsa->n); | |
118 | decbuf = netpgp_allocate(1, nbytes); | 118 | decbuf = netpgp_allocate(1, nbytes); | |
119 | (void) memcpy(decbuf, plain, plainc); | 119 | (void) memcpy(decbuf, plain, plainc); | |
120 | if (BN_bin2bn(decbuf, nbytes, decbn) == NULL) { | 120 | if (PGPV_BN_bin2bn(decbuf, nbytes, decbn) == NULL) { | |
121 | printf("bin2bn failed\n"); | 121 | printf("bin2bn failed\n"); | |
122 | goto err; | 122 | goto err; | |
123 | } | 123 | } | |
124 | if (BN_cmp(decbn, rsa->n) >= 0) { | 124 | if (PGPV_BN_cmp(decbn, rsa->n) >= 0) { | |
125 | printf("BN_cmp failed\n"); | 125 | printf("PGPV_BN_cmp failed\n"); | |
126 | goto err; | 126 | goto err; | |
127 | } | 127 | } | |
128 | if (!BN_mod_exp(encbn, decbn, rsa->e, rsa->n, NULL)) { | 128 | if (!PGPV_BN_mod_exp(encbn, decbn, rsa->e, rsa->n, NULL)) { | |
129 | printf("BN_mod_exp failed\n"); | 129 | printf("PGPV_BN_mod_exp failed\n"); | |
130 | goto err; | 130 | goto err; | |
131 | } | 131 | } | |
132 | encc = BN_num_bytes(encbn); | 132 | encc = PGPV_BN_num_bytes(encbn); | |
133 | i = BN_bn2bin(encbn, &encbuf[nbytes - encc]); | 133 | i = PGPV_BN_bn2bin(encbn, &encbuf[nbytes - encc]); | |
134 | (void) memset(encbuf, 0x0, nbytes - i); | 134 | (void) memset(encbuf, 0x0, nbytes - i); | |
135 | r = nbytes; | 135 | r = nbytes; | |
136 | err: | 136 | err: | |
137 | if (decbuf) { | 137 | if (decbuf) { | |
138 | memset(decbuf, 0x0, nbytes); | 138 | memset(decbuf, 0x0, nbytes); | |
139 | netpgp_deallocate(decbuf, nbytes); | 139 | netpgp_deallocate(decbuf, nbytes); | |
140 | } | 140 | } | |
141 | BN_clear_free(decbn); | 141 | PGPV_BN_clear_free(decbn); | |
142 | BN_clear_free(encbn); | 142 | PGPV_BN_clear_free(encbn); | |
143 | return r; | 143 | return r; | |
144 | } | 144 | } | |
145 | 145 | |||
146 | static int | 146 | static int | |
147 | lowlevel_rsa_private_decrypt(int enclen, const unsigned char *encbuf, unsigned char *to, RSA *rsa) | 147 | lowlevel_rsa_private_decrypt(int enclen, const unsigned char *encbuf, unsigned char *to, RSA *rsa) | |
148 | { | 148 | { | |
149 | BIGNUM *encbn; | 149 | PGPV_BIGNUM *encbn; | |
150 | BIGNUM *decbn; | 150 | PGPV_BIGNUM *decbn; | |
151 | uint8_t *buf; | 151 | uint8_t *buf; | |
152 | int nbytes; | 152 | int nbytes; | |
153 | int j; | 153 | int j; | |
154 | int r; | 154 | int r; | |
155 | 155 | |||
156 | r = -1; | 156 | r = -1; | |
157 | decbn = encbn = NULL; | 157 | decbn = encbn = NULL; | |
158 | buf = NULL; | 158 | buf = NULL; | |
159 | if (BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) { | 159 | if (PGPV_BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) { | |
160 | return -1; | 160 | return -1; | |
161 | } | 161 | } | |
162 | if (BN_cmp(rsa->n, rsa->e) <= 0) { | 162 | if (PGPV_BN_cmp(rsa->n, rsa->e) <= 0) { | |
163 | return -1; | 163 | return -1; | |
164 | } | 164 | } | |
165 | encbn = BN_new(); | 165 | encbn = PGPV_BN_new(); | |
166 | decbn = BN_new(); | 166 | decbn = PGPV_BN_new(); | |
167 | nbytes = BN_num_bytes(rsa->n); | 167 | nbytes = PGPV_BN_num_bytes(rsa->n); | |
168 | buf = netpgp_allocate(1, nbytes); | 168 | buf = netpgp_allocate(1, nbytes); | |
169 | if (enclen > nbytes) { | 169 | if (enclen > nbytes) { | |
170 | printf("bad enclen\n"); | 170 | printf("bad enclen\n"); | |
171 | goto err; | 171 | goto err; | |
172 | } | 172 | } | |
173 | BN_bin2bn(encbuf, enclen, encbn); | 173 | PGPV_BN_bin2bn(encbuf, enclen, encbn); | |
174 | if (BN_cmp(encbn, rsa->n) >= 0) { | 174 | if (PGPV_BN_cmp(encbn, rsa->n) >= 0) { | |
175 | printf("bad encbn\n"); | 175 | printf("bad encbn\n"); | |
176 | goto err; | 176 | goto err; | |
177 | } | 177 | } | |
178 | BN_mod_exp(decbn, encbn, rsa->d, rsa->n, NULL); | 178 | PGPV_BN_mod_exp(decbn, encbn, rsa->d, rsa->n, NULL); | |
179 | j = BN_bn2bin(decbn, buf); | 179 | j = PGPV_BN_bn2bin(decbn, buf); | |
180 | r = rsa_padding_check_none(to, nbytes, buf, j, nbytes); | 180 | r = rsa_padding_check_none(to, nbytes, buf, j, nbytes); | |
181 | err: | 181 | err: | |
182 | BN_clear_free(encbn); | 182 | PGPV_BN_clear_free(encbn); | |
183 | BN_clear_free(decbn); | 183 | PGPV_BN_clear_free(decbn); | |
184 | netpgp_deallocate(buf, nbytes); | 184 | netpgp_deallocate(buf, nbytes); | |
185 | return r; | 185 | return r; | |
186 | } | 186 | } | |
187 | 187 | |||
188 | static int | 188 | static int | |
189 | lowlevel_rsa_public_decrypt(const uint8_t *encbuf, int enclen, uint8_t *dec, const rsa_pubkey_t *rsa) | 189 | lowlevel_rsa_public_decrypt(const uint8_t *encbuf, int enclen, uint8_t *dec, const rsa_pubkey_t *rsa) | |
190 | { | 190 | { | |
191 | uint8_t *decbuf; | 191 | uint8_t *decbuf; | |
192 | BIGNUM *decbn; | 192 | PGPV_BIGNUM *decbn; | |
193 | BIGNUM *encbn; | 193 | PGPV_BIGNUM *encbn; | |
194 | int decbytes; | 194 | int decbytes; | |
195 | int nbytes; | 195 | int nbytes; | |
196 | int r; | 196 | int r; | |
197 | 197 | |||
198 | nbytes = 0; | 198 | nbytes = 0; | |
199 | r = -1; | 199 | r = -1; | |
200 | decbuf = NULL; | 200 | decbuf = NULL; | |
201 | decbn = encbn = NULL; | 201 | decbn = encbn = NULL; | |
202 | if (BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) { | 202 | if (PGPV_BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) { | |
203 | printf("rsa r modulus too large\n"); | 203 | printf("rsa r modulus too large\n"); | |
204 | goto err; | 204 | goto err; | |
205 | } | 205 | } | |
206 | if (BN_cmp(rsa->n, rsa->e) <= 0) { | 206 | if (PGPV_BN_cmp(rsa->n, rsa->e) <= 0) { | |
207 | printf("rsa r bad n value\n"); | 207 | printf("rsa r bad n value\n"); | |
208 | goto err; | 208 | goto err; | |
209 | } | 209 | } | |
210 | if (BN_num_bits(rsa->n) > RSA_SMALL_MODULUS_BITS && | 210 | if (PGPV_BN_num_bits(rsa->n) > RSA_SMALL_MODULUS_BITS && | |
211 | BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS) { | 211 | PGPV_BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS) { | |
212 | printf("rsa r bad exponent limit\n"); | 212 | printf("rsa r bad exponent limit\n"); | |
213 | goto err; | 213 | goto err; | |
214 | } | 214 | } | |
215 | if ((encbn = BN_new()) == NULL || | 215 | if ((encbn = PGPV_BN_new()) == NULL || | |
216 | (decbn = BN_new()) == NULL || | 216 | (decbn = PGPV_BN_new()) == NULL || | |
217 | (decbuf = netpgp_allocate(1, nbytes = BN_num_bytes(rsa->n))) == NULL) { | 217 | (decbuf = netpgp_allocate(1, nbytes = PGPV_BN_num_bytes(rsa->n))) == NULL) { | |
218 | printf("allocation failure\n"); | 218 | printf("allocation failure\n"); | |
219 | goto err; | 219 | goto err; | |
220 | } | 220 | } | |
221 | if (enclen > nbytes) { | 221 | if (enclen > nbytes) { | |
222 | printf("rsa r > mod len\n"); | 222 | printf("rsa r > mod len\n"); | |
223 | goto err; | 223 | goto err; | |
224 | } | 224 | } | |
225 | if (BN_bin2bn(encbuf, enclen, encbn) == NULL) { | 225 | if (PGPV_BN_bin2bn(encbuf, enclen, encbn) == NULL) { | |
226 | printf("null encrypted BN\n"); | 226 | printf("null encrypted BN\n"); | |
227 | goto err; | 227 | goto err; | |
228 | } | 228 | } | |
229 | if (BN_cmp(encbn, rsa->n) >= 0) { | 229 | if (PGPV_BN_cmp(encbn, rsa->n) >= 0) { | |
230 | printf("rsa r data too large for modulus\n"); | 230 | printf("rsa r data too large for modulus\n"); | |
231 | goto err; | 231 | goto err; | |
232 | } | 232 | } | |
233 | if (BN_mod_exp(decbn, encbn, rsa->e, rsa->n, NULL) < 0) { | 233 | if (PGPV_BN_mod_exp(decbn, encbn, rsa->e, rsa->n, NULL) < 0) { | |
234 | printf("BN_mod_exp < 0\n"); | 234 | printf("PGPV_BN_mod_exp < 0\n"); | |
235 | goto err; | 235 | goto err; | |
236 | } | 236 | } | |
237 | decbytes = BN_num_bytes(decbn); | 237 | decbytes = PGPV_BN_num_bytes(decbn); | |
238 | (void) BN_bn2bin(decbn, decbuf); | 238 | (void) PGPV_BN_bn2bin(decbn, decbuf); | |
239 | if ((r = rsa_padding_check_none(dec, nbytes, decbuf, decbytes, 0)) < 0) { | 239 | if ((r = rsa_padding_check_none(dec, nbytes, decbuf, decbytes, 0)) < 0) { | |
240 | printf("rsa r padding check failed\n"); | 240 | printf("rsa r padding check failed\n"); | |
241 | } | 241 | } | |
242 | err: | 242 | err: | |
243 | BN_free(encbn); | 243 | PGPV_BN_free(encbn); | |
244 | BN_free(decbn); | 244 | PGPV_BN_free(decbn); | |
245 | if (decbuf != NULL) { | 245 | if (decbuf != NULL) { | |
246 | (void) memset(decbuf, 0x0, nbytes); | 246 | (void) memset(decbuf, 0x0, nbytes); | |
247 | netpgp_deallocate(decbuf, nbytes); | 247 | netpgp_deallocate(decbuf, nbytes); | |
248 | } | 248 | } | |
249 | return r; | 249 | return r; | |
250 | } | 250 | } | |
251 | 251 | |||
252 | #if 0 | 252 | #if 0 | |
253 | /** | 253 | /** | |
254 | @file rsa_make_key.c | 254 | @file rsa_make_key.c | |
255 | RSA key generation, Tom St Denis | 255 | RSA key generation, Tom St Denis | |
256 | */ | 256 | */ | |
257 | 257 | |||
@@ -397,197 +397,197 @@ errkey: | @@ -397,197 +397,197 @@ errkey: | |||
397 | cleanup: | 397 | cleanup: | |
398 | mp_clear_multi(tmp3, tmp2, tmp1, p, q, NULL); | 398 | mp_clear_multi(tmp3, tmp2, tmp1, p, q, NULL); | |
399 | return err; | 399 | return err; | |
400 | } | 400 | } | |
401 | #endif | 401 | #endif | |
402 | 402 | |||
403 | #define HASHBUF_LEN 512 | 403 | #define HASHBUF_LEN 512 | |
404 | 404 | |||
405 | #define DSA_MAX_MODULUS_BITS 10000 | 405 | #define DSA_MAX_MODULUS_BITS 10000 | |
406 | 406 | |||
407 | static int | 407 | static int | |
408 | dsa_do_verify(const unsigned char *calculated, int dgst_len, const dsasig_t *sig, mpi_dsa_t *dsa) | 408 | dsa_do_verify(const unsigned char *calculated, int dgst_len, const dsasig_t *sig, mpi_dsa_t *dsa) | |
409 | { | 409 | { | |
410 | BIGNUM *M; | 410 | PGPV_BIGNUM *M; | |
411 | BIGNUM *W; | 411 | PGPV_BIGNUM *W; | |
412 | BIGNUM *t1; | 412 | PGPV_BIGNUM *t1; | |
413 | int ret = -1; | 413 | int ret = -1; | |
414 | int qbits; | 414 | int qbits; | |
415 | 415 | |||
416 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { | 416 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { | |
417 | return 0; | 417 | return 0; | |
418 | } | 418 | } | |
419 | M = W = t1 = NULL; | 419 | M = W = t1 = NULL; | |
420 | qbits = BN_num_bits(dsa->q); | 420 | qbits = PGPV_BN_num_bits(dsa->q); | |
421 | switch(qbits) { | 421 | switch(qbits) { | |
422 | case 160: | 422 | case 160: | |
423 | case 224: | 423 | case 224: | |
424 | case 256: | 424 | case 256: | |
425 | /* openssl sources say these are the valid values */ | 425 | /* openssl sources say these are the valid values */ | |
426 | /* according to FIPS 186-3 */ | 426 | /* according to FIPS 186-3 */ | |
427 | break; | 427 | break; | |
428 | default: | 428 | default: | |
429 | printf("dsa: bad # of Q bits\n"); | 429 | printf("dsa: bad # of Q bits\n"); | |
430 | return 0; | 430 | return 0; | |
431 | } | 431 | } | |
432 | if (BN_num_bits(dsa->p) > DSA_MAX_MODULUS_BITS) { | 432 | if (PGPV_BN_num_bits(dsa->p) > DSA_MAX_MODULUS_BITS) { | |
433 | printf("dsa: p too large\n"); | 433 | printf("dsa: p too large\n"); | |
434 | return 0; | 434 | return 0; | |
435 | } | 435 | } | |
436 | /* no love for SHA512? */ | 436 | /* no love for SHA512? */ | |
437 | if (dgst_len > SHA256_DIGEST_LENGTH) { | 437 | if (dgst_len > SHA256_DIGEST_LENGTH) { | |
438 | printf("dsa: digest too long\n"); | 438 | printf("dsa: digest too long\n"); | |
439 | return 0; | 439 | return 0; | |
440 | } | 440 | } | |
441 | ret = 0; | 441 | ret = 0; | |
442 | if ((M = BN_new()) == NULL || | 442 | if ((M = PGPV_BN_new()) == NULL || | |
443 | (W = BN_new()) == NULL || | 443 | (W = PGPV_BN_new()) == NULL || | |
444 | (t1 = BN_new()) == NULL) { | 444 | (t1 = PGPV_BN_new()) == NULL) { | |
445 | goto err; | 445 | goto err; | |
446 | } | 446 | } | |
447 | if (BN_is_zero(sig->r) || | 447 | if (PGPV_BN_is_zero(sig->r) || | |
448 | BN_is_negative(sig->r) || | 448 | PGPV_BN_is_negative(sig->r) || | |
449 | BN_cmp(sig->r, dsa->q) >= 0) { | 449 | PGPV_BN_cmp(sig->r, dsa->q) >= 0) { | |
450 | goto err; | 450 | goto err; | |
451 | } | 451 | } | |
452 | if (BN_is_zero(sig->s) || | 452 | if (PGPV_BN_is_zero(sig->s) || | |
453 | BN_is_negative(sig->s) || | 453 | PGPV_BN_is_negative(sig->s) || | |
454 | BN_cmp(sig->s, dsa->q) >= 0) { | 454 | PGPV_BN_cmp(sig->s, dsa->q) >= 0) { | |
455 | goto err; | 455 | goto err; | |
456 | } | 456 | } | |
457 | if (BN_mod_inverse(W, sig->s, dsa->q, NULL) != MP_OKAY) { | 457 | if (PGPV_BN_mod_inverse(W, sig->s, dsa->q, NULL) != MP_OKAY) { | |
458 | goto err; | 458 | goto err; | |
459 | } | 459 | } | |
460 | if (dgst_len > qbits / 8) { | 460 | if (dgst_len > qbits / 8) { | |
461 | dgst_len = qbits / 8; | 461 | dgst_len = qbits / 8; | |
462 | } | 462 | } | |
463 | if (BN_bin2bn(calculated, dgst_len, M) == NULL) { | 463 | if (PGPV_BN_bin2bn(calculated, dgst_len, M) == NULL) { | |
464 | goto err; | 464 | goto err; | |
465 | } | 465 | } | |
466 | if (!BN_mod_mul(M, M, W, dsa->q, NULL)) { | 466 | if (!PGPV_BN_mod_mul(M, M, W, dsa->q, NULL)) { | |
467 | goto err; | 467 | goto err; | |
468 | } | 468 | } | |
469 | if (!BN_mod_mul(W, sig->r, W, dsa->q, NULL)) { | 469 | if (!PGPV_BN_mod_mul(W, sig->r, W, dsa->q, NULL)) { | |
470 | goto err; | 470 | goto err; | |
471 | } | 471 | } | |
472 | if (!BN_mod_exp(dsa->p, t1, dsa->g, M, NULL)) { | 472 | if (!PGPV_BN_mod_exp(dsa->p, t1, dsa->g, M, NULL)) { | |
473 | goto err; | 473 | goto err; | |
474 | } | 474 | } | |
475 | if (!BN_div(NULL, M, t1, dsa->q, NULL)) { | 475 | if (!PGPV_BN_div(NULL, M, t1, dsa->q, NULL)) { | |
476 | goto err; | 476 | goto err; | |
477 | } | 477 | } | |
478 | ret = (BN_cmp(M, sig->r) == 0); | 478 | ret = (PGPV_BN_cmp(M, sig->r) == 0); | |
479 | err: | 479 | err: | |
480 | if (M) { | 480 | if (M) { | |
481 | BN_free(M); | 481 | PGPV_BN_free(M); | |
482 | } | 482 | } | |
483 | if (W) { | 483 | if (W) { | |
484 | BN_free(W); | 484 | PGPV_BN_free(W); | |
485 | } | 485 | } | |
486 | if (t1) { | 486 | if (t1) { | |
487 | BN_free(t1); | 487 | PGPV_BN_free(t1); | |
488 | } | 488 | } | |
489 | return ret; | 489 | return ret; | |
490 | } | 490 | } | |
491 | 491 | |||
492 | /*************************************************************************/ | 492 | /*************************************************************************/ | |
493 | 493 | |||
494 | int | 494 | int | |
495 | RSA_size(const RSA *rsa) | 495 | RSA_size(const RSA *rsa) | |
496 | { | 496 | { | |
497 | return (rsa == NULL) ? 0 : BN_num_bits(rsa->n); | 497 | return (rsa == NULL) ? 0 : PGPV_BN_num_bits(rsa->n); | |
498 | } | 498 | } | |
499 | 499 | |||
500 | int | 500 | int | |
501 | DSA_size(const DSA *dsa) | 501 | DSA_size(const DSA *dsa) | |
502 | { | 502 | { | |
503 | return (dsa == NULL) ? 0 : BN_num_bits(dsa->p); | 503 | return (dsa == NULL) ? 0 : PGPV_BN_num_bits(dsa->p); | |
504 | } | 504 | } | |
505 | 505 | |||
506 | unsigned | 506 | unsigned | |
507 | dsa_verify(const signature_t *signature, const dsa_pubkey_t *pubdsa, const uint8_t *calculated, size_t hash_length) | 507 | dsa_verify(const signature_t *signature, const dsa_pubkey_t *pubdsa, const uint8_t *calculated, size_t hash_length) | |
508 | { | 508 | { | |
509 | mpi_dsa_t odsa; | 509 | mpi_dsa_t odsa; | |
510 | dsasig_t osig; | 510 | dsasig_t osig; | |
511 | unsigned qlen; | 511 | unsigned qlen; | |
512 | int ret; | 512 | int ret; | |
513 | 513 | |||
514 | if (signature == NULL || pubdsa == NULL || calculated == NULL) { | 514 | if (signature == NULL || pubdsa == NULL || calculated == NULL) { | |
515 | return -1; | 515 | return -1; | |
516 | } | 516 | } | |
517 | (void) memset(&osig, 0x0, sizeof(osig)); | 517 | (void) memset(&osig, 0x0, sizeof(osig)); | |
518 | (void) memset(&odsa, 0x0, sizeof(odsa)); | 518 | (void) memset(&odsa, 0x0, sizeof(odsa)); | |
519 | BN_copy(osig.r, signature->dsa.r); | 519 | PGPV_BN_copy(osig.r, signature->dsa.r); | |
520 | BN_copy(osig.s, signature->dsa.s); | 520 | PGPV_BN_copy(osig.s, signature->dsa.s); | |
521 | odsa.p = pubdsa->p; | 521 | odsa.p = pubdsa->p; | |
522 | odsa.q = pubdsa->q; | 522 | odsa.q = pubdsa->q; | |
523 | odsa.g = pubdsa->g; | 523 | odsa.g = pubdsa->g; | |
524 | odsa.pub_key = pubdsa->y; | 524 | odsa.pub_key = pubdsa->y; | |
525 | if ((qlen = BN_num_bytes(odsa.q)) < hash_length) { | 525 | if ((qlen = PGPV_BN_num_bytes(odsa.q)) < hash_length) { | |
526 | hash_length = qlen; | 526 | hash_length = qlen; | |
527 | } | 527 | } | |
528 | ret = dsa_do_verify(calculated, (int)hash_length, &signature->dsa, &odsa); | 528 | ret = dsa_do_verify(calculated, (int)hash_length, &signature->dsa, &odsa); | |
529 | if (ret < 0) { | 529 | if (ret < 0) { | |
530 | return 0; | 530 | return 0; | |
531 | } | 531 | } | |
532 | BN_free(odsa.p); | 532 | PGPV_BN_free(odsa.p); | |
533 | BN_free(odsa.q); | 533 | PGPV_BN_free(odsa.q); | |
534 | BN_free(odsa.g); | 534 | PGPV_BN_free(odsa.g); | |
535 | BN_free(odsa.pub_key); | 535 | PGPV_BN_free(odsa.pub_key); | |
536 | odsa.p = odsa.q = odsa.g = odsa.pub_key = NULL; | 536 | odsa.p = odsa.q = odsa.g = odsa.pub_key = NULL; | |
537 | BN_free(osig.r); | 537 | PGPV_BN_free(osig.r); | |
538 | BN_free(osig.s); | 538 | PGPV_BN_free(osig.s); | |
539 | osig.r = osig.s = NULL; | 539 | osig.r = osig.s = NULL; | |
540 | return (unsigned)ret; | 540 | return (unsigned)ret; | |
541 | } | 541 | } | |
542 | 542 | |||
543 | RSA * | 543 | RSA * | |
544 | RSA_new(void) | 544 | RSA_new(void) | |
545 | { | 545 | { | |
546 | return netpgp_allocate(1, sizeof(RSA)); | 546 | return netpgp_allocate(1, sizeof(RSA)); | |
547 | } | 547 | } | |
548 | 548 | |||
549 | void | 549 | void | |
550 | RSA_free(RSA *rsa) | 550 | RSA_free(RSA *rsa) | |
551 | { | 551 | { | |
552 | if (rsa) { | 552 | if (rsa) { | |
553 | netpgp_deallocate(rsa, sizeof(*rsa)); | 553 | netpgp_deallocate(rsa, sizeof(*rsa)); | |
554 | } | 554 | } | |
555 | } | 555 | } | |
556 | 556 | |||
557 | int | 557 | int | |
558 | RSA_check_key(RSA *rsa) | 558 | RSA_check_key(RSA *rsa) | |
559 | { | 559 | { | |
560 | BIGNUM *calcn; | 560 | PGPV_BIGNUM *calcn; | |
561 | int ret; | 561 | int ret; | |
562 | 562 | |||
563 | ret = 0; | 563 | ret = 0; | |
564 | if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->n == NULL) { | 564 | if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->n == NULL) { | |
565 | return -1; | 565 | return -1; | |
566 | } | 566 | } | |
567 | /* check that p and q are coprime, and that n = p*q. */ | 567 | /* check that p and q are coprime, and that n = p*q. */ | |
568 | if (!BN_is_prime(rsa->p, 1, NULL, NULL, NULL) || | 568 | if (!PGPV_BN_is_prime(rsa->p, 1, NULL, NULL, NULL) || | |
569 | !BN_is_prime(rsa->q, 1, NULL, NULL, NULL)) { | 569 | !PGPV_BN_is_prime(rsa->q, 1, NULL, NULL, NULL)) { | |
570 | return 0; | 570 | return 0; | |
571 | } | 571 | } | |
572 | calcn = BN_new(); | 572 | calcn = PGPV_BN_new(); | |
573 | BN_mul(calcn, rsa->p, rsa->q, NULL); | 573 | PGPV_BN_mul(calcn, rsa->p, rsa->q, NULL); | |
574 | if (BN_cmp(calcn, rsa->n) != 0) { | 574 | if (PGPV_BN_cmp(calcn, rsa->n) != 0) { | |
575 | goto errout; | 575 | goto errout; | |
576 | } | 576 | } | |
577 | /* XXX - check that d*e = 1 mod (p-1*q-1) */ | 577 | /* XXX - check that d*e = 1 mod (p-1*q-1) */ | |
578 | ret = 1; | 578 | ret = 1; | |
579 | errout: | 579 | errout: | |
580 | BN_clear_free(calcn); | 580 | PGPV_BN_clear_free(calcn); | |
581 | return ret; | 581 | return ret; | |
582 | } | 582 | } | |
583 | 583 | |||
584 | RSA * | 584 | RSA * | |
585 | RSA_generate_key(int num, unsigned long e, void (*callback)(int,int,void *), void *cb_arg) | 585 | RSA_generate_key(int num, unsigned long e, void (*callback)(int,int,void *), void *cb_arg) | |
586 | { | 586 | { | |
587 | /* STUBBED */ | 587 | /* STUBBED */ | |
588 | USE_ARG(num); | 588 | USE_ARG(num); | |
589 | USE_ARG(e); | 589 | USE_ARG(e); | |
590 | USE_ARG(callback); | 590 | USE_ARG(callback); | |
591 | USE_ARG(cb_arg); | 591 | USE_ARG(cb_arg); | |
592 | printf("RSA_generate_key stubbed\n"); | 592 | printf("RSA_generate_key stubbed\n"); | |
593 | return RSA_new(); | 593 | return RSA_new(); | |
@@ -628,31 +628,31 @@ RSA_private_encrypt(int plainc, const un | @@ -628,31 +628,31 @@ RSA_private_encrypt(int plainc, const un | |||
628 | 628 | |||
629 | /* verify */ | 629 | /* verify */ | |
630 | int | 630 | int | |
631 | RSA_public_decrypt(int enclen, const unsigned char *enc, unsigned char *dec, RSA *rsa, int padding) | 631 | RSA_public_decrypt(int enclen, const unsigned char *enc, unsigned char *dec, RSA *rsa, int padding) | |
632 | { | 632 | { | |
633 | rsa_pubkey_t pub; | 633 | rsa_pubkey_t pub; | |
634 | int ret; | 634 | int ret; | |
635 | 635 | |||
636 | if (enc == NULL || dec == NULL || rsa == NULL) { | 636 | if (enc == NULL || dec == NULL || rsa == NULL) { | |
637 | return 0; | 637 | return 0; | |
638 | } | 638 | } | |
639 | USE_ARG(padding); | 639 | USE_ARG(padding); | |
640 | (void) memset(&pub, 0x0, sizeof(pub)); | 640 | (void) memset(&pub, 0x0, sizeof(pub)); | |
641 | pub.n = BN_dup(rsa->n); | 641 | pub.n = PGPV_BN_dup(rsa->n); | |
642 | pub.e = BN_dup(rsa->e); | 642 | pub.e = PGPV_BN_dup(rsa->e); | |
643 | ret = lowlevel_rsa_public_decrypt(enc, enclen, dec, &pub); | 643 | ret = lowlevel_rsa_public_decrypt(enc, enclen, dec, &pub); | |
644 | BN_free(pub.n); | 644 | PGPV_BN_free(pub.n); | |
645 | BN_free(pub.e); | 645 | PGPV_BN_free(pub.e); | |
646 | return ret; | 646 | return ret; | |
647 | } | 647 | } | |
648 | 648 | |||
649 | /***********************************************************************/ | 649 | /***********************************************************************/ | |
650 | 650 | |||
651 | DSA * | 651 | DSA * | |
652 | DSA_new(void) | 652 | DSA_new(void) | |
653 | { | 653 | { | |
654 | return netpgp_allocate(1, sizeof(DSA)); | 654 | return netpgp_allocate(1, sizeof(DSA)); | |
655 | } | 655 | } | |
656 | 656 | |||
657 | void | 657 | void | |
658 | DSA_free(DSA *dsa) | 658 | DSA_free(DSA *dsa) |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: sha1.c,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: sha1.c,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | /* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ | 2 | /* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */ | |
3 | 3 | |||
4 | /* | 4 | /* | |
5 | * SHA-1 in C | 5 | * SHA-1 in C | |
6 | * By Steve Reid <steve@edmweb.com> | 6 | * By Steve Reid <steve@edmweb.com> | |
7 | * 100% Public Domain | 7 | * 100% Public Domain | |
8 | * | 8 | * | |
9 | * Test Vectors (from FIPS PUB 180-1) | 9 | * Test Vectors (from FIPS PUB 180-1) | |
10 | * "abc" | 10 | * "abc" | |
11 | * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D | 11 | * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D | |
12 | * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" | 12 | * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" | |
13 | * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 | 13 | * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 | |
14 | * A million repetitions of "a" | 14 | * A million repetitions of "a" | |
@@ -107,27 +107,27 @@ void | @@ -107,27 +107,27 @@ void | |||
107 | do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) | 107 | do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block) | |
108 | { | 108 | { | |
109 | nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63); | 109 | nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63); | |
110 | nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); | 110 | nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); | |
111 | nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); | 111 | nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71); | |
112 | nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75); | 112 | nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75); | |
113 | nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); | 113 | nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79); | |
114 | } | 114 | } | |
115 | #endif | 115 | #endif | |
116 | 116 | |||
117 | /* | 117 | /* | |
118 | * Hash a single 512-bit block. This is the core of the algorithm. | 118 | * Hash a single 512-bit block. This is the core of the algorithm. | |
119 | */ | 119 | */ | |
120 | void SHA1Transform(uint32_t state[5], const uint8_t buffer[64]) | 120 | void netpgpv_SHA1Transform(uint32_t state[5], const uint8_t buffer[64]) | |
121 | { | 121 | { | |
122 | uint32_t a, b, c, d, e; | 122 | uint32_t a, b, c, d, e; | |
123 | CHAR64LONG16 *block; | 123 | CHAR64LONG16 *block; | |
124 | 124 | |||
125 | #ifdef SHA1HANDSOFF | 125 | #ifdef SHA1HANDSOFF | |
126 | CHAR64LONG16 workspace; | 126 | CHAR64LONG16 workspace; | |
127 | #endif | 127 | #endif | |
128 | 128 | |||
129 | #ifdef SHA1HANDSOFF | 129 | #ifdef SHA1HANDSOFF | |
130 | block = &workspace; | 130 | block = &workspace; | |
131 | (void)memcpy(block, buffer, 64); | 131 | (void)memcpy(block, buffer, 64); | |
132 | #else | 132 | #else | |
133 | block = (CHAR64LONG16 *)(void *)buffer; | 133 | block = (CHAR64LONG16 *)(void *)buffer; | |
@@ -174,75 +174,75 @@ void SHA1Transform(uint32_t state[5], co | @@ -174,75 +174,75 @@ void SHA1Transform(uint32_t state[5], co | |||
174 | state[1] += b; | 174 | state[1] += b; | |
175 | state[2] += c; | 175 | state[2] += c; | |
176 | state[3] += d; | 176 | state[3] += d; | |
177 | state[4] += e; | 177 | state[4] += e; | |
178 | 178 | |||
179 | /* Wipe variables */ | 179 | /* Wipe variables */ | |
180 | a = b = c = d = e = 0; | 180 | a = b = c = d = e = 0; | |
181 | } | 181 | } | |
182 | 182 | |||
183 | 183 | |||
184 | /* | 184 | /* | |
185 | * SHA1Init - Initialize new context | 185 | * SHA1Init - Initialize new context | |
186 | */ | 186 | */ | |
187 | void SHA1Init(SHA1_CTX *context) | 187 | void netpgpv_SHA1Init(NETPGPV_SHA1_CTX *context) | |
188 | { | 188 | { | |
189 | 189 | |||
190 | /* SHA1 initialization constants */ | 190 | /* SHA1 initialization constants */ | |
191 | context->state[0] = 0x67452301; | 191 | context->state[0] = 0x67452301; | |
192 | context->state[1] = 0xEFCDAB89; | 192 | context->state[1] = 0xEFCDAB89; | |
193 | context->state[2] = 0x98BADCFE; | 193 | context->state[2] = 0x98BADCFE; | |
194 | context->state[3] = 0x10325476; | 194 | context->state[3] = 0x10325476; | |
195 | context->state[4] = 0xC3D2E1F0; | 195 | context->state[4] = 0xC3D2E1F0; | |
196 | context->count[0] = context->count[1] = 0; | 196 | context->count[0] = context->count[1] = 0; | |
197 | } | 197 | } | |
198 | 198 | |||
199 | 199 | |||
200 | /* | 200 | /* | |
201 | * Run your data through this. | 201 | * Run your data through this. | |
202 | */ | 202 | */ | |
203 | void SHA1Update(SHA1_CTX *context, const uint8_t *data, unsigned int len) | 203 | void netpgpv_SHA1Update(NETPGPV_SHA1_CTX *context, const uint8_t *data, unsigned int len) | |
204 | { | 204 | { | |
205 | unsigned int i, j; | 205 | unsigned int i, j; | |
206 | 206 | |||
207 | j = context->count[0]; | 207 | j = context->count[0]; | |
208 | if ((context->count[0] += len << 3) < j) | 208 | if ((context->count[0] += len << 3) < j) | |
209 | context->count[1] += (len>>29)+1; | 209 | context->count[1] += (len>>29)+1; | |
210 | j = (j >> 3) & 63; | 210 | j = (j >> 3) & 63; | |
211 | if ((j + len) > 63) { | 211 | if ((j + len) > 63) { | |
212 | (void)memcpy(&context->buffer[j], data, (i = 64-j)); | 212 | (void)memcpy(&context->buffer[j], data, (i = 64-j)); | |
213 | SHA1Transform(context->state, context->buffer); | 213 | netpgpv_SHA1Transform(context->state, context->buffer); | |
214 | for ( ; i + 63 < len; i += 64) | 214 | for ( ; i + 63 < len; i += 64) | |
215 | SHA1Transform(context->state, &data[i]); | 215 | netpgpv_SHA1Transform(context->state, &data[i]); | |
216 | j = 0; | 216 | j = 0; | |
217 | } else { | 217 | } else { | |
218 | i = 0; | 218 | i = 0; | |
219 | } | 219 | } | |
220 | (void)memcpy(&context->buffer[j], &data[i], len - i); | 220 | (void)memcpy(&context->buffer[j], &data[i], len - i); | |
221 | } | 221 | } | |
222 | 222 | |||
223 | 223 | |||
224 | /* | 224 | /* | |
225 | * Add padding and return the message digest. | 225 | * Add padding and return the message digest. | |
226 | */ | 226 | */ | |
227 | void SHA1Final(uint8_t digest[20], SHA1_CTX *context) | 227 | void netpgpv_SHA1Final(uint8_t digest[20], NETPGPV_SHA1_CTX *context) | |
228 | { | 228 | { | |
229 | unsigned int i; | 229 | unsigned int i; | |
230 | uint8_t finalcount[8]; | 230 | uint8_t finalcount[8]; | |
231 | 231 | |||
232 | for (i = 0; i < 8; i++) { | 232 | for (i = 0; i < 8; i++) { | |
233 | finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] | 233 | finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] | |
234 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ | 234 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ | |
235 | } | 235 | } | |
236 | SHA1Update(context, (const uint8_t *)"\200", 1); | 236 | netpgpv_SHA1Update(context, (const uint8_t *)"\200", 1); | |
237 | while ((context->count[0] & 504) != 448) | 237 | while ((context->count[0] & 504) != 448) | |
238 | SHA1Update(context, (const uint8_t *)"\0", 1); | 238 | netpgpv_SHA1Update(context, (const uint8_t *)"\0", 1); | |
239 | SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ | 239 | netpgpv_SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ | |
240 | 240 | |||
241 | if (digest) { | 241 | if (digest) { | |
242 | for (i = 0; i < 20; i++) | 242 | for (i = 0; i < 20; i++) | |
243 | digest[i] = (uint8_t) | 243 | digest[i] = (uint8_t) | |
244 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); | 244 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); | |
245 | } | 245 | } | |
246 | } | 246 | } | |
247 | 247 | |||
248 | #endif /* HAVE_SHA1_H */ | 248 | #endif /* HAVE_SHA1_H */ |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: sha1.h,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: sha1.h,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * SHA-1 in C | 4 | * SHA-1 in C | |
5 | * By Steve Reid <steve@edmweb.com> | 5 | * By Steve Reid <steve@edmweb.com> | |
6 | * 100% Public Domain | 6 | * 100% Public Domain | |
7 | */ | 7 | */ | |
8 | 8 | |||
9 | #ifndef _SYS_SHA1_H_ | 9 | #ifndef _SYS_SHA1_H_ | |
10 | #define _SYS_SHA1_H_ | 10 | #define _SYS_SHA1_H_ | |
11 | 11 | |||
12 | #include <sys/types.h> | 12 | #include <sys/types.h> | |
13 | 13 | |||
14 | #include <inttypes.h> | 14 | #include <inttypes.h> | |
@@ -20,29 +20,29 @@ | @@ -20,29 +20,29 @@ | |||
20 | # if defined(__cplusplus) | 20 | # if defined(__cplusplus) | |
21 | # define __BEGIN_DECLS extern "C" { | 21 | # define __BEGIN_DECLS extern "C" { | |
22 | # define __END_DECLS } | 22 | # define __END_DECLS } | |
23 | # else | 23 | # else | |
24 | # define __BEGIN_DECLS | 24 | # define __BEGIN_DECLS | |
25 | # define __END_DECLS | 25 | # define __END_DECLS | |
26 | # endif | 26 | # endif | |
27 | #endif | 27 | #endif | |
28 | 28 | |||
29 | typedef struct { | 29 | typedef struct { | |
30 | uint32_t state[5]; | 30 | uint32_t state[5]; | |
31 | uint32_t count[2]; | 31 | uint32_t count[2]; | |
32 | uint8_t buffer[64]; | 32 | uint8_t buffer[64]; | |
33 | } SHA1_CTX; | 33 | } NETPGPV_SHA1_CTX; | |
34 | 34 | |||
35 | __BEGIN_DECLS | 35 | __BEGIN_DECLS | |
36 | void SHA1Transform(uint32_t[5], const uint8_t[64]); | 36 | void netpgpv_SHA1Transform(uint32_t[5], const uint8_t[64]); | |
37 | void SHA1Init(SHA1_CTX *); | 37 | void netpgpv_SHA1Init(NETPGPV_SHA1_CTX *); | |
38 | void SHA1Update(SHA1_CTX *, const uint8_t *, unsigned int); | 38 | void netpgpv_SHA1Update(NETPGPV_SHA1_CTX *, const uint8_t *, unsigned int); | |
39 | void SHA1Final(uint8_t[SHA1_DIGEST_LENGTH], SHA1_CTX *); | 39 | void netpgpv_SHA1Final(uint8_t[SHA1_DIGEST_LENGTH], NETPGPV_SHA1_CTX *); | |
40 | #ifndef _KERNEL | 40 | #ifndef _KERNEL | |
41 | char *SHA1End(SHA1_CTX *, char *); | 41 | char *netpgpv_SHA1End(NETPGPV_SHA1_CTX *, char *); | |
42 | char *SHA1FileChunk(const char *, char *, off_t, off_t); | 42 | char *netpgpv_SHA1FileChunk(const char *, char *, off_t, off_t); | |
43 | char *SHA1File(const char *, char *); | 43 | char *netpgpv_SHA1File(const char *, char *); | |
44 | char *SHA1Data(const uint8_t *, size_t, char *); | 44 | char *netpgpv_SHA1Data(const uint8_t *, size_t, char *); | |
45 | #endif /* _KERNEL */ | 45 | #endif /* _KERNEL */ | |
46 | __END_DECLS | 46 | __END_DECLS | |
47 | 47 | |||
48 | #endif /* _SYS_SHA1_H_ */ | 48 | #endif /* _SYS_SHA1_H_ */ |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: sha2.h,v 1.2 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: sha2.h,v 1.3 2015/09/01 19:38:42 agc Exp $ */ | |
2 | /* $KAME: sha2.h,v 1.4 2003/07/20 00:28:38 itojun Exp $ */ | 2 | /* $KAME: sha2.h,v 1.4 2003/07/20 00:28:38 itojun Exp $ */ | |
3 | 3 | |||
4 | /* | 4 | /* | |
5 | * sha2.h | 5 | * sha2.h | |
6 | * | 6 | * | |
7 | * Version 1.0.0beta1 | 7 | * Version 1.0.0beta1 | |
8 | * | 8 | * | |
9 | * Written by Aaron D. Gifford <me@aarongifford.com> | 9 | * Written by Aaron D. Gifford <me@aarongifford.com> | |
10 | * | 10 | * | |
11 | * Copyright 2000 Aaron D. Gifford. All rights reserved. | 11 | * Copyright 2000 Aaron D. Gifford. All rights reserved. | |
12 | * | 12 | * | |
13 | * Redistribution and use in source and binary forms, with or without | 13 | * Redistribution and use in source and binary forms, with or without | |
14 | * modification, are permitted provided that the following conditions | 14 | * modification, are permitted provided that the following conditions | |
@@ -56,73 +56,73 @@ | @@ -56,73 +56,73 @@ | |||
56 | #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) | 56 | #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) | |
57 | 57 | |||
58 | #ifndef __BEGIN_DECLS | 58 | #ifndef __BEGIN_DECLS | |
59 | # if defined(__cplusplus) | 59 | # if defined(__cplusplus) | |
60 | # define __BEGIN_DECLS extern "C" { | 60 | # define __BEGIN_DECLS extern "C" { | |
61 | # define __END_DECLS } | 61 | # define __END_DECLS } | |
62 | # else | 62 | # else | |
63 | # define __BEGIN_DECLS | 63 | # define __BEGIN_DECLS | |
64 | # define __END_DECLS | 64 | # define __END_DECLS | |
65 | # endif | 65 | # endif | |
66 | #endif | 66 | #endif | |
67 | 67 | |||
68 | /*** SHA-256/384/512 Context Structures *******************************/ | 68 | /*** SHA-256/384/512 Context Structures *******************************/ | |
69 | typedef struct _SHA256_CTX { | 69 | typedef struct _NETPGPV_SHA256_CTX { | |
70 | uint32_t state[8]; | 70 | uint32_t state[8]; | |
71 | uint64_t bitcount; | 71 | uint64_t bitcount; | |
72 | uint8_t buffer[SHA256_BLOCK_LENGTH]; | 72 | uint8_t buffer[SHA256_BLOCK_LENGTH]; | |
73 | } SHA256_CTX; | 73 | } NETPGPV_SHA256_CTX; | |
74 | 74 | |||
75 | typedef struct _SHA512_CTX { | 75 | typedef struct _NETPGPV_SHA512_CTX { | |
76 | uint64_t state[8]; | 76 | uint64_t state[8]; | |
77 | uint64_t bitcount[2]; | 77 | uint64_t bitcount[2]; | |
78 | uint8_t buffer[SHA512_BLOCK_LENGTH]; | 78 | uint8_t buffer[SHA512_BLOCK_LENGTH]; | |
79 | } SHA512_CTX; | 79 | } NETPGPV_SHA512_CTX; | |
80 | 80 | |||
81 | typedef SHA256_CTX SHA224_CTX; | 81 | typedef NETPGPV_SHA256_CTX NETPGPV_SHA224_CTX; | |
82 | typedef SHA512_CTX SHA384_CTX; | 82 | typedef NETPGPV_SHA512_CTX NETPGPV_SHA384_CTX; | |
83 | 83 | |||
84 | 84 | |||
85 | /*** SHA-256/384/512 Function Prototypes ******************************/ | 85 | /*** SHA-256/384/512 Function Prototypes ******************************/ | |
86 | __BEGIN_DECLS | 86 | __BEGIN_DECLS | |
87 | int SHA224_Init(SHA224_CTX *); | 87 | int netpgpv_SHA224_Init(NETPGPV_SHA224_CTX *); | |
88 | int SHA224_Update(SHA224_CTX*, const uint8_t*, size_t); | 88 | int netpgpv_SHA224_Update(NETPGPV_SHA224_CTX*, const uint8_t*, size_t); | |
89 | int SHA224_Final(uint8_t[SHA224_DIGEST_LENGTH], SHA224_CTX*); | 89 | int netpgpv_SHA224_Final(uint8_t[SHA224_DIGEST_LENGTH], NETPGPV_SHA224_CTX*); | |
90 | #ifndef _KERNEL | 90 | #ifndef _KERNEL | |
91 | char *SHA224_End(SHA224_CTX *, char[SHA224_DIGEST_STRING_LENGTH]); | 91 | char *netpgpv_SHA224_End(NETPGPV_SHA224_CTX *, char[SHA224_DIGEST_STRING_LENGTH]); | |
92 | char *SHA224_FileChunk(const char *, char *, off_t, off_t); | 92 | char *netpgpv_SHA224_FileChunk(const char *, char *, off_t, off_t); | |
93 | char *SHA224_File(const char *, char *); | 93 | char *netpgpv_SHA224_File(const char *, char *); | |
94 | char *SHA224_Data(const uint8_t *, size_t, char[SHA224_DIGEST_STRING_LENGTH]); | 94 | char *netpgpv_SHA224_Data(const uint8_t *, size_t, char[SHA224_DIGEST_STRING_LENGTH]); | |
95 | #endif /* !_KERNEL */ | 95 | #endif /* !_KERNEL */ | |
96 | 96 | |||
97 | int SHA256_Init(SHA256_CTX *); | 97 | int netpgpv_SHA256_Init(NETPGPV_SHA256_CTX *); | |
98 | int SHA256_Update(SHA256_CTX*, const uint8_t*, size_t); | 98 | int netpgpv_SHA256_Update(NETPGPV_SHA256_CTX*, const uint8_t*, size_t); | |
99 | int SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); | 99 | int netpgpv_SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], NETPGPV_SHA256_CTX*); | |
100 | #ifndef _KERNEL | 100 | #ifndef _KERNEL | |
101 | char *SHA256_End(SHA256_CTX *, char[SHA256_DIGEST_STRING_LENGTH]); | 101 | char *netpgpv_SHA256_End(NETPGPV_SHA256_CTX *, char[SHA256_DIGEST_STRING_LENGTH]); | |
102 | char *SHA256_FileChunk(const char *, char *, off_t, off_t); | 102 | char *netpgpv_SHA256_FileChunk(const char *, char *, off_t, off_t); | |
103 | char *SHA256_File(const char *, char *); | 103 | char *netpgpv_SHA256_File(const char *, char *); | |
104 | char *SHA256_Data(const uint8_t *, size_t, char[SHA256_DIGEST_STRING_LENGTH]); | 104 | char *netpgpv_SHA256_Data(const uint8_t *, size_t, char[SHA256_DIGEST_STRING_LENGTH]); | |
105 | #endif /* !_KERNEL */ | 105 | #endif /* !_KERNEL */ | |
106 | 106 | |||
107 | int SHA384_Init(SHA384_CTX*); | 107 | int netpgpv_SHA384_Init(NETPGPV_SHA384_CTX*); | |
108 | int SHA384_Update(SHA384_CTX*, const uint8_t*, size_t); | 108 | int netpgpv_SHA384_Update(NETPGPV_SHA384_CTX*, const uint8_t*, size_t); | |
109 | int SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); | 109 | int netpgpv_SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], NETPGPV_SHA384_CTX*); | |
110 | #ifndef _KERNEL | 110 | #ifndef _KERNEL | |
111 | char *SHA384_End(SHA384_CTX *, char[SHA384_DIGEST_STRING_LENGTH]); | 111 | char *netpgpv_SHA384_End(NETPGPV_SHA384_CTX *, char[SHA384_DIGEST_STRING_LENGTH]); | |
112 | char *SHA384_FileChunk(const char *, char *, off_t, off_t); | 112 | char *netpgpv_SHA384_FileChunk(const char *, char *, off_t, off_t); | |
113 | char *SHA384_File(const char *, char *); | 113 | char *netpgpv_SHA384_File(const char *, char *); | |
114 | char *SHA384_Data(const uint8_t *, size_t, char[SHA384_DIGEST_STRING_LENGTH]); | 114 | char *netpgpv_SHA384_Data(const uint8_t *, size_t, char[SHA384_DIGEST_STRING_LENGTH]); | |
115 | #endif /* !_KERNEL */ | 115 | #endif /* !_KERNEL */ | |
116 | 116 | |||
117 | int SHA512_Init(SHA512_CTX*); | 117 | int netpgpv_SHA512_Init(NETPGPV_SHA512_CTX*); | |
118 | int SHA512_Update(SHA512_CTX*, const uint8_t*, size_t); | 118 | int netpgpv_SHA512_Update(NETPGPV_SHA512_CTX*, const uint8_t*, size_t); | |
119 | int SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); | 119 | int NETPGPV_SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], NETPGPV_SHA512_CTX*); | |
120 | #ifndef _KERNEL | 120 | #ifndef _KERNEL | |
121 | char *SHA512_End(SHA512_CTX *, char[SHA512_DIGEST_STRING_LENGTH]); | 121 | char *netpgpv_SHA512_End(NETPGPV_SHA512_CTX *, char[SHA512_DIGEST_STRING_LENGTH]); | |
122 | char *SHA512_FileChunk(const char *, char *, off_t, off_t); | 122 | char *netpgpv_SHA512_FileChunk(const char *, char *, off_t, off_t); | |
123 | char *SHA512_File(const char *, char *); | 123 | char *netpgpv_SHA512_File(const char *, char *); | |
124 | char *SHA512_Data(const uint8_t *, size_t, char[SHA512_DIGEST_STRING_LENGTH]); | 124 | char *netpgpv_SHA512_Data(const uint8_t *, size_t, char[SHA512_DIGEST_STRING_LENGTH]); | |
125 | #endif /* !_KERNEL */ | 125 | #endif /* !_KERNEL */ | |
126 | __END_DECLS | 126 | __END_DECLS | |
127 | 127 | |||
128 | #endif /* __SHA2_H__ */ | 128 | #endif /* __SHA2_H__ */ |
@@ -709,75 +709,75 @@ sprint_uint64(char *buf, uint64_t val) | @@ -709,75 +709,75 @@ sprint_uint64(char *buf, uint64_t val) | |||
709 | for (i = 0; i < 8; ++i) { | 709 | for (i = 0; i < 8; ++i) { | |
710 | if (IS_LITTLE_ENDIAN(indian)) { | 710 | if (IS_LITTLE_ENDIAN(indian)) { | |
711 | buf[2 * (7 - i)] = hexdigits[(val >> (56 - 8 * i + 4)) & 15]; | 711 | buf[2 * (7 - i)] = hexdigits[(val >> (56 - 8 * i + 4)) & 15]; | |
712 | buf[(2 * (7 - i)) + 1] = hexdigits[(val >> (56 - 8 * i)) & 15]; | 712 | buf[(2 * (7 - i)) + 1] = hexdigits[(val >> (56 - 8 * i)) & 15]; | |
713 | } else { | 713 | } else { | |
714 | buf[2 * i] = hexdigits[(val >> (56 - 8 * i + 4)) & 15]; | 714 | buf[2 * i] = hexdigits[(val >> (56 - 8 * i + 4)) & 15]; | |
715 | buf[(2 * i) + 1] = hexdigits[(val >> (56 - 8 * i)) & 15]; | 715 | buf[(2 * i) + 1] = hexdigits[(val >> (56 - 8 * i)) & 15]; | |
716 | } | 716 | } | |
717 | } | 717 | } | |
718 | } | 718 | } | |
719 | 719 | |||
720 | /* common function to initialise context */ | 720 | /* common function to initialise context */ | |
721 | static void | 721 | static void | |
722 | initcontext(TIGER_CTX *ctx, uint8_t pad) | 722 | initcontext(NETPGPV_TIGER_CTX *ctx, uint8_t pad) | |
723 | { | 723 | { | |
724 | (void) memset(ctx, 0x0, sizeof(*ctx)); | 724 | (void) memset(ctx, 0x0, sizeof(*ctx)); | |
725 | ctx->ctx[0] = 0x0123456789ABCDEFLL; | 725 | ctx->ctx[0] = 0x0123456789ABCDEFLL; | |
726 | ctx->ctx[1] = 0xFEDCBA9876543210LL; | 726 | ctx->ctx[1] = 0xFEDCBA9876543210LL; | |
727 | ctx->ctx[2] = 0xF096A5B4C3B2E187LL; | 727 | ctx->ctx[2] = 0xF096A5B4C3B2E187LL; | |
728 | ctx->init = 1; | 728 | ctx->init = 1; | |
729 | ctx->pad = pad; | 729 | ctx->pad = pad; | |
730 | } | 730 | } | |
731 | 731 | |||
732 | /* set the version number (0 is same as 1 for Tiger) */ | 732 | /* set the version number (0 is same as 1 for Tiger) */ | |
733 | static int | 733 | static int | |
734 | setversion(TIGER_CTX *ctx, int version) | 734 | setversion(NETPGPV_TIGER_CTX *ctx, int version) | |
735 | { | 735 | { | |
736 | switch(version) { | 736 | switch(version) { | |
737 | case 0: | 737 | case 0: | |
738 | case 1: | 738 | case 1: | |
739 | initcontext(ctx, 0x01); | 739 | initcontext(ctx, 0x01); | |
740 | break; | 740 | break; | |
741 | case 2: | 741 | case 2: | |
742 | initcontext(ctx, 0x80); | 742 | initcontext(ctx, 0x80); | |
743 | break; | 743 | break; | |
744 | default: | 744 | default: | |
745 | (void) fprintf(stderr, "unknown version %d\n", version); | 745 | (void) fprintf(stderr, "unknown version %d\n", version); | |
746 | return 0; | 746 | return 0; | |
747 | } | 747 | } | |
748 | return 1; | 748 | return 1; | |
749 | } | 749 | } | |
750 | 750 | |||
751 | /*****************************************************************************/ | 751 | /*****************************************************************************/ | |
752 | 752 | |||
753 | void | 753 | void | |
754 | TIGER_Init(TIGER_CTX *ctx) | 754 | netpgpv_TIGER_Init(NETPGPV_TIGER_CTX *ctx) | |
755 | { | 755 | { | |
756 | if (ctx) { | 756 | if (ctx) { | |
757 | initcontext(ctx, 0x01); | 757 | initcontext(ctx, 0x01); | |
758 | } | 758 | } | |
759 | } | 759 | } | |
760 | 760 | |||
761 | void | 761 | void | |
762 | TIGER2_Init(TIGER_CTX *ctx) | 762 | netpgpv_TIGER2_Init(NETPGPV_TIGER_CTX *ctx) | |
763 | { | 763 | { | |
764 | if (ctx) { | 764 | if (ctx) { | |
765 | initcontext(ctx, 0x80); | 765 | initcontext(ctx, 0x80); | |
766 | } | 766 | } | |
767 | } | 767 | } | |
768 | 768 | |||
769 | void | 769 | void | |
770 | TIGER_Update(TIGER_CTX *ctx, const void *data, size_t length) | 770 | netpgpv_TIGER_Update(NETPGPV_TIGER_CTX *ctx, const void *data, size_t length) | |
771 | { | 771 | { | |
772 | const uint64_t *str = (const uint64_t *)data; | 772 | const uint64_t *str = (const uint64_t *)data; | |
773 | uint64_t i; | 773 | uint64_t i; | |
774 | uint64_t j; | 774 | uint64_t j; | |
775 | union { | 775 | union { | |
776 | uint8_t temp8[64]; | 776 | uint8_t temp8[64]; | |
777 | uint64_t temp64[8]; | 777 | uint64_t temp64[8]; | |
778 | } u; | 778 | } u; | |
779 | int indian = 1; | 779 | int indian = 1; | |
780 | 780 | |||
781 | if (ctx == NULL || data == NULL) { | 781 | if (ctx == NULL || data == NULL) { | |
782 | return; | 782 | return; | |
783 | } | 783 | } | |
@@ -814,93 +814,93 @@ TIGER_Update(TIGER_CTX *ctx, const void | @@ -814,93 +814,93 @@ TIGER_Update(TIGER_CTX *ctx, const void | |||
814 | u.temp8[j] = 0; | 814 | u.temp8[j] = 0; | |
815 | } | 815 | } | |
816 | tiger_compress(u.temp64, ctx->ctx); | 816 | tiger_compress(u.temp64, ctx->ctx); | |
817 | j = 0; | 817 | j = 0; | |
818 | } | 818 | } | |
819 | for (; j < 56; j++) { | 819 | for (; j < 56; j++) { | |
820 | u.temp8[j] = 0; | 820 | u.temp8[j] = 0; | |
821 | } | 821 | } | |
822 | u.temp64[7] = ((uint64_t)length) << 3; | 822 | u.temp64[7] = ((uint64_t)length) << 3; | |
823 | tiger_compress(u.temp64, ctx->ctx); | 823 | tiger_compress(u.temp64, ctx->ctx); | |
824 | } | 824 | } | |
825 | 825 | |||
826 | void | 826 | void | |
827 | TIGER_Final(uint8_t *digest, TIGER_CTX *ctx) | 827 | netpgpv_TIGER_Final(uint8_t *digest, NETPGPV_TIGER_CTX *ctx) | |
828 | { | 828 | { | |
829 | uint64_t le[3]; | 829 | uint64_t le[3]; | |
830 | int indian = 1; | 830 | int indian = 1; | |
831 | int i; | 831 | int i; | |
832 | 832 | |||
833 | if (digest == NULL || ctx == NULL) { | 833 | if (digest == NULL || ctx == NULL) { | |
834 | return; | 834 | return; | |
835 | } | 835 | } | |
836 | if (!ctx->init) { | 836 | if (!ctx->init) { | |
837 | TIGER_Init(ctx); | 837 | netpgpv_TIGER_Init(ctx); | |
838 | TIGER_Update(ctx, NULL, 0); | 838 | netpgpv_TIGER_Update(ctx, NULL, 0); | |
839 | } | 839 | } | |
840 | if (IS_LITTLE_ENDIAN(indian)) { | 840 | if (IS_LITTLE_ENDIAN(indian)) { | |
841 | for (i = 0; i < 3; ++i) { | 841 | for (i = 0; i < 3; ++i) { | |
842 | le[i] = (uint64_t)BSWAP64(ctx->ctx[i]); | 842 | le[i] = (uint64_t)BSWAP64(ctx->ctx[i]); | |
843 | } | 843 | } | |
844 | (void) memcpy(digest, le, 3 * sizeof(le[0])); | 844 | (void) memcpy(digest, le, 3 * sizeof(le[0])); | |
845 | } else { | 845 | } else { | |
846 | (void) memcpy(digest, ctx->ctx, 3 * sizeof(ctx->ctx[0])); | 846 | (void) memcpy(digest, ctx->ctx, 3 * sizeof(ctx->ctx[0])); | |
847 | } | 847 | } | |
848 | } | 848 | } | |
849 | 849 | |||
850 | char * | 850 | char * | |
851 | TIGER_End(TIGER_CTX *ctx, char *buf) | 851 | netpgpv_TIGER_End(NETPGPV_TIGER_CTX *ctx, char *buf) | |
852 | { | 852 | { | |
853 | int i; | 853 | int i; | |
854 | 854 | |||
855 | if (ctx == NULL) { | 855 | if (ctx == NULL) { | |
856 | return NULL; | 856 | return NULL; | |
857 | } | 857 | } | |
858 | if (buf == NULL && (buf = calloc(1, 49)) == NULL) { | 858 | if (buf == NULL && (buf = calloc(1, 49)) == NULL) { | |
859 | return NULL; | 859 | return NULL; | |
860 | } | 860 | } | |
861 | if (!ctx->init) { | 861 | if (!ctx->init) { | |
862 | TIGER_Init(ctx); | 862 | netpgpv_TIGER_Init(ctx); | |
863 | TIGER_Update(ctx, NULL, 0); | 863 | netpgpv_TIGER_Update(ctx, NULL, 0); | |
864 | } | 864 | } | |
865 | for (i = 0; i < 3; ++i) { | 865 | for (i = 0; i < 3; ++i) { | |
866 | sprint_uint64(buf + i * 16, ctx->ctx[i]); | 866 | sprint_uint64(buf + i * 16, ctx->ctx[i]); | |
867 | } | 867 | } | |
868 | buf[16 * i] = 0x0; | 868 | buf[16 * i] = 0x0; | |
869 | return buf; | 869 | return buf; | |
870 | } | 870 | } | |
871 | 871 | |||
872 | char * | 872 | char * | |
873 | TIGER_File(char *filename, char *buf, int version) | 873 | netpgpv_TIGER_File(char *filename, char *buf, int version) | |
874 | { | 874 | { | |
875 | TIGER_CTX ctx; | 875 | NETPGPV_TIGER_CTX ctx; | |
876 | uint8_t buffer[BUFSIZ]; | 876 | uint8_t buffer[BUFSIZ]; | |
877 | ssize_t num; | 877 | ssize_t num; | |
878 | int fd; | 878 | int fd; | |
879 | int oerrno; | 879 | int oerrno; | |
880 | 880 | |||
881 | if (filename == NULL || buf == NULL || !setversion(&ctx, version)) { | 881 | if (filename == NULL || buf == NULL || !setversion(&ctx, version)) { | |
882 | return NULL; | 882 | return NULL; | |
883 | } | 883 | } | |
884 | if ((fd = open(filename, O_RDONLY)) < 0) { | 884 | if ((fd = open(filename, O_RDONLY)) < 0) { | |
885 | return NULL; | 885 | return NULL; | |
886 | } | 886 | } | |
887 | while ((num = read(fd, buffer, sizeof(buffer))) > 0) { | 887 | while ((num = read(fd, buffer, sizeof(buffer))) > 0) { | |
888 | TIGER_Update(&ctx, buffer, (size_t)num); | 888 | netpgpv_TIGER_Update(&ctx, buffer, (size_t)num); | |
889 | } | 889 | } | |
890 | oerrno = errno; | 890 | oerrno = errno; | |
891 | close(fd); | 891 | close(fd); | |
892 | errno = oerrno; | 892 | errno = oerrno; | |
893 | return (num < 0) ? NULL : TIGER_End(&ctx, buf); | 893 | return (num < 0) ? NULL : netpgpv_TIGER_End(&ctx, buf); | |
894 | } | 894 | } | |
895 | 895 | |||
896 | char * | 896 | char * | |
897 | TIGER_Data(const uint8_t *data, size_t len, char *buf, int version) | 897 | netpgpv_TIGER_Data(const uint8_t *data, size_t len, char *buf, int version) | |
898 | { | 898 | { | |
899 | TIGER_CTX ctx; | 899 | NETPGPV_TIGER_CTX ctx; | |
900 | 900 | |||
901 | if (data == NULL || buf == NULL || !setversion(&ctx, version)) { | 901 | if (data == NULL || buf == NULL || !setversion(&ctx, version)) { | |
902 | return NULL; | 902 | return NULL; | |
903 | } | 903 | } | |
904 | TIGER_Update(&ctx, data, len); | 904 | netpgpv_TIGER_Update(&ctx, data, len); | |
905 | return TIGER_End(&ctx, buf); | 905 | return netpgpv_TIGER_End(&ctx, buf); | |
906 | } | 906 | } |
@@ -104,69 +104,69 @@ digest_alg_size(unsigned alg) | @@ -104,69 +104,69 @@ digest_alg_size(unsigned alg) | |||
104 | return 0; | 104 | return 0; | |
105 | } | 105 | } | |
106 | } | 106 | } | |
107 | 107 | |||
108 | /* initialise the hash structure */ | 108 | /* initialise the hash structure */ | |
109 | int | 109 | int | |
110 | digest_init(digest_t *hash, const uint32_t hashalg) | 110 | digest_init(digest_t *hash, const uint32_t hashalg) | |
111 | { | 111 | { | |
112 | if (hash == NULL) { | 112 | if (hash == NULL) { | |
113 | return 0; | 113 | return 0; | |
114 | } | 114 | } | |
115 | switch(hash->alg = hashalg) { | 115 | switch(hash->alg = hashalg) { | |
116 | case MD5_HASH_ALG: | 116 | case MD5_HASH_ALG: | |
117 | MD5Init(&hash->u.md5ctx); | 117 | netpgpv_MD5Init(&hash->u.md5ctx); | |
118 | hash->size = 16; | 118 | hash->size = 16; | |
119 | hash->prefix = prefix_md5; | 119 | hash->prefix = prefix_md5; | |
120 | hash->len = sizeof(prefix_md5); | 120 | hash->len = sizeof(prefix_md5); | |
121 | hash->ctx = &hash->u.md5ctx; | 121 | hash->ctx = &hash->u.md5ctx; | |
122 | return 1; | 122 | return 1; | |
123 | case SHA1_HASH_ALG: | 123 | case SHA1_HASH_ALG: | |
124 | SHA1Init(&hash->u.sha1ctx); | 124 | netpgpv_SHA1Init(&hash->u.sha1ctx); | |
125 | hash->size = 20; | 125 | hash->size = 20; | |
126 | hash->prefix = prefix_sha1; | 126 | hash->prefix = prefix_sha1; | |
127 | hash->len = sizeof(prefix_sha1); | 127 | hash->len = sizeof(prefix_sha1); | |
128 | hash->ctx = &hash->u.sha1ctx; | 128 | hash->ctx = &hash->u.sha1ctx; | |
129 | return 1; | 129 | return 1; | |
130 | case RIPEMD_HASH_ALG: | 130 | case RIPEMD_HASH_ALG: | |
131 | RMD160Init(&hash->u.rmd160ctx); | 131 | RMD160Init(&hash->u.rmd160ctx); | |
132 | hash->size = 20; | 132 | hash->size = 20; | |
133 | hash->prefix = prefix_rmd160; | 133 | hash->prefix = prefix_rmd160; | |
134 | hash->len = sizeof(prefix_rmd160); | 134 | hash->len = sizeof(prefix_rmd160); | |
135 | hash->ctx = &hash->u.rmd160ctx; | 135 | hash->ctx = &hash->u.rmd160ctx; | |
136 | return 1; | 136 | return 1; | |
137 | case SHA256_HASH_ALG: | 137 | case SHA256_HASH_ALG: | |
138 | SHA256_Init(&hash->u.sha256ctx); | 138 | netpgpv_SHA256_Init(&hash->u.sha256ctx); | |
139 | hash->size = 32; | 139 | hash->size = 32; | |
140 | hash->prefix = prefix_sha256; | 140 | hash->prefix = prefix_sha256; | |
141 | hash->len = sizeof(prefix_sha256); | 141 | hash->len = sizeof(prefix_sha256); | |
142 | hash->ctx = &hash->u.sha256ctx; | 142 | hash->ctx = &hash->u.sha256ctx; | |
143 | return 1; | 143 | return 1; | |
144 | case SHA512_HASH_ALG: | 144 | case SHA512_HASH_ALG: | |
145 | SHA512_Init(&hash->u.sha512ctx); | 145 | netpgpv_SHA512_Init(&hash->u.sha512ctx); | |
146 | hash->size = 64; | 146 | hash->size = 64; | |
147 | hash->prefix = prefix_sha512; | 147 | hash->prefix = prefix_sha512; | |
148 | hash->len = sizeof(prefix_sha512); | 148 | hash->len = sizeof(prefix_sha512); | |
149 | hash->ctx = &hash->u.sha512ctx; | 149 | hash->ctx = &hash->u.sha512ctx; | |
150 | return 1; | 150 | return 1; | |
151 | case TIGER_HASH_ALG: | 151 | case TIGER_HASH_ALG: | |
152 | TIGER_Init(&hash->u.tigerctx); | 152 | netpgpv_TIGER_Init(&hash->u.tigerctx); | |
153 | hash->size = TIGER_DIGEST_LENGTH; | 153 | hash->size = TIGER_DIGEST_LENGTH; | |
154 | hash->prefix = prefix_tiger; | 154 | hash->prefix = prefix_tiger; | |
155 | hash->len = sizeof(prefix_tiger); | 155 | hash->len = sizeof(prefix_tiger); | |
156 | hash->ctx = &hash->u.tigerctx; | 156 | hash->ctx = &hash->u.tigerctx; | |
157 | return 1; | 157 | return 1; | |
158 | case TIGER2_HASH_ALG: | 158 | case TIGER2_HASH_ALG: | |
159 | TIGER2_Init(&hash->u.tigerctx); | 159 | netpgpv_TIGER2_Init(&hash->u.tigerctx); | |
160 | hash->size = TIGER_DIGEST_LENGTH; | 160 | hash->size = TIGER_DIGEST_LENGTH; | |
161 | hash->prefix = prefix_tiger; | 161 | hash->prefix = prefix_tiger; | |
162 | hash->len = sizeof(prefix_tiger); | 162 | hash->len = sizeof(prefix_tiger); | |
163 | hash->ctx = &hash->u.tigerctx; | 163 | hash->ctx = &hash->u.tigerctx; | |
164 | return 1; | 164 | return 1; | |
165 | default: | 165 | default: | |
166 | printf("hash_any: bad algorithm\n"); | 166 | printf("hash_any: bad algorithm\n"); | |
167 | return 0; | 167 | return 0; | |
168 | } | 168 | } | |
169 | } | 169 | } | |
170 | 170 | |||
171 | typedef struct rec_t { | 171 | typedef struct rec_t { | |
172 | const char *s; | 172 | const char *s; | |
@@ -196,74 +196,74 @@ digest_get_alg(const char *hashalg) | @@ -196,74 +196,74 @@ digest_get_alg(const char *hashalg) | |||
196 | } | 196 | } | |
197 | } | 197 | } | |
198 | return 0; | 198 | return 0; | |
199 | } | 199 | } | |
200 | 200 | |||
201 | int | 201 | int | |
202 | digest_update(digest_t *hash, const uint8_t *data, size_t length) | 202 | digest_update(digest_t *hash, const uint8_t *data, size_t length) | |
203 | { | 203 | { | |
204 | if (hash == NULL || data == NULL) { | 204 | if (hash == NULL || data == NULL) { | |
205 | return 0; | 205 | return 0; | |
206 | } | 206 | } | |
207 | switch(hash->alg) { | 207 | switch(hash->alg) { | |
208 | case MD5_HASH_ALG: | 208 | case MD5_HASH_ALG: | |
209 | MD5Update(hash->ctx, data, (unsigned)length); | 209 | netpgpv_MD5Update(hash->ctx, data, (unsigned)length); | |
210 | return 1; | 210 | return 1; | |
211 | case SHA1_HASH_ALG: | 211 | case SHA1_HASH_ALG: | |
212 | SHA1Update(hash->ctx, data, (unsigned)length); | 212 | netpgpv_SHA1Update(hash->ctx, data, (unsigned)length); | |
213 | return 1; | 213 | return 1; | |
214 | case RIPEMD_HASH_ALG: | 214 | case RIPEMD_HASH_ALG: | |
215 | RMD160Update(hash->ctx, data, (unsigned)length); | 215 | RMD160Update(hash->ctx, data, (unsigned)length); | |
216 | return 1; | 216 | return 1; | |
217 | case SHA256_HASH_ALG: | 217 | case SHA256_HASH_ALG: | |
218 | SHA256_Update(hash->ctx, data, length); | 218 | netpgpv_SHA256_Update(hash->ctx, data, length); | |
219 | return 1; | 219 | return 1; | |
220 | case SHA512_HASH_ALG: | 220 | case SHA512_HASH_ALG: | |
221 | SHA512_Update(hash->ctx, data, length); | 221 | netpgpv_SHA512_Update(hash->ctx, data, length); | |
222 | return 1; | 222 | return 1; | |
223 | case TIGER_HASH_ALG: | 223 | case TIGER_HASH_ALG: | |
224 | case TIGER2_HASH_ALG: | 224 | case TIGER2_HASH_ALG: | |
225 | TIGER_Update(hash->ctx, data, length); | 225 | netpgpv_TIGER_Update(hash->ctx, data, length); | |
226 | return 1; | 226 | return 1; | |
227 | default: | 227 | default: | |
228 | printf("hash_any: bad algorithm\n"); | 228 | printf("hash_any: bad algorithm\n"); | |
229 | return 0; | 229 | return 0; | |
230 | } | 230 | } | |
231 | } | 231 | } | |
232 | 232 | |||
233 | unsigned | 233 | unsigned | |
234 | digest_final(uint8_t *out, digest_t *hash) | 234 | digest_final(uint8_t *out, digest_t *hash) | |
235 | { | 235 | { | |
236 | if (hash == NULL || out == NULL) { | 236 | if (hash == NULL || out == NULL) { | |
237 | return 0; | 237 | return 0; | |
238 | } | 238 | } | |
239 | switch(hash->alg) { | 239 | switch(hash->alg) { | |
240 | case MD5_HASH_ALG: | 240 | case MD5_HASH_ALG: | |
241 | MD5Final(out, hash->ctx); | 241 | netpgpv_MD5Final(out, hash->ctx); | |
242 | break; | 242 | break; | |
243 | case SHA1_HASH_ALG: | 243 | case SHA1_HASH_ALG: | |
244 | SHA1Final(out, hash->ctx); | 244 | netpgpv_SHA1Final(out, hash->ctx); | |
245 | break; | 245 | break; | |
246 | case RIPEMD_HASH_ALG: | 246 | case RIPEMD_HASH_ALG: | |
247 | RMD160Final(out, hash->ctx); | 247 | RMD160Final(out, hash->ctx); | |
248 | break; | 248 | break; | |
249 | case SHA256_HASH_ALG: | 249 | case SHA256_HASH_ALG: | |
250 | SHA256_Final(out, hash->ctx); | 250 | netpgpv_SHA256_Final(out, hash->ctx); | |
251 | break; | 251 | break; | |
252 | case SHA512_HASH_ALG: | 252 | case SHA512_HASH_ALG: | |
253 | SHA512_Final(out, hash->ctx); | 253 | netpgpv_SHA512_Final(out, hash->ctx); | |
254 | break; | 254 | break; | |
255 | case TIGER_HASH_ALG: | 255 | case TIGER_HASH_ALG: | |
256 | TIGER_Final(out, hash->ctx); | 256 | netpgpv_TIGER_Final(out, hash->ctx); | |
257 | break; | 257 | break; | |
258 | default: | 258 | default: | |
259 | printf("hash_any: bad algorithm\n"); | 259 | printf("hash_any: bad algorithm\n"); | |
260 | return 0; | 260 | return 0; | |
261 | } | 261 | } | |
262 | (void) memset(hash->ctx, 0x0, hash->size); | 262 | (void) memset(hash->ctx, 0x0, hash->size); | |
263 | return (unsigned)hash->size; | 263 | return (unsigned)hash->size; | |
264 | } | 264 | } | |
265 | 265 | |||
266 | int | 266 | int | |
267 | digest_length(digest_t *hash, unsigned hashedlen) | 267 | digest_length(digest_t *hash, unsigned hashedlen) | |
268 | { | 268 | { | |
269 | uint8_t trailer[6]; | 269 | uint8_t trailer[6]; |
@@ -52,32 +52,32 @@ __BEGIN_DECLS | @@ -52,32 +52,32 @@ __BEGIN_DECLS | |||
52 | #define RIPEMD_HASH_ALG 3 | 52 | #define RIPEMD_HASH_ALG 3 | |
53 | #define TIGER_HASH_ALG 6 /* from rfc2440 */ | 53 | #define TIGER_HASH_ALG 6 /* from rfc2440 */ | |
54 | #define SHA256_HASH_ALG 8 | 54 | #define SHA256_HASH_ALG 8 | |
55 | #define SHA384_HASH_ALG 9 | 55 | #define SHA384_HASH_ALG 9 | |
56 | #define SHA512_HASH_ALG 10 | 56 | #define SHA512_HASH_ALG 10 | |
57 | #define SHA224_HASH_ALG 11 | 57 | #define SHA224_HASH_ALG 11 | |
58 | #define TIGER2_HASH_ALG 100 /* private/experimental from rfc4880 */ | 58 | #define TIGER2_HASH_ALG 100 /* private/experimental from rfc4880 */ | |
59 | 59 | |||
60 | /* structure to describe digest methods */ | 60 | /* structure to describe digest methods */ | |
61 | typedef struct digest_t { | 61 | typedef struct digest_t { | |
62 | uint32_t alg; /* algorithm */ | 62 | uint32_t alg; /* algorithm */ | |
63 | size_t size; /* size */ | 63 | size_t size; /* size */ | |
64 | union { | 64 | union { | |
65 | MD5_CTX md5ctx; /* MD5 */ | 65 | NETPGPV_MD5_CTX md5ctx; /* MD5 */ | |
66 | SHA1_CTX sha1ctx; /* SHA1 */ | 66 | NETPGPV_SHA1_CTX sha1ctx; /* SHA1 */ | |
67 | RMD160_CTX rmd160ctx; /* RIPEMD */ | 67 | NETPGPV_RMD160_CTX rmd160ctx; /* RIPEMD */ | |
68 | SHA256_CTX sha256ctx; /* SHA256 */ | 68 | NETPGPV_SHA256_CTX sha256ctx; /* SHA256 */ | |
69 | SHA512_CTX sha512ctx; /* SHA512 */ | 69 | NETPGPV_SHA512_CTX sha512ctx; /* SHA512 */ | |
70 | TIGER_CTX tigerctx; /* TIGER/TIGER2 */ | 70 | NETPGPV_TIGER_CTX tigerctx; /* TIGER/TIGER2 */ | |
71 | } u; | 71 | } u; | |
72 | void *prefix; /* points to specific prefix */ | 72 | void *prefix; /* points to specific prefix */ | |
73 | uint32_t len; /* prefix length */ | 73 | uint32_t len; /* prefix length */ | |
74 | void *ctx; /* pointer to context array */ | 74 | void *ctx; /* pointer to context array */ | |
75 | } digest_t; | 75 | } digest_t; | |
76 | 76 | |||
77 | unsigned digest_get_alg(const char */*hashalg*/); | 77 | unsigned digest_get_alg(const char */*hashalg*/); | |
78 | 78 | |||
79 | int digest_init(digest_t */*digest*/, const uint32_t /*hashalg*/); | 79 | int digest_init(digest_t */*digest*/, const uint32_t /*hashalg*/); | |
80 | 80 | |||
81 | int digest_update(digest_t */*digest*/, const uint8_t */*data*/, size_t /*size*/); | 81 | int digest_update(digest_t */*digest*/, const uint8_t */*data*/, size_t /*size*/); | |
82 | unsigned digest_final(uint8_t */*out*/, digest_t */*digest*/); | 82 | unsigned digest_final(uint8_t */*out*/, digest_t */*digest*/); | |
83 | int digest_alg_size(unsigned /*alg*/); | 83 | int digest_alg_size(unsigned /*alg*/); |
@@ -30,73 +30,73 @@ | @@ -30,73 +30,73 @@ | |||
30 | #ifndef __BEGIN_DECLS | 30 | #ifndef __BEGIN_DECLS | |
31 | # if defined(__cplusplus) | 31 | # if defined(__cplusplus) | |
32 | # define __BEGIN_DECLS extern "C" { | 32 | # define __BEGIN_DECLS extern "C" { | |
33 | # define __END_DECLS } | 33 | # define __END_DECLS } | |
34 | # else | 34 | # else | |
35 | # define __BEGIN_DECLS | 35 | # define __BEGIN_DECLS | |
36 | # define __END_DECLS | 36 | # define __END_DECLS | |
37 | # endif | 37 | # endif | |
38 | #endif | 38 | #endif | |
39 | 39 | |||
40 | __BEGIN_DECLS | 40 | __BEGIN_DECLS | |
41 | 41 | |||
42 | typedef struct rsa_pubkey_t { | 42 | typedef struct rsa_pubkey_t { | |
43 | BIGNUM *n; /* RSA public modulus n */ | 43 | PGPV_BIGNUM *n; /* RSA public modulus n */ | |
44 | BIGNUM *e; /* RSA public encryption exponent e */ | 44 | PGPV_BIGNUM *e; /* RSA public encryption exponent e */ | |
45 | } rsa_pubkey_t; | 45 | } rsa_pubkey_t; | |
46 | 46 | |||
47 | typedef struct mpi_rsa_t { | 47 | typedef struct mpi_rsa_t { | |
48 | int f1; /* openssl pad */ | 48 | int f1; /* openssl pad */ | |
49 | long f2; /* openssl version */ | 49 | long f2; /* openssl version */ | |
50 | const void *f3; /* openssl method */ | 50 | const void *f3; /* openssl method */ | |
51 | void *f4; /* openssl engine */ | 51 | void *f4; /* openssl engine */ | |
52 | BIGNUM *n; | 52 | PGPV_BIGNUM *n; | |
53 | BIGNUM *e; | 53 | PGPV_BIGNUM *e; | |
54 | BIGNUM *d; | 54 | PGPV_BIGNUM *d; | |
55 | BIGNUM *p; | 55 | PGPV_BIGNUM *p; | |
56 | BIGNUM *q; | 56 | PGPV_BIGNUM *q; | |
57 | BIGNUM *dmp1; | 57 | PGPV_BIGNUM *dmp1; | |
58 | BIGNUM *dmq1; | 58 | PGPV_BIGNUM *dmq1; | |
59 | BIGNUM *iqmp; | 59 | PGPV_BIGNUM *iqmp; | |
60 | } mpi_rsa_t; | 60 | } mpi_rsa_t; | |
61 | 61 | |||
62 | #define RSA mpi_rsa_t | 62 | #define RSA mpi_rsa_t | |
63 | 63 | |||
64 | typedef struct dsa_pubkey_t { | 64 | typedef struct dsa_pubkey_t { | |
65 | BIGNUM *p; /* DSA public modulus n */ | 65 | PGPV_BIGNUM *p; /* DSA public modulus n */ | |
66 | BIGNUM *q; /* DSA public encryption exponent e */ | 66 | PGPV_BIGNUM *q; /* DSA public encryption exponent e */ | |
67 | BIGNUM *g; | 67 | PGPV_BIGNUM *g; | |
68 | BIGNUM *y; | 68 | PGPV_BIGNUM *y; | |
69 | } dsa_pubkey_t; | 69 | } dsa_pubkey_t; | |
70 | 70 | |||
71 | typedef struct mpi_dsa_t { | 71 | typedef struct mpi_dsa_t { | |
72 | BIGNUM *p; | 72 | PGPV_BIGNUM *p; | |
73 | BIGNUM *q; | 73 | PGPV_BIGNUM *q; | |
74 | BIGNUM *g; | 74 | PGPV_BIGNUM *g; | |
75 | BIGNUM *y; | 75 | PGPV_BIGNUM *y; | |
76 | BIGNUM *x; | 76 | PGPV_BIGNUM *x; | |
77 | BIGNUM *pub_key; | 77 | PGPV_BIGNUM *pub_key; | |
78 | BIGNUM *priv_key; | 78 | PGPV_BIGNUM *priv_key; | |
79 | } mpi_dsa_t; | 79 | } mpi_dsa_t; | |
80 | 80 | |||
81 | #define DSA mpi_dsa_t | 81 | #define DSA mpi_dsa_t | |
82 | 82 | |||
83 | typedef struct rsasig_t { | 83 | typedef struct rsasig_t { | |
84 | BIGNUM *sig; /* mpi which is actual signature */ | 84 | PGPV_BIGNUM *sig; /* mpi which is actual signature */ | |
85 | } rsasig_t; | 85 | } rsasig_t; | |
86 | 86 | |||
87 | typedef struct dsasig_t { | 87 | typedef struct dsasig_t { | |
88 | BIGNUM *r; /* mpi which is actual signature */ | 88 | PGPV_BIGNUM *r; /* mpi which is actual signature */ | |
89 | BIGNUM *s; /* mpi which is actual signature */ | 89 | PGPV_BIGNUM *s; /* mpi which is actual signature */ | |
90 | } dsasig_t; | 90 | } dsasig_t; | |
91 | 91 | |||
92 | #define DSA_SIG dsasig_t | 92 | #define DSA_SIG dsasig_t | |
93 | 93 | |||
94 | /* misc defs */ | 94 | /* misc defs */ | |
95 | #define RSA_NO_PADDING 3 | 95 | #define RSA_NO_PADDING 3 | |
96 | 96 | |||
97 | #define SIGNETBSD_ID_SIZE 8 | 97 | #define SIGNETBSD_ID_SIZE 8 | |
98 | #define SIGNETBSD_NAME_SIZE 128 | 98 | #define SIGNETBSD_NAME_SIZE 128 | |
99 | 99 | |||
100 | #define RSA_PUBKEY_ALG 1 | 100 | #define RSA_PUBKEY_ALG 1 | |
101 | #define DSA_PUBKEY_ALG 17 | 101 | #define DSA_PUBKEY_ALG 17 | |
102 | 102 |
@@ -34,32 +34,32 @@ | @@ -34,32 +34,32 @@ | |||
34 | # define __BEGIN_DECLS extern "C" { | 34 | # define __BEGIN_DECLS extern "C" { | |
35 | # define __END_DECLS } | 35 | # define __END_DECLS } | |
36 | # else | 36 | # else | |
37 | # define __BEGIN_DECLS | 37 | # define __BEGIN_DECLS | |
38 | # define __END_DECLS | 38 | # define __END_DECLS | |
39 | # endif | 39 | # endif | |
40 | #endif | 40 | #endif | |
41 | 41 | |||
42 | __BEGIN_DECLS | 42 | __BEGIN_DECLS | |
43 | 43 | |||
44 | #define TIGER_DIGEST_LENGTH 24 | 44 | #define TIGER_DIGEST_LENGTH 24 | |
45 | #define TIGER_DIGEST_STRING_LENGTH ((TIGER_DIGEST_LENGTH * 2) + 1) | 45 | #define TIGER_DIGEST_STRING_LENGTH ((TIGER_DIGEST_LENGTH * 2) + 1) | |
46 | 46 | |||
47 | typedef struct TIGER_CTX { | 47 | typedef struct NETPGPV_TIGER_CTX { | |
48 | uint64_t ctx[3]; | 48 | uint64_t ctx[3]; | |
49 | int init; | 49 | int init; | |
50 | uint8_t pad; | 50 | uint8_t pad; | |
51 | } TIGER_CTX; | 51 | } NETPGPV_TIGER_CTX; | |
52 | 52 | |||
53 | void TIGER_Init(TIGER_CTX *); | 53 | void netpgpv_TIGER_Init(NETPGPV_TIGER_CTX *); | |
54 | void TIGER2_Init(TIGER_CTX *); | 54 | void netpgpv_TIGER2_Init(NETPGPV_TIGER_CTX *); | |
55 | void TIGER_Update(TIGER_CTX *, const void *, size_t); | 55 | void netpgpv_TIGER_Update(NETPGPV_TIGER_CTX *, const void *, size_t); | |
56 | void TIGER_Final(uint8_t *, TIGER_CTX *); | 56 | void netpgpv_TIGER_Final(uint8_t *, NETPGPV_TIGER_CTX *); | |
57 | 57 | |||
58 | char *TIGER_End(TIGER_CTX *, char *); | 58 | char *netpgpv_TIGER_End(NETPGPV_TIGER_CTX *, char *); | |
59 | 59 | |||
60 | char *TIGER_File(char *, char *, int); | 60 | char *netpgpv_TIGER_File(char *, char *, int); | |
61 | char *TIGER_Data(const uint8_t *, size_t, char *, int); | 61 | char *netpgpv_TIGER_Data(const uint8_t *, size_t, char *, int); | |
62 | 62 | |||
63 | __END_DECLS | 63 | __END_DECLS | |
64 | 64 | |||
65 | #endif | 65 | #endif |
@@ -421,55 +421,55 @@ fmt_binary(char *s, size_t size, const u | @@ -421,55 +421,55 @@ fmt_binary(char *s, size_t size, const u | |||
421 | size_t cc; | 421 | size_t cc; | |
422 | 422 | |||
423 | for (cc = 0, i = 0 ; i < len && cc < size ; i++) { | 423 | for (cc = 0, i = 0 ; i < len && cc < size ; i++) { | |
424 | cc += snprintf(&s[cc], size - cc, "%02x", bin[i]); | 424 | cc += snprintf(&s[cc], size - cc, "%02x", bin[i]); | |
425 | } | 425 | } | |
426 | return cc; | 426 | return cc; | |
427 | } | 427 | } | |
428 | 428 | |||
429 | /* format an mpi into memory */ | 429 | /* format an mpi into memory */ | |
430 | static unsigned | 430 | static unsigned | |
431 | fmt_binary_mpi(pgpv_bignum_t *mpi, uint8_t *p, size_t size) | 431 | fmt_binary_mpi(pgpv_bignum_t *mpi, uint8_t *p, size_t size) | |
432 | { | 432 | { | |
433 | unsigned bytes; | 433 | unsigned bytes; | |
434 | BIGNUM *bn; | 434 | PGPV_BIGNUM *bn; | |
435 | 435 | |||
436 | bytes = BITS_TO_BYTES(mpi->bits); | 436 | bytes = BITS_TO_BYTES(mpi->bits); | |
437 | if ((size_t)bytes + 2 + 1 > size) { | 437 | if ((size_t)bytes + 2 + 1 > size) { | |
438 | fprintf(stderr, "truncated mpi"); | 438 | fprintf(stderr, "truncated mpi"); | |
439 | return 0; | 439 | return 0; | |
440 | } | 440 | } | |
441 | bn = (BIGNUM *)mpi->bn; | 441 | bn = (PGPV_BIGNUM *)mpi->bn; | |
442 | if (bn == NULL || BN_is_zero(bn)) { | 442 | if (bn == NULL || PGPV_BN_is_zero(bn)) { | |
443 | fmt_32(p, 0); | 443 | fmt_32(p, 0); | |
444 | return 2 + 1; | 444 | return 2 + 1; | |
445 | } | 445 | } | |
446 | fmt_16(p, mpi->bits); | 446 | fmt_16(p, mpi->bits); | |
447 | BN_bn2bin(bn, &p[2]); | 447 | PGPV_BN_bn2bin(bn, &p[2]); | |
448 | return bytes + 2; | 448 | return bytes + 2; | |
449 | } | 449 | } | |
450 | 450 | |||
451 | /* dump an mpi value onto stdout */ | 451 | /* dump an mpi value onto stdout */ | |
452 | static size_t | 452 | static size_t | |
453 | fmt_mpi(char *s, size_t size, pgpv_bignum_t *bn, const char *name, int pbits) | 453 | fmt_mpi(char *s, size_t size, pgpv_bignum_t *bn, const char *name, int pbits) | |
454 | { | 454 | { | |
455 | size_t cc; | 455 | size_t cc; | |
456 | char *buf; | 456 | char *buf; | |
457 | 457 | |||
458 | cc = snprintf(s, size, "%s=", name); | 458 | cc = snprintf(s, size, "%s=", name); | |
459 | if (pbits) { | 459 | if (pbits) { | |
460 | cc += snprintf(&s[cc], size - cc, "[%u bits] ", bn->bits); | 460 | cc += snprintf(&s[cc], size - cc, "[%u bits] ", bn->bits); | |
461 | } | 461 | } | |
462 | buf = BN_bn2hex(bn->bn); | 462 | buf = PGPV_BN_bn2hex(bn->bn); | |
463 | cc += snprintf(&s[cc], size - cc, "%s\n", buf); | 463 | cc += snprintf(&s[cc], size - cc, "%s\n", buf); | |
464 | free(buf); | 464 | free(buf); | |
465 | return cc; | 465 | return cc; | |
466 | } | 466 | } | |
467 | 467 | |||
468 | #define ALG_IS_RSA(alg) (((alg) == PUBKEY_RSA_ENCRYPT_OR_SIGN) || \ | 468 | #define ALG_IS_RSA(alg) (((alg) == PUBKEY_RSA_ENCRYPT_OR_SIGN) || \ | |
469 | ((alg) == PUBKEY_RSA_ENCRYPT) || \ | 469 | ((alg) == PUBKEY_RSA_ENCRYPT) || \ | |
470 | ((alg) == PUBKEY_RSA_SIGN)) | 470 | ((alg) == PUBKEY_RSA_SIGN)) | |
471 | 471 | |||
472 | #define ALG_IS_DSA(alg) ((alg) == PUBKEY_DSA) | 472 | #define ALG_IS_DSA(alg) ((alg) == PUBKEY_DSA) | |
473 | 473 | |||
474 | /* format key mpis into memory */ | 474 | /* format key mpis into memory */ | |
475 | static unsigned | 475 | static unsigned | |
@@ -723,27 +723,27 @@ print_key_mpis(pgpv_bignum_t *v, uint8_t | @@ -723,27 +723,27 @@ print_key_mpis(pgpv_bignum_t *v, uint8_t | |||
723 | } | 723 | } | |
724 | 724 | |||
725 | /* get an mpi, including 2 byte length */ | 725 | /* get an mpi, including 2 byte length */ | |
726 | static int | 726 | static int | |
727 | get_mpi(pgpv_bignum_t *mpi, uint8_t *p, size_t pktlen, size_t *off) | 727 | get_mpi(pgpv_bignum_t *mpi, uint8_t *p, size_t pktlen, size_t *off) | |
728 | { | 728 | { | |
729 | size_t bytes; | 729 | size_t bytes; | |
730 | 730 | |||
731 | mpi->bits = get_16(p); | 731 | mpi->bits = get_16(p); | |
732 | if ((bytes = (size_t)BITS_TO_BYTES(mpi->bits)) > pktlen) { | 732 | if ((bytes = (size_t)BITS_TO_BYTES(mpi->bits)) > pktlen) { | |
733 | return 0; | 733 | return 0; | |
734 | } | 734 | } | |
735 | *off += sizeof(mpi->bits); | 735 | *off += sizeof(mpi->bits); | |
736 | mpi->bn = BN_bin2bn(&p[sizeof(mpi->bits)], (int)bytes, NULL); | 736 | mpi->bn = PGPV_BN_bin2bn(&p[sizeof(mpi->bits)], (int)bytes, NULL); | |
737 | *off += bytes; | 737 | *off += bytes; | |
738 | return 1; | 738 | return 1; | |
739 | } | 739 | } | |
740 | 740 | |||
741 | /* read mpis in signature */ | 741 | /* read mpis in signature */ | |
742 | static int | 742 | static int | |
743 | read_signature_mpis(pgpv_sigpkt_t *sigpkt, uint8_t *p, size_t pktlen) | 743 | read_signature_mpis(pgpv_sigpkt_t *sigpkt, uint8_t *p, size_t pktlen) | |
744 | { | 744 | { | |
745 | size_t off; | 745 | size_t off; | |
746 | 746 | |||
747 | off = 0; | 747 | off = 0; | |
748 | switch(sigpkt->sig.keyalg) { | 748 | switch(sigpkt->sig.keyalg) { | |
749 | case PUBKEY_RSA_ENCRYPT_OR_SIGN: | 749 | case PUBKEY_RSA_ENCRYPT_OR_SIGN: | |
@@ -1523,104 +1523,104 @@ rsa_padding_check_none(uint8_t *to, int | @@ -1523,104 +1523,104 @@ rsa_padding_check_none(uint8_t *to, int | |||
1523 | (void) memcpy(to + tlen - flen, from, (size_t)flen); | 1523 | (void) memcpy(to + tlen - flen, from, (size_t)flen); | |
1524 | return tlen; | 1524 | return tlen; | |
1525 | } | 1525 | } | |
1526 | 1526 | |||
1527 | #define RSA_MAX_MODULUS_BITS 16384 | 1527 | #define RSA_MAX_MODULUS_BITS 16384 | |
1528 | #define RSA_SMALL_MODULUS_BITS 3072 | 1528 | #define RSA_SMALL_MODULUS_BITS 3072 | |
1529 | #define RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */ | 1529 | #define RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */ | |
1530 | 1530 | |||
1531 | /* check against the exponent/moudulo operation */ | 1531 | /* check against the exponent/moudulo operation */ | |
1532 | static int | 1532 | static int | |
1533 | lowlevel_rsa_public_check(const uint8_t *encbuf, int enclen, uint8_t *dec, const rsa_pubkey_t *rsa) | 1533 | lowlevel_rsa_public_check(const uint8_t *encbuf, int enclen, uint8_t *dec, const rsa_pubkey_t *rsa) | |
1534 | { | 1534 | { | |
1535 | uint8_t *decbuf; | 1535 | uint8_t *decbuf; | |
1536 | BIGNUM *decbn; | 1536 | PGPV_BIGNUM *decbn; | |
1537 | BIGNUM *encbn; | 1537 | PGPV_BIGNUM *encbn; | |
1538 | int decbytes; | 1538 | int decbytes; | |
1539 | int nbytes; | 1539 | int nbytes; | |
1540 | int r; | 1540 | int r; | |
1541 | 1541 | |||
1542 | nbytes = 0; | 1542 | nbytes = 0; | |
1543 | r = -1; | 1543 | r = -1; | |
1544 | decbuf = NULL; | 1544 | decbuf = NULL; | |
1545 | decbn = encbn = NULL; | 1545 | decbn = encbn = NULL; | |
1546 | if (BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) { | 1546 | if (PGPV_BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) { | |
1547 | printf("rsa r modulus too large\n"); | 1547 | printf("rsa r modulus too large\n"); | |
1548 | goto err; | 1548 | goto err; | |
1549 | } | 1549 | } | |
1550 | if (BN_cmp(rsa->n, rsa->e) <= 0) { | 1550 | if (PGPV_BN_cmp(rsa->n, rsa->e) <= 0) { | |
1551 | printf("rsa r bad n value\n"); | 1551 | printf("rsa r bad n value\n"); | |
1552 | goto err; | 1552 | goto err; | |
1553 | } | 1553 | } | |
1554 | if (BN_num_bits(rsa->n) > RSA_SMALL_MODULUS_BITS && | 1554 | if (PGPV_BN_num_bits(rsa->n) > RSA_SMALL_MODULUS_BITS && | |
1555 | BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS) { | 1555 | PGPV_BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS) { | |
1556 | printf("rsa r bad exponent limit\n"); | 1556 | printf("rsa r bad exponent limit\n"); | |
1557 | goto err; | 1557 | goto err; | |
1558 | } | 1558 | } | |
1559 | nbytes = BN_num_bytes(rsa->n); | 1559 | nbytes = PGPV_BN_num_bytes(rsa->n); | |
1560 | if ((encbn = BN_new()) == NULL || | 1560 | if ((encbn = PGPV_BN_new()) == NULL || | |
1561 | (decbn = BN_new()) == NULL || | 1561 | (decbn = PGPV_BN_new()) == NULL || | |
1562 | (decbuf = calloc(1, (size_t)nbytes)) == NULL) { | 1562 | (decbuf = calloc(1, (size_t)nbytes)) == NULL) { | |
1563 | printf("allocation failure\n"); | 1563 | printf("allocation failure\n"); | |
1564 | goto err; | 1564 | goto err; | |
1565 | } | 1565 | } | |
1566 | if (enclen > nbytes) { | 1566 | if (enclen > nbytes) { | |
1567 | printf("rsa r > mod len\n"); | 1567 | printf("rsa r > mod len\n"); | |
1568 | goto err; | 1568 | goto err; | |
1569 | } | 1569 | } | |
1570 | if (BN_bin2bn(encbuf, enclen, encbn) == NULL) { | 1570 | if (PGPV_BN_bin2bn(encbuf, enclen, encbn) == NULL) { | |
1571 | printf("null encrypted BN\n"); | 1571 | printf("null encrypted BN\n"); | |
1572 | goto err; | 1572 | goto err; | |
1573 | } | 1573 | } | |
1574 | if (BN_cmp(encbn, rsa->n) >= 0) { | 1574 | if (PGPV_BN_cmp(encbn, rsa->n) >= 0) { | |
1575 | printf("rsa r data too large for modulus\n"); | 1575 | printf("rsa r data too large for modulus\n"); | |
1576 | goto err; | 1576 | goto err; | |
1577 | } | 1577 | } | |
1578 | if (BN_mod_exp(decbn, encbn, rsa->e, rsa->n, NULL) < 0) { | 1578 | if (PGPV_BN_mod_exp(decbn, encbn, rsa->e, rsa->n, NULL) < 0) { | |
1579 | printf("BN_mod_exp < 0\n"); | 1579 | printf("PGPV_BN_mod_exp < 0\n"); | |
1580 | goto err; | 1580 | goto err; | |
1581 | } | 1581 | } | |
1582 | decbytes = BN_num_bytes(decbn); | 1582 | decbytes = PGPV_BN_num_bytes(decbn); | |
1583 | (void) BN_bn2bin(decbn, decbuf); | 1583 | (void) PGPV_BN_bn2bin(decbn, decbuf); | |
1584 | if ((r = rsa_padding_check_none(dec, nbytes, decbuf, decbytes, 0)) < 0) { | 1584 | if ((r = rsa_padding_check_none(dec, nbytes, decbuf, decbytes, 0)) < 0) { | |
1585 | printf("rsa r padding check failed\n"); | 1585 | printf("rsa r padding check failed\n"); | |
1586 | } | 1586 | } | |
1587 | err: | 1587 | err: | |
1588 | BN_free(encbn); | 1588 | PGPV_BN_free(encbn); | |
1589 | BN_free(decbn); | 1589 | PGPV_BN_free(decbn); | |
1590 | if (decbuf != NULL) { | 1590 | if (decbuf != NULL) { | |
1591 | (void) memset(decbuf, 0x0, nbytes); | 1591 | (void) memset(decbuf, 0x0, nbytes); | |
1592 | free(decbuf); | 1592 | free(decbuf); | |
1593 | } | 1593 | } | |
1594 | return r; | 1594 | return r; | |
1595 | } | 1595 | } | |
1596 | 1596 | |||
1597 | /* verify */ | 1597 | /* verify */ | |
1598 | static int | 1598 | static int | |
1599 | rsa_public_decrypt(int enclen, const unsigned char *enc, unsigned char *dec, RSA *rsa, int padding) | 1599 | rsa_public_decrypt(int enclen, const unsigned char *enc, unsigned char *dec, RSA *rsa, int padding) | |
1600 | { | 1600 | { | |
1601 | rsa_pubkey_t pub; | 1601 | rsa_pubkey_t pub; | |
1602 | int ret; | 1602 | int ret; | |
1603 | 1603 | |||
1604 | if (enc == NULL || dec == NULL || rsa == NULL) { | 1604 | if (enc == NULL || dec == NULL || rsa == NULL) { | |
1605 | return 0; | 1605 | return 0; | |
1606 | } | 1606 | } | |
1607 | USE_ARG(padding); | 1607 | USE_ARG(padding); | |
1608 | (void) memset(&pub, 0x0, sizeof(pub)); | 1608 | (void) memset(&pub, 0x0, sizeof(pub)); | |
1609 | pub.n = BN_dup(rsa->n); | 1609 | pub.n = PGPV_BN_dup(rsa->n); | |
1610 | pub.e = BN_dup(rsa->e); | 1610 | pub.e = PGPV_BN_dup(rsa->e); | |
1611 | ret = lowlevel_rsa_public_check(enc, enclen, dec, &pub); | 1611 | ret = lowlevel_rsa_public_check(enc, enclen, dec, &pub); | |
1612 | BN_free(pub.n); | 1612 | PGPV_BN_free(pub.n); | |
1613 | BN_free(pub.e); | 1613 | PGPV_BN_free(pub.e); | |
1614 | return ret; | 1614 | return ret; | |
1615 | } | 1615 | } | |
1616 | 1616 | |||
1617 | #define SUBKEY_LEN(x) (80 + 80) | 1617 | #define SUBKEY_LEN(x) (80 + 80) | |
1618 | #define SIG_LEN 80 | 1618 | #define SIG_LEN 80 | |
1619 | #define UID_LEN 80 | 1619 | #define UID_LEN 80 | |
1620 | 1620 | |||
1621 | /* return worst case number of bytes needed to format a primary key */ | 1621 | /* return worst case number of bytes needed to format a primary key */ | |
1622 | static size_t | 1622 | static size_t | |
1623 | estimate_primarykey_size(pgpv_primarykey_t *primary) | 1623 | estimate_primarykey_size(pgpv_primarykey_t *primary) | |
1624 | { | 1624 | { | |
1625 | size_t cc; | 1625 | size_t cc; | |
1626 | 1626 | |||
@@ -1651,74 +1651,74 @@ pgpv_rsa_public_decrypt(uint8_t *out, co | @@ -1651,74 +1651,74 @@ pgpv_rsa_public_decrypt(uint8_t *out, co | |||
1651 | /* verify rsa signature */ | 1651 | /* verify rsa signature */ | |
1652 | static int | 1652 | static int | |
1653 | rsa_verify(uint8_t *calculated, unsigned calclen, uint8_t hashalg, pgpv_bignum_t *bn, pgpv_pubkey_t *pubkey) | 1653 | rsa_verify(uint8_t *calculated, unsigned calclen, uint8_t hashalg, pgpv_bignum_t *bn, pgpv_pubkey_t *pubkey) | |
1654 | { | 1654 | { | |
1655 | unsigned prefixlen; | 1655 | unsigned prefixlen; | |
1656 | unsigned decryptc; | 1656 | unsigned decryptc; | |
1657 | unsigned i; | 1657 | unsigned i; | |
1658 | uint8_t decrypted[8192]; | 1658 | uint8_t decrypted[8192]; | |
1659 | uint8_t sigbn[8192]; | 1659 | uint8_t sigbn[8192]; | |
1660 | uint8_t prefix[64]; | 1660 | uint8_t prefix[64]; | |
1661 | size_t keysize; | 1661 | size_t keysize; | |
1662 | 1662 | |||
1663 | keysize = BITS_TO_BYTES(pubkey->bn[RSA_N].bits); | 1663 | keysize = BITS_TO_BYTES(pubkey->bn[RSA_N].bits); | |
1664 | BN_bn2bin(bn[RSA_SIG].bn, sigbn); | 1664 | PGPV_BN_bn2bin(bn[RSA_SIG].bn, sigbn); | |
1665 | decryptc = pgpv_rsa_public_decrypt(decrypted, sigbn, BITS_TO_BYTES(bn[RSA_SIG].bits), pubkey); | 1665 | decryptc = pgpv_rsa_public_decrypt(decrypted, sigbn, BITS_TO_BYTES(bn[RSA_SIG].bits), pubkey); | |
1666 | if (decryptc != keysize || (decrypted[0] != 0 || decrypted[1] != 1)) { | 1666 | if (decryptc != keysize || (decrypted[0] != 0 || decrypted[1] != 1)) { | |
1667 | return 0; | 1667 | return 0; | |
1668 | } | 1668 | } | |
1669 | if ((prefixlen = digest_get_prefix((unsigned)hashalg, prefix, sizeof(prefix))) == 0) { | 1669 | if ((prefixlen = digest_get_prefix((unsigned)hashalg, prefix, sizeof(prefix))) == 0) { | |
1670 | printf("rsa_verify: unknown hash algorithm: %d\n", hashalg); | 1670 | printf("rsa_verify: unknown hash algorithm: %d\n", hashalg); | |
1671 | return 0; | 1671 | return 0; | |
1672 | } | 1672 | } | |
1673 | for (i = 2 ; i < keysize - prefixlen - calclen - 1 ; i++) { | 1673 | for (i = 2 ; i < keysize - prefixlen - calclen - 1 ; i++) { | |
1674 | if (decrypted[i] != 0xff) { | 1674 | if (decrypted[i] != 0xff) { | |
1675 | return 0; | 1675 | return 0; | |
1676 | } | 1676 | } | |
1677 | } | 1677 | } | |
1678 | if (decrypted[i++] != 0x0) { | 1678 | if (decrypted[i++] != 0x0) { | |
1679 | return 0; | 1679 | return 0; | |
1680 | } | 1680 | } | |
1681 | if (memcmp(&decrypted[i], prefix, prefixlen) != 0) { | 1681 | if (memcmp(&decrypted[i], prefix, prefixlen) != 0) { | |
1682 | printf("rsa_verify: wrong hash algorithm\n"); | 1682 | printf("rsa_verify: wrong hash algorithm\n"); | |
1683 | return 0; | 1683 | return 0; | |
1684 | } | 1684 | } | |
1685 | return memcmp(&decrypted[i + prefixlen], calculated, calclen) == 0; | 1685 | return memcmp(&decrypted[i + prefixlen], calculated, calclen) == 0; | |
1686 | } | 1686 | } | |
1687 | 1687 | |||
1688 | /* return 1 if bn <= 0 */ | 1688 | /* return 1 if bn <= 0 */ | |
1689 | static int | 1689 | static int | |
1690 | bignum_is_bad(BIGNUM *bn) | 1690 | bignum_is_bad(PGPV_BIGNUM *bn) | |
1691 | { | 1691 | { | |
1692 | return BN_is_zero(bn) || BN_is_negative(bn); | 1692 | return PGPV_BN_is_zero(bn) || PGPV_BN_is_negative(bn); | |
1693 | } | 1693 | } | |
1694 | 1694 | |||
1695 | #define BAD_BIGNUM(s, k) \ | 1695 | #define BAD_BIGNUM(s, k) \ | |
1696 | (bignum_is_bad((s)->bn) || BN_cmp((s)->bn, (k)->bn) >= 0) | 1696 | (bignum_is_bad((s)->bn) || PGPV_BN_cmp((s)->bn, (k)->bn) >= 0) | |
1697 | 1697 | |||
1698 | #ifndef DSA_MAX_MODULUS_BITS | 1698 | #ifndef DSA_MAX_MODULUS_BITS | |
1699 | #define DSA_MAX_MODULUS_BITS 10000 | 1699 | #define DSA_MAX_MODULUS_BITS 10000 | |
1700 | #endif | 1700 | #endif | |
1701 | 1701 | |||
1702 | /* verify DSA signature */ | 1702 | /* verify DSA signature */ | |
1703 | static int | 1703 | static int | |
1704 | verify_dsa_sig(uint8_t *calculated, unsigned calclen, pgpv_bignum_t *sig, pgpv_pubkey_t *pubkey) | 1704 | verify_dsa_sig(uint8_t *calculated, unsigned calclen, pgpv_bignum_t *sig, pgpv_pubkey_t *pubkey) | |
1705 | { | 1705 | { | |
1706 | unsigned qbits; | 1706 | unsigned qbits; | |
1707 | uint8_t calcnum[128]; | 1707 | uint8_t calcnum[128]; | |
1708 | uint8_t signum[128]; | 1708 | uint8_t signum[128]; | |
1709 | BIGNUM *M; | 1709 | PGPV_BIGNUM *M; | |
1710 | BIGNUM *W; | 1710 | PGPV_BIGNUM *W; | |
1711 | BIGNUM *t1; | 1711 | PGPV_BIGNUM *t1; | |
1712 | int ret; | 1712 | int ret; | |
1713 | 1713 | |||
1714 | if (pubkey->bn[DSA_P].bn == NULL || | 1714 | if (pubkey->bn[DSA_P].bn == NULL || | |
1715 | pubkey->bn[DSA_Q].bn == NULL || | 1715 | pubkey->bn[DSA_Q].bn == NULL || | |
1716 | pubkey->bn[DSA_G].bn == NULL) { | 1716 | pubkey->bn[DSA_G].bn == NULL) { | |
1717 | return 0; | 1717 | return 0; | |
1718 | } | 1718 | } | |
1719 | M = W = t1 = NULL; | 1719 | M = W = t1 = NULL; | |
1720 | qbits = pubkey->bn[DSA_Q].bits; | 1720 | qbits = pubkey->bn[DSA_Q].bits; | |
1721 | switch(qbits) { | 1721 | switch(qbits) { | |
1722 | case 160: | 1722 | case 160: | |
1723 | case 224: | 1723 | case 224: | |
1724 | case 256: | 1724 | case 256: | |
@@ -1726,57 +1726,57 @@ verify_dsa_sig(uint8_t *calculated, unsi | @@ -1726,57 +1726,57 @@ verify_dsa_sig(uint8_t *calculated, unsi | |||
1726 | default: | 1726 | default: | |
1727 | printf("dsa: bad # of Q bits\n"); | 1727 | printf("dsa: bad # of Q bits\n"); | |
1728 | return 0; | 1728 | return 0; | |
1729 | } | 1729 | } | |
1730 | if (pubkey->bn[DSA_P].bits > DSA_MAX_MODULUS_BITS) { | 1730 | if (pubkey->bn[DSA_P].bits > DSA_MAX_MODULUS_BITS) { | |
1731 | printf("dsa: p too large\n"); | 1731 | printf("dsa: p too large\n"); | |
1732 | return 0; | 1732 | return 0; | |
1733 | } | 1733 | } | |
1734 | if (calclen > SHA256_DIGEST_LENGTH) { | 1734 | if (calclen > SHA256_DIGEST_LENGTH) { | |
1735 | printf("dsa: digest too long\n"); | 1735 | printf("dsa: digest too long\n"); | |
1736 | return 0; | 1736 | return 0; | |
1737 | } | 1737 | } | |
1738 | ret = 0; | 1738 | ret = 0; | |
1739 | if ((M = BN_new()) == NULL || (W = BN_new()) == NULL || (t1 = BN_new()) == NULL || | 1739 | if ((M = PGPV_BN_new()) == NULL || (W = PGPV_BN_new()) == NULL || (t1 = PGPV_BN_new()) == NULL || | |
1740 | BAD_BIGNUM(&sig[DSA_R], &pubkey->bn[DSA_Q]) || | 1740 | BAD_BIGNUM(&sig[DSA_R], &pubkey->bn[DSA_Q]) || | |
1741 | BAD_BIGNUM(&sig[DSA_S], &pubkey->bn[DSA_Q]) || | 1741 | BAD_BIGNUM(&sig[DSA_S], &pubkey->bn[DSA_Q]) || | |
1742 | BN_mod_inverse(W, sig[DSA_S].bn, pubkey->bn[DSA_Q].bn, NULL) == NULL) { | 1742 | PGPV_BN_mod_inverse(W, sig[DSA_S].bn, pubkey->bn[DSA_Q].bn, NULL) == NULL) { | |
1743 | goto done; | 1743 | goto done; | |
1744 | } | 1744 | } | |
1745 | if (calclen > qbits / 8) { | 1745 | if (calclen > qbits / 8) { | |
1746 | calclen = qbits / 8; | 1746 | calclen = qbits / 8; | |
1747 | } | 1747 | } | |
1748 | if (BN_bin2bn(calculated, (int)calclen, M) == NULL || | 1748 | if (PGPV_BN_bin2bn(calculated, (int)calclen, M) == NULL || | |
1749 | !BN_mod_mul(M, M, W, pubkey->bn[DSA_Q].bn, NULL) || | 1749 | !PGPV_BN_mod_mul(M, M, W, pubkey->bn[DSA_Q].bn, NULL) || | |
1750 | !BN_mod_mul(W, sig[DSA_R].bn, W, pubkey->bn[DSA_Q].bn, NULL) || | 1750 | !PGPV_BN_mod_mul(W, sig[DSA_R].bn, W, pubkey->bn[DSA_Q].bn, NULL) || | |
1751 | !BN_mod_exp(t1, pubkey->bn[DSA_G].bn, M, pubkey->bn[DSA_P].bn, NULL) || | 1751 | !PGPV_BN_mod_exp(t1, pubkey->bn[DSA_G].bn, M, pubkey->bn[DSA_P].bn, NULL) || | |
1752 | !BN_mod_exp(W, pubkey->bn[DSA_Y].bn, W, pubkey->bn[DSA_P].bn, NULL) || | 1752 | !PGPV_BN_mod_exp(W, pubkey->bn[DSA_Y].bn, W, pubkey->bn[DSA_P].bn, NULL) || | |
1753 | !BN_mod_mul(t1, t1, W, pubkey->bn[DSA_P].bn, NULL) || | 1753 | !PGPV_BN_mod_mul(t1, t1, W, pubkey->bn[DSA_P].bn, NULL) || | |
1754 | !BN_div(NULL, t1, t1, pubkey->bn[DSA_Q].bn, NULL)) { | 1754 | !PGPV_BN_div(NULL, t1, t1, pubkey->bn[DSA_Q].bn, NULL)) { | |
1755 | goto done; | 1755 | goto done; | |
1756 | } | 1756 | } | |
1757 | /* only compare the first q bits */ | 1757 | /* only compare the first q bits */ | |
1758 | BN_bn2bin(t1, calcnum); | 1758 | PGPV_BN_bn2bin(t1, calcnum); | |
1759 | BN_bn2bin(sig[DSA_R].bn, signum); | 1759 | PGPV_BN_bn2bin(sig[DSA_R].bn, signum); | |
1760 | ret = memcmp(calcnum, signum, BITS_TO_BYTES(qbits)) == 0; | 1760 | ret = memcmp(calcnum, signum, BITS_TO_BYTES(qbits)) == 0; | |
1761 | done: | 1761 | done: | |
1762 | if (M) { | 1762 | if (M) { | |
1763 | BN_free(M); | 1763 | PGPV_BN_free(M); | |
1764 | } | 1764 | } | |
1765 | if (W) { | 1765 | if (W) { | |
1766 | BN_free(W); | 1766 | PGPV_BN_free(W); | |
1767 | } | 1767 | } | |
1768 | if (t1) { | 1768 | if (t1) { | |
1769 | BN_free(t1); | 1769 | PGPV_BN_free(t1); | |
1770 | } | 1770 | } | |
1771 | return ret; | 1771 | return ret; | |
1772 | } | 1772 | } | |
1773 | 1773 | |||
1774 | #define TIME_SNPRINTF(_cc, _buf, _size, _fmt, _val) do { \ | 1774 | #define TIME_SNPRINTF(_cc, _buf, _size, _fmt, _val) do { \ | |
1775 | time_t _t; \ | 1775 | time_t _t; \ | |
1776 | char *_s; \ | 1776 | char *_s; \ | |
1777 | \ | 1777 | \ | |
1778 | _t = _val; \ | 1778 | _t = _val; \ | |
1779 | _s = ctime(&_t); \ | 1779 | _s = ctime(&_t); \ | |
1780 | _cc += snprintf(_buf, _size, _fmt, _s); \ | 1780 | _cc += snprintf(_buf, _size, _fmt, _s); \ | |
1781 | } while(/*CONSTCOND*/0) | 1781 | } while(/*CONSTCOND*/0) | |
1782 | 1782 | |||
@@ -2192,28 +2192,28 @@ read_binary_file(pgpv_t *pgp, const char | @@ -2192,28 +2192,28 @@ read_binary_file(pgpv_t *pgp, const char | |||
2192 | } | 2192 | } | |
2193 | 2193 | |||
2194 | /* get a bignum from the buffer gap */ | 2194 | /* get a bignum from the buffer gap */ | |
2195 | static int | 2195 | static int | |
2196 | getbignum(pgpv_bignum_t *bignum, bufgap_t *bg, char *buf, const char *header) | 2196 | getbignum(pgpv_bignum_t *bignum, bufgap_t *bg, char *buf, const char *header) | |
2197 | { | 2197 | { | |
2198 | uint32_t len; | 2198 | uint32_t len; | |
2199 | 2199 | |||
2200 | USE_ARG(header); | 2200 | USE_ARG(header); | |
2201 | (void) bufgap_getbin(bg, &len, sizeof(len)); | 2201 | (void) bufgap_getbin(bg, &len, sizeof(len)); | |
2202 | len = pgp_ntoh32(len); | 2202 | len = pgp_ntoh32(len); | |
2203 | (void) bufgap_seek(bg, sizeof(len), BGFromHere, BGByte); | 2203 | (void) bufgap_seek(bg, sizeof(len), BGFromHere, BGByte); | |
2204 | (void) bufgap_getbin(bg, buf, len); | 2204 | (void) bufgap_getbin(bg, buf, len); | |
2205 | bignum->bn = BN_bin2bn((const uint8_t *)buf, (int)len, NULL); | 2205 | bignum->bn = PGPV_BN_bin2bn((const uint8_t *)buf, (int)len, NULL); | |
2206 | bignum->bits = BN_num_bits(bignum->bn); | 2206 | bignum->bits = PGPV_BN_num_bits(bignum->bn); | |
2207 | (void) bufgap_seek(bg, len, BGFromHere, BGByte); | 2207 | (void) bufgap_seek(bg, len, BGFromHere, BGByte); | |
2208 | return 1; | 2208 | return 1; | |
2209 | } | 2209 | } | |
2210 | 2210 | |||
2211 | /* structure for searching for constant strings */ | 2211 | /* structure for searching for constant strings */ | |
2212 | typedef struct str_t { | 2212 | typedef struct str_t { | |
2213 | const char *s; /* string */ | 2213 | const char *s; /* string */ | |
2214 | size_t len; /* its length */ | 2214 | size_t len; /* its length */ | |
2215 | int type; /* return type */ | 2215 | int type; /* return type */ | |
2216 | } str_t; | 2216 | } str_t; | |
2217 | 2217 | |||
2218 | static str_t pkatypes[] = { | 2218 | static str_t pkatypes[] = { | |
2219 | { "ssh-rsa", 7, PUBKEY_RSA_SIGN }, | 2219 | { "ssh-rsa", 7, PUBKEY_RSA_SIGN }, |
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: sha2.c,v 1.3 2015/08/17 11:37:55 jperkin Exp $ */ | 1 | /* $NetBSD: sha2.c,v 1.4 2015/09/01 19:38:42 agc Exp $ */ | |
2 | /* $KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $ */ | 2 | /* $KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $ */ | |
3 | 3 | |||
4 | /* | 4 | /* | |
5 | * sha2.c | 5 | * sha2.c | |
6 | * | 6 | * | |
7 | * Version 1.0.0beta1 | 7 | * Version 1.0.0beta1 | |
8 | * | 8 | * | |
9 | * Written by Aaron D. Gifford <me@aarongifford.com> | 9 | * Written by Aaron D. Gifford <me@aarongifford.com> | |
10 | * | 10 | * | |
11 | * Copyright 2000 Aaron D. Gifford. All rights reserved. | 11 | * Copyright 2000 Aaron D. Gifford. All rights reserved. | |
12 | * | 12 | * | |
13 | * Redistribution and use in source and binary forms, with or without | 13 | * Redistribution and use in source and binary forms, with or without | |
14 | * modification, are permitted provided that the following conditions | 14 | * modification, are permitted provided that the following conditions | |
@@ -149,31 +149,31 @@ be64toh(uint64_t x) | @@ -149,31 +149,31 @@ be64toh(uint64_t x) | |||
149 | #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) | 149 | #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x))) | |
150 | 150 | |||
151 | /* Four of six logical functions used in SHA-384 and SHA-512: */ | 151 | /* Four of six logical functions used in SHA-384 and SHA-512: */ | |
152 | #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) | 152 | #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) | |
153 | #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) | 153 | #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) | |
154 | #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) | 154 | #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) | |
155 | #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x))) | 155 | #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x))) | |
156 | 156 | |||
157 | /*** INTERNAL FUNCTION PROTOTYPES *************************************/ | 157 | /*** INTERNAL FUNCTION PROTOTYPES *************************************/ | |
158 | /* NOTE: These should not be accessed directly from outside this | 158 | /* NOTE: These should not be accessed directly from outside this | |
159 | * library -- they are intended for private internal visibility/use | 159 | * library -- they are intended for private internal visibility/use | |
160 | * only. | 160 | * only. | |
161 | */ | 161 | */ | |
162 | static void SHA512_Last(SHA512_CTX *); | 162 | static void netpgpv_SHA512_Last(NETPGPV_SHA512_CTX *); | |
163 | void SHA224_Transform(SHA224_CTX *, const uint32_t*); | 163 | void netpgpv_SHA224_Transform(NETPGPV_SHA224_CTX *, const uint32_t*); | |
164 | void SHA256_Transform(SHA256_CTX *, const uint32_t*); | 164 | void netpgpv_SHA256_Transform(NETPGPV_SHA256_CTX *, const uint32_t*); | |
165 | void SHA384_Transform(SHA384_CTX *, const uint64_t*); | 165 | void netpgpv_SHA384_Transform(NETPGPV_SHA384_CTX *, const uint64_t*); | |
166 | void SHA512_Transform(SHA512_CTX *, const uint64_t*); | 166 | void netpgpv_SHA512_Transform(NETPGPV_SHA512_CTX *, const uint64_t*); | |
167 | 167 | |||
168 | 168 | |||
169 | /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ | 169 | /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ | |
170 | /* Hash constant words K for SHA-256: */ | 170 | /* Hash constant words K for SHA-256: */ | |
171 | static const uint32_t K256[64] = { | 171 | static const uint32_t K256[64] = { | |
172 | 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, | 172 | 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, | |
173 | 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, | 173 | 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, | |
174 | 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, | 174 | 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, | |
175 | 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, | 175 | 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, | |
176 | 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, | 176 | 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, | |
177 | 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, | 177 | 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, | |
178 | 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, | 178 | 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, | |
179 | 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, | 179 | 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, | |
@@ -271,27 +271,27 @@ static const uint64_t sha384_initial_has | @@ -271,27 +271,27 @@ static const uint64_t sha384_initial_has | |||
271 | static const uint64_t sha512_initial_hash_value[8] = { | 271 | static const uint64_t sha512_initial_hash_value[8] = { | |
272 | 0x6a09e667f3bcc908ULL, | 272 | 0x6a09e667f3bcc908ULL, | |
273 | 0xbb67ae8584caa73bULL, | 273 | 0xbb67ae8584caa73bULL, | |
274 | 0x3c6ef372fe94f82bULL, | 274 | 0x3c6ef372fe94f82bULL, | |
275 | 0xa54ff53a5f1d36f1ULL, | 275 | 0xa54ff53a5f1d36f1ULL, | |
276 | 0x510e527fade682d1ULL, | 276 | 0x510e527fade682d1ULL, | |
277 | 0x9b05688c2b3e6c1fULL, | 277 | 0x9b05688c2b3e6c1fULL, | |
278 | 0x1f83d9abfb41bd6bULL, | 278 | 0x1f83d9abfb41bd6bULL, | |
279 | 0x5be0cd19137e2179ULL | 279 | 0x5be0cd19137e2179ULL | |
280 | }; | 280 | }; | |
281 | 281 | |||
282 | /*** SHA-256: *********************************************************/ | 282 | /*** SHA-256: *********************************************************/ | |
283 | int | 283 | int | |
284 | SHA256_Init(SHA256_CTX *context) | 284 | netpgpv_SHA256_Init(NETPGPV_SHA256_CTX *context) | |
285 | { | 285 | { | |
286 | if (context == NULL) | 286 | if (context == NULL) | |
287 | return 1; | 287 | return 1; | |
288 | 288 | |||
289 | memcpy(context->state, sha256_initial_hash_value, | 289 | memcpy(context->state, sha256_initial_hash_value, | |
290 | (size_t)(SHA256_DIGEST_LENGTH)); | 290 | (size_t)(SHA256_DIGEST_LENGTH)); | |
291 | memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH)); | 291 | memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH)); | |
292 | context->bitcount = 0; | 292 | context->bitcount = 0; | |
293 | 293 | |||
294 | return 1; | 294 | return 1; | |
295 | } | 295 | } | |
296 | 296 | |||
297 | #ifdef SHA2_UNROLL_TRANSFORM | 297 | #ifdef SHA2_UNROLL_TRANSFORM | |
@@ -309,27 +309,27 @@ SHA256_Init(SHA256_CTX *context) | @@ -309,27 +309,27 @@ SHA256_Init(SHA256_CTX *context) | |||
309 | 309 | |||
310 | #define ROUND256(a,b,c,d,e,f,g,h) \ | 310 | #define ROUND256(a,b,c,d,e,f,g,h) \ | |
311 | s0 = W256[(j+1)&0x0f]; \ | 311 | s0 = W256[(j+1)&0x0f]; \ | |
312 | s0 = sigma0_256(s0); \ | 312 | s0 = sigma0_256(s0); \ | |
313 | s1 = W256[(j+14)&0x0f]; \ | 313 | s1 = W256[(j+14)&0x0f]; \ | |
314 | s1 = sigma1_256(s1); \ | 314 | s1 = sigma1_256(s1); \ | |
315 | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \ | 315 | T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \ | |
316 | (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ | 316 | (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ | |
317 | (d) += T1; \ | 317 | (d) += T1; \ | |
318 | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ | 318 | (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ | |
319 | j++ | 319 | j++ | |
320 | 320 | |||
321 | void | 321 | void | |
322 | SHA256_Transform(SHA256_CTX *context, const uint32_t *data) | 322 | netpgpv_SHA256_Transform(NETPGPV_SHA256_CTX *context, const uint32_t *data) | |
323 | { | 323 | { | |
324 | uint32_t a, b, c, d, e, f, g, h, s0, s1; | 324 | uint32_t a, b, c, d, e, f, g, h, s0, s1; | |
325 | uint32_t T1, *W256; | 325 | uint32_t T1, *W256; | |
326 | int j; | 326 | int j; | |
327 | 327 | |||
328 | W256 = (uint32_t *)context->buffer; | 328 | W256 = (uint32_t *)context->buffer; | |
329 | 329 | |||
330 | /* Initialize registers with the prev. intermediate value */ | 330 | /* Initialize registers with the prev. intermediate value */ | |
331 | a = context->state[0]; | 331 | a = context->state[0]; | |
332 | b = context->state[1]; | 332 | b = context->state[1]; | |
333 | c = context->state[2]; | 333 | c = context->state[2]; | |
334 | d = context->state[3]; | 334 | d = context->state[3]; | |
335 | e = context->state[4]; | 335 | e = context->state[4]; | |
@@ -369,27 +369,27 @@ SHA256_Transform(SHA256_CTX *context, co | @@ -369,27 +369,27 @@ SHA256_Transform(SHA256_CTX *context, co | |||
369 | context->state[3] += d; | 369 | context->state[3] += d; | |
370 | context->state[4] += e; | 370 | context->state[4] += e; | |
371 | context->state[5] += f; | 371 | context->state[5] += f; | |
372 | context->state[6] += g; | 372 | context->state[6] += g; | |
373 | context->state[7] += h; | 373 | context->state[7] += h; | |
374 | 374 | |||
375 | /* Clean up */ | 375 | /* Clean up */ | |
376 | a = b = c = d = e = f = g = h = T1 = 0; | 376 | a = b = c = d = e = f = g = h = T1 = 0; | |
377 | } | 377 | } | |
378 | 378 | |||
379 | #else /* SHA2_UNROLL_TRANSFORM */ | 379 | #else /* SHA2_UNROLL_TRANSFORM */ | |
380 | 380 | |||
381 | void | 381 | void | |
382 | SHA256_Transform(SHA256_CTX *context, const uint32_t *data) | 382 | netpgpv_SHA256_Transform(NETPGPV_SHA256_CTX *context, const uint32_t *data) | |
383 | { | 383 | { | |
384 | uint32_t a, b, c, d, e, f, g, h, s0, s1; | 384 | uint32_t a, b, c, d, e, f, g, h, s0, s1; | |
385 | uint32_t T1, T2, *W256; | 385 | uint32_t T1, T2, *W256; | |
386 | int j; | 386 | int j; | |
387 | 387 | |||
388 | W256 = (uint32_t *)(void *)context->buffer; | 388 | W256 = (uint32_t *)(void *)context->buffer; | |
389 | 389 | |||
390 | /* Initialize registers with the prev. intermediate value */ | 390 | /* Initialize registers with the prev. intermediate value */ | |
391 | a = context->state[0]; | 391 | a = context->state[0]; | |
392 | b = context->state[1]; | 392 | b = context->state[1]; | |
393 | c = context->state[2]; | 393 | c = context->state[2]; | |
394 | d = context->state[3]; | 394 | d = context->state[3]; | |
395 | e = context->state[4]; | 395 | e = context->state[4]; | |
@@ -446,27 +446,27 @@ SHA256_Transform(SHA256_CTX *context, co | @@ -446,27 +446,27 @@ SHA256_Transform(SHA256_CTX *context, co | |||
446 | context->state[3] += d; | 446 | context->state[3] += d; | |
447 | context->state[4] += e; | 447 | context->state[4] += e; | |
448 | context->state[5] += f; | 448 | context->state[5] += f; | |
449 | context->state[6] += g; | 449 | context->state[6] += g; | |
450 | context->state[7] += h; | 450 | context->state[7] += h; | |
451 | 451 | |||
452 | /* Clean up */ | 452 | /* Clean up */ | |
453 | a = b = c = d = e = f = g = h = T1 = T2 = 0; | 453 | a = b = c = d = e = f = g = h = T1 = T2 = 0; | |
454 | } | 454 | } | |
455 | 455 | |||
456 | #endif /* SHA2_UNROLL_TRANSFORM */ | 456 | #endif /* SHA2_UNROLL_TRANSFORM */ | |
457 | 457 | |||
458 | int | 458 | int | |
459 | SHA256_Update(SHA256_CTX *context, const uint8_t *data, size_t len) | 459 | netpgpv_SHA256_Update(NETPGPV_SHA256_CTX *context, const uint8_t *data, size_t len) | |
460 | { | 460 | { | |
461 | unsigned int freespace, usedspace; | 461 | unsigned int freespace, usedspace; | |
462 | 462 | |||
463 | if (len == 0) { | 463 | if (len == 0) { | |
464 | /* Calling with no data is valid - we do nothing */ | 464 | /* Calling with no data is valid - we do nothing */ | |
465 | return 1; | 465 | return 1; | |
466 | } | 466 | } | |
467 | 467 | |||
468 | usedspace = (unsigned int)((context->bitcount >> 3) % | 468 | usedspace = (unsigned int)((context->bitcount >> 3) % | |
469 | SHA256_BLOCK_LENGTH); | 469 | SHA256_BLOCK_LENGTH); | |
470 | if (usedspace > 0) { | 470 | if (usedspace > 0) { | |
471 | /* Calculate how much free space is available in the buffer */ | 471 | /* Calculate how much free space is available in the buffer */ | |
472 | freespace = SHA256_BLOCK_LENGTH - usedspace; | 472 | freespace = SHA256_BLOCK_LENGTH - usedspace; | |
@@ -516,27 +516,27 @@ SHA256_Update(SHA256_CTX *context, const | @@ -516,27 +516,27 @@ SHA256_Update(SHA256_CTX *context, const | |||
516 | } | 516 | } | |
517 | if (len > 0) { | 517 | if (len > 0) { | |
518 | /* There's left-overs, so save 'em */ | 518 | /* There's left-overs, so save 'em */ | |
519 | memcpy(context->buffer, data, len); | 519 | memcpy(context->buffer, data, len); | |
520 | context->bitcount += len << 3; | 520 | context->bitcount += len << 3; | |
521 | } | 521 | } | |
522 | /* Clean up: */ | 522 | /* Clean up: */ | |
523 | usedspace = freespace = 0; | 523 | usedspace = freespace = 0; | |
524 | 524 | |||
525 | return 1; | 525 | return 1; | |
526 | } | 526 | } | |
527 | 527 | |||
528 | static int | 528 | static int | |
529 | SHA224_256_Final(uint8_t digest[], SHA256_CTX *context, size_t len) | 529 | netpgpv_SHA224_256_Final(uint8_t digest[], NETPGPV_SHA256_CTX *context, size_t len) | |
530 | { | 530 | { | |
531 | unsigned int usedspace; | 531 | unsigned int usedspace; | |
532 | size_t i; | 532 | size_t i; | |
533 | 533 | |||
534 | /* If no digest buffer is passed, we don't bother doing this: */ | 534 | /* If no digest buffer is passed, we don't bother doing this: */ | |
535 | if (digest != NULL) { | 535 | if (digest != NULL) { | |
536 | usedspace = (unsigned int)((context->bitcount >> 3) % | 536 | usedspace = (unsigned int)((context->bitcount >> 3) % | |
537 | SHA256_BLOCK_LENGTH); | 537 | SHA256_BLOCK_LENGTH); | |
538 | context->bitcount = htobe64(context->bitcount); | 538 | context->bitcount = htobe64(context->bitcount); | |
539 | if (usedspace > 0) { | 539 | if (usedspace > 0) { | |
540 | /* Begin padding with a 1 bit: */ | 540 | /* Begin padding with a 1 bit: */ | |
541 | context->buffer[usedspace++] = 0x80; | 541 | context->buffer[usedspace++] = 0x80; | |
542 | 542 | |||
@@ -576,69 +576,69 @@ SHA224_256_Final(uint8_t digest[], SHA25 | @@ -576,69 +576,69 @@ SHA224_256_Final(uint8_t digest[], SHA25 | |||
576 | 576 | |||
577 | for (i = 0; i < len / 4; i++) | 577 | for (i = 0; i < len / 4; i++) | |
578 | be32encode(digest + 4 * i, context->state[i]); | 578 | be32encode(digest + 4 * i, context->state[i]); | |
579 | } | 579 | } | |
580 | 580 | |||
581 | /* Clean up state data: */ | 581 | /* Clean up state data: */ | |
582 | memset(context, 0, sizeof(*context)); | 582 | memset(context, 0, sizeof(*context)); | |
583 | usedspace = 0; | 583 | usedspace = 0; | |
584 | 584 | |||
585 | return 1; | 585 | return 1; | |
586 | } | 586 | } | |
587 | 587 | |||
588 | int | 588 | int | |
589 | SHA256_Final(uint8_t digest[], SHA256_CTX *context) | 589 | netpgpv_SHA256_Final(uint8_t digest[], NETPGPV_SHA256_CTX *context) | |
590 | { | 590 | { | |
591 | return SHA224_256_Final(digest, context, SHA256_DIGEST_LENGTH); | 591 | return netpgpv_SHA224_256_Final(digest, context, SHA256_DIGEST_LENGTH); | |
592 | } | 592 | } | |
593 | 593 | |||
594 | /*** SHA-224: *********************************************************/ | 594 | /*** SHA-224: *********************************************************/ | |
595 | int | 595 | int | |
596 | SHA224_Init(SHA224_CTX *context) | 596 | netpgpv_SHA224_Init(NETPGPV_SHA224_CTX *context) | |
597 | { | 597 | { | |
598 | if (context == NULL) | 598 | if (context == NULL) | |
599 | return 1; | 599 | return 1; | |
600 | 600 | |||
601 | /* The state and buffer size are driven by SHA256, not by SHA224. */ | 601 | /* The state and buffer size are driven by SHA256, not by SHA224. */ | |
602 | memcpy(context->state, sha224_initial_hash_value, | 602 | memcpy(context->state, sha224_initial_hash_value, | |
603 | (size_t)(SHA256_DIGEST_LENGTH)); | 603 | (size_t)(SHA256_DIGEST_LENGTH)); | |
604 | memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH)); | 604 | memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH)); | |
605 | context->bitcount = 0; | 605 | context->bitcount = 0; | |
606 | 606 | |||
607 | return 1; | 607 | return 1; | |
608 | } | 608 | } | |
609 | 609 | |||
610 | int | 610 | int | |
611 | SHA224_Update(SHA224_CTX *context, const uint8_t *data, size_t len) | 611 | netpgpv_SHA224_Update(NETPGPV_SHA224_CTX *context, const uint8_t *data, size_t len) | |
612 | { | 612 | { | |
613 | return SHA256_Update((SHA256_CTX *)context, data, len); | 613 | return SHA256_Update((NETPGPV_SHA256_CTX *)context, data, len); | |
614 | } | 614 | } | |
615 | 615 | |||
616 | void | 616 | void | |
617 | SHA224_Transform(SHA224_CTX *context, const uint32_t *data) | 617 | SHA224_Transform(NETPGPV_SHA224_CTX *context, const uint32_t *data) | |
618 | { | 618 | { | |
619 | SHA256_Transform((SHA256_CTX *)context, data); | 619 | SHA256_Transform((NETPGPV_SHA256_CTX *)context, data); | |
620 | } | 620 | } | |
621 | 621 | |||
622 | int | 622 | int | |
623 | SHA224_Final(uint8_t digest[], SHA224_CTX *context) | 623 | netpgpv_SHA224_Final(uint8_t digest[], NETPGPV_SHA224_CTX *context) | |
624 | { | 624 | { | |
625 | return SHA224_256_Final(digest, (SHA256_CTX *)context, | 625 | return netpgpv_SHA224_256_Final(digest, (NETPGPV_SHA256_CTX *)context, | |
626 | SHA224_DIGEST_LENGTH); | 626 | SHA224_DIGEST_LENGTH); | |
627 | } | 627 | } | |
628 | 628 | |||
629 | /*** SHA-512: *********************************************************/ | 629 | /*** SHA-512: *********************************************************/ | |
630 | int | 630 | int | |
631 | SHA512_Init(SHA512_CTX *context) | 631 | netpgpv_SHA512_Init(NETPGPV_SHA512_CTX *context) | |
632 | { | 632 | { | |
633 | if (context == NULL) | 633 | if (context == NULL) | |
634 | return 1; | 634 | return 1; | |
635 | 635 | |||
636 | memcpy(context->state, sha512_initial_hash_value, | 636 | memcpy(context->state, sha512_initial_hash_value, | |
637 | (size_t)(SHA512_DIGEST_LENGTH)); | 637 | (size_t)(SHA512_DIGEST_LENGTH)); | |
638 | memset(context->buffer, 0, (size_t)(SHA512_BLOCK_LENGTH)); | 638 | memset(context->buffer, 0, (size_t)(SHA512_BLOCK_LENGTH)); | |
639 | context->bitcount[0] = context->bitcount[1] = 0; | 639 | context->bitcount[0] = context->bitcount[1] = 0; | |
640 | 640 | |||
641 | return 1; | 641 | return 1; | |
642 | } | 642 | } | |
643 | 643 | |||
644 | #ifdef SHA2_UNROLL_TRANSFORM | 644 | #ifdef SHA2_UNROLL_TRANSFORM | |
@@ -655,27 +655,27 @@ SHA512_Init(SHA512_CTX *context) | @@ -655,27 +655,27 @@ SHA512_Init(SHA512_CTX *context) | |||
655 | 655 | |||
656 | #define ROUND512(a,b,c,d,e,f,g,h) \ | 656 | #define ROUND512(a,b,c,d,e,f,g,h) \ | |
657 | s0 = W512[(j+1)&0x0f]; \ | 657 | s0 = W512[(j+1)&0x0f]; \ | |
658 | s0 = sigma0_512(s0); \ | 658 | s0 = sigma0_512(s0); \ | |
659 | s1 = W512[(j+14)&0x0f]; \ | 659 | s1 = W512[(j+14)&0x0f]; \ | |
660 | s1 = sigma1_512(s1); \ | 660 | s1 = sigma1_512(s1); \ | |
661 | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \ | 661 | T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \ | |
662 | (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \ | 662 | (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \ | |
663 | (d) += T1; \ | 663 | (d) += T1; \ | |
664 | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ | 664 | (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ | |
665 | j++ | 665 | j++ | |
666 | 666 | |||
667 | void | 667 | void | |
668 | SHA512_Transform(SHA512_CTX *context, const uint64_t *data) | 668 | netpgpv_SHA512_Transform(NETPGPV_SHA512_CTX *context, const uint64_t *data) | |
669 | { | 669 | { | |
670 | uint64_t a, b, c, d, e, f, g, h, s0, s1; | 670 | uint64_t a, b, c, d, e, f, g, h, s0, s1; | |
671 | uint64_t T1, *W512 = (uint64_t *)context->buffer; | 671 | uint64_t T1, *W512 = (uint64_t *)context->buffer; | |
672 | int j; | 672 | int j; | |
673 | 673 | |||
674 | /* Initialize registers with the prev. intermediate value */ | 674 | /* Initialize registers with the prev. intermediate value */ | |
675 | a = context->state[0]; | 675 | a = context->state[0]; | |
676 | b = context->state[1]; | 676 | b = context->state[1]; | |
677 | c = context->state[2]; | 677 | c = context->state[2]; | |
678 | d = context->state[3]; | 678 | d = context->state[3]; | |
679 | e = context->state[4]; | 679 | e = context->state[4]; | |
680 | f = context->state[5]; | 680 | f = context->state[5]; | |
681 | g = context->state[6]; | 681 | g = context->state[6]; | |
@@ -712,27 +712,27 @@ SHA512_Transform(SHA512_CTX *context, co | @@ -712,27 +712,27 @@ SHA512_Transform(SHA512_CTX *context, co | |||
712 | context->state[3] += d; | 712 | context->state[3] += d; | |
713 | context->state[4] += e; | 713 | context->state[4] += e; | |
714 | context->state[5] += f; | 714 | context->state[5] += f; | |
715 | context->state[6] += g; | 715 | context->state[6] += g; | |
716 | context->state[7] += h; | 716 | context->state[7] += h; | |
717 | 717 | |||
718 | /* Clean up */ | 718 | /* Clean up */ | |
719 | a = b = c = d = e = f = g = h = T1 = 0; | 719 | a = b = c = d = e = f = g = h = T1 = 0; | |
720 | } | 720 | } | |
721 | 721 | |||
722 | #else /* SHA2_UNROLL_TRANSFORM */ | 722 | #else /* SHA2_UNROLL_TRANSFORM */ | |
723 | 723 | |||
724 | void | 724 | void | |
725 | SHA512_Transform(SHA512_CTX *context, const uint64_t *data) | 725 | netpgpv_SHA512_Transform(NETPGPV_SHA512_CTX *context, const uint64_t *data) | |
726 | { | 726 | { | |
727 | uint64_t a, b, c, d, e, f, g, h, s0, s1; | 727 | uint64_t a, b, c, d, e, f, g, h, s0, s1; | |
728 | uint64_t T1, T2, *W512 = (void *)context->buffer; | 728 | uint64_t T1, T2, *W512 = (void *)context->buffer; | |
729 | int j; | 729 | int j; | |
730 | 730 | |||
731 | /* Initialize registers with the prev. intermediate value */ | 731 | /* Initialize registers with the prev. intermediate value */ | |
732 | a = context->state[0]; | 732 | a = context->state[0]; | |
733 | b = context->state[1]; | 733 | b = context->state[1]; | |
734 | c = context->state[2]; | 734 | c = context->state[2]; | |
735 | d = context->state[3]; | 735 | d = context->state[3]; | |
736 | e = context->state[4]; | 736 | e = context->state[4]; | |
737 | f = context->state[5]; | 737 | f = context->state[5]; | |
738 | g = context->state[6]; | 738 | g = context->state[6]; | |
@@ -787,27 +787,27 @@ SHA512_Transform(SHA512_CTX *context, co | @@ -787,27 +787,27 @@ SHA512_Transform(SHA512_CTX *context, co | |||
787 | context->state[3] += d; | 787 | context->state[3] += d; | |
788 | context->state[4] += e; | 788 | context->state[4] += e; | |
789 | context->state[5] += f; | 789 | context->state[5] += f; | |
790 | context->state[6] += g; | 790 | context->state[6] += g; | |
791 | context->state[7] += h; | 791 | context->state[7] += h; | |
792 | 792 | |||
793 | /* Clean up */ | 793 | /* Clean up */ | |
794 | a = b = c = d = e = f = g = h = T1 = T2 = 0; | 794 | a = b = c = d = e = f = g = h = T1 = T2 = 0; | |
795 | } | 795 | } | |
796 | 796 | |||
797 | #endif /* SHA2_UNROLL_TRANSFORM */ | 797 | #endif /* SHA2_UNROLL_TRANSFORM */ | |
798 | 798 | |||
799 | int | 799 | int | |
800 | SHA512_Update(SHA512_CTX *context, const uint8_t *data, size_t len) | 800 | netpgpv_SHA512_Update(NETPGPV_SHA512_CTX *context, const uint8_t *data, size_t len) | |
801 | { | 801 | { | |
802 | unsigned int freespace, usedspace; | 802 | unsigned int freespace, usedspace; | |
803 | 803 | |||
804 | if (len == 0) { | 804 | if (len == 0) { | |
805 | /* Calling with no data is valid - we do nothing */ | 805 | /* Calling with no data is valid - we do nothing */ | |
806 | return 1; | 806 | return 1; | |
807 | } | 807 | } | |
808 | 808 | |||
809 | usedspace = (unsigned int)((context->bitcount[0] >> 3) % | 809 | usedspace = (unsigned int)((context->bitcount[0] >> 3) % | |
810 | SHA512_BLOCK_LENGTH); | 810 | SHA512_BLOCK_LENGTH); | |
811 | if (usedspace > 0) { | 811 | if (usedspace > 0) { | |
812 | /* Calculate how much free space is available in the buffer */ | 812 | /* Calculate how much free space is available in the buffer */ | |
813 | freespace = SHA512_BLOCK_LENGTH - usedspace; | 813 | freespace = SHA512_BLOCK_LENGTH - usedspace; | |
@@ -857,27 +857,27 @@ SHA512_Update(SHA512_CTX *context, const | @@ -857,27 +857,27 @@ SHA512_Update(SHA512_CTX *context, const | |||
857 | } | 857 | } | |
858 | if (len > 0) { | 858 | if (len > 0) { | |
859 | /* There's left-overs, so save 'em */ | 859 | /* There's left-overs, so save 'em */ | |
860 | memcpy(context->buffer, data, len); | 860 | memcpy(context->buffer, data, len); | |
861 | ADDINC128(context->bitcount, len << 3); | 861 | ADDINC128(context->bitcount, len << 3); | |
862 | } | 862 | } | |
863 | /* Clean up: */ | 863 | /* Clean up: */ | |
864 | usedspace = freespace = 0; | 864 | usedspace = freespace = 0; | |
865 | 865 | |||
866 | return 1; | 866 | return 1; | |
867 | } | 867 | } | |
868 | 868 | |||
869 | static void | 869 | static void | |
870 | SHA512_Last(SHA512_CTX *context) | 870 | netpgpv_SHA512_Last(NETPGPV_SHA512_CTX *context) | |
871 | { | 871 | { | |
872 | unsigned int usedspace; | 872 | unsigned int usedspace; | |
873 | 873 | |||
874 | usedspace = (unsigned int)((context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH); | 874 | usedspace = (unsigned int)((context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH); | |
875 | context->bitcount[0] = htobe64(context->bitcount[0]); | 875 | context->bitcount[0] = htobe64(context->bitcount[0]); | |
876 | context->bitcount[1] = htobe64(context->bitcount[1]); | 876 | context->bitcount[1] = htobe64(context->bitcount[1]); | |
877 | if (usedspace > 0) { | 877 | if (usedspace > 0) { | |
878 | /* Begin padding with a 1 bit: */ | 878 | /* Begin padding with a 1 bit: */ | |
879 | context->buffer[usedspace++] = 0x80; | 879 | context->buffer[usedspace++] = 0x80; | |
880 | 880 | |||
881 | if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) { | 881 | if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) { | |
882 | /* Set-up for the last transform: */ | 882 | /* Set-up for the last transform: */ | |
883 | memset(&context->buffer[usedspace], 0, | 883 | memset(&context->buffer[usedspace], 0, | |
@@ -903,78 +903,78 @@ SHA512_Last(SHA512_CTX *context) | @@ -903,78 +903,78 @@ SHA512_Last(SHA512_CTX *context) | |||
903 | *context->buffer = 0x80; | 903 | *context->buffer = 0x80; | |
904 | } | 904 | } | |
905 | /* Store the length of input data (in bits): */ | 905 | /* Store the length of input data (in bits): */ | |
906 | memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], | 906 | memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], | |
907 | &context->bitcount[1], sizeof(context->bitcount[1])); | 907 | &context->bitcount[1], sizeof(context->bitcount[1])); | |
908 | memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8], | 908 | memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8], | |
909 | &context->bitcount[0], sizeof(context->bitcount[0])); | 909 | &context->bitcount[0], sizeof(context->bitcount[0])); | |
910 | 910 | |||
911 | /* Final transform: */ | 911 | /* Final transform: */ | |
912 | SHA512_Transform(context, (uint64_t *)(void *)context->buffer); | 912 | SHA512_Transform(context, (uint64_t *)(void *)context->buffer); | |
913 | } | 913 | } | |
914 | 914 | |||
915 | int | 915 | int | |
916 | SHA512_Final(uint8_t digest[], SHA512_CTX *context) | 916 | netpgpv_SHA512_Final(uint8_t digest[], NETPGPV_SHA512_CTX *context) | |
917 | { | 917 | { | |
918 | size_t i; | 918 | size_t i; | |
919 | 919 | |||
920 | /* If no digest buffer is passed, we don't bother doing this: */ | 920 | /* If no digest buffer is passed, we don't bother doing this: */ | |
921 | if (digest != NULL) { | 921 | if (digest != NULL) { | |
922 | SHA512_Last(context); | 922 | netpgpv_SHA512_Last(context); | |
923 | 923 | |||
924 | /* Save the hash data for output: */ | 924 | /* Save the hash data for output: */ | |
925 | for (i = 0; i < 8; ++i) | 925 | for (i = 0; i < 8; ++i) | |
926 | be64encode(digest + 8 * i, context->state[i]); | 926 | be64encode(digest + 8 * i, context->state[i]); | |
927 | } | 927 | } | |
928 | 928 | |||
929 | /* Zero out state data */ | 929 | /* Zero out state data */ | |
930 | memset(context, 0, sizeof(*context)); | 930 | memset(context, 0, sizeof(*context)); | |
931 | 931 | |||
932 | return 1; | 932 | return 1; | |
933 | } | 933 | } | |
934 | 934 | |||
935 | /*** SHA-384: *********************************************************/ | 935 | /*** SHA-384: *********************************************************/ | |
936 | int | 936 | int | |
937 | SHA384_Init(SHA384_CTX *context) | 937 | netpgpv_SHA384_Init(NETPGPV_SHA384_CTX *context) | |
938 | { | 938 | { | |
939 | if (context == NULL) | 939 | if (context == NULL) | |
940 | return 1; | 940 | return 1; | |
941 | 941 | |||
942 | memcpy(context->state, sha384_initial_hash_value, | 942 | memcpy(context->state, sha384_initial_hash_value, | |
943 | (size_t)(SHA512_DIGEST_LENGTH)); | 943 | (size_t)(SHA512_DIGEST_LENGTH)); | |
944 | memset(context->buffer, 0, (size_t)(SHA384_BLOCK_LENGTH)); | 944 | memset(context->buffer, 0, (size_t)(SHA384_BLOCK_LENGTH)); | |
945 | context->bitcount[0] = context->bitcount[1] = 0; | 945 | context->bitcount[0] = context->bitcount[1] = 0; | |
946 | 946 | |||
947 | return 1; | 947 | return 1; | |
948 | } | 948 | } | |
949 | 949 | |||
950 | int | 950 | int | |
951 | SHA384_Update(SHA384_CTX *context, const uint8_t *data, size_t len) | 951 | netpgpv_SHA384_Update(NETPGPV_SHA384_CTX *context, const uint8_t *data, size_t len) | |
952 | { | 952 | { | |
953 | return SHA512_Update((SHA512_CTX *)context, data, len); | 953 | return SHA512_Update((NETPGPV_SHA512_CTX *)context, data, len); | |
954 | } | 954 | } | |
955 | 955 | |||
956 | void | 956 | void | |
957 | SHA384_Transform(SHA512_CTX *context, const uint64_t *data) | 957 | netpgpv_SHA384_Transform(NETPGPV_SHA512_CTX *context, const uint64_t *data) | |
958 | { | 958 | { | |
959 | SHA512_Transform((SHA512_CTX *)context, data); | 959 | SHA512_Transform((NETPGPV_SHA512_CTX *)context, data); | |
960 | } | 960 | } | |
961 | 961 | |||
962 | int | 962 | int | |
963 | SHA384_Final(uint8_t digest[], SHA384_CTX *context) | 963 | netpgpv_SHA384_Final(uint8_t digest[], NETPGPV_SHA384_CTX *context) | |
964 | { | 964 | { | |
965 | size_t i; | 965 | size_t i; | |
966 | 966 | |||
967 | /* If no digest buffer is passed, we don't bother doing this: */ | 967 | /* If no digest buffer is passed, we don't bother doing this: */ | |
968 | if (digest != NULL) { | 968 | if (digest != NULL) { | |
969 | SHA512_Last((SHA512_CTX *)context); | 969 | netpgpv_SHA512_Last((NETPGPV_SHA512_CTX *)context); | |
970 | 970 | |||
971 | /* Save the hash data for output: */ | 971 | /* Save the hash data for output: */ | |
972 | for (i = 0; i < 6; ++i) | 972 | for (i = 0; i < 6; ++i) | |
973 | be64encode(digest + 8 * i, context->state[i]); | 973 | be64encode(digest + 8 * i, context->state[i]); | |
974 | } | 974 | } | |
975 | 975 | |||
976 | /* Zero out state data */ | 976 | /* Zero out state data */ | |
977 | memset(context, 0, sizeof(*context)); | 977 | memset(context, 0, sizeof(*context)); | |
978 | 978 | |||
979 | return 1; | 979 | return 1; | |
980 | } | 980 | } |
@@ -13,29 +13,29 @@ | @@ -13,29 +13,29 @@ | |||
13 | * | 13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | 24 | */ | |
25 | #ifndef NETPGP_VERIFY_H_ | 25 | #ifndef NETPGP_VERIFY_H_ | |
26 | #define NETPGP_VERIFY_H_ 20150205 | 26 | #define NETPGP_VERIFY_H_ 20150901 | |
27 | 27 | |||
28 | #define NETPGPVERIFY_VERSION "netpgpverify portable 20150205" | 28 | #define NETPGPVERIFY_VERSION "netpgpverify portable 20150901" | |
29 | 29 | |||
30 | #include <sys/types.h> | 30 | #include <sys/types.h> | |
31 | 31 | |||
32 | #include <inttypes.h> | 32 | #include <inttypes.h> | |
33 | 33 | |||
34 | #ifndef PGPV_ARRAY | 34 | #ifndef PGPV_ARRAY | |
35 | /* creates 2 unsigned vars called "name"c and "name"size in current scope */ | 35 | /* creates 2 unsigned vars called "name"c and "name"size in current scope */ | |
36 | /* also creates an array called "name"s in current scope */ | 36 | /* also creates an array called "name"s in current scope */ | |
37 | #define PGPV_ARRAY(type, name) \ | 37 | #define PGPV_ARRAY(type, name) \ | |
38 | unsigned name##c; unsigned name##vsize; type *name##s | 38 | unsigned name##c; unsigned name##vsize; type *name##s | |
39 | #endif | 39 | #endif | |
40 | 40 | |||
41 | /* 64bit key ids */ | 41 | /* 64bit key ids */ |