Skip to content

Commit 807bbe6

Browse files
committed
More pgcrypto fixes: handle long messages correctly, suppress
compiler warnings. Marko Kreen and Kris Jurka.
1 parent 7f0b690 commit 807bbe6

File tree

12 files changed

+104
-11
lines changed

12 files changed

+104
-11
lines changed

contrib/pgcrypto/expected/3des.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ select decrypt_iv(decode('50735067b073bb93', 'hex'), '0123456', 'abcd', '3des');
5454
foo
5555
(1 row)
5656

57+
-- long message
58+
select encode(encrypt('Lets try a longer message.', '0123456789012345678901', '3des'), 'hex');
59+
encode
60+
------------------------------------------------------------------
61+
b71e3422269d0ded19468f33d65cd663c28e0871984792a7b3ba0ddcecec8d2c
62+
(1 row)
63+
64+
select decrypt(encrypt('Lets try a longer message.', '0123456789012345678901', '3des'), '0123456789012345678901', '3des');
65+
decrypt
66+
----------------------------
67+
Lets try a longer message.
68+
(1 row)
69+

contrib/pgcrypto/expected/blowfish.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,16 @@ select decrypt_iv(decode('95c7e89322525d59', 'hex'), '0123456', 'abcd', 'bf');
158158
foo
159159
(1 row)
160160

161+
-- long message
162+
select encode(encrypt('Lets try a longer message.', '0123456789', 'bf'), 'hex');
163+
encode
164+
------------------------------------------------------------------
165+
a76059f7a1b627b5b84080d9beb337714c7a7f8b70300023e5feb6dfa6813536
166+
(1 row)
167+
168+
select decrypt(encrypt('Lets try a longer message.', '0123456789', 'bf'), '0123456789', 'bf');
169+
decrypt
170+
----------------------------
171+
Lets try a longer message.
172+
(1 row)
173+

