void edp_BasePointMultiply( OUT Affine_POINT *R, IN const U_WORD *sk, IN const void *blinding) { Ext_POINT S; U_WORD t[K_WORDS]; if (blinding) { eco_AddReduce(t, sk, ((EDP_BLINDING_CTX*)blinding)->bl); edp_BasePointMult(&S, t, ((EDP_BLINDING_CTX*)blinding)->zr); edp_AddPoint(&S, &S, &((EDP_BLINDING_CTX*)blinding)->BP); } else { edp_BasePointMult(&S, sk, edp_custom_blinding.zr); } ecp_Inverse(S.z, S.z); ecp_MulMod(R->x, S.x, S.z); ecp_MulMod(R->y, S.y, S.z); }
/* Return R = a*P where P is curve25519 base point */ void x25519_BasePointMultiply(OUT U8 *r, IN const U8 *sk) { Ext_POINT S; U_WORD t[K_WORDS]; ecp_BytesToWords(t, sk); edp_BasePointMult(&S, t, edp_custom_blinding.zr); /* Convert ed25519 point to x25519 point */ /* u = (1 + y)/(1 - y) = (Z + Y)/(Z - Y) */ ecp_AddReduce(S.t, S.z, S.y); ecp_SubReduce(S.z, S.z, S.y); ecp_Inverse(S.z, S.z); ecp_MulMod(S.t, S.t, S.z); ecp_WordsToBytes(r, S.t); }
int curve25519_SelfTest(int level) { int rc = 0; U64 A[4], B[4], C[4]; U8 a[32], b[32], c[32], d[32]; ecp_AddReduce(A, _w_I, _w_P); ECP_MOD(A); if (ecp_Cmp(A, _w_I) != 0) { rc++; printf("assert I+p == I mod p FAILED!!\n"); ecp_PrintHexWords("A_1", A, 4); } if (ecp_Cmp(_w_I, _w_P) >= 0) { rc++; printf("assert I < P FAILED!!\n"); } if (ecp_Cmp(_w_P, _w_I) <= 0) { rc++; printf("assert P > I FAILED!!\n"); } ecp_MulReduce(B, _w_I, _w_D); ECP_MOD(B); if (ecp_Cmp(B, _w_IxD) != 0) { rc++; printf("assert I*D FAILED!!\n"); ecp_PrintHexWords("A_2", B, 4); } // assert I*I == p-1 ecp_MulMod(A, _w_I, _w_I); if (ecp_Cmp(A, _w_Pm1) != 0) { rc++; printf("assert mul(I,I) == p-1 FAILED!!\n"); ecp_PrintHexWords("A_3", A, 4); } // assert I**2 == p-1 ecp_SqrReduce(B, _w_I); ECP_MOD(B); if (ecp_Cmp(B, _w_Pm1) != 0) { rc++; printf("assert square(I) == p-1 FAILED!!\n"); ecp_PrintHexWords("B_4", B, 4); } // assert (-I)*(-I) == p-1 ecp_Sub(B, _w_P, _w_I); ecp_MulMod(A, B, B); if (ecp_Cmp(A, _w_Pm1) != 0) { rc++; printf("assert mul(-I,-I) == p-1 FAILED!!\n"); ecp_PrintHexWords("A_5", A, 4); ecp_PrintHexWords("B_5", B, 4); } ecp_SetValue(A, 50153); ecp_Inverse(B, A); ecp_MulMod(A, A, B); if (ecp_Cmp(A, _w_One) != 0) { rc++; printf("invmod FAILED!!\n"); ecp_PrintHexWords("inv_50153", B, 4); ecp_PrintHexWords("expected_1", A, 4); } // assert expmod(d,(p-1)/2,p) == p-1 ecp_ExpMod(A, _w_D, _b_Pm1d2, 32); if (ecp_Cmp(A, _w_Pm1) != 0) { rc++; printf("assert expmod(d,(p-1)/2,p) == p-1 FAILED!!\n"); ecp_PrintHexWords("A_3", A, 4); } ecp_CalculateY(a, ecp_BasePoint); ecp_BytesToWords(A, a); if (ecp_Cmp(A, _w_Gy) != 0) { rc++; printf("assert clacY(Base) == Base.y FAILED!!\n"); ecp_PrintHexBytes("Calculated_Base.y", a, 32); } ecp_PointMultiply(a, ecp_BasePoint, _b_Om1, 32); if (memcmp(a, ecp_BasePoint, 32) != 0) { rc++; printf("assert (l-1).Base == Base FAILED!!\n"); ecp_PrintHexBytes("A_5", a, 32); } ecp_PointMultiply(a, ecp_BasePoint, _b_O, 32); ecp_BytesToWords(A, a); if (!ecp_IsZero(A)) { rc++; printf("assert l.Base == 0 FAILED!!\n"); ecp_PrintHexBytes("A_6", a, 32); } // Key generation ecp_PointMultiply(a, ecp_BasePoint, pk1, 32); ecp_PrintHexBytes("PublicKey1", a, 32); ecp_PointMultiply(b, ecp_BasePoint, pk2, 32); ecp_PrintHexBytes("PublicKey2", b, 32); // ECDH - key exchange ecp_PointMultiply(c, b, pk1, 32); ecp_PrintHexBytes("SharedKey1", c, 32); ecp_PointMultiply(d, a, pk2, 32); ecp_PrintHexBytes("SharedKey2", d, 32); if (memcmp(c, d, 32) != 0) { rc++; printf("ECDH key exchange FAILED!!\n"); } memset(a, 0x44, 32); // our secret key ecp_PointMultiply(b, ecp_BasePoint, a, 32); // public key ecp_PointMultiply(c, b, _b_k1, 32); ecp_PointMultiply(d, c, _b_k2, 32); if (memcmp(d, b, 32) != 0) { rc++; printf("assert k1.k2.D == D FAILED!!\n"); ecp_PrintHexBytes("D", d, 4); ecp_PrintHexBytes("C", c, 4); ecp_PrintHexBytes("A", a, 4); } ecp_BytesToWords(A, _b_k1); ecp_BytesToWords(B, _b_k2); eco_InvModBPO(C, A); if (ecp_Cmp(C, B) != 0) { rc++; printf("assert 1/k1 == k2 mod BPO FAILED!!\n"); ecp_PrintHexWords("Calc", C, 4); ecp_PrintHexWords("Expt", B, 4); } eco_MulMod(C, A, B); if (ecp_Cmp(C, _w_One) != 0) { rc++; printf("assert k1*k2 == 1 mod BPO FAILED!!\n"); ecp_PrintHexWords("Calc", C, 4); } return rc; }
/* K in a little-endian byte array */ void ecp_PointMultiply( OUT U8 *PublicKey, IN const U8 *BasePoint, IN const U8 *SecretKey, IN int len) { int i, j, k; U_WORD X[K_WORDS]; XZ_POINT P, Q, *PP[2], *QP[2]; ecp_BytesToWords(X, BasePoint); /* 1: P = (2k+1)G, Q = (2k+2)G */ /* 0: Q = (2k+1)G, P = (2k)G */ /* Find first non-zero bit */ while (len-- > 0) { k = SecretKey[len]; for (i = 0; i < 8; i++, k <<= 1) { /* P = kG, Q = (k+1)G */ if (k & 0x80) { /* We have first non-zero bit // This is always bit 254 for keys created according to the spec. // Start with randomized base point */ ecp_Add(P.Z, X, edp_custom_blinding.zr); /* P.Z = random */ ecp_MulReduce(P.X, X, P.Z); ecp_MontDouble(&Q, &P); PP[1] = &P; PP[0] = &Q; QP[1] = &Q; QP[0] = &P; /* Everything we reference in the below loop are on the stack // and already touched (cached) */ while (++i < 8) { k <<= 1; ECP_MONT(7); } while (len > 0) { k = SecretKey[--len]; ECP_MONT(7); ECP_MONT(6); ECP_MONT(5); ECP_MONT(4); ECP_MONT(3); ECP_MONT(2); ECP_MONT(1); ECP_MONT(0); } ecp_Inverse(Q.Z, P.Z); ecp_MulMod(X, P.X, Q.Z); ecp_WordsToBytes(PublicKey, X); return; } } } /* K is 0 */ mem_fill(PublicKey, 0, 32); }