File: canlocktest.c

package info (click to toggle)
canlock 2b-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 160 kB
  • ctags: 77
  • sloc: ansic: 716; makefile: 112; sh: 2
file content (110 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download | duplicates (9)
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
/* 
 * canlocktest.c - just checking.
 * 
 * This program doesn't really do anything but lightly exercise all the
 * library functions, so you can make sure it all compiled correctly. 
 * Everything's kept simple so that you can also see how they would be
 * called in a real application.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "canlock.h"

#define BUFFSIZE 512

void checker(char *key, char *lock);

void
checker(char *key, char *lock)
{
    char
        *rawkey,
        *rawlock;
    char
        keytype[BUFFSIZE],
        locktype[BUFFSIZE];

    printf("L %s K %s ", lock, key);

    rawkey = lock_strip_alpha(key, keytype);
    rawlock = lock_strip_alpha(lock, locktype);

    if (!strcmp(keytype, locktype)) {
        if (!strcmp(keytype, "sha1")) {
            if (!sha_verify(rawkey, rawlock))
                printf("sha1 OK\n");
            else
                printf("sha1 no\n");
        }
        else
            printf("unknown\n");
    }
    else
        printf("Mismatch %s %s\n", keytype, locktype);
}


int
main(void)
{
    char
        cankey[256],
        canlock[256],
        *lkey,
        *llock;
    unsigned char
        secret[] = "fluffy",
        message[] = "<lkr905851929.22670@meow.invalid>";

    printf("Secret %s\n", secret);
    printf("Message %s\n", message);

    llock = sha_lock(secret, strlen((char *) secret),
                     message, strlen((char *) message));
    lkey = sha_key(secret, strlen((char *) secret),
                   message, strlen((char *)message));

    printf("%s%s %s\n", "SHA Expect Lock/Key:\n",
           "L sha1:ScU1gyAi9bd/aFEOyzg4m99lwXs=",
           "K sha1:C1Me/4n0l/V778Ih3J2UnhAoHrA=");

    checker(lkey, llock);
    free((void *) llock);
    free((void *) lkey);
    printf("---\n");

/*********/

    printf("Testing against usefor cancel lock draft 01 samples...\n");

    sprintf(canlock, "%s", "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=");
    sprintf(cankey, "%s", "sha1:aaaBBBcccDDDeeeFFF");
    checker(cankey, canlock);
    printf("---above should have been OK---\n");

/*********/

    sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
    sprintf(cankey, "%s", "sha1:chW8hNeDx3iNUsGBU6/ezDk88P4=");
    checker(cankey, canlock);

    sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
    sprintf(cankey, "%s", "sha1:4srkWaRIzvK51ArAP:Hc");
    checker(cankey, canlock);
    printf("---above should have been OK, no---\n");

/*********/

    sprintf(canlock, "%s", "sha1:JyEBL4w9/abCBuzCxMIE/E73GM4=");
    sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
    checker(cankey, canlock);

    sprintf(canlock, "%s", "sha1:2Bmg+zWaY1noRiCdy8k3IapwSDU=");
    sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
    checker(cankey, canlock);
    printf("---above should have been OK, no---\n");
    
    return 0;
}