Пример #1
0
        public static void CryptoSignKeypair(byte[] pk, int pkoffset, byte[] sk, int skoffset, byte[] seed,
                                             int seedoffset)
        {
            int i;

            Array.Copy(seed, seedoffset, sk, skoffset, 32);
            byte[] h = Sha512.Hash(sk, skoffset, 32); //ToDo: Remove alloc
            ScalarOperations.ScClamp(h, 0);

            GroupOperations.GeScalarmultBase(out GroupElementP3 a, h, 0);
            GroupOperations.ge_p3_tobytes(pk, pkoffset, ref a);

            for (i = 0; i < 32; ++i)
            {
                sk[skoffset + 32 + i] = pk[pkoffset + i];
            }

            CryptoBytes.Wipe(h);
        }
Пример #2
0
        // Original crypto_sign_open, for reference only

        /*public static int crypto_sign_open(
         * byte[] m, out int mlen,
         * byte[] sm, int smlen,
         * byte[] pk)
         * {
         *  byte[] h = new byte[64];
         *  byte[] checkr = new byte[32];
         *  GroupElementP3 A;
         *  GroupElementP2 R;
         *  int i;
         *
         *  mlen = -1;
         *  if (smlen < 64) return -1;
         *  if ((sm[63] & 224) != 0) return -1;
         *  if (GroupOperations.ge_frombytes_negate_vartime(out A, pk, 0) != 0) return -1;
         *
         *  for (i = 0; i < smlen; ++i) m[i] = sm[i];
         *  for (i = 0; i < 32; ++i) m[32 + i] = pk[i];
         *  Sha512BclWrapper.crypto_hash_sha512(h, m, 0, smlen);
         *  ScalarOperations.sc_reduce(h);
         *
         *  var sm32 = new byte[32];
         *  Array.Copy(sm, 32, sm32, 0, 32);
         *  GroupOperations.ge_double_scalarmult_vartime(out R, h, ref A, sm32);
         *  GroupOperations.ge_tobytes(checkr, 0, ref R);
         *  if (Helpers.crypto_verify_32(checkr, sm) != 0)
         *  {
         *      for (i = 0; i < smlen; ++i)
         *          m[i] = 0;
         *      return -1;
         *  }
         *
         *  for (i = 0; i < smlen - 64; ++i)
         *      m[i] = sm[64 + i];
         *  for (i = smlen - 64; i < smlen; ++i)
         *      m[i] = 0;
         *  mlen = smlen - 64;
         *  return 0;
         * }*/

        public static bool CryptoSignVerify(
            byte[] sig, int sigoffset,
            byte[] m, int moffset, int mlen,
            byte[] pk, int pkoffset)
        {
            byte[]         checkr = new byte[32];
            GroupElementP3 A;
            GroupElementP2 R;

            if ((sig[sigoffset + 63] & 224) != 0)
            {
                return(false);
            }

            if (GroupOperations.ge_frombytes_negate_vartime(out A, pk, pkoffset) != 0)
            {
                return(false);
            }

            Sha512 hasher = new Sha512();

            hasher.Update(sig, sigoffset, 32);
            hasher.Update(pk, pkoffset, 32);
            hasher.Update(m, moffset, mlen);
            byte[] h = hasher.Finish();

            ScalarOperations.ScReduce(h);

            byte[] sm32 = new byte[32]; //todo: remove allocation
            Array.Copy(sig, sigoffset + 32, sm32, 0, 32);
            GroupOperations.ge_double_scalarmult_vartime(out R, h, ref A, sm32);
            GroupOperations.GeToBytes(checkr, 0, ref R);
            bool result = CryptoBytes.ConstantTimeEquals(checkr, 0, sig, sigoffset, 32);

            CryptoBytes.Wipe(h);
            CryptoBytes.Wipe(checkr);
            return(result);
        }
Пример #3
0
        /*public static void crypto_sign(
         * byte[] sm, out int smlen,
         * byte[] m, int mlen,
         * byte[] sk
         * )
         * {
         *  byte[] az = new byte[64];
         *  byte[] r = new byte[64];
         *  byte[] hram = new byte[64];
         *  GroupElementP3 R;
         *  int i;
         *
         *  Helpers.crypto_hash_sha512(az, sk, 0, 32);
         *  az[0] &= 248;
         *  az[31] &= 63;
         *  az[31] |= 64;
         *
         *  smlen = mlen + 64;
         *  for (i = 0; i < mlen; ++i) sm[64 + i] = m[i];
         *  for (i = 0; i < 32; ++i) sm[32 + i] = az[32 + i];
         *  Helpers.crypto_hash_sha512(r, sm, 32, mlen + 32);
         *  for (i = 0; i < 32; ++i) sm[32 + i] = sk[32 + i];
         *
         *  ScalarOperations.sc_reduce(r);
         *  GroupOperations.ge_scalarmult_base(out R, r, 0);
         *  GroupOperations.ge_p3_tobytes(sm, 0, ref R);
         *
         *  Helpers.crypto_hash_sha512(hram, sm, 0, mlen + 64);
         *  ScalarOperations.sc_reduce(hram);
         *  var sm32 = new byte[32];
         *  Array.Copy(sm, 32, sm32, 0, 32);
         *  ScalarOperations.sc_muladd(sm32, hram, az, r);
         *  Array.Copy(sm32, 0, sm, 32, 32);
         * }*/

        public static void crypto_sign2(
            byte[] sig, int sigoffset,
            byte[] m, int moffset, int mlen,
            byte[] sk, int skoffset)
        {
            Sha512 hasher = new Sha512();

            {
                hasher.Update(sk, skoffset, 32);
                byte[] az = hasher.Finish();
                ScalarOperations.ScClamp(az, 0);

                hasher.Init();
                hasher.Update(az, 32, 32);
                hasher.Update(m, moffset, mlen);
                byte[] r = hasher.Finish();

                ScalarOperations.ScReduce(r);
                GroupElementP3 R;
                GroupOperations.GeScalarmultBase(out R, r, 0);
                GroupOperations.ge_p3_tobytes(sig, sigoffset, ref R);

                hasher.Init();
                hasher.Update(sig, sigoffset, 32);
                hasher.Update(sk, skoffset + 32, 32);
                hasher.Update(m, moffset, mlen);
                byte[] hram = hasher.Finish();

                ScalarOperations.ScReduce(hram);
                byte[] s = new byte[32];
                Array.Copy(sig, sigoffset + 32, s, 0, 32);
                ScalarOperations.ScMulAdd(s, hram, az, r);
                Array.Copy(s, 0, sig, sigoffset + 32, 32);
                CryptoBytes.Wipe(s);
            }
        }