⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ripemd.c

📁 flint库 RSA算法
💻 C
📖 第 1 页 / 共 3 页
字号:
  /*    solution of l+1+k = 448 mod 512, in this example k = 448-1-24 */  /*    = 423, 7 bit following the single "1" plus additional         */  /*    13 ULONGs (i.e. 416 bit) in the positions ULBlock[1]...[13].  */  /* 3. The length of the message is appended in 64 bit (2 ULONGs).   */  /*                                                                  */  /*    01100001 01100010 01100011 1 000...00 00...011000             */  /*       "a"      "b"      "c"   1+ 423Bit   64 bit (length)        */  /* Message length modulo 64 ULONG-blocks (512 bit) */  rest = total[0] & 0x3f;  /* Insert ULONGs into ULBlock */  for (i = 0; i < (rest >> 2); i++)    {      ULBlock[i] = UC2UL (clear);      clear += 4;    }  /* Remaining UCHARs go into ULBLock. Invariant: 0 <= i <= 15 */  for (j = i << 2; j < rest; j++)    {      ULBlock[i] |= (ULONG)*clear++ << ((j & 3) << 3);    }  /* Append 0x80 to ULBlock: At least one byte is still free */  ULBlock[i] |= (ULONG)0x80 << ((j & 3) << 3);  if (rest > 55) /* No space left for appending the message length (8 Byte), */    {            /* therefore store length into the following block          */      swallow (stomach, ULBlock);      memset (ULBlock, 0, sizeof (ULONG) << 4);    }  /* Append message length in bit */  ULBlock[14] = total[0] << 3;  ULBlock[15] = (total[0] >> 29) | (total[1] << 3);  swallow (stomach, ULBlock);#ifdef FLINT_SECURE  /* Overwrite temporary variables */  Zero4Ulong (&ULBlock[0], &ULBlock[1], &ULBlock[2], &ULBlock[3]);  Zero4Ulong (&ULBlock[4], &ULBlock[5], &ULBlock[6], &ULBlock[7]);  Zero4Ulong (&ULBlock[8], &ULBlock[9], &ULBlock[10], &ULBlock[11]);  Zero4Ulong (&ULBlock[12], &ULBlock[13], &ULBlock[14], &ULBlock[15]);#endif  return;}/******************************************************************************//* RIPEMD-128 kernel functions                                                *//******************************************************************************/static voidappetize128 (ULONG *stomach){  stomach[0] = 0x67452301UL;  stomach[1] = 0xefcdab89UL;  stomach[2] = 0x98badcfeUL;  stomach[3] = 0x10325476UL;  return;}static voidswallow128 (ULONG *stomach, ULONG *ULBlock){  int round, rol;  ULONG x;  ULONG a1 = stomach[0];  ULONG b1 = stomach[1];  ULONG c1 = stomach[2];  ULONG d1 = stomach[3];  ULONG a2 = stomach[0];  ULONG b2 = stomach[1];  ULONG c2 = stomach[2];  ULONG d2 = stomach[3];  /*lint -e123 Don't complain about "Macros ... defined with arguments" */  /* Rounds and parallel rounds 0-15 */  for (round = 0; round < 16; round++)    {      rol = s1[round];      x = a1 + (b1 ^ c1 ^ d1) + ULBlock[round];      CHAIN128 (a1, b1, c1, d1, x, rol);      rol = s2[round];      x = a2 + (c2 ^ (d2 & (b2 ^ c2))) + ULBlock[r2[round]] + 0x50a28be6UL;      CHAIN128 (a2, b2, c2, d2, x, rol);    }  /* Rounds and parallel rounds 16-31 */  for (round = 16; round < 32; round++)    {      rol = s1[round];      x = a1 + (d1 ^ (b1 & (c1 ^ d1))) + ULBlock[r1[round]] + 0x5A827999UL;      CHAIN128 (a1, b1, c1, d1, x, rol);      rol = s2[round];      x = a2 + ((b2 | ~c2) ^ d2) + ULBlock[r2[round]] + 0x5c4dd124UL;      CHAIN128 (a2, b2, c2, d2, x, rol);    }  /* Rounds and parallel rounds 32-47 */  for (round = 32; round < 48; round++)    {      rol = s1[round];      x = a1 + ((b1 | ~c1) ^ d1) + ULBlock[r1[round]] + 0x6ed9eba1UL;      CHAIN128 (a1, b1, c1, d1, x, rol);      rol = s2[round];      x = a2 + (d2 ^ (b2 & (c2 ^ d2))) + ULBlock[r2[round]] + 0x6d703ef3UL;      CHAIN128 (a2, b2, c2, d2, x, rol);    }  /* Rounds and parallel rounds 48-63 */  for (round = 48; round < 64; round++)    {      rol = s1[round];      x = a1 + (c1 ^ (d1 & (b1 ^ c1))) + ULBlock[r1[round]] + 0x8f1bbcdcUL;      CHAIN128 (a1, b1, c1, d1, x, rol);      rol = s2[round];      x = a2 + (b2 ^ c2 ^ d2) + ULBlock[r2[round]];      CHAIN128 (a2, b2, c2, d2, x, rol);    }  /* Result in stomach */  d2 += c1 + stomach[1];  stomach[1] = stomach[2] + d1 + a2;  stomach[2] = stomach[3] + a1 + b2;  stomach[3] = stomach[0] + b1 + c2;  stomach[0] = d2;#ifdef FLINT_SECURE  /* Overwrite temporary variables */  Zero4Ulong (&a1, &b1, &c1, &d1);  Zero4Ulong (&a2, &b2, &c2, &d2);  ZeroUlong (&x);#endif    return;}static voiddigest128 (ULONG *stomach, UCHAR *clear, ULONG total[]){  ULONG i,j, rest;  ULONG ULBlock[16];  memset (ULBlock, 0, sizeof (ULONG) << 4);  /* Padding as for RIPEMD-160 */  /* Message length modulo 64 ULONG-blocks (512 bit) */  rest = total[0] & 0x3f;  /* Insert ULONGs into ULBlock */  for (i = 0; i < (rest >> 2); i++)    {      ULBlock[i] = UC2UL (clear);      clear += 4;    }  /* Remaining UCHARs go into ULBLock. Invariant: 0 <= i <= 15 */  for (j = i << 2; j < rest; j++)    {      ULBlock[i] |= (ULONG)*clear++ << ((j & 3) << 3);    }  /* Append 0x80 to ULBlock: At least one byte is still free */  ULBlock[i] |= (ULONG)0x80 << ((j & 3) << 3);  if (rest > 55) /* No space left for appending the message length (8 Byte), */    {            /* therefore store length into the following block          */      swallow128 (stomach, ULBlock);      memset (ULBlock, 0, sizeof (ULONG) << 4);    }  /* Append message length in bit */  ULBlock[14] = total[0] << 3;  ULBlock[15] = (total[0] >> 29) | (total[1] << 3);  swallow128 (stomach, ULBlock);#ifdef FLINT_SECURE  /* Overwrite temporary variables */  Zero4Ulong (&ULBlock[0], &ULBlock[1], &ULBlock[2], &ULBlock[3]);  Zero4Ulong (&ULBlock[4], &ULBlock[5], &ULBlock[6], &ULBlock[7]);  Zero4Ulong (&ULBlock[8], &ULBlock[9], &ULBlock[10], &ULBlock[11]);  Zero4Ulong (&ULBlock[12], &ULBlock[13], &ULBlock[14], &ULBlock[15]);#endif  return;}#ifdef FLINT_SECURE/******************************************************************************//*                                                                            *//*  Function:   Purging of variables                                          *//*  Syntax:     ZeroUlong (ULONG *a);                                         *//*  Input:      ULONG *a (Pointer to ULONG variable to be purged)             *//*  Output:     *a overwritten by 0                                           *//*  Returns:    -                                                             *//*                                                                            *//******************************************************************************/static inline void ZeroUlong (ULONG *a){  *a = 0;}/******************************************************************************//*                                                                            *//*  Function:   Purging of variables                                          *//*  Syntax:     Zero2Ulong (ULONG *a, ULONG *b);                              *//*  Input:      ULONG *a (Pointer to ULONG variable to be purged)             *//*              ULONG *b (Pointer to ULONG variable to be purged)             *//*  Output:     *a, *b overwritten by 0                                       *//*  Returns:    -                                                             *//*                                                                            *//******************************************************************************/static inline void Zero2Ulong (ULONG *a, ULONG *b){  *a = *b = 0;}/******************************************************************************//*                                                                            *//*  Function:   Purging of variables                                          *//*  Syntax:     Zero4Ulong (ULONG *a, ULONG *b, ULONG *c, ULONG *d);          *//*  Input:      ULONG *a (Pointer to ULONG variable to be purged)             *//*              ULONG *b (Pointer to ULONG variable to be purged)             *//*              ULONG *c (Pointer to ULONG variable to be purged)             *//*              ULONG *d (Pointer to ULONG variable to be purged)             *//*  Output:     *a, *b, *c and *d overwritten by 0                            *//*  Returns:    -                                                             *//*                                                                            *//******************************************************************************/static inline void Zero4Ulong (ULONG *a, ULONG *b, ULONG *c, ULONG *d){  *a = *b = *c = *d = 0;}/******************************************************************************//*                                                                            *//*  Function:   Purging of Array                                              *//*  Syntax:     ZeroUcharArray (UCHAR *a, int Len);                           *//*  Input:      UCHAR *a (Pointer to array of UCHARs)                         *//*              int Len (Length of array in byte)                             *//*  Output:     Array overwritten by 0                                        *//*  Returns:    -                                                             *//*                                                                            *//******************************************************************************/static inline void ZeroUcharArray (void *a, size_t Len){  memset ((UCHAR*)a, 0, Len);}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -