1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include <stdio.h> 25 #include <string.h> 26 #include <stdlib.h> 27 28 #include <rtl/digest.h> 29 30 static const char *digest_in_MD[] = 31 { 32 "", 33 "a", 34 "abc", 35 "message digest", 36 "abcdefghijklmnopqrstuvwxyz", 37 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 38 "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 39 NULL, 40 }; 41 42 static const char *digest_out_MD2[]= 43 { 44 "8350e5a3e24c153df2275c9f80692773", 45 "32ec01ec4a6dac72c0ab96fb34c0b5d1", 46 "da853b0d3f88d99b30283a69e6ded6bb", 47 "ab4f496bfb2a530b219ff33031fe06b0", 48 "4e8ddff3650292ab5a4108c3aa47940b", 49 "da33def2a42df13975352846c30338cd", 50 "d5976f79d83d3a0dc9806c3c66f3efd8", 51 }; 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 64 static const char *digest_in_SHA[]= 65 { 66 "abc", 67 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 68 NULL, 69 }; 70 71 static const char *digest_out_SHA_0[]= 72 { 73 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880", 74 "d2516ee1acfa5baf33dfc1c471e438449ef134c8", 75 }; 76 77 static const char *digest_out_SHA_1[]= 78 { 79 "a9993e364706816aba3e25717850c26c9cd0d89d", 80 "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 81 }; 82 83 static const char *digest_bigout_SHA_0= 84 "3232affa48628a26653b5aaa44541fd90d690603"; 85 86 static const char *digest_bigout_SHA_1= 87 "34aa973cd4c4daa4f61eeb2bdbad27316534016f"; 88 89 90 static const char digest_key_HMAC_MD5_1[] = 91 { 92 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 93 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 94 0x00 95 }; 96 static const char digest_key_HMAC_MD5_2[] = 97 { 98 /* "Jefe" */ 99 'J', 'e', 'f', 'e', 100 0x00 101 }; 102 static const unsigned char digest_key_HMAC_MD5_3[] = 103 { 104 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 105 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 106 0x00 107 }; 108 static const char *digest_key_HMAC_MD5[] = 109 { 110 (const char*)&digest_key_HMAC_MD5_1, 111 (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */ 112 (const char*)&digest_key_HMAC_MD5_3, 113 NULL 114 }; 115 116 static const unsigned char digest_in_HMAC_MD5_3[] = 117 { 118 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 119 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 120 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 121 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 122 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 123 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 124 0xDD, 0xDD, 125 0x00 126 }; 127 static const char *digest_in_HMAC_MD5[] = 128 { 129 "Hi There", 130 "what do ya want for nothing?", 131 (const char*)&digest_in_HMAC_MD5_3, 132 NULL 133 }; 134 135 static const char *digest_out_HMAC_MD5[] = 136 { 137 "9294727a3638bb1c13f48ef8158bfc9d", 138 "750c783e6ab0b503eaa86e310a5db738", 139 "56be34521d144c88dbb8c733f0e8b3f6", 140 NULL 141 }; 142 static const char *digest_out_HMAC_SHA1[] = 143 { 144 /* unofficial, i.e. not verified */ 145 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9", 146 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", 147 "d730594d167e35d5956fd8003d0db3d3f46dc7bb", 148 NULL 149 }; 150 151 152 static char *pt (unsigned char *md, int length) 153 { 154 int i; 155 static char buf[80]; 156 157 for (i=0; i<length; i++) 158 sprintf(&(buf[i*2]),"%02x",md[i]); 159 160 return(buf); 161 } 162 163 int SAL_CALL main (int argc, char **argv) 164 { 165 const char **P,**R, **Q; 166 char *p; 167 int i=1, err=0; 168 169 unsigned char md[80]; 170 unsigned char buffer[1000]; 171 172 rtlDigest Digest; 173 174 P=digest_in_MD; 175 R=digest_out_MD2; 176 i = 1; 177 while (*P) 178 { 179 rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md)); 180 181 p=pt (md, RTL_DIGEST_LENGTH_MD2); 182 if (strcmp (p, *R)) 183 { 184 printf("error calculating MD2 on '%s'\n",*P); 185 printf("got %s instead of %s\n",p,*R); 186 err++; 187 } 188 else 189 printf("test (MD2) %d ok\n",i); 190 i++; 191 R++; 192 P++; 193 } 194 195 P=digest_in_MD; 196 R=digest_out_MD5; 197 i=1; 198 while (*P) 199 { 200 rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md)); 201 202 p=pt (md, RTL_DIGEST_LENGTH_MD5); 203 if (strcmp (p, *R)) 204 { 205 printf("error calculating MD5 on '%s'\n",*P); 206 printf("got %s instead of %s\n",p,*R); 207 err++; 208 } 209 else 210 printf("test (MD5) %d ok\n",i); 211 i++; 212 R++; 213 P++; 214 } 215 216 P=digest_in_SHA; 217 R=digest_out_SHA_0; 218 i=1; 219 while (*P) 220 { 221 rtl_digest_SHA (*P, strlen(*P), md, sizeof(md)); 222 223 p=pt (md, RTL_DIGEST_LENGTH_SHA); 224 if (strcmp (p, *R)) 225 { 226 printf("error calculating SHA-0 on '%s'\n",*P); 227 printf("got %s instead of %s\n",p,*R); 228 err++; 229 } 230 else 231 printf("test (SHA-0) %d ok\n",i); 232 i++; 233 R++; 234 P++; 235 } 236 237 memset (buffer, 'a', sizeof(buffer)); 238 R = &digest_bigout_SHA_0; 239 240 Digest = rtl_digest_createSHA(); 241 for (i=0; i<1000; i++) 242 rtl_digest_updateSHA (Digest, buffer, sizeof(buffer)); 243 244 rtl_digest_getSHA (Digest, md, sizeof(md)); 245 rtl_digest_destroySHA (Digest); 246 247 p=pt (md, RTL_DIGEST_LENGTH_SHA); 248 if (strcmp (p, *R)) 249 { 250 printf("error calculating SHA-0 on '%s'\n",p); 251 printf("got %s instead of %s\n",p,*R); 252 err++; 253 } 254 else 255 printf("test (SHA-0) n ok\n"); 256 257 P=digest_in_SHA; 258 R=digest_out_SHA_1; 259 i=1; 260 while (*P) 261 { 262 rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md)); 263 264 p=pt (md, RTL_DIGEST_LENGTH_SHA1); 265 if (strcmp (p, *R)) 266 { 267 printf("error calculating SHA-1 on '%s'\n",*P); 268 printf("got %s instead of %s\n",p,*R); 269 err++; 270 } 271 else 272 printf("test (SHA-1) %d ok\n",i); 273 i++; 274 R++; 275 P++; 276 } 277 278 memset (buffer, 'a', sizeof(buffer)); 279 R = &digest_bigout_SHA_1; 280 281 Digest = rtl_digest_createSHA1(); 282 for (i=0; i<1000; i++) 283 rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer)); 284 285 rtl_digest_getSHA1 (Digest, md, sizeof(md)); 286 rtl_digest_destroySHA1 (Digest); 287 288 p=pt (md, RTL_DIGEST_LENGTH_SHA1); 289 if (strcmp (p, *R)) 290 { 291 printf("error calculating SHA-1 on '%s'\n",p); 292 printf("got %s instead of %s\n",p,*R); 293 err++; 294 } 295 else 296 printf("test (SHA-1) n ok\n"); 297 298 299 P=digest_in_HMAC_MD5; 300 Q=digest_key_HMAC_MD5; 301 R=digest_out_HMAC_MD5; 302 Digest = rtl_digest_createHMAC_MD5(); 303 i = 1; 304 while (*P) 305 { 306 rtl_digest_initHMAC_MD5 (Digest, *Q, strlen(*Q)); 307 rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P)); 308 rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md)); 309 310 p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5); 311 if (strcmp (p, *R)) 312 { 313 printf("error calculating HMAC-MD5 on '%s'\n",*P); 314 printf("got %s instead of %s\n",p,*R); 315 err++; 316 } 317 else 318 printf("test (HMAC-MD5) %d ok\n",i); 319 i++; 320 R++; 321 P++; 322 Q++; 323 } 324 rtl_digest_destroyHMAC_MD5 (Digest); 325 326 327 P=digest_in_HMAC_MD5; 328 Q=digest_key_HMAC_MD5; 329 R=digest_out_HMAC_SHA1; 330 Digest = rtl_digest_createHMAC_SHA1(); 331 i = 1; 332 while (*P) 333 { 334 rtl_digest_initHMAC_SHA1 (Digest, *Q, strlen(*Q)); 335 rtl_digest_updateHMAC_SHA1 (Digest, *P, strlen(*P)); 336 rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md)); 337 338 p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1); 339 if (strcmp (p, *R)) 340 { 341 printf("error calculating HMAC-SHA-1 on '%s'\n",*P); 342 printf("got %s instead of %s\n",p,*R); 343 err++; 344 } 345 else 346 printf("test (HMAC-SHA-1) %d ok\n",i); 347 i++; 348 P++; 349 Q++; 350 R++; 351 } 352 rtl_digest_destroyHMAC_SHA1 (Digest); 353 354 355 P=digest_in_HMAC_MD5; 356 Q=digest_key_HMAC_MD5; 357 rtl_digest_PBKDF2 ( 358 md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */ 359 Q[1], strlen(Q[1]), /* [in] password */ 360 P[1], strlen(P[1]), /* [in] salt */ 361 1000); /* [in] iteration count */ 362 363 p=pt (md, RTL_DIGEST_LENGTH_MD5); 364 if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df")) 365 { 366 printf("error calculating PBKDF2 on '%s'\n", P[1]); 367 err++; 368 } 369 else 370 printf("test (PBKDF2) %d ok\n", 1); 371 372 return (err); 373 } 374 375