PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/libreoffice-3.6.0.2/sal/workben/t_digest.c

#
C | 380 lines | 302 code | 48 blank | 30 comment | 18 complexity | a8c8735616e1bb26af666a5343405a93 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, LGPL-3.0
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*************************************************************************
  3. *
  4. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5. *
  6. * Copyright 2000, 2010 Oracle and/or its affiliates.
  7. *
  8. * OpenOffice.org - a multi-platform office productivity suite
  9. *
  10. * This file is part of OpenOffice.org.
  11. *
  12. * OpenOffice.org is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License version 3
  14. * only, as published by the Free Software Foundation.
  15. *
  16. * OpenOffice.org is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License version 3 for more details
  20. * (a copy is included in the LICENSE file that accompanied this code).
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * version 3 along with OpenOffice.org. If not, see
  24. * <http://www.openoffice.org/license.html>
  25. * for a copy of the LGPLv3 License.
  26. *
  27. ************************************************************************/
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <rtl/digest.h>
  32. static const char *digest_in_MD[] =
  33. {
  34. "",
  35. "a",
  36. "abc",
  37. "message digest",
  38. "abcdefghijklmnopqrstuvwxyz",
  39. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  40. "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  41. NULL,
  42. };
  43. static const char *digest_out_MD2[]=
  44. {
  45. "8350e5a3e24c153df2275c9f80692773",
  46. "32ec01ec4a6dac72c0ab96fb34c0b5d1",
  47. "da853b0d3f88d99b30283a69e6ded6bb",
  48. "ab4f496bfb2a530b219ff33031fe06b0",
  49. "4e8ddff3650292ab5a4108c3aa47940b",
  50. "da33def2a42df13975352846c30338cd",
  51. "d5976f79d83d3a0dc9806c3c66f3efd8",
  52. };
  53. static const char *digest_out_MD5[]=
  54. {
  55. "d41d8cd98f00b204e9800998ecf8427e",
  56. "0cc175b9c0f1b6a831c399e269772661",
  57. "900150983cd24fb0d6963f7d28e17f72",
  58. "f96b697d7cb7938d525a2f31aaf161d0",
  59. "c3fcd3d76192e4007dfb496cca67e13b",
  60. "d174ab98d277d9f5a5611c2c9f419d9f",
  61. "57edf4a22be3c955ac49da2e2107b67a",
  62. };
  63. static const char *digest_in_SHA[]=
  64. {
  65. "abc",
  66. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  67. NULL,
  68. };
  69. static const char *digest_out_SHA_0[]=
  70. {
  71. "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
  72. "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
  73. };
  74. static const char *digest_out_SHA_1[]=
  75. {
  76. "a9993e364706816aba3e25717850c26c9cd0d89d",
  77. "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  78. };
  79. static const char *digest_bigout_SHA_0=
  80. "3232affa48628a26653b5aaa44541fd90d690603";
  81. static const char *digest_bigout_SHA_1=
  82. "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
  83. static const char digest_key_HMAC_MD5_1[] =
  84. {
  85. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  86. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  87. 0x00
  88. };
  89. static const char digest_key_HMAC_MD5_2[] =
  90. {
  91. /* "Jefe" */
  92. 'J', 'e', 'f', 'e',
  93. 0x00
  94. };
  95. static const unsigned char digest_key_HMAC_MD5_3[] =
  96. {
  97. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  98. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  99. 0x00
  100. };
  101. static const char *digest_key_HMAC_MD5[] =
  102. {
  103. (const char*)&digest_key_HMAC_MD5_1,
  104. (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
  105. (const char*)&digest_key_HMAC_MD5_3,
  106. NULL
  107. };
  108. static const unsigned char digest_in_HMAC_MD5_3[] =
  109. {
  110. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  111. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  112. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  113. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  114. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  115. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  116. 0xDD, 0xDD,
  117. 0x00
  118. };
  119. static const char *digest_in_HMAC_MD5[] =
  120. {
  121. "Hi There",
  122. "what do ya want for nothing?",
  123. (const char*)&digest_in_HMAC_MD5_3,
  124. NULL
  125. };
  126. static const char *digest_out_HMAC_MD5[] =
  127. {
  128. "9294727a3638bb1c13f48ef8158bfc9d",
  129. "750c783e6ab0b503eaa86e310a5db738",
  130. "56be34521d144c88dbb8c733f0e8b3f6",
  131. NULL
  132. };
  133. static const char *digest_out_HMAC_SHA1[] =
  134. {
  135. /* unofficial, i.e. not verified */
  136. "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
  137. "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
  138. "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
  139. NULL
  140. };
  141. static char *pt (unsigned char *md, int length)
  142. {
  143. int i;
  144. static char buf[80];
  145. for (i=0; i<length; i++)
  146. sprintf(&(buf[i*2]),"%02x",md[i]);
  147. return(buf);
  148. }
  149. int SAL_CALL main (void)
  150. {
  151. const char **P,**R, **Q;
  152. char *p;
  153. int i=1, err=0;
  154. unsigned char md[80];
  155. unsigned char buffer[1000];
  156. rtlDigest Digest;
  157. P=digest_in_MD;
  158. R=digest_out_MD2;
  159. i = 1;
  160. while (*P)
  161. {
  162. rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
  163. p=pt (md, RTL_DIGEST_LENGTH_MD2);
  164. if (strcmp (p, *R))
  165. {
  166. printf("error calculating MD2 on '%s'\n",*P);
  167. printf("got %s instead of %s\n",p,*R);
  168. err++;
  169. }
  170. else
  171. printf("test (MD2) %d ok\n",i);
  172. i++;
  173. R++;
  174. P++;
  175. }
  176. P=digest_in_MD;
  177. R=digest_out_MD5;
  178. i=1;
  179. while (*P)
  180. {
  181. rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
  182. p=pt (md, RTL_DIGEST_LENGTH_MD5);
  183. if (strcmp (p, *R))
  184. {
  185. printf("error calculating MD5 on '%s'\n",*P);
  186. printf("got %s instead of %s\n",p,*R);
  187. err++;
  188. }
  189. else
  190. printf("test (MD5) %d ok\n",i);
  191. i++;
  192. R++;
  193. P++;
  194. }
  195. P=digest_in_SHA;
  196. R=digest_out_SHA_0;
  197. i=1;
  198. while (*P)
  199. {
  200. rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
  201. p=pt (md, RTL_DIGEST_LENGTH_SHA);
  202. if (strcmp (p, *R))
  203. {
  204. printf("error calculating SHA-0 on '%s'\n",*P);
  205. printf("got %s instead of %s\n",p,*R);
  206. err++;
  207. }
  208. else
  209. printf("test (SHA-0) %d ok\n",i);
  210. i++;
  211. R++;
  212. P++;
  213. }
  214. memset (buffer, 'a', sizeof(buffer));
  215. R = &digest_bigout_SHA_0;
  216. Digest = rtl_digest_createSHA();
  217. for (i=0; i<1000; i++)
  218. rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
  219. rtl_digest_getSHA (Digest, md, sizeof(md));
  220. rtl_digest_destroySHA (Digest);
  221. p=pt (md, RTL_DIGEST_LENGTH_SHA);
  222. if (strcmp (p, *R))
  223. {
  224. printf("error calculating SHA-0 on '%s'\n",p);
  225. printf("got %s instead of %s\n",p,*R);
  226. err++;
  227. }
  228. else
  229. printf("test (SHA-0) n ok\n");
  230. P=digest_in_SHA;
  231. R=digest_out_SHA_1;
  232. i=1;
  233. while (*P)
  234. {
  235. rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
  236. p=pt (md, RTL_DIGEST_LENGTH_SHA1);
  237. if (strcmp (p, *R))
  238. {
  239. printf("error calculating SHA-1 on '%s'\n",*P);
  240. printf("got %s instead of %s\n",p,*R);
  241. err++;
  242. }
  243. else
  244. printf("test (SHA-1) %d ok\n",i);
  245. i++;
  246. R++;
  247. P++;
  248. }
  249. memset (buffer, 'a', sizeof(buffer));
  250. R = &digest_bigout_SHA_1;
  251. Digest = rtl_digest_createSHA1();
  252. for (i=0; i<1000; i++)
  253. rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
  254. rtl_digest_getSHA1 (Digest, md, sizeof(md));
  255. rtl_digest_destroySHA1 (Digest);
  256. p=pt (md, RTL_DIGEST_LENGTH_SHA1);
  257. if (strcmp (p, *R))
  258. {
  259. printf("error calculating SHA-1 on '%s'\n",p);
  260. printf("got %s instead of %s\n",p,*R);
  261. err++;
  262. }
  263. else
  264. printf("test (SHA-1) n ok\n");
  265. P=digest_in_HMAC_MD5;
  266. Q=digest_key_HMAC_MD5;
  267. R=digest_out_HMAC_MD5;
  268. Digest = rtl_digest_createHMAC_MD5();
  269. i = 1;
  270. while (*P)
  271. {
  272. rtl_digest_initHMAC_MD5 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
  273. rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
  274. rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
  275. p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
  276. if (strcmp (p, *R))
  277. {
  278. printf("error calculating HMAC-MD5 on '%s'\n",*P);
  279. printf("got %s instead of %s\n",p,*R);
  280. err++;
  281. }
  282. else
  283. printf("test (HMAC-MD5) %d ok\n",i);
  284. i++;
  285. R++;
  286. P++;
  287. Q++;
  288. }
  289. rtl_digest_destroyHMAC_MD5 (Digest);
  290. P=digest_in_HMAC_MD5;
  291. Q=digest_key_HMAC_MD5;
  292. R=digest_out_HMAC_SHA1;
  293. Digest = rtl_digest_createHMAC_SHA1();
  294. i = 1;
  295. while (*P)
  296. {
  297. rtl_digest_initHMAC_SHA1 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
  298. rtl_digest_updateHMAC_SHA1 (Digest, (const sal_uInt8*)(*P), strlen(*P));
  299. rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
  300. p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
  301. if (strcmp (p, *R))
  302. {
  303. printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
  304. printf("got %s instead of %s\n",p,*R);
  305. err++;
  306. }
  307. else
  308. printf("test (HMAC-SHA-1) %d ok\n",i);
  309. i++;
  310. P++;
  311. Q++;
  312. R++;
  313. }
  314. rtl_digest_destroyHMAC_SHA1 (Digest);
  315. P=digest_in_HMAC_MD5;
  316. Q=digest_key_HMAC_MD5;
  317. rtl_digest_PBKDF2 (
  318. md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */
  319. (const sal_uInt8*)(Q[1]), strlen(Q[1]), /* [in] password */
  320. (const sal_uInt8*)(P[1]), strlen(P[1]), /* [in] salt */
  321. 1000); /* [in] iteration count */
  322. p=pt (md, RTL_DIGEST_LENGTH_MD5);
  323. if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
  324. {
  325. printf("error calculating PBKDF2 on '%s'\n", P[1]);
  326. err++;
  327. }
  328. else
  329. printf("test (PBKDF2) %d ok\n", 1);
  330. return (err);
  331. }
  332. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */