2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 /* Forcibly enable assertions */
39 #include <ipxe/crypto.h>
40 #include <ipxe/profile.h>
41 #include <ipxe/test.h>
42 #include "cipher_test.h"
44 /** Number of sample iterations for profiling */
45 #define PROFILE_COUNT 16
48 * Report a cipher encryption test result
51 * @v file Test code file
52 * @v line Test code line
54 void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
56 struct cipher_algorithm *cipher = test->cipher;
57 size_t len = test->len;
58 uint8_t ctx[cipher->ctxsize];
59 uint8_t ciphertext[len];
61 /* Initialise cipher */
62 okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
64 cipher_setiv ( cipher, ctx, test->iv );
66 /* Perform encryption */
67 cipher_encrypt ( cipher, ctx, test->plaintext, ciphertext, len );
69 /* Compare against expected ciphertext */
70 okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
74 * Report a cipher decryption test result
77 * @v file Test code file
78 * @v line Test code line
80 void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
82 struct cipher_algorithm *cipher = test->cipher;
83 size_t len = test->len;
84 uint8_t ctx[cipher->ctxsize];
85 uint8_t plaintext[len];
87 /* Initialise cipher */
88 okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
90 cipher_setiv ( cipher, ctx, test->iv );
92 /* Perform encryption */
93 cipher_decrypt ( cipher, ctx, test->ciphertext, plaintext, len );
95 /* Compare against expected plaintext */
96 okx ( memcmp ( plaintext, test->plaintext, len ) == 0, file, line );
100 * Report a cipher encryption and decryption test result
102 * @v test Cipher test
103 * @v file Test code file
104 * @v line Test code line
106 void cipher_okx ( struct cipher_test *test, const char *file,
107 unsigned int line ) {
109 cipher_encrypt_okx ( test, file, line );
110 cipher_decrypt_okx ( test, file, line );
114 * Calculate cipher encryption or decryption cost
116 * @v cipher Cipher algorithm
117 * @v key_len Length of key
118 * @v op Encryption or decryption operation
119 * @ret cost Cost (in cycles per byte)
122 cipher_cost ( struct cipher_algorithm *cipher, size_t key_len,
123 void ( * op ) ( struct cipher_algorithm *cipher, void *ctx,
124 const void *src, void *dst, size_t len ) ) {
125 static uint8_t random[8192]; /* Too large for stack */
126 uint8_t key[key_len];
127 uint8_t iv[cipher->blocksize];
128 uint8_t ctx[cipher->ctxsize];
129 struct profiler profiler;
134 /* Fill buffer with pseudo-random data */
136 for ( i = 0 ; i < sizeof ( random ) ; i++ )
138 for ( i = 0 ; i < sizeof ( key ) ; i++ )
140 for ( i = 0 ; i < sizeof ( iv ) ; i++ )
143 /* Initialise cipher */
144 rc = cipher_setkey ( cipher, ctx, key, key_len );
146 cipher_setiv ( cipher, ctx, iv );
148 /* Profile cipher operation */
149 memset ( &profiler, 0, sizeof ( profiler ) );
150 for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
151 profile_start ( &profiler );
152 op ( cipher, ctx, random, random, sizeof ( random ) );
153 profile_stop ( &profiler );
156 /* Round to nearest whole number of cycles per byte */
157 cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
164 * Calculate cipher encryption cost
166 * @v cipher Cipher algorithm
167 * @v key_len Length of key
168 * @ret cost Cost (in cycles per byte)
170 unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
172 return cipher_cost ( cipher, key_len, cipher_encrypt );
176 * Calculate cipher decryption cost
178 * @v cipher Cipher algorithm
179 * @v key_len Length of key
180 * @ret cost Cost (in cycles per byte)
182 unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
184 return cipher_cost ( cipher, key_len, cipher_decrypt );