testrand2.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_general.h"
  17. #include "fspr_random.h"
  18. #include "fspr_thread_proc.h"
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "testutil.h"
  23. static void hexdump(const unsigned char *b,int n)
  24. {
  25. int i;
  26. for(i=0 ; i < n ; ++i)
  27. {
  28. #if 0
  29. if((i&0xf) == 0)
  30. printf("%04x",i);
  31. printf(" %02x",b[i]);
  32. if((i&0xf) == 0xf)
  33. printf("\n");
  34. #else
  35. printf("0x%02x,",b[i]);
  36. if((i&7) == 7)
  37. printf("\n");
  38. #endif
  39. }
  40. printf("\n");
  41. }
  42. static fspr_random_t *r;
  43. typedef fspr_status_t APR_THREAD_FUNC rnd_fn(fspr_random_t *r,void *b,fspr_size_t n);
  44. static void rand_run_kat(abts_case *tc,rnd_fn *f,fspr_random_t *r,
  45. const unsigned char expected[128])
  46. {
  47. unsigned char c[128];
  48. fspr_status_t rv;
  49. rv=f(r,c,128);
  50. ABTS_INT_EQUAL(tc,0,rv);
  51. if(rv)
  52. return;
  53. if(memcmp(c,expected,128))
  54. {
  55. hexdump(c,128);
  56. hexdump(expected,128);
  57. ABTS_FAIL(tc,"Randomness mismatch");
  58. }
  59. }
  60. static int rand_check_kat(rnd_fn *f,fspr_random_t *r,
  61. const unsigned char expected[128])
  62. {
  63. unsigned char c[128];
  64. fspr_status_t rv;
  65. rv=f(r,c,128);
  66. if(rv)
  67. return 2;
  68. if(memcmp(c,expected,128))
  69. return 1;
  70. return 0;
  71. }
  72. static void rand_add_zeroes(fspr_random_t *r)
  73. {
  74. static unsigned char c[2048];
  75. fspr_random_add_entropy(r,c,sizeof c);
  76. }
  77. static void rand_run_seed_short(abts_case *tc,rnd_fn *f,fspr_random_t *r,
  78. int count)
  79. {
  80. int i;
  81. fspr_status_t rv;
  82. char c[1];
  83. for(i=0 ; i < count ; ++i)
  84. rand_add_zeroes(r);
  85. rv=f(r,c,1);
  86. ABTS_INT_EQUAL(tc,1,APR_STATUS_IS_ENOTENOUGHENTROPY(rv));
  87. }
  88. static void rand_seed_short(abts_case *tc, void *data)
  89. {
  90. r=fspr_random_standard_new(p);
  91. rand_run_seed_short(tc,fspr_random_insecure_bytes,r,32);
  92. }
  93. static void rand_kat(abts_case *tc, void *data)
  94. {
  95. unsigned char expected[128]=
  96. { 0x82,0x04,0xad,0xd2,0x0b,0xd5,0xac,0xda,
  97. 0x3d,0x85,0x58,0x38,0x54,0x6b,0x69,0x45,
  98. 0x37,0x4c,0xc7,0xd7,0x87,0xeb,0xbf,0xd9,
  99. 0xb1,0xb8,0xb8,0x2d,0x9b,0x33,0x6e,0x97,
  100. 0x04,0x1d,0x4c,0xb0,0xd1,0xdf,0x3d,0xac,
  101. 0xd2,0xaa,0xfa,0xcd,0x96,0xb7,0xcf,0xb1,
  102. 0x8e,0x3d,0xb3,0xe5,0x37,0xa9,0x95,0xb4,
  103. 0xaa,0x3d,0x11,0x1a,0x08,0x20,0x21,0x9f,
  104. 0xdb,0x08,0x3a,0xb9,0x57,0x9f,0xf2,0x1f,
  105. 0x27,0xdc,0xb6,0xc0,0x85,0x08,0x05,0xbb,
  106. 0x13,0xbe,0xb1,0xe9,0x63,0x2a,0xe2,0xa4,
  107. 0x23,0x15,0x2a,0x10,0xbf,0xdf,0x09,0xb3,
  108. 0xc7,0xfb,0x2d,0x87,0x48,0x19,0xfb,0xc0,
  109. 0x15,0x8c,0xcb,0xc6,0xbd,0x89,0x38,0x69,
  110. 0xa3,0xae,0xa3,0x21,0x58,0x50,0xe7,0xc4,
  111. 0x87,0xec,0x2e,0xb1,0x2d,0x6a,0xbd,0x46 };
  112. rand_add_zeroes(r);
  113. rand_run_kat(tc,fspr_random_insecure_bytes,r,expected);
  114. }
  115. static void rand_seed_short2(abts_case *tc, void *data)
  116. {
  117. rand_run_seed_short(tc,fspr_random_secure_bytes,r,320);
  118. }
  119. static void rand_kat2(abts_case *tc, void *data)
  120. {
  121. unsigned char expected[128]=
  122. { 0x38,0x8f,0x01,0x29,0x5a,0x5c,0x1f,0xa8,
  123. 0x00,0xde,0x16,0x4c,0xe5,0xf7,0x1f,0x58,
  124. 0xc0,0x67,0xe2,0x98,0x3d,0xde,0x4a,0x75,
  125. 0x61,0x3f,0x23,0xd8,0x45,0x7a,0x10,0x60,
  126. 0x59,0x9b,0xd6,0xaf,0xcb,0x0a,0x2e,0x34,
  127. 0x9c,0x39,0x5b,0xd0,0xbc,0x9a,0xf0,0x7b,
  128. 0x7f,0x40,0x8b,0x33,0xc0,0x0e,0x2a,0x56,
  129. 0xfc,0xe5,0xab,0xde,0x7b,0x13,0xf5,0xec,
  130. 0x15,0x68,0xb8,0x09,0xbc,0x2c,0x15,0xf0,
  131. 0x7b,0xef,0x2a,0x97,0x19,0xa8,0x69,0x51,
  132. 0xdf,0xb0,0x5f,0x1a,0x4e,0xdf,0x42,0x02,
  133. 0x71,0x36,0xa7,0x25,0x64,0x85,0xe2,0x72,
  134. 0xc7,0x87,0x4d,0x7d,0x15,0xbb,0x15,0xd1,
  135. 0xb1,0x62,0x0b,0x25,0xd9,0xd3,0xd9,0x5a,
  136. 0xe3,0x47,0x1e,0xae,0x67,0xb4,0x19,0x9e,
  137. 0xed,0xd2,0xde,0xce,0x18,0x70,0x57,0x12 };
  138. rand_add_zeroes(r);
  139. rand_run_kat(tc,fspr_random_secure_bytes,r,expected);
  140. }
  141. static void rand_barrier(abts_case *tc, void *data)
  142. {
  143. fspr_random_barrier(r);
  144. rand_run_seed_short(tc,fspr_random_secure_bytes,r,320);
  145. }
  146. static void rand_kat3(abts_case *tc, void *data)
  147. {
  148. unsigned char expected[128]=
  149. { 0xe8,0xe7,0xc9,0x45,0xe2,0x2a,0x54,0xb2,
  150. 0xdd,0xe0,0xf9,0xbc,0x3d,0xf9,0xce,0x3c,
  151. 0x4c,0xbd,0xc9,0xe2,0x20,0x4a,0x35,0x1c,
  152. 0x04,0x52,0x7f,0xb8,0x0f,0x60,0x89,0x63,
  153. 0x8a,0xbe,0x0a,0x44,0xac,0x5d,0xd8,0xeb,
  154. 0x24,0x7d,0xd1,0xda,0x4d,0x86,0x9b,0x94,
  155. 0x26,0x56,0x4a,0x5e,0x30,0xea,0xd4,0xa9,
  156. 0x9a,0xdf,0xdd,0xb6,0xb1,0x15,0xe0,0xfa,
  157. 0x28,0xa4,0xd6,0x95,0xa4,0xf1,0xd8,0x6e,
  158. 0xeb,0x8c,0xa4,0xac,0x34,0xfe,0x06,0x92,
  159. 0xc5,0x09,0x99,0x86,0xdc,0x5a,0x3c,0x92,
  160. 0xc8,0x3e,0x52,0x00,0x4d,0x01,0x43,0x6f,
  161. 0x69,0xcf,0xe2,0x60,0x9c,0x23,0xb3,0xa5,
  162. 0x5f,0x51,0x47,0x8c,0x07,0xde,0x60,0xc6,
  163. 0x04,0xbf,0x32,0xd6,0xdc,0xb7,0x31,0x01,
  164. 0x29,0x51,0x51,0xb3,0x19,0x6e,0xe4,0xf8 };
  165. rand_run_kat(tc,fspr_random_insecure_bytes,r,expected);
  166. }
  167. static void rand_kat4(abts_case *tc, void *data)
  168. {
  169. unsigned char expected[128]=
  170. { 0x7d,0x0e,0xc4,0x4e,0x3e,0xac,0x86,0x50,
  171. 0x37,0x95,0x7a,0x98,0x23,0x26,0xa7,0xbf,
  172. 0x60,0xfb,0xa3,0x70,0x90,0xc3,0x58,0xc6,
  173. 0xbd,0xd9,0x5e,0xa6,0x77,0x62,0x7a,0x5c,
  174. 0x96,0x83,0x7f,0x80,0x3d,0xf4,0x9c,0xcc,
  175. 0x9b,0x0c,0x8c,0xe1,0x72,0xa8,0xfb,0xc9,
  176. 0xc5,0x43,0x91,0xdc,0x9d,0x92,0xc2,0xce,
  177. 0x1c,0x5e,0x36,0xc7,0x87,0xb1,0xb4,0xa3,
  178. 0xc8,0x69,0x76,0xfc,0x35,0x75,0xcb,0x08,
  179. 0x2f,0xe3,0x98,0x76,0x37,0x80,0x04,0x5c,
  180. 0xb8,0xb0,0x7f,0xb2,0xda,0xe3,0xa3,0xba,
  181. 0xed,0xff,0xf5,0x9d,0x3b,0x7b,0xf3,0x32,
  182. 0x6c,0x50,0xa5,0x3e,0xcc,0xe1,0x84,0x9c,
  183. 0x17,0x9e,0x80,0x64,0x09,0xbb,0x62,0xf1,
  184. 0x95,0xf5,0x2c,0xc6,0x9f,0x6a,0xee,0x6d,
  185. 0x17,0x35,0x5f,0x35,0x8d,0x55,0x0c,0x07 };
  186. rand_add_zeroes(r);
  187. rand_run_kat(tc,fspr_random_secure_bytes,r,expected);
  188. }
  189. #if APR_HAS_FORK
  190. static void rand_fork(abts_case *tc, void *data)
  191. {
  192. fspr_proc_t proc;
  193. fspr_status_t rv;
  194. unsigned char expected[128]=
  195. { 0xac,0x93,0xd2,0x5c,0xc7,0xf5,0x8d,0xc2,
  196. 0xd8,0x8d,0xb6,0x7a,0x94,0xe1,0x83,0x4c,
  197. 0x26,0xe2,0x38,0x6d,0xf5,0xbd,0x9d,0x6e,
  198. 0x91,0x77,0x3a,0x4b,0x9b,0xef,0x9b,0xa3,
  199. 0x9f,0xf6,0x6d,0x0c,0xdc,0x4b,0x02,0xe9,
  200. 0x5d,0x3d,0xfc,0x92,0x6b,0xdf,0xc9,0xef,
  201. 0xb9,0xa8,0x74,0x09,0xa3,0xff,0x64,0x8d,
  202. 0x19,0xc1,0x31,0x31,0x17,0xe1,0xb7,0x7a,
  203. 0xe7,0x55,0x14,0x92,0x05,0xe3,0x1e,0xb8,
  204. 0x9b,0x1b,0xdc,0xac,0x0e,0x15,0x08,0xa2,
  205. 0x93,0x13,0xf6,0x04,0xc6,0x9d,0xf8,0x7f,
  206. 0x26,0x32,0x68,0x43,0x2e,0x5a,0x4f,0x47,
  207. 0xe8,0xf8,0x59,0xb7,0xfb,0xbe,0x30,0x04,
  208. 0xb6,0x63,0x6f,0x19,0xf3,0x2c,0xd4,0xeb,
  209. 0x32,0x8a,0x54,0x01,0xd0,0xaf,0x3f,0x13,
  210. 0xc1,0x7f,0x10,0x2e,0x08,0x1c,0x28,0x4b, };
  211. rv=fspr_proc_fork(&proc,p);
  212. if(rv == APR_INCHILD)
  213. {
  214. int n;
  215. n=rand_check_kat(fspr_random_secure_bytes,r,expected);
  216. exit(n);
  217. }
  218. else if(rv == APR_INPARENT)
  219. {
  220. int exitcode;
  221. fspr_exit_why_e why;
  222. rand_run_kat(tc,fspr_random_secure_bytes,r,expected);
  223. fspr_proc_wait(&proc,&exitcode,&why,APR_WAIT);
  224. if(why != APR_PROC_EXIT)
  225. {
  226. ABTS_FAIL(tc,"Child terminated abnormally");
  227. return;
  228. }
  229. if(exitcode == 0)
  230. {
  231. ABTS_FAIL(tc,"Child produced our randomness");
  232. return;
  233. }
  234. else if(exitcode == 2)
  235. {
  236. ABTS_FAIL(tc,"Child randomness failed");
  237. return;
  238. }
  239. else if(exitcode != 1)
  240. {
  241. ABTS_FAIL(tc,"Uknown child error");
  242. return;
  243. }
  244. }
  245. else
  246. {
  247. ABTS_FAIL(tc,"Fork failed");
  248. return;
  249. }
  250. }
  251. #endif
  252. abts_suite *testrand2(abts_suite *suite)
  253. {
  254. suite = ADD_SUITE(suite)
  255. abts_run_test(suite, rand_seed_short, NULL);
  256. abts_run_test(suite, rand_kat, NULL);
  257. abts_run_test(suite, rand_seed_short2, NULL);
  258. abts_run_test(suite, rand_kat2, NULL);
  259. abts_run_test(suite, rand_barrier, NULL);
  260. abts_run_test(suite, rand_kat3, NULL);
  261. abts_run_test(suite, rand_kat4, NULL);
  262. #if APR_HAS_FORK
  263. abts_run_test(suite, rand_fork, NULL);
  264. #endif
  265. return suite;
  266. }