-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagro-crypto.h
More file actions
167 lines (148 loc) · 5.48 KB
/
agro-crypto.h
File metadata and controls
167 lines (148 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* agro-crypt: Cryptographic Operations Module
*
* Copyright (c) Ronan Le Meillat - SCTG Development 2008-2012
* Licensed under the MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This module handles all cryptographic operations including:
* - ECC certificate generation and signing
* - AES-256 encryption/decryption
* - Base64 encoding/decoding
* - SHA-256 hashing
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/ec.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include "agro-crypt.h"
/**
* OpenSSL "Salted__" magic header for encrypted files
* This matches the format used by "openssl enc" command
*/
#define OPENSSL_ENC_MAGIC "Salted__"
#define SALT_SIZE 8
/**
* Initialize OpenSSL library
*
* This function must be called before using any OpenSSL functions.
* It loads algorithm implementations and error strings.
*/
static void init_openssl(void) {
static int initialized = 0;
if (!initialized) {
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
initialized = 1;
}
}
/**
* Generate a new ECC key pair using NIST P-192 curve
*
* The P-192 curve (also known as prime192v1 or secp192r1) is a NIST
* standard elliptic curve that provides approximately 96 bits of security.
*
* @return EVP_PKEY pointer on success, NULL on error
*/
static EVP_PKEY *generate_ecc_key(void) {
EVP_PKEY *pkey = NULL;
EC_KEY *ec_key = NULL;
/* Create an EC_KEY object for the prime192v1 curve */
ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
if (ec_key == NULL) {
fprintf(stderr, "Error: Failed to create EC_KEY\n");
return NULL;
}
/* Generate the key pair */
if (EC_KEY_generate_key(ec_key) != 1) {
fprintf(stderr, "Error: Failed to generate EC key pair\n");
EC_KEY_free(ec_key);
return NULL;
}
/* Wrap the EC_KEY in an EVP_PKEY structure for general use */
pkey = EVP_PKEY_new();
if (pkey == NULL || EVP_PKEY_set1_EC_KEY(pkey, ec_key) != 1) {
fprintf(stderr, "Error: Failed to wrap EC_KEY in EVP_PKEY\n");
EC_KEY_free(ec_key);
if (pkey) EVP_PKEY_free(pkey);
return NULL;
}
EC_KEY_free(ec_key);
return pkey;
}
/**
* Create a self-signed X.509 certificate request
*
* This creates a certificate with basic fields (serial number, validity period,
* subject name). The certificate is then ready to be signed by a CA.
*
* @param pkey Key pair to include in certificate
* @return X509 certificate pointer on success, NULL on error
*/
static X509 *create_certificate(EVP_PKEY *pkey) {
X509 *cert = NULL;
X509_NAME *name = NULL;
cert = X509_new();
if (cert == NULL) {
fprintf(stderr, "Error: Failed to create X509 certificate\n");
return NULL;
}
/* Set certificate version to V3 (value 2 = version 3) */
X509_set_version(cert, 2);
/* Generate a random serial number */
unsigned char serial_bytes[8];
RAND_bytes(serial_bytes, sizeof(serial_bytes));
ASN1_INTEGER *serial = ASN1_INTEGER_new();
ASN1_INTEGER_set(serial, *(long *)serial_bytes);
X509_set_serialNumber(cert, serial);
ASN1_INTEGER_free(serial);
/* Set validity period: now to +365 days */
X509_gmtime_adj(X509_get_notBefore(cert), 0);
X509_gmtime_adj(X509_get_notAfter(cert), 365L * 24L * 3600L);
/* Set the public key */
X509_set_pubkey(cert, pkey);
/* Set the subject name */
name = X509_get_subject_name(cert);
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC,
(unsigned char *)"FR", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
(unsigned char *)"SCTG Development", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
(unsigned char *)"Agro-Crypt Certificate", -1, -1, 0);
/* For now, issuer = subject (will be replaced when signed by CA) */
X509_set_issuer_name(cert, name);
return cert;
}
/**
* Sign a certificate with a CA using RSA-SHA256
*
* This function takes an unsigned certificate and signs it using the CA's
* private key. The signature algorithm is SHA-256 with RSA encryption.
*
* @param cert Certificate to sign
* @param ca_cert CA certificate (for issuer