Skip to content

Commit 9589658

Browse files
committed
fix: potential alignment issues on take
1 parent 5366e0c commit 9589658

File tree

4 files changed

+189
-17
lines changed

4 files changed

+189
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
- "docs/**"
1212

1313
env:
14-
ROCKSPEC_VERSION: 0.0.2
14+
ROCKSPEC_VERSION: 0.0.3
1515
DEV_ROCKSPEC: lua-cryptorandom-dev-1.rockspec
1616

1717
jobs:

README.md

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,11 @@
1414
>
1515
> ```lua-cryptorandom``` is implemented in C, and also compiles as C++.
1616
17-
## Use cases
18-
19-
Many security operations rely on high-quality randomization services to avoid reproducibility and remain resistant to reverse engineering:
20-
21-
* randomized password generation;
22-
* nonces (numbers used once) generation;
23-
* initialization vectors;
24-
* salts in passwords before hashing;
25-
* tokenization (*token generation*) to represent sensitive data;
26-
* secure random sampling in statistical analysis.
27-
2817
## Table of Contents
2918

3019
* [Installation](#installation)
20+
* [Usage](#usage)
21+
* [Use cases](#use-cases)
3122
* [Methods](#methods)
3223
* [bytes](#bytes)
3324
* [integer](#integer)
@@ -66,6 +57,69 @@ Assuming that [LuaRocks](https://luarocks.org) is properly installed and configu
6657
luarocks install lua-cryptorandom
6758
```
6859

60+
## Usage
61+
62+
* How to generate random bytes
63+
64+
```lua
65+
local random = require("lua-cryptorandom")
66+
67+
-- number of bytes to generate
68+
local n = 10
69+
70+
local bytes, err = random.bytes(n)
71+
72+
if (bytes == nil) then
73+
print("error code: ", err)
74+
else
75+
assert(n == #bytes, "Unexpected number of bytes")
76+
77+
-- print each byte
78+
for i, b in ipairs(bytes) do
79+
print(i, ("0x%02X"):format(b))
80+
end
81+
end
82+
```
83+
84+
* How to generate a random integer
85+
86+
```lua
87+
local random = require("lua-cryptorandom")
88+
89+
local take, err = random.take()
90+
91+
if (take == nil) then
92+
print("error code: ", err)
93+
else
94+
print("take: ", take)
95+
end
96+
```
97+
98+
* How to generate a random float number
99+
100+
```lua
101+
local random = require("lua-cryptorandom")
102+
103+
local number, err = random.number()
104+
105+
if (number == nil) then
106+
print("error code: ", err)
107+
else
108+
print("number: ", number)
109+
end
110+
```
111+
112+
## Use cases
113+
114+
Many security operations rely on high-quality randomization services to avoid reproducibility and remain resistant to reverse engineering:
115+
116+
* randomized password generation;
117+
* nonces (numbers used once) generation;
118+
* initialization vectors;
119+
* salts in passwords before hashing;
120+
* tokenization (*token generation*) to represent sensitive data;
121+
* secure random sampling in statistical analysis.
122+
69123
## Methods
70124

71125
> [!IMPORTANT]
@@ -176,10 +230,13 @@ luarocks install lua-cryptorandom
176230
>
177231
> This section mostly applies to users running a customized build of Lua.
178232

179-
* The error code (second return value) on each method might deliver a value different than the one returned by the underlying library. This condition might happen when the Lua type ```lua_Integer``` is shorter than an ```unsigned long``` in size. Even though it can be achieved on personalized builds of Lua (e.g.: Lua compiled as ANSI C on some platforms), the usual build of Lua should be safe for most users and platforms.
233+
* The error code (second return value) on each method might deliver a value different than the one returned by the underlying library. This condition might happen when the Lua type ```lua_Integer``` is shorter than an ```unsigned long``` in size. Even though it can be achieved on personalized builds of Lua (e.g.: Lua compiled as C89 on some platforms), the usual build of Lua should be safe for most users and platforms.
180234

181235
## Change log
182236

237+
* v0.0.3:
238+
* Using unions on [take](#take) to avoid alignment issues;
239+
* Added the [Usage](#usage) section on README.
183240
* v0.0.2: Prevent the generation of ```NaN``` and positive/negative infinity values in the function [number](#number).
184241
* v0.0.1: Initial release.
185242

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package = "lua-cryptorandom"
2+
version = "0.0.3-1"
3+
4+
source = {
5+
url = "git+https://github.com/luau-project/lua-cryptorandom",
6+
tag = "v0.0.3"
7+
}
8+
9+
description = {
10+
homepage = "https://github.com/luau-project/lua-cryptorandom",
11+
summary = [[Generate cryptographically secure pseudo random numbers for Lua]],
12+
detailed = [=[
13+
lua-cryptorandom is a lightweight, native library for Lua aimed to generate cryptographically secure pseudo random numbers, using trusted sources of randomness provided by the operating system.
14+
15+
Visit the repository for more information.]=],
16+
license = "MIT"
17+
}
18+
19+
dependencies = {
20+
"lua >= 5.1"
21+
}
22+
23+
local function external_dependencies_plat()
24+
return {
25+
["CRYPTO"] = {
26+
header = "openssl/rand.h"
27+
}
28+
}
29+
end
30+
31+
external_dependencies = {
32+
platforms = {
33+
linux = external_dependencies_plat(),
34+
freebsd = external_dependencies_plat(),
35+
openbsd = external_dependencies_plat(),
36+
netbsd = external_dependencies_plat(),
37+
dragonfly = external_dependencies_plat()
38+
}
39+
}
40+
41+
local function build_plat(plat)
42+
if (plat == "macosx" or plat == "macos") then
43+
return {
44+
type = "make",
45+
makefile = "Makefile.macosx",
46+
build_variables = {
47+
CFLAGS = "$(CFLAGS)",
48+
LIBFLAG = "$(LIBFLAG)",
49+
CFLAGS_EXTRA = "-DLUA_CRYPTORANDOM_BUILD_SHARED -DLUA_CRYPTORANDOM_USE_APPLE",
50+
LIBFLAG_EXTRA = "-framework Security",
51+
LUA_INCDIR = "$(LUA_INCDIR)",
52+
OBJ_EXTENSION = "$(OBJ_EXTENSION)",
53+
LIB_EXTENSION = "$(LIB_EXTENSION)"
54+
},
55+
install_variables = {
56+
INSTALL_PREFIX = "$(PREFIX)",
57+
INSTALL_LIBDIR = "$(LIBDIR)",
58+
LUA_VERSION = "$(LUA_VERSION)",
59+
LIB_EXTENSION = "$(LIB_EXTENSION)"
60+
}
61+
}
62+
elseif (plat == "windows" or plat == "cygwin") then
63+
return {
64+
type = "builtin",
65+
modules = {
66+
["lua-cryptorandom"] = {
67+
sources = { "src/lua-cryptorandom.c" },
68+
libraries = { "bcrypt" },
69+
defines = { "LUA_CRYPTORANDOM_BUILD_SHARED", "LUA_CRYPTORANDOM_USE_WIN32" },
70+
incdirs = { "src" },
71+
libdirs = { }
72+
}
73+
}
74+
}
75+
elseif (plat == "linux" or plat == "bsd") then
76+
return {
77+
type = "builtin",
78+
modules = {
79+
["lua-cryptorandom"] = {
80+
sources = { "src/lua-cryptorandom.c" },
81+
libraries = { "crypto" },
82+
defines = { "LUA_CRYPTORANDOM_BUILD_SHARED", "LUA_CRYPTORANDOM_USE_OPENSSL" },
83+
incdirs = { "src", "$(CRYPTO_INCDIR)" },
84+
libdirs = { "$(CRYPTO_LIBDIR)" }
85+
}
86+
}
87+
}
88+
else
89+
error("Unknown platform", 2)
90+
end
91+
end
92+
93+
build = {
94+
platforms = {
95+
macosx = build_plat("macosx"),
96+
macos = build_plat("macos"),
97+
windows = build_plat("windows"),
98+
cygwin = build_plat("cygwin"),
99+
linux = build_plat("linux"),
100+
freebsd = build_plat("bsd"),
101+
openbsd = build_plat("bsd"),
102+
netbsd = build_plat("bsd"),
103+
dragonfly = build_plat("bsd")
104+
}
105+
}

src/lua-cryptorandom.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@
3232
#define isinf(x) ((x)==(HUGE_VAL)?(1):((x)==(-HUGE_VAL)?(-1):(0)))
3333
#endif
3434

35+
/*
36+
** helper union to the function
37+
** lua_cryptorandom_take
38+
*/
39+
typedef union tagLuaCryptoRandomInt
40+
{
41+
int value;
42+
unsigned char buffer[sizeof(int)];
43+
44+
} LuaCryptoRandomInt;
45+
3546
/*
3647
** helper union to the function
3748
** lua_cryptorandom_integer
@@ -124,18 +135,17 @@ static int lua_cryptorandom_bytes(lua_State *L)
124135

125136
static int lua_cryptorandom_take(lua_State *L)
126137
{
127-
unsigned char buffer[sizeof(int)];
138+
LuaCryptoRandomInt rtake;
128139

129140
unsigned long err;
130-
if (lua_cryptorandom_bytes_impl(L, (unsigned char *)buffer, sizeof(int), &err) == 0)
141+
if (lua_cryptorandom_bytes_impl(L, (unsigned char *)(rtake.buffer), sizeof(int), &err) == 0)
131142
{
132143
lua_pushnil(L);
133144
lua_pushinteger(L, (lua_Integer)err);
134145
}
135146
else
136147
{
137-
int *take_ptr = (int *)buffer;
138-
lua_pushinteger(L, *take_ptr);
148+
lua_pushinteger(L, rtake.value);
139149
lua_pushnil(L);
140150
}
141151

0 commit comments

Comments
 (0)