aes-unwrap.c

Go to the documentation of this file.
00001 
00017 #include "includes.h"
00018 
00019 #include "common.h"
00020 #include "aes.h"
00021 
00032 int aes_unwrap(const u8 *kek, int n, const u8 *cipher, u8 *plain)
00033 {
00034         u8 a[8], *r, b[16];
00035         int i, j;
00036         void *ctx;
00037 
00038         /* 1) Initialize variables. */
00039         os_memcpy(a, cipher, 8);
00040         r = plain;
00041         os_memcpy(r, cipher + 8, 8 * n);
00042 
00043         ctx = aes_decrypt_init(kek, 16);
00044         if (ctx == NULL)
00045                 return -1;
00046 
00047         /* 2) Compute intermediate values.
00048          * For j = 5 to 0
00049          *     For i = n to 1
00050          *         B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
00051          *         A = MSB(64, B)
00052          *         R[i] = LSB(64, B)
00053          */
00054         for (j = 5; j >= 0; j--) {
00055                 r = plain + (n - 1) * 8;
00056                 for (i = n; i >= 1; i--) {
00057                         os_memcpy(b, a, 8);
00058                         b[7] ^= n * j + i;
00059 
00060                         os_memcpy(b + 8, r, 8);
00061                         aes_decrypt(ctx, b, b);
00062                         os_memcpy(a, b, 8);
00063                         os_memcpy(r, b + 8, 8);
00064                         r -= 8;
00065                 }
00066         }
00067         aes_decrypt_deinit(ctx);
00068 
00069         /* 3) Output results.
00070          *
00071          * These are already in @plain due to the location of temporary
00072          * variables. Just verify that the IV matches with the expected value.
00073          */
00074         for (i = 0; i < 8; i++) {
00075                 if (a[i] != 0xa6)
00076                         return -1;
00077         }
00078 
00079         return 0;
00080 }
00081 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:49 2009 for hostapd by  doxygen 1.6.1