[go: up one dir, main page]

File: sha1test.c

package info (click to toggle)
i2util 1.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,048 kB
  • ctags: 456
  • sloc: ansic: 5,286; sh: 3,791; makefile: 48
file content (154 lines) | stat: -rw-r--r-- 4,540 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * ex: set tabstop=4 ai expandtab softtabstop=4 shiftwidth=4:
 * -*- mode: c-basic-indent: 4; tab-width: 4; indent-tabls-mode: nil -*-
 *      $Id: sha1test.c 216 2012-10-16 19:57:01Z aaron $
 */
/************************************************************************
*                                                                       *
*                           Copyright (C)  2006                         *
*                               Internet2                               *
*                           All Rights Reserved                         *
*                                                                       *
************************************************************************/
/*
 *    File:         sha1test.c
 *
 *    Author:       Jeff W. Boote
 *                  Internet2
 *
 *    Date:         Fri Sep 29 18:48:51 MDT 2006
 *
 *    Description:    
 *                  Use the test vectors from fips 180-1 to test the
 *                  sha-1 implementation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include <I2util/sha1.h>

int
main(
        int     argc    __attribute__((unused)),
        char    **argv
    ) {
    char                *progname;
    I2LogImmediateAttr  ia;
    I2ErrHandle         eh;
    int                 status = 0;
    char   *in[] =
                    {"abc",
                    "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
                    NULL,
                    NULL};
    char                *out[] = 
                    {"A9993E364706816ABA3E25717850C26C9CD0D89D",
                    "84983E441C3BD26EBAAE4AA1F95129E5E54670F1",
                    "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F",
                    NULL};
    char            *ststr;
    uint8_t         digest[I2SHA1_DIGEST_SIZE];
    uint8_t         result[I2SHA1_DIGEST_SIZE];
    char            hd[(2*I2SHA1_DIGEST_SIZE)+1];

    ia.line_info = (I2NAME | I2MSG);
#ifndef        NDEBUG
    ia.line_info |= (I2LINE | I2FILE);
#endif
    ia.fp = stderr;

    progname = (progname = strrchr(argv[0], '/')) ? progname+1 : *argv;

    /*
     * Start an error logging session for reporing errors to the
     * standard error
     */
    eh = I2ErrOpen(progname, I2ErrLogImmediate, &ia, NULL, NULL);
    if(! eh) {
        fprintf(stderr, "%s : Couldn't init error module\n", progname);
        exit(1);
    }


    /*
     * First test vector
     */
    I2Sha1((uint8_t*)in[0],strlen(in[0]),result);

    I2HexDecode(out[0],digest,I2SHA1_DIGEST_SIZE);
    if(memcmp(digest,result,I2SHA1_DIGEST_SIZE)){
        status = 1;
        ststr = "FAILED";
    }
    else{
        ststr = "SUCCESS";
    }

    hd[2*I2SHA1_DIGEST_SIZE] = '\0';
    I2HexEncode(hd,result,I2SHA1_DIGEST_SIZE);

    fprintf(stdout,"%s: sha1(\"%s\") = %s\n",ststr,in[0],hd);

    /*
     * Second test vector
     */
    I2Sha1((uint8_t*)in[1],strlen(in[1]),result);

    I2HexDecode(out[1],digest,I2SHA1_DIGEST_SIZE);
    if(memcmp(digest,result,I2SHA1_DIGEST_SIZE)){
        status = 1;
        ststr = "FAILED";
    }
    else{
        ststr = "SUCCESS";
    }

    hd[2*I2SHA1_DIGEST_SIZE] = '\0';
    I2HexEncode(hd,result,I2SHA1_DIGEST_SIZE);

    fprintf(stdout,"%s: sha1(\"%s\") = %s\n",ststr,in[1],hd);

    /*
     * Third test vector
     * (1,000,000 "a"'s)
     */
    in[2] = malloc(1000000*sizeof(char));
    if(!in[2]){
        perror("Unable to malloc 1000000 char's for 3rd test vector");
        exit(1);
    }
    memset(in[2],'a',1000000);
    I2Sha1((uint8_t*)in[2],1000000,result);

    I2HexDecode(out[2],digest,I2SHA1_DIGEST_SIZE);
    if(memcmp(digest,result,I2SHA1_DIGEST_SIZE)){
        status = 1;
        ststr = "FAILED";
    }
    else{
        ststr = "SUCCESS";
    }

    hd[2*I2SHA1_DIGEST_SIZE] = '\0';
    I2HexEncode(hd,result,I2SHA1_DIGEST_SIZE);

    fprintf(stdout,"%s: sha1(\"%s\") = %s\n",ststr,"1000000 a\'s",hd);

    exit(status);
}