summaryrefslogtreecommitdiff
path: root/inc/CryptX_KeyDerivation.xs.inc
blob: 9e88d16068f49ea9d4b7664a9e51a34579b5a3a9 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
MODULE = CryptX         PACKAGE = Crypt::KeyDerivation

SV *
_pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
    CODE:
    {
        /*
        int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
                        const unsigned char *salt,
                        int iteration_count,  int hash_idx,
                        unsigned char *out,   unsigned long *outlen)
        */
        int rv, id;
        unsigned char *output;
        unsigned char *password_ptr=NULL;
        STRLEN password_len=0;
        unsigned char *salt_ptr=NULL;
        STRLEN salt_len=0;

        id = find_hash(hash_name);
        if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);

        password_ptr = (unsigned char *)SvPVbyte(password, password_len);
        salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
        if (salt_len < 8) croak("FATAL: salt_len has to be 8");

        RETVAL = NEWSV(0, output_len);
        SvPOK_only(RETVAL);
        SvCUR_set(RETVAL, output_len);
        output = (unsigned char *)SvPVX(RETVAL);

        rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
        if (rv != CRYPT_OK) {
          SvREFCNT_dec(RETVAL);
          croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
        }
        SvCUR_set(RETVAL, output_len);
    }
    OUTPUT:
        RETVAL

SV *
_pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
    CODE:
    {
        /*
        int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
                        const unsigned char *salt,     unsigned long salt_len,
                        int iteration_count,           int hash_idx,
                        unsigned char *out,            unsigned long *outlen)
        */
        int rv, id;
        unsigned char *output;
        unsigned char *password_ptr=NULL;
        STRLEN password_len=0;
        unsigned char *salt_ptr=NULL;
        STRLEN salt_len=0;

        id = find_hash(hash_name);
        if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);

        password_ptr = (unsigned char *)SvPVbyte(password, password_len);
        salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);

        RETVAL = NEWSV(0, output_len);
        SvPOK_only(RETVAL);
        SvCUR_set(RETVAL, output_len);
        output = (unsigned char *)SvPVX(RETVAL);

        rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len);
        if (rv != CRYPT_OK) {
          SvREFCNT_dec(RETVAL);
          croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
        }
        SvCUR_set(RETVAL, output_len);
    }
    OUTPUT:
        RETVAL

SV *
_hkdf_extract(char * hash_name, SV * salt, SV * in)
    CODE:
    {
        /*
        int hkdf_extract(int hash_idx, const unsigned char *salt, unsigned long  saltlen,
                                       const unsigned char *in,   unsigned long  inlen,
                                       unsigned char *out,  unsigned long *outlen)
        */
        int rv, id;
        unsigned char output[MAXBLOCKSIZE];
        unsigned long output_len;
        unsigned char *in_ptr=NULL;
        STRLEN in_len=0;
        unsigned char *salt_ptr=NULL;
        STRLEN salt_len=0;

        id = find_hash(hash_name);
        if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);

        in_ptr = (unsigned char *)SvPVbyte(in, in_len);
        salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);

        output_len = sizeof(output);
        rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len);
        if (rv != CRYPT_OK) croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv));

        RETVAL = newSVpvn((char *)output, output_len);
    }
    OUTPUT:
        RETVAL

SV *
_hkdf_expand(char * hash_name, SV * info, SV * in, unsigned long output_len)
    CODE:
    {
        /*
        int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen,
                                      const unsigned char *in,   unsigned long inlen,
                                      unsigned char *out,  unsigned long outlen)
        */
        int rv, id;
        unsigned char *output;
        unsigned char *in_ptr=NULL;
        STRLEN in_len=0;
        unsigned char *info_ptr=NULL;
        STRLEN info_len=0;

        id = find_hash(hash_name);
        if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);

        in_ptr = (unsigned char *)SvPVbyte(in, in_len);
        info_ptr = (unsigned char *)SvPVbyte(info, info_len);

        RETVAL = NEWSV(0, output_len);
        SvPOK_only(RETVAL);
        SvCUR_set(RETVAL, output_len);
        output = (unsigned char *)SvPVX(RETVAL);

        rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
        if (rv != CRYPT_OK) {
          SvREFCNT_dec(RETVAL);
          croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
        }
        SvCUR_set(RETVAL, output_len);
    }
    OUTPUT:
        RETVAL

SV *
_hkdf(char * hash_name, SV * salt, SV * info, SV * in, unsigned long output_len)
    CODE:
    {
        /*
        int hkdf(int hash_idx, const unsigned char *salt, unsigned long saltlen,
                               const unsigned char *info, unsigned long infolen,
                               const unsigned char *in,   unsigned long inlen,
                               unsigned char *out,  unsigned long outlen)
        */
        int rv, id;
        unsigned char *output;
        unsigned char *in_ptr=NULL;
        STRLEN in_len=0;
        unsigned char *info_ptr=NULL;
        STRLEN info_len=0;
        unsigned char *salt_ptr=NULL;
        STRLEN salt_len=0;

        id = find_hash(hash_name);
        if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);

        in_ptr   = (unsigned char *)SvPVbyte(in, in_len);
        info_ptr = (unsigned char *)SvPVbyte(info, info_len);
        salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);

        RETVAL = NEWSV(0, output_len);
        SvPOK_only(RETVAL);
        SvCUR_set(RETVAL, output_len);
        output = (unsigned char *)SvPVX(RETVAL);

        rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
        if (rv != CRYPT_OK) {
          SvREFCNT_dec(RETVAL);
          croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
        }
        SvCUR_set(RETVAL, output_len);
    }
    OUTPUT:
        RETVAL