contrib/pgcrypto/expected/cast5.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,16 @@ select decrypt_iv(decode('384a970695ce016a', 'hex'),
7171
foo
7272
(1 row)
7373

74+
-- long message
75+
select encode(encrypt('Lets try a longer message.', '0123456789', 'cast5'), 'hex');
76+
encode
77+
------------------------------------------------------------------
78+
04fcffc91533e1505dadcb10766d9fed0937818e663e402384e049942ba60fff
79+
(1 row)
80+
81+
select decrypt(encrypt('Lets try a longer message.', '0123456789', 'cast5'), '0123456789', 'cast5');
82+
decrypt
83+
----------------------------
84+
Lets try a longer message.
85+
(1 row)
86+

contrib/pgcrypto/expected/des.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,16 @@ select decrypt_iv(decode('50735067b073bb93', 'hex'), '0123456', 'abcd', 'des');
4646
foo
4747
(1 row)
4848

49+
-- long message
50+
select encode(encrypt('Lets try a longer message.', '01234567', 'des'), 'hex');
51+
encode
52+
------------------------------------------------------------------
53+
5ad146043e5f30967e06a0fcbae602daf4ff2a5fd0ed12d6c5913cf85f1e36ca
54+
(1 row)
55+
56+
select decrypt(encrypt('Lets try a longer message.', '01234567', 'des'), '01234567', 'des');
57+
decrypt
58+
----------------------------
59+
Lets try a longer message.
60+
(1 row)
61+

contrib/pgcrypto/expected/rijndael.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,16 @@ select decrypt_iv(decode('2c24cb7da91d6d5699801268b0f5adad', 'hex'),
109109
foo
110110
(1 row)
111111

112+
-- long message
113+
select encode(encrypt('Lets try a longer message.', '0123456789', 'aes'), 'hex');
114+
encode
115+
------------------------------------------------------------------
116+
d9beb785dd5403ed02f66b755bb191b93ed93ca54930153f2c3b9ec7785056ad
117+
(1 row)
118+
119+
select decrypt(encrypt('Lets try a longer message.', '0123456789', 'aes'), '0123456789', 'aes');
120+
decrypt
121+
----------------------------
122+
Lets try a longer message.
123+
(1 row)
124+

contrib/pgcrypto/openssl.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.24 2005/07/11 15:07:59 tgl Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.25 2005/07/12 20:27:42 tgl Exp $
3030
*/
3131

3232
#include "postgres.h"
@@ -40,6 +40,11 @@
4040
#include <openssl/rand.h>
4141
#include <openssl/err.h>
4242

43+
/*
44+
* Max lengths we might want to handle.
45+
*/
46+
#define MAX_KEY (512/8)
47+
#define MAX_IV (128/8)
4348

4449
/*
4550
* Does OpenSSL support AES?
@@ -78,10 +83,13 @@
7883
#define AES_cbc_encrypt(src, dst, len, ctx, iv, enc) \
7984
do { \
8085
memcpy((dst), (src), (len)); \
81-
if (enc) \
86+
if (enc) { \
8287
aes_cbc_encrypt((ctx), (iv), (dst), (len)); \
83-
else \
88+
memcpy((iv), (dst) + (len) - 16, 16); \
89+
} else { \
8490
aes_cbc_decrypt((ctx), (iv), (dst), (len)); \
91+
memcpy(iv, (src) + (len) - 16, 16); \
92+
} \
8593
} while (0)
8694

8795
#endif /* old OPENSSL */
@@ -243,8 +251,8 @@ typedef struct
243251
CAST_KEY cast_key;
244252
AES_KEY aes_key;
245253
} u;
246-
uint8 key[EVP_MAX_KEY_LENGTH];
247-
uint8 iv[EVP_MAX_IV_LENGTH];
254+
uint8 key[MAX_KEY];
255+
uint8 iv[MAX_IV];
248256
unsigned klen;
249257
unsigned init;
250258
const struct ossl_cipher *ciph;

contrib/pgcrypto/sha2.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
3535
*
36-
* $PostgreSQL: pgsql/contrib/pgcrypto/sha2.c,v 1.3 2005/07/11 15:40:38 tgl Exp $
36+
* $PostgreSQL: pgsql/contrib/pgcrypto/sha2.c,v 1.4 2005/07/12 20:27:42 tgl Exp $
3737
*/
3838

3939
#include "postgres.h"
@@ -170,7 +170,7 @@ void SHA512_Transform(SHA512_CTX *, const uint8 *);
170170

171171
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
172172
/* Hash constant words K for SHA-256: */
173-
const static uint32 K256[64] = {
173+
static const uint32 K256[64] = {
174174
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
175175
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
176176
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
@@ -190,7 +190,7 @@ const static uint32 K256[64] = {
190190
};
191191

192192
/* Initial hash value H for SHA-256: */
193-
const static uint32 sha256_initial_hash_value[8] = {
193+
static const uint32 sha256_initial_hash_value[8] = {
194194
0x6a09e667UL,
195195
0xbb67ae85UL,
196196
0x3c6ef372UL,
@@ -202,7 +202,7 @@ const static uint32 sha256_initial_hash_value[8] = {
202202
};
203203

204204
/* Hash constant words K for SHA-384 and SHA-512: */
205-
const static uint64 K512[80] = {
205+
static const uint64 K512[80] = {
206206
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
207207
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
208208
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
@@ -246,7 +246,7 @@ const static uint64 K512[80] = {
246246
};
247247

248248
/* Initial hash value H for SHA-384 */
249-
const static uint64 sha384_initial_hash_value[8] = {
249+
static const uint64 sha384_initial_hash_value[8] = {
250250
0xcbbb9d5dc1059ed8ULL,
251251
0x629a292a367cd507ULL,
252252
0x9159015a3070dd17ULL,
@@ -258,7 +258,7 @@ const static uint64 sha384_initial_hash_value[8] = {
258258
};
259259

260260
/* Initial hash value H for SHA-512 */
261-
const static uint64 sha512_initial_hash_value[8] = {
261+
static const uint64 sha512_initial_hash_value[8] = {
262262
0x6a09e667f3bcc908ULL,
263263
0xbb67ae8584caa73bULL,
264264
0x3c6ef372fe94f82bULL,

contrib/pgcrypto/sql/3des.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ select decrypt(encrypt('foo', '0123456', '3des'), '0123456', '3des');
2424
select encode(encrypt_iv('foo', '0123456', 'abcd', '3des'), 'hex');
2525
select decrypt_iv(decode('50735067b073bb93', 'hex'), '0123456', 'abcd', '3des');
2626

27+
-- long message
28+
select encode(encrypt('Lets try a longer message.', '0123456789012345678901', '3des'), 'hex');
29+
select decrypt(encrypt('Lets try a longer message.', '0123456789012345678901', '3des'), '0123456789012345678901', '3des');
30+

contrib/pgcrypto/sql/blowfish.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ select decrypt(encrypt('foo', '0123456', 'bf'), '0123456', 'bf');
8585
select encode(encrypt_iv('foo', '0123456', 'abcd', 'bf'), 'hex');
8686
select decrypt_iv(decode('95c7e89322525d59', 'hex'), '0123456', 'abcd', 'bf');
8787

88+
-- long message
89+
select encode(encrypt('Lets try a longer message.', '0123456789', 'bf'), 'hex');
90+
select decrypt(encrypt('Lets try a longer message.', '0123456789', 'bf'), '0123456789', 'bf');
91+

contrib/pgcrypto/sql/cast5.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ select encode(encrypt_iv('foo', '0123456', 'abcd', 'cast5'), 'hex');
4040
select decrypt_iv(decode('384a970695ce016a', 'hex'),
4141
'0123456', 'abcd', 'cast5');
4242

43+
-- long message
44+
select encode(encrypt('Lets try a longer message.', '0123456789', 'cast5'), 'hex');
45+
select decrypt(encrypt('Lets try a longer message.', '0123456789', 'cast5'), '0123456789', 'cast5');
46+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy