chore(upgradeSQLCipher): Upgrading SQLCipher to version 5.4.5 (#3559)
* chore(upgradeSQLCipher): Upgrading SQLCipher to version 5.4.5 Changes: ### github.com/mutecomm/go-sqlcipher 1. The improved crypto argorighms from go-sqlcipher v3 are merged in v4 Tags: v4.4.2-status.1 - merge `burn_stack` improvement v4.4.2-status.2 - merge `SHA1` improvement v4.4.2-status.4- merge 'AES' improvement 2. Fixed `go-sqlcipher` to support v3 database in compatibility mode (`sqlcipher` already supports this) (Tag: v4.4.2-status.3) 3. Upgrade `sqlcipher` to v5.4.5 (Tag: v4.5.4-status.1) ### github.com/status-im/migrate/v4 1. Upgrade `go-sqlcipher` version in `github.com/status-im/migrate/v4` ### status-go 1. Upgrade `go-sqlcipher` and `migrate` modules in status-go 2. Configure the DB connections to open the DB in v3 compatibility mode * chore(upgradeSQLCipher): Use sqlcipher v3 configuration to encrypt a plain text database * chore(upgradeSQLCipher): Scanning NULL BLOB value should return nil Fixing failing tests: TestSyncDeviceSuite/TestPairingSyncDeviceClientAsReceiver; TestSyncDeviceSuite/TestPairingSyncDeviceClientAsSender Considering the following configuration: 1. Table with BLOB column has 1 NULL value 2. Query the value 3. Rows.Scan(&dest sql.NullString) Expected: dest.Valid == false; dest.String == nil Actual: dest.Valid == true; dest.String == "" * chore: Bump go-sqlcipher version to include NULL BLOB fix
This commit is contained in:
parent
fb4b93e8be
commit
e8c4b7647f
6
go.mod
6
go.mod
|
@ -10,7 +10,7 @@ replace github.com/nfnt/resize => github.com/status-im/resize v0.0.0-20201215164
|
|||
|
||||
replace github.com/forPelevin/gomoji => github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4
|
||||
|
||||
replace github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f => github.com/status-im/go-sqlcipher v0.1.0-status.6
|
||||
replace github.com/mutecomm/go-sqlcipher/v4 v4.4.2 => github.com/status-im/go-sqlcipher/v4 v4.5.4-status.2
|
||||
|
||||
require (
|
||||
github.com/anacrolix/torrent v1.41.0
|
||||
|
@ -39,7 +39,6 @@ require (
|
|||
github.com/multiformats/go-multibase v0.2.0
|
||||
github.com/multiformats/go-multihash v0.2.1
|
||||
github.com/multiformats/go-varint v0.0.7
|
||||
github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f
|
||||
github.com/nfnt/resize v0.0.0-00010101000000-000000000000
|
||||
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd
|
||||
github.com/oliamb/cutter v0.2.2
|
||||
|
@ -49,7 +48,7 @@ require (
|
|||
github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a
|
||||
github.com/status-im/doubleratchet v3.0.0+incompatible
|
||||
github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5
|
||||
github.com/status-im/migrate/v4 v4.6.2-status.2
|
||||
github.com/status-im/migrate/v4 v4.6.2-status.3
|
||||
github.com/status-im/rendezvous v1.3.6
|
||||
github.com/status-im/status-go/extkeys v1.1.2
|
||||
github.com/status-im/tcp-shaker v0.0.0-20191114194237-215893130501
|
||||
|
@ -80,6 +79,7 @@ require (
|
|||
github.com/ipfs/go-log/v2 v2.5.1
|
||||
github.com/ladydascalie/currency v1.6.0
|
||||
github.com/meirf/gopart v0.0.0-20180520194036-37e9492a85a8
|
||||
github.com/mutecomm/go-sqlcipher/v4 v4.4.2
|
||||
github.com/schollz/peerdiscovery v1.7.0
|
||||
github.com/siphiuel/lc-proxy-wrapper v0.0.0-20230516150924-246507cee8c7
|
||||
github.com/waku-org/go-waku v0.6.1-0.20230526151800-10c2e20910bf
|
||||
|
|
8
go.sum
8
go.sum
|
@ -1555,6 +1555,7 @@ github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/n
|
|||
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f/go.mod h1:MyUWrZlB1aI5bs7j9/pJ8ckLLZ4QcCYcNiSbsAW32D4=
|
||||
github.com/mutecomm/go-sqlcipher/v4 v4.4.0/go.mod h1:PyN04SaWalavxRGH9E8ZftG6Ju7rsPrGmQRjrEaVpiY=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
|
@ -1997,8 +1998,8 @@ github.com/status-im/go-ethereum v1.10.25-status.6 h1:5YC8k1inTBqA6LpON0uX6y86ni
|
|||
github.com/status-im/go-ethereum v1.10.25-status.6/go.mod h1:Dt4K5JYMhJRdtXJwBEyGZLZn9iz/chSOZyjVmt5ZhwQ=
|
||||
github.com/status-im/go-multiaddr-ethv4 v1.2.4 h1:7fw0Y48TJXEqx4fOHlDOUiM/uBq9zG5w4x975Mjh4E0=
|
||||
github.com/status-im/go-multiaddr-ethv4 v1.2.4/go.mod h1:PDh4D7h5CvecPIy0ji0rLNwTnzzEcyz9uTPHD42VyH4=
|
||||
github.com/status-im/go-sqlcipher v0.1.0-status.6 h1:6bpgmTZYWc6y96E4yuMwBxq7WaTQVM0aWmsnebk8Dfw=
|
||||
github.com/status-im/go-sqlcipher v0.1.0-status.6/go.mod h1:MyUWrZlB1aI5bs7j9/pJ8ckLLZ4QcCYcNiSbsAW32D4=
|
||||
github.com/status-im/go-sqlcipher/v4 v4.5.4-status.2 h1:Oi9JTAI2DZEe5UKlpUcvKBCCSn3ULsLIrix7jPnEoPE=
|
||||
github.com/status-im/go-sqlcipher/v4 v4.5.4-status.2/go.mod h1:mF2UmIpBnzFeBdu/ypTDb/LdbS0nk0dfSN1WUsWTjMA=
|
||||
github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4 h1:CtobZoiNdHpx+xurFxnuJ1xsGm3oKMfcZkB3vmomJmA=
|
||||
github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4/go.mod h1:hmpnZzkzSZJbFYWAUkrPV8I36x7mdYiPhPqnALP4fKA=
|
||||
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=
|
||||
|
@ -2006,8 +2007,9 @@ github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 h1:Oo2KZNP70K
|
|||
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=
|
||||
github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5 h1:Mie/nQtRrI/+qqyV4j5ydesjoPh6v6KF583NOPLKFQY=
|
||||
github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5/go.mod h1:5rjPyv3KffPNVbFjnsVy0NGj9+JeW40WvXLdxH1VKuE=
|
||||
github.com/status-im/migrate/v4 v4.6.2-status.2 h1:SdC+sMDl/aI7vUlwD2qj2p7KsK4T60IS9z4/rYCCbI8=
|
||||
github.com/status-im/migrate/v4 v4.6.2-status.2/go.mod h1:c/kc90n47GZu/58nnz1OMLTf7uE4Da4gZP5qmU+A/v8=
|
||||
github.com/status-im/migrate/v4 v4.6.2-status.3 h1:Khwjb59NzniloUr5i9s9AtkEyqBbQFt1lkoAu66sAu0=
|
||||
github.com/status-im/migrate/v4 v4.6.2-status.3/go.mod h1:c/kc90n47GZu/58nnz1OMLTf7uE4Da4gZP5qmU+A/v8=
|
||||
github.com/status-im/rendezvous v1.3.6 h1:iZTmTjNjy0aHtwpr+qoqZfDcwHDlxp/JMh3AOCi3gnc=
|
||||
github.com/status-im/rendezvous v1.3.6/go.mod h1:wznFLwGWJl8s0EFEBn9mCsSmLvEvvMTpW9qTNJEZLFY=
|
||||
github.com/status-im/resize v0.0.0-20201215164250-7c6d9f0d3088 h1:ClCAP2FPCvl8hGMhbUx/tq/sOu2wibztAa5jAvQEe4Q=
|
||||
|
|
|
@ -24,7 +24,7 @@ import (
|
|||
"github.com/status-im/status-go/services/wallet/thirdparty/opensea"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
_ "github.com/mutecomm/go-sqlcipher" // require go-sqlcipher that overrides default implementation
|
||||
_ "github.com/mutecomm/go-sqlcipher/v4" // require go-sqlcipher that overrides default implementation
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/mutecomm/go-sqlcipher" // require go-sqlcipher that overrides default implementation
|
||||
_ "github.com/mutecomm/go-sqlcipher/v4" // require go-sqlcipher that overrides default implementation
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/zap"
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
sqlite3 "github.com/mutecomm/go-sqlcipher"
|
||||
sqlite3 "github.com/mutecomm/go-sqlcipher/v4"
|
||||
|
||||
"github.com/status-im/status-go/eth-node/crypto"
|
||||
"github.com/status-im/status-go/protocol/protobuf"
|
||||
|
|
|
@ -3,7 +3,7 @@ package sqlite
|
|||
import (
|
||||
"database/sql"
|
||||
|
||||
_ "github.com/mutecomm/go-sqlcipher" // We require go sqlcipher that overrides default implementation
|
||||
_ "github.com/mutecomm/go-sqlcipher/v4" // We require go sqlcipher that overrides default implementation
|
||||
"github.com/pkg/errors"
|
||||
"github.com/status-im/migrate/v4"
|
||||
)
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
_ "github.com/mutecomm/go-sqlcipher" // We require go sqlcipher that overrides default implementation
|
||||
_ "github.com/mutecomm/go-sqlcipher/v4" // We require go sqlcipher that overrides default implementation
|
||||
"github.com/status-im/migrate/v4"
|
||||
"github.com/status-im/migrate/v4/database/sqlcipher"
|
||||
bindata "github.com/status-im/migrate/v4/source/go_bindata"
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
||||
"github.com/status-im/status-go/protocol/tt"
|
||||
|
||||
_ "github.com/mutecomm/go-sqlcipher"
|
||||
_ "github.com/mutecomm/go-sqlcipher/v4"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/zap"
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"runtime"
|
||||
"strings"
|
||||
|
||||
sqlcipher "github.com/mutecomm/go-sqlcipher" // We require go sqlcipher that overrides default implementation
|
||||
sqlcipher "github.com/mutecomm/go-sqlcipher/v4" // We require go sqlcipher that overrides default implementation
|
||||
|
||||
"github.com/status-im/status-go/protocol/sqlite"
|
||||
)
|
||||
|
@ -71,6 +71,20 @@ func EncryptDB(unencryptedPath string, encryptedPath string, key string, kdfIter
|
|||
return err
|
||||
}
|
||||
|
||||
if _, err := db.Exec("PRAGMA encrypted.cipher_page_size = 1024"); err != nil {
|
||||
fmt.Println("failed to set cipher_page_size pragma")
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec("PRAGMA encrypted.cipher_hmac_algorithm = HMAC_SHA1"); err != nil {
|
||||
fmt.Println("failed to set cipher_hmac_algorithm pragma")
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := db.Exec("PRAGMA encrypted.cipher_kdf_algorithm = PBKDF2_HMAC_SHA1"); err != nil {
|
||||
fmt.Println("failed to set cipher_kdf_algorithm pragma")
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.Exec(`SELECT sqlcipher_export('encrypted')`)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -120,6 +134,20 @@ func openDB(path string, key string, kdfIterationsNumber int) (*sql.DB, error) {
|
|||
kdfIterationsNumber = sqlite.ReducedKDFIterationsNumber
|
||||
}
|
||||
|
||||
if _, err := conn.Exec("PRAGMA cipher_page_size = 1024", nil); err != nil {
|
||||
fmt.Println("failed to set cipher_page_size pragma")
|
||||
return err
|
||||
}
|
||||
if _, err := conn.Exec("PRAGMA cipher_hmac_algorithm = HMAC_SHA1", nil); err != nil {
|
||||
fmt.Println("failed to set cipher_hmac_algorithm pragma")
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := conn.Exec("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1", nil); err != nil {
|
||||
fmt.Println("failed to set cipher_kdf_algorithm pragma")
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := conn.Exec(fmt.Sprintf("PRAGMA kdf_iter = '%d'", kdfIterationsNumber), []driver.Value{}); err != nil {
|
||||
return errors.New("failed to set `kdf_iter` pragma")
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
language: go
|
||||
go: 1.12
|
||||
before_install:
|
||||
- go get github.com/frankbraun/gocheck
|
||||
- go get -t github.com/mutecomm/go-sqlcipher
|
||||
script:
|
||||
- gocheck -g -c -e _example -e sqlite3_test
|
|
@ -1,90 +0,0 @@
|
|||
To maintain this code properly, the following three repositories have to be
|
||||
tracked for changes (maintenance details below):
|
||||
|
||||
- https://github.com/mattn/go-sqlite
|
||||
- https://github.com/sqlcipher/sqlcipher
|
||||
- https://github.com/libtom/libtomcrypt
|
||||
|
||||
|
||||
Update code from https://github.com/mattn/go-sqlite3
|
||||
----------------------------------------------------
|
||||
|
||||
Current HEAD: cf7286f069c3ef596efcc87781a4653a2e7607bd
|
||||
|
||||
Track files:
|
||||
|
||||
error.go
|
||||
error_test.go
|
||||
sqlite3.go (dead code and extension loading removed, sqlcipher pragmas added)
|
||||
sqlite3_other.go
|
||||
sqlite3_test.go (adjust path)
|
||||
sqlite3_windows.go
|
||||
_example/simple/simple.go
|
||||
sqlite3_test/sqltest.go
|
||||
|
||||
|
||||
Update code from https://github.com/sqlcipher/sqlcipher
|
||||
-------------------------------------------------------
|
||||
|
||||
Current release: v3.4.2
|
||||
|
||||
Execute:
|
||||
./configure
|
||||
make
|
||||
|
||||
Track files:
|
||||
sqlite3.h
|
||||
sqlite3.c
|
||||
|
||||
|
||||
Update code from https://github.com/libtom/libtomcrypt
|
||||
------------------------------------------------------
|
||||
|
||||
Current HEAD: bd7933cc2b43ebe7c4349614c6cf1271251ebee4
|
||||
|
||||
Track header files:
|
||||
|
||||
tomcrypt_argchk.h
|
||||
tomcrypt_cfg.h
|
||||
tomcrypt_cipher.h
|
||||
tomcrypt_custom.h (modified)
|
||||
tomcrypt.h
|
||||
tomcrypt_hash.h
|
||||
tomcrypt_mac.h
|
||||
tomcrypt_macros.h
|
||||
tomcrypt_math.h
|
||||
tomcrypt_misc.h
|
||||
tomcrypt_pkcs.h
|
||||
tomcrypt_pk.h
|
||||
tomcrypt_prng.h
|
||||
|
||||
Track C files:
|
||||
|
||||
aes.c (modified include "aes_tab.h" instead of "aes_tab.c")
|
||||
aes_tab.c (renamed to aes_tab.h)
|
||||
burn_stack.c
|
||||
cbc_decrypt.c
|
||||
cbc_done.c
|
||||
cbc_encrypt.c
|
||||
cbc_start.c
|
||||
crypt_argchk.c
|
||||
crypt_cipher_descriptor.c
|
||||
crypt_cipher_is_valid.c
|
||||
crypt_find_cipher.c
|
||||
crypt_find_hash.c
|
||||
crypt_hash_descriptor.c
|
||||
crypt_hash_is_valid.c
|
||||
crypt_prng_descriptor.c
|
||||
crypt_register_cipher.c
|
||||
crypt_register_hash.c
|
||||
crypt_register_prng.c
|
||||
fortuna.c
|
||||
hash_memory.c
|
||||
hmac_done.c
|
||||
hmac_init.c
|
||||
hmac_memory.c
|
||||
hmac_process.c
|
||||
pkcs_5_2.c
|
||||
sha1.c
|
||||
sha256.c
|
||||
zeromem.c
|
|
@ -1,8 +0,0 @@
|
|||
.PHONY: all update-modules
|
||||
|
||||
all:
|
||||
env GO111MODULE=on go build -v . ./_example/simple/...
|
||||
|
||||
update-modules:
|
||||
env GO111MODULE=on go get -u
|
||||
env GO111MODULE=on go mod tidy -v
|
|
@ -1,97 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_decrypt.c
|
||||
CBC implementation, encrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_CBC_MODE
|
||||
|
||||
/**
|
||||
CBC decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param len The number of bytes to process (must be multiple of block length)
|
||||
@param cbc CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
unsigned char tmp[16];
|
||||
#ifdef LTC_FAST
|
||||
LTC_FAST_TYPE tmpy;
|
||||
#else
|
||||
unsigned char tmpy;
|
||||
#endif
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen valid? */
|
||||
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (len % cbc->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#ifdef LTC_FAST
|
||||
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
|
||||
return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
|
||||
} else {
|
||||
while (len) {
|
||||
/* decrypt */
|
||||
if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* xor IV against plaintext */
|
||||
#if defined(LTC_FAST)
|
||||
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
|
||||
tmpy = *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)tmp + x));
|
||||
*((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x));
|
||||
*((LTC_FAST_TYPE*)((unsigned char *)pt + x)) = tmpy;
|
||||
}
|
||||
#else
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
tmpy = tmp[x] ^ cbc->IV[x];
|
||||
cbc->IV[x] = ct[x];
|
||||
pt[x] = tmpy;
|
||||
}
|
||||
#endif
|
||||
|
||||
ct += cbc->blocklen;
|
||||
pt += cbc->blocklen;
|
||||
len -= cbc->blocklen;
|
||||
}
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,98 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_encrypt.c
|
||||
CBC implementation, encrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_CBC_MODE
|
||||
|
||||
/**
|
||||
CBC encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param len The number of bytes to process (must be multiple of block length)
|
||||
@param cbc CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen valid? */
|
||||
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (len % cbc->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#ifdef LTC_FAST
|
||||
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) {
|
||||
return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key);
|
||||
} else {
|
||||
while (len) {
|
||||
/* xor IV against plaintext */
|
||||
#if defined(LTC_FAST)
|
||||
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
|
||||
*((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^= *((LTC_FAST_TYPE*)((unsigned char *)pt + x));
|
||||
}
|
||||
#else
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] ^= pt[x];
|
||||
}
|
||||
#endif
|
||||
|
||||
/* encrypt */
|
||||
if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* store IV [ciphertext] for a future block */
|
||||
#if defined(LTC_FAST)
|
||||
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
|
||||
*((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x));
|
||||
}
|
||||
#else
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] = ct[x];
|
||||
}
|
||||
#endif
|
||||
|
||||
ct += cbc->blocklen;
|
||||
pt += cbc->blocklen;
|
||||
len -= cbc->blocklen;
|
||||
}
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,30 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
#include <signal.h>
|
||||
|
||||
/**
|
||||
@file crypt_argchk.c
|
||||
Perform argument checking, Tom St Denis
|
||||
*/
|
||||
|
||||
#if (ARGTYPE == 0)
|
||||
void crypt_argchk(char *v, char *s, int d)
|
||||
{
|
||||
fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n",
|
||||
v, d, s);
|
||||
(void)raise(SIGABRT);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,27 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file crypt_cipher_descriptor.c
|
||||
Stores the cipher descriptor table, Tom St Denis
|
||||
*/
|
||||
|
||||
struct ltc_cipher_descriptor cipher_descriptor[TAB_SIZE] = {
|
||||
{ NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
LTC_MUTEX_GLOBAL(ltc_cipher_mutex)
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,27 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file crypt_hash_descriptor.c
|
||||
Stores the hash descriptor table, Tom St Denis
|
||||
*/
|
||||
|
||||
struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = {
|
||||
{ NULL, 0, 0, 0, { 0 }, 0, NULL, NULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
LTC_MUTEX_GLOBAL(ltc_hash_mutex)
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,26 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file crypt_prng_descriptor.c
|
||||
Stores the PRNG descriptors, Tom St Denis
|
||||
*/
|
||||
struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = {
|
||||
{ NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
LTC_MUTEX_GLOBAL(ltc_prng_mutex)
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,430 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file fortuna.c
|
||||
Fortuna PRNG, Tom St Denis
|
||||
*/
|
||||
|
||||
/* Implementation of Fortuna by Tom St Denis
|
||||
|
||||
We deviate slightly here for reasons of simplicity [and to fit in the API]. First all "sources"
|
||||
in the AddEntropy function are fixed to 0. Second since no reliable timer is provided
|
||||
we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to the read function */
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
|
||||
/* requries LTC_SHA256 and AES */
|
||||
#if !(defined(LTC_RIJNDAEL) && defined(LTC_SHA256))
|
||||
#error LTC_FORTUNA requires LTC_SHA256 and LTC_RIJNDAEL (AES)
|
||||
#endif
|
||||
|
||||
#ifndef LTC_FORTUNA_POOLS
|
||||
#warning LTC_FORTUNA_POOLS was not previously defined (old headers?)
|
||||
#define LTC_FORTUNA_POOLS 32
|
||||
#endif
|
||||
|
||||
#if LTC_FORTUNA_POOLS < 4 || LTC_FORTUNA_POOLS > 32
|
||||
#error LTC_FORTUNA_POOLS must be in [4..32]
|
||||
#endif
|
||||
|
||||
const struct ltc_prng_descriptor fortuna_desc = {
|
||||
"fortuna", 1024,
|
||||
&fortuna_start,
|
||||
&fortuna_add_entropy,
|
||||
&fortuna_ready,
|
||||
&fortuna_read,
|
||||
&fortuna_done,
|
||||
&fortuna_export,
|
||||
&fortuna_import,
|
||||
&fortuna_test
|
||||
};
|
||||
|
||||
/* update the IV */
|
||||
static void fortuna_update_iv(prng_state *prng)
|
||||
{
|
||||
int x;
|
||||
unsigned char *IV;
|
||||
/* update IV */
|
||||
IV = prng->fortuna.IV;
|
||||
for (x = 0; x < 16; x++) {
|
||||
IV[x] = (IV[x] + 1) & 255;
|
||||
if (IV[x] != 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
/* reseed the PRNG */
|
||||
static int fortuna_reseed(prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
hash_state md;
|
||||
int err, x;
|
||||
|
||||
++prng->fortuna.reset_cnt;
|
||||
|
||||
/* new K == LTC_SHA256(K || s) where s == LTC_SHA256(P0) || LTC_SHA256(P1) ... */
|
||||
sha256_init(&md);
|
||||
if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if (x == 0 || ((prng->fortuna.reset_cnt >> (x-1)) & 1) == 0) {
|
||||
/* terminate this hash */
|
||||
if ((err = sha256_done(&prng->fortuna.pool[x], tmp)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
/* add it to the string */
|
||||
if ((err = sha256_process(&md, tmp, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
/* reset this pool */
|
||||
if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* finish key */
|
||||
if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
fortuna_update_iv(prng);
|
||||
|
||||
/* reset pool len */
|
||||
prng->fortuna.pool0_len = 0;
|
||||
prng->fortuna.wd = 0;
|
||||
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(&md, sizeof(md));
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_start(prng_state *prng)
|
||||
{
|
||||
int err, x, y;
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
/* initialize the pools */
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
|
||||
for (y = 0; y < x; y++) {
|
||||
sha256_done(&prng->fortuna.pool[y], tmp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
}
|
||||
prng->fortuna.pool_idx = prng->fortuna.pool0_len = prng->fortuna.wd = 0;
|
||||
prng->fortuna.reset_cnt = 0;
|
||||
|
||||
/* reset bufs */
|
||||
zeromem(prng->fortuna.K, 32);
|
||||
if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
sha256_done(&prng->fortuna.pool[x], tmp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
zeromem(prng->fortuna.IV, 16);
|
||||
|
||||
LTC_MUTEX_INIT(&prng->fortuna.prng_lock)
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[2];
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->fortuna.prng_lock);
|
||||
|
||||
/* ensure inlen <= 32 */
|
||||
if (inlen > 32) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* add s || length(in) || in to pool[pool_idx] */
|
||||
tmp[0] = 0;
|
||||
tmp[1] = (unsigned char)inlen;
|
||||
if ((err = sha256_process(&prng->fortuna.pool[prng->fortuna.pool_idx], tmp, 2)) != CRYPT_OK) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return err;
|
||||
}
|
||||
if ((err = sha256_process(&prng->fortuna.pool[prng->fortuna.pool_idx], in, inlen)) != CRYPT_OK) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return err;
|
||||
}
|
||||
if (prng->fortuna.pool_idx == 0) {
|
||||
prng->fortuna.pool0_len += inlen;
|
||||
}
|
||||
if (++(prng->fortuna.pool_idx) == LTC_FORTUNA_POOLS) {
|
||||
prng->fortuna.pool_idx = 0;
|
||||
}
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_ready(prng_state *prng)
|
||||
{
|
||||
return fortuna_reseed(prng);
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[16];
|
||||
unsigned long tlen;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->fortuna.prng_lock);
|
||||
|
||||
/* do we have to reseed? */
|
||||
if (++prng->fortuna.wd == LTC_FORTUNA_WD || prng->fortuna.pool0_len >= 64) {
|
||||
if (fortuna_reseed(prng) != CRYPT_OK) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* now generate the blocks required */
|
||||
tlen = outlen;
|
||||
|
||||
/* handle whole blocks without the extra XMEMCPY */
|
||||
while (outlen >= 16) {
|
||||
/* encrypt the IV and store it */
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, out, &prng->fortuna.skey);
|
||||
out += 16;
|
||||
outlen -= 16;
|
||||
fortuna_update_iv(prng);
|
||||
}
|
||||
|
||||
/* left over bytes? */
|
||||
if (outlen > 0) {
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, tmp, &prng->fortuna.skey);
|
||||
XMEMCPY(out, tmp, outlen);
|
||||
fortuna_update_iv(prng);
|
||||
}
|
||||
|
||||
/* generate new key */
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K , &prng->fortuna.skey);
|
||||
fortuna_update_iv(prng);
|
||||
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K+16, &prng->fortuna.skey);
|
||||
fortuna_update_iv(prng);
|
||||
|
||||
if (rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey) != CRYPT_OK) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return tlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_done(prng_state *prng)
|
||||
{
|
||||
int err, x;
|
||||
unsigned char tmp[32];
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_MUTEX_LOCK(&prng->fortuna.prng_lock);
|
||||
|
||||
/* terminate all the hashes */
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = sha256_done(&(prng->fortuna.pool[x]), tmp)) != CRYPT_OK) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
/* call cipher done when we invent one ;-) */
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
int x, err;
|
||||
hash_state *md;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->fortuna.prng_lock);
|
||||
|
||||
/* we'll write bytes for s&g's */
|
||||
if (*outlen < 32*LTC_FORTUNA_POOLS) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
*outlen = 32*LTC_FORTUNA_POOLS;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
md = XMALLOC(sizeof(hash_state));
|
||||
if (md == NULL) {
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* to emit the state we copy each pool, terminate it then hash it again so
|
||||
* an attacker who sees the state can't determine the current state of the PRNG
|
||||
*/
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
/* copy the PRNG */
|
||||
XMEMCPY(md, &(prng->fortuna.pool[x]), sizeof(*md));
|
||||
|
||||
/* terminate it */
|
||||
if ((err = sha256_done(md, out+x*32)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* now hash it */
|
||||
if ((err = sha256_init(md)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = sha256_process(md, out+x*32, 32)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = sha256_done(md, out+x*32)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
*outlen = 32*LTC_FORTUNA_POOLS;
|
||||
err = CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(md, sizeof(*md));
|
||||
#endif
|
||||
XFREE(md);
|
||||
LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err, x;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
if (inlen != 32*LTC_FORTUNA_POOLS) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if ((err = fortuna_start(prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = fortuna_add_entropy(in+x*32, 32, prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int fortuna_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
int err;
|
||||
|
||||
if ((err = sha256_test()) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
return rijndael_test();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,43 +0,0 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file hmac_process.c
|
||||
LTC_HMAC support, process data, Tom St Denis/Dobes Vandermeer
|
||||
*/
|
||||
|
||||
#ifdef LTC_HMAC
|
||||
|
||||
/**
|
||||
Process data through LTC_HMAC
|
||||
@param hmac The hmac state
|
||||
@param in The data to send through LTC_HMAC
|
||||
@param inlen The length of the data to LTC_HMAC (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(hmac != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
if ((err = hash_is_valid(hmac->hash)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
return hash_descriptor[hmac->hash].process(&hmac->md, in, inlen);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,823 +0,0 @@
|
|||
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -std=gnu99
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61
|
||||
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
|
||||
#cgo CFLAGS: -DSQLITE_DISABLE_INTRINSIC
|
||||
#cgo CFLAGS: -Wno-deprecated-declarations
|
||||
#include <sqlite3.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
# include <errno.h>
|
||||
#endif
|
||||
|
||||
#ifndef SQLITE_OPEN_READWRITE
|
||||
# define SQLITE_OPEN_READWRITE 0
|
||||
#endif
|
||||
|
||||
#ifndef SQLITE_OPEN_FULLMUTEX
|
||||
# define SQLITE_OPEN_FULLMUTEX 0
|
||||
#endif
|
||||
|
||||
#ifndef SQLITE_DETERMINISTIC
|
||||
# define SQLITE_DETERMINISTIC 0
|
||||
#endif
|
||||
|
||||
static int
|
||||
_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
|
||||
#ifdef SQLITE_OPEN_URI
|
||||
return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
|
||||
#else
|
||||
return sqlite3_open_v2(filename, ppDb, flags, zVfs);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
|
||||
return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
|
||||
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static int
|
||||
_sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
|
||||
{
|
||||
int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
|
||||
*rowid = (long long) sqlite3_last_insert_rowid(db);
|
||||
*changes = (long long) sqlite3_changes(db);
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_step(sqlite3_stmt* stmt, long long* rowid, long long* changes)
|
||||
{
|
||||
int rv = sqlite3_step(stmt);
|
||||
sqlite3* db = sqlite3_db_handle(stmt);
|
||||
*rowid = (long long) sqlite3_last_insert_rowid(db);
|
||||
*changes = (long long) sqlite3_changes(db);
|
||||
return rv;
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// SQLiteTimestampFormats is timestamp formats understood by both this module
|
||||
// and SQLite. The first format in the slice will be used when saving time
|
||||
// values into the database. When parsing a string from a timestamp or datetime
|
||||
// column, the formats are tried in order.
|
||||
var SQLiteTimestampFormats = []string{
|
||||
// By default, store timestamps with whatever timezone they come with.
|
||||
// When parsed, they will be returned with the same timezone.
|
||||
"2006-01-02 15:04:05.999999999-07:00",
|
||||
"2006-01-02T15:04:05.999999999-07:00",
|
||||
"2006-01-02 15:04:05.999999999",
|
||||
"2006-01-02T15:04:05.999999999",
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02 15:04",
|
||||
"2006-01-02T15:04",
|
||||
"2006-01-02",
|
||||
}
|
||||
|
||||
func init() {
|
||||
sql.Register("sqlite3", &SQLiteDriver{})
|
||||
}
|
||||
|
||||
// Version returns SQLite library version information.
|
||||
func Version() (libVersion string, libVersionNumber int, sourceID string) {
|
||||
libVersion = C.GoString(C.sqlite3_libversion())
|
||||
libVersionNumber = int(C.sqlite3_libversion_number())
|
||||
sourceID = C.GoString(C.sqlite3_sourceid())
|
||||
return libVersion, libVersionNumber, sourceID
|
||||
}
|
||||
|
||||
// SQLiteDriver implement sql.Driver.
|
||||
type SQLiteDriver struct {
|
||||
ConnectHook func(*SQLiteConn) error
|
||||
}
|
||||
|
||||
// SQLiteConn implement sql.Conn.
|
||||
type SQLiteConn struct {
|
||||
db *C.sqlite3
|
||||
loc *time.Location
|
||||
txlock string
|
||||
}
|
||||
|
||||
// SQLiteTx implemen sql.Tx.
|
||||
type SQLiteTx struct {
|
||||
c *SQLiteConn
|
||||
}
|
||||
|
||||
// SQLiteStmt implement sql.Stmt.
|
||||
type SQLiteStmt struct {
|
||||
c *SQLiteConn
|
||||
s *C.sqlite3_stmt
|
||||
t string
|
||||
closed bool
|
||||
cls bool
|
||||
}
|
||||
|
||||
// SQLiteResult implement sql.Result.
|
||||
type SQLiteResult struct {
|
||||
id int64
|
||||
changes int64
|
||||
}
|
||||
|
||||
// SQLiteRows implement sql.Rows.
|
||||
type SQLiteRows struct {
|
||||
s *SQLiteStmt
|
||||
nc int
|
||||
cols []string
|
||||
decltype []string
|
||||
cls bool
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// Commit transaction.
|
||||
func (tx *SQLiteTx) Commit() error {
|
||||
_, err := tx.c.exec(context.Background(), "COMMIT", nil)
|
||||
if err != nil && err.(Error).Code == C.SQLITE_BUSY {
|
||||
// sqlite3 will leave the transaction open in this scenario.
|
||||
// However, database/sql considers the transaction complete once we
|
||||
// return from Commit() - we must clean up to honour its semantics.
|
||||
tx.c.exec(context.Background(), "ROLLBACK", nil)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Rollback transaction.
|
||||
func (tx *SQLiteTx) Rollback() error {
|
||||
_, err := tx.c.exec(context.Background(), "ROLLBACK", nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// AutoCommit return which currently auto commit or not.
|
||||
func (c *SQLiteConn) AutoCommit() bool {
|
||||
return int(C.sqlite3_get_autocommit(c.db)) != 0
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) lastError() error {
|
||||
return lastError(c.db)
|
||||
}
|
||||
|
||||
func lastError(db *C.sqlite3) error {
|
||||
rv := C.sqlite3_errcode(db)
|
||||
if rv == C.SQLITE_OK {
|
||||
return nil
|
||||
}
|
||||
return Error{
|
||||
Code: ErrNo(rv),
|
||||
ExtendedCode: ErrNoExtended(C.sqlite3_extended_errcode(db)),
|
||||
err: C.GoString(C.sqlite3_errmsg(db)),
|
||||
}
|
||||
}
|
||||
|
||||
// Exec implements Execer.
|
||||
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, v := range args {
|
||||
list[i] = namedValue{
|
||||
Ordinal: i + 1,
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
return c.exec(context.Background(), query, list)
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) {
|
||||
start := 0
|
||||
for {
|
||||
s, err := c.prepare(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res driver.Result
|
||||
if s.(*SQLiteStmt).s != nil {
|
||||
na := s.NumInput()
|
||||
if len(args) < na {
|
||||
s.Close()
|
||||
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
|
||||
}
|
||||
for i := 0; i < na; i++ {
|
||||
args[i].Ordinal -= start
|
||||
}
|
||||
res, err = s.(*SQLiteStmt).exec(ctx, args[:na])
|
||||
if err != nil && err != driver.ErrSkip {
|
||||
s.Close()
|
||||
return nil, err
|
||||
}
|
||||
args = args[na:]
|
||||
start += na
|
||||
}
|
||||
tail := s.(*SQLiteStmt).t
|
||||
s.Close()
|
||||
if tail == "" {
|
||||
return res, nil
|
||||
}
|
||||
query = tail
|
||||
}
|
||||
}
|
||||
|
||||
type namedValue struct {
|
||||
Name string
|
||||
Ordinal int
|
||||
Value driver.Value
|
||||
}
|
||||
|
||||
// Query implements Queryer.
|
||||
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, v := range args {
|
||||
list[i] = namedValue{
|
||||
Ordinal: i + 1,
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
return c.query(context.Background(), query, list)
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) {
|
||||
start := 0
|
||||
for {
|
||||
s, err := c.prepare(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.(*SQLiteStmt).cls = true
|
||||
na := s.NumInput()
|
||||
if len(args) < na {
|
||||
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
|
||||
}
|
||||
for i := 0; i < na; i++ {
|
||||
args[i].Ordinal -= start
|
||||
}
|
||||
rows, err := s.(*SQLiteStmt).query(ctx, args[:na])
|
||||
if err != nil && err != driver.ErrSkip {
|
||||
s.Close()
|
||||
return rows, err
|
||||
}
|
||||
args = args[na:]
|
||||
start += na
|
||||
tail := s.(*SQLiteStmt).t
|
||||
if tail == "" {
|
||||
return rows, nil
|
||||
}
|
||||
rows.Close()
|
||||
s.Close()
|
||||
query = tail
|
||||
}
|
||||
}
|
||||
|
||||
// Begin transaction.
|
||||
func (c *SQLiteConn) Begin() (driver.Tx, error) {
|
||||
return c.begin(context.Background())
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) {
|
||||
if _, err := c.exec(ctx, c.txlock, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLiteTx{c}, nil
|
||||
}
|
||||
|
||||
func errorString(err Error) string {
|
||||
return C.GoString(C.sqlite3_errstr(C.int(err.Code)))
|
||||
}
|
||||
|
||||
// Open database and return a new connection.
|
||||
// You can specify a DSN string using a URI as the filename.
|
||||
// test.db
|
||||
// file:test.db?cache=shared&mode=memory
|
||||
// :memory:
|
||||
// file::memory:
|
||||
// go-sqlite3 adds the following query parameters to those used by SQLite:
|
||||
// _loc=XXX
|
||||
// Specify location of time format. It's possible to specify "auto".
|
||||
// _busy_timeout=XXX
|
||||
// Specify value for sqlite3_busy_timeout.
|
||||
// _txlock=XXX
|
||||
// Specify locking behavior for transactions. XXX can be "immediate",
|
||||
// "deferred", "exclusive".
|
||||
// _foreign_keys=X
|
||||
// Enable or disable enforcement of foreign keys. X can be 1 or 0.
|
||||
// go-sqlcipher adds the following query parameters to those used by SQLite:
|
||||
// _pragma_key=XXX
|
||||
// Specify PRAGMA key.
|
||||
// _pragma_cipher_page_size=XXX
|
||||
// Set the PRAGMA cipher_page_size to adjust the page size.
|
||||
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
||||
if C.sqlite3_threadsafe() == 0 {
|
||||
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
|
||||
}
|
||||
|
||||
var loc *time.Location
|
||||
txlock := "BEGIN"
|
||||
busyTimeout := 5000
|
||||
foreignKeys := -1
|
||||
pos := strings.IndexRune(dsn, '?')
|
||||
var params url.Values
|
||||
if pos >= 1 {
|
||||
var err error
|
||||
params, err = url.ParseQuery(dsn[pos+1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// _loc
|
||||
if val := params.Get("_loc"); val != "" {
|
||||
if val == "auto" {
|
||||
loc = time.Local
|
||||
} else {
|
||||
loc, err = time.LoadLocation(val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// _busy_timeout
|
||||
if val := params.Get("_busy_timeout"); val != "" {
|
||||
iv, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
|
||||
}
|
||||
busyTimeout = int(iv)
|
||||
}
|
||||
|
||||
// _txlock
|
||||
if val := params.Get("_txlock"); val != "" {
|
||||
switch val {
|
||||
case "immediate":
|
||||
txlock = "BEGIN IMMEDIATE"
|
||||
case "exclusive":
|
||||
txlock = "BEGIN EXCLUSIVE"
|
||||
case "deferred":
|
||||
txlock = "BEGIN"
|
||||
default:
|
||||
return nil, fmt.Errorf("Invalid _txlock: %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
// _foreign_keys
|
||||
if val := params.Get("_foreign_keys"); val != "" {
|
||||
switch val {
|
||||
case "1":
|
||||
foreignKeys = 1
|
||||
case "0":
|
||||
foreignKeys = 0
|
||||
default:
|
||||
return nil, fmt.Errorf("Invalid _foreign_keys: %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(dsn, "file:") {
|
||||
dsn = dsn[:pos]
|
||||
}
|
||||
}
|
||||
|
||||
var db *C.sqlite3
|
||||
name := C.CString(dsn)
|
||||
defer C.free(unsafe.Pointer(name))
|
||||
rv := C._sqlite3_open_v2(name, &db,
|
||||
C.SQLITE_OPEN_NOMUTEX|
|
||||
C.SQLITE_OPEN_READWRITE|
|
||||
C.SQLITE_OPEN_CREATE,
|
||||
nil)
|
||||
if rv != 0 {
|
||||
return nil, Error{Code: ErrNo(rv)}
|
||||
}
|
||||
if db == nil {
|
||||
return nil, errors.New("sqlite succeeded without returning a database")
|
||||
}
|
||||
|
||||
rv = C.sqlite3_busy_timeout(db, C.int(busyTimeout))
|
||||
if rv != C.SQLITE_OK {
|
||||
C.sqlite3_close_v2(db)
|
||||
return nil, Error{Code: ErrNo(rv)}
|
||||
}
|
||||
|
||||
exec := func(s string) error {
|
||||
cs := C.CString(s)
|
||||
rv := C.sqlite3_exec(db, cs, nil, nil, nil)
|
||||
C.free(unsafe.Pointer(cs))
|
||||
if rv != C.SQLITE_OK {
|
||||
return lastError(db)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if foreignKeys == 0 {
|
||||
if err := exec("PRAGMA foreign_keys = OFF;"); err != nil {
|
||||
C.sqlite3_close_v2(db)
|
||||
return nil, err
|
||||
}
|
||||
} else if foreignKeys == 1 {
|
||||
if err := exec("PRAGMA foreign_keys = ON;"); err != nil {
|
||||
C.sqlite3_close_v2(db)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// process SQLCipher pragmas encoded in dsn, if necessary
|
||||
if params != nil {
|
||||
// _pragma_key
|
||||
if val := params.Get("_pragma_key"); val != "" {
|
||||
query := fmt.Sprintf("PRAGMA key = \"%s\";", val)
|
||||
if err := exec(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// _pragma_cipher_page_size
|
||||
if val := params.Get("_pragma_cipher_page_size"); val != "" {
|
||||
pageSize, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sqlite3: _pragma_cipher_page_size cannot be parsed: %s", err)
|
||||
}
|
||||
query := fmt.Sprintf("PRAGMA cipher_page_size = %d;", pageSize)
|
||||
if err := exec(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
|
||||
|
||||
if d.ConnectHook != nil {
|
||||
if err := d.ConnectHook(conn); err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
runtime.SetFinalizer(conn, (*SQLiteConn).Close)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// Close the connection.
|
||||
func (c *SQLiteConn) Close() error {
|
||||
rv := C.sqlite3_close_v2(c.db)
|
||||
if rv != C.SQLITE_OK {
|
||||
return c.lastError()
|
||||
}
|
||||
c.db = nil
|
||||
runtime.SetFinalizer(c, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Prepare the query string. Return a new statement.
|
||||
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
|
||||
return c.prepare(context.Background(), query)
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
|
||||
pquery := C.CString(query)
|
||||
defer C.free(unsafe.Pointer(pquery))
|
||||
var s *C.sqlite3_stmt
|
||||
var tail *C.char
|
||||
rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
|
||||
if rv != C.SQLITE_OK {
|
||||
return nil, c.lastError()
|
||||
}
|
||||
var t string
|
||||
if tail != nil && *tail != '\000' {
|
||||
t = strings.TrimSpace(C.GoString(tail))
|
||||
}
|
||||
ss := &SQLiteStmt{c: c, s: s, t: t}
|
||||
runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
// Close the statement.
|
||||
func (s *SQLiteStmt) Close() error {
|
||||
if s.closed {
|
||||
return nil
|
||||
}
|
||||
s.closed = true
|
||||
if s.c == nil || s.c.db == nil {
|
||||
return errors.New("sqlite statement with already closed database connection")
|
||||
}
|
||||
rv := C.sqlite3_finalize(s.s)
|
||||
if rv != C.SQLITE_OK {
|
||||
return s.c.lastError()
|
||||
}
|
||||
runtime.SetFinalizer(s, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NumInput return a number of parameters.
|
||||
func (s *SQLiteStmt) NumInput() int {
|
||||
return int(C.sqlite3_bind_parameter_count(s.s))
|
||||
}
|
||||
|
||||
type bindArg struct {
|
||||
n int
|
||||
v driver.Value
|
||||
}
|
||||
|
||||
func (s *SQLiteStmt) bind(args []namedValue) error {
|
||||
rv := C.sqlite3_reset(s.s)
|
||||
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
|
||||
return s.c.lastError()
|
||||
}
|
||||
|
||||
for i, v := range args {
|
||||
if v.Name != "" {
|
||||
cname := C.CString(":" + v.Name)
|
||||
args[i].Ordinal = int(C.sqlite3_bind_parameter_index(s.s, cname))
|
||||
C.free(unsafe.Pointer(cname))
|
||||
}
|
||||
}
|
||||
|
||||
for _, arg := range args {
|
||||
n := C.int(arg.Ordinal)
|
||||
switch v := arg.Value.(type) {
|
||||
case nil:
|
||||
rv = C.sqlite3_bind_null(s.s, n)
|
||||
case string:
|
||||
if len(v) == 0 {
|
||||
b := []byte{0}
|
||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
|
||||
} else {
|
||||
b := []byte(v)
|
||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
||||
}
|
||||
case int64:
|
||||
rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
|
||||
case bool:
|
||||
if bool(v) {
|
||||
rv = C.sqlite3_bind_int(s.s, n, 1)
|
||||
} else {
|
||||
rv = C.sqlite3_bind_int(s.s, n, 0)
|
||||
}
|
||||
case float64:
|
||||
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
|
||||
case []byte:
|
||||
if len(v) == 0 {
|
||||
rv = C._sqlite3_bind_blob(s.s, n, nil, 0)
|
||||
} else {
|
||||
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(len(v)))
|
||||
}
|
||||
case time.Time:
|
||||
b := []byte(v.Format(SQLiteTimestampFormats[0]))
|
||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
||||
}
|
||||
if rv != C.SQLITE_OK {
|
||||
return s.c.lastError()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Query the statement with arguments. Return records.
|
||||
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, v := range args {
|
||||
list[i] = namedValue{
|
||||
Ordinal: i + 1,
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
return s.query(context.Background(), list)
|
||||
}
|
||||
|
||||
func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
|
||||
if err := s.bind(args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := &SQLiteRows{
|
||||
s: s,
|
||||
nc: int(C.sqlite3_column_count(s.s)),
|
||||
cols: nil,
|
||||
decltype: nil,
|
||||
cls: s.cls,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
go func(db *C.sqlite3) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
select {
|
||||
case <-rows.done:
|
||||
default:
|
||||
C.sqlite3_interrupt(db)
|
||||
rows.Close()
|
||||
}
|
||||
case <-rows.done:
|
||||
}
|
||||
}(s.c.db)
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// LastInsertId teturn last inserted ID.
|
||||
func (r *SQLiteResult) LastInsertId() (int64, error) {
|
||||
return r.id, nil
|
||||
}
|
||||
|
||||
// RowsAffected return how many rows affected.
|
||||
func (r *SQLiteResult) RowsAffected() (int64, error) {
|
||||
return r.changes, nil
|
||||
}
|
||||
|
||||
// Exec execute the statement with arguments. Return result object.
|
||||
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, v := range args {
|
||||
list[i] = namedValue{
|
||||
Ordinal: i + 1,
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
return s.exec(context.Background(), list)
|
||||
}
|
||||
|
||||
func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
|
||||
if err := s.bind(args); err != nil {
|
||||
C.sqlite3_reset(s.s)
|
||||
C.sqlite3_clear_bindings(s.s)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
go func(db *C.sqlite3) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
C.sqlite3_interrupt(db)
|
||||
case <-done:
|
||||
}
|
||||
}(s.c.db)
|
||||
|
||||
var rowid, changes C.longlong
|
||||
rv := C._sqlite3_step(s.s, &rowid, &changes)
|
||||
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
|
||||
err := s.c.lastError()
|
||||
C.sqlite3_reset(s.s)
|
||||
C.sqlite3_clear_bindings(s.s)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
|
||||
}
|
||||
|
||||
// Close the rows.
|
||||
func (rc *SQLiteRows) Close() error {
|
||||
if rc.s.closed {
|
||||
return nil
|
||||
}
|
||||
if rc.done != nil {
|
||||
close(rc.done)
|
||||
}
|
||||
if rc.cls {
|
||||
return rc.s.Close()
|
||||
}
|
||||
rv := C.sqlite3_reset(rc.s.s)
|
||||
if rv != C.SQLITE_OK {
|
||||
return rc.s.c.lastError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Columns return column names.
|
||||
func (rc *SQLiteRows) Columns() []string {
|
||||
if rc.nc != len(rc.cols) {
|
||||
rc.cols = make([]string, rc.nc)
|
||||
for i := 0; i < rc.nc; i++ {
|
||||
rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
|
||||
}
|
||||
}
|
||||
return rc.cols
|
||||
}
|
||||
|
||||
// DeclTypes return column types.
|
||||
func (rc *SQLiteRows) DeclTypes() []string {
|
||||
if rc.decltype == nil {
|
||||
rc.decltype = make([]string, rc.nc)
|
||||
for i := 0; i < rc.nc; i++ {
|
||||
rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
|
||||
}
|
||||
}
|
||||
return rc.decltype
|
||||
}
|
||||
|
||||
// Next move cursor to next.
|
||||
func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
||||
rv := C.sqlite3_step(rc.s.s)
|
||||
if rv == C.SQLITE_DONE {
|
||||
return io.EOF
|
||||
}
|
||||
if rv != C.SQLITE_ROW {
|
||||
rv = C.sqlite3_reset(rc.s.s)
|
||||
if rv != C.SQLITE_OK {
|
||||
return rc.s.c.lastError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
rc.DeclTypes()
|
||||
|
||||
for i := range dest {
|
||||
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
|
||||
case C.SQLITE_INTEGER:
|
||||
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
||||
switch rc.decltype[i] {
|
||||
case "timestamp", "datetime", "date":
|
||||
var t time.Time
|
||||
// Assume a millisecond unix timestamp if it's 13 digits -- too
|
||||
// large to be a reasonable timestamp in seconds.
|
||||
if val > 1e12 || val < -1e12 {
|
||||
val *= int64(time.Millisecond) // convert ms to nsec
|
||||
} else {
|
||||
val *= int64(time.Second) // convert sec to nsec
|
||||
}
|
||||
t = time.Unix(0, val).UTC()
|
||||
if rc.s.c.loc != nil {
|
||||
t = t.In(rc.s.c.loc)
|
||||
}
|
||||
dest[i] = t
|
||||
case "boolean":
|
||||
dest[i] = val > 0
|
||||
default:
|
||||
dest[i] = val
|
||||
}
|
||||
case C.SQLITE_FLOAT:
|
||||
dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
|
||||
case C.SQLITE_BLOB:
|
||||
p := C.sqlite3_column_blob(rc.s.s, C.int(i))
|
||||
if p == nil {
|
||||
dest[i] = nil
|
||||
continue
|
||||
}
|
||||
n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
|
||||
switch dest[i].(type) {
|
||||
case sql.RawBytes:
|
||||
dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
|
||||
default:
|
||||
slice := make([]byte, n)
|
||||
copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n])
|
||||
dest[i] = slice
|
||||
}
|
||||
case C.SQLITE_NULL:
|
||||
dest[i] = nil
|
||||
case C.SQLITE_TEXT:
|
||||
var err error
|
||||
var timeVal time.Time
|
||||
|
||||
n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
|
||||
s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
|
||||
|
||||
switch rc.decltype[i] {
|
||||
case "timestamp", "datetime", "date":
|
||||
var t time.Time
|
||||
s = strings.TrimSuffix(s, "Z")
|
||||
for _, format := range SQLiteTimestampFormats {
|
||||
if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
|
||||
t = timeVal
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
// The column is a time value, so return the zero time on parse failure.
|
||||
t = time.Time{}
|
||||
}
|
||||
if rc.s.c.loc != nil {
|
||||
t = t.In(rc.s.c.loc)
|
||||
}
|
||||
dest[i] = t
|
||||
default:
|
||||
dest[i] = []byte(s)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -1,136 +0,0 @@
|
|||
/* This is the build config file.
|
||||
*
|
||||
* With this you can setup what to inlcude/exclude automatically during any build. Just comment
|
||||
* out the line that #define's the word for the thing you want to remove. phew!
|
||||
*/
|
||||
|
||||
#ifndef TOMCRYPT_CFG_H
|
||||
#define TOMCRYPT_CFG_H
|
||||
|
||||
#if defined(_WIN32) || defined(_MSC_VER)
|
||||
#define LTC_CALL __cdecl
|
||||
#else
|
||||
#ifndef LTC_CALL
|
||||
#define LTC_CALL
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef LTC_EXPORT
|
||||
#define LTC_EXPORT
|
||||
#endif
|
||||
|
||||
/* certain platforms use macros for these, making the prototypes broken */
|
||||
#ifndef LTC_NO_PROTOTYPES
|
||||
|
||||
/* you can change how memory allocation works ... */
|
||||
LTC_EXPORT void * LTC_CALL XMALLOC(size_t n);
|
||||
LTC_EXPORT void * LTC_CALL XREALLOC(void *p, size_t n);
|
||||
LTC_EXPORT void * LTC_CALL XCALLOC(size_t n, size_t s);
|
||||
LTC_EXPORT void LTC_CALL XFREE(void *p);
|
||||
|
||||
LTC_EXPORT void LTC_CALL XQSORT(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
|
||||
|
||||
|
||||
/* change the clock function too */
|
||||
LTC_EXPORT clock_t LTC_CALL XCLOCK(void);
|
||||
|
||||
/* various other functions */
|
||||
LTC_EXPORT void * LTC_CALL XMEMCPY(void *dest, const void *src, size_t n);
|
||||
LTC_EXPORT int LTC_CALL XMEMCMP(const void *s1, const void *s2, size_t n);
|
||||
LTC_EXPORT void * LTC_CALL XMEMSET(void *s, int c, size_t n);
|
||||
|
||||
LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
|
||||
|
||||
#endif
|
||||
|
||||
/* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */
|
||||
#ifndef ARGTYPE
|
||||
#define ARGTYPE 0
|
||||
#endif
|
||||
|
||||
/* Controls endianess and size of registers. Leave uncommented to get platform neutral [slower] code
|
||||
*
|
||||
* Note: in order to use the optimized macros your platform must support unaligned 32 and 64 bit read/writes.
|
||||
* The x86 platforms allow this but some others [ARM for instance] do not. On those platforms you **MUST**
|
||||
* use the portable [slower] macros.
|
||||
*/
|
||||
|
||||
/* detect x86-32 machines somewhat */
|
||||
#if !defined(__STRICT_ANSI__) && (defined(INTEL_CC) || (defined(_MSC_VER) && defined(WIN32)) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__))))
|
||||
#define ENDIAN_LITTLE
|
||||
#define ENDIAN_32BITWORD
|
||||
#define LTC_FAST
|
||||
#define LTC_FAST_TYPE unsigned long
|
||||
#endif
|
||||
|
||||
/* detects MIPS R5900 processors (PS2) */
|
||||
#if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips))
|
||||
#define ENDIAN_LITTLE
|
||||
#define ENDIAN_64BITWORD
|
||||
#endif
|
||||
|
||||
/* detect amd64 */
|
||||
#if !defined(__STRICT_ANSI__) && defined(__x86_64__)
|
||||
#define ENDIAN_LITTLE
|
||||
#define ENDIAN_64BITWORD
|
||||
#define LTC_FAST
|
||||
#define LTC_FAST_TYPE unsigned long
|
||||
#endif
|
||||
|
||||
/* detect PPC32 */
|
||||
#if !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
|
||||
#define ENDIAN_BIG
|
||||
#define ENDIAN_32BITWORD
|
||||
#define LTC_FAST
|
||||
#define LTC_FAST_TYPE unsigned long
|
||||
#endif
|
||||
|
||||
/* detect sparc and sparc64 */
|
||||
#if defined(__sparc__)
|
||||
#define ENDIAN_BIG
|
||||
#if defined(__arch64__)
|
||||
#define ENDIAN_64BITWORD
|
||||
#else
|
||||
#define ENDIAN_32BITWORD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef LTC_NO_FAST
|
||||
#ifdef LTC_FAST
|
||||
#undef LTC_FAST
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* No asm is a quick way to disable anything "not portable" */
|
||||
#ifdef LTC_NO_ASM
|
||||
#undef ENDIAN_LITTLE
|
||||
#undef ENDIAN_BIG
|
||||
#undef ENDIAN_32BITWORD
|
||||
#undef ENDIAN_64BITWORD
|
||||
#undef LTC_FAST
|
||||
#undef LTC_FAST_TYPE
|
||||
#define LTC_NO_ROLC
|
||||
#define LTC_NO_BSWAP
|
||||
#endif
|
||||
|
||||
/* #define ENDIAN_LITTLE */
|
||||
/* #define ENDIAN_BIG */
|
||||
|
||||
/* #define ENDIAN_32BITWORD */
|
||||
/* #define ENDIAN_64BITWORD */
|
||||
|
||||
#if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
|
||||
#error You must specify a word size as well as endianess in tomcrypt_cfg.h
|
||||
#endif
|
||||
|
||||
#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
|
||||
#define ENDIAN_NEUTRAL
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,435 +0,0 @@
|
|||
#ifndef TOMCRYPT_CUSTOM_H_
|
||||
#define TOMCRYPT_CUSTOM_H_
|
||||
|
||||
/* macros for various libc functions you can change for embedded targets */
|
||||
#ifndef XMALLOC
|
||||
#ifdef malloc
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XMALLOC malloc
|
||||
#endif
|
||||
#ifndef XREALLOC
|
||||
#ifdef realloc
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XREALLOC realloc
|
||||
#endif
|
||||
#ifndef XCALLOC
|
||||
#ifdef calloc
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XCALLOC calloc
|
||||
#endif
|
||||
#ifndef XFREE
|
||||
#ifdef free
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XFREE free
|
||||
#endif
|
||||
|
||||
#ifndef XMEMSET
|
||||
#ifdef memset
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XMEMSET memset
|
||||
#endif
|
||||
#ifndef XMEMCPY
|
||||
#ifdef memcpy
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XMEMCPY memcpy
|
||||
#endif
|
||||
#ifndef XMEMCMP
|
||||
#ifdef memcmp
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XMEMCMP memcmp
|
||||
#endif
|
||||
#ifndef XSTRCMP
|
||||
#ifdef strcmp
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XSTRCMP strcmp
|
||||
#endif
|
||||
|
||||
#ifndef XCLOCK
|
||||
#define XCLOCK clock
|
||||
#endif
|
||||
#ifndef XCLOCKS_PER_SEC
|
||||
#define XCLOCKS_PER_SEC CLOCKS_PER_SEC
|
||||
#endif
|
||||
|
||||
#ifndef XQSORT
|
||||
#ifdef qsort
|
||||
#define LTC_NO_PROTOTYPES
|
||||
#endif
|
||||
#define XQSORT qsort
|
||||
#endif
|
||||
|
||||
/* Easy button? */
|
||||
#ifdef LTC_EASY
|
||||
#define LTC_NO_CIPHERS
|
||||
#define LTC_RIJNDAEL
|
||||
#define LTC_BLOWFISH
|
||||
#define LTC_DES
|
||||
#define LTC_CAST5
|
||||
|
||||
#define LTC_NO_MODES
|
||||
#define LTC_ECB_MODE
|
||||
#define LTC_CBC_MODE
|
||||
#define LTC_CTR_MODE
|
||||
|
||||
#define LTC_NO_HASHES
|
||||
#define LTC_SHA1
|
||||
#define LTC_SHA512
|
||||
#define LTC_SHA384
|
||||
#define LTC_SHA256
|
||||
#define LTC_SHA224
|
||||
|
||||
#define LTC_NO_MACS
|
||||
#define LTC_HMAC
|
||||
#define LTC_OMAC
|
||||
#define LTC_CCM_MODE
|
||||
|
||||
#define LTC_NO_PRNGS
|
||||
#define LTC_SPRNG
|
||||
#define LTC_YARROW
|
||||
#define LTC_DEVRANDOM
|
||||
#define TRY_URANDOM_FIRST
|
||||
|
||||
#define LTC_NO_PK
|
||||
#define LTC_MRSA
|
||||
#define LTC_MECC
|
||||
#endif
|
||||
|
||||
/* Use small code where possible */
|
||||
/* #define LTC_SMALL_CODE */
|
||||
|
||||
/* Enable self-test test vector checking */
|
||||
#ifndef LTC_NO_TEST
|
||||
#define LTC_TEST
|
||||
#endif
|
||||
|
||||
/* clean the stack of functions which put private information on stack */
|
||||
#define LTC_CLEAN_STACK
|
||||
|
||||
/* disable all file related functions */
|
||||
/* #define LTC_NO_FILE */
|
||||
|
||||
/* disable all forms of ASM */
|
||||
/* #define LTC_NO_ASM */
|
||||
|
||||
/* disable FAST mode */
|
||||
/* #define LTC_NO_FAST */
|
||||
|
||||
/* disable BSWAP on x86 */
|
||||
/* #define LTC_NO_BSWAP */
|
||||
|
||||
/* ---> Symmetric Block Ciphers <--- */
|
||||
#ifndef LTC_NO_CIPHERS
|
||||
|
||||
#define LTC_BLOWFISH
|
||||
#define LTC_RC2
|
||||
#define LTC_RC5
|
||||
#define LTC_RC6
|
||||
#define LTC_SAFERP
|
||||
#define LTC_RIJNDAEL
|
||||
#define LTC_XTEA
|
||||
/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format
|
||||
* (saves 4KB of ram), _ALL_TABLES enables all tables during setup */
|
||||
#define LTC_TWOFISH
|
||||
#ifndef LTC_NO_TABLES
|
||||
#define LTC_TWOFISH_TABLES
|
||||
/* #define LTC_TWOFISH_ALL_TABLES */
|
||||
#else
|
||||
#define LTC_TWOFISH_SMALL
|
||||
#endif
|
||||
/* #define LTC_TWOFISH_SMALL */
|
||||
/* LTC_DES includes EDE triple-LTC_DES */
|
||||
#define LTC_DES
|
||||
#define LTC_CAST5
|
||||
#define LTC_NOEKEON
|
||||
#define LTC_SKIPJACK
|
||||
#define LTC_SAFER
|
||||
#define LTC_KHAZAD
|
||||
#define LTC_ANUBIS
|
||||
#define LTC_ANUBIS_TWEAK
|
||||
#define LTC_KSEED
|
||||
#define LTC_KASUMI
|
||||
#define LTC_MULTI2
|
||||
#define LTC_CAMELLIA
|
||||
|
||||
#endif /* LTC_NO_CIPHERS */
|
||||
|
||||
|
||||
/* ---> Block Cipher Modes of Operation <--- */
|
||||
#ifndef LTC_NO_MODES
|
||||
|
||||
#define LTC_CFB_MODE
|
||||
#define LTC_OFB_MODE
|
||||
#define LTC_ECB_MODE
|
||||
#define LTC_CBC_MODE
|
||||
#define LTC_CTR_MODE
|
||||
|
||||
/* F8 chaining mode */
|
||||
#define LTC_F8_MODE
|
||||
|
||||
/* LRW mode */
|
||||
#define LTC_LRW_MODE
|
||||
#ifndef LTC_NO_TABLES
|
||||
/* like GCM mode this will enable 16 8x128 tables [64KB] that make
|
||||
* seeking very fast.
|
||||
*/
|
||||
#define LRW_TABLES
|
||||
#endif
|
||||
|
||||
/* XTS mode */
|
||||
#define LTC_XTS_MODE
|
||||
|
||||
#endif /* LTC_NO_MODES */
|
||||
|
||||
/* ---> One-Way Hash Functions <--- */
|
||||
#ifndef LTC_NO_HASHES
|
||||
|
||||
#define LTC_CHC_HASH
|
||||
#define LTC_WHIRLPOOL
|
||||
#define LTC_SHA512
|
||||
#define LTC_SHA384
|
||||
#define LTC_SHA256
|
||||
#define LTC_TIGER
|
||||
#define LTC_SHA1
|
||||
#define LTC_MD5
|
||||
#define LTC_MD4
|
||||
#define LTC_MD2
|
||||
#define LTC_RIPEMD128
|
||||
#define LTC_RIPEMD160
|
||||
#define LTC_RIPEMD256
|
||||
#define LTC_RIPEMD320
|
||||
|
||||
#endif /* LTC_NO_HASHES */
|
||||
|
||||
/* ---> MAC functions <--- */
|
||||
#ifndef LTC_NO_MACS
|
||||
|
||||
#define LTC_HMAC
|
||||
#define LTC_OMAC
|
||||
#define LTC_PMAC
|
||||
#define LTC_XCBC
|
||||
#define LTC_F9_MODE
|
||||
#define LTC_PELICAN
|
||||
|
||||
#if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL)
|
||||
#error Pelican-MAC requires LTC_RIJNDAEL
|
||||
#endif
|
||||
|
||||
/* ---> Encrypt + Authenticate Modes <--- */
|
||||
|
||||
#define LTC_EAX_MODE
|
||||
#if defined(LTC_EAX_MODE) && !(defined(LTC_CTR_MODE) && defined(LTC_OMAC))
|
||||
#error LTC_EAX_MODE requires CTR and LTC_OMAC mode
|
||||
#endif
|
||||
|
||||
#define LTC_OCB_MODE
|
||||
#define LTC_CCM_MODE
|
||||
#define LTC_GCM_MODE
|
||||
|
||||
/* Use 64KiB tables */
|
||||
#ifndef LTC_NO_TABLES
|
||||
#define LTC_GCM_TABLES
|
||||
#endif
|
||||
|
||||
/* USE SSE2? requires GCC works on x86_32 and x86_64*/
|
||||
#ifdef LTC_GCM_TABLES
|
||||
/* #define LTC_GCM_TABLES_SSE2 */
|
||||
#endif
|
||||
|
||||
#endif /* LTC_NO_MACS */
|
||||
|
||||
/* Various tidbits of modern neatoness */
|
||||
#define LTC_BASE64
|
||||
|
||||
/* --> Pseudo Random Number Generators <--- */
|
||||
#ifndef LTC_NO_PRNGS
|
||||
|
||||
/* Yarrow */
|
||||
#define LTC_YARROW
|
||||
/* which descriptor of AES to use? */
|
||||
/* 0 = rijndael_enc 1 = aes_enc, 2 = rijndael [full], 3 = aes [full] */
|
||||
#define LTC_YARROW_AES 0
|
||||
|
||||
#if defined(LTC_YARROW) && !defined(LTC_CTR_MODE)
|
||||
#error LTC_YARROW requires LTC_CTR_MODE chaining mode to be defined!
|
||||
#endif
|
||||
|
||||
/* a PRNG that simply reads from an available system source */
|
||||
#define LTC_SPRNG
|
||||
|
||||
/* The LTC_RC4 stream cipher */
|
||||
#define LTC_RC4
|
||||
|
||||
/* Fortuna PRNG */
|
||||
#define LTC_FORTUNA
|
||||
/* reseed every N calls to the read function */
|
||||
#define LTC_FORTUNA_WD 10
|
||||
/* number of pools (4..32) can save a bit of ram by lowering the count */
|
||||
#define LTC_FORTUNA_POOLS 32
|
||||
|
||||
/* Greg's LTC_SOBER128 PRNG ;-0 */
|
||||
#define LTC_SOBER128
|
||||
|
||||
/* the *nix style /dev/random device */
|
||||
#define LTC_DEVRANDOM
|
||||
/* try /dev/urandom before trying /dev/random */
|
||||
#define TRY_URANDOM_FIRST
|
||||
|
||||
#endif /* LTC_NO_PRNGS */
|
||||
|
||||
/* ---> math provider? <--- */
|
||||
#ifndef LTC_NO_MATH
|
||||
|
||||
/* LibTomMath */
|
||||
/* #define LTM_DESC */
|
||||
|
||||
/* TomsFastMath */
|
||||
/* #define TFM_DESC */
|
||||
|
||||
#endif /* LTC_NO_MATH */
|
||||
|
||||
/* ---> Public Key Crypto <--- */
|
||||
#ifndef LTC_NO_PK
|
||||
|
||||
/* Include RSA support */
|
||||
#define LTC_MRSA
|
||||
|
||||
/* Enable RSA blinding when doing private key operations? */
|
||||
/* #define LTC_RSA_BLINDING */
|
||||
|
||||
/* Include Diffie-Hellman support */
|
||||
#ifndef GPM_DESC
|
||||
/* is_prime fails for GPM */
|
||||
#define MDH
|
||||
/* Supported Key Sizes */
|
||||
#define DH768
|
||||
#define DH1024
|
||||
#define DH1280
|
||||
#define DH1536
|
||||
#define DH1792
|
||||
#define DH2048
|
||||
|
||||
#ifndef TFM_DESC
|
||||
/* tfm has a problem in fp_isprime for larger key sizes */
|
||||
#define DH2560
|
||||
#define DH3072
|
||||
#define DH4096
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Include Katja (a Rabin variant like RSA) */
|
||||
/* #define MKAT */
|
||||
|
||||
/* Digital Signature Algorithm */
|
||||
#define LTC_MDSA
|
||||
|
||||
/* ECC */
|
||||
#define LTC_MECC
|
||||
|
||||
/* use Shamir's trick for point mul (speeds up signature verification) */
|
||||
#define LTC_ECC_SHAMIR
|
||||
|
||||
#if defined(TFM_LTC_DESC) && defined(LTC_MECC)
|
||||
#define LTC_MECC_ACCEL
|
||||
#endif
|
||||
|
||||
/* do we want fixed point ECC */
|
||||
/* #define LTC_MECC_FP */
|
||||
|
||||
/* Timing Resistant? */
|
||||
/* #define LTC_ECC_TIMING_RESISTANT */
|
||||
|
||||
#endif /* LTC_NO_PK */
|
||||
|
||||
/* LTC_PKCS #1 (RSA) and #5 (Password Handling) stuff */
|
||||
#ifndef LTC_NO_PKCS
|
||||
|
||||
#define LTC_PKCS_1
|
||||
#define LTC_PKCS_5
|
||||
|
||||
/* Include ASN.1 DER (required by DSA/RSA) */
|
||||
#define LTC_DER
|
||||
|
||||
#endif /* LTC_NO_PKCS */
|
||||
|
||||
/* cleanup */
|
||||
|
||||
#ifdef LTC_MECC
|
||||
/* Supported ECC Key Sizes */
|
||||
#ifndef LTC_NO_CURVES
|
||||
#define ECC112
|
||||
#define ECC128
|
||||
#define ECC160
|
||||
#define ECC192
|
||||
#define ECC224
|
||||
#define ECC256
|
||||
#define ECC384
|
||||
#define ECC521
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(MKATJA)
|
||||
/* Include the MPI functionality? (required by the PK algorithms) */
|
||||
#define MPI
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MRSA
|
||||
#define LTC_PKCS_1
|
||||
#endif
|
||||
|
||||
#if defined(TFM_DESC) && defined(LTC_RSA_BLINDING)
|
||||
#warning RSA blinding currently not supported in combination with TFM
|
||||
#undef LTC_RSA_BLINDING
|
||||
#endif
|
||||
|
||||
#if defined(LTC_DER) && !defined(MPI)
|
||||
#error ASN.1 DER requires MPI functionality
|
||||
#endif
|
||||
|
||||
#if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(MKATJA)) && !defined(LTC_DER)
|
||||
#error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
|
||||
#endif
|
||||
|
||||
/* THREAD management */
|
||||
#ifdef LTC_PTHREAD
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#define LTC_MUTEX_GLOBAL(x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define LTC_MUTEX_PROTO(x) extern pthread_mutex_t x;
|
||||
#define LTC_MUTEX_TYPE(x) pthread_mutex_t x;
|
||||
#define LTC_MUTEX_INIT(x) pthread_mutex_init(x, NULL);
|
||||
#define LTC_MUTEX_LOCK(x) pthread_mutex_lock(x);
|
||||
#define LTC_MUTEX_UNLOCK(x) pthread_mutex_unlock(x);
|
||||
|
||||
#else
|
||||
|
||||
/* default no functions */
|
||||
#define LTC_MUTEX_GLOBAL(x)
|
||||
#define LTC_MUTEX_PROTO(x)
|
||||
#define LTC_MUTEX_TYPE(x)
|
||||
#define LTC_MUTEX_INIT(x)
|
||||
#define LTC_MUTEX_LOCK(x)
|
||||
#define LTC_MUTEX_UNLOCK(x)
|
||||
|
||||
#endif
|
||||
|
||||
/* Debuggers */
|
||||
|
||||
/* define this if you use Valgrind, note: it CHANGES the way SOBER-128 and LTC_RC4 work (see the code) */
|
||||
/* #define LTC_VALGRIND */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,23 +0,0 @@
|
|||
/* ---- LTC_BASE64 Routines ---- */
|
||||
#ifdef LTC_BASE64
|
||||
int base64_encode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int base64_decode(const unsigned char *in, unsigned long len,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
/* ---- MEM routines ---- */
|
||||
void zeromem(void *dst, size_t len);
|
||||
void burn_stack(unsigned long len);
|
||||
|
||||
const char *error_to_string(int err);
|
||||
|
||||
extern const char *crypt_build_settings;
|
||||
|
||||
/* ---- HMM ---- */
|
||||
int crypt_fsa(void *mp, ...);
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,622 +0,0 @@
|
|||
/* ---- NUMBER THEORY ---- */
|
||||
|
||||
enum {
|
||||
PK_PUBLIC=0,
|
||||
PK_PRIVATE=1
|
||||
};
|
||||
|
||||
int rand_prime(void *N, long len, prng_state *prng, int wprng);
|
||||
|
||||
enum {
|
||||
PKA_RSA,
|
||||
PKA_DSA
|
||||
};
|
||||
|
||||
typedef struct Oid {
|
||||
unsigned long OID[16];
|
||||
/** Length of DER encoding */
|
||||
unsigned long OIDlen;
|
||||
} oid_st;
|
||||
|
||||
int pk_get_oid(int pk, oid_st *st);
|
||||
|
||||
/* ---- RSA ---- */
|
||||
#ifdef LTC_MRSA
|
||||
|
||||
/* Min and Max RSA key sizes (in bits) */
|
||||
#define MIN_RSA_SIZE 1024
|
||||
#define MAX_RSA_SIZE 4096
|
||||
|
||||
/** RSA LTC_PKCS style key */
|
||||
typedef struct Rsa_key {
|
||||
/** Type of key, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
/** The public exponent */
|
||||
void *e;
|
||||
/** The private exponent */
|
||||
void *d;
|
||||
/** The modulus */
|
||||
void *N;
|
||||
/** The p factor of N */
|
||||
void *p;
|
||||
/** The q factor of N */
|
||||
void *q;
|
||||
/** The 1/q mod p CRT param */
|
||||
void *qP;
|
||||
/** The d mod (p - 1) CRT param */
|
||||
void *dP;
|
||||
/** The d mod (q - 1) CRT param */
|
||||
void *dQ;
|
||||
} rsa_key;
|
||||
|
||||
int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
|
||||
|
||||
int rsa_exptmod(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int which,
|
||||
rsa_key *key);
|
||||
|
||||
void rsa_free(rsa_key *key);
|
||||
|
||||
/* These use LTC_PKCS #1 v2.0 padding */
|
||||
#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
|
||||
rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
|
||||
|
||||
#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
|
||||
rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
|
||||
|
||||
#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
|
||||
rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
|
||||
|
||||
#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
|
||||
rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
|
||||
|
||||
/* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */
|
||||
int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
|
||||
|
||||
int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
int hash_idx, int padding,
|
||||
int *stat, rsa_key *key);
|
||||
|
||||
int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
int padding,
|
||||
prng_state *prng, int prng_idx,
|
||||
int hash_idx, unsigned long saltlen,
|
||||
rsa_key *key);
|
||||
|
||||
int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int padding,
|
||||
int hash_idx, unsigned long saltlen,
|
||||
int *stat, rsa_key *key);
|
||||
|
||||
/* LTC_PKCS #1 import/export */
|
||||
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
|
||||
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
|
||||
|
||||
#endif
|
||||
|
||||
/* ---- Katja ---- */
|
||||
#ifdef MKAT
|
||||
|
||||
/* Min and Max KAT key sizes (in bits) */
|
||||
#define MIN_KAT_SIZE 1024
|
||||
#define MAX_KAT_SIZE 4096
|
||||
|
||||
/** Katja LTC_PKCS style key */
|
||||
typedef struct KAT_key {
|
||||
/** Type of key, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
/** The private exponent */
|
||||
void *d;
|
||||
/** The modulus */
|
||||
void *N;
|
||||
/** The p factor of N */
|
||||
void *p;
|
||||
/** The q factor of N */
|
||||
void *q;
|
||||
/** The 1/q mod p CRT param */
|
||||
void *qP;
|
||||
/** The d mod (p - 1) CRT param */
|
||||
void *dP;
|
||||
/** The d mod (q - 1) CRT param */
|
||||
void *dQ;
|
||||
/** The pq param */
|
||||
void *pq;
|
||||
} katja_key;
|
||||
|
||||
int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
|
||||
|
||||
int katja_exptmod(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int which,
|
||||
katja_key *key);
|
||||
|
||||
void katja_free(katja_key *key);
|
||||
|
||||
/* These use LTC_PKCS #1 v2.0 padding */
|
||||
int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
|
||||
|
||||
int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
const unsigned char *lparam, unsigned long lparamlen,
|
||||
int hash_idx, int *stat,
|
||||
katja_key *key);
|
||||
|
||||
/* LTC_PKCS #1 import/export */
|
||||
int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
|
||||
int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
|
||||
|
||||
#endif
|
||||
|
||||
/* ---- DH Routines ---- */
|
||||
#ifdef MDH
|
||||
|
||||
typedef struct Dh_key {
|
||||
int idx, type;
|
||||
void *x;
|
||||
void *y;
|
||||
} dh_key;
|
||||
|
||||
int dh_compat_test(void);
|
||||
void dh_sizes(int *low, int *high);
|
||||
int dh_get_size(dh_key *key);
|
||||
|
||||
int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
|
||||
void dh_free(dh_key *key);
|
||||
|
||||
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
|
||||
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
|
||||
|
||||
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int dh_encrypt_key(const unsigned char *in, unsigned long keylen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
dh_key *key);
|
||||
|
||||
int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
dh_key *key);
|
||||
|
||||
int dh_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, dh_key *key);
|
||||
|
||||
int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dh_key *key);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ---- ECC Routines ---- */
|
||||
#ifdef LTC_MECC
|
||||
|
||||
/* size of our temp buffers for exported keys */
|
||||
#define ECC_BUF_SIZE 256
|
||||
|
||||
/* max private key size */
|
||||
#define ECC_MAXSIZE 66
|
||||
|
||||
/** Structure defines a NIST GF(p) curve */
|
||||
typedef struct {
|
||||
/** The size of the curve in octets */
|
||||
int size;
|
||||
|
||||
/** name of curve */
|
||||
char *name;
|
||||
|
||||
/** The prime that defines the field the curve is in (encoded in hex) */
|
||||
char *prime;
|
||||
|
||||
/** The fields B param (hex) */
|
||||
char *B;
|
||||
|
||||
/** The order of the curve (hex) */
|
||||
char *order;
|
||||
|
||||
/** The x co-ordinate of the base point on the curve (hex) */
|
||||
char *Gx;
|
||||
|
||||
/** The y co-ordinate of the base point on the curve (hex) */
|
||||
char *Gy;
|
||||
} ltc_ecc_set_type;
|
||||
|
||||
/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
|
||||
typedef struct {
|
||||
/** The x co-ordinate */
|
||||
void *x;
|
||||
|
||||
/** The y co-ordinate */
|
||||
void *y;
|
||||
|
||||
/** The z co-ordinate */
|
||||
void *z;
|
||||
} ecc_point;
|
||||
|
||||
/** An ECC key */
|
||||
typedef struct {
|
||||
/** Type of key, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
|
||||
/** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
|
||||
int idx;
|
||||
|
||||
/** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
|
||||
const ltc_ecc_set_type *dp;
|
||||
|
||||
/** The public key */
|
||||
ecc_point pubkey;
|
||||
|
||||
/** The private key */
|
||||
void *k;
|
||||
} ecc_key;
|
||||
|
||||
/** the ECC params provided */
|
||||
extern const ltc_ecc_set_type ltc_ecc_sets[];
|
||||
|
||||
int ecc_test(void);
|
||||
void ecc_sizes(int *low, int *high);
|
||||
int ecc_get_size(ecc_key *key);
|
||||
|
||||
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
|
||||
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
|
||||
void ecc_free(ecc_key *key);
|
||||
|
||||
int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
|
||||
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
|
||||
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
|
||||
|
||||
int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
|
||||
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
|
||||
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
|
||||
|
||||
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
ecc_key *key);
|
||||
|
||||
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
ecc_key *key);
|
||||
|
||||
int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, ecc_key *key);
|
||||
|
||||
int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, ecc_key *key);
|
||||
|
||||
/* low level functions */
|
||||
ecc_point *ltc_ecc_new_point(void);
|
||||
void ltc_ecc_del_point(ecc_point *p);
|
||||
int ltc_ecc_is_valid_idx(int n);
|
||||
|
||||
/* point ops (mp == montgomery digit) */
|
||||
#if !defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC) || defined(GMP_LTC_DESC)
|
||||
/* R = 2P */
|
||||
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
|
||||
|
||||
/* R = P + Q */
|
||||
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
|
||||
#endif
|
||||
|
||||
#if defined(LTC_MECC_FP)
|
||||
/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
|
||||
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
|
||||
|
||||
/* functions for saving/loading/freeing/adding to fixed point cache */
|
||||
int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
|
||||
int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
|
||||
void ltc_ecc_fp_free(void);
|
||||
int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
|
||||
|
||||
/* lock/unlock all points currently in fixed point cache */
|
||||
void ltc_ecc_fp_tablelock(int lock);
|
||||
#endif
|
||||
|
||||
/* R = kG */
|
||||
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
|
||||
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
/* kA*A + kB*B = C */
|
||||
int ltc_ecc_mul2add(ecc_point *A, void *kA,
|
||||
ecc_point *B, void *kB,
|
||||
ecc_point *C,
|
||||
void *modulus);
|
||||
|
||||
#ifdef LTC_MECC_FP
|
||||
/* Shamir's trick with optimized point multiplication using fixed point cache */
|
||||
int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
|
||||
ecc_point *B, void *kB,
|
||||
ecc_point *C, void *modulus);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* map P to affine from projective */
|
||||
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_MDSA
|
||||
|
||||
/* Max diff between group and modulus size in bytes */
|
||||
#define LTC_MDSA_DELTA 512
|
||||
|
||||
/* Max DSA group size in bytes (default allows 4k-bit groups) */
|
||||
#define LTC_MDSA_MAX_GROUP 512
|
||||
|
||||
/** DSA key structure */
|
||||
typedef struct {
|
||||
/** The key type, PK_PRIVATE or PK_PUBLIC */
|
||||
int type;
|
||||
|
||||
/** The order of the sub-group used in octets */
|
||||
int qord;
|
||||
|
||||
/** The generator */
|
||||
void *g;
|
||||
|
||||
/** The prime used to generate the sub-group */
|
||||
void *q;
|
||||
|
||||
/** The large prime that generats the field the contains the sub-group */
|
||||
void *p;
|
||||
|
||||
/** The private key */
|
||||
void *x;
|
||||
|
||||
/** The public key */
|
||||
void *y;
|
||||
} dsa_key;
|
||||
|
||||
int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
|
||||
void dsa_free(dsa_key *key);
|
||||
|
||||
int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
|
||||
void *r, void *s,
|
||||
prng_state *prng, int wprng, dsa_key *key);
|
||||
|
||||
int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, dsa_key *key);
|
||||
|
||||
int dsa_verify_hash_raw( void *r, void *s,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dsa_key *key);
|
||||
|
||||
int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
|
||||
const unsigned char *hash, unsigned long hashlen,
|
||||
int *stat, dsa_key *key);
|
||||
|
||||
int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
prng_state *prng, int wprng, int hash,
|
||||
dsa_key *key);
|
||||
|
||||
int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen,
|
||||
dsa_key *key);
|
||||
|
||||
int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
|
||||
int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
|
||||
int dsa_verify_key(dsa_key *key, int *stat);
|
||||
|
||||
int dsa_shared_secret(void *private_key, void *base,
|
||||
dsa_key *public_key,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
#endif
|
||||
|
||||
#ifdef LTC_DER
|
||||
/* DER handling */
|
||||
|
||||
enum {
|
||||
LTC_ASN1_EOL,
|
||||
LTC_ASN1_BOOLEAN,
|
||||
LTC_ASN1_INTEGER,
|
||||
LTC_ASN1_SHORT_INTEGER,
|
||||
LTC_ASN1_BIT_STRING,
|
||||
LTC_ASN1_OCTET_STRING,
|
||||
LTC_ASN1_NULL,
|
||||
LTC_ASN1_OBJECT_IDENTIFIER,
|
||||
LTC_ASN1_IA5_STRING,
|
||||
LTC_ASN1_PRINTABLE_STRING,
|
||||
LTC_ASN1_UTF8_STRING,
|
||||
LTC_ASN1_UTCTIME,
|
||||
LTC_ASN1_CHOICE,
|
||||
LTC_ASN1_SEQUENCE,
|
||||
LTC_ASN1_SET,
|
||||
LTC_ASN1_SETOF,
|
||||
LTC_ASN1_RAW_BIT_STRING,
|
||||
};
|
||||
|
||||
/** A LTC ASN.1 list type */
|
||||
typedef struct ltc_asn1_list_ {
|
||||
/** The LTC ASN.1 enumerated type identifier */
|
||||
int type;
|
||||
/** The data to encode or place for decoding */
|
||||
void *data;
|
||||
/** The size of the input or resulting output */
|
||||
unsigned long size;
|
||||
/** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
|
||||
int used;
|
||||
/** prev/next entry in the list */
|
||||
struct ltc_asn1_list_ *prev, *next, *child, *parent;
|
||||
} ltc_asn1_list;
|
||||
|
||||
#define LTC_SET_ASN1(list, index, Type, Data, Size) \
|
||||
do { \
|
||||
int LTC_MACRO_temp = (index); \
|
||||
ltc_asn1_list *LTC_MACRO_list = (list); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
|
||||
LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
|
||||
} while (0);
|
||||
|
||||
/* SEQUENCE */
|
||||
int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen, int type_of);
|
||||
|
||||
#define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
|
||||
|
||||
int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
|
||||
ltc_asn1_list *list, unsigned long outlen, int ordered);
|
||||
|
||||
#define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
|
||||
|
||||
int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned long *outlen);
|
||||
|
||||
/* SUBJECT PUBLIC KEY INFO */
|
||||
int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long public_key_len,
|
||||
unsigned long parameters_type, void* parameters, unsigned long parameters_len);
|
||||
|
||||
int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
|
||||
unsigned int algorithm, void* public_key, unsigned long* public_key_len,
|
||||
unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
|
||||
|
||||
/* SET */
|
||||
#define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
|
||||
#define der_length_set der_length_sequence
|
||||
int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
/* VA list handy helpers with triplets of <type, size, data> */
|
||||
int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
|
||||
int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
|
||||
|
||||
/* FLEXI DECODER handle unknown list decoder */
|
||||
int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
|
||||
void der_free_sequence_flexi(ltc_asn1_list *list);
|
||||
void der_sequence_free(ltc_asn1_list *in);
|
||||
|
||||
/* BOOLEAN */
|
||||
int der_length_boolean(unsigned long *outlen);
|
||||
int der_encode_boolean(int in,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_boolean(const unsigned char *in, unsigned long inlen,
|
||||
int *out);
|
||||
/* INTEGER */
|
||||
int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
|
||||
int der_length_integer(void *num, unsigned long *len);
|
||||
|
||||
/* INTEGER -- handy for 0..2^32-1 values */
|
||||
int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
|
||||
int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
|
||||
int der_length_short_integer(unsigned long num, unsigned long *outlen);
|
||||
|
||||
/* BIT STRING */
|
||||
int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
|
||||
|
||||
/* OCTET STRING */
|
||||
int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
/* OBJECT IDENTIFIER */
|
||||
int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
|
||||
unsigned long *words, unsigned long *outlen);
|
||||
int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
|
||||
unsigned long der_object_identifier_bits(unsigned long x);
|
||||
|
||||
/* IA5 STRING */
|
||||
int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
int der_ia5_char_encode(int c);
|
||||
int der_ia5_value_decode(int v);
|
||||
|
||||
/* Printable STRING */
|
||||
int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
int der_printable_char_encode(int c);
|
||||
int der_printable_value_decode(int v);
|
||||
|
||||
/* UTF-8 */
|
||||
#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
|
||||
#include <wchar.h>
|
||||
#else
|
||||
typedef ulong32 wchar_t;
|
||||
#endif
|
||||
|
||||
int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
||||
wchar_t *out, unsigned long *outlen);
|
||||
unsigned long der_utf8_charsize(const wchar_t c);
|
||||
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
|
||||
|
||||
|
||||
/* CHOICE */
|
||||
int der_decode_choice(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_asn1_list *list, unsigned long outlen);
|
||||
|
||||
/* UTCTime */
|
||||
typedef struct {
|
||||
unsigned YY, /* year */
|
||||
MM, /* month */
|
||||
DD, /* day */
|
||||
hh, /* hour */
|
||||
mm, /* minute */
|
||||
ss, /* second */
|
||||
off_dir, /* timezone offset direction 0 == +, 1 == - */
|
||||
off_hh, /* timezone offset hours */
|
||||
off_mm; /* timezone offset minutes */
|
||||
} ltc_utctime;
|
||||
|
||||
int der_encode_utctime(ltc_utctime *utctime,
|
||||
unsigned char *out, unsigned long *outlen);
|
||||
|
||||
int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
|
||||
ltc_utctime *out);
|
||||
|
||||
int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,29 +1,32 @@
|
|||
The code taken from https://github.com/mattn/go-sqlite3 is licensed under:
|
||||
|
||||
Copyright © 2012-2014 Yasuhiro Matsumoto, http://mattn.kaoriya.net <mattn.jp@gmail.com>
|
||||
The MIT License (MIT)
|
||||
|
||||
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:
|
||||
Copyright (c) 2014 Yasuhiro Matsumoto
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
The code taken from https://github.com/sqlcipher/sqlcipher is licensed under:
|
||||
|
||||
Copyright (c) 2008-2013 Zetetic LLC
|
||||
Copyright (c) 2008, ZETETIC LLC
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -64,6 +67,29 @@ a legal notice, here is a blessing:
|
|||
The code taken from https://github.com/libtom/libtomcrypt is unlicensed with the
|
||||
following notice:
|
||||
|
||||
LibTomCrypt is public domain. As should all quality software be.
|
||||
The LibTom license
|
||||
|
||||
Tom St Denis
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
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 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.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
|
@ -0,0 +1,37 @@
|
|||
To maintain this code properly, the following three repositories have to be
|
||||
tracked for changes (maintenance details below):
|
||||
|
||||
- https://github.com/mattn/go-sqlite
|
||||
- https://github.com/sqlcipher/sqlcipher
|
||||
- https://github.com/libtom/libtomcrypt
|
||||
|
||||
|
||||
Update code from https://github.com/mattn/go-sqlite3
|
||||
----------------------------------------------------
|
||||
|
||||
Current release: v1.14.5
|
||||
|
||||
Use ./track_go-sqlite3.sh
|
||||
|
||||
|
||||
Update code from https://github.com/sqlcipher/sqlcipher
|
||||
-------------------------------------------------------
|
||||
|
||||
Current release: v4.4.2
|
||||
|
||||
Execute:
|
||||
./configure
|
||||
make
|
||||
|
||||
Track files:
|
||||
sqlite3.h
|
||||
sqlite3.c
|
||||
|
||||
|
||||
Update code from https://github.com/libtom/libtomcrypt
|
||||
------------------------------------------------------
|
||||
|
||||
Current HEAD: cfbd7f8d364e1438555ff2a247f7e17add11840e
|
||||
(from develop branch, 2020-08-29)
|
||||
|
||||
Use ./track_libtomcrypt.sh
|
|
@ -0,0 +1,11 @@
|
|||
.PHONY: all test update-modules
|
||||
|
||||
all:
|
||||
env GO111MODULE=on go build -v ./...
|
||||
|
||||
test:
|
||||
gocheck -g -c -e _example -e sqlite3_test -novet
|
||||
|
||||
update-modules:
|
||||
env GO111MODULE=on go get -u
|
||||
env GO111MODULE=on go mod tidy -v
|
|
@ -1,18 +1,33 @@
|
|||
## go-sqlcipher [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/mutecomm/go-sqlcipher) [![Build Status](http://img.shields.io/travis/mutecomm/go-sqlcipher.svg?style=flat-square)](https://travis-ci.org/mutecomm/go-sqlcipher)
|
||||
## go-sqlcipher
|
||||
|
||||
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/mutecomm/go-sqlcipher) [![CI](https://github.com/mutecomm/go-sqlcipher/workflows/CI/badge.svg)](https://github.com/mutecomm/go-sqlcipher/actions)
|
||||
|
||||
### Description
|
||||
|
||||
Self-contained Go sqlite3 driver with an AES-256 encrypted sqlite3 database
|
||||
conforming to the built-in database/sql interface. It is based on:
|
||||
|
||||
- stripped-down Go sqlite3 driver: https://github.com/mattn/go-sqlite3
|
||||
- Go sqlite3 driver: https://github.com/mattn/go-sqlite3
|
||||
- SQLite extension with AES-256 codec: https://github.com/sqlcipher/sqlcipher
|
||||
- AES-256 implementation from: https://github.com/libtom/libtomcrypt
|
||||
|
||||
SQLite itself is part of SQLCipher.
|
||||
|
||||
go-sqlcipher currently only supports the SQLCipher 3.x releases.
|
||||
### Incompatibilities of SQLCipher
|
||||
|
||||
The version tags of go-sqlcipher are the same as for SQLCipher.
|
||||
|
||||
**SQLCipher 4.x is incompatible with SQLCipher 3.x!**
|
||||
|
||||
go-sqlcipher does not implement any migration strategies at the moment.
|
||||
So if you upgrade a major version of go-sqlcipher, you yourself are responsible
|
||||
to upgrade existing database files.
|
||||
|
||||
See [migrating databases](https://www.zetetic.net/sqlcipher/sqlcipher-api/#Migrating_Databases) for details.
|
||||
|
||||
To upgrade your Go code to the 4.x series, change the import path to
|
||||
|
||||
"github.com/mutecomm/go-sqlcipher/v4"
|
||||
|
||||
### Installation
|
||||
|
||||
|
@ -33,7 +48,18 @@ db, _ := sql.Open("sqlite3", dbname)
|
|||
|
||||
`_pragma_key` is the hex encoded 32 byte key (must be 64 characters long).
|
||||
`_pragma_cipher_page_size` is the page size of the encrypted database (set if
|
||||
you want a different value than the default 1024 bytes).
|
||||
you want a different value than the default size).
|
||||
|
||||
```go
|
||||
key := url.QueryEscape("secret")
|
||||
dbname := fmt.Sprintf("db?_pragma_key=%s&_pragma_cipher_page_size=4096", key)
|
||||
db, _ := sql.Open("sqlite3", dbname)
|
||||
```
|
||||
|
||||
This uses a passphrase directly as `_pragma_key` with the key derivation function in
|
||||
SQLCipher. Do not forget the `url.QueryEscape()` call in your code!
|
||||
|
||||
See also [PRAGMA key](https://www.zetetic.net/sqlcipher/sqlcipher-api/#PRAGMA_key).
|
||||
|
||||
API documentation can be found here:
|
||||
http://godoc.org/github.com/mutecomm/go-sqlcipher
|
35
vendor/github.com/mutecomm/go-sqlcipher/v4/_example/mod_regexp/sqlite3_mod_regexp.c
generated
vendored
Normal file
35
vendor/github.com/mutecomm/go-sqlcipher/v4/_example/mod_regexp/sqlite3_mod_regexp.c
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <pcre.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sqlite3ext.h>
|
||||
|
||||
SQLITE_EXTENSION_INIT1
|
||||
static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
|
||||
if (argc >= 2) {
|
||||
const char *target = (const char *)sqlite3_value_text(argv[1]);
|
||||
const char *pattern = (const char *)sqlite3_value_text(argv[0]);
|
||||
const char* errstr = NULL;
|
||||
int erroff = 0;
|
||||
int vec[500];
|
||||
int n, rc;
|
||||
pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL);
|
||||
if (!re) {
|
||||
sqlite3_result_error(context, errstr, 0);
|
||||
return;
|
||||
}
|
||||
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
|
||||
if (rc <= 0) {
|
||||
sqlite3_result_int(context, 0);
|
||||
return;
|
||||
}
|
||||
sqlite3_result_int(context, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) {
|
||||
SQLITE_EXTENSION_INIT2(api);
|
||||
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, (void*)db, regexp_func, NULL, NULL);
|
||||
}
|
1040
vendor/github.com/mutecomm/go-sqlcipher/v4/_example/mod_vtable/picojson.h
generated
vendored
Normal file
1040
vendor/github.com/mutecomm/go-sqlcipher/v4/_example/mod_vtable/picojson.h
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -14,7 +14,7 @@
|
|||
Implementation of AES
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#ifdef LTC_RIJNDAEL
|
||||
|
||||
|
@ -38,7 +38,7 @@ const struct ltc_cipher_descriptor rijndael_desc =
|
|||
6,
|
||||
16, 32, 16, 10,
|
||||
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
const struct ltc_cipher_descriptor aes_desc =
|
||||
|
@ -47,7 +47,7 @@ const struct ltc_cipher_descriptor aes_desc =
|
|||
6,
|
||||
16, 32, 16, 10,
|
||||
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
#else
|
||||
|
@ -63,7 +63,7 @@ const struct ltc_cipher_descriptor rijndael_enc_desc =
|
|||
6,
|
||||
16, 32, 16, 10,
|
||||
SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
const struct ltc_cipher_descriptor aes_enc_desc =
|
||||
|
@ -72,7 +72,7 @@ const struct ltc_cipher_descriptor aes_enc_desc =
|
|||
6,
|
||||
16, 32, 16, 10,
|
||||
SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -123,9 +123,9 @@ int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *s
|
|||
@return CRYPT_OK if successful
|
||||
*/
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
|
||||
static int s_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
|
||||
#else
|
||||
int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
|
||||
int ECB_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
|
||||
#endif
|
||||
{
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
|
@ -140,9 +140,9 @@ int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
|
|||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
|
||||
int ECB_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
|
||||
{
|
||||
return _rijndael_ecb_encrypt(pt, ct, skey);
|
||||
return s_rijndael_ecb_encrypt(pt, ct, skey);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -156,9 +156,9 @@ int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
|
|||
@return CRYPT_OK if successful
|
||||
*/
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
|
||||
static int s_rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
|
||||
#else
|
||||
int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
|
||||
int ECB_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
|
||||
#endif
|
||||
{
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
|
@ -174,9 +174,9 @@ int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
|
|||
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
|
||||
int ECB_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
|
||||
{
|
||||
return _rijndael_ecb_decrypt(ct, pt, skey);
|
||||
return s_rijndael_ecb_decrypt(ct, pt, skey);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -235,23 +235,8 @@ int ECB_TEST(void)
|
|||
|
||||
rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key);
|
||||
rijndael_ecb_decrypt(tmp[0], tmp[1], &key);
|
||||
if (XMEMCMP(tmp[0], tests[i].ct, 16) || XMEMCMP(tmp[1], tests[i].pt, 16)) {
|
||||
#if 0
|
||||
printf("\n\nTest %d failed\n", i);
|
||||
if (XMEMCMP(tmp[0], tests[i].ct, 16)) {
|
||||
printf("CT: ");
|
||||
for (i = 0; i < 16; i++) {
|
||||
printf("%02x ", tmp[0][i]);
|
||||
}
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("PT: ");
|
||||
for (i = 0; i < 16; i++) {
|
||||
printf("%02x ", tmp[1][i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "AES Encrypt", i) ||
|
||||
compare_testvector(tmp[1], 16, tests[i].pt, 16, "AES Decrypt", i)) {
|
||||
return CRYPT_FAIL_TESTVECTOR;
|
||||
}
|
||||
|
||||
|
@ -289,18 +274,19 @@ int ECB_KS(int *keysize)
|
|||
{
|
||||
LTC_ARGCHK(keysize != NULL);
|
||||
|
||||
if (*keysize < 16)
|
||||
if (*keysize < 16) {
|
||||
return CRYPT_INVALID_KEYSIZE;
|
||||
}
|
||||
if (*keysize < 24) {
|
||||
*keysize = 16;
|
||||
return CRYPT_OK;
|
||||
} else if (*keysize < 32) {
|
||||
}
|
||||
if (*keysize < 32) {
|
||||
*keysize = 24;
|
||||
return CRYPT_OK;
|
||||
} else {
|
||||
}
|
||||
*keysize = 32;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,85 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// SQLiteBackup implement interface of Backup.
|
||||
type SQLiteBackup struct {
|
||||
b *C.sqlite3_backup
|
||||
}
|
||||
|
||||
// Backup make backup from src to dest.
|
||||
func (destConn *SQLiteConn) Backup(dest string, srcConn *SQLiteConn, src string) (*SQLiteBackup, error) {
|
||||
destptr := C.CString(dest)
|
||||
defer C.free(unsafe.Pointer(destptr))
|
||||
srcptr := C.CString(src)
|
||||
defer C.free(unsafe.Pointer(srcptr))
|
||||
|
||||
if b := C.sqlite3_backup_init(destConn.db, destptr, srcConn.db, srcptr); b != nil {
|
||||
bb := &SQLiteBackup{b: b}
|
||||
runtime.SetFinalizer(bb, (*SQLiteBackup).Finish)
|
||||
return bb, nil
|
||||
}
|
||||
return nil, destConn.lastError()
|
||||
}
|
||||
|
||||
// Step to backs up for one step. Calls the underlying `sqlite3_backup_step`
|
||||
// function. This function returns a boolean indicating if the backup is done
|
||||
// and an error signalling any other error. Done is returned if the underlying
|
||||
// C function returns SQLITE_DONE (Code 101)
|
||||
func (b *SQLiteBackup) Step(p int) (bool, error) {
|
||||
ret := C.sqlite3_backup_step(b.b, C.int(p))
|
||||
if ret == C.SQLITE_DONE {
|
||||
return true, nil
|
||||
} else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY {
|
||||
return false, Error{Code: ErrNo(ret)}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Remaining return whether have the rest for backup.
|
||||
func (b *SQLiteBackup) Remaining() int {
|
||||
return int(C.sqlite3_backup_remaining(b.b))
|
||||
}
|
||||
|
||||
// PageCount return count of pages.
|
||||
func (b *SQLiteBackup) PageCount() int {
|
||||
return int(C.sqlite3_backup_pagecount(b.b))
|
||||
}
|
||||
|
||||
// Finish close backup.
|
||||
func (b *SQLiteBackup) Finish() error {
|
||||
return b.Close()
|
||||
}
|
||||
|
||||
// Close close backup.
|
||||
func (b *SQLiteBackup) Close() error {
|
||||
ret := C.sqlite3_backup_finish(b.b)
|
||||
|
||||
// sqlite3_backup_finish() never fails, it just returns the
|
||||
// error code from previous operations, so clean up before
|
||||
// checking and returning an error
|
||||
b.b = nil
|
||||
runtime.SetFinalizer(b, nil)
|
||||
|
||||
if ret != 0 {
|
||||
return Error{Code: ErrNo(ret)}
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,392 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sqlite3
|
||||
|
||||
// You can't export a Go function to C and have definitions in the C
|
||||
// preamble in the same file, so we have to have callbackTrampoline in
|
||||
// its own file. Because we need a separate file anyway, the support
|
||||
// code for SQLite custom functions is in here.
|
||||
|
||||
/*
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
void _sqlite3_result_text(sqlite3_context* ctx, const char* s);
|
||||
void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//export callbackTrampoline
|
||||
func callbackTrampoline(ctx *C.sqlite3_context, argc int, argv **C.sqlite3_value) {
|
||||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
|
||||
fi := lookupHandle(C.sqlite3_user_data(ctx)).(*functionInfo)
|
||||
fi.Call(ctx, args)
|
||||
}
|
||||
|
||||
//export stepTrampoline
|
||||
func stepTrampoline(ctx *C.sqlite3_context, argc C.int, argv **C.sqlite3_value) {
|
||||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:int(argc):int(argc)]
|
||||
ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo)
|
||||
ai.Step(ctx, args)
|
||||
}
|
||||
|
||||
//export doneTrampoline
|
||||
func doneTrampoline(ctx *C.sqlite3_context) {
|
||||
ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo)
|
||||
ai.Done(ctx)
|
||||
}
|
||||
|
||||
//export compareTrampoline
|
||||
func compareTrampoline(handlePtr unsafe.Pointer, la C.int, a *C.char, lb C.int, b *C.char) C.int {
|
||||
cmp := lookupHandle(handlePtr).(func(string, string) int)
|
||||
return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb)))
|
||||
}
|
||||
|
||||
//export commitHookTrampoline
|
||||
func commitHookTrampoline(handle unsafe.Pointer) int {
|
||||
callback := lookupHandle(handle).(func() int)
|
||||
return callback()
|
||||
}
|
||||
|
||||
//export rollbackHookTrampoline
|
||||
func rollbackHookTrampoline(handle unsafe.Pointer) {
|
||||
callback := lookupHandle(handle).(func())
|
||||
callback()
|
||||
}
|
||||
|
||||
//export updateHookTrampoline
|
||||
func updateHookTrampoline(handle unsafe.Pointer, op int, db *C.char, table *C.char, rowid int64) {
|
||||
callback := lookupHandle(handle).(func(int, string, string, int64))
|
||||
callback(op, C.GoString(db), C.GoString(table), rowid)
|
||||
}
|
||||
|
||||
//export authorizerTrampoline
|
||||
func authorizerTrampoline(handle unsafe.Pointer, op int, arg1 *C.char, arg2 *C.char, arg3 *C.char) int {
|
||||
callback := lookupHandle(handle).(func(int, string, string, string) int)
|
||||
return callback(op, C.GoString(arg1), C.GoString(arg2), C.GoString(arg3))
|
||||
}
|
||||
|
||||
//export preUpdateHookTrampoline
|
||||
func preUpdateHookTrampoline(handle unsafe.Pointer, dbHandle uintptr, op int, db *C.char, table *C.char, oldrowid int64, newrowid int64) {
|
||||
hval := lookupHandleVal(handle)
|
||||
data := SQLitePreUpdateData{
|
||||
Conn: hval.db,
|
||||
Op: op,
|
||||
DatabaseName: C.GoString(db),
|
||||
TableName: C.GoString(table),
|
||||
OldRowID: oldrowid,
|
||||
NewRowID: newrowid,
|
||||
}
|
||||
callback := hval.val.(func(SQLitePreUpdateData))
|
||||
callback(data)
|
||||
}
|
||||
|
||||
// Use handles to avoid passing Go pointers to C.
|
||||
type handleVal struct {
|
||||
db *SQLiteConn
|
||||
val interface{}
|
||||
}
|
||||
|
||||
var handleLock sync.Mutex
|
||||
var handleVals = make(map[unsafe.Pointer]handleVal)
|
||||
|
||||
func newHandle(db *SQLiteConn, v interface{}) unsafe.Pointer {
|
||||
handleLock.Lock()
|
||||
defer handleLock.Unlock()
|
||||
val := handleVal{db: db, val: v}
|
||||
var p unsafe.Pointer = C.malloc(C.size_t(1))
|
||||
if p == nil {
|
||||
panic("can't allocate 'cgo-pointer hack index pointer': ptr == nil")
|
||||
}
|
||||
handleVals[p] = val
|
||||
return p
|
||||
}
|
||||
|
||||
func lookupHandleVal(handle unsafe.Pointer) handleVal {
|
||||
handleLock.Lock()
|
||||
defer handleLock.Unlock()
|
||||
return handleVals[handle]
|
||||
}
|
||||
|
||||
func lookupHandle(handle unsafe.Pointer) interface{} {
|
||||
return lookupHandleVal(handle).val
|
||||
}
|
||||
|
||||
func deleteHandles(db *SQLiteConn) {
|
||||
handleLock.Lock()
|
||||
defer handleLock.Unlock()
|
||||
for handle, val := range handleVals {
|
||||
if val.db == db {
|
||||
delete(handleVals, handle)
|
||||
C.free(handle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is only here so that tests can refer to it.
|
||||
type callbackArgRaw C.sqlite3_value
|
||||
|
||||
type callbackArgConverter func(*C.sqlite3_value) (reflect.Value, error)
|
||||
|
||||
type callbackArgCast struct {
|
||||
f callbackArgConverter
|
||||
typ reflect.Type
|
||||
}
|
||||
|
||||
func (c callbackArgCast) Run(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
val, err := c.f(v)
|
||||
if err != nil {
|
||||
return reflect.Value{}, err
|
||||
}
|
||||
if !val.Type().ConvertibleTo(c.typ) {
|
||||
return reflect.Value{}, fmt.Errorf("cannot convert %s to %s", val.Type(), c.typ)
|
||||
}
|
||||
return val.Convert(c.typ), nil
|
||||
}
|
||||
|
||||
func callbackArgInt64(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
if C.sqlite3_value_type(v) != C.SQLITE_INTEGER {
|
||||
return reflect.Value{}, fmt.Errorf("argument must be an INTEGER")
|
||||
}
|
||||
return reflect.ValueOf(int64(C.sqlite3_value_int64(v))), nil
|
||||
}
|
||||
|
||||
func callbackArgBool(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
if C.sqlite3_value_type(v) != C.SQLITE_INTEGER {
|
||||
return reflect.Value{}, fmt.Errorf("argument must be an INTEGER")
|
||||
}
|
||||
i := int64(C.sqlite3_value_int64(v))
|
||||
val := false
|
||||
if i != 0 {
|
||||
val = true
|
||||
}
|
||||
return reflect.ValueOf(val), nil
|
||||
}
|
||||
|
||||
func callbackArgFloat64(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
if C.sqlite3_value_type(v) != C.SQLITE_FLOAT {
|
||||
return reflect.Value{}, fmt.Errorf("argument must be a FLOAT")
|
||||
}
|
||||
return reflect.ValueOf(float64(C.sqlite3_value_double(v))), nil
|
||||
}
|
||||
|
||||
func callbackArgBytes(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
switch C.sqlite3_value_type(v) {
|
||||
case C.SQLITE_BLOB:
|
||||
l := C.sqlite3_value_bytes(v)
|
||||
p := C.sqlite3_value_blob(v)
|
||||
return reflect.ValueOf(C.GoBytes(p, l)), nil
|
||||
case C.SQLITE_TEXT:
|
||||
l := C.sqlite3_value_bytes(v)
|
||||
c := unsafe.Pointer(C.sqlite3_value_text(v))
|
||||
return reflect.ValueOf(C.GoBytes(c, l)), nil
|
||||
default:
|
||||
return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT")
|
||||
}
|
||||
}
|
||||
|
||||
func callbackArgString(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
switch C.sqlite3_value_type(v) {
|
||||
case C.SQLITE_BLOB:
|
||||
l := C.sqlite3_value_bytes(v)
|
||||
p := (*C.char)(C.sqlite3_value_blob(v))
|
||||
return reflect.ValueOf(C.GoStringN(p, l)), nil
|
||||
case C.SQLITE_TEXT:
|
||||
c := (*C.char)(unsafe.Pointer(C.sqlite3_value_text(v)))
|
||||
return reflect.ValueOf(C.GoString(c)), nil
|
||||
default:
|
||||
return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT")
|
||||
}
|
||||
}
|
||||
|
||||
func callbackArgGeneric(v *C.sqlite3_value) (reflect.Value, error) {
|
||||
switch C.sqlite3_value_type(v) {
|
||||
case C.SQLITE_INTEGER:
|
||||
return callbackArgInt64(v)
|
||||
case C.SQLITE_FLOAT:
|
||||
return callbackArgFloat64(v)
|
||||
case C.SQLITE_TEXT:
|
||||
return callbackArgString(v)
|
||||
case C.SQLITE_BLOB:
|
||||
return callbackArgBytes(v)
|
||||
case C.SQLITE_NULL:
|
||||
// Interpret NULL as a nil byte slice.
|
||||
var ret []byte
|
||||
return reflect.ValueOf(ret), nil
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
func callbackArg(typ reflect.Type) (callbackArgConverter, error) {
|
||||
switch typ.Kind() {
|
||||
case reflect.Interface:
|
||||
if typ.NumMethod() != 0 {
|
||||
return nil, errors.New("the only supported interface type is interface{}")
|
||||
}
|
||||
return callbackArgGeneric, nil
|
||||
case reflect.Slice:
|
||||
if typ.Elem().Kind() != reflect.Uint8 {
|
||||
return nil, errors.New("the only supported slice type is []byte")
|
||||
}
|
||||
return callbackArgBytes, nil
|
||||
case reflect.String:
|
||||
return callbackArgString, nil
|
||||
case reflect.Bool:
|
||||
return callbackArgBool, nil
|
||||
case reflect.Int64:
|
||||
return callbackArgInt64, nil
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint:
|
||||
c := callbackArgCast{callbackArgInt64, typ}
|
||||
return c.Run, nil
|
||||
case reflect.Float64:
|
||||
return callbackArgFloat64, nil
|
||||
case reflect.Float32:
|
||||
c := callbackArgCast{callbackArgFloat64, typ}
|
||||
return c.Run, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("don't know how to convert to %s", typ)
|
||||
}
|
||||
}
|
||||
|
||||
func callbackConvertArgs(argv []*C.sqlite3_value, converters []callbackArgConverter, variadic callbackArgConverter) ([]reflect.Value, error) {
|
||||
var args []reflect.Value
|
||||
|
||||
if len(argv) < len(converters) {
|
||||
return nil, fmt.Errorf("function requires at least %d arguments", len(converters))
|
||||
}
|
||||
|
||||
for i, arg := range argv[:len(converters)] {
|
||||
v, err := converters[i](arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args = append(args, v)
|
||||
}
|
||||
|
||||
if variadic != nil {
|
||||
for _, arg := range argv[len(converters):] {
|
||||
v, err := variadic(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args = append(args, v)
|
||||
}
|
||||
}
|
||||
return args, nil
|
||||
}
|
||||
|
||||
type callbackRetConverter func(*C.sqlite3_context, reflect.Value) error
|
||||
|
||||
func callbackRetInteger(ctx *C.sqlite3_context, v reflect.Value) error {
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Int64:
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint:
|
||||
v = v.Convert(reflect.TypeOf(int64(0)))
|
||||
case reflect.Bool:
|
||||
b := v.Interface().(bool)
|
||||
if b {
|
||||
v = reflect.ValueOf(int64(1))
|
||||
} else {
|
||||
v = reflect.ValueOf(int64(0))
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("cannot convert %s to INTEGER", v.Type())
|
||||
}
|
||||
|
||||
C.sqlite3_result_int64(ctx, C.sqlite3_int64(v.Interface().(int64)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func callbackRetFloat(ctx *C.sqlite3_context, v reflect.Value) error {
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Float64:
|
||||
case reflect.Float32:
|
||||
v = v.Convert(reflect.TypeOf(float64(0)))
|
||||
default:
|
||||
return fmt.Errorf("cannot convert %s to FLOAT", v.Type())
|
||||
}
|
||||
|
||||
C.sqlite3_result_double(ctx, C.double(v.Interface().(float64)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func callbackRetBlob(ctx *C.sqlite3_context, v reflect.Value) error {
|
||||
if v.Type().Kind() != reflect.Slice || v.Type().Elem().Kind() != reflect.Uint8 {
|
||||
return fmt.Errorf("cannot convert %s to BLOB", v.Type())
|
||||
}
|
||||
i := v.Interface()
|
||||
if i == nil || len(i.([]byte)) == 0 {
|
||||
C.sqlite3_result_null(ctx)
|
||||
} else {
|
||||
bs := i.([]byte)
|
||||
C._sqlite3_result_blob(ctx, unsafe.Pointer(&bs[0]), C.int(len(bs)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func callbackRetText(ctx *C.sqlite3_context, v reflect.Value) error {
|
||||
if v.Type().Kind() != reflect.String {
|
||||
return fmt.Errorf("cannot convert %s to TEXT", v.Type())
|
||||
}
|
||||
C._sqlite3_result_text(ctx, C.CString(v.Interface().(string)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
|
||||
switch typ.Kind() {
|
||||
case reflect.Interface:
|
||||
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
|
||||
if typ.Implements(errorInterface) {
|
||||
return callbackRetNil, nil
|
||||
}
|
||||
fallthrough
|
||||
case reflect.Slice:
|
||||
if typ.Elem().Kind() != reflect.Uint8 {
|
||||
return nil, errors.New("the only supported slice type is []byte")
|
||||
}
|
||||
return callbackRetBlob, nil
|
||||
case reflect.String:
|
||||
return callbackRetText, nil
|
||||
case reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint:
|
||||
return callbackRetInteger, nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return callbackRetFloat, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("don't know how to convert to %s", typ)
|
||||
}
|
||||
}
|
||||
|
||||
func callbackError(ctx *C.sqlite3_context, err error) {
|
||||
cstr := C.CString(err.Error())
|
||||
defer C.free(unsafe.Pointer(cstr))
|
||||
C.sqlite3_result_error(ctx, cstr, C.int(-1))
|
||||
}
|
||||
|
||||
// Test support code. Tests are not allowed to import "C", so we can't
|
||||
// declare any functions that use C.sqlite3_value.
|
||||
func callbackSyntheticForTests(v reflect.Value, err error) callbackArgConverter {
|
||||
return func(*C.sqlite3_value) (reflect.Value, error) {
|
||||
return v, err
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file cbc_decrypt.c
|
||||
CBC implementation, encrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_CBC_MODE
|
||||
|
||||
/**
|
||||
CBC decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param len The number of bytes to process (must be multiple of block length)
|
||||
@param cbc CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
unsigned char tmp[16];
|
||||
#ifdef LTC_FAST
|
||||
LTC_FAST_TYPE tmpy;
|
||||
#else
|
||||
unsigned char tmpy;
|
||||
#endif
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen valid? */
|
||||
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (len % cbc->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#ifdef LTC_FAST
|
||||
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
|
||||
return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
|
||||
}
|
||||
while (len) {
|
||||
/* decrypt */
|
||||
if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* xor IV against plaintext */
|
||||
#if defined(LTC_FAST)
|
||||
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
|
||||
tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
|
||||
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
|
||||
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
|
||||
}
|
||||
#else
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
tmpy = tmp[x] ^ cbc->IV[x];
|
||||
cbc->IV[x] = ct[x];
|
||||
pt[x] = tmpy;
|
||||
}
|
||||
#endif
|
||||
|
||||
ct += cbc->blocklen;
|
||||
pt += cbc->blocklen;
|
||||
len -= cbc->blocklen;
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file cbc_done.c
|
||||
|
@ -36,7 +28,3 @@ int cbc_done(symmetric_CBC *cbc)
|
|||
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -0,0 +1,85 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file cbc_encrypt.c
|
||||
CBC implementation, encrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_CBC_MODE
|
||||
|
||||
/**
|
||||
CBC encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param len The number of bytes to process (must be multiple of block length)
|
||||
@param cbc CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen valid? */
|
||||
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (len % cbc->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#ifdef LTC_FAST
|
||||
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) {
|
||||
return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key);
|
||||
}
|
||||
while (len) {
|
||||
/* xor IV against plaintext */
|
||||
#if defined(LTC_FAST)
|
||||
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
|
||||
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x));
|
||||
}
|
||||
#else
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] ^= pt[x];
|
||||
}
|
||||
#endif
|
||||
|
||||
/* encrypt */
|
||||
if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* store IV [ciphertext] for a future block */
|
||||
#if defined(LTC_FAST)
|
||||
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
|
||||
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
|
||||
}
|
||||
#else
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] = ct[x];
|
||||
}
|
||||
#endif
|
||||
|
||||
ct += cbc->blocklen;
|
||||
pt += cbc->blocklen;
|
||||
len -= cbc->blocklen;
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file cbc_start.c
|
||||
|
@ -20,7 +12,7 @@
|
|||
/**
|
||||
Initialize a CBC context
|
||||
@param cipher The index of the cipher desired
|
||||
@param IV The initial vector
|
||||
@param IV The initialization vector
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param num_rounds Number of rounds in the cipher desired (0 for default)
|
||||
|
@ -56,7 +48,3 @@ int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -0,0 +1,81 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file compare_testvector.c
|
||||
Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel
|
||||
*/
|
||||
|
||||
#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
|
||||
static void s_print_hex(const char* what, const void* v, const unsigned long l)
|
||||
{
|
||||
const unsigned char* p = v;
|
||||
unsigned long x, y = 0, z;
|
||||
fprintf(stderr, "%s contents: \n", what);
|
||||
for (x = 0; x < l; ) {
|
||||
fprintf(stderr, "%02X ", p[x]);
|
||||
if (!(++x % 16) || x == l) {
|
||||
if((x % 16) != 0) {
|
||||
z = 16 - (x % 16);
|
||||
if(z >= 8)
|
||||
fprintf(stderr, " ");
|
||||
for (; z != 0; --z) {
|
||||
fprintf(stderr, " ");
|
||||
}
|
||||
}
|
||||
fprintf(stderr, " | ");
|
||||
for(; y < x; y++) {
|
||||
if((y % 8) == 0)
|
||||
fprintf(stderr, " ");
|
||||
if(isgraph(p[y]))
|
||||
fprintf(stderr, "%c", p[y]);
|
||||
else
|
||||
fprintf(stderr, ".");
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
else if((x % 8) == 0) {
|
||||
fprintf(stderr, " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
Compare two test-vectors
|
||||
|
||||
@param is The data as it is
|
||||
@param is_len The length of is
|
||||
@param should The data as it should
|
||||
@param should_len The length of should
|
||||
@param what The type of the data
|
||||
@param which The iteration count
|
||||
@return 0 on equality, -1 or 1 on difference
|
||||
*/
|
||||
int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
|
||||
{
|
||||
int res = 0;
|
||||
if(is_len != should_len) {
|
||||
res = is_len > should_len ? -1 : 1;
|
||||
} else {
|
||||
res = XMEMCMP(is, should, is_len);
|
||||
}
|
||||
#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
|
||||
if (res != 0) {
|
||||
fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
|
||||
s_print_hex("SHOULD", should, should_len);
|
||||
s_print_hex("IS ", is, is_len);
|
||||
#if LTC_TEST_DBG > 1
|
||||
} else {
|
||||
fprintf(stderr, "Testvector #%i of %s passed!\n", which, what);
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
LTC_UNUSED_PARAM(which);
|
||||
LTC_UNUSED_PARAM(what);
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
|
@ -0,0 +1,299 @@
|
|||
// Extracted from Go database/sql source code
|
||||
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Type conversions for Scan.
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
|
||||
|
||||
// convertAssign copies to dest the value in src, converting it if possible.
|
||||
// An error is returned if the copy would result in loss of information.
|
||||
// dest should be a pointer type.
|
||||
func convertAssign(dest, src interface{}) error {
|
||||
// Common cases, without reflect.
|
||||
switch s := src.(type) {
|
||||
case string:
|
||||
switch d := dest.(type) {
|
||||
case *string:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = []byte(s)
|
||||
return nil
|
||||
case *sql.RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = append((*d)[:0], s...)
|
||||
return nil
|
||||
}
|
||||
case []byte:
|
||||
switch d := dest.(type) {
|
||||
case *string:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = string(s)
|
||||
return nil
|
||||
case *interface{}:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = cloneBytes(s)
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = cloneBytes(s)
|
||||
return nil
|
||||
case *sql.RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s
|
||||
return nil
|
||||
}
|
||||
case time.Time:
|
||||
switch d := dest.(type) {
|
||||
case *time.Time:
|
||||
*d = s
|
||||
return nil
|
||||
case *string:
|
||||
*d = s.Format(time.RFC3339Nano)
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = []byte(s.Format(time.RFC3339Nano))
|
||||
return nil
|
||||
case *sql.RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s.AppendFormat((*d)[:0], time.RFC3339Nano)
|
||||
return nil
|
||||
}
|
||||
case nil:
|
||||
switch d := dest.(type) {
|
||||
case *interface{}:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
case *sql.RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var sv reflect.Value
|
||||
|
||||
switch d := dest.(type) {
|
||||
case *string:
|
||||
sv = reflect.ValueOf(src)
|
||||
switch sv.Kind() {
|
||||
case reflect.Bool,
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
*d = asString(src)
|
||||
return nil
|
||||
}
|
||||
case *[]byte:
|
||||
sv = reflect.ValueOf(src)
|
||||
if b, ok := asBytes(nil, sv); ok {
|
||||
*d = b
|
||||
return nil
|
||||
}
|
||||
case *sql.RawBytes:
|
||||
sv = reflect.ValueOf(src)
|
||||
if b, ok := asBytes([]byte(*d)[:0], sv); ok {
|
||||
*d = sql.RawBytes(b)
|
||||
return nil
|
||||
}
|
||||
case *bool:
|
||||
bv, err := driver.Bool.ConvertValue(src)
|
||||
if err == nil {
|
||||
*d = bv.(bool)
|
||||
}
|
||||
return err
|
||||
case *interface{}:
|
||||
*d = src
|
||||
return nil
|
||||
}
|
||||
|
||||
if scanner, ok := dest.(sql.Scanner); ok {
|
||||
return scanner.Scan(src)
|
||||
}
|
||||
|
||||
dpv := reflect.ValueOf(dest)
|
||||
if dpv.Kind() != reflect.Ptr {
|
||||
return errors.New("destination not a pointer")
|
||||
}
|
||||
if dpv.IsNil() {
|
||||
return errNilPtr
|
||||
}
|
||||
|
||||
if !sv.IsValid() {
|
||||
sv = reflect.ValueOf(src)
|
||||
}
|
||||
|
||||
dv := reflect.Indirect(dpv)
|
||||
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
|
||||
switch b := src.(type) {
|
||||
case []byte:
|
||||
dv.Set(reflect.ValueOf(cloneBytes(b)))
|
||||
default:
|
||||
dv.Set(sv)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
|
||||
dv.Set(sv.Convert(dv.Type()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// The following conversions use a string value as an intermediate representation
|
||||
// to convert between various numeric types.
|
||||
//
|
||||
// This also allows scanning into user defined types such as "type Int int64".
|
||||
// For symmetry, also check for string destination types.
|
||||
switch dv.Kind() {
|
||||
case reflect.Ptr:
|
||||
if src == nil {
|
||||
dv.Set(reflect.Zero(dv.Type()))
|
||||
return nil
|
||||
}
|
||||
dv.Set(reflect.New(dv.Type().Elem()))
|
||||
return convertAssign(dv.Interface(), src)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
s := asString(src)
|
||||
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
|
||||
if err != nil {
|
||||
err = strconvErr(err)
|
||||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
|
||||
}
|
||||
dv.SetInt(i64)
|
||||
return nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
s := asString(src)
|
||||
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
|
||||
if err != nil {
|
||||
err = strconvErr(err)
|
||||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
|
||||
}
|
||||
dv.SetUint(u64)
|
||||
return nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
s := asString(src)
|
||||
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
|
||||
if err != nil {
|
||||
err = strconvErr(err)
|
||||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
|
||||
}
|
||||
dv.SetFloat(f64)
|
||||
return nil
|
||||
case reflect.String:
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
dv.SetString(v)
|
||||
return nil
|
||||
case []byte:
|
||||
dv.SetString(string(v))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
|
||||
}
|
||||
|
||||
func strconvErr(err error) error {
|
||||
if ne, ok := err.(*strconv.NumError); ok {
|
||||
return ne.Err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func cloneBytes(b []byte) []byte {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
c := make([]byte, len(b))
|
||||
copy(c, b)
|
||||
return c
|
||||
}
|
||||
|
||||
func asString(src interface{}) string {
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
}
|
||||
rv := reflect.ValueOf(src)
|
||||
switch rv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return strconv.FormatInt(rv.Int(), 10)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return strconv.FormatUint(rv.Uint(), 10)
|
||||
case reflect.Float64:
|
||||
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
|
||||
case reflect.Float32:
|
||||
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
|
||||
case reflect.Bool:
|
||||
return strconv.FormatBool(rv.Bool())
|
||||
}
|
||||
return fmt.Sprintf("%v", src)
|
||||
}
|
||||
|
||||
func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
|
||||
switch rv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return strconv.AppendInt(buf, rv.Int(), 10), true
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return strconv.AppendUint(buf, rv.Uint(), 10), true
|
||||
case reflect.Float32:
|
||||
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
|
||||
case reflect.Float64:
|
||||
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
|
||||
case reflect.Bool:
|
||||
return strconv.AppendBool(buf, rv.Bool()), true
|
||||
case reflect.String:
|
||||
s := rv.String()
|
||||
return append(buf, s...), true
|
||||
}
|
||||
return
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_argchk.c
|
||||
Perform argument checking, Tom St Denis
|
||||
*/
|
||||
|
||||
#if (ARGTYPE == 0)
|
||||
void crypt_argchk(const char *v, const char *s, int d)
|
||||
{
|
||||
fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n",
|
||||
v, d, s);
|
||||
abort();
|
||||
}
|
||||
#endif
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/crypt_cipher_descriptor.c
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/crypt_cipher_descriptor.c
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_cipher_descriptor.c
|
||||
Stores the cipher descriptor table, Tom St Denis
|
||||
*/
|
||||
|
||||
struct ltc_cipher_descriptor cipher_descriptor[TAB_SIZE] = {
|
||||
{ NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
LTC_MUTEX_GLOBAL(ltc_cipher_mutex)
|
||||
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_cipher_is_valid.c
|
||||
|
@ -30,7 +22,3 @@ int cipher_is_valid(int idx)
|
|||
LTC_MUTEX_UNLOCK(<c_cipher_mutex);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_find_cipher.c
|
||||
|
@ -35,7 +27,3 @@ int find_cipher(const char *name)
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_find_hash.c
|
||||
|
@ -34,7 +26,3 @@ int find_hash(const char *name)
|
|||
LTC_MUTEX_UNLOCK(<c_hash_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/crypt_hash_descriptor.c
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/crypt_hash_descriptor.c
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_hash_descriptor.c
|
||||
Stores the hash descriptor table, Tom St Denis
|
||||
*/
|
||||
|
||||
struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = {
|
||||
{ NULL, 0, 0, 0, { 0 }, 0, NULL, NULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
LTC_MUTEX_GLOBAL(ltc_hash_mutex)
|
||||
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_hash_is_valid.c
|
||||
|
@ -30,7 +22,3 @@ int hash_is_valid(int idx)
|
|||
LTC_MUTEX_UNLOCK(<c_hash_mutex);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
14
vendor/github.com/mutecomm/go-sqlcipher/v4/crypt_prng_descriptor.c
generated
vendored
Normal file
14
vendor/github.com/mutecomm/go-sqlcipher/v4/crypt_prng_descriptor.c
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_prng_descriptor.c
|
||||
Stores the PRNG descriptors, Tom St Denis
|
||||
*/
|
||||
struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = {
|
||||
{ NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
LTC_MUTEX_GLOBAL(ltc_prng_mutex)
|
||||
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_register_cipher.c
|
||||
|
@ -48,7 +40,3 @@ int register_cipher(const struct ltc_cipher_descriptor *cipher)
|
|||
LTC_MUTEX_UNLOCK(<c_cipher_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_register_hash.c
|
||||
|
@ -48,7 +40,3 @@ int register_hash(const struct ltc_hash_descriptor *hash)
|
|||
LTC_MUTEX_UNLOCK(<c_hash_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file crypt_register_prng.c
|
||||
|
@ -48,7 +40,3 @@ int register_prng(const struct ltc_prng_descriptor *prng)
|
|||
LTC_MUTEX_UNLOCK(<c_prng_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
Package sqlite3 provides interface to SQLite3 databases.
|
||||
|
||||
This works as a driver for database/sql.
|
||||
|
||||
Installation
|
||||
|
||||
go get github.com/mattn/go-sqlite3
|
||||
|
||||
Supported Types
|
||||
|
||||
Currently, go-sqlite3 supports the following data types.
|
||||
|
||||
+------------------------------+
|
||||
|go | sqlite3 |
|
||||
|----------|-------------------|
|
||||
|nil | null |
|
||||
|int | integer |
|
||||
|int64 | integer |
|
||||
|float64 | float |
|
||||
|bool | integer |
|
||||
|[]byte | blob |
|
||||
|string | text |
|
||||
|time.Time | timestamp/datetime|
|
||||
+------------------------------+
|
||||
|
||||
SQLite3 Extension
|
||||
|
||||
You can write your own extension module for sqlite3. For example, below is an
|
||||
extension for a Regexp matcher operation.
|
||||
|
||||
#include <pcre.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sqlite3ext.h>
|
||||
|
||||
SQLITE_EXTENSION_INIT1
|
||||
static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
|
||||
if (argc >= 2) {
|
||||
const char *target = (const char *)sqlite3_value_text(argv[1]);
|
||||
const char *pattern = (const char *)sqlite3_value_text(argv[0]);
|
||||
const char* errstr = NULL;
|
||||
int erroff = 0;
|
||||
int vec[500];
|
||||
int n, rc;
|
||||
pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL);
|
||||
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
|
||||
if (rc <= 0) {
|
||||
sqlite3_result_error(context, errstr, 0);
|
||||
return;
|
||||
}
|
||||
sqlite3_result_int(context, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
int sqlite3_extension_init(sqlite3 *db, char **errmsg,
|
||||
const sqlite3_api_routines *api) {
|
||||
SQLITE_EXTENSION_INIT2(api);
|
||||
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8,
|
||||
(void*)db, regexp_func, NULL, NULL);
|
||||
}
|
||||
|
||||
It needs to be built as a so/dll shared library. And you need to register
|
||||
the extension module like below.
|
||||
|
||||
sql.Register("sqlite3_with_extensions",
|
||||
&sqlite3.SQLiteDriver{
|
||||
Extensions: []string{
|
||||
"sqlite3_mod_regexp",
|
||||
},
|
||||
})
|
||||
|
||||
Then, you can use this extension.
|
||||
|
||||
rows, err := db.Query("select text from mytable where name regexp '^golang'")
|
||||
|
||||
Connection Hook
|
||||
|
||||
You can hook and inject your code when the connection is established by setting
|
||||
ConnectHook to get the SQLiteConn.
|
||||
|
||||
sql.Register("sqlite3_with_hook_example",
|
||||
&sqlite3.SQLiteDriver{
|
||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||
sqlite3conn = append(sqlite3conn, conn)
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
You can also use database/sql.Conn.Raw (Go >= 1.13):
|
||||
|
||||
conn, err := db.Conn(context.Background())
|
||||
// if err != nil { ... }
|
||||
defer conn.Close()
|
||||
err = conn.Raw(func (driverConn interface{}) error {
|
||||
sqliteConn := driverConn.(*sqlite3.SQLiteConn)
|
||||
// ... use sqliteConn
|
||||
})
|
||||
// if err != nil { ... }
|
||||
|
||||
Go SQlite3 Extensions
|
||||
|
||||
If you want to register Go functions as SQLite extension functions
|
||||
you can make a custom driver by calling RegisterFunction from
|
||||
ConnectHook.
|
||||
|
||||
regex = func(re, s string) (bool, error) {
|
||||
return regexp.MatchString(re, s)
|
||||
}
|
||||
sql.Register("sqlite3_extended",
|
||||
&sqlite3.SQLiteDriver{
|
||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||
return conn.RegisterFunc("regexp", regex, true)
|
||||
},
|
||||
})
|
||||
|
||||
You can then use the custom driver by passing its name to sql.Open.
|
||||
|
||||
var i int
|
||||
conn, err := sql.Open("sqlite3_extended", "./foo.db")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = db.QueryRow(`SELECT regexp("foo.*", "seafood")`).Scan(&i)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
See the documentation of RegisterFunc for more details.
|
||||
|
||||
*/
|
||||
package sqlite3
|
|
@ -1,11 +1,19 @@
|
|||
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "syscall"
|
||||
|
||||
// ErrNo inherit errno.
|
||||
type ErrNo int
|
||||
|
@ -20,6 +28,7 @@ type ErrNoExtended int
|
|||
type Error struct {
|
||||
Code ErrNo /* The error code returned by SQLite */
|
||||
ExtendedCode ErrNoExtended /* The extended error code returned by SQLite */
|
||||
SystemErrno syscall.Errno /* The system errno returned by the OS through SQLite, if applicable */
|
||||
err string /* The error string returned by sqlite3_errmsg(),
|
||||
this usually contains more specific details. */
|
||||
}
|
||||
|
@ -72,10 +81,16 @@ func (err ErrNoExtended) Error() string {
|
|||
}
|
||||
|
||||
func (err Error) Error() string {
|
||||
var str string
|
||||
if err.err != "" {
|
||||
return err.err
|
||||
str = err.err
|
||||
} else {
|
||||
str = C.GoString(C.sqlite3_errstr(C.int(err.Code)))
|
||||
}
|
||||
return errorString(err)
|
||||
if err.SystemErrno != 0 {
|
||||
str += ": " + err.SystemErrno.Error()
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// result codes from http://www.sqlite.org/c3ref/c_abort_rollback.html
|
|
@ -1,6 +1,9 @@
|
|||
package sqlite3
|
||||
|
||||
/*
|
||||
// make go-sqlite3 use embedded library without code changes
|
||||
#cgo CFLAGS: -DUSE_LIBSQLITE3
|
||||
|
||||
// enable encryption codec in sqlite
|
||||
#cgo CFLAGS: -DSQLITE_HAS_CODEC
|
||||
|
||||
|
@ -10,9 +13,6 @@ package sqlite3
|
|||
// use libtomcrypt implementation in sqlcipher
|
||||
#cgo CFLAGS: -DSQLCIPHER_CRYPTO_LIBTOMCRYPT
|
||||
|
||||
// disable loadable extensions in sqlite
|
||||
#cgo CFLAGS: -DSQLITE_OMIT_LOAD_EXTENSION=1
|
||||
|
||||
// disable anything "not portable" in libtomcrypt
|
||||
#cgo CFLAGS: -DLTC_NO_ASM
|
||||
|
|
@ -0,0 +1,520 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#elif defined(LTC_CLOCK_GETTIME)
|
||||
#include <time.h> /* struct timespec + clock_gettime */
|
||||
#else
|
||||
#include <sys/time.h> /* struct timeval + gettimeofday */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/**
|
||||
@file fortuna.c
|
||||
Fortuna PRNG, Tom St Denis
|
||||
*/
|
||||
|
||||
/* Implementation of Fortuna by Tom St Denis
|
||||
|
||||
We deviate slightly here for reasons of simplicity [and to fit in the API]. First all "sources"
|
||||
in the AddEntropy function are fixed to 0. Second since no reliable timer is provided
|
||||
we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to the read function */
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
|
||||
/* requries LTC_SHA256 and AES */
|
||||
#if !(defined(LTC_RIJNDAEL) && defined(LTC_SHA256))
|
||||
#error LTC_FORTUNA requires LTC_SHA256 and LTC_RIJNDAEL (AES)
|
||||
#endif
|
||||
|
||||
#ifndef LTC_FORTUNA_POOLS
|
||||
#warning LTC_FORTUNA_POOLS was not previously defined (old headers?)
|
||||
#define LTC_FORTUNA_POOLS 32
|
||||
#endif
|
||||
|
||||
#if LTC_FORTUNA_POOLS < 4 || LTC_FORTUNA_POOLS > 32
|
||||
#error LTC_FORTUNA_POOLS must be in [4..32]
|
||||
#endif
|
||||
|
||||
const struct ltc_prng_descriptor fortuna_desc = {
|
||||
"fortuna",
|
||||
64,
|
||||
&fortuna_start,
|
||||
&fortuna_add_entropy,
|
||||
&fortuna_ready,
|
||||
&fortuna_read,
|
||||
&fortuna_done,
|
||||
&fortuna_export,
|
||||
&fortuna_import,
|
||||
&fortuna_test
|
||||
};
|
||||
|
||||
/* update the IV */
|
||||
static void s_fortuna_update_iv(prng_state *prng)
|
||||
{
|
||||
int x;
|
||||
unsigned char *IV;
|
||||
/* update IV */
|
||||
IV = prng->u.fortuna.IV;
|
||||
for (x = 0; x < 16; x++) {
|
||||
IV[x] = (IV[x] + 1) & 255;
|
||||
if (IV[x] != 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
|
||||
/* get the current time in 100ms steps */
|
||||
static ulong64 s_fortuna_current_time(void)
|
||||
{
|
||||
ulong64 cur_time;
|
||||
#if defined(_WIN32)
|
||||
FILETIME CurrentTime;
|
||||
ULARGE_INTEGER ul;
|
||||
GetSystemTimeAsFileTime(&CurrentTime);
|
||||
ul.LowPart = CurrentTime.dwLowDateTime;
|
||||
ul.HighPart = CurrentTime.dwHighDateTime;
|
||||
cur_time = ul.QuadPart; /* now we have 100ns intervals since 1 January 1601 */
|
||||
cur_time -= CONST64(116444736000000000); /* subtract 100ns intervals between 1601-1970 */
|
||||
cur_time /= 10; /* 100ns intervals > microseconds */
|
||||
#elif defined(LTC_CLOCK_GETTIME)
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
cur_time = (ulong64)(ts.tv_sec) * 1000000 + (ulong64)(ts.tv_nsec) / 1000; /* get microseconds */
|
||||
#else
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
cur_time = (ulong64)(tv.tv_sec) * 1000000 + (ulong64)(tv.tv_usec); /* get microseconds */
|
||||
#endif
|
||||
return cur_time / 100;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* reseed the PRNG */
|
||||
static int s_fortuna_reseed(prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
hash_state md;
|
||||
ulong64 reset_cnt;
|
||||
int err, x;
|
||||
|
||||
#ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
|
||||
ulong64 now = s_fortuna_current_time();
|
||||
if (now == prng->u.fortuna.wd) {
|
||||
return CRYPT_OK;
|
||||
}
|
||||
#else
|
||||
if (++prng->u.fortuna.wd < LTC_FORTUNA_WD) {
|
||||
return CRYPT_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* new K == LTC_SHA256(K || s) where s == LTC_SHA256(P0) || LTC_SHA256(P1) ... */
|
||||
sha256_init(&md);
|
||||
if ((err = sha256_process(&md, prng->u.fortuna.K, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
reset_cnt = prng->u.fortuna.reset_cnt + 1;
|
||||
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if (x == 0 || ((reset_cnt >> (x-1)) & 1) == 0) {
|
||||
/* terminate this hash */
|
||||
if ((err = sha256_done(&prng->u.fortuna.pool[x], tmp)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
/* add it to the string */
|
||||
if ((err = sha256_process(&md, tmp, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
/* reset this pool */
|
||||
if ((err = sha256_init(&prng->u.fortuna.pool[x])) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* finish key */
|
||||
if ((err = sha256_done(&md, prng->u.fortuna.K)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
s_fortuna_update_iv(prng);
|
||||
|
||||
/* reset/update internals */
|
||||
prng->u.fortuna.pool0_len = 0;
|
||||
#ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
|
||||
prng->u.fortuna.wd = now;
|
||||
#else
|
||||
prng->u.fortuna.wd = 0;
|
||||
#endif
|
||||
prng->u.fortuna.reset_cnt = reset_cnt;
|
||||
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(&md, sizeof(md));
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
"Update Seed File"-compliant update of K
|
||||
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_update_seed(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
hash_state md;
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
/* new K = LTC_SHA256(K || in) */
|
||||
sha256_init(&md);
|
||||
if ((err = sha256_process(&md, prng->u.fortuna.K, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
if ((err = sha256_process(&md, in, inlen)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
/* finish key */
|
||||
if ((err = sha256_done(&md, prng->u.fortuna.K)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
s_fortuna_update_iv(prng);
|
||||
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(&md, sizeof(md));
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_start(prng_state *prng)
|
||||
{
|
||||
int err, x, y;
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
prng->ready = 0;
|
||||
|
||||
/* initialize the pools */
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = sha256_init(&prng->u.fortuna.pool[x])) != CRYPT_OK) {
|
||||
for (y = 0; y < x; y++) {
|
||||
sha256_done(&prng->u.fortuna.pool[y], tmp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
}
|
||||
prng->u.fortuna.pool_idx = prng->u.fortuna.pool0_len = 0;
|
||||
prng->u.fortuna.reset_cnt = prng->u.fortuna.wd = 0;
|
||||
|
||||
/* reset bufs */
|
||||
zeromem(prng->u.fortuna.K, 32);
|
||||
if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
sha256_done(&prng->u.fortuna.pool[x], tmp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
zeromem(prng->u.fortuna.IV, 16);
|
||||
|
||||
LTC_MUTEX_INIT(&prng->lock)
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int s_fortuna_add(unsigned long source, unsigned long pool, const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[2];
|
||||
int err;
|
||||
|
||||
/* ensure inlen <= 32 */
|
||||
if (inlen > 32) {
|
||||
inlen = 32;
|
||||
}
|
||||
|
||||
/* add s || length(in) || in to pool[pool_idx] */
|
||||
tmp[0] = (unsigned char)source;
|
||||
tmp[1] = (unsigned char)inlen;
|
||||
|
||||
if ((err = sha256_process(&prng->u.fortuna.pool[pool], tmp, 2)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = sha256_process(&prng->u.fortuna.pool[pool], in, inlen)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (pool == 0) {
|
||||
prng->u.fortuna.pool0_len += inlen;
|
||||
}
|
||||
return CRYPT_OK; /* success */
|
||||
}
|
||||
|
||||
/**
|
||||
Add random event to the PRNG state as proposed by the original paper.
|
||||
@param source The source this random event comes from (0 .. 255)
|
||||
@param pool The pool where to add the data to (0 .. LTC_FORTUNA_POOLS)
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_add_random_event(unsigned long source, unsigned long pool, const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
LTC_ARGCHK(source <= 255);
|
||||
LTC_ARGCHK(pool < LTC_FORTUNA_POOLS);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
err = s_fortuna_add(source, pool, in, inlen, prng);
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
err = s_fortuna_add(0, prng->u.fortuna.pool_idx, in, inlen, prng);
|
||||
|
||||
if (err == CRYPT_OK) {
|
||||
++(prng->u.fortuna.pool_idx);
|
||||
prng->u.fortuna.pool_idx %= LTC_FORTUNA_POOLS;
|
||||
}
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_ready(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
/* make sure the reseed doesn't fail because
|
||||
* of the chosen rate limit */
|
||||
#ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
|
||||
prng->u.fortuna.wd = s_fortuna_current_time() - 1;
|
||||
#else
|
||||
prng->u.fortuna.wd = LTC_FORTUNA_WD;
|
||||
#endif
|
||||
err = s_fortuna_reseed(prng);
|
||||
prng->ready = (err == CRYPT_OK) ? 1 : 0;
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[16];
|
||||
unsigned long tlen = 0;
|
||||
|
||||
if (outlen == 0 || prng == NULL || out == NULL) return 0;
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
if (!prng->ready) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* do we have to reseed? */
|
||||
if (prng->u.fortuna.pool0_len >= 64) {
|
||||
if (s_fortuna_reseed(prng) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
}
|
||||
|
||||
/* ensure that one reseed happened before allowing to read */
|
||||
if (prng->u.fortuna.reset_cnt == 0) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* now generate the blocks required */
|
||||
tlen = outlen;
|
||||
|
||||
/* handle whole blocks without the extra XMEMCPY */
|
||||
while (outlen >= 16) {
|
||||
/* encrypt the IV and store it */
|
||||
rijndael_ecb_encrypt(prng->u.fortuna.IV, out, &prng->u.fortuna.skey);
|
||||
out += 16;
|
||||
outlen -= 16;
|
||||
s_fortuna_update_iv(prng);
|
||||
}
|
||||
|
||||
/* left over bytes? */
|
||||
if (outlen > 0) {
|
||||
rijndael_ecb_encrypt(prng->u.fortuna.IV, tmp, &prng->u.fortuna.skey);
|
||||
XMEMCPY(out, tmp, outlen);
|
||||
s_fortuna_update_iv(prng);
|
||||
}
|
||||
|
||||
/* generate new key */
|
||||
rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K , &prng->u.fortuna.skey);
|
||||
s_fortuna_update_iv(prng);
|
||||
|
||||
rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K+16, &prng->u.fortuna.skey);
|
||||
s_fortuna_update_iv(prng);
|
||||
|
||||
if (rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey) != CRYPT_OK) {
|
||||
tlen = 0;
|
||||
}
|
||||
|
||||
LBL_UNLOCK:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return tlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_done(prng_state *prng)
|
||||
{
|
||||
int err, x;
|
||||
unsigned char tmp[32];
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
prng->ready = 0;
|
||||
|
||||
/* terminate all the hashes */
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = sha256_done(&(prng->u.fortuna.pool[x]), tmp)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
}
|
||||
/* call cipher done when we invent one ;-) */
|
||||
err = CRYPT_OK; /* success */
|
||||
|
||||
LBL_UNLOCK:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
LTC_MUTEX_DESTROY(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
LTC_PRNG_EXPORT(fortuna)
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
if (inlen < (unsigned long)fortuna_desc.export_size) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if ((err = fortuna_start(prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = fortuna_update_seed(in, inlen, prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int fortuna_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
int err;
|
||||
|
||||
if ((err = sha256_test()) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
return rijndael_test();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,15 +1,8 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#ifdef LTC_HASH_HELPERS
|
||||
/**
|
||||
@file hash_memory.c
|
||||
Hash memory helper, Tom St Denis
|
||||
|
@ -63,7 +56,4 @@ LBL_ERR:
|
|||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
#endif /* #ifdef LTC_HASH_HELPERS */
|
|
@ -1,18 +1,10 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file hmac_done.c
|
||||
LTC_HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
|
||||
HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
|
||||
*/
|
||||
|
||||
#ifdef LTC_HMAC
|
||||
|
@ -20,10 +12,10 @@
|
|||
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
|
||||
|
||||
/**
|
||||
Terminate an LTC_HMAC session
|
||||
@param hmac The LTC_HMAC state
|
||||
@param out [out] The destination of the LTC_HMAC authentication tag
|
||||
@param outlen [in/out] The max size and resulting size of the LTC_HMAC authentication tag
|
||||
Terminate an HMAC session
|
||||
@param hmac The HMAC state
|
||||
@param out [out] The destination of the HMAC authentication tag
|
||||
@param outlen [in/out] The max size and resulting size of the HMAC authentication tag
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
|
||||
|
@ -57,12 +49,12 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
|
|||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* Get the hash of the first LTC_HMAC vector plus the data */
|
||||
/* Get the hash of the first HMAC vector plus the data */
|
||||
if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* Create the second LTC_HMAC vector vector for step (3) */
|
||||
/* Create the second HMAC vector vector for step (3) */
|
||||
for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
|
||||
buf[i] = hmac->key[i] ^ 0x5C;
|
||||
}
|
||||
|
@ -89,7 +81,6 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
|
|||
|
||||
err = CRYPT_OK;
|
||||
LBL_ERR:
|
||||
XFREE(hmac->key);
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(isha, hashsize);
|
||||
zeromem(buf, hashsize);
|
||||
|
@ -103,7 +94,3 @@ LBL_ERR:
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,18 +1,10 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file hmac_init.c
|
||||
LTC_HMAC support, initialize state, Tom St Denis/Dobes Vandermeer
|
||||
HMAC support, initialize state, Tom St Denis/Dobes Vandermeer
|
||||
*/
|
||||
|
||||
#ifdef LTC_HMAC
|
||||
|
@ -20,8 +12,8 @@
|
|||
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
|
||||
|
||||
/**
|
||||
Initialize an LTC_HMAC context.
|
||||
@param hmac The LTC_HMAC state
|
||||
Initialize an HMAC context.
|
||||
@param hmac The HMAC state
|
||||
@param hash The index of the hash you want to use
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
|
@ -55,11 +47,10 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
|
|||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* allocate memory for key */
|
||||
hmac->key = XMALLOC(LTC_HMAC_BLOCKSIZE);
|
||||
if (hmac->key == NULL) {
|
||||
XFREE(buf);
|
||||
return CRYPT_MEM;
|
||||
/* check hash block fits */
|
||||
if (sizeof(hmac->key) < LTC_HMAC_BLOCKSIZE) {
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* (1) make sure we have a large enough key */
|
||||
|
@ -68,18 +59,16 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
|
|||
if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if(hashsize < LTC_HMAC_BLOCKSIZE) {
|
||||
zeromem((hmac->key) + hashsize, (size_t)(LTC_HMAC_BLOCKSIZE - hashsize));
|
||||
}
|
||||
keylen = hashsize;
|
||||
} else {
|
||||
XMEMCPY(hmac->key, key, (size_t)keylen);
|
||||
}
|
||||
|
||||
if(keylen < LTC_HMAC_BLOCKSIZE) {
|
||||
zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen));
|
||||
}
|
||||
}
|
||||
|
||||
/* Create the initial vector for step (3) */
|
||||
/* Create the initialization vector for step (3) */
|
||||
for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
|
||||
buf[i] = hmac->key[i] ^ 0x36;
|
||||
}
|
||||
|
@ -92,11 +81,8 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
|
|||
if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
goto done;
|
||||
|
||||
LBL_ERR:
|
||||
/* free the key since we failed */
|
||||
XFREE(hmac->key);
|
||||
done:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, LTC_HMAC_BLOCKSIZE);
|
||||
#endif
|
||||
|
@ -106,7 +92,3 @@ done:
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,29 +1,21 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file hmac_memory.c
|
||||
LTC_HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer
|
||||
HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer
|
||||
*/
|
||||
|
||||
#ifdef LTC_HMAC
|
||||
|
||||
/**
|
||||
LTC_HMAC a block of memory to produce the authentication tag
|
||||
HMAC a block of memory to produce the authentication tag
|
||||
@param hash The index of the hash to use
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param in The data to LTC_HMAC
|
||||
@param inlen The length of the data to LTC_HMAC (octets)
|
||||
@param in The data to HMAC
|
||||
@param inlen The length of the data to HMAC (octets)
|
||||
@param out [out] Destination of the authentication tag
|
||||
@param outlen [in/out] Max size and resulting size of authentication tag
|
||||
@return CRYPT_OK if successful
|
||||
|
@ -82,7 +74,3 @@ LBL_ERR:
|
|||
|
||||
#endif
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -0,0 +1,31 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file hmac_process.c
|
||||
HMAC support, process data, Tom St Denis/Dobes Vandermeer
|
||||
*/
|
||||
|
||||
#ifdef LTC_HMAC
|
||||
|
||||
/**
|
||||
Process data through HMAC
|
||||
@param hmac The hmac state
|
||||
@param in The data to send through HMAC
|
||||
@param inlen The length of the data to HMAC (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(hmac != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
if ((err = hash_is_valid(hmac->hash)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
return hash_descriptor[hmac->hash].process(&hmac->md, in, inlen);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,28 +1,20 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include <tomcrypt.h>
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file pkcs_5_2.c
|
||||
LTC_PKCS #5, Algorithm #2, Tom St Denis
|
||||
PKCS #5, Algorithm #2, Tom St Denis
|
||||
*/
|
||||
#ifdef LTC_PKCS_5
|
||||
|
||||
/**
|
||||
Execute LTC_PKCS #5 v2
|
||||
Execute PKCS #5 v2
|
||||
@param password The input password (or key)
|
||||
@param password_len The length of the password (octets)
|
||||
@param salt The salt (or nonce)
|
||||
@param salt_len The length of the salt (octets)
|
||||
@param iteration_count # of iterations desired for LTC_PKCS #5 v2 [read specs for more]
|
||||
@param iteration_count # of iterations desired for PKCS #5 v2 [read specs for more]
|
||||
@param hash_idx The index of the hash desired
|
||||
@param out [out] The destination for this algorithm
|
||||
@param outlen [in/out] The max size and resulting size of the algorithm output
|
||||
|
@ -123,7 +115,3 @@ LBL_ERR:
|
|||
|
||||
#endif
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -77,9 +69,9 @@ const struct ltc_hash_descriptor sha1_desc =
|
|||
};
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int _sha1_compress(hash_state *md, unsigned char *buf)
|
||||
static int ss_sha1_compress(hash_state *md, const unsigned char *buf)
|
||||
#else
|
||||
static int sha1_compress(hash_state *md, unsigned char *buf)
|
||||
static int s_sha1_compress(hash_state *md, const unsigned char *buf)
|
||||
#endif
|
||||
{
|
||||
#if SHA1_TARGET_ARM
|
||||
|
@ -582,10 +574,10 @@ static int sha1_compress(hash_state *md, unsigned char *buf)
|
|||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int sha1_compress(hash_state *md, unsigned char *buf)
|
||||
static int s_sha1_compress(hash_state *md, const unsigned char *buf)
|
||||
{
|
||||
int err;
|
||||
err = _sha1_compress(md, buf);
|
||||
err = ss_sha1_compress(md, buf);
|
||||
burn_stack(sizeof(ulong32) * 87);
|
||||
return err;
|
||||
}
|
||||
|
@ -616,7 +608,7 @@ int sha1_init(hash_state * md)
|
|||
@param inlen The length of the data (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
|
||||
HASH_PROCESS(sha1_process, s_sha1_compress, sha1, 64)
|
||||
|
||||
/**
|
||||
Terminate the hash to get the digest
|
||||
|
@ -649,7 +641,7 @@ int sha1_done(hash_state * md, unsigned char *out)
|
|||
while (md->sha1.curlen < 64) {
|
||||
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
|
||||
}
|
||||
sha1_compress(md, md->sha1.buf);
|
||||
s_sha1_compress(md, md->sha1.buf);
|
||||
md->sha1.curlen = 0;
|
||||
}
|
||||
|
||||
|
@ -660,7 +652,7 @@ int sha1_done(hash_state * md, unsigned char *out)
|
|||
|
||||
/* store length */
|
||||
STORE64H(md->sha1.length, md->sha1.buf+56);
|
||||
sha1_compress(md, md->sha1.buf);
|
||||
s_sha1_compress(md, md->sha1.buf);
|
||||
|
||||
/* copy output */
|
||||
for (i = 0; i < 5; i++) {
|
||||
|
@ -682,7 +674,7 @@ int sha1_test(void)
|
|||
return CRYPT_NOP;
|
||||
#else
|
||||
static const struct {
|
||||
char *msg;
|
||||
const char *msg;
|
||||
unsigned char hash[20];
|
||||
} tests[] = {
|
||||
{ "abc",
|
||||
|
@ -703,9 +695,9 @@ int sha1_test(void)
|
|||
|
||||
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
|
||||
sha1_init(&md);
|
||||
sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
|
||||
sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
|
||||
sha1_done(&md, tmp);
|
||||
if (XMEMCMP(tmp, tests[i].hash, 20) != 0) {
|
||||
if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) {
|
||||
return CRYPT_FAIL_TESTVECTOR;
|
||||
}
|
||||
}
|
||||
|
@ -716,7 +708,3 @@ int sha1_test(void)
|
|||
#endif
|
||||
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -1,14 +1,6 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file sha256.c
|
||||
|
@ -66,9 +58,9 @@ static const ulong32 K[64] = {
|
|||
|
||||
/* compress 512-bits */
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int _sha256_compress(hash_state * md, unsigned char *buf)
|
||||
static int ss_sha256_compress(hash_state * md, const unsigned char *buf)
|
||||
#else
|
||||
static int sha256_compress(hash_state * md, unsigned char *buf)
|
||||
static int s_sha256_compress(hash_state * md, const unsigned char *buf)
|
||||
#endif
|
||||
{
|
||||
ulong32 S[8], W[64], t0, t1;
|
||||
|
@ -176,10 +168,8 @@ static int sha256_compress(hash_state * md, unsigned char *buf)
|
|||
RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
|
||||
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
|
||||
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
|
||||
|
||||
#undef RND
|
||||
|
||||
#endif
|
||||
#undef RND
|
||||
|
||||
/* feedback */
|
||||
for (i = 0; i < 8; i++) {
|
||||
|
@ -189,10 +179,10 @@ static int sha256_compress(hash_state * md, unsigned char *buf)
|
|||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int sha256_compress(hash_state * md, unsigned char *buf)
|
||||
static int s_sha256_compress(hash_state * md, const unsigned char *buf)
|
||||
{
|
||||
int err;
|
||||
err = _sha256_compress(md, buf);
|
||||
err = ss_sha256_compress(md, buf);
|
||||
burn_stack(sizeof(ulong32) * 74);
|
||||
return err;
|
||||
}
|
||||
|
@ -227,7 +217,7 @@ int sha256_init(hash_state * md)
|
|||
@param inlen The length of the data (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
|
||||
HASH_PROCESS(sha256_process,s_sha256_compress, sha256, 64)
|
||||
|
||||
/**
|
||||
Terminate the hash to get the digest
|
||||
|
@ -261,7 +251,7 @@ int sha256_done(hash_state * md, unsigned char *out)
|
|||
while (md->sha256.curlen < 64) {
|
||||
md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
|
||||
}
|
||||
sha256_compress(md, md->sha256.buf);
|
||||
s_sha256_compress(md, md->sha256.buf);
|
||||
md->sha256.curlen = 0;
|
||||
}
|
||||
|
||||
|
@ -272,7 +262,7 @@ int sha256_done(hash_state * md, unsigned char *out)
|
|||
|
||||
/* store length */
|
||||
STORE64H(md->sha256.length, md->sha256.buf+56);
|
||||
sha256_compress(md, md->sha256.buf);
|
||||
s_sha256_compress(md, md->sha256.buf);
|
||||
|
||||
/* copy output */
|
||||
for (i = 0; i < 8; i++) {
|
||||
|
@ -294,7 +284,7 @@ int sha256_test(void)
|
|||
return CRYPT_NOP;
|
||||
#else
|
||||
static const struct {
|
||||
char *msg;
|
||||
const char *msg;
|
||||
unsigned char hash[32];
|
||||
} tests[] = {
|
||||
{ "abc",
|
||||
|
@ -317,9 +307,9 @@ int sha256_test(void)
|
|||
|
||||
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
|
||||
sha256_init(&md);
|
||||
sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
|
||||
sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
|
||||
sha256_done(&md, tmp);
|
||||
if (XMEMCMP(tmp, tests[i].hash, 32) != 0) {
|
||||
if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) {
|
||||
return CRYPT_FAIL_TESTVECTOR;
|
||||
}
|
||||
}
|
||||
|
@ -327,14 +317,6 @@ int sha256_test(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
#ifdef LTC_SHA224
|
||||
#include "sha224.c"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
|
@ -0,0 +1,303 @@
|
|||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@param sha512.c
|
||||
LTC_SHA512 by Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_SHA512
|
||||
|
||||
const struct ltc_hash_descriptor sha512_desc =
|
||||
{
|
||||
"sha512",
|
||||
5,
|
||||
64,
|
||||
128,
|
||||
|
||||
/* OID */
|
||||
{ 2, 16, 840, 1, 101, 3, 4, 2, 3, },
|
||||
9,
|
||||
|
||||
&sha512_init,
|
||||
&sha512_process,
|
||||
&sha512_done,
|
||||
&sha512_test,
|
||||
NULL
|
||||
};
|
||||
|
||||
/* the K array */
|
||||
static const ulong64 K[80] = {
|
||||
CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
|
||||
CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
|
||||
CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
|
||||
CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118),
|
||||
CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
|
||||
CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2),
|
||||
CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
|
||||
CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694),
|
||||
CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
|
||||
CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65),
|
||||
CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
|
||||
CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5),
|
||||
CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
|
||||
CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4),
|
||||
CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
|
||||
CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70),
|
||||
CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
|
||||
CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df),
|
||||
CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
|
||||
CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b),
|
||||
CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001),
|
||||
CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30),
|
||||
CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
|
||||
CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8),
|
||||
CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
|
||||
CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8),
|
||||
CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
|
||||
CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3),
|
||||
CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
|
||||
CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec),
|
||||
CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
|
||||
CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b),
|
||||
CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
|
||||
CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178),
|
||||
CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
|
||||
CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b),
|
||||
CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
|
||||
CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c),
|
||||
CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
|
||||
CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817)
|
||||
};
|
||||
|
||||
/* Various logical functions */
|
||||
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
|
||||
#define Maj(x,y,z) (((x | y) & z) | (x & y))
|
||||
#define S(x, n) ROR64c(x, n)
|
||||
#define R(x, n) (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)n))
|
||||
#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
|
||||
#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
|
||||
#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
|
||||
#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
|
||||
|
||||
/* compress 1024-bits */
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int ss_sha512_compress(hash_state * md, const unsigned char *buf)
|
||||
#else
|
||||
static int s_sha512_compress(hash_state * md, const unsigned char *buf)
|
||||
#endif
|
||||
{
|
||||
ulong64 S[8], W[80], t0, t1;
|
||||
int i;
|
||||
|
||||
/* copy state into S */
|
||||
for (i = 0; i < 8; i++) {
|
||||
S[i] = md->sha512.state[i];
|
||||
}
|
||||
|
||||
/* copy the state into 1024-bits into W[0..15] */
|
||||
for (i = 0; i < 16; i++) {
|
||||
LOAD64H(W[i], buf + (8*i));
|
||||
}
|
||||
|
||||
/* fill W[16..79] */
|
||||
for (i = 16; i < 80; i++) {
|
||||
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
|
||||
}
|
||||
|
||||
/* Compress */
|
||||
#ifdef LTC_SMALL_CODE
|
||||
for (i = 0; i < 80; i++) {
|
||||
t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
|
||||
t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
|
||||
S[7] = S[6];
|
||||
S[6] = S[5];
|
||||
S[5] = S[4];
|
||||
S[4] = S[3] + t0;
|
||||
S[3] = S[2];
|
||||
S[2] = S[1];
|
||||
S[1] = S[0];
|
||||
S[0] = t0 + t1;
|
||||
}
|
||||
#else
|
||||
#define RND(a,b,c,d,e,f,g,h,i) \
|
||||
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
|
||||
t1 = Sigma0(a) + Maj(a, b, c); \
|
||||
d += t0; \
|
||||
h = t0 + t1;
|
||||
|
||||
for (i = 0; i < 80; i += 8) {
|
||||
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
|
||||
RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
|
||||
RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
|
||||
RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
|
||||
RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
|
||||
RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
|
||||
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
|
||||
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* feedback */
|
||||
for (i = 0; i < 8; i++) {
|
||||
md->sha512.state[i] = md->sha512.state[i] + S[i];
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* compress 1024-bits */
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
static int s_sha512_compress(hash_state * md, const unsigned char *buf)
|
||||
{
|
||||
int err;
|
||||
err = ss_sha512_compress(md, buf);
|
||||
burn_stack(sizeof(ulong64) * 90 + sizeof(int));
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
Initialize the hash state
|
||||
@param md The hash state you wish to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sha512_init(hash_state * md)
|
||||
{
|
||||
LTC_ARGCHK(md != NULL);
|
||||
md->sha512.curlen = 0;
|
||||
md->sha512.length = 0;
|
||||
md->sha512.state[0] = CONST64(0x6a09e667f3bcc908);
|
||||
md->sha512.state[1] = CONST64(0xbb67ae8584caa73b);
|
||||
md->sha512.state[2] = CONST64(0x3c6ef372fe94f82b);
|
||||
md->sha512.state[3] = CONST64(0xa54ff53a5f1d36f1);
|
||||
md->sha512.state[4] = CONST64(0x510e527fade682d1);
|
||||
md->sha512.state[5] = CONST64(0x9b05688c2b3e6c1f);
|
||||
md->sha512.state[6] = CONST64(0x1f83d9abfb41bd6b);
|
||||
md->sha512.state[7] = CONST64(0x5be0cd19137e2179);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Process a block of memory though the hash
|
||||
@param md The hash state
|
||||
@param in The data to hash
|
||||
@param inlen The length of the data (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
HASH_PROCESS(sha512_process, s_sha512_compress, sha512, 128)
|
||||
|
||||
/**
|
||||
Terminate the hash to get the digest
|
||||
@param md The hash state
|
||||
@param out [out] The destination of the hash (64 bytes)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sha512_done(hash_state * md, unsigned char *out)
|
||||
{
|
||||
int i;
|
||||
|
||||
LTC_ARGCHK(md != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* increase the length of the message */
|
||||
md->sha512.length += md->sha512.curlen * CONST64(8);
|
||||
|
||||
/* append the '1' bit */
|
||||
md->sha512.buf[md->sha512.curlen++] = (unsigned char)0x80;
|
||||
|
||||
/* if the length is currently above 112 bytes we append zeros
|
||||
* then compress. Then we can fall back to padding zeros and length
|
||||
* encoding like normal.
|
||||
*/
|
||||
if (md->sha512.curlen > 112) {
|
||||
while (md->sha512.curlen < 128) {
|
||||
md->sha512.buf[md->sha512.curlen++] = (unsigned char)0;
|
||||
}
|
||||
s_sha512_compress(md, md->sha512.buf);
|
||||
md->sha512.curlen = 0;
|
||||
}
|
||||
|
||||
/* pad upto 120 bytes of zeroes
|
||||
* note: that from 112 to 120 is the 64 MSB of the length. We assume that you won't hash
|
||||
* > 2^64 bits of data... :-)
|
||||
*/
|
||||
while (md->sha512.curlen < 120) {
|
||||
md->sha512.buf[md->sha512.curlen++] = (unsigned char)0;
|
||||
}
|
||||
|
||||
/* store length */
|
||||
STORE64H(md->sha512.length, md->sha512.buf+120);
|
||||
s_sha512_compress(md, md->sha512.buf);
|
||||
|
||||
/* copy output */
|
||||
for (i = 0; i < 8; i++) {
|
||||
STORE64H(md->sha512.state[i], out+(8*i));
|
||||
}
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(md, sizeof(hash_state));
|
||||
#endif
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Self-test the hash
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
|
||||
*/
|
||||
int sha512_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
static const struct {
|
||||
const char *msg;
|
||||
unsigned char hash[64];
|
||||
} tests[] = {
|
||||
{ "abc",
|
||||
{ 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
|
||||
0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
|
||||
0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
|
||||
0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
|
||||
0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
|
||||
0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
|
||||
0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
|
||||
0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f }
|
||||
},
|
||||
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
||||
{ 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
|
||||
0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
|
||||
0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
|
||||
0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
|
||||
0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
|
||||
0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
|
||||
0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
|
||||
0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 }
|
||||
},
|
||||
};
|
||||
|
||||
int i;
|
||||
unsigned char tmp[64];
|
||||
hash_state md;
|
||||
|
||||
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
|
||||
sha512_init(&md);
|
||||
sha512_process(&md, (unsigned char *)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
|
||||
sha512_done(&md, tmp);
|
||||
if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512", i)) {
|
||||
return CRYPT_FAIL_TESTVECTOR;
|
||||
}
|
||||
}
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
100984
vendor/github.com/mutecomm/go-sqlcipher/sqlite3.c → vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3.c
generated
vendored
100984
vendor/github.com/mutecomm/go-sqlcipher/sqlite3.c → vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3.c
generated
vendored
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,103 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
// These wrappers are necessary because SQLITE_TRANSIENT
|
||||
// is a pointer constant, and cgo doesn't translate them correctly.
|
||||
|
||||
static inline void my_result_text(sqlite3_context *ctx, char *p, int np) {
|
||||
sqlite3_result_text(ctx, p, np, SQLITE_TRANSIENT);
|
||||
}
|
||||
|
||||
static inline void my_result_blob(sqlite3_context *ctx, void *p, int np) {
|
||||
sqlite3_result_blob(ctx, p, np, SQLITE_TRANSIENT);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"math"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const i64 = unsafe.Sizeof(int(0)) > 4
|
||||
|
||||
// SQLiteContext behave sqlite3_context
|
||||
type SQLiteContext C.sqlite3_context
|
||||
|
||||
// ResultBool sets the result of an SQL function.
|
||||
func (c *SQLiteContext) ResultBool(b bool) {
|
||||
if b {
|
||||
c.ResultInt(1)
|
||||
} else {
|
||||
c.ResultInt(0)
|
||||
}
|
||||
}
|
||||
|
||||
// ResultBlob sets the result of an SQL function.
|
||||
// See: sqlite3_result_blob, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultBlob(b []byte) {
|
||||
if i64 && len(b) > math.MaxInt32 {
|
||||
C.sqlite3_result_error_toobig((*C.sqlite3_context)(c))
|
||||
return
|
||||
}
|
||||
var p *byte
|
||||
if len(b) > 0 {
|
||||
p = &b[0]
|
||||
}
|
||||
C.my_result_blob((*C.sqlite3_context)(c), unsafe.Pointer(p), C.int(len(b)))
|
||||
}
|
||||
|
||||
// ResultDouble sets the result of an SQL function.
|
||||
// See: sqlite3_result_double, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultDouble(d float64) {
|
||||
C.sqlite3_result_double((*C.sqlite3_context)(c), C.double(d))
|
||||
}
|
||||
|
||||
// ResultInt sets the result of an SQL function.
|
||||
// See: sqlite3_result_int, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultInt(i int) {
|
||||
if i64 && (i > math.MaxInt32 || i < math.MinInt32) {
|
||||
C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i))
|
||||
} else {
|
||||
C.sqlite3_result_int((*C.sqlite3_context)(c), C.int(i))
|
||||
}
|
||||
}
|
||||
|
||||
// ResultInt64 sets the result of an SQL function.
|
||||
// See: sqlite3_result_int64, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultInt64(i int64) {
|
||||
C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i))
|
||||
}
|
||||
|
||||
// ResultNull sets the result of an SQL function.
|
||||
// See: sqlite3_result_null, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultNull() {
|
||||
C.sqlite3_result_null((*C.sqlite3_context)(c))
|
||||
}
|
||||
|
||||
// ResultText sets the result of an SQL function.
|
||||
// See: sqlite3_result_text, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultText(s string) {
|
||||
h := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
cs, l := (*C.char)(unsafe.Pointer(h.Data)), C.int(h.Len)
|
||||
C.my_result_text((*C.sqlite3_context)(c), cs, l)
|
||||
}
|
||||
|
||||
// ResultZeroblob sets the result of an SQL function.
|
||||
// See: sqlite3_result_zeroblob, http://sqlite.org/c3ref/result_blob.html
|
||||
func (c *SQLiteContext) ResultZeroblob(n int) {
|
||||
C.sqlite3_result_zeroblob((*C.sqlite3_context)(c), C.int(n))
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
)
|
||||
|
||||
// This file provides several different implementations for the
|
||||
// default embedded sqlite_crypt function.
|
||||
// This function is uses a caesar-cypher by default
|
||||
// and is used within the UserAuthentication module to encode
|
||||
// the password.
|
||||
//
|
||||
// The provided functions can be used as an overload to the sqlite_crypt
|
||||
// function through the use of the RegisterFunc on the connection.
|
||||
//
|
||||
// Because the functions can serv a purpose to an end-user
|
||||
// without using the UserAuthentication module
|
||||
// the functions are default compiled in.
|
||||
//
|
||||
// From SQLITE3 - user-auth.txt
|
||||
// The sqlite_user.pw field is encoded by a built-in SQL function
|
||||
// "sqlite_crypt(X,Y)". The two arguments are both BLOBs. The first argument
|
||||
// is the plaintext password supplied to the sqlite3_user_authenticate()
|
||||
// interface. The second argument is the sqlite_user.pw value and is supplied
|
||||
// so that the function can extract the "salt" used by the password encoder.
|
||||
// The result of sqlite_crypt(X,Y) is another blob which is the value that
|
||||
// ends up being stored in sqlite_user.pw. To verify credentials X supplied
|
||||
// by the sqlite3_user_authenticate() routine, SQLite runs:
|
||||
//
|
||||
// sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw)
|
||||
//
|
||||
// To compute an appropriate sqlite_user.pw value from a new or modified
|
||||
// password X, sqlite_crypt(X,NULL) is run. A new random salt is selected
|
||||
// when the second argument is NULL.
|
||||
//
|
||||
// The built-in version of of sqlite_crypt() uses a simple Caesar-cypher
|
||||
// which prevents passwords from being revealed by searching the raw database
|
||||
// for ASCII text, but is otherwise trivally broken. For better password
|
||||
// security, the database should be encrypted using the SQLite Encryption
|
||||
// Extension or similar technology. Or, the application can use the
|
||||
// sqlite3_create_function() interface to provide an alternative
|
||||
// implementation of sqlite_crypt() that computes a stronger password hash,
|
||||
// perhaps using a cryptographic hash function like SHA1.
|
||||
|
||||
// CryptEncoderSHA1 encodes a password with SHA1
|
||||
func CryptEncoderSHA1(pass []byte, hash interface{}) []byte {
|
||||
h := sha1.Sum(pass)
|
||||
return h[:]
|
||||
}
|
||||
|
||||
// CryptEncoderSSHA1 encodes a password with SHA1 with the
|
||||
// configured salt.
|
||||
func CryptEncoderSSHA1(salt string) func(pass []byte, hash interface{}) []byte {
|
||||
return func(pass []byte, hash interface{}) []byte {
|
||||
s := []byte(salt)
|
||||
p := append(pass, s...)
|
||||
h := sha1.Sum(p)
|
||||
return h[:]
|
||||
}
|
||||
}
|
||||
|
||||
// CryptEncoderSHA256 encodes a password with SHA256
|
||||
func CryptEncoderSHA256(pass []byte, hash interface{}) []byte {
|
||||
h := sha256.Sum256(pass)
|
||||
return h[:]
|
||||
}
|
||||
|
||||
// CryptEncoderSSHA256 encodes a password with SHA256
|
||||
// with the configured salt
|
||||
func CryptEncoderSSHA256(salt string) func(pass []byte, hash interface{}) []byte {
|
||||
return func(pass []byte, hash interface{}) []byte {
|
||||
s := []byte(salt)
|
||||
p := append(pass, s...)
|
||||
h := sha256.Sum256(p)
|
||||
return h[:]
|
||||
}
|
||||
}
|
||||
|
||||
// CryptEncoderSHA384 encodes a password with SHA384
|
||||
func CryptEncoderSHA384(pass []byte, hash interface{}) []byte {
|
||||
h := sha512.Sum384(pass)
|
||||
return h[:]
|
||||
}
|
||||
|
||||
// CryptEncoderSSHA384 encodes a password with SHA384
|
||||
// with the configured salt
|
||||
func CryptEncoderSSHA384(salt string) func(pass []byte, hash interface{}) []byte {
|
||||
return func(pass []byte, hash interface{}) []byte {
|
||||
s := []byte(salt)
|
||||
p := append(pass, s...)
|
||||
h := sha512.Sum384(p)
|
||||
return h[:]
|
||||
}
|
||||
}
|
||||
|
||||
// CryptEncoderSHA512 encodes a password with SHA512
|
||||
func CryptEncoderSHA512(pass []byte, hash interface{}) []byte {
|
||||
h := sha512.Sum512(pass)
|
||||
return h[:]
|
||||
}
|
||||
|
||||
// CryptEncoderSSHA512 encodes a password with SHA512
|
||||
// with the configured salt
|
||||
func CryptEncoderSSHA512(salt string) func(pass []byte, hash interface{}) []byte {
|
||||
return func(pass []byte, hash interface{}) []byte {
|
||||
s := []byte(salt)
|
||||
p := append(pass, s...)
|
||||
h := sha512.Sum512(p)
|
||||
return h[:]
|
||||
}
|
||||
}
|
||||
|
||||
// EOF
|
|
@ -0,0 +1,70 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build cgo
|
||||
// +build go1.8
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
|
||||
"context"
|
||||
)
|
||||
|
||||
// Ping implement Pinger.
|
||||
func (c *SQLiteConn) Ping(ctx context.Context) error {
|
||||
if c.db == nil {
|
||||
// must be ErrBadConn for sql to close the database
|
||||
return driver.ErrBadConn
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryContext implement QueryerContext.
|
||||
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, nv := range args {
|
||||
list[i] = namedValue(nv)
|
||||
}
|
||||
return c.query(ctx, query, list)
|
||||
}
|
||||
|
||||
// ExecContext implement ExecerContext.
|
||||
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, nv := range args {
|
||||
list[i] = namedValue(nv)
|
||||
}
|
||||
return c.exec(ctx, query, list)
|
||||
}
|
||||
|
||||
// PrepareContext implement ConnPrepareContext.
|
||||
func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
||||
return c.prepare(ctx, query)
|
||||
}
|
||||
|
||||
// BeginTx implement ConnBeginTx.
|
||||
func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||
return c.begin(ctx)
|
||||
}
|
||||
|
||||
// QueryContext implement QueryerContext.
|
||||
func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, nv := range args {
|
||||
list[i] = namedValue(nv)
|
||||
}
|
||||
return s.query(ctx, list)
|
||||
}
|
||||
|
||||
// ExecContext implement ExecerContext.
|
||||
func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||
list := make([]namedValue, len(args))
|
||||
for i, nv := range args {
|
||||
list[i] = namedValue(nv)
|
||||
}
|
||||
return s.exec(ctx, list)
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build libsqlite3
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DUSE_LIBSQLITE3
|
||||
#cgo linux LDFLAGS: -lsqlite3
|
||||
#cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3
|
||||
#cgo darwin CFLAGS: -I/usr/local/opt/sqlite/include
|
||||
#cgo openbsd LDFLAGS: -lsqlite3
|
||||
#cgo solaris LDFLAGS: -lsqlite3
|
||||
#cgo windows LDFLAGS: -lsqlite3
|
||||
*/
|
||||
import "C"
|
84
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_load_extension.go
generated
vendored
Normal file
84
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_load_extension.go
generated
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !sqlite_omit_load_extension
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func (c *SQLiteConn) loadExtensions(extensions []string) error {
|
||||
rv := C.sqlite3_enable_load_extension(c.db, 1)
|
||||
if rv != C.SQLITE_OK {
|
||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
||||
}
|
||||
|
||||
for _, extension := range extensions {
|
||||
if err := c.loadExtension(extension, nil); err != nil {
|
||||
C.sqlite3_enable_load_extension(c.db, 0)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
rv = C.sqlite3_enable_load_extension(c.db, 0)
|
||||
if rv != C.SQLITE_OK {
|
||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadExtension load the sqlite3 extension.
|
||||
func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
|
||||
rv := C.sqlite3_enable_load_extension(c.db, 1)
|
||||
if rv != C.SQLITE_OK {
|
||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
||||
}
|
||||
|
||||
if err := c.loadExtension(lib, &entry); err != nil {
|
||||
C.sqlite3_enable_load_extension(c.db, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
rv = C.sqlite3_enable_load_extension(c.db, 0)
|
||||
if rv != C.SQLITE_OK {
|
||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) loadExtension(lib string, entry *string) error {
|
||||
clib := C.CString(lib)
|
||||
defer C.free(unsafe.Pointer(clib))
|
||||
|
||||
var centry *C.char
|
||||
if entry != nil {
|
||||
centry = C.CString(*entry)
|
||||
defer C.free(unsafe.Pointer(centry))
|
||||
}
|
||||
|
||||
var errMsg *C.char
|
||||
defer C.sqlite3_free(unsafe.Pointer(errMsg))
|
||||
|
||||
rv := C.sqlite3_load_extension(c.db, clib, centry, &errMsg)
|
||||
if rv != C.SQLITE_OK {
|
||||
return errors.New(C.GoString(errMsg))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
24
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_load_extension_omit.go
generated
vendored
Normal file
24
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_load_extension_omit.go
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_omit_load_extension
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_OMIT_LOAD_EXTENSION
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func (c *SQLiteConn) loadExtensions(extensions []string) error {
|
||||
return errors.New("Extensions have been disabled for static builds")
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
|
||||
return errors.New("Extensions have been disabled for static builds")
|
||||
}
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_allow_uri_authority.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_allow_uri_authority.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_allow_uri_authority
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ALLOW_URI_AUTHORITY
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
16
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_app_armor.go
generated
vendored
Normal file
16
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_app_armor.go
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !windows
|
||||
// +build sqlite_app_armor
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_API_ARMOR
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_foreign_keys.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_foreign_keys.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_foreign_keys
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_DEFAULT_FOREIGN_KEYS=1
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_fts5 fts5
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS5
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_icu icu
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -licuuc -licui18n
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_ICU
|
||||
#cgo darwin CFLAGS: -I/usr/local/opt/icu4c/include
|
||||
#cgo darwin LDFLAGS: -L/usr/local/opt/icu4c/lib
|
||||
#cgo openbsd LDFLAGS: -lsqlite3
|
||||
*/
|
||||
import "C"
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_introspect.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_introspect.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_introspect
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_INTROSPECTION_PRAGMAS
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_json sqlite_json1 json1
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_JSON1
|
||||
*/
|
||||
import "C"
|
20
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_preupdate.go
generated
vendored
Normal file
20
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_preupdate.go
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
// Copyright (C) 2018 segment.com <friends@segment.com>
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build cgo
|
||||
|
||||
package sqlite3
|
||||
|
||||
// SQLitePreUpdateData represents all of the data available during a
|
||||
// pre-update hook call.
|
||||
type SQLitePreUpdateData struct {
|
||||
Conn *SQLiteConn
|
||||
Op int
|
||||
DatabaseName string
|
||||
TableName string
|
||||
OldRowID int64
|
||||
NewRowID int64
|
||||
}
|
112
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_preupdate_hook.go
generated
vendored
Normal file
112
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_preupdate_hook.go
generated
vendored
Normal file
|
@ -0,0 +1,112 @@
|
|||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
// Copyright (C) 2018 segment.com <friends@segment.com>
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_preupdate_hook
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_PREUPDATE_HOOK
|
||||
#cgo LDFLAGS: -lm
|
||||
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void preUpdateHookTrampoline(void*, sqlite3 *, int, char *, char *, sqlite3_int64, sqlite3_int64);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// RegisterPreUpdateHook sets the pre-update hook for a connection.
|
||||
//
|
||||
// The callback is passed a SQLitePreUpdateData struct with the data for
|
||||
// the update, as well as methods for fetching copies of impacted data.
|
||||
//
|
||||
// If there is an existing update hook for this connection, it will be
|
||||
// removed. If callback is nil the existing hook (if any) will be removed
|
||||
// without creating a new one.
|
||||
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
|
||||
if callback == nil {
|
||||
C.sqlite3_preupdate_hook(c.db, nil, nil)
|
||||
} else {
|
||||
C.sqlite3_preupdate_hook(c.db, (*[0]byte)(unsafe.Pointer(C.preUpdateHookTrampoline)), unsafe.Pointer(newHandle(c, callback)))
|
||||
}
|
||||
}
|
||||
|
||||
// Depth returns the source path of the write, see sqlite3_preupdate_depth()
|
||||
func (d *SQLitePreUpdateData) Depth() int {
|
||||
return int(C.sqlite3_preupdate_depth(d.Conn.db))
|
||||
}
|
||||
|
||||
// Count returns the number of columns in the row
|
||||
func (d *SQLitePreUpdateData) Count() int {
|
||||
return int(C.sqlite3_preupdate_count(d.Conn.db))
|
||||
}
|
||||
|
||||
func (d *SQLitePreUpdateData) row(dest []interface{}, new bool) error {
|
||||
for i := 0; i < d.Count() && i < len(dest); i++ {
|
||||
var val *C.sqlite3_value
|
||||
var src interface{}
|
||||
|
||||
// Initially I tried making this just a function pointer argument, but
|
||||
// it's absurdly complicated to pass C function pointers.
|
||||
if new {
|
||||
C.sqlite3_preupdate_new(d.Conn.db, C.int(i), &val)
|
||||
} else {
|
||||
C.sqlite3_preupdate_old(d.Conn.db, C.int(i), &val)
|
||||
}
|
||||
|
||||
switch C.sqlite3_value_type(val) {
|
||||
case C.SQLITE_INTEGER:
|
||||
src = int64(C.sqlite3_value_int64(val))
|
||||
case C.SQLITE_FLOAT:
|
||||
src = float64(C.sqlite3_value_double(val))
|
||||
case C.SQLITE_BLOB:
|
||||
len := C.sqlite3_value_bytes(val)
|
||||
blobptr := C.sqlite3_value_blob(val)
|
||||
src = C.GoBytes(blobptr, len)
|
||||
case C.SQLITE_TEXT:
|
||||
len := C.sqlite3_value_bytes(val)
|
||||
cstrptr := unsafe.Pointer(C.sqlite3_value_text(val))
|
||||
src = C.GoBytes(cstrptr, len)
|
||||
case C.SQLITE_NULL:
|
||||
src = nil
|
||||
}
|
||||
|
||||
err := convertAssign(&dest[i], src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Old populates dest with the row data to be replaced. This works similar to
|
||||
// database/sql's Rows.Scan()
|
||||
func (d *SQLitePreUpdateData) Old(dest ...interface{}) error {
|
||||
if d.Op == SQLITE_INSERT {
|
||||
return errors.New("There is no old row for INSERT operations")
|
||||
}
|
||||
return d.row(dest, false)
|
||||
}
|
||||
|
||||
// New populates dest with the replacement row data. This works similar to
|
||||
// database/sql's Rows.Scan()
|
||||
func (d *SQLitePreUpdateData) New(dest ...interface{}) error {
|
||||
if d.Op == SQLITE_DELETE {
|
||||
return errors.New("There is no new row for DELETE operations")
|
||||
}
|
||||
return d.row(dest, true)
|
||||
}
|
21
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_preupdate_omit.go
generated
vendored
Normal file
21
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_preupdate_omit.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
// Copyright (C) 2018 segment.com <friends@segment.com>
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !sqlite_preupdate_hook,cgo
|
||||
|
||||
package sqlite3
|
||||
|
||||
// RegisterPreUpdateHook sets the pre-update hook for a connection.
|
||||
//
|
||||
// The callback is passed a SQLitePreUpdateData struct with the data for
|
||||
// the update, as well as methods for fetching copies of impacted data.
|
||||
//
|
||||
// If there is an existing update hook for this connection, it will be
|
||||
// removed. If callback is nil the existing hook (if any) will be removed
|
||||
// without creating a new one.
|
||||
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
|
||||
// NOOP
|
||||
}
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_secure_delete.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_secure_delete.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_secure_delete
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_SECURE_DELETE=1
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_secure_delete_fast.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_secure_delete_fast.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_secure_delete_fast
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_SECURE_DELETE=FAST
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_stat4
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_STAT4
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
85
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_unlock_notify.c
generated
vendored
Normal file
85
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_unlock_notify.c
generated
vendored
Normal file
|
@ -0,0 +1,85 @@
|
|||
// Copyright (C) 2018 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
|
||||
#include <stdio.h>
|
||||
#include <sqlite3-binding.h>
|
||||
|
||||
extern int unlock_notify_wait(sqlite3 *db);
|
||||
|
||||
int
|
||||
_sqlite3_step_blocking(sqlite3_stmt *stmt)
|
||||
{
|
||||
int rv;
|
||||
sqlite3* db;
|
||||
|
||||
db = sqlite3_db_handle(stmt);
|
||||
for (;;) {
|
||||
rv = sqlite3_step(stmt);
|
||||
if (rv != SQLITE_LOCKED) {
|
||||
break;
|
||||
}
|
||||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) {
|
||||
break;
|
||||
}
|
||||
rv = unlock_notify_wait(db);
|
||||
if (rv != SQLITE_OK) {
|
||||
break;
|
||||
}
|
||||
sqlite3_reset(stmt);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
int
|
||||
_sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes)
|
||||
{
|
||||
int rv;
|
||||
sqlite3* db;
|
||||
|
||||
db = sqlite3_db_handle(stmt);
|
||||
for (;;) {
|
||||
rv = sqlite3_step(stmt);
|
||||
if (rv!=SQLITE_LOCKED) {
|
||||
break;
|
||||
}
|
||||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) {
|
||||
break;
|
||||
}
|
||||
rv = unlock_notify_wait(db);
|
||||
if (rv != SQLITE_OK) {
|
||||
break;
|
||||
}
|
||||
sqlite3_reset(stmt);
|
||||
}
|
||||
|
||||
*rowid = (long long) sqlite3_last_insert_rowid(db);
|
||||
*changes = (long long) sqlite3_changes(db);
|
||||
return rv;
|
||||
}
|
||||
|
||||
int
|
||||
_sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
|
||||
{
|
||||
int rv;
|
||||
|
||||
for (;;) {
|
||||
rv = sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
|
||||
if (rv!=SQLITE_LOCKED) {
|
||||
break;
|
||||
}
|
||||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) {
|
||||
break;
|
||||
}
|
||||
rv = unlock_notify_wait(db);
|
||||
if (rv != SQLITE_OK) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
#endif
|
93
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_unlock_notify.go
generated
vendored
Normal file
93
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_unlock_notify.go
generated
vendored
Normal file
|
@ -0,0 +1,93 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build cgo
|
||||
// +build sqlite_unlock_notify
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sqlite3-binding.h>
|
||||
|
||||
extern void unlock_notify_callback(void *arg, int argc);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type unlock_notify_table struct {
|
||||
sync.Mutex
|
||||
seqnum uint
|
||||
table map[uint]chan struct{}
|
||||
}
|
||||
|
||||
var unt unlock_notify_table = unlock_notify_table{table: make(map[uint]chan struct{})}
|
||||
|
||||
func (t *unlock_notify_table) add(c chan struct{}) uint {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
h := t.seqnum
|
||||
t.table[h] = c
|
||||
t.seqnum++
|
||||
return h
|
||||
}
|
||||
|
||||
func (t *unlock_notify_table) remove(h uint) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
delete(t.table, h)
|
||||
}
|
||||
|
||||
func (t *unlock_notify_table) get(h uint) chan struct{} {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
c, ok := t.table[h]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Non-existent key for unlcok-notify channel: %d", h))
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
//export unlock_notify_callback
|
||||
func unlock_notify_callback(argv unsafe.Pointer, argc C.int) {
|
||||
for i := 0; i < int(argc); i++ {
|
||||
parg := ((*(*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.uint)(nil))]*[1]uint)(argv))[i])
|
||||
arg := *parg
|
||||
h := arg[0]
|
||||
c := unt.get(h)
|
||||
c <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
//export unlock_notify_wait
|
||||
func unlock_notify_wait(db *C.sqlite3) C.int {
|
||||
// It has to be a bufferred channel to not block in sqlite_unlock_notify
|
||||
// as sqlite_unlock_notify could invoke the callback before it returns.
|
||||
c := make(chan struct{}, 1)
|
||||
defer close(c)
|
||||
|
||||
h := unt.add(c)
|
||||
defer unt.remove(h)
|
||||
|
||||
pargv := C.malloc(C.sizeof_uint)
|
||||
defer C.free(pargv)
|
||||
|
||||
argv := (*[1]uint)(pargv)
|
||||
argv[0] = h
|
||||
if rv := C.sqlite3_unlock_notify(db, (*[0]byte)(C.unlock_notify_callback), unsafe.Pointer(pargv)); rv != C.SQLITE_OK {
|
||||
return rv
|
||||
}
|
||||
|
||||
<-c
|
||||
|
||||
return C.SQLITE_OK
|
||||
}
|
289
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_userauth.go
generated
vendored
Normal file
289
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_userauth.go
generated
vendored
Normal file
|
@ -0,0 +1,289 @@
|
|||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_userauth
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_USER_AUTHENTICATION
|
||||
#cgo LDFLAGS: -lm
|
||||
#ifndef USE_LIBSQLITE3
|
||||
#include <sqlite3-binding.h>
|
||||
#else
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
static int
|
||||
_sqlite3_user_authenticate(sqlite3* db, const char* zUsername, const char* aPW, int nPW)
|
||||
{
|
||||
return sqlite3_user_authenticate(db, zUsername, aPW, nPW);
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_user_add(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin)
|
||||
{
|
||||
return sqlite3_user_add(db, zUsername, aPW, nPW, isAdmin);
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_user_change(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin)
|
||||
{
|
||||
return sqlite3_user_change(db, zUsername, aPW, nPW, isAdmin);
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_user_delete(sqlite3* db, const char* zUsername)
|
||||
{
|
||||
return sqlite3_user_delete(db, zUsername);
|
||||
}
|
||||
|
||||
static int
|
||||
_sqlite3_auth_enabled(sqlite3* db)
|
||||
{
|
||||
int exists = -1;
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
sqlite3_prepare_v2(db, "select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';", -1, &stmt, NULL);
|
||||
|
||||
while ( sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
exists = sqlite3_column_int(stmt, 0);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return exists;
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
SQLITE_AUTH = C.SQLITE_AUTH
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized")
|
||||
ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required")
|
||||
)
|
||||
|
||||
// Authenticate will perform an authentication of the provided username
|
||||
// and password against the database.
|
||||
//
|
||||
// If a database contains the SQLITE_USER table, then the
|
||||
// call to Authenticate must be invoked with an
|
||||
// appropriate username and password prior to enable read and write
|
||||
//access to the database.
|
||||
//
|
||||
// Return SQLITE_OK on success or SQLITE_ERROR if the username/password
|
||||
// combination is incorrect or unknown.
|
||||
//
|
||||
// If the SQLITE_USER table is not present in the database file, then
|
||||
// this interface is a harmless no-op returnning SQLITE_OK.
|
||||
func (c *SQLiteConn) Authenticate(username, password string) error {
|
||||
rv := c.authenticate(username, password)
|
||||
switch rv {
|
||||
case C.SQLITE_ERROR, C.SQLITE_AUTH:
|
||||
return ErrUnauthorized
|
||||
case C.SQLITE_OK:
|
||||
return nil
|
||||
default:
|
||||
return c.lastError()
|
||||
}
|
||||
}
|
||||
|
||||
// authenticate provides the actual authentication to SQLite.
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authenticate(username, password string) int {
|
||||
// Allocate C Variables
|
||||
cuser := C.CString(username)
|
||||
cpass := C.CString(password)
|
||||
|
||||
// Free C Variables
|
||||
defer func() {
|
||||
C.free(unsafe.Pointer(cuser))
|
||||
C.free(unsafe.Pointer(cpass))
|
||||
}()
|
||||
|
||||
return int(C._sqlite3_user_authenticate(c.db, cuser, cpass, C.int(len(password))))
|
||||
}
|
||||
|
||||
// AuthUserAdd can be used (by an admin user only)
|
||||
// to create a new user. When called on a no-authentication-required
|
||||
// database, this routine converts the database into an authentication-
|
||||
// required database, automatically makes the added user an
|
||||
// administrator, and logs in the current connection as that user.
|
||||
// The AuthUserAdd only works for the "main" database, not
|
||||
// for any ATTACH-ed databases. Any call to AuthUserAdd by a
|
||||
// non-admin user results in an error.
|
||||
func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
|
||||
isAdmin := 0
|
||||
if admin {
|
||||
isAdmin = 1
|
||||
}
|
||||
|
||||
rv := c.authUserAdd(username, password, isAdmin)
|
||||
switch rv {
|
||||
case C.SQLITE_ERROR, C.SQLITE_AUTH:
|
||||
return ErrAdminRequired
|
||||
case C.SQLITE_OK:
|
||||
return nil
|
||||
default:
|
||||
return c.lastError()
|
||||
}
|
||||
}
|
||||
|
||||
// authUserAdd enables the User Authentication if not enabled.
|
||||
// Otherwise it will add a user.
|
||||
//
|
||||
// When user authentication is already enabled then this function
|
||||
// can only be called by an admin.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authUserAdd(username, password string, admin int) int {
|
||||
// Allocate C Variables
|
||||
cuser := C.CString(username)
|
||||
cpass := C.CString(password)
|
||||
|
||||
// Free C Variables
|
||||
defer func() {
|
||||
C.free(unsafe.Pointer(cuser))
|
||||
C.free(unsafe.Pointer(cpass))
|
||||
}()
|
||||
|
||||
return int(C._sqlite3_user_add(c.db, cuser, cpass, C.int(len(password)), C.int(admin)))
|
||||
}
|
||||
|
||||
// AuthUserChange can be used to change a users
|
||||
// login credentials or admin privilege. Any user can change their own
|
||||
// login credentials. Only an admin user can change another users login
|
||||
// credentials or admin privilege setting. No user may change their own
|
||||
// admin privilege setting.
|
||||
func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error {
|
||||
isAdmin := 0
|
||||
if admin {
|
||||
isAdmin = 1
|
||||
}
|
||||
|
||||
rv := c.authUserChange(username, password, isAdmin)
|
||||
switch rv {
|
||||
case C.SQLITE_ERROR, C.SQLITE_AUTH:
|
||||
return ErrAdminRequired
|
||||
case C.SQLITE_OK:
|
||||
return nil
|
||||
default:
|
||||
return c.lastError()
|
||||
}
|
||||
}
|
||||
|
||||
// authUserChange allows to modify a user.
|
||||
// Users can change their own password.
|
||||
//
|
||||
// Only admins can change passwords for other users
|
||||
// and modify the admin flag.
|
||||
//
|
||||
// The admin flag of the current logged in user cannot be changed.
|
||||
// THis ensures that their is always an admin.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authUserChange(username, password string, admin int) int {
|
||||
// Allocate C Variables
|
||||
cuser := C.CString(username)
|
||||
cpass := C.CString(password)
|
||||
|
||||
// Free C Variables
|
||||
defer func() {
|
||||
C.free(unsafe.Pointer(cuser))
|
||||
C.free(unsafe.Pointer(cpass))
|
||||
}()
|
||||
|
||||
return int(C._sqlite3_user_change(c.db, cuser, cpass, C.int(len(password)), C.int(admin)))
|
||||
}
|
||||
|
||||
// AuthUserDelete can be used (by an admin user only)
|
||||
// to delete a user. The currently logged-in user cannot be deleted,
|
||||
// which guarantees that there is always an admin user and hence that
|
||||
// the database cannot be converted into a no-authentication-required
|
||||
// database.
|
||||
func (c *SQLiteConn) AuthUserDelete(username string) error {
|
||||
rv := c.authUserDelete(username)
|
||||
switch rv {
|
||||
case C.SQLITE_ERROR, C.SQLITE_AUTH:
|
||||
return ErrAdminRequired
|
||||
case C.SQLITE_OK:
|
||||
return nil
|
||||
default:
|
||||
return c.lastError()
|
||||
}
|
||||
}
|
||||
|
||||
// authUserDelete can be used to delete a user.
|
||||
//
|
||||
// This function can only be executed by an admin.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authUserDelete(username string) int {
|
||||
// Allocate C Variables
|
||||
cuser := C.CString(username)
|
||||
|
||||
// Free C Variables
|
||||
defer func() {
|
||||
C.free(unsafe.Pointer(cuser))
|
||||
}()
|
||||
|
||||
return int(C._sqlite3_user_delete(c.db, cuser))
|
||||
}
|
||||
|
||||
// AuthEnabled checks if the database is protected by user authentication
|
||||
func (c *SQLiteConn) AuthEnabled() (exists bool) {
|
||||
rv := c.authEnabled()
|
||||
if rv == 1 {
|
||||
exists = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// authEnabled perform the actual check for user authentication.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// 0 - Disabled
|
||||
// 1 - Enabled
|
||||
func (c *SQLiteConn) authEnabled() int {
|
||||
return int(C._sqlite3_auth_enabled(c.db))
|
||||
}
|
||||
|
||||
// EOF
|
152
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_userauth_omit.go
generated
vendored
Normal file
152
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_userauth_omit.go
generated
vendored
Normal file
|
@ -0,0 +1,152 @@
|
|||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !sqlite_userauth
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"C"
|
||||
)
|
||||
|
||||
// Authenticate will perform an authentication of the provided username
|
||||
// and password against the database.
|
||||
//
|
||||
// If a database contains the SQLITE_USER table, then the
|
||||
// call to Authenticate must be invoked with an
|
||||
// appropriate username and password prior to enable read and write
|
||||
//access to the database.
|
||||
//
|
||||
// Return SQLITE_OK on success or SQLITE_ERROR if the username/password
|
||||
// combination is incorrect or unknown.
|
||||
//
|
||||
// If the SQLITE_USER table is not present in the database file, then
|
||||
// this interface is a harmless no-op returnning SQLITE_OK.
|
||||
func (c *SQLiteConn) Authenticate(username, password string) error {
|
||||
// NOOP
|
||||
return nil
|
||||
}
|
||||
|
||||
// authenticate provides the actual authentication to SQLite.
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authenticate(username, password string) int {
|
||||
// NOOP
|
||||
return 0
|
||||
}
|
||||
|
||||
// AuthUserAdd can be used (by an admin user only)
|
||||
// to create a new user. When called on a no-authentication-required
|
||||
// database, this routine converts the database into an authentication-
|
||||
// required database, automatically makes the added user an
|
||||
// administrator, and logs in the current connection as that user.
|
||||
// The AuthUserAdd only works for the "main" database, not
|
||||
// for any ATTACH-ed databases. Any call to AuthUserAdd by a
|
||||
// non-admin user results in an error.
|
||||
func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
|
||||
// NOOP
|
||||
return nil
|
||||
}
|
||||
|
||||
// authUserAdd enables the User Authentication if not enabled.
|
||||
// Otherwise it will add a user.
|
||||
//
|
||||
// When user authentication is already enabled then this function
|
||||
// can only be called by an admin.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authUserAdd(username, password string, admin int) int {
|
||||
// NOOP
|
||||
return 0
|
||||
}
|
||||
|
||||
// AuthUserChange can be used to change a users
|
||||
// login credentials or admin privilege. Any user can change their own
|
||||
// login credentials. Only an admin user can change another users login
|
||||
// credentials or admin privilege setting. No user may change their own
|
||||
// admin privilege setting.
|
||||
func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error {
|
||||
// NOOP
|
||||
return nil
|
||||
}
|
||||
|
||||
// authUserChange allows to modify a user.
|
||||
// Users can change their own password.
|
||||
//
|
||||
// Only admins can change passwords for other users
|
||||
// and modify the admin flag.
|
||||
//
|
||||
// The admin flag of the current logged in user cannot be changed.
|
||||
// THis ensures that their is always an admin.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authUserChange(username, password string, admin int) int {
|
||||
// NOOP
|
||||
return 0
|
||||
}
|
||||
|
||||
// AuthUserDelete can be used (by an admin user only)
|
||||
// to delete a user. The currently logged-in user cannot be deleted,
|
||||
// which guarantees that there is always an admin user and hence that
|
||||
// the database cannot be converted into a no-authentication-required
|
||||
// database.
|
||||
func (c *SQLiteConn) AuthUserDelete(username string) error {
|
||||
// NOOP
|
||||
return nil
|
||||
}
|
||||
|
||||
// authUserDelete can be used to delete a user.
|
||||
//
|
||||
// This function can only be executed by an admin.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// C.SQLITE_OK (0)
|
||||
// C.SQLITE_ERROR (1)
|
||||
// C.SQLITE_AUTH (23)
|
||||
func (c *SQLiteConn) authUserDelete(username string) int {
|
||||
// NOOP
|
||||
return 0
|
||||
}
|
||||
|
||||
// AuthEnabled checks if the database is protected by user authentication
|
||||
func (c *SQLiteConn) AuthEnabled() (exists bool) {
|
||||
// NOOP
|
||||
return false
|
||||
}
|
||||
|
||||
// authEnabled perform the actual check for user authentication.
|
||||
//
|
||||
// This is not exported for usage in Go.
|
||||
// It is however exported for usage within SQL by the user.
|
||||
//
|
||||
// Returns:
|
||||
// 0 - Disabled
|
||||
// 1 - Enabled
|
||||
func (c *SQLiteConn) authEnabled() int {
|
||||
// NOOP
|
||||
return 0
|
||||
}
|
||||
|
||||
// EOF
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_vacuum_full.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_vacuum_full.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_vacuum_full
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=1
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_vacuum_incr.go
generated
vendored
Normal file
15
vendor/github.com/mutecomm/go-sqlcipher/v4/sqlite3_opt_vacuum_incr.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build sqlite_vacuum_incr
|
||||
|
||||
package sqlite3
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=2
|
||||
#cgo LDFLAGS: -lm
|
||||
*/
|
||||
import "C"
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue