Merge commit 'release-4-0-0alpha1' into v4-0-test
[kai/samba.git] / source / lib / crypto / hmacsha1test.c
1 /* 
2    Unix SMB/CIFS implementation.
3    HMAC SHA-1 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 #include "includes.h"
20 #include "lib/crypto/crypto.h"
21
22 struct torture_context;
23
24 static DATA_BLOB data_blob_repeat_byte(uint8_t byte, size_t length)
25 {
26         DATA_BLOB b = data_blob(NULL, length);
27         memset(b.data, byte, length);
28         return b;
29 }
30
31 /*
32  This uses the test values from rfc2202
33 */
34 bool torture_local_crypto_hmacsha1(struct torture_context *torture) 
35 {
36         bool ret = true;
37         uint32_t i;
38         struct {
39                 DATA_BLOB key;
40                 DATA_BLOB data;
41                 DATA_BLOB sha1;
42         } testarray[7];
43
44         testarray[0].key        = data_blob_repeat_byte(0x0b, 20);
45         testarray[0].data       = data_blob_string_const("Hi There");
46         testarray[0].sha1       = strhex_to_data_blob("b617318655057264e28bc0b6fb378c8ef146be00");
47
48         testarray[1].key        = data_blob_string_const("Jefe");
49         testarray[1].data       = data_blob_string_const("what do ya want for nothing?");
50         testarray[1].sha1 = strhex_to_data_blob("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
51
52         testarray[2].key        = data_blob_repeat_byte(0xaa, 20);
53         testarray[2].data       = data_blob_repeat_byte(0xdd, 50);
54         testarray[2].sha1       = strhex_to_data_blob("125d7342b9ac11cd91a39af48aa17b4f63f175d3");
55         
56         testarray[3].key        = strhex_to_data_blob("0102030405060708090a0b0c0d0e0f10111213141516171819");
57         testarray[3].data       = data_blob_repeat_byte(0xcd, 50);
58         testarray[3].sha1       = strhex_to_data_blob("4c9007f4026250c6bc8414f9bf50c86c2d7235da");
59
60         testarray[4].key        = data_blob_repeat_byte(0x0c, 20);
61         testarray[4].data       = data_blob_string_const("Test With Truncation");
62         testarray[4].sha1       = strhex_to_data_blob("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04");
63         /* sha1-96 =                 0x4c1a03424b55e07fe7f27be1 */
64
65         testarray[5].key        = data_blob_repeat_byte(0xaa, 80);
66         testarray[5].data       = data_blob_string_const("Test Using Larger Than Block-Size Key - Hash Key First");
67         testarray[5].sha1       = strhex_to_data_blob("aa4ae5e15272d00e95705637ce8a3b55ed402112");
68
69         testarray[6].key        = data_blob_repeat_byte(0xaa, 80);
70         testarray[6].data       = data_blob_string_const("Test Using Larger Than Block-Size Key "
71                                                          "and Larger Than One Block-Size Data");
72         testarray[6].sha1       = strhex_to_data_blob("e8e99d0f45237d786d6bbaa7965c7808bbff1a91");
73
74         for (i=0; i < ARRAY_SIZE(testarray); i++) {
75                 struct HMACSHA1Context ctx;
76                 uint8_t sha1[SHA1HashSize];
77                 int e;
78
79                 hmac_sha1_init(testarray[i].key.data, testarray[i].key.length, &ctx);
80                 hmac_sha1_update(testarray[i].data.data, testarray[i].data.length, &ctx);
81                 hmac_sha1_final(sha1, &ctx);
82
83                 e = memcmp(testarray[i].sha1.data,
84                            sha1,
85                            MIN(testarray[i].sha1.length, sizeof(sha1)));
86                 if (e != 0) {
87                         printf("hmacsha1 test[%u]: failed\n", i);
88                         dump_data(0, testarray[i].key.data, testarray[i].key.length);
89                         dump_data(0, testarray[i].data.data, testarray[i].data.length);
90                         dump_data(0, testarray[i].sha1.data, testarray[i].sha1.length);
91                         dump_data(0, sha1, sizeof(sha1));
92                         ret = false;
93                 }
94         }
95
96         return ret;
97 }