source: bootcd/isolinux/syslinux-6.03/gpxe/src/crypto/cbc.c

Last change on this file was e16e8f2, checked in by Edwin Eefting <edwin@datux.nl>, 3 years ago

bootstuff

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19FILE_LICENCE ( GPL2_OR_LATER );
20
21#include <string.h>
22#include <assert.h>
23#include <gpxe/crypto.h>
24#include <gpxe/cbc.h>
25
26/** @file
27 *
28 * Cipher-block chaining
29 *
30 */
31
32/**
33 * XOR data blocks
34 *
35 * @v src               Input data
36 * @v dst               Second input data and output data buffer
37 * @v len               Length of data
38 */
39static void cbc_xor ( const void *src, void *dst, size_t len ) {
40        const uint32_t *srcl = src;
41        uint32_t *dstl = dst;
42        unsigned int i;
43
44        /* Assume that block sizes will always be dword-aligned, for speed */
45        assert ( ( len % sizeof ( *srcl ) ) == 0 );
46
47        for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
48                dstl[i] ^= srcl[i];
49}
50
51/**
52 * Encrypt data
53 *
54 * @v ctx               Context
55 * @v src               Data to encrypt
56 * @v dst               Buffer for encrypted data
57 * @v len               Length of data
58 * @v raw_cipher        Underlying cipher algorithm
59 * @v cbc_ctx           CBC context
60 */
61void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
62                   struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
63        size_t blocksize = raw_cipher->blocksize;
64
65        assert ( ( len % blocksize ) == 0 );
66
67        while ( len ) {
68                cbc_xor ( src, cbc_ctx, blocksize );
69                cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
70                memcpy ( cbc_ctx, dst, blocksize );
71                dst += blocksize;
72                src += blocksize;
73                len -= blocksize;
74        }
75}
76
77/**
78 * Decrypt data
79 *
80 * @v ctx               Context
81 * @v src               Data to decrypt
82 * @v dst               Buffer for decrypted data
83 * @v len               Length of data
84 * @v raw_cipher        Underlying cipher algorithm
85 * @v cbc_ctx           CBC context
86 */
87void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
88                   struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
89        size_t blocksize = raw_cipher->blocksize;
90
91        assert ( ( len % blocksize ) == 0 );
92
93        while ( len ) {
94                cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
95                cbc_xor ( cbc_ctx, dst, blocksize );
96                memcpy ( cbc_ctx, src, blocksize );
97                dst += blocksize;
98                src += blocksize;
99                len -= blocksize;
100        }
101}
Note: See TracBrowser for help on using the repository browser.