nettle-bugs
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2002 -----
- December
- November
- October
March 2020
- 9 participants
- 35 discussions
I'm looking at implementing elliptic curve point compression a la SEC1
(admittedly, mostly to reduce the number of "feature not supported" code
paths in a library, but it seems like a somewhat useful ability).
Nettle/Hogweed already implements it internally for curve25519, but I want
to implement it for the "secp" curves as well.
Point compression is easy enough, but point decompression requires some
curve math, potentially dependent on the specific curve, and some of it is
redundant with what's already done in ecc_point_set(). So I was thinking
about moving this functionality into Hogweed as a function along the lines
of ecc_point_set_compressed(), which would take, instead of a y-coordinate,
an int containing the sign/parity of the y-coordinate.
So my question for the list and for the maintainers is, is this a
reasonable API to add to Hogweed? Is there interest in including it in
Hogweed if I were to take the time to turn it into a tidy patch?
3
10
From: Dmitry Baryshkov <dbaryshkov(a)gmail.com>
GnuTLS is now compatible again with Nettle master branch. Remove
--disable-gost.
Signed-off-by: Dmitry Baryshkov <dbaryshkov(a)gmail.com>
---
.gitlab-ci.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 663f98f5cb8e..5b348f38568f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -100,7 +100,7 @@ build/gnutls:
make -j4 && make install
- git clone --depth 1 --branch master https://gitlab.com/gnutls/gnutls.git gnutls-git
- cd gnutls-git && git submodule update --init && ./bootstrap &&
- ./configure --disable-gost --disable-cxx --disable-guile --disable-doc && make -j$(nproc) &&
+ ./configure --disable-cxx --disable-guile --disable-doc && make -j$(nproc) &&
make -j $(nproc) check
tags:
- shared
--
2.25.0
3
2
08 May '20
From: Dmitry Baryshkov <dbaryshkov(a)gmail.com>
GOST curves will require different "fixups" for fast (mul X mod p)
operations. Move these operations to ecc_modulo structure and call them
via function pointer.
Signed-off-by: Dmitry Baryshkov <dbaryshkov(a)gmail.com>
---
ecc-add-jja.c | 8 ++++----
ecc-add-jjj.c | 8 ++++----
ecc-curve25519.c | 6 ++++++
ecc-curve448.c | 6 ++++++
ecc-dup-jj.c | 8 ++++----
ecc-gost-gc256b.c | 6 ++++++
ecc-gost-gc512a.c | 6 ++++++
ecc-internal.h | 25 ++++++++++++++++---------
ecc-mod-arith.c | 12 ++++++------
ecc-mul-m.c | 6 +++---
ecc-secp192r1.c | 6 ++++++
ecc-secp224r1.c | 6 ++++++
ecc-secp256r1.c | 6 ++++++
ecc-secp384r1.c | 6 ++++++
ecc-secp521r1.c | 6 ++++++
15 files changed, 91 insertions(+), 30 deletions(-)
diff --git a/ecc-add-jja.c b/ecc-add-jja.c
index 037711d38249..55ad954587da 100644
--- a/ecc-add-jja.c
+++ b/ecc-add-jja.c
@@ -102,10 +102,10 @@ ecc_add_jja (const struct ecc_curve *ecc,
/* w */
ecc_mod_mul (&ecc->p, j, y2, w);
ecc_mod_sub (&ecc->p, w, j, y1);
- ecc_mod_mul_1 (&ecc->p, w, w, 2);
+ ecc->p.mul_1 (&ecc->p, w, w, 2);
/* i replaces hh, j */
- ecc_mod_mul_1 (&ecc->p, hh, hh, 4);
+ ecc->p.mul_1 (&ecc->p, hh, hh, 4);
ecc_mod_mul (&ecc->p, j, hh, h);
/* v */
@@ -114,12 +114,12 @@ ecc_add_jja (const struct ecc_curve *ecc,
/* x_3, use (h, hh) as sqratch */
ecc_mod_sqr (&ecc->p, h, w);
ecc_mod_sub (&ecc->p, r, h, j);
- ecc_mod_submul_1 (&ecc->p, r, v, 2);
+ ecc->p.submul_1 (&ecc->p, r, v, 2);
/* y_3, use (h, hh) as sqratch */
ecc_mod_mul (&ecc->p, h, y1, j); /* frees j */
ecc_mod_sub (&ecc->p, r + ecc->p.size, v, r);
ecc_mod_mul (&ecc->p, j, r + ecc->p.size, w);
- ecc_mod_submul_1 (&ecc->p, j, h, 2);
+ ecc->p.submul_1 (&ecc->p, j, h, 2);
mpn_copyi (r + ecc->p.size, j, ecc->p.size);
}
diff --git a/ecc-add-jjj.c b/ecc-add-jjj.c
index 54b2246aeb24..cad26193234a 100644
--- a/ecc-add-jjj.c
+++ b/ecc-add-jjj.c
@@ -94,14 +94,14 @@ ecc_add_jjj (const struct ecc_curve *ecc,
ecc_mod_mul (&ecc->p, s1, p + ecc->p.size, v);
ecc_mod_mul (&ecc->p, v, j, q + ecc->p.size);
ecc_mod_sub (&ecc->p, s2, v, s1);
- ecc_mod_mul_1 (&ecc->p, s2, s2, 2);
+ ecc->p.mul_1 (&ecc->p, s2, s2, 2);
/* Store z3 */
mpn_copyi (r + 2*ecc->p.size, i, ecc->p.size);
/* i, j, v */
ecc_mod_sqr (&ecc->p, i, u2);
- ecc_mod_mul_1 (&ecc->p, i, i, 4);
+ ecc->p.mul_1 (&ecc->p, i, i, 4);
ecc_mod_mul (&ecc->p, j, u2, i);
ecc_mod_mul (&ecc->p, v, u1, i);
@@ -109,12 +109,12 @@ ecc_add_jjj (const struct ecc_curve *ecc,
/* x3, use u1, u2 as scratch */
ecc_mod_sqr (&ecc->p, u1, s2);
ecc_mod_sub (&ecc->p, r, u1, j);
- ecc_mod_submul_1 (&ecc->p, r, v, 2);
+ ecc->p.submul_1 (&ecc->p, r, v, 2);
/* y3 */
ecc_mod_mul (&ecc->p, u1, s1, j); /* Frees j */
ecc_mod_sub (&ecc->p, u2, v, r); /* Frees v */
ecc_mod_mul (&ecc->p, i, s2, u2);
- ecc_mod_submul_1 (&ecc->p, i, u1, 2);
+ ecc->p.submul_1 (&ecc->p, i, u1, 2);
mpn_copyi (r + ecc->p.size, i, ecc->p.size);
}
diff --git a/ecc-curve25519.c b/ecc-curve25519.c
index f8f2c64af868..04df696f7357 100644
--- a/ecc-curve25519.c
+++ b/ecc-curve25519.c
@@ -310,6 +310,9 @@ const struct ecc_curve _nettle_curve25519 =
ecc_curve25519_modp,
ecc_curve25519_inv,
ecc_curve25519_sqrt,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
253,
@@ -329,6 +332,9 @@ const struct ecc_curve _nettle_curve25519 =
ecc_curve25519_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
0, /* No redc */
diff --git a/ecc-curve448.c b/ecc-curve448.c
index 484b7d1e0870..ce7a25d14c4e 100644
--- a/ecc-curve448.c
+++ b/ecc-curve448.c
@@ -288,6 +288,9 @@ const struct ecc_curve _nettle_curve448 =
ecc_curve448_modp,
ecc_curve448_inv,
ecc_curve448_sqrt,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
446,
@@ -307,6 +310,9 @@ const struct ecc_curve _nettle_curve448 =
ecc_mod, /* FIXME: Implement optimized reduce function */
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
0, /* No redc */
diff --git a/ecc-dup-jj.c b/ecc-dup-jj.c
index 2247e8fdfd5a..4bbd5163c0e3 100644
--- a/ecc-dup-jj.c
+++ b/ecc-dup-jj.c
@@ -87,7 +87,7 @@ ecc_dup_jj (const struct ecc_curve *ecc,
ecc_mod_add (&ecc->p, sum, xp, delta);
ecc_mod_sub (&ecc->p, delta, xp, delta);
ecc_mod_mul (&ecc->p, beta, sum, delta);
- ecc_mod_mul_1 (&ecc->p, alpha, beta, 3);
+ ecc->p.mul_1 (&ecc->p, alpha, beta, 3);
/* beta */
ecc_mod_mul (&ecc->p, beta, xp, gamma);
@@ -95,16 +95,16 @@ ecc_dup_jj (const struct ecc_curve *ecc,
/* Do gamma^2 and 4*beta early, to get them out of the way. We can
then use the old area at gamma as scratch. */
ecc_mod_sqr (&ecc->p, g2, gamma);
- ecc_mod_mul_1 (&ecc->p, sum, beta, 4);
+ ecc->p.mul_1 (&ecc->p, sum, beta, 4);
/* x' */
ecc_mod_sqr (&ecc->p, gamma, alpha); /* Overwrites gamma and beta */
- ecc_mod_submul_1 (&ecc->p, gamma, sum, 2);
+ ecc->p.submul_1 (&ecc->p, gamma, sum, 2);
mpn_copyi (r, gamma, ecc->p.size);
/* y' */
ecc_mod_sub (&ecc->p, sum, sum, r);
ecc_mod_mul (&ecc->p, gamma, sum, alpha);
- ecc_mod_submul_1 (&ecc->p, gamma, g2, 8);
+ ecc->p.submul_1 (&ecc->p, gamma, g2, 8);
mpn_copyi (r + ecc->p.size, gamma, ecc->p.size);
}
diff --git a/ecc-gost-gc256b.c b/ecc-gost-gc256b.c
index a23d46fc8af6..24e1ac6c99a7 100644
--- a/ecc-gost-gc256b.c
+++ b/ecc-gost-gc256b.c
@@ -77,6 +77,9 @@ const struct ecc_curve _nettle_gost_gc256b =
ecc_gost_gc256b_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
256,
@@ -96,6 +99,9 @@ const struct ecc_curve _nettle_gost_gc256b =
ecc_gost_gc256b_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-gost-gc512a.c b/ecc-gost-gc512a.c
index 398762c337d6..5de4eda85d9c 100644
--- a/ecc-gost-gc512a.c
+++ b/ecc-gost-gc512a.c
@@ -77,6 +77,9 @@ const struct ecc_curve _nettle_gost_gc512a =
ecc_gost_gc512a_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
512,
@@ -96,6 +99,9 @@ const struct ecc_curve _nettle_gost_gc512a =
ecc_gost_gc512a_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-internal.h b/ecc-internal.h
index 9e24e0ce4521..e1380bfb2b20 100644
--- a/ecc-internal.h
+++ b/ecc-internal.h
@@ -44,9 +44,9 @@
#define ecc_pm1_redc _nettle_ecc_pm1_redc
#define ecc_mod_add _nettle_ecc_mod_add
#define ecc_mod_sub _nettle_ecc_mod_sub
-#define ecc_mod_mul_1 _nettle_ecc_mod_mul_1
-#define ecc_mod_addmul_1 _nettle_ecc_mod_addmul_1
-#define ecc_mod_submul_1 _nettle_ecc_mod_submul_1
+#define ecc_mod_mul_1_std _nettle_ecc_mod_mul_1_std
+#define ecc_mod_addmul_1_std _nettle_ecc_mod_addmul_1_std
+#define ecc_mod_submul_1_std _nettle_ecc_mod_submul_1_std
#define ecc_mod_mul _nettle_ecc_mod_mul
#define ecc_mod_sqr _nettle_ecc_mod_sqr
#define ecc_mod_random _nettle_ecc_mod_random
@@ -146,6 +146,10 @@ typedef void ecc_h_to_a_func (const struct ecc_curve *ecc,
mp_limb_t *r, const mp_limb_t *p,
mp_limb_t *scratch);
+typedef void ecc_mod_mul_1_func (const struct ecc_modulo *m,
+ mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b);
+
struct ecc_modulo
{
unsigned short bit_size;
@@ -170,6 +174,9 @@ struct ecc_modulo
ecc_mod_func *reduce;
ecc_mod_inv_func *invert;
ecc_mod_sqrt_func *sqrt;
+
+ ecc_mod_mul_1_func *mul_1;
+ ecc_mod_mul_1_func *submul_1;
};
/* Represents an elliptic curve of the form
@@ -237,15 +244,15 @@ ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
const mp_limb_t *ap, const mp_limb_t *bp);
void
-ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, const mp_limb_t b);
+ecc_mod_mul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, const mp_limb_t b);
void
-ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b);
+ecc_mod_addmul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b);
void
-ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b);
+ecc_mod_submul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b);
/* The mul and sqr functions need 2*m->size limbs at rp */
void
diff --git a/ecc-mod-arith.c b/ecc-mod-arith.c
index f2e47f6747c1..0399a2cdd7c5 100644
--- a/ecc-mod-arith.c
+++ b/ecc-mod-arith.c
@@ -65,8 +65,8 @@ ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
}
void
-ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b)
+ecc_mod_mul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
@@ -80,8 +80,8 @@ ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
}
void
-ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b)
+ecc_mod_addmul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
@@ -95,8 +95,8 @@ ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
}
void
-ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
- const mp_limb_t *ap, mp_limb_t b)
+ecc_mod_submul_1_std (const struct ecc_modulo *m, mp_limb_t *rp,
+ const mp_limb_t *ap, mp_limb_t b)
{
mp_limb_t hi;
diff --git a/ecc-mul-m.c b/ecc-mul-m.c
index 68bdd16e8e94..539b9d0677e7 100644
--- a/ecc-mul-m.c
+++ b/ecc-mul-m.c
@@ -80,7 +80,7 @@ ecc_mul_m (const struct ecc_modulo *m,
ecc_mod_sqr (m, BB, B);
ecc_mod_mul (m, x3, AA, BB);
ecc_mod_sub (m, E, AA, BB);
- ecc_mod_addmul_1 (m, AA, E, a24);
+ ecc_mod_addmul_1_std (m, AA, E, a24);
ecc_mod_mul (m, z3, E, AA);
for (i = bit_high; i >= bit_low; i--)
@@ -98,7 +98,7 @@ ecc_mul_m (const struct ecc_modulo *m,
ecc_mod_sqr (m, BB, B);
ecc_mod_mul (m, x2, AA, BB); /* Last use of BB */
ecc_mod_sub (m, E, AA, BB);
- ecc_mod_addmul_1 (m, AA, E, a24);
+ ecc_mod_addmul_1_std (m, AA, E, a24);
ecc_mod_add (m, C, x3, z3);
ecc_mod_sub (m, D, x3, z3);
ecc_mod_mul (m, z2, E, AA); /* Last use of E and AA */
@@ -124,7 +124,7 @@ ecc_mul_m (const struct ecc_modulo *m,
ecc_mod_sqr (m, BB, B);
ecc_mod_mul (m, x2, AA, BB);
ecc_mod_sub (m, E, AA, BB);
- ecc_mod_addmul_1 (m, AA, E, a24);
+ ecc_mod_addmul_1_std (m, AA, E, a24);
ecc_mod_mul (m, z2, E, AA);
}
assert (m->invert_itch <= 7 * m->size);
diff --git a/ecc-secp192r1.c b/ecc-secp192r1.c
index 046026f3f697..79080495ec7e 100644
--- a/ecc-secp192r1.c
+++ b/ecc-secp192r1.c
@@ -130,6 +130,9 @@ const struct ecc_curve _nettle_secp_192r1 =
ecc_secp192r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
192,
@@ -149,6 +152,9 @@ const struct ecc_curve _nettle_secp_192r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp224r1.c b/ecc-secp224r1.c
index 05d84017a68a..064a9e2f7fd4 100644
--- a/ecc-secp224r1.c
+++ b/ecc-secp224r1.c
@@ -82,6 +82,9 @@ const struct ecc_curve _nettle_secp_224r1 =
USE_REDC ? ecc_secp224r1_redc : ecc_secp224r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
224,
@@ -101,6 +104,9 @@ const struct ecc_curve _nettle_secp_224r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp256r1.c b/ecc-secp256r1.c
index d399642453d5..0a25a086f3fe 100644
--- a/ecc-secp256r1.c
+++ b/ecc-secp256r1.c
@@ -259,6 +259,9 @@ const struct ecc_curve _nettle_secp_256r1 =
USE_REDC ? ecc_secp256r1_redc : ecc_secp256r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
256,
@@ -278,6 +281,9 @@ const struct ecc_curve _nettle_secp_256r1 =
ecc_secp256r1_modq,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp384r1.c b/ecc-secp384r1.c
index 54bcd1128d39..5a9131f72c98 100644
--- a/ecc-secp384r1.c
+++ b/ecc-secp384r1.c
@@ -167,6 +167,9 @@ const struct ecc_curve _nettle_secp_384r1 =
ecc_secp384r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
384,
@@ -186,6 +189,9 @@ const struct ecc_curve _nettle_secp_384r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
diff --git a/ecc-secp521r1.c b/ecc-secp521r1.c
index 776f7ae03e27..f01a97537eb8 100644
--- a/ecc-secp521r1.c
+++ b/ecc-secp521r1.c
@@ -95,6 +95,9 @@ const struct ecc_curve _nettle_secp_521r1 =
ecc_secp521r1_modp,
ecc_mod_inv,
NULL,
+
+ ecc_mod_mul_1_std,
+ ecc_mod_submul_1_std,
},
{
521,
@@ -114,6 +117,9 @@ const struct ecc_curve _nettle_secp_521r1 =
ecc_mod,
ecc_mod_inv,
NULL,
+
+ NULL,
+ NULL,
},
USE_REDC,
--
2.25.0
2
2
[PATCH 1/6] Start separating GOST 28147-89 from GOST R 34.11-94
by Dmitry Eremin-Solenikov 19 Apr '20
by Dmitry Eremin-Solenikov 19 Apr '20
19 Apr '20
Hash function GOST R 34.11-94 (gosthash94) in its compression function
uses Russian block cipher (GOST 28147-89, Magma). Start separating block
cipher code from hash function code. For now there is no public
interface for this cipher, it will be added later.
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov(a)gmail.com>
---
Makefile.in | 4 +-
gost28147.c | 342 +++++++++++++++++++++++++++++++++++++++++++++++++++
gost28147.h | 63 ++++++++++
gosthash94.c | 245 +-----------------------------------
4 files changed, 412 insertions(+), 242 deletions(-)
create mode 100644 gost28147.c
create mode 100644 gost28147.h
diff --git a/Makefile.in b/Makefile.in
index 537e97d19bea..795d66edb2c8 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -102,7 +102,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
gcm-camellia128.c gcm-camellia128-meta.c \
gcm-camellia256.c gcm-camellia256-meta.c \
cmac.c cmac-aes128.c cmac-aes256.c \
- gosthash94.c gosthash94-meta.c \
+ gost28147.c gosthash94.c gosthash94-meta.c \
hmac.c hmac-md5.c hmac-ripemd160.c hmac-sha1.c \
hmac-sha224.c hmac-sha256.c hmac-sha384.c hmac-sha512.c \
knuth-lfib.c hkdf.c \
@@ -195,7 +195,7 @@ HEADERS = aes.h arcfour.h arctwo.h asn1.h blowfish.h \
cbc.h ccm.h cfb.h chacha.h chacha-poly1305.h ctr.h \
curve25519.h des.h dsa.h dsa-compat.h eax.h \
ecc-curve.h ecc.h ecdsa.h eddsa.h \
- gcm.h gosthash94.h hmac.h \
+ gcm.h gost28147.h gosthash94.h hmac.h \
knuth-lfib.h hkdf.h \
macros.h \
cmac.h \
diff --git a/gost28147.c b/gost28147.c
new file mode 100644
index 000000000000..c734c4365fa7
--- /dev/null
+++ b/gost28147.c
@@ -0,0 +1,342 @@
+/* gost28147.c - GOST 28147-89 cipher implementation
+ *
+ * based on Russian standard GOST 28147-89
+ * For English description, check RFC 5830.
+ * S-Boxes are expanded from the tables defined in RFC4357:
+ * https://tools.ietf.org/html/rfc4357
+ *
+ * Copyright: 2019 Dmitry Eremin-Solenikov <dbaryshkov(a)gmail.com>
+ * Copyright: 2009-2012 Aleksey Kravchenko <rhash.admin(a)gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "macros.h"
+#include "gost28147.h"
+
+/* pre-initialized GOST lookup tables based on rotated S-Box */
+const struct gost28147_param gost28147_param_test_3411 =
+{
+ {
+ /* 0 */
+ 0x00072000, 0x00075000, 0x00074800, 0x00071000,
+ 0x00076800, 0x00074000, 0x00070000, 0x00077000,
+ 0x00073000, 0x00075800, 0x00070800, 0x00076000,
+ 0x00073800, 0x00077800, 0x00072800, 0x00071800,
+ 0x0005a000, 0x0005d000, 0x0005c800, 0x00059000,
+ 0x0005e800, 0x0005c000, 0x00058000, 0x0005f000,
+ 0x0005b000, 0x0005d800, 0x00058800, 0x0005e000,
+ 0x0005b800, 0x0005f800, 0x0005a800, 0x00059800,
+ 0x00022000, 0x00025000, 0x00024800, 0x00021000,
+ 0x00026800, 0x00024000, 0x00020000, 0x00027000,
+ 0x00023000, 0x00025800, 0x00020800, 0x00026000,
+ 0x00023800, 0x00027800, 0x00022800, 0x00021800,
+ 0x00062000, 0x00065000, 0x00064800, 0x00061000,
+ 0x00066800, 0x00064000, 0x00060000, 0x00067000,
+ 0x00063000, 0x00065800, 0x00060800, 0x00066000,
+ 0x00063800, 0x00067800, 0x00062800, 0x00061800,
+ 0x00032000, 0x00035000, 0x00034800, 0x00031000,
+ 0x00036800, 0x00034000, 0x00030000, 0x00037000,
+ 0x00033000, 0x00035800, 0x00030800, 0x00036000,
+ 0x00033800, 0x00037800, 0x00032800, 0x00031800,
+ 0x0006a000, 0x0006d000, 0x0006c800, 0x00069000,
+ 0x0006e800, 0x0006c000, 0x00068000, 0x0006f000,
+ 0x0006b000, 0x0006d800, 0x00068800, 0x0006e000,
+ 0x0006b800, 0x0006f800, 0x0006a800, 0x00069800,
+ 0x0007a000, 0x0007d000, 0x0007c800, 0x00079000,
+ 0x0007e800, 0x0007c000, 0x00078000, 0x0007f000,
+ 0x0007b000, 0x0007d800, 0x00078800, 0x0007e000,
+ 0x0007b800, 0x0007f800, 0x0007a800, 0x00079800,
+ 0x00052000, 0x00055000, 0x00054800, 0x00051000,
+ 0x00056800, 0x00054000, 0x00050000, 0x00057000,
+ 0x00053000, 0x00055800, 0x00050800, 0x00056000,
+ 0x00053800, 0x00057800, 0x00052800, 0x00051800,
+ 0x00012000, 0x00015000, 0x00014800, 0x00011000,
+ 0x00016800, 0x00014000, 0x00010000, 0x00017000,
+ 0x00013000, 0x00015800, 0x00010800, 0x00016000,
+ 0x00013800, 0x00017800, 0x00012800, 0x00011800,
+ 0x0001a000, 0x0001d000, 0x0001c800, 0x00019000,
+ 0x0001e800, 0x0001c000, 0x00018000, 0x0001f000,
+ 0x0001b000, 0x0001d800, 0x00018800, 0x0001e000,
+ 0x0001b800, 0x0001f800, 0x0001a800, 0x00019800,
+ 0x00042000, 0x00045000, 0x00044800, 0x00041000,
+ 0x00046800, 0x00044000, 0x00040000, 0x00047000,
+ 0x00043000, 0x00045800, 0x00040800, 0x00046000,
+ 0x00043800, 0x00047800, 0x00042800, 0x00041800,
+ 0x0000a000, 0x0000d000, 0x0000c800, 0x00009000,
+ 0x0000e800, 0x0000c000, 0x00008000, 0x0000f000,
+ 0x0000b000, 0x0000d800, 0x00008800, 0x0000e000,
+ 0x0000b800, 0x0000f800, 0x0000a800, 0x00009800,
+ 0x00002000, 0x00005000, 0x00004800, 0x00001000,
+ 0x00006800, 0x00004000, 0x00000000, 0x00007000,
+ 0x00003000, 0x00005800, 0x00000800, 0x00006000,
+ 0x00003800, 0x00007800, 0x00002800, 0x00001800,
+ 0x0003a000, 0x0003d000, 0x0003c800, 0x00039000,
+ 0x0003e800, 0x0003c000, 0x00038000, 0x0003f000,
+ 0x0003b000, 0x0003d800, 0x00038800, 0x0003e000,
+ 0x0003b800, 0x0003f800, 0x0003a800, 0x00039800,
+ 0x0002a000, 0x0002d000, 0x0002c800, 0x00029000,
+ 0x0002e800, 0x0002c000, 0x00028000, 0x0002f000,
+ 0x0002b000, 0x0002d800, 0x00028800, 0x0002e000,
+ 0x0002b800, 0x0002f800, 0x0002a800, 0x00029800,
+ 0x0004a000, 0x0004d000, 0x0004c800, 0x00049000,
+ 0x0004e800, 0x0004c000, 0x00048000, 0x0004f000,
+ 0x0004b000, 0x0004d800, 0x00048800, 0x0004e000,
+ 0x0004b800, 0x0004f800, 0x0004a800, 0x00049800,
+ /* 1 */
+ 0x03a80000, 0x03c00000, 0x03880000, 0x03e80000,
+ 0x03d00000, 0x03980000, 0x03a00000, 0x03900000,
+ 0x03f00000, 0x03f80000, 0x03e00000, 0x03b80000,
+ 0x03b00000, 0x03800000, 0x03c80000, 0x03d80000,
+ 0x06a80000, 0x06c00000, 0x06880000, 0x06e80000,
+ 0x06d00000, 0x06980000, 0x06a00000, 0x06900000,
+ 0x06f00000, 0x06f80000, 0x06e00000, 0x06b80000,
+ 0x06b00000, 0x06800000, 0x06c80000, 0x06d80000,
+ 0x05280000, 0x05400000, 0x05080000, 0x05680000,
+ 0x05500000, 0x05180000, 0x05200000, 0x05100000,
+ 0x05700000, 0x05780000, 0x05600000, 0x05380000,
+ 0x05300000, 0x05000000, 0x05480000, 0x05580000,
+ 0x00a80000, 0x00c00000, 0x00880000, 0x00e80000,
+ 0x00d00000, 0x00980000, 0x00a00000, 0x00900000,
+ 0x00f00000, 0x00f80000, 0x00e00000, 0x00b80000,
+ 0x00b00000, 0x00800000, 0x00c80000, 0x00d80000,
+ 0x00280000, 0x00400000, 0x00080000, 0x00680000,
+ 0x00500000, 0x00180000, 0x00200000, 0x00100000,
+ 0x00700000, 0x00780000, 0x00600000, 0x00380000,
+ 0x00300000, 0x00000000, 0x00480000, 0x00580000,
+ 0x04280000, 0x04400000, 0x04080000, 0x04680000,
+ 0x04500000, 0x04180000, 0x04200000, 0x04100000,
+ 0x04700000, 0x04780000, 0x04600000, 0x04380000,
+ 0x04300000, 0x04000000, 0x04480000, 0x04580000,
+ 0x04a80000, 0x04c00000, 0x04880000, 0x04e80000,
+ 0x04d00000, 0x04980000, 0x04a00000, 0x04900000,
+ 0x04f00000, 0x04f80000, 0x04e00000, 0x04b80000,
+ 0x04b00000, 0x04800000, 0x04c80000, 0x04d80000,
+ 0x07a80000, 0x07c00000, 0x07880000, 0x07e80000,
+ 0x07d00000, 0x07980000, 0x07a00000, 0x07900000,
+ 0x07f00000, 0x07f80000, 0x07e00000, 0x07b80000,
+ 0x07b00000, 0x07800000, 0x07c80000, 0x07d80000,
+ 0x07280000, 0x07400000, 0x07080000, 0x07680000,
+ 0x07500000, 0x07180000, 0x07200000, 0x07100000,
+ 0x07700000, 0x07780000, 0x07600000, 0x07380000,
+ 0x07300000, 0x07000000, 0x07480000, 0x07580000,
+ 0x02280000, 0x02400000, 0x02080000, 0x02680000,
+ 0x02500000, 0x02180000, 0x02200000, 0x02100000,
+ 0x02700000, 0x02780000, 0x02600000, 0x02380000,
+ 0x02300000, 0x02000000, 0x02480000, 0x02580000,
+ 0x03280000, 0x03400000, 0x03080000, 0x03680000,
+ 0x03500000, 0x03180000, 0x03200000, 0x03100000,
+ 0x03700000, 0x03780000, 0x03600000, 0x03380000,
+ 0x03300000, 0x03000000, 0x03480000, 0x03580000,
+ 0x06280000, 0x06400000, 0x06080000, 0x06680000,
+ 0x06500000, 0x06180000, 0x06200000, 0x06100000,
+ 0x06700000, 0x06780000, 0x06600000, 0x06380000,
+ 0x06300000, 0x06000000, 0x06480000, 0x06580000,
+ 0x05a80000, 0x05c00000, 0x05880000, 0x05e80000,
+ 0x05d00000, 0x05980000, 0x05a00000, 0x05900000,
+ 0x05f00000, 0x05f80000, 0x05e00000, 0x05b80000,
+ 0x05b00000, 0x05800000, 0x05c80000, 0x05d80000,
+ 0x01280000, 0x01400000, 0x01080000, 0x01680000,
+ 0x01500000, 0x01180000, 0x01200000, 0x01100000,
+ 0x01700000, 0x01780000, 0x01600000, 0x01380000,
+ 0x01300000, 0x01000000, 0x01480000, 0x01580000,
+ 0x02a80000, 0x02c00000, 0x02880000, 0x02e80000,
+ 0x02d00000, 0x02980000, 0x02a00000, 0x02900000,
+ 0x02f00000, 0x02f80000, 0x02e00000, 0x02b80000,
+ 0x02b00000, 0x02800000, 0x02c80000, 0x02d80000,
+ 0x01a80000, 0x01c00000, 0x01880000, 0x01e80000,
+ 0x01d00000, 0x01980000, 0x01a00000, 0x01900000,
+ 0x01f00000, 0x01f80000, 0x01e00000, 0x01b80000,
+ 0x01b00000, 0x01800000, 0x01c80000, 0x01d80000,
+ /* 2 */
+ 0x30000002, 0x60000002, 0x38000002, 0x08000002,
+ 0x28000002, 0x78000002, 0x68000002, 0x40000002,
+ 0x20000002, 0x50000002, 0x48000002, 0x70000002,
+ 0x00000002, 0x18000002, 0x58000002, 0x10000002,
+ 0xb0000005, 0xe0000005, 0xb8000005, 0x88000005,
+ 0xa8000005, 0xf8000005, 0xe8000005, 0xc0000005,
+ 0xa0000005, 0xd0000005, 0xc8000005, 0xf0000005,
+ 0x80000005, 0x98000005, 0xd8000005, 0x90000005,
+ 0x30000005, 0x60000005, 0x38000005, 0x08000005,
+ 0x28000005, 0x78000005, 0x68000005, 0x40000005,
+ 0x20000005, 0x50000005, 0x48000005, 0x70000005,
+ 0x00000005, 0x18000005, 0x58000005, 0x10000005,
+ 0x30000000, 0x60000000, 0x38000000, 0x08000000,
+ 0x28000000, 0x78000000, 0x68000000, 0x40000000,
+ 0x20000000, 0x50000000, 0x48000000, 0x70000000,
+ 0x00000000, 0x18000000, 0x58000000, 0x10000000,
+ 0xb0000003, 0xe0000003, 0xb8000003, 0x88000003,
+ 0xa8000003, 0xf8000003, 0xe8000003, 0xc0000003,
+ 0xa0000003, 0xd0000003, 0xc8000003, 0xf0000003,
+ 0x80000003, 0x98000003, 0xd8000003, 0x90000003,
+ 0x30000001, 0x60000001, 0x38000001, 0x08000001,
+ 0x28000001, 0x78000001, 0x68000001, 0x40000001,
+ 0x20000001, 0x50000001, 0x48000001, 0x70000001,
+ 0x00000001, 0x18000001, 0x58000001, 0x10000001,
+ 0xb0000000, 0xe0000000, 0xb8000000, 0x88000000,
+ 0xa8000000, 0xf8000000, 0xe8000000, 0xc0000000,
+ 0xa0000000, 0xd0000000, 0xc8000000, 0xf0000000,
+ 0x80000000, 0x98000000, 0xd8000000, 0x90000000,
+ 0xb0000006, 0xe0000006, 0xb8000006, 0x88000006,
+ 0xa8000006, 0xf8000006, 0xe8000006, 0xc0000006,
+ 0xa0000006, 0xd0000006, 0xc8000006, 0xf0000006,
+ 0x80000006, 0x98000006, 0xd8000006, 0x90000006,
+ 0xb0000001, 0xe0000001, 0xb8000001, 0x88000001,
+ 0xa8000001, 0xf8000001, 0xe8000001, 0xc0000001,
+ 0xa0000001, 0xd0000001, 0xc8000001, 0xf0000001,
+ 0x80000001, 0x98000001, 0xd8000001, 0x90000001,
+ 0x30000003, 0x60000003, 0x38000003, 0x08000003,
+ 0x28000003, 0x78000003, 0x68000003, 0x40000003,
+ 0x20000003, 0x50000003, 0x48000003, 0x70000003,
+ 0x00000003, 0x18000003, 0x58000003, 0x10000003,
+ 0x30000004, 0x60000004, 0x38000004, 0x08000004,
+ 0x28000004, 0x78000004, 0x68000004, 0x40000004,
+ 0x20000004, 0x50000004, 0x48000004, 0x70000004,
+ 0x00000004, 0x18000004, 0x58000004, 0x10000004,
+ 0xb0000002, 0xe0000002, 0xb8000002, 0x88000002,
+ 0xa8000002, 0xf8000002, 0xe8000002, 0xc0000002,
+ 0xa0000002, 0xd0000002, 0xc8000002, 0xf0000002,
+ 0x80000002, 0x98000002, 0xd8000002, 0x90000002,
+ 0xb0000004, 0xe0000004, 0xb8000004, 0x88000004,
+ 0xa8000004, 0xf8000004, 0xe8000004, 0xc0000004,
+ 0xa0000004, 0xd0000004, 0xc8000004, 0xf0000004,
+ 0x80000004, 0x98000004, 0xd8000004, 0x90000004,
+ 0x30000006, 0x60000006, 0x38000006, 0x08000006,
+ 0x28000006, 0x78000006, 0x68000006, 0x40000006,
+ 0x20000006, 0x50000006, 0x48000006, 0x70000006,
+ 0x00000006, 0x18000006, 0x58000006, 0x10000006,
+ 0xb0000007, 0xe0000007, 0xb8000007, 0x88000007,
+ 0xa8000007, 0xf8000007, 0xe8000007, 0xc0000007,
+ 0xa0000007, 0xd0000007, 0xc8000007, 0xf0000007,
+ 0x80000007, 0x98000007, 0xd8000007, 0x90000007,
+ 0x30000007, 0x60000007, 0x38000007, 0x08000007,
+ 0x28000007, 0x78000007, 0x68000007, 0x40000007,
+ 0x20000007, 0x50000007, 0x48000007, 0x70000007,
+ 0x00000007, 0x18000007, 0x58000007, 0x10000007,
+ /* 3 */
+ 0x000000e8, 0x000000d8, 0x000000a0, 0x00000088,
+ 0x00000098, 0x000000f8, 0x000000a8, 0x000000c8,
+ 0x00000080, 0x000000d0, 0x000000f0, 0x000000b8,
+ 0x000000b0, 0x000000c0, 0x00000090, 0x000000e0,
+ 0x000007e8, 0x000007d8, 0x000007a0, 0x00000788,
+ 0x00000798, 0x000007f8, 0x000007a8, 0x000007c8,
+ 0x00000780, 0x000007d0, 0x000007f0, 0x000007b8,
+ 0x000007b0, 0x000007c0, 0x00000790, 0x000007e0,
+ 0x000006e8, 0x000006d8, 0x000006a0, 0x00000688,
+ 0x00000698, 0x000006f8, 0x000006a8, 0x000006c8,
+ 0x00000680, 0x000006d0, 0x000006f0, 0x000006b8,
+ 0x000006b0, 0x000006c0, 0x00000690, 0x000006e0,
+ 0x00000068, 0x00000058, 0x00000020, 0x00000008,
+ 0x00000018, 0x00000078, 0x00000028, 0x00000048,
+ 0x00000000, 0x00000050, 0x00000070, 0x00000038,
+ 0x00000030, 0x00000040, 0x00000010, 0x00000060,
+ 0x000002e8, 0x000002d8, 0x000002a0, 0x00000288,
+ 0x00000298, 0x000002f8, 0x000002a8, 0x000002c8,
+ 0x00000280, 0x000002d0, 0x000002f0, 0x000002b8,
+ 0x000002b0, 0x000002c0, 0x00000290, 0x000002e0,
+ 0x000003e8, 0x000003d8, 0x000003a0, 0x00000388,
+ 0x00000398, 0x000003f8, 0x000003a8, 0x000003c8,
+ 0x00000380, 0x000003d0, 0x000003f0, 0x000003b8,
+ 0x000003b0, 0x000003c0, 0x00000390, 0x000003e0,
+ 0x00000568, 0x00000558, 0x00000520, 0x00000508,
+ 0x00000518, 0x00000578, 0x00000528, 0x00000548,
+ 0x00000500, 0x00000550, 0x00000570, 0x00000538,
+ 0x00000530, 0x00000540, 0x00000510, 0x00000560,
+ 0x00000268, 0x00000258, 0x00000220, 0x00000208,
+ 0x00000218, 0x00000278, 0x00000228, 0x00000248,
+ 0x00000200, 0x00000250, 0x00000270, 0x00000238,
+ 0x00000230, 0x00000240, 0x00000210, 0x00000260,
+ 0x000004e8, 0x000004d8, 0x000004a0, 0x00000488,
+ 0x00000498, 0x000004f8, 0x000004a8, 0x000004c8,
+ 0x00000480, 0x000004d0, 0x000004f0, 0x000004b8,
+ 0x000004b0, 0x000004c0, 0x00000490, 0x000004e0,
+ 0x00000168, 0x00000158, 0x00000120, 0x00000108,
+ 0x00000118, 0x00000178, 0x00000128, 0x00000148,
+ 0x00000100, 0x00000150, 0x00000170, 0x00000138,
+ 0x00000130, 0x00000140, 0x00000110, 0x00000160,
+ 0x000001e8, 0x000001d8, 0x000001a0, 0x00000188,
+ 0x00000198, 0x000001f8, 0x000001a8, 0x000001c8,
+ 0x00000180, 0x000001d0, 0x000001f0, 0x000001b8,
+ 0x000001b0, 0x000001c0, 0x00000190, 0x000001e0,
+ 0x00000768, 0x00000758, 0x00000720, 0x00000708,
+ 0x00000718, 0x00000778, 0x00000728, 0x00000748,
+ 0x00000700, 0x00000750, 0x00000770, 0x00000738,
+ 0x00000730, 0x00000740, 0x00000710, 0x00000760,
+ 0x00000368, 0x00000358, 0x00000320, 0x00000308,
+ 0x00000318, 0x00000378, 0x00000328, 0x00000348,
+ 0x00000300, 0x00000350, 0x00000370, 0x00000338,
+ 0x00000330, 0x00000340, 0x00000310, 0x00000360,
+ 0x000005e8, 0x000005d8, 0x000005a0, 0x00000588,
+ 0x00000598, 0x000005f8, 0x000005a8, 0x000005c8,
+ 0x00000580, 0x000005d0, 0x000005f0, 0x000005b8,
+ 0x000005b0, 0x000005c0, 0x00000590, 0x000005e0,
+ 0x00000468, 0x00000458, 0x00000420, 0x00000408,
+ 0x00000418, 0x00000478, 0x00000428, 0x00000448,
+ 0x00000400, 0x00000450, 0x00000470, 0x00000438,
+ 0x00000430, 0x00000440, 0x00000410, 0x00000460,
+ 0x00000668, 0x00000658, 0x00000620, 0x00000608,
+ 0x00000618, 0x00000678, 0x00000628, 0x00000648,
+ 0x00000600, 0x00000650, 0x00000670, 0x00000638,
+ 0x00000630, 0x00000640, 0x00000610, 0x00000660,
+ }
+};
+
+/*
+ * A macro that performs a full encryption round of GOST 28147-89.
+ * Temporary variables tmp assumed and variables r and l for left and right
+ * blocks.
+ */
+#define GOST_ENCRYPT_ROUND(key1, key2, sbox) \
+ tmp = (key1) + r; \
+ l ^= (sbox)[0*256 + (tmp & 0xff)] ^ (sbox)[1*256 + ((tmp >> 8) & 0xff)] ^ \
+ (sbox)[2*256 + ((tmp >> 16) & 0xff)] ^ (sbox)[3*256 + (tmp >> 24)]; \
+ tmp = (key2) + l; \
+ r ^= (sbox)[0*256 + (tmp & 0xff)] ^ (sbox)[1*256 + ((tmp >> 8) & 0xff)] ^ \
+ (sbox)[2*256 + ((tmp >> 16) & 0xff)] ^ (sbox)[3*256 + (tmp >> 24)];
+
+/* encrypt a block with the given key */
+void gost28147_encrypt_simple (const uint32_t *key, const uint32_t *sbox,
+ const uint32_t *in, uint32_t *out)
+{
+ uint32_t l, r, tmp;
+
+ r = in[0], l = in[1];
+ GOST_ENCRYPT_ROUND(key[0], key[1], sbox)
+ GOST_ENCRYPT_ROUND(key[2], key[3], sbox)
+ GOST_ENCRYPT_ROUND(key[4], key[5], sbox)
+ GOST_ENCRYPT_ROUND(key[6], key[7], sbox)
+ GOST_ENCRYPT_ROUND(key[0], key[1], sbox)
+ GOST_ENCRYPT_ROUND(key[2], key[3], sbox)
+ GOST_ENCRYPT_ROUND(key[4], key[5], sbox)
+ GOST_ENCRYPT_ROUND(key[6], key[7], sbox)
+ GOST_ENCRYPT_ROUND(key[0], key[1], sbox)
+ GOST_ENCRYPT_ROUND(key[2], key[3], sbox)
+ GOST_ENCRYPT_ROUND(key[4], key[5], sbox)
+ GOST_ENCRYPT_ROUND(key[6], key[7], sbox)
+ GOST_ENCRYPT_ROUND(key[7], key[6], sbox)
+ GOST_ENCRYPT_ROUND(key[5], key[4], sbox)
+ GOST_ENCRYPT_ROUND(key[3], key[2], sbox)
+ GOST_ENCRYPT_ROUND(key[1], key[0], sbox)
+ *out = l, *(out + 1) = r;
+}
diff --git a/gost28147.h b/gost28147.h
new file mode 100644
index 000000000000..83d8915b9d38
--- /dev/null
+++ b/gost28147.h
@@ -0,0 +1,63 @@
+/* gost28147.h
+
+ The GOST 28147-89 cipher function, described in RFC 5831.
+
+ Copyright (C) 2019 Dmitry Eremin-Solenikov
+
+ This file is part of GNU Nettle.
+
+ GNU Nettle is free software: you can redistribute it and/or
+ modify it under the terms of either:
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at your
+ option) any later version.
+
+ or
+
+ * the GNU General Public License as published by the Free
+ Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
+
+ or both in parallel, as here.
+
+ GNU Nettle is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received copies of the GNU General Public License and
+ the GNU Lesser General Public License along with this program. If
+ not, see http://www.gnu.org/licenses/.
+*/
+
+#ifndef NETTLE_GOST28147_H_INCLUDED
+#define NETTLE_GOST28147_H_INCLUDED
+
+#include "nettle-types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define gost28147_param_test_3411 nettle_gost28147_param_test_3411
+
+/* Private */
+#define gost28147_encrypt_simple nettle_gost28147_encrypt_simple
+
+struct gost28147_param
+{
+ uint32_t sbox[4*256];
+};
+
+extern const struct gost28147_param gost28147_param_test_3411;
+
+/* Internal interface for use by GOST R 34.11-94 */
+void gost28147_encrypt_simple (const uint32_t *key, const uint32_t *sbox,
+ const uint32_t *in, uint32_t *out);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NETTLE_GOST28147_H_INCLUDED */
diff --git a/gosthash94.c b/gosthash94.c
index e60c9ae5cddf..870235eefa34 100644
--- a/gosthash94.c
+++ b/gosthash94.c
@@ -1,5 +1,7 @@
-/* gost.c - an implementation of GOST Hash Function
+/* gosthash94.c - an implementation of GOST Hash Function
+ *
* based on the Russian Standard GOST R 34.11-94.
+ * English description in RFC 5831.
* See also RFC 4357.
*
* Copyright: 2009-2012 Aleksey Kravchenko <rhash.admin(a)gmail.com>
@@ -38,209 +40,7 @@
#include "macros.h"
#include "nettle-write.h"
#include "gosthash94.h"
-
-/* pre-initialized GOST lookup tables based on rotated S-Box */
-static const uint32_t gosthash94_sbox[4][256] = {
- {
- 0x72000, 0x75000, 0x74800, 0x71000, 0x76800,
- 0x74000, 0x70000, 0x77000, 0x73000, 0x75800,
- 0x70800, 0x76000, 0x73800, 0x77800, 0x72800,
- 0x71800, 0x5A000, 0x5D000, 0x5C800, 0x59000,
- 0x5E800, 0x5C000, 0x58000, 0x5F000, 0x5B000,
- 0x5D800, 0x58800, 0x5E000, 0x5B800, 0x5F800,
- 0x5A800, 0x59800, 0x22000, 0x25000, 0x24800,
- 0x21000, 0x26800, 0x24000, 0x20000, 0x27000,
- 0x23000, 0x25800, 0x20800, 0x26000, 0x23800,
- 0x27800, 0x22800, 0x21800, 0x62000, 0x65000,
- 0x64800, 0x61000, 0x66800, 0x64000, 0x60000,
- 0x67000, 0x63000, 0x65800, 0x60800, 0x66000,
- 0x63800, 0x67800, 0x62800, 0x61800, 0x32000,
- 0x35000, 0x34800, 0x31000, 0x36800, 0x34000,
- 0x30000, 0x37000, 0x33000, 0x35800, 0x30800,
- 0x36000, 0x33800, 0x37800, 0x32800, 0x31800,
- 0x6A000, 0x6D000, 0x6C800, 0x69000, 0x6E800,
- 0x6C000, 0x68000, 0x6F000, 0x6B000, 0x6D800,
- 0x68800, 0x6E000, 0x6B800, 0x6F800, 0x6A800,
- 0x69800, 0x7A000, 0x7D000, 0x7C800, 0x79000,
- 0x7E800, 0x7C000, 0x78000, 0x7F000, 0x7B000,
- 0x7D800, 0x78800, 0x7E000, 0x7B800, 0x7F800,
- 0x7A800, 0x79800, 0x52000, 0x55000, 0x54800,
- 0x51000, 0x56800, 0x54000, 0x50000, 0x57000,
- 0x53000, 0x55800, 0x50800, 0x56000, 0x53800,
- 0x57800, 0x52800, 0x51800, 0x12000, 0x15000,
- 0x14800, 0x11000, 0x16800, 0x14000, 0x10000,
- 0x17000, 0x13000, 0x15800, 0x10800, 0x16000,
- 0x13800, 0x17800, 0x12800, 0x11800, 0x1A000,
- 0x1D000, 0x1C800, 0x19000, 0x1E800, 0x1C000,
- 0x18000, 0x1F000, 0x1B000, 0x1D800, 0x18800,
- 0x1E000, 0x1B800, 0x1F800, 0x1A800, 0x19800,
- 0x42000, 0x45000, 0x44800, 0x41000, 0x46800,
- 0x44000, 0x40000, 0x47000, 0x43000, 0x45800,
- 0x40800, 0x46000, 0x43800, 0x47800, 0x42800,
- 0x41800, 0xA000, 0xD000, 0xC800, 0x9000,
- 0xE800, 0xC000, 0x8000, 0xF000, 0xB000,
- 0xD800, 0x8800, 0xE000, 0xB800, 0xF800,
- 0xA800, 0x9800, 0x2000, 0x5000, 0x4800,
- 0x1000, 0x6800, 0x4000, 0x0, 0x7000,
- 0x3000, 0x5800, 0x800, 0x6000, 0x3800,
- 0x7800, 0x2800, 0x1800, 0x3A000, 0x3D000,
- 0x3C800, 0x39000, 0x3E800, 0x3C000, 0x38000,
- 0x3F000, 0x3B000, 0x3D800, 0x38800, 0x3E000,
- 0x3B800, 0x3F800, 0x3A800, 0x39800, 0x2A000,
- 0x2D000, 0x2C800, 0x29000, 0x2E800, 0x2C000,
- 0x28000, 0x2F000, 0x2B000, 0x2D800, 0x28800,
- 0x2E000, 0x2B800, 0x2F800, 0x2A800, 0x29800,
- 0x4A000, 0x4D000, 0x4C800, 0x49000, 0x4E800,
- 0x4C000, 0x48000, 0x4F000, 0x4B000, 0x4D800,
- 0x48800, 0x4E000, 0x4B800, 0x4F800, 0x4A800,
- 0x49800
- }, {
- 0x3A80000, 0x3C00000, 0x3880000, 0x3E80000, 0x3D00000,
- 0x3980000, 0x3A00000, 0x3900000, 0x3F00000, 0x3F80000,
- 0x3E00000, 0x3B80000, 0x3B00000, 0x3800000, 0x3C80000,
- 0x3D80000, 0x6A80000, 0x6C00000, 0x6880000, 0x6E80000,
- 0x6D00000, 0x6980000, 0x6A00000, 0x6900000, 0x6F00000,
- 0x6F80000, 0x6E00000, 0x6B80000, 0x6B00000, 0x6800000,
- 0x6C80000, 0x6D80000, 0x5280000, 0x5400000, 0x5080000,
- 0x5680000, 0x5500000, 0x5180000, 0x5200000, 0x5100000,
- 0x5700000, 0x5780000, 0x5600000, 0x5380000, 0x5300000,
- 0x5000000, 0x5480000, 0x5580000, 0xA80000, 0xC00000,
- 0x880000, 0xE80000, 0xD00000, 0x980000, 0xA00000,
- 0x900000, 0xF00000, 0xF80000, 0xE00000, 0xB80000,
- 0xB00000, 0x800000, 0xC80000, 0xD80000, 0x280000,
- 0x400000, 0x80000, 0x680000, 0x500000, 0x180000,
- 0x200000, 0x100000, 0x700000, 0x780000, 0x600000,
- 0x380000, 0x300000, 0x0, 0x480000, 0x580000,
- 0x4280000, 0x4400000, 0x4080000, 0x4680000, 0x4500000,
- 0x4180000, 0x4200000, 0x4100000, 0x4700000, 0x4780000,
- 0x4600000, 0x4380000, 0x4300000, 0x4000000, 0x4480000,
- 0x4580000, 0x4A80000, 0x4C00000, 0x4880000, 0x4E80000,
- 0x4D00000, 0x4980000, 0x4A00000, 0x4900000, 0x4F00000,
- 0x4F80000, 0x4E00000, 0x4B80000, 0x4B00000, 0x4800000,
- 0x4C80000, 0x4D80000, 0x7A80000, 0x7C00000, 0x7880000,
- 0x7E80000, 0x7D00000, 0x7980000, 0x7A00000, 0x7900000,
- 0x7F00000, 0x7F80000, 0x7E00000, 0x7B80000, 0x7B00000,
- 0x7800000, 0x7C80000, 0x7D80000, 0x7280000, 0x7400000,
- 0x7080000, 0x7680000, 0x7500000, 0x7180000, 0x7200000,
- 0x7100000, 0x7700000, 0x7780000, 0x7600000, 0x7380000,
- 0x7300000, 0x7000000, 0x7480000, 0x7580000, 0x2280000,
- 0x2400000, 0x2080000, 0x2680000, 0x2500000, 0x2180000,
- 0x2200000, 0x2100000, 0x2700000, 0x2780000, 0x2600000,
- 0x2380000, 0x2300000, 0x2000000, 0x2480000, 0x2580000,
- 0x3280000, 0x3400000, 0x3080000, 0x3680000, 0x3500000,
- 0x3180000, 0x3200000, 0x3100000, 0x3700000, 0x3780000,
- 0x3600000, 0x3380000, 0x3300000, 0x3000000, 0x3480000,
- 0x3580000, 0x6280000, 0x6400000, 0x6080000, 0x6680000,
- 0x6500000, 0x6180000, 0x6200000, 0x6100000, 0x6700000,
- 0x6780000, 0x6600000, 0x6380000, 0x6300000, 0x6000000,
- 0x6480000, 0x6580000, 0x5A80000, 0x5C00000, 0x5880000,
- 0x5E80000, 0x5D00000, 0x5980000, 0x5A00000, 0x5900000,
- 0x5F00000, 0x5F80000, 0x5E00000, 0x5B80000, 0x5B00000,
- 0x5800000, 0x5C80000, 0x5D80000, 0x1280000, 0x1400000,
- 0x1080000, 0x1680000, 0x1500000, 0x1180000, 0x1200000,
- 0x1100000, 0x1700000, 0x1780000, 0x1600000, 0x1380000,
- 0x1300000, 0x1000000, 0x1480000, 0x1580000, 0x2A80000,
- 0x2C00000, 0x2880000, 0x2E80000, 0x2D00000, 0x2980000,
- 0x2A00000, 0x2900000, 0x2F00000, 0x2F80000, 0x2E00000,
- 0x2B80000, 0x2B00000, 0x2800000, 0x2C80000, 0x2D80000,
- 0x1A80000, 0x1C00000, 0x1880000, 0x1E80000, 0x1D00000,
- 0x1980000, 0x1A00000, 0x1900000, 0x1F00000, 0x1F80000,
- 0x1E00000, 0x1B80000, 0x1B00000, 0x1800000, 0x1C80000,
- 0x1D80000
- }, {
- 0x30000002, 0x60000002, 0x38000002, 0x8000002,
- 0x28000002, 0x78000002, 0x68000002, 0x40000002,
- 0x20000002, 0x50000002, 0x48000002, 0x70000002,
- 0x2, 0x18000002, 0x58000002, 0x10000002,
- 0xB0000005, 0xE0000005, 0xB8000005, 0x88000005,
- 0xA8000005, 0xF8000005, 0xE8000005, 0xC0000005,
- 0xA0000005, 0xD0000005, 0xC8000005, 0xF0000005,
- 0x80000005, 0x98000005, 0xD8000005, 0x90000005,
- 0x30000005, 0x60000005, 0x38000005, 0x8000005,
- 0x28000005, 0x78000005, 0x68000005, 0x40000005,
- 0x20000005, 0x50000005, 0x48000005, 0x70000005,
- 0x5, 0x18000005, 0x58000005, 0x10000005,
- 0x30000000, 0x60000000, 0x38000000, 0x8000000,
- 0x28000000, 0x78000000, 0x68000000, 0x40000000,
- 0x20000000, 0x50000000, 0x48000000, 0x70000000,
- 0x0, 0x18000000, 0x58000000, 0x10000000,
- 0xB0000003, 0xE0000003, 0xB8000003, 0x88000003,
- 0xA8000003, 0xF8000003, 0xE8000003, 0xC0000003,
- 0xA0000003, 0xD0000003, 0xC8000003, 0xF0000003,
- 0x80000003, 0x98000003, 0xD8000003, 0x90000003,
- 0x30000001, 0x60000001, 0x38000001, 0x8000001,
- 0x28000001, 0x78000001, 0x68000001, 0x40000001,
- 0x20000001, 0x50000001, 0x48000001, 0x70000001,
- 0x1, 0x18000001, 0x58000001, 0x10000001,
- 0xB0000000, 0xE0000000, 0xB8000000, 0x88000000,
- 0xA8000000, 0xF8000000, 0xE8000000, 0xC0000000,
- 0xA0000000, 0xD0000000, 0xC8000000, 0xF0000000,
- 0x80000000, 0x98000000, 0xD8000000, 0x90000000,
- 0xB0000006, 0xE0000006, 0xB8000006, 0x88000006,
- 0xA8000006, 0xF8000006, 0xE8000006, 0xC0000006,
- 0xA0000006, 0xD0000006, 0xC8000006, 0xF0000006,
- 0x80000006, 0x98000006, 0xD8000006, 0x90000006,
- 0xB0000001, 0xE0000001, 0xB8000001, 0x88000001,
- 0xA8000001, 0xF8000001, 0xE8000001, 0xC0000001,
- 0xA0000001, 0xD0000001, 0xC8000001, 0xF0000001,
- 0x80000001, 0x98000001, 0xD8000001, 0x90000001,
- 0x30000003, 0x60000003, 0x38000003, 0x8000003,
- 0x28000003, 0x78000003, 0x68000003, 0x40000003,
- 0x20000003, 0x50000003, 0x48000003, 0x70000003,
- 0x3, 0x18000003, 0x58000003, 0x10000003,
- 0x30000004, 0x60000004, 0x38000004, 0x8000004,
- 0x28000004, 0x78000004, 0x68000004, 0x40000004,
- 0x20000004, 0x50000004, 0x48000004, 0x70000004,
- 0x4, 0x18000004, 0x58000004, 0x10000004,
- 0xB0000002, 0xE0000002, 0xB8000002, 0x88000002,
- 0xA8000002, 0xF8000002, 0xE8000002, 0xC0000002,
- 0xA0000002, 0xD0000002, 0xC8000002, 0xF0000002,
- 0x80000002, 0x98000002, 0xD8000002, 0x90000002,
- 0xB0000004, 0xE0000004, 0xB8000004, 0x88000004,
- 0xA8000004, 0xF8000004, 0xE8000004, 0xC0000004,
- 0xA0000004, 0xD0000004, 0xC8000004, 0xF0000004,
- 0x80000004, 0x98000004, 0xD8000004, 0x90000004,
- 0x30000006, 0x60000006, 0x38000006, 0x8000006,
- 0x28000006, 0x78000006, 0x68000006, 0x40000006,
- 0x20000006, 0x50000006, 0x48000006, 0x70000006,
- 0x6, 0x18000006, 0x58000006, 0x10000006,
- 0xB0000007, 0xE0000007, 0xB8000007, 0x88000007,
- 0xA8000007, 0xF8000007, 0xE8000007, 0xC0000007,
- 0xA0000007, 0xD0000007, 0xC8000007, 0xF0000007,
- 0x80000007, 0x98000007, 0xD8000007, 0x90000007,
- 0x30000007, 0x60000007, 0x38000007, 0x8000007,
- 0x28000007, 0x78000007, 0x68000007, 0x40000007,
- 0x20000007, 0x50000007, 0x48000007, 0x70000007,
- 0x7, 0x18000007, 0x58000007, 0x10000007
- }, {
- 0xE8, 0xD8, 0xA0, 0x88, 0x98, 0xF8, 0xA8, 0xC8, 0x80, 0xD0,
- 0xF0, 0xB8, 0xB0, 0xC0, 0x90, 0xE0, 0x7E8, 0x7D8, 0x7A0, 0x788,
- 0x798, 0x7F8, 0x7A8, 0x7C8, 0x780, 0x7D0, 0x7F0, 0x7B8, 0x7B0, 0x7C0,
- 0x790, 0x7E0, 0x6E8, 0x6D8, 0x6A0, 0x688, 0x698, 0x6F8, 0x6A8, 0x6C8,
- 0x680, 0x6D0, 0x6F0, 0x6B8, 0x6B0, 0x6C0, 0x690, 0x6E0, 0x68, 0x58,
- 0x20, 0x8, 0x18, 0x78, 0x28, 0x48, 0x0, 0x50, 0x70, 0x38,
- 0x30, 0x40, 0x10, 0x60, 0x2E8, 0x2D8, 0x2A0, 0x288, 0x298, 0x2F8,
- 0x2A8, 0x2C8, 0x280, 0x2D0, 0x2F0, 0x2B8, 0x2B0, 0x2C0, 0x290, 0x2E0,
- 0x3E8, 0x3D8, 0x3A0, 0x388, 0x398, 0x3F8, 0x3A8, 0x3C8, 0x380, 0x3D0,
- 0x3F0, 0x3B8, 0x3B0, 0x3C0, 0x390, 0x3E0, 0x568, 0x558, 0x520, 0x508,
- 0x518, 0x578, 0x528, 0x548, 0x500, 0x550, 0x570, 0x538, 0x530, 0x540,
- 0x510, 0x560, 0x268, 0x258, 0x220, 0x208, 0x218, 0x278, 0x228, 0x248,
- 0x200, 0x250, 0x270, 0x238, 0x230, 0x240, 0x210, 0x260, 0x4E8, 0x4D8,
- 0x4A0, 0x488, 0x498, 0x4F8, 0x4A8, 0x4C8, 0x480, 0x4D0, 0x4F0, 0x4B8,
- 0x4B0, 0x4C0, 0x490, 0x4E0, 0x168, 0x158, 0x120, 0x108, 0x118, 0x178,
- 0x128, 0x148, 0x100, 0x150, 0x170, 0x138, 0x130, 0x140, 0x110, 0x160,
- 0x1E8, 0x1D8, 0x1A0, 0x188, 0x198, 0x1F8, 0x1A8, 0x1C8, 0x180, 0x1D0,
- 0x1F0, 0x1B8, 0x1B0, 0x1C0, 0x190, 0x1E0, 0x768, 0x758, 0x720, 0x708,
- 0x718, 0x778, 0x728, 0x748, 0x700, 0x750, 0x770, 0x738, 0x730, 0x740,
- 0x710, 0x760, 0x368, 0x358, 0x320, 0x308, 0x318, 0x378, 0x328, 0x348,
- 0x300, 0x350, 0x370, 0x338, 0x330, 0x340, 0x310, 0x360, 0x5E8, 0x5D8,
- 0x5A0, 0x588, 0x598, 0x5F8, 0x5A8, 0x5C8, 0x580, 0x5D0, 0x5F0, 0x5B8,
- 0x5B0, 0x5C0, 0x590, 0x5E0, 0x468, 0x458, 0x420, 0x408, 0x418, 0x478,
- 0x428, 0x448, 0x400, 0x450, 0x470, 0x438, 0x430, 0x440, 0x410, 0x460,
- 0x668, 0x658, 0x620, 0x608, 0x618, 0x678, 0x628, 0x648, 0x600, 0x650,
- 0x670, 0x638, 0x630, 0x640, 0x610, 0x660
- }
-};
+#include "gost28147.h"
/**
* Initialize algorithm context before calculating hash
@@ -254,40 +54,6 @@ gosthash94_init (struct gosthash94_ctx *ctx)
memset (ctx, 0, sizeof (struct gosthash94_ctx));
}
-/*
- * A macro that performs a full encryption round of GOST 28147-89.
- * Temporary variables tmp assumed and variables r and l for left and right
- * blocks.
- */
-#define GOST_ENCRYPT_ROUND(key1, key2, sbox) \
- tmp = (key1) + r; \
- l ^= (sbox)[0][tmp & 0xff] ^ (sbox)[1][(tmp >> 8) & 0xff] ^ \
- (sbox)[2][(tmp >> 16) & 0xff] ^ (sbox)[3][tmp >> 24]; \
- tmp = (key2) + l; \
- r ^= (sbox)[0][tmp & 0xff] ^ (sbox)[1][(tmp >> 8) & 0xff] ^ \
- (sbox)[2][(tmp >> 16) & 0xff] ^ (sbox)[3][tmp >> 24];
-
-/* encrypt a block with the given key */
-#define GOST_ENCRYPT(result, i, key, hash, sbox) \
- r = hash[i], l = hash[i + 1]; \
- GOST_ENCRYPT_ROUND(key[0], key[1], sbox) \
- GOST_ENCRYPT_ROUND(key[2], key[3], sbox) \
- GOST_ENCRYPT_ROUND(key[4], key[5], sbox) \
- GOST_ENCRYPT_ROUND(key[6], key[7], sbox) \
- GOST_ENCRYPT_ROUND(key[0], key[1], sbox) \
- GOST_ENCRYPT_ROUND(key[2], key[3], sbox) \
- GOST_ENCRYPT_ROUND(key[4], key[5], sbox) \
- GOST_ENCRYPT_ROUND(key[6], key[7], sbox) \
- GOST_ENCRYPT_ROUND(key[0], key[1], sbox) \
- GOST_ENCRYPT_ROUND(key[2], key[3], sbox) \
- GOST_ENCRYPT_ROUND(key[4], key[5], sbox) \
- GOST_ENCRYPT_ROUND(key[6], key[7], sbox) \
- GOST_ENCRYPT_ROUND(key[7], key[6], sbox) \
- GOST_ENCRYPT_ROUND(key[5], key[4], sbox) \
- GOST_ENCRYPT_ROUND(key[3], key[2], sbox) \
- GOST_ENCRYPT_ROUND(key[1], key[0], sbox) \
- result[i] = l, result[i + 1] = r;
-
/**
* The core transformation. Process a 512-bit block.
*
@@ -299,7 +65,6 @@ gost_block_compress (struct gosthash94_ctx *ctx, const uint32_t *block)
{
unsigned i;
uint32_t key[8], u[8], v[8], w[8], s[8];
- uint32_t l, r, tmp;
/* u := hash, v := <256-bit message block> */
memcpy (u, ctx->hash, sizeof (u));
@@ -341,7 +106,7 @@ gost_block_compress (struct gosthash94_ctx *ctx, const uint32_t *block)
((w[5] & 0xff000000) >> 8) | (w[7] & 0xff000000);
/* encryption: s_i := E_{key_i} (h_i) */
- GOST_ENCRYPT (s, i, key, ctx->hash, gosthash94_sbox);
+ gost28147_encrypt_simple (key, gost28147_param_test_3411.sbox, &ctx->hash[i], &s[i]);
if (i == 0)
{
--
2.20.1
2
8
From: Dmitry Eremin-Solenikov <dbaryshkov(a)gmail.com>
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov(a)gmail.com>
---
Makefile.in | 2 +-
gostdsa-vko.c | 77 ++++++++++++++++++++++++++++++
gostdsa.h | 7 +++
testsuite/.gitignore | 1 +
testsuite/.test-rules.make | 3 ++
testsuite/Makefile.in | 2 +-
testsuite/gostdsa-vko-test.c | 92 ++++++++++++++++++++++++++++++++++++
7 files changed, 182 insertions(+), 2 deletions(-)
create mode 100644 gostdsa-vko.c
create mode 100644 testsuite/gostdsa-vko-test.c
diff --git a/Makefile.in b/Makefile.in
index d4fcb81302a2..8f031d7a580d 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -194,7 +194,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \
ecc-ecdsa-sign.c ecdsa-sign.c \
ecc-ecdsa-verify.c ecdsa-verify.c ecdsa-keygen.c \
ecc-gostdsa-sign.c gostdsa-sign.c \
- ecc-gostdsa-verify.c gostdsa-verify.c \
+ ecc-gostdsa-verify.c gostdsa-verify.c gostdsa-vko.c \
curve25519-mul-g.c curve25519-mul.c curve25519-eh-to-x.c \
curve448-mul-g.c curve448-mul.c curve448-eh-to-x.c \
eddsa-compress.c eddsa-decompress.c eddsa-expand.c \
diff --git a/gostdsa-vko.c b/gostdsa-vko.c
new file mode 100644
index 000000000000..f78159a736b3
--- /dev/null
+++ b/gostdsa-vko.c
@@ -0,0 +1,77 @@
+/* gostdsa-vko.c
+
+ Copyright (C) 2016 Dmitry Eremin-Solenikov
+
+ This file is part of GNU Nettle.
+
+ GNU Nettle is free software: you can redistribute it and/or
+ modify it under the terms of either:
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at your
+ option) any later version.
+
+ or
+
+ * the GNU General Public License as published by the Free
+ Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
+
+ or both in parallel, as here.
+
+ GNU Nettle is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received copies of the GNU General Public License and
+ the GNU Lesser General Public License along with this program. If
+ not, see http://www.gnu.org/licenses/.
+*/
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include "ecc-internal.h"
+#include "gostdsa.h"
+
+int
+gostdsa_vko(const struct ecc_scalar *key,
+ const struct ecc_point *pub,
+ size_t ukm_length, const uint8_t *ukm,
+ size_t out_length, uint8_t *out)
+{
+ const struct ecc_curve *ecc = key->ecc;
+ unsigned bsize = (ecc_bit_size(ecc) + 7) / 8;
+ mp_size_t size = ecc->p.size;
+ mp_size_t itch = 4*size + ecc->mul_itch;
+ mp_limb_t *scratch;
+
+ if (itch < 5*size + ecc->h_to_a_itch)
+ itch = 5*size + ecc->h_to_a_itch;
+
+ if (pub->ecc != ecc)
+ return 0;
+
+ if (out_length < 2 * bsize) {
+ return 0;
+ }
+
+ scratch = gmp_alloc_limbs (itch);
+
+ mpn_set_base256_le (scratch, size, ukm, ukm_length);
+ if (mpn_zero_p (scratch, size))
+ mpn_add_1 (scratch, scratch, size, 1);
+ ecc_mod_mul (&ecc->q, scratch + 3*size, key->p, scratch);
+ ecc->mul (ecc, scratch, scratch + 3*size, pub->p, scratch + 4*size);
+ ecc->h_to_a (ecc, 0, scratch + 3*size, scratch, scratch + 5*size);
+ mpn_get_base256_le (out, bsize, scratch + 3*size, size);
+ mpn_get_base256_le (out+bsize, bsize, scratch + 4*size, size);
+ gmp_free_limbs (scratch, itch);
+
+ return 2 * bsize;
+}
diff --git a/gostdsa.h b/gostdsa.h
index c92dfd1e1dd6..6667d0f1d3a8 100644
--- a/gostdsa.h
+++ b/gostdsa.h
@@ -44,6 +44,7 @@ extern "C" {
/* Name mangling */
#define gostdsa_sign nettle_gostdsa_sign
#define gostdsa_verify nettle_gostdsa_verify
+#define gostdsa_vko nettle_gostdsa_vko
#define ecc_gostdsa_sign nettle_ecc_gostdsa_sign
#define ecc_gostdsa_sign_itch nettle_ecc_gostdsa_sign_itch
#define ecc_gostdsa_verify nettle_ecc_gostdsa_verify
@@ -68,6 +69,12 @@ gostdsa_verify (const struct ecc_point *pub,
size_t length, const uint8_t *digest,
const struct dsa_signature *signature);
+int
+gostdsa_vko(const struct ecc_scalar *key,
+ const struct ecc_point *pub,
+ size_t ukm_length, const uint8_t *ukm,
+ size_t out_length, uint8_t *out);
+
/* Low-level GOSTDSA functions. */
mp_size_t
ecc_gostdsa_sign_itch (const struct ecc_curve *ecc);
diff --git a/testsuite/.gitignore b/testsuite/.gitignore
index b8b36c2accc2..a2b3d52312cd 100644
--- a/testsuite/.gitignore
+++ b/testsuite/.gitignore
@@ -46,6 +46,7 @@
/gostdsa-keygen-test
/gostdsa-sign-test
/gostdsa-verify-test
+/gostdsa-vko-test
/gosthash94-test
/hkdf-test
/hmac-test
diff --git a/testsuite/.test-rules.make b/testsuite/.test-rules.make
index 922a2c7f1350..b340e3c1b7b9 100644
--- a/testsuite/.test-rules.make
+++ b/testsuite/.test-rules.make
@@ -301,6 +301,9 @@ gostdsa-verify-test$(EXEEXT): gostdsa-verify-test.$(OBJEXT)
gostdsa-keygen-test$(EXEEXT): gostdsa-keygen-test.$(OBJEXT)
$(LINK) gostdsa-keygen-test.$(OBJEXT) $(TEST_OBJS) -o gostdsa-keygen-test$(EXEEXT)
+gostdsa-vko-test$(EXEEXT): gostdsa-vko-test.$(OBJEXT)
+ $(LINK) gostdsa-vko-test.$(OBJEXT) $(TEST_OBJS) -o gostdsa-vko-test$(EXEEXT)
+
sha1-huge-test$(EXEEXT): sha1-huge-test.$(OBJEXT)
$(LINK) sha1-huge-test.$(OBJEXT) $(TEST_OBJS) -o sha1-huge-test$(EXEEXT)
diff --git a/testsuite/Makefile.in b/testsuite/Makefile.in
index 813467a548bd..9f87c86b1c2f 100644
--- a/testsuite/Makefile.in
+++ b/testsuite/Makefile.in
@@ -55,7 +55,7 @@ TS_HOGWEED_SOURCES = sexp-test.c sexp-format-test.c \
eddsa-compress-test.c eddsa-sign-test.c \
eddsa-verify-test.c ed25519-test.c ed448-test.c \
gostdsa-sign-test.c gostdsa-verify-test.c \
- gostdsa-keygen-test.c
+ gostdsa-keygen-test.c gostdsa-vko-test.c
TS_SOURCES = $(TS_NETTLE_SOURCES) $(TS_HOGWEED_SOURCES)
CXX_SOURCES = cxx-test.cxx
diff --git a/testsuite/gostdsa-vko-test.c b/testsuite/gostdsa-vko-test.c
new file mode 100644
index 000000000000..9c86774198ed
--- /dev/null
+++ b/testsuite/gostdsa-vko-test.c
@@ -0,0 +1,92 @@
+#include "testutils.h"
+#include "gostdsa.h"
+#include "streebog.h"
+
+static void
+test_vko (const struct ecc_curve *ecc,
+ const char *priv,
+ const char *x,
+ const char *y,
+ const struct tstring *ukm,
+ const struct nettle_hash *hash,
+ void * hash_ctx,
+ const struct tstring *res)
+{
+ struct ecc_scalar ecc_key;
+ struct ecc_point ecc_pub;
+ mpz_t temp1, temp2;
+ int ret;
+ uint8_t out[128];
+
+ ecc_point_init (&ecc_pub, ecc);
+ mpz_init_set_str (temp1, x, 16);
+ mpz_init_set_str (temp2, y, 16);
+ ASSERT (ecc_point_set (&ecc_pub, temp1, temp2) != 0);
+
+ ecc_scalar_init (&ecc_key, ecc);
+ mpz_set_str (temp1, priv, 16);
+ ASSERT (ecc_scalar_set (&ecc_key, temp1) != 0);
+
+ mpz_clear (temp1);
+ mpz_clear (temp2);
+
+ ret = gostdsa_vko (&ecc_key, &ecc_pub,
+ ukm->length, ukm->data,
+ sizeof(out), out);
+ ASSERT (ret != 0);
+
+ ecc_scalar_clear (&ecc_key);
+ ecc_point_clear (&ecc_pub);
+
+ hash->init (hash_ctx);
+ hash->update (hash_ctx, ret, out);
+ hash->digest (hash_ctx, hash->digest_size, out);
+
+ ASSERT (hash->digest_size == res->length);
+ ASSERT (MEMEQ (res->length, out, res->data));
+}
+
+void
+test_main (void)
+{
+ struct streebog256_ctx ctx_256;
+ struct streebog256_ctx ctx_512;
+
+ test_vko(nettle_get_gost_gc512a(),
+ "67b63ca4ac8d2bb32618d89296c7476dbeb9f9048496f202b1902cf2ce41dbc2f847712d960483458d4b380867f426c7ca0ff5782702dbc44ee8fc72d9ec90c9",
+ "51a6d54ee932d176e87591121cce5f395cb2f2f147114d95f463c8a7ed74a9fc5ecd2325a35fb6387831ea66bc3d2aa42ede35872cc75372073a71b983e12f19",
+ "793bde5bf72840ad22b02a363ae4772d4a52fc08ba1a20f7458a222a13bf98b53be002d1973f1e398ce46c17da6d00d9b6d0076f8284dcc42e599b4c413b8804",
+ SHEX("1d 80 60 3c 85 44 c7 27"),
+ &nettle_streebog256,
+ &ctx_256,
+ SHEX("c9 a9 a7 73 20 e2 cc 55 9e d7 2d ce 6f 47 e2 19 2c ce a9 5f a6 48 67 05 82 c0 54 c0 ef 36 c2 21"));
+
+ test_vko(nettle_get_gost_gc512a(),
+ "dbd09213a592da5bbfd8ed068cccccbbfbeda4feac96b9b4908591440b0714803b9eb763ef932266d4c0181a9b73eacf9013efc65ec07c888515f1b6f759c848",
+ "a7c0adb12743c10c3c1beb97c8f631242f7937a1deb6bce5e664e49261baccd3f5dc56ec53b2abb90ca1eb703078ba546655a8b99f79188d2021ffaba4edb0aa",
+ "5adb1c63a4e4465e0bbefd897fb9016475934cfa0f8c95f992ea402d47921f46382d00481b720314b19d8c878e75d81b9763358dd304b2ed3a364e07a3134691",
+ SHEX("1d 80 60 3c 85 44 c7 27"),
+ &nettle_streebog256,
+ &ctx_256,
+ SHEX("c9 a9 a7 73 20 e2 cc 55 9e d7 2d ce 6f 47 e2 19 2c ce a9 5f a6 48 67 05 82 c0 54 c0 ef 36 c2 21"));
+
+ test_vko(nettle_get_gost_gc512a(),
+ "67b63ca4ac8d2bb32618d89296c7476dbeb9f9048496f202b1902cf2ce41dbc2f847712d960483458d4b380867f426c7ca0ff5782702dbc44ee8fc72d9ec90c9",
+ "51a6d54ee932d176e87591121cce5f395cb2f2f147114d95f463c8a7ed74a9fc5ecd2325a35fb6387831ea66bc3d2aa42ede35872cc75372073a71b983e12f19",
+ "793bde5bf72840ad22b02a363ae4772d4a52fc08ba1a20f7458a222a13bf98b53be002d1973f1e398ce46c17da6d00d9b6d0076f8284dcc42e599b4c413b8804",
+ SHEX("1d 80 60 3c 85 44 c7 27"),
+ &nettle_streebog512,
+ &ctx_512,
+ SHEX("79 f0 02 a9 69 40 ce 7b de 32 59 a5 2e 01 52 97 ad aa d8 45 97 a0 d2 05 b5 0e 3e 17 19 f9 7b fa"
+ "7e e1 d2 66 1f a9 97 9a 5a a2 35 b5 58 a7 e6 d9 f8 8f 98 2d d6 3f c3 5a 8e c0 dd 5e 24 2d 3b df"));
+
+ test_vko(nettle_get_gost_gc512a(),
+ "dbd09213a592da5bbfd8ed068cccccbbfbeda4feac96b9b4908591440b0714803b9eb763ef932266d4c0181a9b73eacf9013efc65ec07c888515f1b6f759c848",
+ "a7c0adb12743c10c3c1beb97c8f631242f7937a1deb6bce5e664e49261baccd3f5dc56ec53b2abb90ca1eb703078ba546655a8b99f79188d2021ffaba4edb0aa",
+ "5adb1c63a4e4465e0bbefd897fb9016475934cfa0f8c95f992ea402d47921f46382d00481b720314b19d8c878e75d81b9763358dd304b2ed3a364e07a3134691",
+ SHEX("1d 80 60 3c 85 44 c7 27"),
+ &nettle_streebog512,
+ &ctx_512,
+ SHEX("79 f0 02 a9 69 40 ce 7b de 32 59 a5 2e 01 52 97 ad aa d8 45 97 a0 d2 05 b5 0e 3e 17 19 f9 7b fa"
+ "7e e1 d2 66 1f a9 97 9a 5a a2 35 b5 58 a7 e6 d9 f8 8f 98 2d d6 3f c3 5a 8e c0 dd 5e 24 2d 3b df"));
+}
--
2.25.0
3
3
---
Makefile.in | 2 +-
base64-decode.c | 17 +-
blowfish-bcrypt.c | 511 ++++++++++++++++++++++++++++++
blowfish.h => blowfish-internal.h | 51 +--
blowfish.c | 23 +-
blowfish.h | 15 +
nettle.texinfo | 67 ++++
7 files changed, 620 insertions(+), 66 deletions(-)
create mode 100644 blowfish-bcrypt.c
copy blowfish.h => blowfish-internal.h (50%)
diff --git a/Makefile.in b/Makefile.in
index ddc30428..bfb7ac57 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -73,7 +73,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
aes256-set-encrypt-key.c aes256-set-decrypt-key.c \
aes256-meta.c \
arcfour.c arcfour-crypt.c \
- arctwo.c arctwo-meta.c blowfish.c \
+ arctwo.c arctwo-meta.c blowfish.c blowfish-bcrypt.c \
base16-encode.c base16-decode.c base16-meta.c \
base64-encode.c base64-decode.c base64-meta.c \
base64url-encode.c base64url-decode.c base64url-meta.c \
diff --git a/base64-decode.c b/base64-decode.c
index b993117a..9273224a 100644
--- a/base64-decode.c
+++ b/base64-decode.c
@@ -45,7 +45,7 @@
void
base64_decode_init(struct base64_decode_ctx *ctx)
{
- static const signed char base64_decode_table[0x100] =
+ static const signed char base64_decode_table[0x80] =
{
/* White space is HT, VT, FF, CR, LF and SPC */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1,
@@ -56,14 +56,6 @@ base64_decode_init(struct base64_decode_ctx *ctx)
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
ctx->word = ctx->bits = ctx->padding = 0;
@@ -75,7 +67,12 @@ base64_decode_single(struct base64_decode_ctx *ctx,
uint8_t *dst,
char src)
{
- int data = ctx->table[(uint8_t) src];
+ int data;
+
+ if ((uint8_t) src > 0x7f)
+ return -1;
+
+ data = ctx->table[(uint8_t) src];
switch(data)
{
diff --git a/blowfish-bcrypt.c b/blowfish-bcrypt.c
new file mode 100644
index 00000000..e573c86e
--- /dev/null
+++ b/blowfish-bcrypt.c
@@ -0,0 +1,511 @@
+/* blowfish-bcrypt.c
+
+ The blowfish bcrypt implementation.
+
+ Copyright (c) 2020 Stephen R. van den Berg
+
+ This file is part of GNU Nettle.
+
+ GNU Nettle is free software: you can redistribute it and/or
+ modify it under the terms of either:
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at your
+ option) any later version.
+
+ or
+
+ * the GNU General Public License as published by the Free
+ Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
+
+ or both in parallel, as here.
+
+ GNU Nettle is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received copies of the GNU General Public License and
+ the GNU Lesser General Public License along with this program. If
+ not, see http://www.gnu.org/licenses/.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "blowfish.h"
+#include "blowfish-internal.h"
+#include "base64.h"
+
+#include "macros.h"
+
+#define CRYPTPLEN 7
+#define SALTLEN ((BLOWFISH_BCRYPT_BINSALT_SIZE*8+5) / 6)
+
+#define HASHOFFSET (CRYPTPLEN + SALTLEN)
+
+static const signed char radix64_decode_table[0x80] = {
+ /* White space is HT, VT, FF, CR, LF and SPC */
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
+ 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, -1, -3, -1, -1,
+ -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1,
+ -1, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
+ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1,
+};
+
+static const char radix64_encode_table[64] =
+ "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789";
+
+int
+blowfish_bcrypt_verify(const char *key,
+ const char *hashed)
+{
+ char newhash[BLOWFISH_BCRYPT_HASH_SIZE];
+
+ return blowfish_bcrypt_hash(sizeof newhash,
+ newhash, key, hashed, -1, (void*)0)
+ && !strcmp(newhash, hashed);
+}
+
+static char *encode_radix64(char *dst, size_t len, const uint8_t *src)
+{
+ struct base64_encode_ctx ctx;
+ base64_encode_init(&ctx);
+ ctx.alphabet = radix64_encode_table;
+ dst += base64_encode_update(&ctx, dst, len, src);
+ dst += base64_encode_final(&ctx, dst);
+ *--dst = '\0'; /* Strip the trailing = */
+ return dst;
+}
+
+/*
+ * Large parts of the code below are based on public domain sources.
+ * The comments and copyright notices have been preserved.
+ * Any code added or modified by me is licensed under the
+ * licenses listed above. -- Stephen R. van den Berg
+ */
+
+/*
+ * This code comes from John the Ripper password cracker, with reentrant
+ * and crypt(3) interfaces added, but optimizations specific to password
+ * cracking removed.
+ *
+ * Written by Solar Designer <solar at openwall.com> in 1998-2015.
+ * No copyright is claimed, and the software is hereby placed in the public
+ * domain. In case this attempt to disclaim copyright and place the software
+ * in the public domain is deemed null and void, then the software is
+ * Copyright (c) 1998-2015 Solar Designer and it is hereby released to the
+ * general public under the following terms:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted.
+ *
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
+ *
+ * It is my intent that you should be able to use this on your system,
+ * as part of a software package, or anywhere else to improve security,
+ * ensure compatibility, or for any other purpose. I would appreciate
+ * it if you give credit where it is due and keep your modifications in
+ * the public domain as well, but I don't require that in order to let
+ * you place this code and any modifications you make under a license
+ * of your choice.
+ *
+ * This implementation is fully compatible with OpenBSD's bcrypt.c for prefix
+ * "$2b$", originally by Niels Provos <provos at citi.umich.edu>, and it uses
+ * some of his ideas. The password hashing algorithm was designed by David
+ * Mazieres <dm at lcs.mit.edu>. For information on the level of
+ * compatibility for bcrypt hash prefixes other than "$2b$", please refer to
+ * the comments in set_key() below and to the included crypt(3) man page.
+ */
+
+typedef uint32_t bf_key[_BLOWFISH_ROUNDS + 2];
+
+/*
+ * Magic IV for 64 Blowfish encryptions that we do at the end.
+ * The string is "OrpheanBeholderScryDoubt" on big-endian.
+ */
+static uint32_t magic_w[6] = {
+ 0x4F727068, 0x65616E42, 0x65686F6C,
+ 0x64657253, 0x63727944, 0x6F756274
+};
+
+static void swap32(uint32_t *x, int count)
+{
+#if !WORDS_BIGENDIAN
+ do {
+ uint32_t tmp = *x;
+ tmp = (tmp << 16) | (tmp >> 16);
+ *x++ = ((tmp & 0x00FF00FF) << 8) | ((tmp >> 8) & 0x00FF00FF);
+ } while (--count);
+#endif
+}
+
+static void set_xkey(const char *key, bf_key expanded, bf_key initial,
+ unsigned bug, uint32_t safety)
+{
+ const char *ptr = key;
+ unsigned i, j;
+ uint32_t sign, diff, tmp[2];
+
+/*
+ * There was a sign extension bug in older revisions of this function. While
+ * we would have liked to simply fix the bug and move on, we have to provide
+ * a backwards compatibility feature (essentially the bug) for some systems and
+ * a safety measure for some others. The latter is needed because for certain
+ * multiple inputs to the buggy algorithm there exist easily found inputs to
+ * the correct algorithm that produce the same hash. Thus, we optionally
+ * deviate from the correct algorithm just enough to avoid such collisions.
+ * While the bug itself affected the majority of passwords containing
+ * characters with the 8th bit set (although only a percentage of those in a
+ * collision-producing way), the anti-collision safety measure affects
+ * only a subset of passwords containing the '\xff' character (not even all of
+ * those passwords, just some of them). This character is not found in valid
+ * UTF-8 sequences and is rarely used in popular 8-bit character encodings.
+ * Thus, the safety measure is unlikely to cause much annoyance, and is a
+ * reasonable tradeoff to use when authenticating against existing hashes that
+ * are not reliably known to have been computed with the correct algorithm.
+ *
+ * We use an approach that tries to minimize side-channel leaks of password
+ * information - that is, we mostly use fixed-cost bitwise operations instead
+ * of branches or table lookups. (One conditional branch based on password
+ * length remains. It is not part of the bug aftermath, though, and is
+ * difficult and possibly unreasonable to avoid given the use of C strings by
+ * the caller, which results in similar timing leaks anyway.)
+ *
+ * For actual implementation, we set an array index in the variable "bug"
+ * (0 means no bug, 1 means sign extension bug emulation) and a flag in the
+ * variable "safety" (bit 16 is set when the safety measure is requested).
+ * Valid combinations of settings are:
+ *
+ * Prefix "$2a$": bug = 0, safety = 0x10000
+ * Prefix "$2b$": bug = 0, safety = 0
+ * Prefix "$2x$": bug = 1, safety = 0
+ * Prefix "$2y$": bug = 0, safety = 0
+ */
+
+ sign = diff = 0;
+
+ for (i = 0; i < _BLOWFISH_ROUNDS + 2; i++) {
+ tmp[0] = tmp[1] = 0;
+ for (j = 0; j < 4; j++) {
+ tmp[0] <<= 8;
+ tmp[0] |= (unsigned char)*ptr; /* correct */
+ tmp[1] <<= 8;
+ tmp[1] |= (signed char)*ptr; /* bug */
+/*
+ * Sign extension in the first char has no effect - nothing to overwrite yet,
+ * and those extra 24 bits will be fully shifted out of the 32-bit word. For
+ * chars 2, 3, 4 in each four-char block, we set bit 7 of "sign" if sign
+ * extension in tmp[1] occurs. Once this flag is set, it remains set.
+ */
+ if (j)
+ sign |= tmp[1] & 0x80;
+ if (!*ptr)
+ ptr = key;
+ else
+ ptr++;
+ }
+ diff |= tmp[0] ^ tmp[1]; /* Non-zero on any differences */
+
+ expanded[i] = tmp[bug];
+ initial[i] = _nettle_blowfish_initial_ctx.p[i] ^ tmp[bug];
+ }
+
+/*
+ * At this point, "diff" is zero if the correct and buggy algorithms produced
+ * exactly the same result. If so and if "sign" is non-zero, which indicates
+ * that there was a non-benign sign extension, this means that we have a
+ * collision between the correctly computed hash for this password and a set of
+ * passwords that could be supplied to the buggy algorithm. Our safety measure
+ * is meant to protect from such many-buggy to one-correct collisions, by
+ * deviating from the correct algorithm in such cases. Let's check for this.
+ */
+ diff |= diff >> 16; /* still zero if exact match */
+ diff &= 0xffff; /* ditto */
+ diff += 0xffff; /* bit 16 set if "diff" was non-zero (on non-match) */
+ sign <<= 9; /* move the non-benign sign extension flag to bit 16 */
+ sign &= ~diff & safety; /* action needed? */
+
+/*
+ * If we have determined that we need to deviate from the correct algorithm,
+ * flip bit 16 in initial expanded key. (The choice of 16 is arbitrary, but
+ * let's stick to it now. It came out of the approach we used above, and it's
+ * not any worse than any other choice we could make.)
+ *
+ * It is crucial that we don't do the same to the expanded key used in the main
+ * Eksblowfish loop. By doing it to only one of these two, we deviate from a
+ * state that could be directly specified by a password to the buggy algorithm
+ * (and to the fully correct one as well, but that's a side-effect).
+ */
+ initial[0] ^= sign;
+}
+
+static int ibcrypt(size_t length, char *dst,
+ const char *key, const char *scheme,
+ int minlog2rounds,
+ int log2rounds, const uint8_t *salt)
+{
+ struct {
+ struct blowfish_ctx ctx;
+ bf_key expanded_key;
+ union {
+ uint32_t salt[4];
+ uint32_t output[6];
+ } binary;
+ } data;
+ uint8_t psalt[BLOWFISH_BCRYPT_BINSALT_SIZE];
+ uint32_t L, R;
+ uint32_t *ptr;
+ uint32_t count;
+ int i;
+ size_t schemelen = strlen(scheme);
+ unsigned cscheme;
+ unsigned bug = 0;
+ uint32_t safety = 0;
+ if (length < BLOWFISH_BCRYPT_HASH_SIZE ||
+ schemelen < 2)
+ return 0;
+
+ if (schemelen >= 3 && *scheme++ != '$')
+ return 0;
+ if (*scheme++ != '2')
+ return 0;
+
+ switch (cscheme = *scheme++) {
+ default:
+ return 0;
+ case 'a': safety = 0x10000;
+ break;
+ case 'x': bug = 1;
+ break;
+ case 'b': case 'y':
+ break;
+ }
+
+ if (schemelen >= 4) {
+ if (*scheme++ != '$')
+ return 0;
+ if (schemelen >= 6) {
+ if (log2rounds < 0)
+ log2rounds = atoi(scheme);
+ scheme += 2;
+ if (schemelen >= CRYPTPLEN && *scheme++ != '$')
+ return 0;
+ if (schemelen >= HASHOFFSET && !salt) {
+ struct base64_decode_ctx ctx;
+ size_t saltlen = BLOWFISH_BCRYPT_BINSALT_SIZE;
+
+ base64_decode_init(&ctx);
+ ctx.table = radix64_decode_table;
+
+ if (!base64_decode_update(&ctx, &saltlen, (uint8_t *) data.binary.salt,
+ SALTLEN, scheme)
+ || saltlen != BLOWFISH_BCRYPT_BINSALT_SIZE)
+ return 0;
+ }
+ }
+ }
+
+ if (salt)
+ memcpy(data.binary.salt, salt, BLOWFISH_BCRYPT_BINSALT_SIZE);
+ else if (schemelen < HASHOFFSET)
+ return 0;
+ memcpy(psalt, data.binary.salt, BLOWFISH_BCRYPT_BINSALT_SIZE);
+ swap32(data.binary.salt, 4);
+
+ if (log2rounds < minlog2rounds || log2rounds > 31)
+ return 0;
+ count = (uint32_t)1 << log2rounds;
+
+ set_xkey(key, data.expanded_key, data.ctx.p, bug, safety);
+ memcpy(data.ctx.s, _nettle_blowfish_initial_ctx.s, sizeof(data.ctx.s));
+
+ L = R = 0;
+ for (i = 0; i < _BLOWFISH_ROUNDS + 2; i += 2) {
+ L ^= data.binary.salt[i & 2];
+ R ^= data.binary.salt[(i & 2) + 1];
+ _nettle_blowfish_encround(&data.ctx, &L, &R);
+ data.ctx.p[i] = L;
+ data.ctx.p[i + 1] = R;
+ }
+
+ ptr = data.ctx.s[0];
+ do {
+ ptr += 4;
+ L ^= data.binary.salt[(_BLOWFISH_ROUNDS + 2) & 3];
+ R ^= data.binary.salt[(_BLOWFISH_ROUNDS + 3) & 3];
+ _nettle_blowfish_encround(&data.ctx, &L, &R);
+ *(ptr - 4) = L;
+ *(ptr - 3) = R;
+
+ L ^= data.binary.salt[(_BLOWFISH_ROUNDS + 4) & 3];
+ R ^= data.binary.salt[(_BLOWFISH_ROUNDS + 5) & 3];
+ _nettle_blowfish_encround(&data.ctx, &L, &R);
+ *(ptr - 2) = L;
+ *(ptr - 1) = R;
+ } while (ptr < &data.ctx.s[3][0xFF]);
+
+ do {
+ int done;
+
+ for (i = 0; i < _BLOWFISH_ROUNDS + 2; i += 2) {
+ data.ctx.p[i] ^= data.expanded_key[i];
+ data.ctx.p[i + 1] ^= data.expanded_key[i + 1];
+ }
+
+ done = 0;
+ do {
+ uint32_t tmp1, tmp2, tmp3, tmp4;
+
+ L = R = 0;
+ ptr = data.ctx.p;
+ do {
+ ptr += 2;
+ _nettle_blowfish_encround(&data.ctx, &L, &R);
+ *(ptr - 2) = L;
+ *(ptr - 1) = R;
+ } while (ptr < &data.ctx.p[_BLOWFISH_ROUNDS + 2]);
+
+ ptr = data.ctx.s[0];
+ do {
+ ptr += 2;
+ _nettle_blowfish_encround(&data.ctx, &L, &R);
+ *(ptr - 2) = L;
+ *(ptr - 1) = R;
+ } while (ptr < &data.ctx.s[3][0xFF]);
+
+ if (done)
+ break;
+ done = 1;
+
+ tmp1 = data.binary.salt[0];
+ tmp2 = data.binary.salt[1];
+ tmp3 = data.binary.salt[2];
+ tmp4 = data.binary.salt[3];
+ for (i = 0; i < _BLOWFISH_ROUNDS; i += 4) {
+ data.ctx.p[i] ^= tmp1;
+ data.ctx.p[i + 1] ^= tmp2;
+ data.ctx.p[i + 2] ^= tmp3;
+ data.ctx.p[i + 3] ^= tmp4;
+ }
+ data.ctx.p[16] ^= tmp1;
+ data.ctx.p[17] ^= tmp2;
+ } while (1);
+ } while (--count);
+
+ for (i = 0; i < 6; i += 2) {
+ L = magic_w[i];
+ R = magic_w[i + 1];
+
+ count = 64;
+ do
+ _nettle_blowfish_encround(&data.ctx, &L, &R);
+ while (--count);
+
+ data.binary.output[i] = L;
+ data.binary.output[i + 1] = R;
+ }
+
+ *dst++ = '$';
+ *dst++ = '2';
+ *dst++ = cscheme;
+ *dst++ = '$';
+ *dst++ = '0' + log2rounds / 10;
+ *dst++ = '0' + log2rounds % 10;
+ *dst++ = '$';
+ dst = encode_radix64(dst, BLOWFISH_BCRYPT_BINSALT_SIZE, psalt) - 1;
+
+ swap32(data.binary.output, 6);
+/* This has to be bug-compatible with the original implementation, so
+ only encode 23 of the 24 bytes. */
+ encode_radix64(dst, 23, (uint8_t *) data.binary.output);
+ return cscheme;
+}
+
+/*
+ * Please preserve the runtime self-test. It serves two purposes at once:
+ *
+ * 1. We really can't afford the risk of producing incompatible hashes e.g.
+ * when there's something like gcc bug 26587 again, whereas an application or
+ * library integrating this code might not also integrate our external tests or
+ * it might not run them after every build. Even if it does, the miscompile
+ * might only occur on the production build, but not on a testing build (such
+ * as because of different optimization settings). It is painful to recover
+ * from incorrectly-computed hashes - merely fixing whatever broke is not
+ * enough. Thus, a proactive measure like this self-test is needed.
+ *
+ * 2. We don't want to leave sensitive data from our actual password hash
+ * computation on the stack or in registers. Previous revisions of the code
+ * would do explicit cleanups, but simply running the self-test after hash
+ * computation is more reliable.
+ *
+ * The performance cost of this quick self-test is around 0.6% at the "$2a$08"
+ * setting.
+ */
+int blowfish_bcrypt_hash(size_t length, char *dst,
+ const char *key, const char *scheme,
+ int log2rounds, const uint8_t *salt)
+{
+ const char *test_pw = "8b \xd0\xc1\xd2\xcf\xcc\xd8";
+ const char *test_scheme = "$2a$00$abcdefghijklmnopqrstuu";
+ static const char * const test_hashes[2] =
+ {"i1D709vfamulimlGcq0qq3UvuUasvEa\0\x55", /* 'a', 'b', 'y' */
+ "VUrPmXD6q/nVSSp7pNDhCR9071IfIRe\0\x55"}; /* 'x' */
+ const char *test_hash = test_hashes[0];
+ int cscheme;
+ int ok;
+ struct {
+ char s[HASHOFFSET + 1];
+ char o[HASHOFFSET + 31 + 1 + 1 + 1];
+ } buf;
+
+ if (length)
+ *dst = '\0';
+/* Hash the supplied password */
+ cscheme = ibcrypt(length, dst, key, scheme, 4, log2rounds, salt);
+
+/*
+ * Do a quick self-test. It is important that we make both calls to ibcrypt()
+ * from the same scope such that they likely use the same stack locations,
+ * which makes the second call overwrite the first call's sensitive data on the
+ * stack and makes it more likely that any alignment related issues would be
+ * detected by the self-test.
+ */
+ memcpy(buf.s, test_scheme, sizeof(buf.s));
+
+ if (cscheme)
+ test_hash = test_hashes[(buf.s[2] = cscheme) == 'x'];
+
+ memset(buf.o, 0x55, sizeof(buf.o));
+ buf.o[sizeof(buf.o) - 1] = 0;
+ ok = ibcrypt(sizeof(buf.o) - (1 + 1), buf.o, test_pw,
+ buf.s, 0, -1, (void*)0);
+
+ ok = (ok &&
+ !memcmp(buf.o, buf.s, HASHOFFSET) &&
+ !memcmp(buf.o + HASHOFFSET, test_hash, 31 + 1 + 1 + 1));
+
+ {
+ const char *k = "\xff\xa3" "34" "\xff\xff\xff\xa3" "345";
+ bf_key ae, ai, ye, yi;
+ set_xkey(k, ae, ai, 0, 0x10000); /* $2a$ */
+ set_xkey(k, ye, yi, 0, 0); /* $2y$ */
+ ai[0] ^= 0x10000; /* undo the safety (for comparison) */
+ ok = ok && ai[0] == 0xdb9c59bc && ye[17] == 0x33343500 &&
+ !memcmp(ae, ye, sizeof(ae)) &&
+ !memcmp(ai, yi, sizeof(ai));
+ }
+
+ return ok && !!cscheme;
+}
diff --git a/blowfish.h b/blowfish-internal.h
similarity index 50%
copy from blowfish.h
copy to blowfish-internal.h
index bcdc7cb6..da28fa34 100644
--- a/blowfish.h
+++ b/blowfish-internal.h
@@ -1,4 +1,4 @@
-/* blowfish.h
+/* blowfish-internal.h
Blowfish block cipher.
@@ -32,8 +32,8 @@
not, see http://www.gnu.org/licenses/.
*/
-#ifndef NETTLE_BLOWFISH_H_INCLUDED
-#define NETTLE_BLOWFISH_H_INCLUDED
+#ifndef NETTLE_BLOWFISH_INTERNAL_H_INCLUDED
+#define NETTLE_BLOWFISH_INTERNAL_H_INCLUDED
#include "nettle-types.h"
@@ -41,49 +41,12 @@
extern "C" {
#endif
-/* Name mangling */
-#define blowfish_set_key nettle_blowfish_set_key
-#define blowfish128_set_key nettle_blowfish128_set_key
-#define blowfish_encrypt nettle_blowfish_encrypt
-#define blowfish_decrypt nettle_blowfish_decrypt
-
-#define BLOWFISH_BLOCK_SIZE 8
-
-/* Variable key size between 64 and 448 bits. */
-#define BLOWFISH_MIN_KEY_SIZE 8
-#define BLOWFISH_MAX_KEY_SIZE 56
-
-/* Default to 128 bits */
-#define BLOWFISH_KEY_SIZE 16
-
-#define BLOWFISH128_KEY_SIZE 16
-
-#define _BLOWFISH_ROUNDS 16
-
-struct blowfish_ctx
-{
- uint32_t s[4][256];
- uint32_t p[_BLOWFISH_ROUNDS+2];
-};
-
-/* Returns 0 for weak keys, otherwise 1. */
-int
-blowfish_set_key(struct blowfish_ctx *ctx,
- size_t length, const uint8_t *key);
-int
-blowfish128_set_key(struct blowfish_ctx *ctx, const uint8_t *key);
-
-void
-blowfish_encrypt(const struct blowfish_ctx *ctx,
- size_t length, uint8_t *dst,
- const uint8_t *src);
-void
-blowfish_decrypt(const struct blowfish_ctx *ctx,
- size_t length, uint8_t *dst,
- const uint8_t *src);
+extern const struct blowfish_ctx _nettle_blowfish_initial_ctx;
+extern void _nettle_blowfish_encround (const struct blowfish_ctx *ctx,
+ uint32_t * ret_xl, uint32_t * ret_xr);
#ifdef __cplusplus
}
#endif
-#endif /* NETTLE_BLOWFISH_H_INCLUDED */
+#endif /* NETTLE_BLOWFISH_INTERNAL_H_INCLUDED */
diff --git a/blowfish.c b/blowfish.c
index 52040f13..e73caffe 100644
--- a/blowfish.c
+++ b/blowfish.c
@@ -54,12 +54,13 @@
#include <assert.h>
#include "blowfish.h"
+#include "blowfish-internal.h"
#include "macros.h"
/* precomputed S boxes */
-static const struct blowfish_ctx
-initial_ctx = {
+const struct blowfish_ctx
+_nettle_blowfish_initial_ctx = {
{
{ /* ks0 */
0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96,
@@ -261,8 +262,8 @@ initial_ctx = {
#define R(c, l,r,i) do { l ^= c->p[i]; r ^= F(c,l); } while(0)
-static void
-encrypt (const struct blowfish_ctx *ctx, uint32_t * ret_xl,
+void
+_nettle_blowfish_encround (const struct blowfish_ctx *ctx, uint32_t * ret_xl,
uint32_t * ret_xr)
{
uint32_t xl, xr;
@@ -295,7 +296,7 @@ encrypt (const struct blowfish_ctx *ctx, uint32_t * ret_xl,
}
static void
-decrypt (const struct blowfish_ctx *ctx, uint32_t * ret_xl, uint32_t * ret_xr)
+decround (const struct blowfish_ctx *ctx, uint32_t * ret_xl, uint32_t * ret_xr)
{
uint32_t xl, xr;
@@ -339,7 +340,7 @@ blowfish_encrypt (const struct blowfish_ctx *ctx,
d1 = READ_UINT32(src);
d2 = READ_UINT32(src+4);
- encrypt (ctx, &d1, &d2);
+ _nettle_blowfish_encround (ctx, &d1, &d2);
dst[0] = (d1 >> 24) & 0xff;
dst[1] = (d1 >> 16) & 0xff;
dst[2] = (d1 >> 8) & 0xff;
@@ -361,7 +362,7 @@ blowfish_decrypt (const struct blowfish_ctx *ctx,
d1 = READ_UINT32(src);
d2 = READ_UINT32(src+4);
- decrypt (ctx, &d1, &d2);
+ decround (ctx, &d1, &d2);
dst[0] = (d1 >> 24) & 0xff;
dst[1] = (d1 >> 16) & 0xff;
dst[2] = (d1 >> 8) & 0xff;
@@ -380,7 +381,7 @@ blowfish_set_key (struct blowfish_ctx *ctx,
int i, j;
uint32_t data, datal, datar;
- *ctx = initial_ctx;
+ *ctx = _nettle_blowfish_initial_ctx;
for (i = j = 0; i < _BLOWFISH_ROUNDS + 2; i++)
{
@@ -393,15 +394,15 @@ blowfish_set_key (struct blowfish_ctx *ctx,
datal = datar = 0;
for (i = 0; i < _BLOWFISH_ROUNDS + 2; i += 2)
{
- encrypt (ctx, &datal, &datar);
+ _nettle_blowfish_encround (ctx, &datal, &datar);
ctx->p[i] = datal;
ctx->p[i + 1] = datar;
}
-
+
for (j = 0; j < 4; j++)
for (i = 0; i < 256; i += 2)
{
- encrypt (ctx, &datal, &datar);
+ _nettle_blowfish_encround (ctx, &datal, &datar);
ctx->s[j][i] = datal;
ctx->s[j][i + 1] = datar;
}
diff --git a/blowfish.h b/blowfish.h
index bcdc7cb6..af48e20f 100644
--- a/blowfish.h
+++ b/blowfish.h
@@ -46,6 +46,8 @@ extern "C" {
#define blowfish128_set_key nettle_blowfish128_set_key
#define blowfish_encrypt nettle_blowfish_encrypt
#define blowfish_decrypt nettle_blowfish_decrypt
+#define blowfish_bcrypt_hash nettle_blowfish_bcrypt_hash
+#define blowfish_bcrypt_verify nettle_blowfish_bcrypt_verify
#define BLOWFISH_BLOCK_SIZE 8
@@ -60,6 +62,9 @@ extern "C" {
#define _BLOWFISH_ROUNDS 16
+#define BLOWFISH_BCRYPT_HASH_SIZE (60 + 1) /* Including null-terminator */
+#define BLOWFISH_BCRYPT_BINSALT_SIZE 16 /* Binary string size */
+
struct blowfish_ctx
{
uint32_t s[4][256];
@@ -81,6 +86,16 @@ void
blowfish_decrypt(const struct blowfish_ctx *ctx,
size_t length, uint8_t *dst,
const uint8_t *src);
+int
+blowfish_bcrypt_hash(size_t length,
+ char *dst,
+ const char *key,
+ const char *scheme,
+ int log2rounds,
+ const uint8_t *salt);
+int
+blowfish_bcrypt_verify(const char *key,
+ const char *hashed);
#ifdef __cplusplus
}
diff --git a/nettle.texinfo b/nettle.texinfo
index ff64889c..13b1df39 100644
--- a/nettle.texinfo
+++ b/nettle.texinfo
@@ -1512,6 +1512,73 @@ in any other way.
Analogous to @code{blowfish_encrypt}
@end deftypefun
+@deftypefun int blowfish_bcrypt_hash (size_t @var{length}, char *@var{dst}, const char *@var{key}, const char *@var{scheme}, int @var{log2rounds}, const uint8_t *@var{salt})
+Compute the bcrypt password hash.
+The function will return @code{0} if the hash cannot be computed
+due to invalid input.
+The function will return @code{1} and store the computed hash
+in the array pointed to by @var{dst}. The hash is computed based
+on the chosen @var{scheme}, number of rounds @var{log2rounds} and
+specified @var{salt}.
+
+@var{length} must be at least @code{BLOWFISH_BCRYPT_HASH_SIZE}.
+
+@var{dst} must point to a character array of the specified @var{length}.
+
+@var{key} contains the zero terminated plaintext password string.
+
+@var{scheme} contains either just the chosen scheme (valid schemes
+are: @code{2a}, @code{2b}, @code{2x} or @code{2y}), or
+(the prefix of) an existing hashed password (typically @code{$2b$10$...}).
+
+@var{log2rounds} contains the log2 of the number of encryption rounds
+that must be used to compute the hash. If it is @code{-1} the value
+will be extracted from @var{scheme}.
+
+@var{salt} should point to an array of @code{BLOWFISH_BCRYPT_BINSALT_SIZE}
+random bytes to be used to perturb the hash computation. If it is @code{NULL}
+the salt will be extracted from @var{scheme}.
+
+Sample code to generate a bcrypt hash:
+@example
+char cleartxtpassword[] = "ExamplePassword";
+uint8_t salt[BLOWFISH_BCRYPT_BINSALT_SIZE];
+@dots{}
+/* Make sure that salt is filled with random bytes */
+@dots{}
+char hashedresult[BLOWFISH_BCRYPT_HASH_SIZE];
+int result = blowfish_bcrypt(sizeof(hashedresult), hashedresult,
+ cleartxtpassword, "2b", 10, salt);
+if (result)
+ printf("%s\n", hashedresult);
+@end example
+@end deftypefun
+
+@deftypefun int blowfish_bcrypt_verify (const char *@var{key}, const char *@var{hashed})
+Verifies the bcrypt password hash against the supplied plaintext password.
+The function will return @code{0} if the password does not match.
+The function will return @code{1} if the password matches.
+
+@var{key} contains the zero terminated plaintext password string.
+
+@var{hashed} contains the zero terminated hashed string to compare with.
+
+Sample code to verify a bcrypt hash:
+@example
+char cleartxtpassword[] = "ExamplePassword";
+char existinghashed[] =
+ "$2y$" /* Hash algorithm version */
+ "10" /* 2^10 hash rounds (strength) */
+ "$" /* separator */
+ "1b2lPgo4XumibnJGN3r3sO" /* base64 encoded 16-byte salt */
+ "u7wE7xNfYDKlAxZffJDCJdVfFTAyevu"; /* Hashedpart */
+if (blowfish_bcrypt_verify(cleartxtpassword, existinghashed))
+ printf("Password is correct.");
+else
+ printf("Password is incorrect.");
+@end example
+@end deftypefun
+
@subsection Camellia
@cindex Camellia
--
2.20.1
3
5
Hi,
I committed a change to update nettle version numbers, which implies a
new symbol version for internal symbols.
That seems to break the gnutls ci build,
https://gitlab.com/gnutls/nettle/-/jobs/487360242
The error is
1217 ./bootstrap: getting translations into po/.reference for gnutls...
1218 wget: /lib64/libhogweed.so.5: version `HOGWEED_INTERNAL_5_0' not found (required by /lib64/libgnutls.so.30)
1219 wget: /lib64/libnettle.so.7: version `NETTLE_INTERNAL_7_0' not found (required by /lib64/libgnutls.so.30)
I don't quite understand all details. This job buils and installs
nettle, as
./configure --disable-documentation --prefix=/usr --libdir=/usr/lib64 &&
make -j4 && make install
replacing any previously instaled version with the same soname. So
that's a somewhat broader test than I had expected. That explains why
linking of wget is affected.
The more worring part is that the installed gnutls library seems to
depend on internal nettle symbols (it would have been nice if the error
message named the offending symbols). Is that intended?
It's not necessarily wrong, but any packaged version of gnutls built
that way *must* be marked to depend on exactly the same versions of
nettle with which it was built. And the ci rule would need to use a
different prefix and some other way to tell the gnutls build to use the
newly built nettle library instead of the system version.
On my own debian machine I have libgnutls.so.30, but apparently an older
version. According to
objdump -T /usr/lib/x86_64-linux-gnu/libgnutls.so.30
the version I have refers only to symbols with version NETTLE_6 and
HOGWEED_4, i.e., nettle version older than 3.5. And no mention of
NETTLE_INTERNAL or HOGWEED_INTERNAL.
Regards,
/Niels
--
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.
5
9
Hi Everyone/Niels,
I've got a bare-bones Mac-mini without Xcode. It has CC Tools but that
is it. It is in a stock configuration.
When I attempt to run 'make check' nearly every test failed due to
missing libnettle and libhogweed. The libraries were present in .lib/,
but something was blowing away LD_LIBRARY_PATH and DYLD_LIBRARY_PATH.
My driver script set it, and the Nettle sources also set it after I
set it. But by the times the test runner scripts were run, they were
empty again (they were empty at the Makefile, before hitting the test
runners).
I know Apple did some hardening lately but I have not read anything
about scrubbing LD_LIBRARY_PATH and DYLD_LIBRARY_PATH. But it appears
something was sanitizing the environment.
Attached is the patch I needed for the issue on OS X 10.12. It also
tested good on Ubuntu 18.04 and Solaris 11.3. Earlier versions of OS
X, like OS X 10.9 and OS X 10.5, were OK. It was just OS X 10.12.
The short of it is, each test runner, like run-tests.sh, needed to
restore the variables using an absolute path. dirname is Posix so it
is available to strip the unwanted path component, like testsuite/ or
example/.
+nettle_lib_dir=`dirname $PWD`/.lib
+LD_LIBRARY_PATH=$nettle_lib_dir
+DYLD_LIBRARY_PATH=$nettle_lib_dir
+
+export LD_LIBRARY_PATH
+export DYLD_LIBRARY_PATH
Jeff
3
13
Hi,
I committed a change to update nettle version numbers, which implies a
new symbol version for internal symbols.
That seems to break the gnutls ci build,
https://gitlab.com/gnutls/nettle/-/jobs/487360242
The error is
1217 ./bootstrap: getting translations into po/.reference for gnutls...
1218 wget: /lib64/libhogweed.so.5: version `HOGWEED_INTERNAL_5_0' not found (required by /lib64/libgnutls.so.30)
1219 wget: /lib64/libnettle.so.7: version `NETTLE_INTERNAL_7_0' not found (required by /lib64/libgnutls.so.30)
I don't quite understand all details. This job buils and installs
nettle, as
./configure --disable-documentation --prefix=/usr --libdir=/usr/lib64 &&
make -j4 && make install
replacing any previously installed version with the same soname. So
that's a somewhat broader test than I had expected. That explains why
linking of wget is affected.
The more worring part is that the installed gnutls library seems to
depend on internal nettle symbols (it would have been nice if the error
message named the offending symbols). Is that intended?
It's not necessarily wrong, but any packaged version of gnutls built
that way *must* be marked to depend on exactly the same versions of
nettle with which it was built. And the ci rule would need to use a
different prefix and some other way to tell the gnutls build to use the
newly built nettle library instead of the system version.
On my own debian machine I have libgnutls.so.30, but apparently an older
version. According to
objdump -T /usr/lib/x86_64-linux-gnu/libgnutls.so.30
the version I have refers only to symbols with version NETTLE_6 and
HOGWEED_4, i.e., nettle version older than 3.5. And no mention of
NETTLE_INTERNAL or HOGWEED_INTERNAL.
Regards,
/Niels
--
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.
1
0
Hi,
I committed a change to update nettle version numbers, which implies a
new symbol version for internal symbols.
That seems to break the gnutls ci build,
https://gitlab.com/gnutls/nettle/-/jobs/487360242
The error is
1217 ./bootstrap: getting translations into po/.reference for gnutls...
1218 wget: /lib64/libhogweed.so.5: version `HOGWEED_INTERNAL_5_0' not found (required by /lib64/libgnutls.so.30)
1219 wget: /lib64/libnettle.so.7: version `NETTLE_INTERNAL_7_0' not found (required by /lib64/libgnutls.so.30)
I don't quite understand all details. This job buils and installs
nettle, as
./configure --disable-documentation --prefix=/usr --libdir=/usr/lib64 &&
make -j4 && make install
replacing any previously installed version with the same soname. So
that's a somewhat broader test than I had expected. That explains why
linking of wget is affected.
The more worring part is that the installed gnutls library seems to
depend on internal nettle symbols (it would have been nice if the error
message named the offending symbols). Is that intended?
It's not necessarily wrong, but any packaged version of gnutls built
that way *must* be marked to depend on exactly the same versions of
nettle with which it was built. And the ci rule would need to use a
different prefix and some other way to tell the gnutls build to use the
newly built nettle library instead of the system version.
On my own debian machine I have libgnutls.so.30, but apparently an older
version. According to
objdump -T /usr/lib/x86_64-linux-gnu/libgnutls.so.30
the version I have refers only to symbols with version NETTLE_6 and
HOGWEED_4, i.e., nettle version older than 3.5. And no mention of
NETTLE_INTERNAL or HOGWEED_INTERNAL.
Regards,
/Niels
--
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.
1
0