iPXE
cipher_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Cipher self-tests
29  *
30  */
31 
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34 
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39 #include <ipxe/crypto.h>
40 #include <ipxe/profile.h>
41 #include <ipxe/test.h>
42 #include "cipher_test.h"
43 
44 /** Number of sample iterations for profiling */
45 #define PROFILE_COUNT 16
46 
47 /**
48  * Report a cipher encryption test result
49  *
50  * @v test Cipher test
51  * @v file Test code file
52  * @v line Test code line
53  */
54 void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
55  unsigned int line ) {
56  struct cipher_algorithm *cipher = test->cipher;
57  size_t len = test->len;
58  uint8_t ctx[cipher->ctxsize];
59  uint8_t ciphertext[len];
60 
61  /* Initialise cipher */
62  okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
63  file, line );
64  cipher_setiv ( cipher, ctx, test->iv );
65 
66  /* Perform encryption */
67  cipher_encrypt ( cipher, ctx, test->plaintext, ciphertext, len );
68 
69  /* Compare against expected ciphertext */
70  okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
71 }
72 
73 /**
74  * Report a cipher decryption test result
75  *
76  * @v test Cipher test
77  * @v file Test code file
78  * @v line Test code line
79  */
80 void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
81  unsigned int line ) {
82  struct cipher_algorithm *cipher = test->cipher;
83  size_t len = test->len;
84  uint8_t ctx[cipher->ctxsize];
85  uint8_t plaintext[len];
86 
87  /* Initialise cipher */
88  okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
89  file, line );
90  cipher_setiv ( cipher, ctx, test->iv );
91 
92  /* Perform encryption */
93  cipher_decrypt ( cipher, ctx, test->ciphertext, plaintext, len );
94 
95  /* Compare against expected plaintext */
96  okx ( memcmp ( plaintext, test->plaintext, len ) == 0, file, line );
97 }
98 
99 /**
100  * Report a cipher encryption and decryption test result
101  *
102  * @v test Cipher test
103  * @v file Test code file
104  * @v line Test code line
105  */
106 void cipher_okx ( struct cipher_test *test, const char *file,
107  unsigned int line ) {
108 
109  cipher_encrypt_okx ( test, file, line );
110  cipher_decrypt_okx ( test, file, line );
111 }
112 
113 /**
114  * Calculate cipher encryption or decryption cost
115  *
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)
120  */
121 static unsigned long
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;
130  unsigned long cost;
131  unsigned int i;
132  int rc;
133 
134  /* Fill buffer with pseudo-random data */
135  srand ( 0x1234568 );
136  for ( i = 0 ; i < sizeof ( random ) ; i++ )
137  random[i] = rand();
138  for ( i = 0 ; i < sizeof ( key ) ; i++ )
139  key[i] = rand();
140  for ( i = 0 ; i < sizeof ( iv ) ; i++ )
141  iv[i] = rand();
142 
143  /* Initialise cipher */
144  rc = cipher_setkey ( cipher, ctx, key, key_len );
145  assert ( rc == 0 );
146  cipher_setiv ( cipher, ctx, iv );
147 
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 );
154  }
155 
156  /* Round to nearest whole number of cycles per byte */
157  cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
158  sizeof ( random ) );
159 
160  return cost;
161 }
162 
163 /**
164  * Calculate cipher encryption cost
165  *
166  * @v cipher Cipher algorithm
167  * @v key_len Length of key
168  * @ret cost Cost (in cycles per byte)
169  */
170 unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
171  size_t key_len ) {
172  return cipher_cost ( cipher, key_len, cipher_encrypt );
173 }
174 
175 /**
176  * Calculate cipher decryption cost
177  *
178  * @v cipher Cipher algorithm
179  * @v key_len Length of key
180  * @ret cost Cost (in cycles per byte)
181  */
182 unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
183  size_t key_len ) {
184  return cipher_cost ( cipher, key_len, cipher_decrypt );
185 }
unsigned long profile_mean(struct profiler *profiler)
Get mean sample value.
Definition: profile.c:241
size_t blocksize
Block size.
Definition: crypto.h:54
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static unsigned long cipher_cost(struct cipher_algorithm *cipher, size_t key_len, void(*op)(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len))
Calculate cipher encryption or decryption cost.
Definition: cipher_test.c:122
Self-test infrastructure.
Cryptographic API.
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
A data structure for storing profiling information.
Definition: profile.h:26
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
unsigned long cipher_cost_decrypt(struct cipher_algorithm *cipher, size_t key_len)
Calculate cipher decryption cost.
Definition: cipher_test.c:182
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:202
void cipher_decrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher decryption test result.
Definition: cipher_test.c:80
A cipher test.
Definition: cipher_test.h:17
void cipher_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher encryption and decryption test result.
Definition: cipher_test.c:106
#define okx(success, file, line)
Report test result.
Definition: test.h:44
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int rand(void)
Definition: stdlib.h:59
static void srand(unsigned int seed)
Definition: stdlib.h:63
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
Profiling.
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:30
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:52
uint32_t cost
Root path cost.
Definition: stp.h:28
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:212
Cipher self-tests.
#define PROFILE_COUNT
Number of sample iterations for profiling.
Definition: cipher_test.c:45
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
uint32_t len
Length.
Definition: ena.h:14
A cipher algorithm.
Definition: crypto.h:48
void cipher_encrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher encryption test result.
Definition: cipher_test.c:54
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:113
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned long cipher_cost_encrypt(struct cipher_algorithm *cipher, size_t key_len)
Calculate cipher encryption cost.
Definition: cipher_test.c:170
String functions.
union @376 key
Sense key.
Definition: scsi.h:18
static int test
Definition: epic100.c:73
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:187
static void cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv)
Definition: crypto.h:192
void * memset(void *dest, int character, size_t len) __nonnull