|
| 1 | +/* |
| 2 | + * GRUB -- GRand Unified Bootloader |
| 3 | + * Copyright (C) 2025 Free Software Foundation, Inc. |
| 4 | + * |
| 5 | + * GRUB is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * GRUB is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <grub/test.h> |
| 20 | +#include <grub/dl.h> |
| 21 | +#include <grub/misc.h> |
| 22 | +#include <grub/crypto.h> |
| 23 | + |
| 24 | +#include "crypto_cipher_mode_vectors.h" |
| 25 | + |
| 26 | +GRUB_MOD_LICENSE ("GPLv3+"); |
| 27 | + |
| 28 | +/* Perform cipher lookup, handle init, and key setting. */ |
| 29 | +static grub_crypto_cipher_handle_t |
| 30 | +handle_init (struct vector vec, grub_crypto_cipher_handle_t handle) |
| 31 | +{ |
| 32 | + gcry_err_code_t err; |
| 33 | + |
| 34 | + const gcry_cipher_spec_t *cipher = grub_crypto_lookup_cipher_by_name (vec.cipher); |
| 35 | + grub_test_assert (cipher != NULL, "\n%s: cipher lookup failed for %s", vec.mode, vec.cipher); |
| 36 | + if (cipher == NULL) |
| 37 | + return NULL; |
| 38 | + |
| 39 | + handle = grub_crypto_cipher_open (cipher); |
| 40 | + grub_test_assert (handle != NULL, "\n%s: handle init failed for %s", vec.mode, vec.cipher); |
| 41 | + if (handle == NULL) |
| 42 | + return NULL; |
| 43 | + |
| 44 | + err = grub_crypto_cipher_set_key (handle, (grub_uint8_t *) vec.key, vec.keylen); |
| 45 | + grub_test_assert (err == GPG_ERR_NO_ERROR, "\n%s: key set of size %d failed for %s with err = %d", |
| 46 | + vec.mode, vec.keylen, vec.cipher, err); |
| 47 | + if (err != GPG_ERR_NO_ERROR) |
| 48 | + { |
| 49 | + grub_crypto_cipher_close (handle); |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + |
| 53 | + return handle; |
| 54 | +} |
| 55 | + |
| 56 | +static void |
| 57 | +ecb_test (struct vector vec) |
| 58 | +{ |
| 59 | + gcry_err_code_t gcry_err; |
| 60 | + grub_crypto_cipher_handle_t handle = NULL; |
| 61 | + grub_uint8_t *plaintext = NULL, *ciphertext = NULL; |
| 62 | + grub_int32_t rc; |
| 63 | + |
| 64 | + handle = handle_init (vec, handle); |
| 65 | + if (handle == NULL) |
| 66 | + return; |
| 67 | + |
| 68 | + /* Test encryption. */ |
| 69 | + ciphertext = grub_zalloc (vec.plen); |
| 70 | + grub_test_assert (ciphertext != NULL, "\necb: ciphertext buffer allocation failed"); |
| 71 | + if (ciphertext == NULL) |
| 72 | + goto out_handle; |
| 73 | + |
| 74 | + gcry_err = grub_crypto_ecb_encrypt (handle, ciphertext, vec.ptext, vec.plen); |
| 75 | + grub_test_assert (gcry_err == GPG_ERR_NO_ERROR, "\necb: encryption failed with err = %d", |
| 76 | + gcry_err); |
| 77 | + if (gcry_err != GPG_ERR_NO_ERROR) |
| 78 | + goto out_ct; |
| 79 | + |
| 80 | + rc = grub_memcmp (ciphertext, vec.ctext, vec.plen); |
| 81 | + grub_test_assert (rc == 0, "\necb: ciphertext mismatch after encryption"); |
| 82 | + if (rc != 0) |
| 83 | + goto out_ct; |
| 84 | + |
| 85 | + /* Test decryption. */ |
| 86 | + plaintext = grub_zalloc (vec.plen); |
| 87 | + grub_test_assert (plaintext != NULL, "\necb: plaintext buffer allocation failed"); |
| 88 | + if (plaintext == NULL) |
| 89 | + goto out_ct; |
| 90 | + |
| 91 | + gcry_err = grub_crypto_ecb_decrypt (handle, plaintext, ciphertext, vec.plen); |
| 92 | + grub_test_assert (gcry_err == GPG_ERR_NO_ERROR, "\necb: decryption failed failed with err = %d", |
| 93 | + gcry_err); |
| 94 | + if (gcry_err != GPG_ERR_NO_ERROR) |
| 95 | + goto out_pt; |
| 96 | + |
| 97 | + rc = grub_memcmp (plaintext, vec.ptext, vec.plen); |
| 98 | + grub_test_assert (rc == 0, "\necb: plaintext mismatch after decryption"); |
| 99 | + |
| 100 | + out_pt: |
| 101 | + grub_free(plaintext); |
| 102 | + out_ct: |
| 103 | + grub_free(ciphertext); |
| 104 | + out_handle: |
| 105 | + grub_crypto_cipher_close(handle); |
| 106 | +} |
| 107 | + |
| 108 | +static void |
| 109 | +cbc_test (struct vector vec) |
| 110 | +{ |
| 111 | + gcry_err_code_t gcry_err; |
| 112 | + grub_crypto_cipher_handle_t handle = NULL; |
| 113 | + grub_uint8_t *plaintext = NULL, *ciphertext = NULL; |
| 114 | + grub_uint32_t *iv = NULL; |
| 115 | + grub_int32_t rc; |
| 116 | + |
| 117 | + handle = handle_init (vec, handle); |
| 118 | + if (handle == NULL) |
| 119 | + return; |
| 120 | + |
| 121 | + /* Test Encryption */ |
| 122 | + iv = grub_malloc(vec.ivlen); |
| 123 | + grub_test_assert (iv != NULL, "\ncbc: IV buffer allocation failed"); |
| 124 | + if (iv == NULL) |
| 125 | + goto out_handle; |
| 126 | + |
| 127 | + grub_memcpy (iv, vec.iv_in, vec.ivlen); |
| 128 | + |
| 129 | + ciphertext = grub_zalloc (vec.plen); |
| 130 | + grub_test_assert (ciphertext != NULL, "\ncbc: ciphertext buffer allocation failed"); |
| 131 | + if (ciphertext == NULL) |
| 132 | + goto out_iv; |
| 133 | + |
| 134 | + gcry_err = grub_crypto_cbc_encrypt (handle, ciphertext, vec.ptext, vec.plen, iv); |
| 135 | + grub_test_assert (gcry_err == GPG_ERR_NO_ERROR, "\ncbc: encryption failed with err = %d", |
| 136 | + gcry_err); |
| 137 | + if (gcry_err != GPG_ERR_NO_ERROR) |
| 138 | + goto out_ct; |
| 139 | + |
| 140 | + rc = grub_memcmp (ciphertext, vec.ctext, vec.plen); |
| 141 | + grub_test_assert (rc == 0, "\ncbc: ciphertext mismatch after encryption"); |
| 142 | + if (rc != 0) |
| 143 | + goto out_ct; |
| 144 | + |
| 145 | + rc = grub_memcmp (iv, vec.iv_out, vec.ivlen); |
| 146 | + grub_test_assert (rc == 0, "\ncbc: IV out mismatch after encryption"); |
| 147 | + if (rc != 0) |
| 148 | + goto out_ct; |
| 149 | + |
| 150 | + /* Test Decryption */ |
| 151 | + grub_memcpy (iv, vec.iv_in, vec.ivlen); |
| 152 | + |
| 153 | + plaintext = grub_zalloc (vec.plen); |
| 154 | + grub_test_assert (plaintext != NULL, "\ncbc: plaintext buffer allocation failed"); |
| 155 | + if (plaintext == NULL) |
| 156 | + goto out_ct; |
| 157 | + |
| 158 | + gcry_err = grub_crypto_cbc_decrypt (handle, plaintext, ciphertext, vec.plen, iv); |
| 159 | + grub_test_assert (gcry_err == GPG_ERR_NO_ERROR, "\ncbc: decryption failed with err = %d", |
| 160 | + gcry_err); |
| 161 | + if (gcry_err != GPG_ERR_NO_ERROR) |
| 162 | + goto out_pt; |
| 163 | + |
| 164 | + rc = grub_memcmp (plaintext, vec.ptext, vec.plen); |
| 165 | + grub_test_assert (rc == 0, "\ncbc: plaintext mismatch after decryption"); |
| 166 | + |
| 167 | + out_pt: |
| 168 | + grub_free(plaintext); |
| 169 | + out_ct: |
| 170 | + grub_free(ciphertext); |
| 171 | + out_iv: |
| 172 | + grub_free(iv); |
| 173 | + out_handle: |
| 174 | + grub_crypto_cipher_close(handle); |
| 175 | +} |
| 176 | + |
| 177 | +static void |
| 178 | +crypto_cipher_mode_test (void) |
| 179 | +{ |
| 180 | + grub_size_t i; |
| 181 | + |
| 182 | + for (i = 0; i < ARRAY_SIZE (vecs); i++) |
| 183 | + { |
| 184 | + if (grub_strcmp (vecs[i].mode, "ecb") == 0) |
| 185 | + ecb_test(vecs[i]); |
| 186 | + else if (grub_strcmp (vecs[i].mode, "cbc") == 0) |
| 187 | + cbc_test(vecs[i]); |
| 188 | + else |
| 189 | + { |
| 190 | + grub_test_assert(0, "\n%s mode unsupported for testing", vecs[i].mode); |
| 191 | + return; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +/* Register example_test method as a functional test. */ |
| 197 | +GRUB_FUNCTIONAL_TEST (crypto_cipher_mode_test, crypto_cipher_mode_test); |
0 commit comments