Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building pgsodium on windows #72

Merged
merged 13 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@
*.su
*~

# Visual Studio Build files
build/.vs/
*.vcxproj.user
build/x64/

pgsodium_encrypted_root.key
.ipynb_checkpoints/
25 changes: 25 additions & 0 deletions build/pgsodium_vc16.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.33214.272
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pgsodium_vc16", "pgsodium_vc16.vcxproj", "{481FD624-9F28-466C-818B-8D2E3F40800B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{481FD624-9F28-466C-818B-8D2E3F40800B}.Debug|x64.ActiveCfg = Debug|x64
{481FD624-9F28-466C-818B-8D2E3F40800B}.Debug|x64.Build.0 = Debug|x64
{481FD624-9F28-466C-818B-8D2E3F40800B}.Release|x64.ActiveCfg = Release|x64
{481FD624-9F28-466C-818B-8D2E3F40800B}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6BEA41E9-128A-4303-A61C-0164FBA0EB32}
EndGlobalSection
EndGlobal
127 changes: 127 additions & 0 deletions build/pgsodium_vc16.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{481fd624-9f28-466c-818b-8d2e3f40800b}</ProjectGuid>
<RootNamespace>pgsodium_vc16</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<GenerateManifest>false</GenerateManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<GenerateManifest>false</GenerateManifest>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<ExceptionHandling>false</ExceptionHandling>
<CompileAs>CompileAsC</CompileAs>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>postgres.lib;libsodium.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ProjectReference>
<LinkLibraryDependencies>false</LinkLibraryDependencies>
</ProjectReference>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<ExceptionHandling>false</ExceptionHandling>
<CompileAs>CompileAsC</CompileAs>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>postgres.lib;libsodium.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ProjectReference>
<LinkLibraryDependencies>false</LinkLibraryDependencies>
</ProjectReference>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\src\crypto_aead_det_xchacha20.h" />
<ClInclude Include="..\src\pgsodium.h" />
<ClInclude Include="..\src\signcrypt_tbsbr.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\aead.c" />
<ClCompile Include="..\src\auth.c" />
<ClCompile Include="..\src\box.c" />
<ClCompile Include="..\src\crypto_aead_det_xchacha20.c" />
<ClCompile Include="..\src\derive.c" />
<ClCompile Include="..\src\hash.c" />
<ClCompile Include="..\src\helpers.c" />
<ClCompile Include="..\src\hmac.c" />
<ClCompile Include="..\src\kdf.c" />
<ClCompile Include="..\src\kx.c" />
<ClCompile Include="..\src\pgsodium.c" />
<ClCompile Include="..\src\pwhash.c" />
<ClCompile Include="..\src\random.c" />
<ClCompile Include="..\src\secretbox.c" />
<ClCompile Include="..\src\secretstream.c" />
<ClCompile Include="..\src\sha.c" />
<ClCompile Include="..\src\sign.c" />
<ClCompile Include="..\src\signcrypt.c" />
<ClCompile Include="..\src\signcrypt_tbsbr.c" />
<ClCompile Include="..\src\stream.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
90 changes: 90 additions & 0 deletions build/pgsodium_vc16.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\crypto_aead_det_xchacha20.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\pgsodium.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\signcrypt_tbsbr.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\aead.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\auth.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\box.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\crypto_aead_det_xchacha20.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\derive.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\hash.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\helpers.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\hmac.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\kdf.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\kx.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\pgsodium.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\pwhash.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\random.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\secretbox.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\secretstream.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sha.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\sign.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\signcrypt.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\signcrypt_tbsbr.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\stream.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions build/windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#### Building on Windows
---------

- Download [libsodium](https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable-msvc.zip) >= 1.018 for msvc and unzip
- Download and run the [postgresql installer](https://www.postgresql.org/download/windows/)
- Open the `build/pgsodium_vcxx.sln` solution file in the appropriate version of Visual Studio
- Right click on the project and select `Properties` to bring up the Property Page.
- For all configurations, under `C/C++ -> General -> Additional Include Directories` add the following paths:

```
%PROGRAMFILES%\PostgreSQL\<version>\include\server\port\win32_msvc
%PROGRAMFILES%\PostgreSQL\<version>\include\server\port\win32
%PROGRAMFILES%\PostgreSQL\<version>\include\server
%PROGRAMFILES%\PostgreSQL\<version>\include
{unziped location}\libsodium\include
```

- For all configurations, under `Linker -> General -> Additional Library Directories` add the following paths:

```
{unziped location}\libsodium\x64\Release\v142\dynamic
%PROGRAMFILES%\PostgreSQL\<version>\lib;
```

- Select the `Release` configuration and build
- Rename `pgsodium_vc16.dll` to `pgsodium.dll`
- Copy `libsodium.dll` (usuall in `libsodium\x64\Release\v142\dynamic`) and the newly built `pgsodium.dll` into your `\PostgreSQL\<version>\lib` directory
7 changes: 7 additions & 0 deletions getkey_scripts/pgsodium_getkey.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@echo off
set KEY_FILE="%PGDATA%/pgsodium_root.key"

IF NOT EXIST %KEY_FILE% (
openssl rand -hex 32 > %KEY_FILE%
)
type "%PGDATA%/pgsodium_root.key"
2 changes: 1 addition & 1 deletion src/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pgsodium_crypto_aead_det_keygen (PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P (result);
}

PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_noncegen);
PGDLLEXPORT PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_noncegen);
Datum
pgsodium_crypto_aead_det_noncegen (PG_FUNCTION_ARGS)
{
Expand Down
2 changes: 1 addition & 1 deletion src/box.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pgsodium_crypto_box_keypair (PG_FUNCTION_ARGS)
return result;
}

PG_FUNCTION_INFO_V1 (pgsodium_crypto_box_new_seed);
PGDLLEXPORT PG_FUNCTION_INFO_V1 (pgsodium_crypto_box_new_seed);
Datum
pgsodium_crypto_box_new_seed (PG_FUNCTION_ARGS)
{
Expand Down
16 changes: 16 additions & 0 deletions src/pgsodium.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "pgsodium.h"

#ifdef WIN32
#define X_OK 6
#define access _access
#endif

PG_MODULE_MAGIC;

bytea *pgsodium_secret_key;
Expand Down Expand Up @@ -136,9 +141,20 @@ _PG_init (void)
proc_exit (1);
}

#ifdef WIN32
secret_len = 65; // extra byte for null terminator
secret_buf = malloc(secret_len);
if (fgets (secret_buf, (int)secret_len, fp) == NULL)
{
ereport(ERROR, errmsg("unable to read secret key"));
proc_exit (1);
}
secret_buf[strcspn(secret_buf, "\r\n")] = '\0';
#else
char_read = getline (&secret_buf, &secret_len, fp);
if (secret_buf[char_read - 1] == '\n')
secret_buf[char_read - 1] = '\0';
#endif

secret_len = strlen (secret_buf);

Expand Down
Loading