aboutsummaryrefslogtreecommitdiff
path: root/05/src/gcm.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 05:31:06 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 06:14:42 +0000
commit9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch)
tree31f328747acb934d365d423adfda144818ff5ad4 /05/src/gcm.c
downloadlab-01-05-master.tar.gz
publishHEADmaster
Diffstat (limited to '05/src/gcm.c')
-rw-r--r--05/src/gcm.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/05/src/gcm.c b/05/src/gcm.c
new file mode 100644
index 0000000..7d3ab68
--- /dev/null
+++ b/05/src/gcm.c
@@ -0,0 +1,143 @@
1int
2aes256_gcm_encrypt (const uint8_t *plaintext, int plaintext_len,
3 const uint8_t *aad, int aad_len,
4 const uint8_t key[AES_256_KEY_SIZE],
5 const uint8_t *nonce, int nonce_len,
6 uint8_t *ciphertext,
7 uint8_t tag[GCM_TAG_SIZE])
8{
9 int len = 0;
10 int ciphertext_len = 0;
11 int ok = 0;
12
13 if (!EVP_CIPHER_CTX_reset (cipher_ctx)) {
14 ERR_print_errors_fp (stderr);
15 return -1;
16 }
17
18 if (EVP_EncryptInit_ex2 (cipher_ctx, cipher_aes_gcm, NULL, NULL, NULL) != 1) {
19 ERR_print_errors_fp (stderr);
20 return -1;
21 }
22
23 /*
24 Set nonce length before setting key/nonce if using a non-default length.
25 For GCM, 12 bytes is the common default and recommended size.
26 */
27 if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_SET_IVLEN,
28 nonce_len, NULL) != 1) {
29 ERR_print_errors_fp (stderr);
30 return -1;
31 }
32
33 if (EVP_EncryptInit_ex (cipher_ctx, NULL, NULL, key, nonce) != 1) {
34 ERR_print_errors_fp (stderr);
35 return -1;
36 }
37
38 /*
39 AAD is authenticated but not encrypted.
40 Pass out=NULL for AAD.
41 */
42 if (aad != NULL && aad_len > 0) {
43 if (EVP_EncryptUpdate (cipher_ctx, NULL, &len, aad, aad_len) != 1) {
44 ERR_print_errors_fp (stderr);
45 return -1;
46 }
47 }
48
49 if (EVP_EncryptUpdate (cipher_ctx, ciphertext, &len,
50 plaintext, plaintext_len) != 1) {
51 ERR_print_errors_fp (stderr);
52 return -1;
53 }
54 ciphertext_len = len;
55
56 /*
57 Final for GCM typically produces no extra ciphertext bytes, but it must
58 still be called.
59 */
60 if (EVP_EncryptFinal_ex (cipher_ctx, ciphertext + ciphertext_len, &len) != 1) {
61 ERR_print_errors_fp (stderr);
62 return -1;
63 }
64 ciphertext_len += len;
65
66 /*
67 Get the authentication tag.
68 */
69 if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_GET_TAG,
70 GCM_TAG_SIZE, tag) != 1) {
71 ERR_print_errors_fp (stderr);
72 return -1;
73 }
74
75 return ciphertext_len;
76}
77
78int aes256_gcm_decrypt (const uint8_t *ciphertext, int ciphertext_len,
79 const uint8_t *aad, int aad_len,
80 const uint8_t tag[GCM_TAG_SIZE],
81 const uint8_t key[AES_256_KEY_SIZE],
82 const uint8_t *nonce, int nonce_len,
83 uint8_t *plaintext)
84{
85 int len = 0;
86 int plaintext_len = 0;
87 int ret = -1;
88
89 if (EVP_CIPHER_CTX_reset (cipher_ctx)) {
90 ERR_print_errors_fp (stderr);
91 return -1;
92 }
93
94 if (EVP_DecryptInit_ex2 (cipher_ctx, cipher_aes_gcm, NULL, NULL, NULL) != 1) {
95 ERR_print_errors_fp (stderr);
96 return -1;
97 }
98
99 if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_SET_IVLEN,
100 nonce_len, NULL) != 1) {
101 ERR_print_errors_fp (stderr);
102 return -1;
103 }
104
105 if (EVP_DecryptInit_ex2 (cipher_ctx, NULL, key, nonce, NULL) != 1) {
106 ERR_print_errors_fp (stderr);
107 return -1;
108 }
109
110 if (aad != NULL && aad_len > 0) {
111 if (EVP_DecryptUpdate (cipher_ctx, NULL, &len, aad, aad_len) != 1) {
112 ERR_print_errors_fp (stderr);
113 return -1;
114 }
115 }
116
117 if (EVP_DecryptUpdate (cipher_ctx, plaintext, &len,
118 ciphertext, ciphertext_len) != 1) {
119 ERR_print_errors_fp (stderr);
120 return -1;
121 }
122 plaintext_len = len;
123
124 /*
125 Set expected tag before finalizing.
126 */
127 if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_SET_TAG,
128 GCM_TAG_SIZE, (void *)tag) != 1) {
129 ERR_print_errors_fp (stderr);
130 return -1;
131 }
132
133 /*
134 Success only if tag verification passes.
135 */
136 if (EVP_DecryptFinal_ex (cipher_ctx, plaintext + plaintext_len, &len) != 1) {
137 ERR_print_errors_fp (stderr);
138 return -1;
139 }
140 plaintext_len += len;
141
142 return plaintext_len;
143}