Loading cmds/keystore/keystore.c +71 −24 Original line number Diff line number Diff line Loading @@ -143,15 +143,20 @@ static void send_message(uint8_t *message, int length) send(the_socket, message, length, 0); } /* Here is the file format. Values are encrypted by AES CBC, and MD5 is used to * compute their checksums. To make the files portable, the length is stored in * network order. Note that the first four bytes are reserved for future use and * are always set to zero in this implementation. */ /* Here is the file format. There are two parts in blob.value, the secret and * the description. The secret is stored in ciphertext, and its original size * can be found in blob.length. The description is stored after the secret in * plaintext, and its size is specified in blob.info. The total size of the two * parts must be no more than VALUE_SIZE bytes. The first three bytes of the * file are reserved for future use and are always set to zero. Fields other * than blob.info, blob.length, and blob.value are modified by encrypt_blob() * and decrypt_blob(). Thus they should not be accessed from outside. */ static int the_entropy = -1; static struct __attribute__((packed)) { uint32_t reserved; uint8_t reserved[3]; uint8_t info; uint8_t vector[AES_BLOCK_SIZE]; uint8_t encrypted[0]; uint8_t digest[MD5_DIGEST_LENGTH]; Loading @@ -170,9 +175,13 @@ static int8_t encrypt_blob(char *name, AES_KEY *aes_key) return SYSTEM_ERROR; } length = blob.length + blob.value - blob.encrypted; length = blob.length + (blob.value - blob.encrypted); length = (length + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE * AES_BLOCK_SIZE; if (blob.info != 0) { memmove(&blob.encrypted[length], &blob.value[blob.length], blob.info); } blob.length = htonl(blob.length); MD5(blob.digested, length - (blob.digested - blob.encrypted), blob.digest); Loading @@ -180,8 +189,8 @@ static int8_t encrypt_blob(char *name, AES_KEY *aes_key) AES_cbc_encrypt(blob.encrypted, blob.encrypted, length, aes_key, vector, AES_ENCRYPT); blob.reserved = 0; length += blob.encrypted - (uint8_t *)&blob; memset(blob.reserved, 0, sizeof(blob.reserved)); length += (blob.encrypted - (uint8_t *)&blob) + blob.info; fd = open(".tmp", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); length -= write(fd, &blob, length); Loading @@ -200,7 +209,7 @@ static int8_t decrypt_blob(char *name, AES_KEY *aes_key) length = read(fd, &blob, sizeof(blob)); close(fd); length -= blob.encrypted - (uint8_t *)&blob; length -= (blob.encrypted - (uint8_t *)&blob) + blob.info; if (length < blob.value - blob.encrypted || length % AES_BLOCK_SIZE != 0) { return VALUE_CORRUPTED; } Loading @@ -215,8 +224,13 @@ static int8_t decrypt_blob(char *name, AES_KEY *aes_key) length -= blob.value - blob.digested; blob.length = ntohl(blob.length); return (blob.length < 0 || blob.length > length) ? VALUE_CORRUPTED : NO_ERROR; if (blob.length < 0 || blob.length > length) { return VALUE_CORRUPTED; } if (blob.info != 0) { memmove(&blob.value[blob.length], &blob.value[length], blob.info); } return NO_ERROR; } /* Here are the actions. Each of them is a function without arguments. All Loading Loading @@ -266,6 +280,7 @@ static int8_t insert() char name[NAME_MAX]; int n = sprintf(name, "%u_", uid); encode_key(&name[n], params[0].value, params[0].length); blob.info = 0; blob.length = params[1].length; memcpy(blob.value, params[1].value, params[1].length); return encrypt_blob(name, &encryption_key); Loading Loading @@ -336,56 +351,88 @@ static int8_t reset() #define MASTER_KEY_FILE ".masterkey" #define MASTER_KEY_SIZE 16 #define SALT_SIZE 16 static void generate_key(uint8_t *key, uint8_t *password, int length) static void set_key(uint8_t *key, uint8_t *password, int length, uint8_t *salt) { if (salt) { PKCS5_PBKDF2_HMAC_SHA1((char *)password, length, salt, SALT_SIZE, 8192, MASTER_KEY_SIZE, key); } else { PKCS5_PBKDF2_HMAC_SHA1((char *)password, length, (uint8_t *)"keystore", sizeof("keystore"), 1024, MASTER_KEY_SIZE, key); } } /* Here is the history. To improve the security, the parameters to generate the * master key has been changed. To make a seamless transition, we update the * file using the same password when the user unlock it for the first time. If * any thing goes wrong during the transition, the new file will not overwrite * the old one. This avoids permanent damages of the existing data. */ static int8_t password() { uint8_t key[MASTER_KEY_SIZE]; AES_KEY aes_key; int n; int8_t response = SYSTEM_ERROR; if (state == UNINITIALIZED) { blob.length = MASTER_KEY_SIZE; if (read(the_entropy, blob.value, MASTER_KEY_SIZE) != MASTER_KEY_SIZE) { return SYSTEM_ERROR; } } else { generate_key(key, params[0].value, params[0].length); int fd = open(MASTER_KEY_FILE, O_RDONLY); uint8_t *salt = NULL; if (fd != -1) { int length = read(fd, &blob, sizeof(blob)); close(fd); if (length > SALT_SIZE && blob.info == SALT_SIZE) { salt = (uint8_t *)&blob + length - SALT_SIZE; } } set_key(key, params[0].value, params[0].length, salt); AES_set_decrypt_key(key, MASTER_KEY_SIZE * 8, &aes_key); n = decrypt_blob(MASTER_KEY_FILE, &aes_key); if (n == SYSTEM_ERROR) { response = decrypt_blob(MASTER_KEY_FILE, &aes_key); if (response == SYSTEM_ERROR) { return SYSTEM_ERROR; } if (n != NO_ERROR || blob.length != MASTER_KEY_SIZE) { if (response != NO_ERROR || blob.length != MASTER_KEY_SIZE) { if (retry <= 0) { reset(); return UNINITIALIZED; } return WRONG_PASSWORD + --retry; } if (!salt && params[1].length == -1) { params[1] = params[0]; } } if (params[1].length == -1) { memcpy(key, blob.value, MASTER_KEY_SIZE); } else { generate_key(key, params[1].value, params[1].length); uint8_t *salt = &blob.value[MASTER_KEY_SIZE]; if (read(the_entropy, salt, SALT_SIZE) != SALT_SIZE) { return SYSTEM_ERROR; } set_key(key, params[1].value, params[1].length, salt); AES_set_encrypt_key(key, MASTER_KEY_SIZE * 8, &aes_key); memcpy(key, blob.value, MASTER_KEY_SIZE); n = encrypt_blob(MASTER_KEY_FILE, &aes_key); blob.info = SALT_SIZE; blob.length = MASTER_KEY_SIZE; response = encrypt_blob(MASTER_KEY_FILE, &aes_key); } if (n == NO_ERROR) { if (response == NO_ERROR) { AES_set_encrypt_key(key, MASTER_KEY_SIZE * 8, &encryption_key); AES_set_decrypt_key(key, MASTER_KEY_SIZE * 8, &decryption_key); state = NO_ERROR; retry = MAX_RETRY; } return n; return response; } static int8_t lock() Loading include/surfaceflinger/ISurfaceComposer.h +2 −1 Original line number Diff line number Diff line Loading @@ -115,7 +115,8 @@ public: */ virtual status_t captureScreen(DisplayID dpy, sp<IMemoryHeap>* heap, uint32_t* width, uint32_t* height, PixelFormat* format) = 0; uint32_t* width, uint32_t* height, PixelFormat* format, uint32_t reqWidth, uint32_t reqHeight) = 0; /* Signal surfaceflinger that there might be some work to do * This is an ASYNCHRONOUS call. Loading include/surfaceflinger/SurfaceComposerClient.h +30 −0 Original line number Diff line number Diff line Loading @@ -169,6 +169,36 @@ private: sp<ISurfaceComposerClient> mClient; }; // --------------------------------------------------------------------------- class ScreenshotClient { sp<IMemoryHeap> mHeap; uint32_t mWidth; uint32_t mHeight; PixelFormat mFormat; public: ScreenshotClient(); // frees the previous screenshot and capture a new one status_t update(); status_t update(uint32_t reqWidth, uint32_t reqHeight); // release memory occupied by the screenshot void release(); // pixels are valid until this object is freed or // release() or update() is called void const* getPixels() const; uint32_t getWidth() const; uint32_t getHeight() const; PixelFormat getFormat() const; uint32_t getStride() const; // size of allocated memory in bytes size_t getSize() const; }; // --------------------------------------------------------------------------- }; // namespace android Loading include/ui/EventHub.h +17 −7 Original line number Diff line number Diff line Loading @@ -142,8 +142,13 @@ protected: public: // Synthetic raw event type codes produced when devices are added or removed. enum { // Sent when a device is added. DEVICE_ADDED = 0x10000000, DEVICE_REMOVED = 0x20000000 // Sent when a device is removed. DEVICE_REMOVED = 0x20000000, // Sent when all added/removed devices from the most recent scan have been reported. // This event is always sent at least once. FINISHED_DEVICE_SCAN = 0x30000000, }; virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; Loading Loading @@ -181,6 +186,8 @@ public: */ virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const = 0; virtual void dump(String8& dump) = 0; }; class EventHub : public EventHubInterface Loading Loading @@ -211,16 +218,18 @@ public: virtual bool getEvent(RawEvent* outEvent); virtual void dump(String8& dump); protected: virtual ~EventHub(); private: bool openPlatformInput(void); int open_device(const char *device); int close_device(const char *device); int scan_dir(const char *dirname); int read_notify(int nfd); int openDevice(const char *device); int closeDevice(const char *device); int scanDir(const char *dirname); int readNotify(int nfd); status_t mError; Loading @@ -239,8 +248,8 @@ private: ~device_t(); }; device_t* getDevice(int32_t deviceId) const; bool hasKeycode(device_t* device, int keycode) const; device_t* getDeviceLocked(int32_t deviceId) const; bool hasKeycodeLocked(device_t* device, int keycode) const; int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const; int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const; Loading Loading @@ -269,6 +278,7 @@ private: int mFDCount; bool mOpened; bool mNeedToSendFinishedDeviceScan; List<String8> mExcludedDevices; // device ids that report particular switches. Loading include/ui/Input.h +3 −1 Original line number Diff line number Diff line Loading @@ -73,7 +73,8 @@ namespace android { * policy decisions such as waking from device sleep. */ enum { /* These flags originate in RawEvents and are generally set in the key map. */ /* These flags originate in RawEvents and are generally set in the key map. * See also labels for policy flags in KeycodeLabels.h. */ POLICY_FLAG_WAKE = 0x00000001, POLICY_FLAG_WAKE_DROPPED = 0x00000002, Loading @@ -83,6 +84,7 @@ enum { POLICY_FLAG_ALT_GR = 0x00000020, POLICY_FLAG_MENU = 0x00000040, POLICY_FLAG_LAUNCHER = 0x00000080, POLICY_FLAG_VIRTUAL = 0x00000100, POLICY_FLAG_RAW_MASK = 0x0000ffff, Loading Loading
cmds/keystore/keystore.c +71 −24 Original line number Diff line number Diff line Loading @@ -143,15 +143,20 @@ static void send_message(uint8_t *message, int length) send(the_socket, message, length, 0); } /* Here is the file format. Values are encrypted by AES CBC, and MD5 is used to * compute their checksums. To make the files portable, the length is stored in * network order. Note that the first four bytes are reserved for future use and * are always set to zero in this implementation. */ /* Here is the file format. There are two parts in blob.value, the secret and * the description. The secret is stored in ciphertext, and its original size * can be found in blob.length. The description is stored after the secret in * plaintext, and its size is specified in blob.info. The total size of the two * parts must be no more than VALUE_SIZE bytes. The first three bytes of the * file are reserved for future use and are always set to zero. Fields other * than blob.info, blob.length, and blob.value are modified by encrypt_blob() * and decrypt_blob(). Thus they should not be accessed from outside. */ static int the_entropy = -1; static struct __attribute__((packed)) { uint32_t reserved; uint8_t reserved[3]; uint8_t info; uint8_t vector[AES_BLOCK_SIZE]; uint8_t encrypted[0]; uint8_t digest[MD5_DIGEST_LENGTH]; Loading @@ -170,9 +175,13 @@ static int8_t encrypt_blob(char *name, AES_KEY *aes_key) return SYSTEM_ERROR; } length = blob.length + blob.value - blob.encrypted; length = blob.length + (blob.value - blob.encrypted); length = (length + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE * AES_BLOCK_SIZE; if (blob.info != 0) { memmove(&blob.encrypted[length], &blob.value[blob.length], blob.info); } blob.length = htonl(blob.length); MD5(blob.digested, length - (blob.digested - blob.encrypted), blob.digest); Loading @@ -180,8 +189,8 @@ static int8_t encrypt_blob(char *name, AES_KEY *aes_key) AES_cbc_encrypt(blob.encrypted, blob.encrypted, length, aes_key, vector, AES_ENCRYPT); blob.reserved = 0; length += blob.encrypted - (uint8_t *)&blob; memset(blob.reserved, 0, sizeof(blob.reserved)); length += (blob.encrypted - (uint8_t *)&blob) + blob.info; fd = open(".tmp", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); length -= write(fd, &blob, length); Loading @@ -200,7 +209,7 @@ static int8_t decrypt_blob(char *name, AES_KEY *aes_key) length = read(fd, &blob, sizeof(blob)); close(fd); length -= blob.encrypted - (uint8_t *)&blob; length -= (blob.encrypted - (uint8_t *)&blob) + blob.info; if (length < blob.value - blob.encrypted || length % AES_BLOCK_SIZE != 0) { return VALUE_CORRUPTED; } Loading @@ -215,8 +224,13 @@ static int8_t decrypt_blob(char *name, AES_KEY *aes_key) length -= blob.value - blob.digested; blob.length = ntohl(blob.length); return (blob.length < 0 || blob.length > length) ? VALUE_CORRUPTED : NO_ERROR; if (blob.length < 0 || blob.length > length) { return VALUE_CORRUPTED; } if (blob.info != 0) { memmove(&blob.value[blob.length], &blob.value[length], blob.info); } return NO_ERROR; } /* Here are the actions. Each of them is a function without arguments. All Loading Loading @@ -266,6 +280,7 @@ static int8_t insert() char name[NAME_MAX]; int n = sprintf(name, "%u_", uid); encode_key(&name[n], params[0].value, params[0].length); blob.info = 0; blob.length = params[1].length; memcpy(blob.value, params[1].value, params[1].length); return encrypt_blob(name, &encryption_key); Loading Loading @@ -336,56 +351,88 @@ static int8_t reset() #define MASTER_KEY_FILE ".masterkey" #define MASTER_KEY_SIZE 16 #define SALT_SIZE 16 static void generate_key(uint8_t *key, uint8_t *password, int length) static void set_key(uint8_t *key, uint8_t *password, int length, uint8_t *salt) { if (salt) { PKCS5_PBKDF2_HMAC_SHA1((char *)password, length, salt, SALT_SIZE, 8192, MASTER_KEY_SIZE, key); } else { PKCS5_PBKDF2_HMAC_SHA1((char *)password, length, (uint8_t *)"keystore", sizeof("keystore"), 1024, MASTER_KEY_SIZE, key); } } /* Here is the history. To improve the security, the parameters to generate the * master key has been changed. To make a seamless transition, we update the * file using the same password when the user unlock it for the first time. If * any thing goes wrong during the transition, the new file will not overwrite * the old one. This avoids permanent damages of the existing data. */ static int8_t password() { uint8_t key[MASTER_KEY_SIZE]; AES_KEY aes_key; int n; int8_t response = SYSTEM_ERROR; if (state == UNINITIALIZED) { blob.length = MASTER_KEY_SIZE; if (read(the_entropy, blob.value, MASTER_KEY_SIZE) != MASTER_KEY_SIZE) { return SYSTEM_ERROR; } } else { generate_key(key, params[0].value, params[0].length); int fd = open(MASTER_KEY_FILE, O_RDONLY); uint8_t *salt = NULL; if (fd != -1) { int length = read(fd, &blob, sizeof(blob)); close(fd); if (length > SALT_SIZE && blob.info == SALT_SIZE) { salt = (uint8_t *)&blob + length - SALT_SIZE; } } set_key(key, params[0].value, params[0].length, salt); AES_set_decrypt_key(key, MASTER_KEY_SIZE * 8, &aes_key); n = decrypt_blob(MASTER_KEY_FILE, &aes_key); if (n == SYSTEM_ERROR) { response = decrypt_blob(MASTER_KEY_FILE, &aes_key); if (response == SYSTEM_ERROR) { return SYSTEM_ERROR; } if (n != NO_ERROR || blob.length != MASTER_KEY_SIZE) { if (response != NO_ERROR || blob.length != MASTER_KEY_SIZE) { if (retry <= 0) { reset(); return UNINITIALIZED; } return WRONG_PASSWORD + --retry; } if (!salt && params[1].length == -1) { params[1] = params[0]; } } if (params[1].length == -1) { memcpy(key, blob.value, MASTER_KEY_SIZE); } else { generate_key(key, params[1].value, params[1].length); uint8_t *salt = &blob.value[MASTER_KEY_SIZE]; if (read(the_entropy, salt, SALT_SIZE) != SALT_SIZE) { return SYSTEM_ERROR; } set_key(key, params[1].value, params[1].length, salt); AES_set_encrypt_key(key, MASTER_KEY_SIZE * 8, &aes_key); memcpy(key, blob.value, MASTER_KEY_SIZE); n = encrypt_blob(MASTER_KEY_FILE, &aes_key); blob.info = SALT_SIZE; blob.length = MASTER_KEY_SIZE; response = encrypt_blob(MASTER_KEY_FILE, &aes_key); } if (n == NO_ERROR) { if (response == NO_ERROR) { AES_set_encrypt_key(key, MASTER_KEY_SIZE * 8, &encryption_key); AES_set_decrypt_key(key, MASTER_KEY_SIZE * 8, &decryption_key); state = NO_ERROR; retry = MAX_RETRY; } return n; return response; } static int8_t lock() Loading
include/surfaceflinger/ISurfaceComposer.h +2 −1 Original line number Diff line number Diff line Loading @@ -115,7 +115,8 @@ public: */ virtual status_t captureScreen(DisplayID dpy, sp<IMemoryHeap>* heap, uint32_t* width, uint32_t* height, PixelFormat* format) = 0; uint32_t* width, uint32_t* height, PixelFormat* format, uint32_t reqWidth, uint32_t reqHeight) = 0; /* Signal surfaceflinger that there might be some work to do * This is an ASYNCHRONOUS call. Loading
include/surfaceflinger/SurfaceComposerClient.h +30 −0 Original line number Diff line number Diff line Loading @@ -169,6 +169,36 @@ private: sp<ISurfaceComposerClient> mClient; }; // --------------------------------------------------------------------------- class ScreenshotClient { sp<IMemoryHeap> mHeap; uint32_t mWidth; uint32_t mHeight; PixelFormat mFormat; public: ScreenshotClient(); // frees the previous screenshot and capture a new one status_t update(); status_t update(uint32_t reqWidth, uint32_t reqHeight); // release memory occupied by the screenshot void release(); // pixels are valid until this object is freed or // release() or update() is called void const* getPixels() const; uint32_t getWidth() const; uint32_t getHeight() const; PixelFormat getFormat() const; uint32_t getStride() const; // size of allocated memory in bytes size_t getSize() const; }; // --------------------------------------------------------------------------- }; // namespace android Loading
include/ui/EventHub.h +17 −7 Original line number Diff line number Diff line Loading @@ -142,8 +142,13 @@ protected: public: // Synthetic raw event type codes produced when devices are added or removed. enum { // Sent when a device is added. DEVICE_ADDED = 0x10000000, DEVICE_REMOVED = 0x20000000 // Sent when a device is removed. DEVICE_REMOVED = 0x20000000, // Sent when all added/removed devices from the most recent scan have been reported. // This event is always sent at least once. FINISHED_DEVICE_SCAN = 0x30000000, }; virtual uint32_t getDeviceClasses(int32_t deviceId) const = 0; Loading Loading @@ -181,6 +186,8 @@ public: */ virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const = 0; virtual void dump(String8& dump) = 0; }; class EventHub : public EventHubInterface Loading Loading @@ -211,16 +218,18 @@ public: virtual bool getEvent(RawEvent* outEvent); virtual void dump(String8& dump); protected: virtual ~EventHub(); private: bool openPlatformInput(void); int open_device(const char *device); int close_device(const char *device); int scan_dir(const char *dirname); int read_notify(int nfd); int openDevice(const char *device); int closeDevice(const char *device); int scanDir(const char *dirname); int readNotify(int nfd); status_t mError; Loading @@ -239,8 +248,8 @@ private: ~device_t(); }; device_t* getDevice(int32_t deviceId) const; bool hasKeycode(device_t* device, int keycode) const; device_t* getDeviceLocked(int32_t deviceId) const; bool hasKeycodeLocked(device_t* device, int keycode) const; int32_t getScanCodeStateLocked(device_t* device, int32_t scanCode) const; int32_t getKeyCodeStateLocked(device_t* device, int32_t keyCode) const; Loading Loading @@ -269,6 +278,7 @@ private: int mFDCount; bool mOpened; bool mNeedToSendFinishedDeviceScan; List<String8> mExcludedDevices; // device ids that report particular switches. Loading
include/ui/Input.h +3 −1 Original line number Diff line number Diff line Loading @@ -73,7 +73,8 @@ namespace android { * policy decisions such as waking from device sleep. */ enum { /* These flags originate in RawEvents and are generally set in the key map. */ /* These flags originate in RawEvents and are generally set in the key map. * See also labels for policy flags in KeycodeLabels.h. */ POLICY_FLAG_WAKE = 0x00000001, POLICY_FLAG_WAKE_DROPPED = 0x00000002, Loading @@ -83,6 +84,7 @@ enum { POLICY_FLAG_ALT_GR = 0x00000020, POLICY_FLAG_MENU = 0x00000040, POLICY_FLAG_LAUNCHER = 0x00000080, POLICY_FLAG_VIRTUAL = 0x00000100, POLICY_FLAG_RAW_MASK = 0x0000ffff, Loading