Rizin
unix-like reverse engineering framework and cli tools
aes-find.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007 Victor Muñoz
2 // SPDX-License-Identifier: CC0-1.0
3 
4 /*
5  * Find expanded AES keys in memory
6  *
7  * Algorithm discovered and developed by Victor Muñoz
8  * - PoC and source published at 24c3 at December 2007
9  *
10  * Thanks for the great moments and code snippets!
11  *
12  * This source is public domain. Feel free to use it and distribute it.
13  */
14 
15 #include <rz_search.h>
16 #include <rz_crypto/rz_aes.h>
17 
18 #define AES128_SEARCH_LENGTH 24
19 #define AES192_SEARCH_LENGTH 32
20 #define AES256_SEARCH_LENGTH 40
21 #define AES128_KEY_LENGTH 16
22 #define AES192_KEY_LENGTH 24
23 #define AES256_KEY_LENGTH 32
24 
25 static bool aes256_key_test(const unsigned char *buf) {
26  bool word1 = buf[32] == (buf[0] ^ Sbox[buf[29]] ^ 1) && buf[33] == (buf[1] ^ Sbox[buf[30]]) && buf[34] == (buf[2] ^ Sbox[buf[31]]) && buf[35] == (buf[3] ^ Sbox[buf[28]]);
27  bool word2 = (buf[36] == (buf[4] ^ buf[32]) && buf[37] == (buf[5] ^ buf[33]) && buf[38] == (buf[6] ^ buf[34]) && buf[39] == (buf[7] ^ buf[35]));
28  return word1 && word2;
29 }
30 
31 static bool aes192_key_test(const unsigned char *buf) {
32  bool word1 = buf[24] == (buf[0] ^ Sbox[buf[21]] ^ 1) && buf[25] == (buf[1] ^ Sbox[buf[22]]) && buf[26] == (buf[2] ^ Sbox[buf[23]]) && buf[27] == (buf[3] ^ Sbox[buf[20]]);
33  bool word2 = buf[28] == (buf[4] ^ buf[24]) && buf[29] == (buf[5] ^ buf[25]) && buf[30] == (buf[6] ^ buf[26]) && buf[31] == (buf[7] ^ buf[27]);
34  return word1 && word2;
35 }
36 
37 static bool aes128_key_test(const unsigned char *buf) {
38  bool word1 = buf[16] == (buf[0] ^ Sbox[buf[13]] ^ 1) && buf[17] == (buf[1] ^ Sbox[buf[14]]) && buf[18] == (buf[2] ^ Sbox[buf[15]]) && buf[19] == (buf[3] ^ Sbox[buf[12]]);
39  bool word2 = buf[20] == (buf[4] ^ buf[16]) && buf[21] == (buf[5] ^ buf[17]) && buf[22] == (buf[6] ^ buf[18]) && buf[23] == (buf[7] ^ buf[19]);
40  return word1 && word2;
41 }
42 
44  int i, t, last = len - AES128_SEARCH_LENGTH;
46  RzSearchKeyword *kw;
47  const int old_nhits = s->nhits;
48 
49  rz_list_foreach (s->kws, iter, kw) {
50  if (last >= 0) {
51  for (i = 0; i < last; i++) {
52  if (aes128_key_test(buf + i)) {
54  t = rz_search_hit_new(s, kw, from + i);
55  if (!t) {
56  return -1;
57  }
58  if (t > 1) {
59  return s->nhits - old_nhits;
60  }
62  }
63  if (len - i - AES192_SEARCH_LENGTH >= 0 && aes192_key_test(buf + i)) {
65  t = rz_search_hit_new(s, kw, from + i);
66  if (!t) {
67  return -1;
68  }
69  if (t > 1) {
70  return s->nhits - old_nhits;
71  }
73  }
74  if (len - i - AES256_SEARCH_LENGTH >= 0 && aes256_key_test(buf + i)) {
76  t = rz_search_hit_new(s, kw, from + i);
77  if (!t) {
78  return -1;
79  }
80  if (t > 1) {
81  return s->nhits - old_nhits;
82  }
84  }
85  }
86  }
87  }
88  return -1;
89 }
size_t len
Definition: 6502dis.c:15
#define AES128_SEARCH_LENGTH
Definition: aes-find.c:18
RZ_API int rz_search_aes_update(RzSearch *s, ut64 from, const ut8 *buf, int len)
Definition: aes-find.c:43
#define AES256_SEARCH_LENGTH
Definition: aes-find.c:20
#define AES128_KEY_LENGTH
Definition: aes-find.c:21
static bool aes256_key_test(const unsigned char *buf)
Definition: aes-find.c:25
#define AES192_KEY_LENGTH
Definition: aes-find.c:22
static bool aes128_key_test(const unsigned char *buf)
Definition: aes-find.c:37
#define AES192_SEARCH_LENGTH
Definition: aes-find.c:19
static bool aes192_key_test(const unsigned char *buf)
Definition: aes-find.c:31
#define AES256_KEY_LENGTH
Definition: aes-find.c:23
lzma_index ** i
Definition: index.h:629
#define RZ_API
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
static RzSocket * s
Definition: rtr.c:28
static const ut8 Sbox[256]
Definition: rz_aes.h:10
RZ_API int rz_search_hit_new(RzSearch *s, RzSearchKeyword *kw, ut64 addr)
Definition: search.c:107
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
ut64(WINAPI *w32_GetEnabledXStateFeatures)()