File: UnixCrypt_XS.xs

package info (click to toggle)
libcrypt-unixcrypt-xs-perl 0.08-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 164 kB
  • ctags: 229
  • sloc: ansic: 652; makefile: 54; perl: 15
file content (177 lines) | stat: -rw-r--r-- 4,287 bytes parent folder | download
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#include <./fcrypt/fcrypt.h>

static void
sv_to_octets(U8 **octets_p, STRLEN *len_p, bool *must_free_p, SV *sv)
{
  U8 *in_str = SvPV(sv, *len_p);
  bool is_utf8 = !!SvUTF8(sv);
  *octets_p = bytes_from_utf8(in_str, len_p, &is_utf8);
  if(is_utf8)
    croak("input must contain only octets");
  *must_free_p = *octets_p != in_str;
}

static void
sv_to_cblock(des_cblock block, SV *in_block)
{
  U8 *in_octets;
  STRLEN in_len;
  bool must_free;
  sv_to_octets(&in_octets, &in_len, &must_free, in_block);
  if(in_len != 8)
    croak("data block must be eight octets long");
  memcpy(block, in_octets, 8);
  if(must_free)
    Safefree(in_octets);
}

MODULE = Crypt::UnixCrypt_XS		PACKAGE = Crypt::UnixCrypt_XS		

char *
crypt( password, salt )
  SV *password
  SV *salt
  CODE:
    STRLEN password_len, salt_len;
    U8 *password_octets, *salt_octets;
    bool password_tofree, salt_tofree;
    char outbuf[21];
    sv_to_octets(&password_octets, &password_len, &password_tofree, password);
    sv_to_octets(&salt_octets, &salt_len, &salt_tofree, salt);
    des_fcrypt((char *)password_octets, password_len,
	(char *)salt_octets, salt_len, outbuf);
    if(password_tofree)
      Safefree(password_octets);
    if(salt_tofree)
      Safefree(salt_octets);
    RETVAL = outbuf;
  OUTPUT:
    RETVAL

SV *
crypt_rounds( password, nrounds, saltnum, in_block )
  SV *password
  unsigned long nrounds
  unsigned long saltnum
  SV *in_block
  CODE:
    STRLEN password_len;
    U8 *password_octets;
    bool password_tofree;
    des_cblock key, block;
    sv_to_octets(&password_octets, &password_len, &password_tofree, password);
    sv_to_cblock(block, in_block);
    trad_password_to_key(key, (char *)password_octets, password_len);
    if(password_tofree)
      Safefree(password_octets);
    crypt_rounds(key, nrounds, saltnum, block);
    RETVAL = newSVpvn(block, 8);
  OUTPUT:
    RETVAL

SV *
fold_password( password )
  SV *password
  CODE:
    STRLEN password_len;
    U8 *password_octets;
    bool password_tofree;
    des_cblock key;
    int i;
    sv_to_octets(&password_octets, &password_len, &password_tofree, password);
    ext_password_to_key(key, (char *)password_octets, password_len);
    if(password_tofree)
      Safefree(password_octets);
    for(i=0; i<8; i++)
      key[i] = (key[i] & 0xfe) >> 1;
    RETVAL = newSVpvn(key, 8);
  OUTPUT:
    RETVAL

SV *
base64_to_block( base64 )
  SV *base64
  CODE:
    STRLEN base64_len;
    U8 *base64_octets;
    bool base64_tofree;
    des_cblock block;
    sv_to_octets(&base64_octets, &base64_len, &base64_tofree, base64);
    if(base64_len != 11)
      croak("data block in base 64 must be eleven characters long");
    base64_to_block(block, (char *)base64_octets);
    if(base64_tofree)
      Safefree(base64_octets);
    RETVAL = newSVpvn(block, 8);
  OUTPUT:
    RETVAL

char *
block_to_base64( in_block )
  SV *in_block
  CODE:
    des_cblock block;
    char base64[12];
    sv_to_cblock(block, in_block);
    block_to_base64(block, base64);
    RETVAL = base64;
  OUTPUT:
    RETVAL

unsigned long
base64_to_int24( base64 )
  SV *base64
  CODE:
    STRLEN base64_len;
    U8 *base64_octets;
    bool base64_tofree;
    sv_to_octets(&base64_octets, &base64_len, &base64_tofree, base64);
    if(base64_len != 4)
      croak("24-bit integer in base 64 must be four characters long");
    RETVAL = base64_to_int24((char *)base64_octets);
    if(base64_tofree)
      Safefree(base64_octets);
  OUTPUT:
    RETVAL

char *
int24_to_base64( val )
  unsigned long val;
  CODE:
    char base64[5];
    int24_to_base64(val, base64);
    RETVAL = base64;
  OUTPUT:
    RETVAL

unsigned long
base64_to_int12( base64 )
  SV *base64
  CODE:
    STRLEN base64_len;
    U8 *base64_octets;
    bool base64_tofree;
    sv_to_octets(&base64_octets, &base64_len, &base64_tofree, base64);
    if(base64_len != 2)
      croak("12-bit integer in base 64 must be two characters long");
    RETVAL = base64_to_int12((char *)base64_octets);
    if(base64_tofree)
      Safefree(base64_octets);
  OUTPUT:
    RETVAL

char *
int12_to_base64( val )
  unsigned long val;
  CODE:
    char base64[3];
    int12_to_base64(val, base64);
    RETVAL = base64;
  OUTPUT:
    RETVAL