| 69 | } |
| 70 | |
| 71 | bool |
| 72 | derive_path_iv(CryptContext *con, const WCHAR *path, unsigned char *iv, const char *type) |
| 73 | { |
| 74 | |
| 75 | //DbgPrint(L"derive_path_iv path = %s, type = %S\n", path, type); |
| 76 | |
| 77 | wstring wpath; |
| 78 | |
| 79 | const WCHAR *pathstr = path; |
| 80 | |
| 81 | if (*pathstr == '\\' || *pathstr == '/') |
| 82 | pathstr++; |
| 83 | |
| 84 | wpath = pathstr; |
| 85 | |
| 86 | if (wpath.length() > 0) { |
| 87 | if (wpath[wpath.length() - 1] == '\\' || wpath[wpath.length()] - 1 == '/') |
| 88 | wpath.erase(wpath.length() - 1); |
| 89 | } |
| 90 | |
| 91 | int i; |
| 92 | int len = (int)wpath.length(); |
| 93 | for (i = 0; i < len; i++) { |
| 94 | if (wpath[i] == '\\') |
| 95 | wpath[i] = '/'; |
| 96 | } |
| 97 | |
| 98 | string utf8path; |
| 99 | |
| 100 | if (!unicode_to_utf8(&wpath[0], utf8path)) |
| 101 | return false; |
| 102 | |
| 103 | bool bRet = true; |
| 104 | |
| 105 | TempBuffer<BYTE, 4096> buffer; |
| 106 | |
| 107 | BYTE *pbuf = NULL; |
| 108 | |
| 109 | try { |
| 110 | int typelen = (int)strlen(type); |
| 111 | int bufsize = (int)(utf8path.length() + 1 + typelen); |
| 112 | pbuf = buffer.get(bufsize); |
| 113 | if (!pbuf) |
| 114 | throw(-1); |
| 115 | memcpy(pbuf, &utf8path[0], utf8path.length() + 1); |
| 116 | memcpy(pbuf + utf8path.length() + 1, type, typelen); |
| 117 | BYTE hash[SHA256_LEN]; |
| 118 | if (!sha256(pbuf, bufsize, hash)) |
| 119 | throw(-1); |
| 120 | |
| 121 | memcpy(iv, hash, DIR_IV_LEN); // all iv's are 16 bytes (DIR_IV_LEN) |
| 122 | |
| 123 | } catch (...) { |
| 124 | bRet = false; |
| 125 | } |
| 126 | |
| 127 | return bRet; |
| 128 | } |
no test coverage detected