OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gsCrypt.h
1
3#ifndef __GS_CRYPT_H__
4#define __GS_CRYPT_H__
5
6
7#include "gsLargeInt.h"
8#include "../md5.h"
9
10
11#if defined(__cplusplus)
12extern "C" {
13#endif
14
15
18// RSA
19//
20// Based on PKCS #1 v2.1, RSA Laboratories June 14, 2002
21//
22//
25#define GS_CRYPT_HASHSIZE GS_CRYPT_SHA1_HASHSIZE
26
27#define GS_CRYPT_SHA1_HASHSIZE 20
28#define GS_CRYPT_MD5_HASHSIZE 16
29
30//#define GS_CRYPT_RSA_ES_OAEP
31#define GS_CRYPT_RSA_ES_PKCS1v1_5
32
33#ifndef GS_CRYPT_RSA_BINARY_SIZE
34#define GS_CRYPT_RSA_BINARY_SIZE 1024
35#endif
36
37#define GS_CRYPT_RSA_BYTE_SIZE (GS_CRYPT_RSA_BINARY_SIZE/8) //1024/8 = 128
38
39#define GS_CRYPT_RSA_DATABLOCKSIZE (GS_CRYPT_RSA_BYTE_SIZE-GS_CRYPT_HASHSIZE-1)
40
41
42
43
44
47typedef struct
48{
49 gsLargeInt_t modulus;
50 gsLargeInt_t exponent;
52
53typedef struct
54{
55 gsi_u8 headerByte; // always 0x00
56 gsi_u8 maskedSeed[GS_CRYPT_HASHSIZE]; // not a MD5 hash, but must be same size
57 gsi_u8 maskedData[GS_CRYPT_RSA_DATABLOCKSIZE]; // data block xor'd
59
60typedef struct
61{
62 gsi_u8 headerByte[2]; // always 0x00 0x02
63 gsi_u8 data[GS_CRYPT_RSA_BYTE_SIZE-2]; // data block xor'd
65
66
67// The cipherText must be equal to GS_CRYPT_RSA_BYTE_SIZE
68// The plainText maximum len is:
69// OAEP: 62-bytes when using 1024-bit encryption (GS_CRYPT_RSA_BYTE_SIZE-2*GS_CRYPT_MD5_HASHSIZE-2)
70// PKCS1: 117-bytes when using 1024-bit encryption (GS_CRYPT_RSA_BYTE_SIZE-11)
71gsi_i32 gsCryptRSAEncryptBuffer(const gsCryptRSAKey *publicKey, const unsigned char *plainText, gsi_u32 len, unsigned char cipherText[GS_CRYPT_RSA_BYTE_SIZE]);
72gsi_i32 gsCryptRSAVerifySignedHash(const gsCryptRSAKey *publicKey, const unsigned char *hash, gsi_u32 hashLen, const unsigned char *sig, gsi_u32 sigLen);
73
74
75// These require the private key, which only the server should have. Included here for test purposes
76gsi_i32 gsCryptRSADecryptBuffer(const gsCryptRSAKey *privateKey, const unsigned char cipherText[GS_CRYPT_RSA_BYTE_SIZE], unsigned char *plainTextOut, gsi_u32 *lenOut);
77// Note: There is a debate on whether or not to sign-first-then-encrypt, or encrypt-first-then-sign
78// SignFirst: Decryption must take place before the signature is validated. This is a high overhead for invalid signatures.
79// EncryptFirst: Signature validation takes place before decryption, but the MAC is unencrypted to packet sniffers. (Exposes it to attack)
80// We use SignFirst since it's unlikely a client will receive a invalid signature DOS attack.
81gsi_i32 gsCryptRSASignData(const gsCryptRSAKey *privateKey, const unsigned char *plainText, gsi_u32 plainTextLen, unsigned char *signedDataOut, gsi_u32 *lenOut);
82gsi_i32 gsCryptRSASignHash(const gsCryptRSAKey *privateKey, const unsigned char *hash, gsi_u32 hashLen, unsigned char *signedDataOut, gsi_u32 *lenOut);
83
86#if defined(__cplusplus)
87}
88#endif
89
90#endif //__GS_CRYPT_H__
Definition gsCrypt.h:48
Definition gsCrypt.h:54
Definition gsCrypt.h:61