MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / mxhwenp / mxhw_crypto_userio.c
blobf6780b46ec0279db6bc70a8cbe11da3a3c45eb17
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
7 #include <mxhw_crypto_io.h>
9 #define MXCIPHER_DEV_NODE "/dev/mxcrypto" /* device node */
11 int
12 mxcrypto_uio_register(u_int algo, u_int mode, u_char *keys, u_int bits, int type)
14 CIPHER info;
15 int fd, i;
17 DBG("mxhw_userio: mxcrypto_uio_register %d %d %d\n", 11,algo,mode);
19 /* open a new context */
20 if ( (fd = open(MXCIPHER_DEV_NODE, O_RDWR, 0)) < 0 )
21 return fd;
23 /* register this context with parameters */
24 info.type = (type)? 1:0;
25 info.algo = algo;
26 info.mode = mode;
27 info.bits = bits;
28 memcpy(info.keys, keys, MAX_MXCIPHER_LEN_KEYS);
29 if ( (i = ioctl(fd, IOCTLSET_MXCIPHER_INFO, &info)) < 0 )
31 close(fd);
32 fd = i;
34 DBG("mxhw_userio: mxcrypto_uio_register %d\n", 99);
35 return fd;
38 __inline__ int
39 mxcrypto_uio_perform(int ctrl, const u_char *input, u_char *output, u_long length,
40 u_char *ivec, u_int ilen)
42 int n=0;
43 u_char data[MAX_CIPHER_PACKET], *ibuf, *obuf;
45 DBG("mxhw_userio: mxcrypto_uio_perform %d %ld %d\n", 11, length, ilen);
46 if (ctrl<=0)
47 return -1;
49 if (ivec!=NULL && ilen>0)
51 if ((length+ilen)>MAX_CIPHER_PACKET)
52 return -2;
53 obuf = ibuf = data;
54 memcpy(ibuf, input, length);
55 memcpy(ibuf+length, ivec, ilen);
56 length += ilen;
58 else
60 ibuf = (u_char*) input;
61 obuf = output;
63 DBG("mxhw_userio: mxcrypto_uio_perform %d %ld %d\n", 22, length, ilen);
64 /* send request to the engine and wait for response */
65 if ((n=write(ctrl, ibuf, length)) < 0)
67 DBG("error: write %d\n", n);
68 return n;
70 DBG("mxhw_userio: mxcrypto_uio_perform %d %ld %d\n", 33, length, ilen);
71 if ((n=read(ctrl, obuf, length)) < 0)
73 DBG("error: read %d\n", n);
74 return n;
76 DBG("mxhw_userio: mxcrypto_uio_perform %d %ld %d\n", 44, length, ilen);
77 if (ivec!=NULL && ilen>0)
79 length -= ilen;
80 memcpy(output, obuf, length);
81 memcpy(ivec, obuf+length, ilen);
83 DBG("mxhw_userio: mxcrypto_uio_perform %d\n", 99);
84 return 0;
87 int
88 mxcrypto_uio_close(int fd)
90 int size=0;
92 if ( ioctl(fd, 99, &size) < 0 )
93 return -1;
94 close(fd);
95 return size;
98 #ifdef OVERFLOW_TEST
99 int
100 mxcrypto_uio_write(int ctxId, const u_char *input, u_long len, u_char *ivec, int ilen)
102 int n=0;
104 /* send request to the engine and wait for response */
105 if ((n=write(ctxId, input, len+ilen)) < 0)
106 DBG("error: write/read %d\n", n);
107 return n;
111 mxcrypto_uio_read(int ctxId, u_char *output, u_long len, u_char *ivec, int ilen)
113 int n=0;
115 /* send request to the engine and wait for response */
116 if ((n=read(ctxId, output, len+ilen)) < 0)
117 DBG("error: write/read %d\n", n);
118 return n;
122 mxcrypto_uio_queue(int ctxId)
124 int n=0;
126 if (ioctl(ctxId, 100, &n)<0)
127 return -1;
128 return n;
130 #endif