Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

93 rader
2.0 KiB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "../crypto_sign.h"
  4. #define MAXMBYTES 2048
  5. typedef uint32_t uint32;
  6. static uint32 seed[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 } ;
  7. static uint32 in[12];
  8. static uint32 out[8];
  9. static int outleft = 0;
  10. #define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
  11. #define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
  12. static void surf(void)
  13. {
  14. uint32 t[12]; uint32 x; uint32 sum = 0;
  15. int r; int i; int loop;
  16. for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
  17. for (i = 0;i < 8;++i) out[i] = seed[24 + i];
  18. x = t[11];
  19. for (loop = 0;loop < 2;++loop) {
  20. for (r = 0;r < 16;++r) {
  21. sum += 0x9e3779b9;
  22. MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
  23. MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
  24. MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
  25. }
  26. for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
  27. }
  28. }
  29. void randombytes(unsigned char *x,unsigned long long xlen)
  30. {
  31. while (xlen > 0) {
  32. if (!outleft) {
  33. if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
  34. surf();
  35. outleft = 8;
  36. }
  37. *x = out[--outleft];
  38. ++x;
  39. --xlen;
  40. }
  41. }
  42. unsigned char pk[CRYPTO_PUBLICKEYBYTES];
  43. unsigned char sk[CRYPTO_SECRETKEYBYTES];
  44. unsigned char m[MAXMBYTES];
  45. unsigned char sm[MAXMBYTES+CRYPTO_BYTES];
  46. //unsigned char mo[MAXMBYTES+CRYPTO_BYTES];
  47. unsigned long long smlen;
  48. unsigned long long mlen;
  49. int main(void)
  50. {
  51. int n,i,r;
  52. for(n=0;n<MAXMBYTES;n++)
  53. {
  54. crypto_sign_keypair(pk,sk);
  55. randombytes(m,n);
  56. crypto_sign(sm, &smlen, m, n, sk);
  57. for(i=0;i<smlen;i++)
  58. printf("%02x",sm[i]);
  59. printf("\n");
  60. r = crypto_sign_open(sm, &mlen, sm, smlen, pk);
  61. if(r)
  62. {
  63. printf("signature verification fails\n");
  64. return -1;
  65. }
  66. if(mlen != n)
  67. {
  68. printf("signature verification produces wrong message length\n");
  69. return -1;
  70. }
  71. for(i=0;i<n;i++)
  72. {
  73. if(sm[i] != m[i])
  74. {
  75. printf("signature verification does not recover message\n");
  76. return -1;
  77. }
  78. }
  79. }
  80. return 0;
  81. }