blob: 02947c1ed3655f306940cc39ac810b77676f41fd [file] [log] [blame]
Rich Salz440e5d82016-05-17 14:20:24 -04001/*
Matt Caswell54b40532021-07-29 15:41:35 +01002 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
cluceyd2458442016-03-03 05:56:10 +00003 *
Richard Levitte909f1a22018-12-06 13:05:25 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salz440e5d82016-05-17 14:20:24 -04005 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
cluceyd2458442016-03-03 05:56:10 +00008 */
Rich Salz440e5d82016-05-17 14:20:24 -04009
Pauli304d0702020-07-14 09:39:20 +100010/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
cluceyd2458442016-03-03 05:56:10 +000013#include <stdio.h>
Emilia Kasperc91a0a82016-03-14 12:21:44 +010014#include <openssl/opensslconf.h>
15
Rich Salz5c8e9d52017-04-16 09:17:39 -040016#include <string.h>
17#include <openssl/engine.h>
18#include <openssl/evp.h>
19#include <openssl/rand.h>
20#include "testutil.h"
21
22/* Use a buffer size which is not aligned to block size */
JitendraLulla49ea0f02017-11-11 12:01:58 +053023#define BUFFER_SIZE 17
Rich Salz5c8e9d52017-04-16 09:17:39 -040024
25#ifndef OPENSSL_NO_ENGINE
26static ENGINE *e;
Rich Salz5c8e9d52017-04-16 09:17:39 -040027
JitendraLulla49ea0f02017-11-11 12:01:58 +053028static int test_afalg_aes_cbc(int keysize_idx)
cluceyd2458442016-03-03 05:56:10 +000029{
30 EVP_CIPHER_CTX *ctx;
JitendraLulla49ea0f02017-11-11 12:01:58 +053031 const EVP_CIPHER *cipher;
Matt Caswella1933882016-03-07 12:36:20 +000032 unsigned char ebuf[BUFFER_SIZE + 32];
33 unsigned char dbuf[BUFFER_SIZE + 32];
Pauli514b7692021-06-30 10:21:17 +100034 const unsigned char *enc_result = NULL;
cluceyd2458442016-03-03 05:56:10 +000035 int encl, encf, decl, decf;
Rich Salz52924392017-04-14 20:32:20 -040036 int ret = 0;
Pauli514b7692021-06-30 10:21:17 +100037 static const unsigned char key[] =
38 "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06"
39 "\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
40 static const unsigned char iv[] =
41 "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
42 /* input = "Single block msg\n" 17 Bytes*/
43 static const unsigned char in[BUFFER_SIZE] =
44 "\x53\x69\x6e\x67\x6c\x65\x20\x62\x6c\x6f\x63\x6b\x20\x6d\x73\x67"
45 "\x0a";
46 static const unsigned char encresult_128[BUFFER_SIZE] =
47 "\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a"
48 "\x2d";
49 static const unsigned char encresult_192[BUFFER_SIZE] =
50 "\xf7\xe4\x26\xd1\xd5\x4f\x8f\x39\xb1\x9e\xe0\xdf\x61\xb9\xc2\x55"
51 "\xeb";
52 static const unsigned char encresult_256[BUFFER_SIZE] =
53 "\xa0\x76\x85\xfd\xc1\x65\x71\x9d\xc7\xe9\x13\x6e\xae\x55\x49\xb4"
54 "\x13";
55
56#ifdef OSSL_SANITIZE_MEMORY
57 /*
58 * Initialise the encryption & decryption buffers to pacify the memory
59 * sanitiser. The sanitiser doesn't know that this memory is modified
60 * by the engine, this tells it that all is good.
61 */
62 OPENSSL_cleanse(ebuf, sizeof(ebuf));
63 OPENSSL_cleanse(dbuf, sizeof(dbuf));
64#endif
cluceyd2458442016-03-03 05:56:10 +000065
JitendraLulla49ea0f02017-11-11 12:01:58 +053066 switch (keysize_idx) {
67 case 0:
68 cipher = EVP_aes_128_cbc();
69 enc_result = &encresult_128[0];
70 break;
71 case 1:
72 cipher = EVP_aes_192_cbc();
73 enc_result = &encresult_192[0];
74 break;
75 case 2:
76 cipher = EVP_aes_256_cbc();
77 enc_result = &encresult_256[0];
78 break;
79 default:
80 cipher = NULL;
81 }
Rich Salz52924392017-04-14 20:32:20 -040082 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
83 return 0;
cluceyd2458442016-03-03 05:56:10 +000084
Rich Salz52924392017-04-14 20:32:20 -040085 if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
86 || !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
Pauli514b7692021-06-30 10:21:17 +100087 || !TEST_true(EVP_CipherFinal_ex(ctx, ebuf + encl, &encf)))
cluceyd2458442016-03-03 05:56:10 +000088 goto end;
cluceyd2458442016-03-03 05:56:10 +000089 encl += encf;
90
JitendraLulla49ea0f02017-11-11 12:01:58 +053091 if (!TEST_mem_eq(enc_result, BUFFER_SIZE, ebuf, BUFFER_SIZE))
92 goto end;
93
Rich Salz52924392017-04-14 20:32:20 -040094 if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
95 || !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
96 || !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
Pauli514b7692021-06-30 10:21:17 +100097 || !TEST_true(EVP_CipherFinal_ex(ctx, dbuf + decl, &decf)))
cluceyd2458442016-03-03 05:56:10 +000098 goto end;
cluceyd2458442016-03-03 05:56:10 +000099 decl += decf;
100
Rich Salz52924392017-04-14 20:32:20 -0400101 if (!TEST_int_eq(decl, BUFFER_SIZE)
102 || !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
cluceyd2458442016-03-03 05:56:10 +0000103 goto end;
cluceyd2458442016-03-03 05:56:10 +0000104
Rich Salz52924392017-04-14 20:32:20 -0400105 ret = 1;
cluceyd2458442016-03-03 05:56:10 +0000106
107 end:
108 EVP_CIPHER_CTX_free(ctx);
Rich Salz52924392017-04-14 20:32:20 -0400109 return ret;
cluceyd2458442016-03-03 05:56:10 +0000110}
cluceyd2458442016-03-03 05:56:10 +0000111
Bernd Edlinger6f6a5e02021-10-04 19:45:19 +0200112static int test_pr16743(void)
113{
114 int ret = 0;
115 const EVP_CIPHER * cipher;
116 EVP_CIPHER_CTX *ctx;
117
118 if (!TEST_true(ENGINE_init(e)))
119 return 0;
120 cipher = ENGINE_get_cipher(e, NID_aes_128_cbc);
121 ctx = EVP_CIPHER_CTX_new();
122 if (cipher != NULL && ctx != NULL)
123 ret = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL);
124 TEST_true(ret);
125 EVP_CIPHER_CTX_free(ctx);
126 ENGINE_finish(e);
127 return ret;
128}
129
Pauliad887412017-07-18 11:48:27 +1000130int global_init(void)
cluceyd2458442016-03-03 05:56:10 +0000131{
cluceyd2458442016-03-03 05:56:10 +0000132 ENGINE_load_builtin_engines();
cluceyd2458442016-03-03 05:56:10 +0000133# ifndef OPENSSL_NO_STATIC_ENGINE
134 OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL);
135# endif
Pauliad887412017-07-18 11:48:27 +1000136 return 1;
137}
138#endif
cluceyd2458442016-03-03 05:56:10 +0000139
Pauliad887412017-07-18 11:48:27 +1000140int setup_tests(void)
141{
142#ifndef OPENSSL_NO_ENGINE
Richard Levittec517ac42018-02-07 22:18:44 +0100143 if ((e = ENGINE_by_id("afalg")) == NULL) {
Rich Salz52924392017-04-14 20:32:20 -0400144 /* Probably a platform env issue, not a test failure. */
Richard Levittec517ac42018-02-07 22:18:44 +0100145 TEST_info("Can't load AFALG engine");
146 } else {
Richard Levittec517ac42018-02-07 22:18:44 +0100147 ADD_ALL_TESTS(test_afalg_aes_cbc, 3);
Bernd Edlinger6f6a5e02021-10-04 19:45:19 +0200148 ADD_TEST(test_pr16743);
Richard Levittec517ac42018-02-07 22:18:44 +0100149 }
Rich Salz5c8e9d52017-04-16 09:17:39 -0400150#endif
151
Pauliad887412017-07-18 11:48:27 +1000152 return 1;
cluceyd2458442016-03-03 05:56:10 +0000153}
Pauliad887412017-07-18 11:48:27 +1000154
155#ifndef OPENSSL_NO_ENGINE
156void cleanup_tests(void)
157{
158 ENGINE_free(e);
159}
160#endif