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
20
FILE_LICENCE ( GPL2_OR_LATER );
28
/* Forcibly enable assertions */
35
#include <ipxe/crypto.h>
36
#include <ipxe/profile.h>
42
* @v cipher Cipher algorithm
44
* @v key_len Length of key
45
* @v iv Initialisation vector
46
* @v plaintext Plaintext data
47
* @v expected_ciphertext Expected ciphertext data
48
* @v len Length of data
49
* @ret ok Ciphertext is as expected
51
int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
52
size_t key_len, const void *iv, const void *plaintext,
53
const void *expected_ciphertext, size_t len ) {
54
uint8_t ctx[cipher->ctxsize];
55
uint8_t ciphertext[len];
58
/* Initialise cipher */
59
rc = cipher_setkey ( cipher, ctx, key, key_len );
61
cipher_setiv ( cipher, ctx, iv );
63
/* Perform encryption */
64
cipher_encrypt ( cipher, ctx, plaintext, ciphertext, len );
67
return ( memcmp ( ciphertext, expected_ciphertext, len ) == 0 );
73
* @v cipher Cipher algorithm
75
* @v key_len Length of key
76
* @v iv Initialisation vector
77
* @v ciphertext Ciphertext data
78
* @v expected_plaintext Expected plaintext data
79
* @v len Length of data
80
* @ret ok Plaintext is as expected
82
int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
83
size_t key_len, const void *iv, const void *ciphertext,
84
const void *expected_plaintext, size_t len ) {
85
uint8_t ctx[cipher->ctxsize];
86
uint8_t plaintext[len];
89
/* Initialise cipher */
90
rc = cipher_setkey ( cipher, ctx, key, key_len );
92
cipher_setiv ( cipher, ctx, iv );
94
/* Perform encryption */
95
cipher_decrypt ( cipher, ctx, ciphertext, plaintext, len );
98
return ( memcmp ( plaintext, expected_plaintext, len ) == 0 );
102
* Calculate CBC encryption or decryption cost
104
* @v cipher Cipher algorithm
105
* @v key_len Length of key
106
* @v op Encryption or decryption operation
107
* @ret cost Cost (in cycles per byte)
109
static unsigned long cbc_cost ( struct cipher_algorithm *cipher,
111
void ( * op ) ( struct cipher_algorithm *cipher,
112
void *ctx, const void *src,
113
void *dst, size_t len ) ) {
114
static uint8_t random[8192]; /* Too large for stack */
115
uint8_t key[key_len];
116
uint8_t iv[cipher->blocksize];
117
uint8_t ctx[cipher->ctxsize];
118
union profiler profiler;
119
unsigned long long elapsed;
124
/* Fill buffer with pseudo-random data */
126
for ( i = 0 ; i < sizeof ( random ) ; i++ )
128
for ( i = 0 ; i < sizeof ( key ) ; i++ )
130
for ( i = 0 ; i < sizeof ( iv ) ; i++ )
133
/* Initialise cipher */
134
rc = cipher_setkey ( cipher, ctx, key, key_len );
136
cipher_setiv ( cipher, ctx, iv );
139
profile ( &profiler );
140
op ( cipher, ctx, random, random, sizeof ( random ) );
141
elapsed = profile ( &profiler );
143
/* Round to nearest whole number of cycles per byte */
144
cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) );
150
* Calculate CBC encryption cost
152
* @v cipher Cipher algorithm
153
* @v key_len Length of key
154
* @ret cost Cost (in cycles per byte)
156
unsigned long cbc_cost_encrypt ( struct cipher_algorithm *cipher,
158
return cbc_cost ( cipher, key_len, cipher_encrypt );
162
* Calculate CBC decryption cost
164
* @v cipher Cipher algorithm
165
* @v key_len Length of key
166
* @ret cost Cost (in cycles per byte)
168
unsigned long cbc_cost_decrypt ( struct cipher_algorithm *cipher,
170
return cbc_cost ( cipher, key_len, cipher_decrypt );