cbc.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <string.h>
00022 #include <assert.h>
00023 #include <gpxe/crypto.h>
00024 #include <gpxe/cbc.h>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 static void cbc_xor ( const void *src, void *dst, size_t len ) {
00040 const uint32_t *srcl = src;
00041 uint32_t *dstl = dst;
00042 unsigned int i;
00043
00044
00045 assert ( ( len % sizeof ( *srcl ) ) == 0 );
00046
00047 for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
00048 dstl[i] ^= srcl[i];
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
00062 struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
00063 size_t blocksize = raw_cipher->blocksize;
00064
00065 assert ( ( len % blocksize ) == 0 );
00066
00067 while ( len ) {
00068 cbc_xor ( src, cbc_ctx, blocksize );
00069 cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
00070 memcpy ( cbc_ctx, dst, blocksize );
00071 dst += blocksize;
00072 src += blocksize;
00073 len -= blocksize;
00074 }
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
00088 struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
00089 size_t blocksize = raw_cipher->blocksize;
00090
00091 assert ( ( len % blocksize ) == 0 );
00092
00093 while ( len ) {
00094 cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
00095 cbc_xor ( cbc_ctx, dst, blocksize );
00096 memcpy ( cbc_ctx, src, blocksize );
00097 dst += blocksize;
00098 src += blocksize;
00099 len -= blocksize;
00100 }
00101 }