aes_wrap.c File Reference

#include <stdlib.h>
#include <string.h>
#include <gpxe/crypto.h>
#include <gpxe/aes.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
int aes_wrap (const void *kek, const void *src, void *dest, int nblk)
 Wrap a key or other data using AES Key Wrap (RFC 3394).
int aes_unwrap (const void *kek, const void *src, void *dest, int nblk)
 Unwrap a key or other data using AES Key Wrap (RFC 3394).


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

int aes_wrap ( const void *  kek,
const void *  src,
void *  dest,
int  nblk 
)

Wrap a key or other data using AES Key Wrap (RFC 3394).

Parameters:
kek Key Encryption Key, 16 bytes
src Data to encrypt
nblk Number of 8-byte blocks in data
Return values:
dest Encrypted data (8 bytes longer than input)
The algorithm is implemented such that src and dest may point to the same buffer.

Definition at line 37 of file aes_wrap.c.

References aes_algorithm, AES_CTX_SIZE, cipher_encrypt, cipher_setkey(), free(), malloc(), memcpy, memmove(), memset(), and u8.

00038 {
00039         u8 *A = dest;
00040         u8 B[16];
00041         u8 *R;
00042         int i, j;
00043         void *aes_ctx = malloc ( AES_CTX_SIZE );
00044 
00045         if ( ! aes_ctx )
00046                 return -1;
00047 
00048         cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
00049 
00050         /* Set up */
00051         memset ( A, 0xA6, sizeof ( A ) );
00052         memmove ( dest + 8, src, nblk * 8 );
00053 
00054         /* Wrap */
00055         for ( j = 0; j < 6; j++ ) {
00056                 R = dest + 8;
00057                 for ( i = 1; i <= nblk; i++ ) {
00058                         memcpy ( B, A, 8 );
00059                         memcpy ( B + 8, R, 8 );
00060                         cipher_encrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
00061                         memcpy ( A, B, 8 );
00062                         A[7] ^= ( nblk * j ) + i;
00063                         memcpy ( R, B + 8, 8 );
00064                         R += 8;
00065                 }
00066         }
00067 
00068         free ( aes_ctx );
00069         return 0;
00070 }

int aes_unwrap ( const void *  kek,
const void *  src,
void *  dest,
int  nblk 
)

Unwrap a key or other data using AES Key Wrap (RFC 3394).

Parameters:
kek Key Encryption Key, 16 bytes
src Data to decrypt
nblk Number of 8-byte blocks in plaintext key
Return values:
dest Decrypted data (8 bytes shorter than input)
rc Zero on success, nonzero on IV mismatch
The algorithm is implemented such that src and dest may point to the same buffer.

Definition at line 84 of file aes_wrap.c.

References aes_algorithm, AES_CTX_SIZE, cipher_decrypt, cipher_setkey(), free(), malloc(), memcpy, memmove(), and u8.

Referenced by ccmp_kie_decrypt().

00085 {
00086         u8 A[8], B[16];
00087         u8 *R;
00088         int i, j;
00089         void *aes_ctx = malloc ( AES_CTX_SIZE );
00090 
00091         if ( ! aes_ctx )
00092                 return -1;
00093 
00094         cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
00095 
00096         /* Set up */
00097         memcpy ( A, src, 8 );
00098         memmove ( dest, src + 8, nblk * 8 );
00099 
00100         /* Unwrap */
00101         for ( j = 5; j >= 0; j-- ) {
00102                 R = dest + ( nblk - 1 ) * 8;
00103                 for ( i = nblk; i >= 1; i-- ) {
00104                         memcpy ( B, A, 8 );
00105                         memcpy ( B + 8, R, 8 );
00106                         B[7] ^= ( nblk * j ) + i;
00107                         cipher_decrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
00108                         memcpy ( A, B, 8 );
00109                         memcpy ( R, B + 8, 8 );
00110                         R -= 8;
00111                 }
00112         }
00113 
00114         free ( aes_ctx );
00115 
00116         /* Check IV */
00117         for ( i = 0; i < 8; i++ ) {
00118                 if ( A[i] != 0xA6 )
00119                         return -1;
00120         }
00121 
00122         return 0;
00123 }


Generated on Tue Apr 6 20:01:15 2010 for gPXE by  doxygen 1.5.7.1