0457d4d4ea4b18e947cd6db75760ba6a76e4049e
[kai/samba-autobuild/.git] / lib / crypto / md5test.c
1 /* 
2    Unix SMB/CIFS implementation.
3    MD5 tests
4    Copyright (C) Stefan Metzmacher
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21 #include "../lib/util/util.h"
22 #include "../lib/crypto/crypto.h"
23
24 struct torture_context;
25
26 /*
27  This uses the test values from rfc1321
28 */
29 bool torture_local_crypto_md5(struct torture_context *torture) 
30 {
31         bool ret = true;
32         uint32_t i;
33         struct {
34                 const char *data;
35                 const char *md5;
36         } testarray[] = {
37         {
38                 .data   = "",
39                 .md5    = "d41d8cd98f00b204e9800998ecf8427e"
40         },{
41                 .data   = "a",
42                 .md5    = "0cc175b9c0f1b6a831c399e269772661"
43         },{
44                 .data   = "abc",
45                 .md5    = "900150983cd24fb0d6963f7d28e17f72"
46         },{
47                 .data   = "message digest",
48                 .md5    = "f96b697d7cb7938d525a2f31aaf161d0"
49         },{
50                 .data   = "abcdefghijklmnopqrstuvwxyz",
51                 .md5    = "c3fcd3d76192e4007dfb496cca67e13b"
52         },{
53                 .data   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
54                                                  "abcdefghijklmnopqrstuvwxyz"
55                                                  "0123456789",
56                 .md5    = "d174ab98d277d9f5a5611c2c9f419d9f"
57         },{
58                 .data   = "123456789012345678901234567890"
59                                                  "123456789012345678901234567890"
60                                                  "12345678901234567890",
61                 .md5    = "57edf4a22be3c955ac49da2e2107b67a"
62         }
63         };
64
65         for (i=0; i < ARRAY_SIZE(testarray); i++) {
66                 struct MD5Context ctx;
67                 uint8_t md5[16];
68                 int e;
69
70                 DATA_BLOB data;
71                 DATA_BLOB md5blob;
72
73                 data = data_blob_string_const(testarray[i].data);
74                 md5blob  = strhex_to_data_blob(NULL, testarray[i].md5);
75
76                 MD5Init(&ctx);
77                 MD5Update(&ctx, data.data, data.length);
78                 MD5Final(md5, &ctx);
79
80                 e = memcmp(md5blob.data,
81                            md5,
82                            MIN(md5blob.length, sizeof(md5)));
83                 if (e != 0) {
84                         printf("md5 test[%u]: failed\n", i);
85                         dump_data(0, data.data, data.length);
86                         dump_data(0, md5blob.data, md5blob.length);
87                         dump_data(0, md5, sizeof(md5));
88                         ret = false;
89                 }
90                 talloc_free(md5blob.data);
91         }
92
93         return ret;
94 }