-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2987ffa
Showing
15 changed files
with
1,885 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 dmitrystas | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
EXTENSION = blake2b | ||
MODULE_big = blake2b | ||
DATA = blake2b--1.0.sql | ||
OBJS = pg_blake2b.o | ||
REGRESS = blake2b-test | ||
PG_CONFIG = pg_config | ||
PGXS := $(shell $(PG_CONFIG) --pgxs) | ||
include $(PGXS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# pg_blake2b | ||
PostgreSQL extension for fast secure hashing | ||
|
||
## About | ||
The extension provides BLAKE2b cryptographic hash function for [PostgreSQL](https://www.postgresql.org/) using original C implementations of [BLAKE2b](https://github.com/BLAKE2/BLAKE2), optimized for speed on CPUs supporting SSE2, SSSE3, SSE4.1, AVX, or XOP | ||
|
||
## Install | ||
|
||
Before you begin the install, you may also need to install the '-dev' PostgreSQL packages to build the extension (replace [XX] with your already installed PostgreSQL versionL: 14, 15, 16, etc): | ||
|
||
``` | ||
sudo apt update | ||
sudo apt install postgresql-server-dev-XX | ||
``` | ||
|
||
Build and install pg_blake2b: | ||
|
||
``` | ||
cd /path/to/pg_blake2b | ||
make | ||
sudo make install | ||
``` | ||
|
||
Run tests: | ||
|
||
``` | ||
make installcheck | ||
``` | ||
|
||
Enable extension: | ||
|
||
``` | ||
create extension blake2b; | ||
``` | ||
|
||
## Uninstall | ||
|
||
Disable extension: | ||
|
||
``` | ||
drop extension blake2b; | ||
``` | ||
|
||
Uninstall pg_blake2b: | ||
|
||
``` | ||
cd /path/to/pg_blake2b | ||
sudo make uninstall | ||
``` | ||
## Usage | ||
|
||
Extension method is defined as function with 3 parameters. The first parameter specifies the data (string or bytea) for hashing, the second parameter is optional length of the hash in bytes (integer from 1 to 64, 64 by default), and the third parameter specifies the optional key (bytea, max 64 bytes): | ||
|
||
``` | ||
blake2b( | ||
data bytea, | ||
digest_size integer DEFAULT NULL, | ||
key bytea DEFAULT NULL | ||
) | ||
blake2b( | ||
data text, | ||
digest_size integer DEFAULT NULL, | ||
key bytea DEFAULT NULL | ||
) | ||
``` | ||
|
||
Example: | ||
|
||
``` | ||
SELECT encode(blake2b('33', 32), 'hex'); | ||
encode | ||
------------------------------------------------------------------ | ||
ef97c9e36f5e33370c795599501e8f834363461eb001fd227831992c57b5bedf | ||
(1 row) | ||
SELECT encode(blake2b('\x3333'::bytea, 32), 'hex'); | ||
encode | ||
------------------------------------------------------------------ | ||
ef97c9e36f5e33370c795599501e8f834363461eb001fd227831992c57b5bedf | ||
(1 row) | ||
SELECT encode(blake2b('3333', 32), 'hex'); | ||
encode | ||
------------------------------------------------------------------ | ||
f4395a05f95dbb8a628c7be3eb5cbcde9393d90fd44837ab390c027daecc6c33 | ||
(1 row) | ||
SELECT encode(blake2b('3333', 32, '\x0123456789'::bytea), 'hex'); | ||
encode | ||
------------------------------------------------------------------ | ||
912bf54103cb9d1093c3e736514040fd6a7ea75d094c8ce843425a4791571b82 | ||
(1 row) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
BLAKE2 reference source code package - optimized C implementations | ||
Copyright 2012, Samuel Neves <[email protected]>. You may use this under the | ||
terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at | ||
your option. The terms of these licenses can be found at: | ||
- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 | ||
- OpenSSL license : https://www.openssl.org/source/license.html | ||
- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 | ||
More information about the BLAKE2 hash function can be found at | ||
https://blake2.net. | ||
*/ | ||
#ifndef BLAKE2_CONFIG_H | ||
#define BLAKE2_CONFIG_H | ||
|
||
/* These don't work everywhere */ | ||
#if defined(__SSE2__) || defined(__x86_64__) || defined(__amd64__) | ||
#define HAVE_SSE2 | ||
#endif | ||
|
||
#if defined(__SSSE3__) | ||
#define HAVE_SSSE3 | ||
#endif | ||
|
||
#if defined(__SSE4_1__) | ||
#define HAVE_SSE41 | ||
#endif | ||
|
||
#if defined(__AVX__) | ||
#define HAVE_AVX | ||
#endif | ||
|
||
#if defined(__XOP__) | ||
#define HAVE_XOP | ||
#endif | ||
|
||
|
||
#ifdef HAVE_AVX2 | ||
#ifndef HAVE_AVX | ||
#define HAVE_AVX | ||
#endif | ||
#endif | ||
|
||
#ifdef HAVE_XOP | ||
#ifndef HAVE_AVX | ||
#define HAVE_AVX | ||
#endif | ||
#endif | ||
|
||
#ifdef HAVE_AVX | ||
#ifndef HAVE_SSE41 | ||
#define HAVE_SSE41 | ||
#endif | ||
#endif | ||
|
||
#ifdef HAVE_SSE41 | ||
#ifndef HAVE_SSSE3 | ||
#define HAVE_SSSE3 | ||
#endif | ||
#endif | ||
|
||
#ifdef HAVE_SSSE3 | ||
#define HAVE_SSE2 | ||
#endif | ||
|
||
#if !defined(HAVE_SSE2) | ||
#error "This code requires at least SSE2." | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
BLAKE2 reference source code package - reference C implementations | ||
Copyright 2012, Samuel Neves <[email protected]>. You may use this under the | ||
terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at | ||
your option. The terms of these licenses can be found at: | ||
- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 | ||
- OpenSSL license : https://www.openssl.org/source/license.html | ||
- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 | ||
More information about the BLAKE2 hash function can be found at | ||
https://blake2.net. | ||
*/ | ||
#ifndef BLAKE2_IMPL_H | ||
#define BLAKE2_IMPL_H | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) | ||
#if defined(_MSC_VER) | ||
#define BLAKE2_INLINE __inline | ||
#elif defined(__GNUC__) | ||
#define BLAKE2_INLINE __inline__ | ||
#else | ||
#define BLAKE2_INLINE | ||
#endif | ||
#else | ||
#define BLAKE2_INLINE inline | ||
#endif | ||
|
||
static BLAKE2_INLINE uint32_t load32( const void *src ) | ||
{ | ||
#if defined(NATIVE_LITTLE_ENDIAN) | ||
uint32_t w; | ||
memcpy(&w, src, sizeof w); | ||
return w; | ||
#else | ||
const uint8_t *p = ( const uint8_t * )src; | ||
return (( uint32_t )( p[0] ) << 0) | | ||
(( uint32_t )( p[1] ) << 8) | | ||
(( uint32_t )( p[2] ) << 16) | | ||
(( uint32_t )( p[3] ) << 24) ; | ||
#endif | ||
} | ||
|
||
static BLAKE2_INLINE uint64_t load64( const void *src ) | ||
{ | ||
#if defined(NATIVE_LITTLE_ENDIAN) | ||
uint64_t w; | ||
memcpy(&w, src, sizeof w); | ||
return w; | ||
#else | ||
const uint8_t *p = ( const uint8_t * )src; | ||
return (( uint64_t )( p[0] ) << 0) | | ||
(( uint64_t )( p[1] ) << 8) | | ||
(( uint64_t )( p[2] ) << 16) | | ||
(( uint64_t )( p[3] ) << 24) | | ||
(( uint64_t )( p[4] ) << 32) | | ||
(( uint64_t )( p[5] ) << 40) | | ||
(( uint64_t )( p[6] ) << 48) | | ||
(( uint64_t )( p[7] ) << 56) ; | ||
#endif | ||
} | ||
|
||
static BLAKE2_INLINE uint16_t load16( const void *src ) | ||
{ | ||
#if defined(NATIVE_LITTLE_ENDIAN) | ||
uint16_t w; | ||
memcpy(&w, src, sizeof w); | ||
return w; | ||
#else | ||
const uint8_t *p = ( const uint8_t * )src; | ||
return ( uint16_t )((( uint32_t )( p[0] ) << 0) | | ||
(( uint32_t )( p[1] ) << 8)); | ||
#endif | ||
} | ||
|
||
static BLAKE2_INLINE void store16( void *dst, uint16_t w ) | ||
{ | ||
#if defined(NATIVE_LITTLE_ENDIAN) | ||
memcpy(dst, &w, sizeof w); | ||
#else | ||
uint8_t *p = ( uint8_t * )dst; | ||
*p++ = ( uint8_t )w; w >>= 8; | ||
*p++ = ( uint8_t )w; | ||
#endif | ||
} | ||
|
||
static BLAKE2_INLINE void store32( void *dst, uint32_t w ) | ||
{ | ||
#if defined(NATIVE_LITTLE_ENDIAN) | ||
memcpy(dst, &w, sizeof w); | ||
#else | ||
uint8_t *p = ( uint8_t * )dst; | ||
p[0] = (uint8_t)(w >> 0); | ||
p[1] = (uint8_t)(w >> 8); | ||
p[2] = (uint8_t)(w >> 16); | ||
p[3] = (uint8_t)(w >> 24); | ||
#endif | ||
} | ||
|
||
static BLAKE2_INLINE void store64( void *dst, uint64_t w ) | ||
{ | ||
#if defined(NATIVE_LITTLE_ENDIAN) | ||
memcpy(dst, &w, sizeof w); | ||
#else | ||
uint8_t *p = ( uint8_t * )dst; | ||
p[0] = (uint8_t)(w >> 0); | ||
p[1] = (uint8_t)(w >> 8); | ||
p[2] = (uint8_t)(w >> 16); | ||
p[3] = (uint8_t)(w >> 24); | ||
p[4] = (uint8_t)(w >> 32); | ||
p[5] = (uint8_t)(w >> 40); | ||
p[6] = (uint8_t)(w >> 48); | ||
p[7] = (uint8_t)(w >> 56); | ||
#endif | ||
} | ||
|
||
static BLAKE2_INLINE uint64_t load48( const void *src ) | ||
{ | ||
const uint8_t *p = ( const uint8_t * )src; | ||
return (( uint64_t )( p[0] ) << 0) | | ||
(( uint64_t )( p[1] ) << 8) | | ||
(( uint64_t )( p[2] ) << 16) | | ||
(( uint64_t )( p[3] ) << 24) | | ||
(( uint64_t )( p[4] ) << 32) | | ||
(( uint64_t )( p[5] ) << 40) ; | ||
} | ||
|
||
static BLAKE2_INLINE void store48( void *dst, uint64_t w ) | ||
{ | ||
uint8_t *p = ( uint8_t * )dst; | ||
p[0] = (uint8_t)(w >> 0); | ||
p[1] = (uint8_t)(w >> 8); | ||
p[2] = (uint8_t)(w >> 16); | ||
p[3] = (uint8_t)(w >> 24); | ||
p[4] = (uint8_t)(w >> 32); | ||
p[5] = (uint8_t)(w >> 40); | ||
} | ||
|
||
static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c ) | ||
{ | ||
return ( w >> c ) | ( w << ( 32 - c ) ); | ||
} | ||
|
||
static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c ) | ||
{ | ||
return ( w >> c ) | ( w << ( 64 - c ) ); | ||
} | ||
|
||
/* prevents compiler optimizing out memset() */ | ||
static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n) | ||
{ | ||
static void *(*const volatile memset_v)(void *, int, size_t) = &memset; | ||
memset_v(v, 0, n); | ||
} | ||
|
||
#endif |
Oops, something went wrong.