-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes_cbindings.cpp.md
130 lines (114 loc) · 3.43 KB
/
aes_cbindings.cpp.md
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
declared in [Aes](aes.hpp.md)
```c++
#include "lxr/lxr-cbindings.hpp"
extern "C" EXPORT
CAesEncrypt* mk_AesEncrypt(CKey256 * k, CKey128 * iv)
{ auto r = new lxr::AesEncrypt(*((lxr::Key256*)k->ptr), *((lxr::Key128*)iv->ptr));
CAesEncrypt *cl = new CAesEncrypt; cl->ptr = r;
cl->lastpos = 0;
return cl;
}
extern "C" EXPORT
unsigned int sz_AesEncrypt()
{ return lxr::Aes::datasz;
}
extern "C" EXPORT
void release_AesEncrypt(CAesEncrypt *cl)
{
if (cl) {
if (cl->ptr) {
free(cl->ptr);
}
delete cl;
}
}
extern "C" EXPORT
int proc_AesEncrypt(CAesEncrypt *cl, unsigned int inlen, unsigned char const *inbuf)
{ if (inlen > lxr::Aes::datasz) return (-1);
sizebounded<unsigned char, lxr::Aes::datasz> buf;
std::memcpy((void*)buf.ptr(), inbuf, std::min(inlen, lxr::Aes::datasz));
int len = ((lxr::AesEncrypt*)(cl->ptr))->process(inlen, buf);
std::memcpy(cl->buf+cl->lastpos, buf.ptr(), len);
cl->lastpos += len;
return len;
}
extern "C" EXPORT
int fin_AesEncrypt(CAesEncrypt *cl)
{ sizebounded<unsigned char, lxr::Aes::datasz> buf;
int len = ((lxr::AesEncrypt*)(cl->ptr))->finish(0, buf);
std::memcpy(cl->buf+cl->lastpos, buf.ptr(), len);
cl->lastpos += len;
return len;
}
extern "C" EXPORT
unsigned int len_AesEncrypt(CAesEncrypt *cl)
{ return cl->lastpos;
}
ATTRIBUTE_NO_SANITIZE_ADDRESS
extern "C" EXPORT
unsigned int copy_AesEncrypt(CAesEncrypt *cl, unsigned int outlen, unsigned char *outbuf)
{ unsigned int copied = std::min(outlen, cl->lastpos);
memcpy(outbuf, cl->buf, copied);
if (copied < cl->lastpos) {
// copied 'copied' bytes out; move from 'copied' to lastpos to front
memcpy(cl->buf, cl->buf + copied, cl->lastpos - copied);
}
cl->lastpos -= copied;
return copied;
}
extern "C" EXPORT
CAesDecrypt* mk_AesDecrypt(CKey256 * k, CKey128 * iv)
{ auto r = new lxr::AesDecrypt(*((lxr::Key256*)k->ptr), *((lxr::Key128*)iv->ptr));
CAesDecrypt *cl = new CAesDecrypt; cl->ptr = r;
cl->lastpos = 0;
return cl;
}
extern "C" EXPORT
unsigned int sz_AesDecrypt()
{ return lxr::Aes::datasz;
}
extern "C" EXPORT
void release_AesDecrypt(CAesDecrypt *cl)
{
if (cl) {
if (cl->ptr) {
free(cl->ptr);
}
delete cl;
}
}
extern "C" EXPORT
int proc_AesDecrypt(CAesDecrypt *cl, unsigned int inlen, unsigned char const *inbuf)
{ if (inlen > lxr::Aes::datasz) return (-1);
sizebounded<unsigned char, lxr::Aes::datasz> buf;
std::memcpy((void*)buf.ptr(), inbuf, std::min(inlen, lxr::Aes::datasz));
int len = ((lxr::AesDecrypt*)(cl->ptr))->process(inlen, buf);
std::memcpy(cl->buf+cl->lastpos, buf.ptr(), len);
cl->lastpos += len;
return len;
}
extern "C" EXPORT
int fin_AesDecrypt(CAesDecrypt *cl)
{ sizebounded<unsigned char, lxr::Aes::datasz> buf;
int len = ((lxr::AesDecrypt*)(cl->ptr))->finish(0, buf);
std::memcpy(cl->buf+cl->lastpos, buf.ptr(), len);
cl->lastpos += len;
return len;
}
extern "C" EXPORT
unsigned int len_AesDecrypt(CAesDecrypt *cl)
{ return cl->lastpos;
}
ATTRIBUTE_NO_SANITIZE_ADDRESS
extern "C" EXPORT
unsigned int copy_AesDecrypt(CAesDecrypt *cl, unsigned int outlen, unsigned char *outbuf)
{ unsigned int copied = std::min(outlen, cl->lastpos);
memcpy(outbuf, cl->buf, copied);
if (copied < cl->lastpos) {
// copied 'copied' bytes out; move from 'copied' to lastpos to front
memcpy(cl->buf, cl->buf + copied, cl->lastpos - copied);
}
cl->lastpos -= copied;
return copied;
}
```