PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/crypto/skein/skein_debug.c

https://bitbucket.org/freebsd/freebsd-base
C | 247 lines | 218 code | 17 blank | 12 comment | 59 complexity | 6138cf55291c5e03ddfecc5e3e0e8644 MD5 | raw file
  1. /***********************************************************************
  2. **
  3. ** Debug output functions for Skein hashing.
  4. **
  5. ** Source code author: Doug Whiting, 2008.
  6. **
  7. ** This algorithm and source code is released to the public domain.
  8. **
  9. ************************************************************************/
  10. #include <stdio.h>
  11. #ifdef SKEIN_DEBUG /* only instantiate this code if SKEIN_DEBUG is on */
  12. #include "skein.h"
  13. static const char INDENT[] = " "; /* how much to indent on new line */
  14. uint_t skein_DebugFlag = 0; /* off by default. Must be set externally */
  15. static void Show64_step(size_t cnt,const u64b_t *X,size_t step)
  16. {
  17. size_t i,j;
  18. for (i=j=0;i < cnt;i++,j+=step)
  19. {
  20. if (i % 4 == 0) printf(INDENT);
  21. printf(" %08X.%08X ",(uint_32t)(X[j] >> 32),(uint_32t)X[j]);
  22. if (i % 4 == 3 || i==cnt-1) printf("\n");
  23. fflush(stdout);
  24. }
  25. }
  26. #define Show64(cnt,X) Show64_step(cnt,X,1)
  27. static void Show64_flag(size_t cnt,const u64b_t *X)
  28. {
  29. size_t xptr = (size_t) X;
  30. size_t step = (xptr & 1) ? 2 : 1;
  31. if (step != 1)
  32. {
  33. X = (const u64b_t *) (xptr & ~1);
  34. }
  35. Show64_step(cnt,X,step);
  36. }
  37. static void Show08(size_t cnt,const u08b_t *b)
  38. {
  39. size_t i;
  40. for (i=0;i < cnt;i++)
  41. {
  42. if (i %16 == 0) printf(INDENT);
  43. else if (i % 4 == 0) printf(" ");
  44. printf(" %02X",b[i]);
  45. if (i %16 == 15 || i==cnt-1) printf("\n");
  46. fflush(stdout);
  47. }
  48. }
  49. static const char *AlgoHeader(uint_t bits)
  50. {
  51. if (skein_DebugFlag & SKEIN_DEBUG_THREEFISH)
  52. switch (bits)
  53. {
  54. case 256: return ":Threefish-256: ";
  55. case 512: return ":Threefish-512: ";
  56. case 1024: return ":Threefish-1024:";
  57. }
  58. else
  59. switch (bits)
  60. {
  61. case 256: return ":Skein-256: ";
  62. case 512: return ":Skein-512: ";
  63. case 1024: return ":Skein-1024:";
  64. }
  65. return NULL;
  66. }
  67. void Skein_Show_Final(uint_t bits,const Skein_Ctxt_Hdr_t *h,size_t cnt,const u08b_t *outPtr)
  68. {
  69. if (skein_DebugFlag & SKEIN_DEBUG_CONFIG || ((h->T[1] & SKEIN_T1_BLK_TYPE_MASK) != SKEIN_T1_BLK_TYPE_CFG))
  70. if (skein_DebugFlag & SKEIN_DEBUG_FINAL)
  71. {
  72. printf("\n%s Final output=\n",AlgoHeader(bits));
  73. Show08(cnt,outPtr);
  74. printf(" ++++++++++\n");
  75. fflush(stdout);
  76. }
  77. }
  78. /* show state after a round (or "pseudo-round") */
  79. void Skein_Show_Round(uint_t bits,const Skein_Ctxt_Hdr_t *h,size_t r,const u64b_t *X)
  80. {
  81. static uint_t injectNum=0; /* not multi-thread safe! */
  82. if (skein_DebugFlag & SKEIN_DEBUG_CONFIG || ((h->T[1] & SKEIN_T1_BLK_TYPE_MASK) != SKEIN_T1_BLK_TYPE_CFG))
  83. if (skein_DebugFlag)
  84. {
  85. if (r >= SKEIN_RND_SPECIAL)
  86. { /* a key injection (or feedforward) point */
  87. injectNum = (r == SKEIN_RND_KEY_INITIAL) ? 0 : injectNum+1;
  88. if ( skein_DebugFlag & SKEIN_DEBUG_INJECT ||
  89. ((skein_DebugFlag & SKEIN_DEBUG_FINAL) && r == SKEIN_RND_FEED_FWD))
  90. {
  91. printf("\n%s",AlgoHeader(bits));
  92. switch (r)
  93. {
  94. case SKEIN_RND_KEY_INITIAL:
  95. printf(" [state after initial key injection]");
  96. break;
  97. case SKEIN_RND_KEY_INJECT:
  98. printf(" [state after key injection #%02d]",injectNum);
  99. break;
  100. case SKEIN_RND_FEED_FWD:
  101. printf(" [state after plaintext feedforward]");
  102. injectNum = 0;
  103. break;
  104. }
  105. printf("=\n");
  106. Show64(bits/64,X);
  107. if (r== SKEIN_RND_FEED_FWD)
  108. printf(" ----------\n");
  109. }
  110. }
  111. else if (skein_DebugFlag & SKEIN_DEBUG_ROUNDS)
  112. {
  113. uint_t j;
  114. u64b_t p[SKEIN_MAX_STATE_WORDS];
  115. const u08b_t *perm;
  116. const static u08b_t PERM_256 [4][ 4] = { { 0,1,2,3 }, { 0,3,2,1 }, { 0,1,2,3 }, { 0,3,2,1 } };
  117. const static u08b_t PERM_512 [4][ 8] = { { 0,1,2,3,4,5,6,7 },
  118. { 2,1,4,7,6,5,0,3 },
  119. { 4,1,6,3,0,5,2,7 },
  120. { 6,1,0,7,2,5,4,3 }
  121. };
  122. const static u08b_t PERM_1024[4][16] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 },
  123. { 0, 9, 2,13, 6,11, 4,15,10, 7,12, 3,14, 5, 8, 1 },
  124. { 0, 7, 2, 5, 4, 3, 6, 1,12,15,14,13, 8,11,10, 9 },
  125. { 0,15, 2,11, 6,13, 4, 9,14, 1, 8, 5,10, 3,12, 7 }
  126. };
  127. if ((skein_DebugFlag & SKEIN_DEBUG_PERMUTE) && (r & 3))
  128. {
  129. printf("\n%s [state after round %2d (permuted)]=\n",AlgoHeader(bits),(int)r);
  130. switch (bits)
  131. {
  132. case 256: perm = PERM_256 [r&3]; break;
  133. case 512: perm = PERM_512 [r&3]; break;
  134. default: perm = PERM_1024[r&3]; break;
  135. }
  136. for (j=0;j<bits/64;j++)
  137. p[j] = X[perm[j]];
  138. Show64(bits/64,p);
  139. }
  140. else
  141. {
  142. printf("\n%s [state after round %2d]=\n",AlgoHeader(bits),(int)r);
  143. Show64(bits/64,X);
  144. }
  145. }
  146. }
  147. }
  148. /* show state after a round (or "pseudo-round"), given a list of pointers */
  149. void Skein_Show_R_Ptr(uint_t bits,const Skein_Ctxt_Hdr_t *h,size_t r,const u64b_t *X_ptr[])
  150. {
  151. uint_t i;
  152. u64b_t X[SKEIN_MAX_STATE_WORDS];
  153. for (i=0;i<bits/64;i++) /* copy over the words */
  154. X[i] = X_ptr[i][0];
  155. Skein_Show_Round(bits,h,r,X);
  156. }
  157. /* show the state at the start of a block */
  158. void Skein_Show_Block(uint_t bits,const Skein_Ctxt_Hdr_t *h,const u64b_t *X,const u08b_t *blkPtr,
  159. const u64b_t *wPtr, const u64b_t *ksPtr, const u64b_t *tsPtr)
  160. {
  161. uint_t n;
  162. if (skein_DebugFlag & SKEIN_DEBUG_CONFIG || ((h->T[1] & SKEIN_T1_BLK_TYPE_MASK) != SKEIN_T1_BLK_TYPE_CFG))
  163. if (skein_DebugFlag)
  164. {
  165. if (skein_DebugFlag & SKEIN_DEBUG_HDR)
  166. {
  167. printf("\n%s Block: outBits=%4d. T0=%06X.",AlgoHeader(bits),(uint_t) h->hashBitLen,(uint_t)h->T[0]);
  168. printf(" Type=");
  169. n = (uint_t) ((h->T[1] & SKEIN_T1_BLK_TYPE_MASK) >> SKEIN_T1_POS_BLK_TYPE);
  170. switch (n)
  171. {
  172. case SKEIN_BLK_TYPE_KEY: printf("KEY. "); break;
  173. case SKEIN_BLK_TYPE_CFG: printf("CFG. "); break;
  174. case SKEIN_BLK_TYPE_PERS: printf("PERS."); break;
  175. case SKEIN_BLK_TYPE_PK : printf("PK. "); break;
  176. case SKEIN_BLK_TYPE_KDF: printf("KDF. "); break;
  177. case SKEIN_BLK_TYPE_MSG: printf("MSG. "); break;
  178. case SKEIN_BLK_TYPE_OUT: printf("OUT. "); break;
  179. default: printf("0x%02X.",n); break;
  180. }
  181. printf(" Flags=");
  182. printf((h->T[1] & SKEIN_T1_FLAG_FIRST) ? " First":" ");
  183. printf((h->T[1] & SKEIN_T1_FLAG_FINAL) ? " Final":" ");
  184. printf((h->T[1] & SKEIN_T1_FLAG_BIT_PAD) ? " Pad" :" ");
  185. n = (uint_t) ((h->T[1] & SKEIN_T1_TREE_LVL_MASK) >> SKEIN_T1_POS_TREE_LVL);
  186. if (n)
  187. printf(" TreeLevel = %02X",n);
  188. printf("\n");
  189. fflush(stdout);
  190. }
  191. if (skein_DebugFlag & SKEIN_DEBUG_TWEAK)
  192. {
  193. printf(" Tweak:\n");
  194. Show64(2,h->T);
  195. }
  196. if (skein_DebugFlag & SKEIN_DEBUG_STATE)
  197. {
  198. printf(" %s words:\n",(skein_DebugFlag & SKEIN_DEBUG_THREEFISH)?"Key":"State");
  199. Show64(bits/64,X);
  200. }
  201. if (skein_DebugFlag & SKEIN_DEBUG_KEYSCHED)
  202. {
  203. printf(" Tweak schedule:\n");
  204. Show64_flag(3,tsPtr);
  205. printf(" Key schedule:\n");
  206. Show64_flag((bits/64)+1,ksPtr);
  207. }
  208. if (skein_DebugFlag & SKEIN_DEBUG_INPUT_64)
  209. {
  210. printf(" Input block (words):\n");
  211. Show64(bits/64,wPtr);
  212. }
  213. if (skein_DebugFlag & SKEIN_DEBUG_INPUT_08)
  214. {
  215. printf(" Input block (bytes):\n");
  216. Show08(bits/8,blkPtr);
  217. }
  218. }
  219. }
  220. void Skein_Show_Key(uint_t bits,const Skein_Ctxt_Hdr_t *h,const u08b_t *key,size_t keyBytes)
  221. {
  222. if (keyBytes)
  223. if (skein_DebugFlag & SKEIN_DEBUG_CONFIG || ((h->T[1] & SKEIN_T1_BLK_TYPE_MASK) != SKEIN_T1_BLK_TYPE_CFG))
  224. if (skein_DebugFlag & SKEIN_DEBUG_KEY)
  225. {
  226. printf("\n%s MAC key = %4u bytes\n",AlgoHeader(bits),(unsigned) keyBytes);
  227. Show08(keyBytes,key);
  228. }
  229. }
  230. #endif