Merge remote-tracking branch 'goog/ics-aah-exp'

empty -s ours merge

Change-Id: Idccc5a540d4d50ad529fe8d08a49fe55556dd9a8
diff --git a/hardware.c b/hardware.c
index 4d5f8fe..1f831cb 100644
--- a/hardware.c
+++ b/hardware.c
@@ -74,7 +74,7 @@
     handle = dlopen(path, RTLD_NOW);
     if (handle == NULL) {
         char const *err_str = dlerror();
-        LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
+        ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
         status = -EINVAL;
         goto done;
     }
@@ -83,14 +83,14 @@
     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
     hmi = (struct hw_module_t *)dlsym(handle, sym);
     if (hmi == NULL) {
-        LOGE("load: couldn't find symbol %s", sym);
+        ALOGE("load: couldn't find symbol %s", sym);
         status = -EINVAL;
         goto done;
     }
 
     /* Check that the id matches */
     if (strcmp(id, hmi->id) != 0) {
-        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
+        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
         status = -EINVAL;
         goto done;
     }
@@ -108,7 +108,7 @@
             handle = NULL;
         }
     } else {
-        LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
+        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                 id, path, *pHmi, handle);
     }
 
diff --git a/include/hardware/audio.h b/include/hardware/audio.h
index ac014a1..af4df04 100644
--- a/include/hardware/audio.h
+++ b/include/hardware/audio.h
@@ -63,6 +63,9 @@
 #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
 #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
 
+/* A2DP sink address set by framework */
+#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
+
 /**
  *  audio stream parameters
  */
@@ -102,12 +105,12 @@
     /**
      * audio format - eg. AUDIO_FORMAT_PCM_16_BIT
      */
-    int (*get_format)(const struct audio_stream *stream);
+    audio_format_t (*get_format)(const struct audio_stream *stream);
 
     /* currently unused - use set_parameters with key
      *     AUDIO_PARAMETER_STREAM_FORMAT
      */
-    int (*set_format)(struct audio_stream *stream, int format);
+    int (*set_format)(struct audio_stream *stream, audio_format_t format);
 
     /**
      * Put the audio hardware input/output into standby mode.
@@ -223,9 +226,9 @@
 /**
  * return the frame size (number of bytes per sample).
  */
-static inline uint32_t audio_stream_frame_size(struct audio_stream *s)
+static inline size_t audio_stream_frame_size(struct audio_stream *s)
 {
-    int chan_samp_sz;
+    size_t chan_samp_sz;
 
     switch (s->get_format(s)) {
     case AUDIO_FORMAT_PCM_16_BIT:
@@ -289,11 +292,11 @@
     int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
 
     /**
-     * setMode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
+     * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
      * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
      * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
      */
-    int (*set_mode)(struct audio_hw_device *dev, int mode);
+    int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
 
     /* mic mute */
     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
@@ -313,12 +316,12 @@
      * 0 if one of the parameters is not supported
      */
     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
-                                    uint32_t sample_rate, int format,
+                                    uint32_t sample_rate, audio_format_t format,
                                     int channel_count);
 
     /** This method creates and opens the audio hardware output stream */
     int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
-                              int *format, uint32_t *channels,
+                              audio_format_t *format, uint32_t *channels,
                               uint32_t *sample_rate,
                               struct audio_stream_out **out);
 
@@ -327,7 +330,7 @@
 
     /** This method creates and opens the audio hardware input stream */
     int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
-                             int *format, uint32_t *channels,
+                             audio_format_t *format, uint32_t *channels,
                              uint32_t *sample_rate,
                              audio_in_acoustics_t acoustics,
                              struct audio_stream_in **stream_in);
diff --git a/include/hardware/audio_effect.h b/include/hardware/audio_effect.h
index 5adab12..65eba36 100644
--- a/include/hardware/audio_effect.h
+++ b/include/hardware/audio_effect.h
@@ -111,19 +111,19 @@
 //  |                           |           | 1 requires device updates
 //  |                           |           | 2, 4 reserved
 //  +---------------------------+-----------+-----------------------------------
-//  | Sample input mode         | 12..13    | 1 direct: process() function or EFFECT_CMD_CONFIGURE
+//  | Sample input mode         | 12..13    | 1 direct: process() function or EFFECT_CMD_SET_CONFIG
 //  |                           |           |   command must specify a buffer descriptor
 //  |                           |           | 2 provider: process() function uses the
 //  |                           |           |   bufferProvider indicated by the
-//  |                           |           |   EFFECT_CMD_CONFIGURE command to request input.
+//  |                           |           |   EFFECT_CMD_SET_CONFIG command to request input.
 //  |                           |           |   buffers.
 //  |                           |           | 3 both: both input modes are supported
 //  +---------------------------+-----------+-----------------------------------
-//  | Sample output mode        | 14..15    | 1 direct: process() function or EFFECT_CMD_CONFIGURE
+//  | Sample output mode        | 14..15    | 1 direct: process() function or EFFECT_CMD_SET_CONFIG
 //  |                           |           |   command must specify a buffer descriptor
 //  |                           |           | 2 provider: process() function uses the
 //  |                           |           |   bufferProvider indicated by the
-//  |                           |           |   EFFECT_CMD_CONFIGURE command to request output
+//  |                           |           |   EFFECT_CMD_SET_CONFIG command to request output
 //  |                           |           |   buffers.
 //  |                           |           | 3 both: both output modes are supported
 //  +---------------------------+-----------+-----------------------------------
@@ -269,7 +269,7 @@
     //          (count and location) in input buffer descriptor and output processed
     //          samples as specified in output buffer descriptor. If the buffer descriptor
     //          is not specified the function must use either the buffer or the
-    //          buffer provider function installed by the EFFECT_CMD_CONFIGURE command.
+    //          buffer provider function installed by the EFFECT_CMD_SET_CONFIG command.
     //          The effect framework will call the process() function after the EFFECT_CMD_ENABLE
     //          command is received and until the EFFECT_CMD_DISABLE is received. When the engine
     //          receives the EFFECT_CMD_DISABLE command it should turn off the effect gracefully
@@ -284,10 +284,10 @@
     //          self:       handle to the effect interface this function
     //              is called on.
     //          inBuffer:   buffer descriptor indicating where to read samples to process.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG command.
     //
     //          outBuffer:   buffer descriptor indicating where to write processed samples.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG command.
     //
     //    Output:
     //        returned value:    0 successful operation
@@ -369,12 +369,12 @@
     //          self:       handle to the effect interface this function
     //              is called on.
     //          inBuffer:   buffer descriptor indicating where to read samples to process.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE_REVERSE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG_REVERSE command.
     //
     //          outBuffer:   buffer descriptor indicating where to write processed samples.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE_REVERSE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG_REVERSE command.
     //              If the buffer and buffer provider in the configuration received by
-    //              EFFECT_CMD_CONFIGURE_REVERSE are also NULL, do not return modified reverse
+    //              EFFECT_CMD_SET_CONFIG_REVERSE are also NULL, do not return modified reverse
     //              stream data
     //
     //    Output:
@@ -395,7 +395,7 @@
 //
 enum effect_command_e {
    EFFECT_CMD_INIT,                 // initialize effect engine
-   EFFECT_CMD_CONFIGURE,            // configure effect engine (see effect_config_t)
+   EFFECT_CMD_SET_CONFIG,           // configure effect engine (see effect_config_t)
    EFFECT_CMD_RESET,                // reset effect engine
    EFFECT_CMD_ENABLE,               // enable effect process
    EFFECT_CMD_DISABLE,              // disable effect process
@@ -406,8 +406,13 @@
    EFFECT_CMD_SET_DEVICE,           // set audio device (see audio.h, audio_devices_t)
    EFFECT_CMD_SET_VOLUME,           // set volume
    EFFECT_CMD_SET_AUDIO_MODE,       // set the audio mode (normal, ring, ...)
-   EFFECT_CMD_CONFIGURE_REVERSE,    // configure effect engine reverse stream(see effect_config_t)
+   EFFECT_CMD_SET_CONFIG_REVERSE,   // configure effect engine reverse stream(see effect_config_t)
    EFFECT_CMD_SET_INPUT_DEVICE,     // set capture device (see audio.h, audio_devices_t)
+   EFFECT_CMD_GET_CONFIG,           // read effect engine configuration
+   EFFECT_CMD_GET_CONFIG_REVERSE,   // read configure effect engine reverse stream configuration
+   EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS,// get all supported configurations for a feature.
+   EFFECT_CMD_GET_FEATURE_CONFIG,   // get current feature configuration
+   EFFECT_CMD_SET_FEATURE_CONFIG,   // set current feature configuration
    EFFECT_CMD_FIRST_PROPRIETARY = 0x10000 // first proprietary command code
 };
 
@@ -425,7 +430,7 @@
 //  size: sizeof(int)
 //  data: status
 //==================================================================================================
-// command: EFFECT_CMD_CONFIGURE
+// command: EFFECT_CMD_SET_CONFIG
 //--------------------------------------------------------------------------------------------------
 // description:
 //  Apply new audio parameters configurations for input and output buffers
@@ -579,16 +584,17 @@
 //--------------------------------------------------------------------------------------------------
 // command format:
 //  size: sizeof(uint32_t)
-//  data: audio_mode_e
+//  data: audio_mode_t
 //--------------------------------------------------------------------------------------------------
 // reply format:
 //  size: 0
 //  data: N/A
 //==================================================================================================
-// command: EFFECT_CMD_CONFIGURE_REVERSE
+// command: EFFECT_CMD_SET_CONFIG_REVERSE
 //--------------------------------------------------------------------------------------------------
 // description:
-//  Apply new audio parameters configurations for input and output buffers of reverse stream
+//  Apply new audio parameters configurations for input and output buffers of reverse stream.
+//  An example of reverse stream is the echo reference supplied to an Acoustic Echo Canceler.
 //--------------------------------------------------------------------------------------------------
 // command format:
 //  size: sizeof(effect_config_t)
@@ -614,6 +620,91 @@
 //  size: 0
 //  data: N/A
 //==================================================================================================
+// command: EFFECT_CMD_GET_CONFIG
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Read audio parameters configurations for input and output buffers
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: 0
+//  data: N/A
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(effect_config_t)
+//  data: effect_config_t
+//==================================================================================================
+// command: EFFECT_CMD_GET_CONFIG_REVERSE
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Read audio parameters configurations for input and output buffers of reverse stream
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: 0
+//  data: N/A
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(effect_config_t)
+//  data: effect_config_t
+//==================================================================================================
+// command: EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Queries for supported configurations for a particular feature (e.g. get the supported
+// combinations of main and auxiliary channels for a noise suppressor).
+// The command parameter is the feature identifier (See effect_feature_e for a list of defined
+// features) followed by the maximum number of configuration descriptor to return.
+// The reply is composed of:
+//  - status (uint32_t):
+//          - 0 if feature is supported
+//          - -ENOSYS if the feature is not supported,
+//          - -ENOMEM if the feature is supported but the total number of supported configurations
+//          exceeds the maximum number indicated by the caller.
+//  - total number of supported configurations (uint32_t)
+//  - an array of configuration descriptors.
+// The actual number of descriptors returned must not exceed the maximum number indicated by
+// the caller.
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: 2 x sizeof(uint32_t)
+//  data: effect_feature_e + maximum number of configurations to return
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: 2 x sizeof(uint32_t) + n x sizeof (<config descriptor>)
+//  data: status + total number of configurations supported + array of n config descriptors
+//==================================================================================================
+// command: EFFECT_CMD_GET_FEATURE_CONFIG
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Retrieves current configuration for a given feature.
+// The reply status is:
+//      - 0 if feature is supported
+//      - -ENOSYS if the feature is not supported,
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: sizeof(uint32_t)
+//  data: effect_feature_e
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(uint32_t) + sizeof (<config descriptor>)
+//  data: status + config descriptor
+//==================================================================================================
+// command: EFFECT_CMD_SET_FEATURE_CONFIG
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Sets current configuration for a given feature.
+// The reply status is:
+//      - 0 if feature is supported
+//      - -ENOSYS if the feature is not supported,
+//      - -EINVAL if the configuration is invalid
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: sizeof(uint32_t) + sizeof (<config descriptor>)
+//  data: effect_feature_e + config descriptor
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(uint32_t)
+//  data: status
+//==================================================================================================
 // command: EFFECT_CMD_FIRST_PROPRIETARY
 //--------------------------------------------------------------------------------------------------
 // description:
@@ -624,7 +715,7 @@
 
 // Audio buffer descriptor used by process(), bufferProvider() functions and buffer_config_t
 // structure. Multi-channel audio is always interleaved. The channel order is from LSB to MSB with
-// regard to the channel mask definition in audio.h, audio_channels_t e.g :
+// regard to the channel mask definition in audio.h, audio_channel_mask_t e.g :
 // Stereo: left, right
 // 5 point 1: front left, front right, front center, low frequency, back left, back right
 // The buffer size is expressed in frame count, a frame being composed of samples for all
@@ -649,7 +740,7 @@
 // with getBuffer() is not needed anymore.
 // The process function should use the buffer provider mechanism to retrieve
 // input or output buffer if the inBuffer or outBuffer passed as argument is NULL
-// and the buffer configuration (buffer_config_t) given by the EFFECT_CMD_CONFIGURE
+// and the buffer configuration (buffer_config_t) given by the EFFECT_CMD_SET_CONFIG
 // command did not specify an audio buffer.
 
 typedef int32_t (* buffer_function_t)(void *cookie, audio_buffer_t *buffer);
@@ -664,11 +755,11 @@
 // The buffer_config_s structure specifies the input or output audio format
 // to be used by the effect engine. It is part of the effect_config_t
 // structure that defines both input and output buffer configurations and is
-// passed by the EFFECT_CMD_CONFIGURE or EFFECT_CMD_CONFIGURE_REVERSE command.
+// passed by the EFFECT_CMD_SET_CONFIG or EFFECT_CMD_SET_CONFIG_REVERSE command.
 typedef struct buffer_config_s {
     audio_buffer_t  buffer;     // buffer for use by process() function if not passed explicitly
     uint32_t   samplingRate;    // sampling rate
-    uint32_t   channels;        // channel mask (see audio_channels_t in audio.h)
+    uint32_t   channels;        // channel mask (see audio_channel_mask_t in audio.h)
     buffer_provider_t bufferProvider;   // buffer provider
     uint8_t    format;          // Audio format  (see see audio_format_t in audio.h)
     uint8_t    accessMode;      // read/write or accumulate in buffer (effect_buffer_access_e)
@@ -684,8 +775,22 @@
 
 };
 
+// feature identifiers for EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS command
+enum effect_feature_e {
+    EFFECT_FEATURE_AUX_CHANNELS, // supports auxiliary channels (e.g. dual mic noise suppressor)
+    EFFECT_FEATURE_CNT
+};
+
+// EFFECT_FEATURE_AUX_CHANNELS feature configuration descriptor. Describe a combination
+// of main and auxiliary channels supported
+typedef struct channel_config_s {
+    uint32_t   main_channels;   // channel mask for main channels
+    uint32_t   aux_channels;    // channel mask for auxiliary channels
+} channel_config_t;
+
+
 // Values for bit field "mask" in buffer_config_t. If a bit is set, the corresponding field
-// in buffer_config_t must be taken into account when executing the EFFECT_CMD_CONFIGURE command
+// in buffer_config_t must be taken into account when executing the EFFECT_CMD_SET_CONFIG command
 #define EFFECT_CONFIG_BUFFER    0x0001  // buffer field must be taken into account
 #define EFFECT_CONFIG_SMP_RATE  0x0002  // samplingRate field must be taken into account
 #define EFFECT_CONFIG_CHANNELS  0x0004  // channels field must be taken into account
@@ -697,7 +802,7 @@
                            EFFECT_CONFIG_ACC_MODE | EFFECT_CONFIG_PROVIDER)
 
 
-// effect_config_s structure describes the format of the pCmdData argument of EFFECT_CMD_CONFIGURE
+// effect_config_s structure describes the format of the pCmdData argument of EFFECT_CMD_SET_CONFIG
 // command to configure audio parameters and buffers for effect engine input and output.
 typedef struct effect_config_s {
     buffer_config_t   inputCfg;
@@ -840,7 +945,7 @@
     //        *pHandle:         updated with the effect interface handle.
     //
     ////////////////////////////////////////////////////////////////////////////////
-    int32_t (*create_effect)(effect_uuid_t *uuid,
+    int32_t (*create_effect)(const effect_uuid_t *uuid,
                              int32_t sessionId,
                              int32_t ioId,
                              effect_handle_t *pHandle);
@@ -882,7 +987,7 @@
     //        *pDescriptor:     updated with the effect descriptor.
     //
     ////////////////////////////////////////////////////////////////////////////////
-    int32_t (*get_descriptor)(effect_uuid_t *uuid,
+    int32_t (*get_descriptor)(const effect_uuid_t *uuid,
                               effect_descriptor_t *pDescriptor);
 } audio_effect_library_t;
 
diff --git a/include/hardware/audio_policy.h b/include/hardware/audio_policy.h
index fc56e5e..43d9e83 100644
--- a/include/hardware/audio_policy.h
+++ b/include/hardware/audio_policy.h
@@ -91,7 +91,7 @@
                                        audio_policy_dev_state_t state,
                                        const char *device_address);
 
-    /* retreive a device connection status */
+    /* retrieve a device connection status */
     audio_policy_dev_state_t (*get_device_connection_state)(
                                             const struct audio_policy *pol,
                                             audio_devices_t device,
@@ -99,9 +99,9 @@
 
     /* indicate a change in phone state. Valid phones states are defined
      * by audio_mode_t */
-    void (*set_phone_state)(struct audio_policy *pol, int state);
+    void (*set_phone_state)(struct audio_policy *pol, audio_mode_t state);
 
-    /* indicate a change in ringer mode */
+    /* deprecated, never called (was "indicate a change in ringer mode") */
     void (*set_ringer_mode)(struct audio_policy *pol, uint32_t mode,
                             uint32_t mask);
 
@@ -110,7 +110,7 @@
                           audio_policy_force_use_t usage,
                           audio_policy_forced_cfg_t config);
 
-    /* retreive current device category forced for a given usage */
+    /* retrieve current device category forced for a given usage */
     audio_policy_forced_cfg_t (*get_force_use)(const struct audio_policy *pol,
                                                audio_policy_force_use_t usage);
 
@@ -131,7 +131,7 @@
     audio_io_handle_t (*get_output)(struct audio_policy *pol,
                                     audio_stream_type_t stream,
                                     uint32_t samplingRate,
-                                    uint32_t format,
+                                    audio_format_t format,
                                     uint32_t channels,
                                     audio_policy_output_flags_t flags);
 
@@ -152,11 +152,11 @@
     /* releases the output. */
     void (*release_output)(struct audio_policy *pol, audio_io_handle_t output);
 
-    /* request an input appriate for record from the supplied device with
+    /* request an input appropriate for record from the supplied device with
      * supplied parameters. */
-    audio_io_handle_t (*get_input)(struct audio_policy *pol, int inputSource,
+    audio_io_handle_t (*get_input)(struct audio_policy *pol, audio_source_t inputSource,
                                    uint32_t samplingRate,
-                                   uint32_t format,
+                                   audio_format_t format,
                                    uint32_t channels,
                                    audio_in_acoustics_t acoustics);
 
@@ -174,29 +174,43 @@
      */
 
     /* initialises stream volume conversion parameters by specifying volume
-     * index range. */
+     * index range. The index range for each stream is defined by AudioService. */
     void (*init_stream_volume)(struct audio_policy *pol,
                                audio_stream_type_t stream,
                                int index_min,
                                int index_max);
 
     /* sets the new stream volume at a level corresponding to the supplied
-     * index */
+     * index. The index is within the range specified by init_stream_volume() */
     int (*set_stream_volume_index)(struct audio_policy *pol,
                                    audio_stream_type_t stream,
                                    int index);
 
-    /* retreive current volume index for the specified stream */
+    /* retrieve current volume index for the specified stream */
     int (*get_stream_volume_index)(const struct audio_policy *pol,
                                    audio_stream_type_t stream,
                                    int *index);
 
+    /* sets the new stream volume at a level corresponding to the supplied
+     * index for the specified device.
+     * The index is within the range specified by init_stream_volume() */
+    int (*set_stream_volume_index_for_device)(struct audio_policy *pol,
+                                   audio_stream_type_t stream,
+                                   int index,
+                                   audio_devices_t device);
+
+    /* retrieve current volume index for the specified stream for the specified device */
+    int (*get_stream_volume_index_for_device)(const struct audio_policy *pol,
+                                   audio_stream_type_t stream,
+                                   int *index,
+                                   audio_devices_t device);
+
     /* return the strategy corresponding to a given stream type */
     uint32_t (*get_strategy_for_stream)(const struct audio_policy *pol,
                                         audio_stream_type_t stream);
 
     /* return the enabled output devices for the given stream type */
-    uint32_t (*get_devices_for_stream)(const struct audio_policy *pol,
+    audio_devices_t (*get_devices_for_stream)(const struct audio_policy *pol,
                                        audio_stream_type_t stream);
 
     /* Audio effect management */
@@ -215,7 +229,7 @@
     int (*set_effect_enabled)(struct audio_policy *pol, int id, bool enabled);
 
     bool (*is_stream_active)(const struct audio_policy *pol,
-                             int stream,
+                             audio_stream_type_t stream,
                              uint32_t in_past_ms);
 
     /* dump state */
@@ -242,7 +256,7 @@
     audio_io_handle_t (*open_output)(void *service,
                                      uint32_t *pDevices,
                                      uint32_t *pSamplingRate,
-                                     uint32_t *pFormat,
+                                     audio_format_t *pFormat,
                                      uint32_t *pChannels,
                                      uint32_t *pLatencyMs,
                                      audio_policy_output_flags_t flags);
@@ -277,9 +291,9 @@
     audio_io_handle_t (*open_input)(void *service,
                                     uint32_t *pDevices,
                                     uint32_t *pSamplingRate,
-                                    uint32_t *pFormat,
+                                    audio_format_t *pFormat,
                                     uint32_t *pChannels,
-                                    uint32_t acoustics);
+                                    audio_in_acoustics_t acoustics);
 
     /* closes an audio input */
     int (*close_input)(void *service, audio_io_handle_t input);
diff --git a/include/hardware/camera.h b/include/hardware/camera.h
index 4d25023..4058c30 100644
--- a/include/hardware/camera.h
+++ b/include/hardware/camera.h
@@ -109,6 +109,11 @@
                 int *count);
     int (*lock_buffer)(struct preview_stream_ops* w,
                 buffer_handle_t* buffer);
+    // Timestamps are measured in nanoseconds, and must be comparable
+    // and monotonically increasing between two frames in the same
+    // preview stream. They do not need to be comparable between
+    // consecutive or parallel preview streams, cameras, or app runs.
+    int (*set_timestamp)(struct preview_stream_ops *w, int64_t timestamp);
 } preview_stream_ops_t;
 
 struct camera_device;
diff --git a/include/hardware/gralloc.h b/include/hardware/gralloc.h
index f8beb5e..2dbd1fa 100644
--- a/include/hardware/gralloc.h
+++ b/include/hardware/gralloc.h
@@ -258,4 +258,4 @@
 
 __END_DECLS
 
-#endif  // ANDROID_ALLOC_INTERFACE_H
+#endif  // ANDROID_GRALLOC_INTERFACE_H
diff --git a/include/hardware/hardware.h b/include/hardware/hardware.h
index 7774b2b..b106e2b 100644
--- a/include/hardware/hardware.h
+++ b/include/hardware/hardware.h
@@ -34,6 +34,34 @@
 #define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')
 #define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')
 
+#define HARDWARE_MAKE_API_VERSION(maj,min) \
+            ((((maj) & 0xff) << 8) | ((min) & 0xff))
+
+/*
+ * The current HAL API version.
+ *
+ * All module implementations must set the hw_module_t.hal_api_version field
+ * to this value when declaring the module with HAL_MODULE_INFO_SYM.
+ *
+ * Note that previous implementations have always set this field to 0.
+ * Therefore, libhardware HAL API will always consider versions 0.0 and 1.0
+ * to be 100% binary compatible.
+ *
+ */
+#define HARDWARE_HAL_API_VERSION HARDWARE_MAKE_API_VERSION(1, 0)
+
+/*
+ * Helper macros for module implementors.
+ *
+ * Use this macro to set the hw_module_t.module_api_version field.
+ */
+#define HARDWARE_MODULE_API_VERSION(maj,min) HARDWARE_MAKE_API_VERSION(maj,min)
+
+/*
+ * Use this macro to set the hw_device_t.version field
+ */
+#define HARDWARE_DEVICE_API_VERSION(maj,min) HARDWARE_MAKE_API_VERSION(maj,min)
+
 struct hw_module_t;
 struct hw_module_methods_t;
 struct hw_device_t;
@@ -47,11 +75,47 @@
     /** tag must be initialized to HARDWARE_MODULE_TAG */
     uint32_t tag;
 
-    /** major version number for the module */
-    uint16_t version_major;
+    /**
+     * The API version of the implemented module. The module owner is
+     * responsible for updating the version when a module interface has
+     * changed.
+     *
+     * The derived modules such as gralloc and audio own and manage this field.
+     * The module user must interpret the version field to decide whether or
+     * not to inter-operate with the supplied module implementation.
+     * For example, SurfaceFlinger is responsible for making sure that
+     * it knows how to manage different versions of the gralloc-module API,
+     * and AudioFlinger must know how to do the same for audio-module API.
+     *
+     * The module API version should include a major and a minor component.
+     * For example, version 1.0 could be represented as 0x0100. This format
+     * implies that versions 0x0100-0x01ff are all API-compatible.
+     *
+     * In the future, libhardware will expose a hw_get_module_version()
+     * (or equivalent) function that will take minimum/maximum supported
+     * versions as arguments and would be able to reject modules with
+     * versions outside of the supplied range.
+     */
+    uint16_t module_api_version;
+#define version_major module_api_version
+    /**
+     * version_major/version_minor defines are supplied here for temporary
+     * source code compatibility. They will be removed in the next version.
+     * ALL clients must convert to the new version format.
+     */
 
-    /** minor version number of the module */
-    uint16_t version_minor;
+    /**
+     * The API version of the HAL module interface. This is meant to
+     * version the hw_module_t, hw_module_methods_t, and hw_device_t
+     * structures and definitions.
+     *
+     * The HAL interface owns this field. Module users/implementations
+     * must NOT rely on this value for version information.
+     *
+     * Presently, 0 is the only valid value.
+     */
+    uint16_t hal_api_version;
+#define version_minor hal_api_version
 
     /** Identifier of module */
     const char *id;
@@ -88,7 +152,22 @@
     /** tag must be initialized to HARDWARE_DEVICE_TAG */
     uint32_t tag;
 
-    /** version number for hw_device_t */
+    /**
+     * Version of the module-specific device API. This value is used by
+     * the derived-module user to manage different device implementations.
+     *
+     * The module user is responsible for checking the module_api_version
+     * and device version fields to ensure that the user is capable of
+     * communicating with the specific module implementation.
+     *
+     * One module can support multiple devices with different versions. This
+     * can be useful when a device interface changes in an incompatible way
+     * but it is still necessary to support older implementations at the same
+     * time. One such example is the Camera 2.0 API.
+     *
+     * This field is interpreted by the module user and is ignored by the
+     * HAL interface itself.
+     */
     uint32_t version;
 
     /** reference to the module this device belongs to */
diff --git a/include/hardware/hwcomposer.h b/include/hardware/hwcomposer.h
index b4a7a7a..f788e46 100644
--- a/include/hardware/hwcomposer.h
+++ b/include/hardware/hwcomposer.h
@@ -150,8 +150,12 @@
     /* see hwc_layer_t::flags above */
     uint32_t flags;
 
-    /* handle of buffer to compose. this handle is guaranteed to have been
-     * allocated with gralloc */
+    /* handle of buffer to compose. This handle is guaranteed to have been
+     * allocated from gralloc using the GRALLOC_USAGE_HW_COMPOSER usage flag. If
+     * the layer's handle is unchanged across two consecutive prepare calls and
+     * the HWC_GEOMETRY_CHANGED flag is not set for the second call then the
+     * HWComposer implementation may assume that the contents of the buffer have
+     * not changed. */
     buffer_handle_t handle;
 
     /* transformation to apply to the buffer during composition */
diff --git a/include/hardware/keymaster.h b/include/hardware/keymaster.h
new file mode 100644
index 0000000..3c7799a
--- /dev/null
+++ b/include/hardware/keymaster.h
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HARDWARE_KEYMASTER_H
+#define ANDROID_HARDWARE_KEYMASTER_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+/**
+ * The id of this module
+ */
+#define KEYSTORE_HARDWARE_MODULE_ID "keystore"
+
+#define KEYSTORE_KEYMASTER "keymaster"
+
+/**
+ * The API level of this version of the header. The allows the implementing
+ * module to recognize which API level of the client it is dealing with in
+ * the case of pre-compiled binary clients.
+ */
+#define KEYMASTER_API_VERSION 1
+
+struct keystore_module {
+    hw_module_t common;
+};
+
+/**
+ * Asymmetric key pair types.
+ */
+typedef enum {
+    TYPE_RSA = 1,
+} keymaster_keypair_t;
+
+/**
+ * Parameters needed to generate an RSA key.
+ */
+typedef struct {
+    uint32_t modulus_size;
+    uint64_t public_exponent;
+} keymaster_rsa_keygen_params_t;
+
+/**
+ * Digest type used for RSA operations.
+ */
+typedef enum {
+    DIGEST_NONE,
+} keymaster_rsa_digest_t;
+
+/**
+ * Type of padding used for RSA operations.
+ */
+typedef enum {
+    PADDING_NONE,
+} keymaster_rsa_padding_t;
+
+typedef struct {
+    keymaster_rsa_digest_t digest_type;
+    keymaster_rsa_padding_t padding_type;
+} keymaster_rsa_sign_params_t;
+
+/**
+ * The parameters that can be set for a given keymaster implementation.
+ */
+struct keymaster_device {
+    struct hw_device_t common;
+
+    uint32_t client_version;
+
+    void* context;
+
+    /**
+     * Generates a public and private key. The key-blob returned is opaque
+     * and must subsequently provided for signing and verification.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     */
+    int (*generate_keypair)(const struct keymaster_device* dev,
+            const keymaster_keypair_t key_type, const void* key_params,
+            uint8_t** key_blob, size_t* key_blob_length);
+
+    /**
+     * Imports a public and private key pair. The imported keys will be in
+     * PKCS#8 format with DER encoding (Java standard). The key-blob
+     * returned is opaque and will be subsequently provided for signing
+     * and verification.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     */
+    int (*import_keypair)(const struct keymaster_device* dev,
+            const uint8_t* key, const size_t key_length,
+            uint8_t** key_blob, size_t* key_blob_length);
+
+    /**
+     * Gets the public key part of a key pair. The public key must be in
+     * X.509 format (Java standard) encoded byte array.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     * On error, x509_data should not be allocated.
+     */
+    int (*get_keypair_public)(const struct keymaster_device* dev,
+            const uint8_t* key_blob, const size_t key_blob_length,
+            uint8_t** x509_data, size_t* x509_data_length);
+
+    /**
+     * Deletes the key pair associated with the key blob.
+     */
+    int (*delete_keypair)(const struct keymaster_device* dev,
+            const uint8_t* key_blob, const size_t key_blob_length);
+
+    /**
+     * Signs data using a key-blob generated before. This can use either
+     * an asymmetric key or a secret key.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     */
+    int (*sign_data)(const struct keymaster_device* dev,
+            const void* signing_params,
+            const uint8_t* key_blob, const size_t key_blob_length,
+            const uint8_t* data, const size_t data_length,
+            uint8_t** signed_data, size_t* signed_data_length);
+
+    /**
+     * Verifies data signed with a key-blob. This can use either
+     * an asymmetric key or a secret key.
+     *
+     * Returns: 0 on successful verification or an error code less than 0.
+     */
+    int (*verify_data)(const struct keymaster_device* dev,
+            const void* signing_params,
+            const uint8_t* key_blob, const size_t key_blob_length,
+            const uint8_t* signed_data, const size_t signed_data_length,
+            const uint8_t* signature, const size_t signature_length);
+};
+typedef struct keymaster_device keymaster_device_t;
+
+
+/* Convenience API for opening and closing keymaster devices */
+
+static inline int keymaster_open(const struct hw_module_t* module,
+        keymaster_device_t** device)
+{
+    int rc = module->methods->open(module, KEYSTORE_KEYMASTER,
+            (struct hw_device_t**) device);
+
+    if (!rc) {
+        (*device)->client_version = KEYMASTER_API_VERSION;
+    }
+
+    return rc;
+}
+
+static inline int keymaster_close(keymaster_device_t* device)
+{
+    return device->common.close(&device->common);
+}
+
+__END_DECLS
+
+#endif  // ANDROID_HARDWARE_KEYMASTER_H
+
diff --git a/include/hardware/nfc.h b/include/hardware/nfc.h
index ca3683b..74b3cfb 100644
--- a/include/hardware/nfc.h
+++ b/include/hardware/nfc.h
@@ -76,6 +76,11 @@
      * Set to 0 to disable the workaround, 1 to enable it.
      */
     uint8_t enable_i2c_workaround;
+    /* I2C slave address. Multiple I2C addresses are
+     * possible for PN544 module. Configure address according to
+     * board design.
+     */
+    uint8_t i2c_device_address;
 } nfc_pn544_device_t;
 
 static inline int nfc_pn544_open(const struct hw_module_t* module,
diff --git a/include/hardware/power.h b/include/hardware/power.h
new file mode 100644
index 0000000..825a74a
--- /dev/null
+++ b/include/hardware/power.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_INCLUDE_HARDWARE_POWER_H
+#define ANDROID_INCLUDE_HARDWARE_POWER_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+/**
+ * The id of this module
+ */
+#define POWER_HARDWARE_MODULE_ID "power"
+
+/**
+ * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
+ * and the fields of this data structure must begin with hw_module_t
+ * followed by module specific information.
+ */
+typedef struct power_module {
+    struct hw_module_t common;
+
+    /*
+     * (*init)() performs power management setup actions at runtime
+     * startup, such as to set default cpufreq parameters.
+     */
+    void (*init)(struct power_module *module);
+
+    /*
+     * (*setInteractive)() performs power management actions upon the
+     * system entering interactive state (that is, the system is awake
+     * and ready for interaction, often with UI devices such as
+     * display and touchscreen enabled) or non-interactive state (the
+     * system appears asleep, display usually turned off).  The
+     * non-interactive state is usually entered after a period of
+     * inactivity, in order to conserve battery power during
+     * such inactive periods.
+     *
+     * Typical actions are to turn on or off devices and adjust
+     * cpufreq parameters.  This function may also call the
+     * appropriate interfaces to allow the kernel to suspend the
+     * system to low-power sleep state when entering non-interactive
+     * state, and to disallow low-power suspend when the system is in
+     * interactive state.  When low-power suspend state is allowed, the
+     * kernel may suspend the system whenever no wakelocks are held.
+     *
+     * on is non-zero when the system is transitioning to an
+     * interactive / awake state, and zero when transitioning to a
+     * non-interactive / asleep state.
+     *
+     * This function is called to enter non-interactive state after
+     * turning off the screen (if present), and called to enter
+     * interactive state prior to turning on the screen.
+     */
+    void (*setInteractive)(struct power_module *module, int on);
+} power_module_t;
+
+
+__END_DECLS
+
+#endif  // ANDROID_INCLUDE_HARDWARE_POWER_H
diff --git a/modules/Android.mk b/modules/Android.mk
index cea1bb3..b8b83ba 100644
--- a/modules/Android.mk
+++ b/modules/Android.mk
@@ -1,2 +1,2 @@
-hardware_modules := gralloc hwcomposer audio local_time nfc
+hardware_modules := gralloc hwcomposer audio nfc local_time power
 include $(call all-named-subdir-makefiles,$(hardware_modules))
diff --git a/modules/audio/audio_hw.c b/modules/audio/audio_hw.c
index f25a053..4069ace 100644
--- a/modules/audio/audio_hw.c
+++ b/modules/audio/audio_hw.c
@@ -60,12 +60,12 @@
     return AUDIO_CHANNEL_OUT_STEREO;
 }
 
-static int out_get_format(const struct audio_stream *stream)
+static audio_format_t out_get_format(const struct audio_stream *stream)
 {
     return AUDIO_FORMAT_PCM_16_BIT;
 }
 
-static int out_set_format(struct audio_stream *stream, int format)
+static int out_set_format(struct audio_stream *stream, audio_format_t format)
 {
     return 0;
 }
@@ -153,12 +153,12 @@
     return AUDIO_CHANNEL_IN_MONO;
 }
 
-static int in_get_format(const struct audio_stream *stream)
+static audio_format_t in_get_format(const struct audio_stream *stream)
 {
     return AUDIO_FORMAT_PCM_16_BIT;
 }
 
-static int in_set_format(struct audio_stream *stream, int format)
+static int in_set_format(struct audio_stream *stream, audio_format_t format)
 {
     return 0;
 }
@@ -214,7 +214,7 @@
 }
 
 static int adev_open_output_stream(struct audio_hw_device *dev,
-                                   uint32_t devices, int *format,
+                                   uint32_t devices, audio_format_t *format,
                                    uint32_t *channels, uint32_t *sample_rate,
                                    struct audio_stream_out **stream_out)
 {
@@ -291,7 +291,7 @@
     return -ENOSYS;
 }
 
-static int adev_set_mode(struct audio_hw_device *dev, int mode)
+static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
 {
     return 0;
 }
@@ -307,14 +307,14 @@
 }
 
 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
-                                         uint32_t sample_rate, int format,
+                                         uint32_t sample_rate, audio_format_t format,
                                          int channel_count)
 {
     return 320;
 }
 
 static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
-                                  int *format, uint32_t *channels,
+                                  audio_format_t *format, uint32_t *channels,
                                   uint32_t *sample_rate,
                                   audio_in_acoustics_t acoustics,
                                   struct audio_stream_in **stream_in)
diff --git a/modules/audio/audio_policy.c b/modules/audio/audio_policy.c
index 409f0a1..8d7e5c3 100644
--- a/modules/audio/audio_policy.c
+++ b/modules/audio/audio_policy.c
@@ -58,10 +58,11 @@
     return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
 }
 
-static void ap_set_phone_state(struct audio_policy *pol, int state)
+static void ap_set_phone_state(struct audio_policy *pol, audio_mode_t state)
 {
 }
 
+// deprecated, never called
 static void ap_set_ringer_mode(struct audio_policy *pol, uint32_t mode,
                                uint32_t mask)
 {
@@ -96,7 +97,7 @@
 static audio_io_handle_t ap_get_output(struct audio_policy *pol,
                                        audio_stream_type_t stream,
                                        uint32_t sampling_rate,
-                                       uint32_t format,
+                                       audio_format_t format,
                                        uint32_t channels,
                                        audio_policy_output_flags_t flags)
 {
@@ -120,9 +121,9 @@
 {
 }
 
-static audio_io_handle_t ap_get_input(struct audio_policy *pol, int inputSource,
+static audio_io_handle_t ap_get_input(struct audio_policy *pol, audio_source_t inputSource,
                                       uint32_t sampling_rate,
-                                      uint32_t format,
+                                      audio_format_t format,
                                       uint32_t channels,
                                       audio_in_acoustics_t acoustics)
 {
@@ -163,13 +164,29 @@
     return -ENOSYS;
 }
 
+static int ap_set_stream_volume_index_for_device(struct audio_policy *pol,
+                                      audio_stream_type_t stream,
+                                      int index,
+                                      audio_devices_t device)
+{
+    return -ENOSYS;
+}
+
+static int ap_get_stream_volume_index_for_device(const struct audio_policy *pol,
+                                      audio_stream_type_t stream,
+                                      int *index,
+                                      audio_devices_t device)
+{
+    return -ENOSYS;
+}
+
 static uint32_t ap_get_strategy_for_stream(const struct audio_policy *pol,
                                            audio_stream_type_t stream)
 {
     return 0;
 }
 
-static uint32_t ap_get_devices_for_stream(const struct audio_policy *pol,
+static audio_devices_t ap_get_devices_for_stream(const struct audio_policy *pol,
                                           audio_stream_type_t stream)
 {
     return 0;
@@ -201,7 +218,7 @@
     return -ENOSYS;
 }
 
-static bool ap_is_stream_active(const struct audio_policy *pol, int stream,
+static bool ap_is_stream_active(const struct audio_policy *pol, audio_stream_type_t stream,
                                 uint32_t in_past_ms)
 {
     return false;
@@ -250,6 +267,8 @@
     dap->policy.init_stream_volume = ap_init_stream_volume;
     dap->policy.set_stream_volume_index = ap_set_stream_volume_index;
     dap->policy.get_stream_volume_index = ap_get_stream_volume_index;
+    dap->policy.set_stream_volume_index_for_device = ap_set_stream_volume_index_for_device;
+    dap->policy.get_stream_volume_index_for_device = ap_get_stream_volume_index_for_device;
     dap->policy.get_strategy_for_stream = ap_get_strategy_for_stream;
     dap->policy.get_devices_for_stream = ap_get_devices_for_stream;
     dap->policy.get_output_for_effect = ap_get_output_for_effect;
diff --git a/modules/gralloc/framebuffer.cpp b/modules/gralloc/framebuffer.cpp
index f908976..a37e2c0 100644
--- a/modules/gralloc/framebuffer.cpp
+++ b/modules/gralloc/framebuffer.cpp
@@ -98,7 +98,7 @@
         m->info.activate = FB_ACTIVATE_VBL;
         m->info.yoffset = offset / m->finfo.line_length;
         if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
-            LOGE("FBIOPUT_VSCREENINFO failed");
+            ALOGE("FBIOPUT_VSCREENINFO failed");
             m->base.unlock(&m->base, buffer); 
             return -errno;
         }
@@ -181,14 +181,14 @@
     if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
         info.yres_virtual = info.yres;
         flags &= ~PAGE_FLIP;
-        LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
+        ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
     }
 
     if (info.yres_virtual < info.yres * 2) {
         // we need at least 2 for page-flipping
         info.yres_virtual = info.yres;
         flags &= ~PAGE_FLIP;
-        LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
+        ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
                 info.yres_virtual, info.yres*2);
     }
 
@@ -222,7 +222,7 @@
     float ydpi = (info.yres * 25.4f) / info.height;
     float fps  = refreshRate / 1000.0f;
 
-    LOGI(   "using (fd=%d)\n"
+    ALOGI(   "using (fd=%d)\n"
             "id           = %s\n"
             "xres         = %d px\n"
             "yres         = %d px\n"
@@ -244,7 +244,7 @@
             info.blue.offset, info.blue.length
     );
 
-    LOGI(   "width        = %d mm (%f dpi)\n"
+    ALOGI(   "width        = %d mm (%f dpi)\n"
             "height       = %d mm (%f dpi)\n"
             "refresh rate = %.2f Hz\n",
             info.width,  xdpi,
@@ -280,7 +280,7 @@
 
     void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
     if (vaddr == MAP_FAILED) {
-        LOGE("Error mapping the framebuffer (%s)", strerror(errno));
+        ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
         return -errno;
     }
     module->framebuffer->base = intptr_t(vaddr);
diff --git a/modules/gralloc/gralloc.cpp b/modules/gralloc/gralloc.cpp
index dcd2e2b..a6b4edd 100644
--- a/modules/gralloc/gralloc.cpp
+++ b/modules/gralloc/gralloc.cpp
@@ -175,7 +175,7 @@
     
     fd = ashmem_create_region("gralloc-buffer", size);
     if (fd < 0) {
-        LOGE("couldn't create ashmem (%s)", strerror(-errno));
+        ALOGE("couldn't create ashmem (%s)", strerror(-errno));
         err = -errno;
     }
 
@@ -189,7 +189,7 @@
         }
     }
     
-    LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
+    ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
     
     return err;
 }
diff --git a/modules/gralloc/gralloc_priv.h b/modules/gralloc/gralloc_priv.h
index 75bcd1d..e1c502a 100644
--- a/modules/gralloc/gralloc_priv.h
+++ b/modules/gralloc/gralloc_priv.h
@@ -101,7 +101,7 @@
                 h->numInts != sNumInts || h->numFds != sNumFds ||
                 hnd->magic != sMagic) 
         {
-            LOGE("invalid gralloc handle (at %p)", h);
+            ALOGE("invalid gralloc handle (at %p)", h);
             return -EINVAL;
         }
         return 0;
diff --git a/modules/gralloc/mapper.cpp b/modules/gralloc/mapper.cpp
index 33d0958..c4096ae 100644
--- a/modules/gralloc/mapper.cpp
+++ b/modules/gralloc/mapper.cpp
@@ -53,11 +53,11 @@
         void* mappedAddress = mmap(0, size,
                 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
         if (mappedAddress == MAP_FAILED) {
-            LOGE("Could not mmap %s", strerror(errno));
+            ALOGE("Could not mmap %s", strerror(errno));
             return -errno;
         }
         hnd->base = intptr_t(mappedAddress) + hnd->offset;
-        //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
+        //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
         //        hnd->fd, hnd->offset, hnd->size, mappedAddress);
     }
     *vaddr = (void*)hnd->base;
@@ -71,9 +71,9 @@
     if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
         void* base = (void*)hnd->base;
         size_t size = hnd->size;
-        //LOGD("unmapping from %p, size=%d", base, size);
+        //ALOGD("unmapping from %p, size=%d", base, size);
         if (munmap(base, size) < 0) {
-            LOGE("Could not unmap %s", strerror(errno));
+            ALOGE("Could not unmap %s", strerror(errno));
         }
     }
     hnd->base = 0;
diff --git a/modules/hwcomposer/hwcomposer.cpp b/modules/hwcomposer/hwcomposer.cpp
index 68b7070..0e04cac 100644
--- a/modules/hwcomposer/hwcomposer.cpp
+++ b/modules/hwcomposer/hwcomposer.cpp
@@ -55,7 +55,7 @@
 /*****************************************************************************/
 
 static void dump_layer(hwc_layer_t const* l) {
-    LOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
+    ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
             l->compositionType, l->flags, l->handle, l->transform, l->blending,
             l->sourceCrop.left,
             l->sourceCrop.top,
diff --git a/modules/local_time/local_time_hw.c b/modules/local_time/local_time_hw.c
index a1f5d43..308f7d9 100644
--- a/modules/local_time/local_time_hw.c
+++ b/modules/local_time/local_time_hw.c
@@ -39,7 +39,7 @@
 
     ret = clock_gettime(CLOCK_MONOTONIC, &ts);
     if (ret < 0) {
-        LOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
+        ALOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
                 dev->common.module->name, ret);
         return 0;
     }
diff --git a/modules/nfc/nfc_hw_example.c b/modules/nfc/nfc_hw_example.c
index 9769282..54c9c56 100644
--- a/modules/nfc/nfc_hw_example.c
+++ b/modules/nfc/nfc_hw_example.c
@@ -44,6 +44,7 @@
         dev->linktype = PN544_LINK_TYPE_INVALID;
         dev->device_node = NULL;
         dev->enable_i2c_workaround = 0;
+        dev->i2c_device_address = 0;
 
         *device = (hw_device_t*) dev;
         return 0;
diff --git a/modules/power/Android.mk b/modules/power/Android.mk
new file mode 100644
index 0000000..50ab221
--- /dev/null
+++ b/modules/power/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2011 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := power.default
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_SRC_FILES := power.c
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/power/power.c b/modules/power/power.c
new file mode 100644
index 0000000..ef3fe94
--- /dev/null
+++ b/modules/power/power.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define LOG_TAG "Legacy PowerHAL"
+#include <utils/Log.h>
+
+#include <hardware/hardware.h>
+#include <hardware/power.h>
+
+/*
+ * This module implements the legacy interface for requesting early
+ * suspend and late resume
+ */
+
+#define LEGACY_SYS_POWER_STATE "/sys/power/state"
+
+static int sPowerStatefd;
+static const char *pwr_states[] = { "mem", "on" };
+
+static void power_init(struct power_module *module)
+{
+    char buf[80];
+
+    sPowerStatefd = open(LEGACY_SYS_POWER_STATE, O_RDWR);
+
+    if (sPowerStatefd < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error opening %s: %s\n", LEGACY_SYS_POWER_STATE, buf);
+    }
+}
+
+static void power_set_interactive(struct power_module *module, int on)
+{
+    char buf[80];
+    int len;
+
+    len = write(sPowerStatefd, pwr_states[!!on], strlen(pwr_states[!!on]));
+    if (len < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error writing to %s: %s\n", LEGACY_SYS_POWER_STATE, buf);
+    }
+}
+
+
+static struct hw_module_methods_t power_module_methods = {
+    .open = NULL,
+};
+
+struct power_module HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .version_major = 1,
+        .version_minor = 0,
+        .id = POWER_HARDWARE_MODULE_ID,
+        .name = "Default Power HAL",
+        .author = "The Android Open Source Project",
+        .methods = &power_module_methods,
+    },
+
+    .init = power_init,
+    .setInteractive = power_set_interactive,
+};
diff --git a/tests/keymaster/Android.mk b/tests/keymaster/Android.mk
new file mode 100644
index 0000000..cd76754
--- /dev/null
+++ b/tests/keymaster/Android.mk
@@ -0,0 +1,30 @@
+# Build the keymaster unit tests
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+    keymaster_test.cpp
+
+# Note that "bionic" is needed because of stlport
+LOCAL_C_INCLUDES := \
+    bionic \
+    libcore/include \
+    external/gtest/include \
+    external/stlport/stlport
+
+LOCAL_SHARED_LIBRARIES := \
+    liblog \
+    libutils \
+    libstlport \
+    libhardware
+
+LOCAL_STATIC_LIBRARIES := \
+    libgtest \
+    libgtest_main
+
+LOCAL_MODULE := keymaster_test
+
+LOCAL_MODULE_TAGS := tests
+
+include $(BUILD_EXECUTABLE)
diff --git a/tests/keymaster/keymaster_test.cpp b/tests/keymaster/keymaster_test.cpp
new file mode 100644
index 0000000..383aeb5
--- /dev/null
+++ b/tests/keymaster/keymaster_test.cpp
@@ -0,0 +1,754 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "keymaster_test"
+#include <utils/Log.h>
+
+#include <hardware/keymaster.h>
+
+#include <gtest/gtest.h>
+
+#include <fstream>
+#include <iostream>
+#include <UniquePtr.h>
+
+namespace android {
+
+class UniqueBlob : public UniquePtr<uint8_t[]> {
+public:
+    UniqueBlob(uint8_t* bytes, size_t length) :
+            UniquePtr<uint8_t[]>(bytes), mLength(length) {
+    }
+
+    bool operator==(const UniqueBlob &other) const {
+        if (other.length() != mLength) {
+            return false;
+        }
+
+        const uint8_t* mine = get();
+        const uint8_t* theirs = other.get();
+
+        for (size_t i = 0; i < mLength; i++) {
+            if (mine[i] != theirs[i]) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    size_t length() const {
+        return mLength;
+    }
+
+    friend std::ostream &operator<<(std::ostream &stream, const UniqueBlob& blob);
+
+private:
+    size_t mLength;
+};
+
+std::ostream &operator<<(std::ostream &stream, const UniqueBlob& blob) {
+    const size_t length = blob.mLength;
+    stream << "Blob length=" << length << " < ";
+
+    const uint8_t* data = blob.get();
+    for (size_t i = 0; i < length; i++) {
+        stream << std::hex << std::setw(2) << std::setfill('0')
+                << static_cast<unsigned int>(data[i]) << ' ';
+    }
+    stream << '>' << std::endl;
+
+    return stream;
+}
+
+class UniqueKey : public UniqueBlob {
+public:
+    UniqueKey(keymaster_device_t** dev, uint8_t* bytes, size_t length) :
+            UniqueBlob(bytes, length), mDevice(dev) {
+    }
+
+    ~UniqueKey() {
+        if (mDevice != NULL && *mDevice != NULL) {
+            keymaster_device_t* dev = *mDevice;
+            if (dev->delete_keypair != NULL) {
+                dev->delete_keypair(dev, get(), length());
+            }
+        }
+    }
+
+private:
+    keymaster_device_t** mDevice;
+};
+
+/*
+ * DER-encoded PKCS#8 format RSA key. Generated using:
+ *
+ * openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | recode ../x1
+ */
+static uint8_t TEST_KEY_1[] = {
+        0x30, 0x82, 0x04, 0xBE, 0x02, 0x01, 0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A,
+        0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
+        0x04, 0xA8, 0x30, 0x82, 0x04, 0xA4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01,
+        0x01, 0x00, 0xD8, 0x58, 0xD4, 0x9F, 0xC0, 0xE8, 0xF0, 0xFF, 0x87, 0x27,
+        0x43, 0xE6, 0x2E, 0xE6, 0x9A, 0x42, 0x3B, 0x39, 0x94, 0x84, 0x43, 0x55,
+        0x8D, 0x20, 0x5B, 0x71, 0x88, 0xE6, 0xD1, 0x62, 0xC8, 0xF2, 0x20, 0xD0,
+        0x75, 0x13, 0x83, 0xA3, 0x5D, 0x19, 0xA8, 0x62, 0xD0, 0x5F, 0x3E, 0x8A,
+        0x7C, 0x0E, 0x26, 0xA9, 0xFF, 0xB2, 0x5E, 0x63, 0xAA, 0x3C, 0x8D, 0x13,
+        0x41, 0xAA, 0xD5, 0x03, 0x01, 0x01, 0x53, 0xC9, 0x02, 0x1C, 0xEC, 0xE8,
+        0xC4, 0x70, 0x3F, 0x43, 0xE5, 0x51, 0xD0, 0x6E, 0x52, 0x0B, 0xC4, 0x0A,
+        0xA3, 0x61, 0xDE, 0xE3, 0x72, 0x0C, 0x94, 0xF1, 0x1C, 0x2D, 0x36, 0x77,
+        0xBB, 0x16, 0xA8, 0x63, 0x4B, 0xD1, 0x07, 0x00, 0x42, 0x2D, 0x2B, 0x10,
+        0x80, 0x45, 0xF3, 0x0C, 0xF9, 0xC5, 0xAC, 0xCC, 0x64, 0x87, 0xFD, 0x5D,
+        0xC8, 0x51, 0xD4, 0x1C, 0x9E, 0x6E, 0x9B, 0xC4, 0x27, 0x5E, 0x73, 0xA7,
+        0x2A, 0xF6, 0x90, 0x42, 0x0C, 0x34, 0x93, 0xB7, 0x02, 0x19, 0xA9, 0x64,
+        0x6C, 0x46, 0x3B, 0x40, 0x02, 0x2F, 0x54, 0x69, 0x79, 0x26, 0x7D, 0xF6,
+        0x85, 0x90, 0x01, 0xD0, 0x21, 0x07, 0xD0, 0x14, 0x00, 0x65, 0x9C, 0xAC,
+        0x24, 0xE8, 0x78, 0x42, 0x3B, 0x90, 0x75, 0x19, 0x55, 0x11, 0x4E, 0xD9,
+        0xE6, 0x97, 0x87, 0xBC, 0x8D, 0x2C, 0x9B, 0xF0, 0x1F, 0x14, 0xEB, 0x6A,
+        0x57, 0xCE, 0x78, 0xAD, 0xCE, 0xD9, 0xFB, 0xB9, 0xA1, 0xEF, 0x0C, 0x1F,
+        0xDD, 0xE3, 0x5B, 0x73, 0xA0, 0xEC, 0x37, 0x9C, 0xE1, 0xFD, 0x86, 0x28,
+        0xC3, 0x4A, 0x42, 0xD0, 0xA3, 0xFE, 0x57, 0x09, 0x29, 0xD8, 0xF6, 0xEC,
+        0xE3, 0xC0, 0x71, 0x7C, 0x29, 0x27, 0xC2, 0xD1, 0x3E, 0x22, 0xBC, 0xBD,
+        0x5A, 0x85, 0x41, 0xF6, 0x15, 0xDA, 0x0C, 0x58, 0x5A, 0x61, 0x5B, 0x78,
+        0xB8, 0xAA, 0xEC, 0x5C, 0x1C, 0x79, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02,
+        0x82, 0x01, 0x00, 0x1D, 0x10, 0x31, 0xE0, 0x14, 0x26, 0x36, 0xD9, 0xDC,
+        0xEA, 0x25, 0x70, 0xF2, 0xB3, 0xFF, 0xDD, 0x0D, 0xDF, 0xBA, 0x57, 0xDA,
+        0x43, 0xCF, 0xE5, 0x9C, 0xE3, 0x2F, 0xA4, 0xF2, 0x53, 0xF6, 0xF2, 0xAF,
+        0xFD, 0xD0, 0xFC, 0x82, 0x1E, 0x9C, 0x0F, 0x2A, 0x53, 0xBB, 0xF2, 0x4F,
+        0x90, 0x83, 0x01, 0xD3, 0xA7, 0xDA, 0xB5, 0xB7, 0x80, 0x64, 0x0A, 0x26,
+        0x59, 0x83, 0xE4, 0xD3, 0x20, 0xC8, 0x2D, 0xC9, 0x77, 0xA3, 0x55, 0x07,
+        0x6E, 0x6D, 0x95, 0x36, 0xAA, 0x84, 0x4F, 0xED, 0x54, 0x24, 0xA9, 0x77,
+        0xF8, 0x85, 0xE2, 0x4B, 0xF2, 0xFA, 0x0B, 0x3E, 0xA6, 0xF5, 0x46, 0x0D,
+        0x9F, 0x1F, 0xFE, 0xF7, 0x37, 0xFF, 0xA3, 0x60, 0xF1, 0x63, 0xF2, 0x75,
+        0x6A, 0x8E, 0x10, 0xD7, 0x89, 0xD2, 0xB3, 0xFF, 0x76, 0xA5, 0xBA, 0xAF,
+        0x0A, 0xBE, 0x32, 0x5F, 0xF0, 0x48, 0x48, 0x4B, 0x9C, 0x9A, 0x3D, 0x12,
+        0xA7, 0xD2, 0x07, 0xC7, 0x59, 0x32, 0x94, 0x95, 0x65, 0x2F, 0x87, 0x34,
+        0x76, 0xBA, 0x7C, 0x08, 0x4B, 0xAB, 0xA6, 0x24, 0xDF, 0x64, 0xDB, 0x48,
+        0x63, 0x42, 0x06, 0xE2, 0x2C, 0x3D, 0xFB, 0xE5, 0x47, 0x81, 0x94, 0x98,
+        0xF7, 0x32, 0x4B, 0x28, 0xEB, 0x42, 0xB8, 0xE9, 0x8E, 0xFC, 0xC9, 0x43,
+        0xC9, 0x47, 0xE6, 0xE7, 0x1C, 0xDC, 0x71, 0xEF, 0x4D, 0x8A, 0xB1, 0xFC,
+        0x45, 0x37, 0xEC, 0xB3, 0x16, 0x88, 0x5B, 0xE2, 0xEC, 0x8B, 0x6B, 0x75,
+        0x16, 0xBE, 0x6B, 0xF8, 0x2C, 0xF8, 0xC9, 0xD1, 0xF7, 0x55, 0x87, 0x57,
+        0x5F, 0xDE, 0xF4, 0x7E, 0x72, 0x13, 0x06, 0x2A, 0x21, 0xB7, 0x78, 0x21,
+        0x05, 0xFD, 0xE2, 0x5F, 0x7B, 0x7C, 0xF0, 0x26, 0x2B, 0x75, 0x7F, 0x68,
+        0xF9, 0xA6, 0x98, 0xFD, 0x54, 0x0E, 0xCC, 0x22, 0x41, 0x7F, 0x29, 0x81,
+        0x2F, 0xA3, 0x3C, 0x3D, 0x64, 0xC8, 0x41, 0x02, 0x81, 0x81, 0x00, 0xFA,
+        0xFA, 0xE4, 0x2E, 0x30, 0xF0, 0x7A, 0x8D, 0x95, 0xB8, 0x39, 0x58, 0x27,
+        0x0F, 0x89, 0x0C, 0xDF, 0xFE, 0x2F, 0x55, 0x3B, 0x6F, 0xDD, 0x5F, 0x12,
+        0xB3, 0xD1, 0xCF, 0x5B, 0x8D, 0xB6, 0x10, 0x1C, 0x87, 0x0C, 0x30, 0x89,
+        0x2D, 0xBB, 0xB8, 0xA1, 0x78, 0x0F, 0x54, 0xA6, 0x36, 0x46, 0x05, 0x8B,
+        0x5A, 0xFF, 0x48, 0x03, 0x13, 0xAE, 0x95, 0x96, 0x5D, 0x6C, 0xDA, 0x5D,
+        0xF7, 0xAD, 0x1D, 0x33, 0xED, 0x23, 0xF5, 0x4B, 0x03, 0x78, 0xE7, 0x50,
+        0xD1, 0x2D, 0x95, 0x22, 0x35, 0x02, 0x5B, 0x4A, 0x4E, 0x73, 0xC9, 0xB7,
+        0x05, 0xC4, 0x21, 0x86, 0x1F, 0x1E, 0x40, 0x83, 0xBC, 0x8A, 0x3A, 0x95,
+        0x24, 0x62, 0xF4, 0x58, 0x38, 0x64, 0x4A, 0x89, 0x8A, 0x27, 0x59, 0x12,
+        0x9D, 0x21, 0xC3, 0xA6, 0x42, 0x1E, 0x2A, 0x3F, 0xD8, 0x65, 0x1F, 0x6E,
+        0x3E, 0x4D, 0x5C, 0xCC, 0xEA, 0x8E, 0x15, 0x02, 0x81, 0x81, 0x00, 0xDC,
+        0xAC, 0x9B, 0x00, 0xDB, 0xF9, 0xB2, 0xBF, 0xC4, 0x5E, 0xB6, 0xB7, 0x63,
+        0xEB, 0x13, 0x4B, 0xE2, 0xA6, 0xC8, 0x72, 0x90, 0xD8, 0xC2, 0x33, 0x33,
+        0xF0, 0x66, 0x75, 0xBD, 0x50, 0x7C, 0xA4, 0x8F, 0x82, 0xFB, 0xFF, 0x44,
+        0x3B, 0xE7, 0x15, 0x3A, 0x0C, 0x7A, 0xF8, 0x92, 0x86, 0x4A, 0x79, 0x32,
+        0x08, 0x82, 0x1D, 0x6A, 0xBA, 0xAD, 0x8A, 0xB3, 0x3D, 0x7F, 0xA5, 0xB4,
+        0x6F, 0x67, 0x86, 0x7E, 0xB2, 0x9C, 0x2A, 0xF6, 0x7C, 0x49, 0x21, 0xC5,
+        0x3F, 0x00, 0x3F, 0x9B, 0xF7, 0x0F, 0x6C, 0x35, 0x80, 0x75, 0x73, 0xC0,
+        0xF8, 0x3E, 0x30, 0x5F, 0x74, 0x2F, 0x15, 0x41, 0xEA, 0x0F, 0xCE, 0x0E,
+        0x18, 0x17, 0x68, 0xBA, 0xC4, 0x29, 0xF2, 0xE2, 0x2C, 0x1D, 0x55, 0x83,
+        0xB6, 0x64, 0x2E, 0x03, 0x12, 0xA4, 0x0D, 0xBF, 0x4F, 0x2E, 0xBE, 0x7C,
+        0x41, 0xD9, 0xCD, 0xD0, 0x52, 0x91, 0xD5, 0x02, 0x81, 0x81, 0x00, 0xD4,
+        0x55, 0xEB, 0x32, 0xC1, 0x28, 0xD3, 0x26, 0x72, 0x22, 0xB8, 0x31, 0x42,
+        0x6A, 0xBC, 0x52, 0x6E, 0x37, 0x48, 0xA8, 0x5D, 0x6E, 0xD8, 0xE5, 0x14,
+        0x97, 0x99, 0xCC, 0x4A, 0xF2, 0xEB, 0xB3, 0x59, 0xCF, 0x4F, 0x9A, 0xC8,
+        0x94, 0x2E, 0x9B, 0x97, 0xD0, 0x51, 0x78, 0x16, 0x5F, 0x18, 0x82, 0x9C,
+        0x51, 0xD2, 0x64, 0x84, 0x65, 0xE4, 0x70, 0x9E, 0x14, 0x50, 0x81, 0xB6,
+        0xBA, 0x52, 0x75, 0xC0, 0x76, 0xC2, 0xD3, 0x46, 0x31, 0x9B, 0xDA, 0x67,
+        0xDF, 0x71, 0x27, 0x19, 0x17, 0xAB, 0xF4, 0xBC, 0x3A, 0xFF, 0x6F, 0x0B,
+        0x2F, 0x0F, 0xAE, 0x25, 0x20, 0xB2, 0xA1, 0x76, 0x52, 0xCE, 0xC7, 0x9D,
+        0x62, 0x79, 0x6D, 0xAC, 0x2D, 0x99, 0x7C, 0x0E, 0x3D, 0x19, 0xE9, 0x1B,
+        0xFC, 0x60, 0x92, 0x7C, 0x58, 0xB7, 0xD8, 0x9A, 0xC7, 0x63, 0x56, 0x62,
+        0x18, 0xC7, 0xAE, 0xD9, 0x97, 0x1F, 0xB9, 0x02, 0x81, 0x81, 0x00, 0x91,
+        0x40, 0xC4, 0x1E, 0x82, 0xAD, 0x0F, 0x6D, 0x8E, 0xD2, 0x51, 0x2E, 0xD1,
+        0x84, 0x30, 0x85, 0x68, 0xC1, 0x23, 0x7B, 0xD5, 0xBF, 0xF7, 0xC4, 0x40,
+        0x51, 0xE2, 0xFF, 0x69, 0x07, 0x8B, 0xA3, 0xBE, 0x1B, 0x17, 0xC8, 0x64,
+        0x9F, 0x91, 0x71, 0xB5, 0x6D, 0xF5, 0x9B, 0x9C, 0xC6, 0xEC, 0x4A, 0x6E,
+        0x16, 0x8F, 0x9E, 0xD1, 0x5B, 0xE3, 0x53, 0x42, 0xBC, 0x1E, 0x43, 0x72,
+        0x4B, 0x4A, 0x37, 0x8B, 0x3A, 0x01, 0xF5, 0x7D, 0x9D, 0x3D, 0x7E, 0x0F,
+        0x19, 0x73, 0x0E, 0x6B, 0x98, 0xE9, 0xFB, 0xEE, 0x13, 0x8A, 0x3C, 0x11,
+        0x2E, 0xD5, 0xB0, 0x7D, 0x84, 0x3A, 0x61, 0xA1, 0xAB, 0x71, 0x8F, 0xCE,
+        0x53, 0x29, 0x45, 0x74, 0x7A, 0x1E, 0xAA, 0x93, 0x19, 0x3A, 0x8D, 0xC9,
+        0x4E, 0xCB, 0x0E, 0x46, 0x53, 0x84, 0xCC, 0xCF, 0xBA, 0x4D, 0x28, 0x71,
+        0x1D, 0xDF, 0x41, 0xCB, 0xF8, 0x2D, 0xA9, 0x02, 0x81, 0x80, 0x04, 0x8B,
+        0x4A, 0xEA, 0xBD, 0x39, 0x0B, 0x96, 0xC5, 0x1D, 0xA4, 0x47, 0xFD, 0x46,
+        0xD2, 0x8A, 0xEA, 0x2A, 0xF3, 0x9D, 0x3A, 0x7E, 0x16, 0x74, 0xFC, 0x13,
+        0xDE, 0x4D, 0xA9, 0x85, 0x42, 0x33, 0x02, 0x92, 0x0B, 0xB6, 0xDB, 0x7E,
+        0xEA, 0x85, 0xC2, 0x94, 0x43, 0x52, 0x37, 0x5A, 0x77, 0xAB, 0xCB, 0x61,
+        0x88, 0xDE, 0xF8, 0xFA, 0xDB, 0xE8, 0x0B, 0x95, 0x7D, 0x39, 0x19, 0xA2,
+        0x89, 0xB9, 0x32, 0xB2, 0x50, 0x38, 0xF7, 0x88, 0x69, 0xFD, 0xA4, 0x63,
+        0x1F, 0x9B, 0x03, 0xD8, 0xA6, 0x7A, 0x05, 0x76, 0x02, 0x28, 0x93, 0x82,
+        0x73, 0x7F, 0x14, 0xCC, 0xBE, 0x29, 0x10, 0xAD, 0x8A, 0x2E, 0xAC, 0xED,
+        0x11, 0xA7, 0x72, 0x7C, 0x60, 0x78, 0x72, 0xFB, 0x78, 0x20, 0x18, 0xC9,
+        0x7E, 0x63, 0xAD, 0x55, 0x54, 0x51, 0xDB, 0x9F, 0x7B, 0xD4, 0x8F, 0xB2,
+        0xDE, 0x3B, 0xF1, 0x70, 0x23, 0xE5,
+};
+
+/*
+ * Generated using keys on the keyboard and lack of imagination.
+ */
+static unsigned char BOGUS_KEY_1[] = { 0xFF, 0xFF, 0xFF, 0xFF };
+
+
+class KeymasterTest : public testing::Test {
+protected:
+    static void SetUpTestCase() {
+        const hw_module_t* mod;
+        ASSERT_EQ(0, hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod))
+                << "Should be able to find a keymaster hardware module";
+
+        std::cout << "Using keymaster module: " << mod->name << std::endl;
+
+        ASSERT_EQ(0, keymaster_open(mod, &sDevice))
+                << "Should be able to open the keymaster device";
+
+        ASSERT_TRUE(sDevice->generate_keypair != NULL)
+                << "Should implement generate_keypair";
+
+        ASSERT_TRUE(sDevice->import_keypair != NULL)
+                << "Should implement import_keypair";
+
+        ASSERT_TRUE(sDevice->get_keypair_public != NULL)
+                << "Should implement get_keypair_public";
+
+        ASSERT_TRUE(sDevice->sign_data != NULL)
+                << "Should implement sign_data";
+
+        ASSERT_TRUE(sDevice->verify_data != NULL)
+                << "Should implement verify_data";
+    }
+
+    static void TearDownTestCase() {
+        ASSERT_EQ(0, keymaster_close(sDevice));
+    }
+
+    virtual void SetUp() {
+    }
+
+    virtual void TearDown() {
+    }
+
+    static keymaster_device_t* sDevice;
+};
+
+keymaster_device_t* KeymasterTest::sDevice = NULL;
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_512_Success) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+    keymaster_rsa_keygen_params_t params = {
+            modulus_size: 512,
+            public_exponent: 0x10001L,
+    };
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->generate_keypair(sDevice, key_type, &params, &key_blob, &key_blob_length))
+            << "Should generate an RSA key with 512 bit modulus size";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_1024_Success) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+    keymaster_rsa_keygen_params_t params = {
+            modulus_size: 1024,
+            public_exponent: 0x3L,
+    };
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->generate_keypair(sDevice, key_type, &params, &key_blob, &key_blob_length))
+            << "Should generate an RSA key with 2048 bit modulus size";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_2048_Success) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+    keymaster_rsa_keygen_params_t params = {
+            modulus_size: 2048,
+            public_exponent: 0x3L,
+    };
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->generate_keypair(sDevice, key_type, &params, &key_blob, &key_blob_length))
+            << "Should generate an RSA key with 2048 bit modulus size";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_NullParams_Failure) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->generate_keypair(sDevice, key_type, NULL, &key_blob, &key_blob_length))
+            << "Should not be able to generate an RSA key with null params";
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_UnknownType_Failure) {
+    keymaster_keypair_t key_type = static_cast<keymaster_keypair_t>(0xFFFF);
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->generate_keypair(sDevice, key_type, NULL, &key_blob, &key_blob_length))
+            << "Should not generate an unknown key type";
+}
+
+TEST_F(KeymasterTest, ImportKeyPair_RSA_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, ImportKeyPair_BogusKey_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->import_keypair(sDevice, BOGUS_KEY_1, sizeof(BOGUS_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should not import an unknown key type";
+}
+
+TEST_F(KeymasterTest, ImportKeyPair_NullKey_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->import_keypair(sDevice, NULL, 0,
+                    &key_blob, &key_blob_length))
+            << "Should not import a null key";
+}
+
+TEST_F(KeymasterTest, GetKeypairPublic_RSA_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    uint8_t* x509_data;
+    size_t x509_data_length;
+    EXPECT_EQ(0,
+            sDevice->get_keypair_public(sDevice, key_blob, key_blob_length,
+                    &x509_data, &x509_data_length))
+            << "Should be able to retrieve RSA public key successfully";
+    UniqueBlob x509_blob(x509_data, x509_data_length);
+}
+
+TEST_F(KeymasterTest, GetKeypairPublic_RSA_NullKey_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    uint8_t* x509_data = NULL;
+    size_t x509_data_length;
+    EXPECT_EQ(-1,
+            sDevice->get_keypair_public(sDevice, NULL, 0,
+                    &x509_data, &x509_data_length))
+            << "Should not be able to retrieve RSA public key from null key";
+    UniqueBlob x509_blob(x509_data, x509_data_length);
+}
+
+TEST_F(KeymasterTest, GetKeypairPublic_RSA_NullDestination_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    EXPECT_EQ(-1,
+            sDevice->get_keypair_public(sDevice, key.get(), key.length(),
+                    NULL, NULL))
+            << "Should not be able to succeed with NULL destination blob";
+}
+
+TEST_F(KeymasterTest, DeleteKeyPair_RSA_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, DeleteKeyPair_RSA_DoubleDelete_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    /*
+     * This is only run if the module indicates it implements key deletion
+     * by implementing delete_keypair.
+     */
+    if (sDevice->delete_keypair != NULL) {
+        ASSERT_EQ(0,
+                sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                        &key_blob, &key_blob_length))
+                << "Should successfully import an RSA key";
+        UniqueBlob blob(key_blob, key_blob_length);
+
+        EXPECT_EQ(0, sDevice->delete_keypair(sDevice, key_blob, key_blob_length))
+                << "Should delete key after import";
+
+        EXPECT_EQ(-1, sDevice->delete_keypair(sDevice, key_blob, key_blob_length))
+                << "Should not be able to delete key twice";
+    }
+}
+
+TEST_F(KeymasterTest, DeleteKeyPair_RSA_NullKey_Failure) {
+    /*
+     * This is only run if the module indicates it implements key deletion
+     * by implementing delete_keypair.
+     */
+    if (sDevice->delete_keypair != NULL) {
+        EXPECT_EQ(-1, sDevice->delete_keypair(sDevice, NULL, 0))
+                << "Should not be able to delete null key";
+    }
+}
+
+/*
+ * DER-encoded PKCS#8 format RSA key. Generated using:
+ *
+ * openssl genrsa 512 | openssl pkcs8 -topk8 -nocrypt -outform der | recode ../x1
+ */
+static uint8_t TEST_SIGN_KEY_1[] = {
+        0x30, 0x82, 0x01, 0x56, 0x02, 0x01, 0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A,
+        0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
+        0x01, 0x40, 0x30, 0x82, 0x01, 0x3C, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
+        0xBD, 0xC0, 0x7F, 0xEF, 0x75, 0x1D, 0x63, 0x2A, 0xD0, 0x9A, 0x26, 0xE5,
+        0x5B, 0xB9, 0x84, 0x7C, 0xE5, 0xC7, 0xE7, 0xDE, 0xFE, 0xB6, 0x54, 0xD9,
+        0xF0, 0x9B, 0xC2, 0xCF, 0x36, 0xDA, 0xE5, 0x4D, 0xC5, 0xD9, 0x25, 0x78,
+        0xBD, 0x55, 0x05, 0xBD, 0x86, 0xFB, 0x37, 0x15, 0x33, 0x42, 0x52, 0xED,
+        0xE5, 0xCD, 0xCB, 0xB7, 0xA2, 0x51, 0xFA, 0x36, 0xE9, 0x9C, 0x2E, 0x5D,
+        0xE3, 0xA5, 0x1F, 0x01, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x41, 0x00,
+        0x96, 0x71, 0xDE, 0xBD, 0x83, 0x94, 0x96, 0x40, 0xA6, 0xFD, 0xE1, 0xA2,
+        0xED, 0xD3, 0xAC, 0x28, 0xBE, 0xA2, 0x7D, 0xC3, 0xFF, 0x1D, 0x9F, 0x2E,
+        0xE0, 0xA7, 0x0E, 0x90, 0xEE, 0x44, 0x25, 0x92, 0xE3, 0x54, 0xDD, 0x55,
+        0xA3, 0xEF, 0x42, 0xF5, 0x52, 0x55, 0x41, 0x47, 0x5E, 0x00, 0xFB, 0x8B,
+        0x47, 0x5E, 0x45, 0x49, 0xEA, 0x3D, 0x2C, 0xFD, 0x9F, 0xEC, 0xC8, 0x4E,
+        0x4E, 0x86, 0x90, 0x31, 0x02, 0x21, 0x00, 0xE6, 0xA5, 0x55, 0xB3, 0x64,
+        0xAB, 0x90, 0x5E, 0xA2, 0xF5, 0x6B, 0x21, 0x4B, 0x15, 0xD6, 0x4A, 0xB6,
+        0x60, 0x24, 0x95, 0x65, 0xA2, 0xBE, 0xBA, 0x2A, 0x73, 0xFB, 0xFF, 0x2C,
+        0x61, 0x88, 0x9D, 0x02, 0x21, 0x00, 0xD2, 0x9C, 0x5B, 0xFE, 0x82, 0xA5,
+        0xFC, 0x52, 0x6A, 0x29, 0x38, 0xDB, 0x22, 0x3B, 0xEB, 0x74, 0x3B, 0xCA,
+        0xB4, 0xDD, 0x1D, 0xE4, 0x48, 0x60, 0x70, 0x19, 0x9B, 0x81, 0xC1, 0x83,
+        0x28, 0xB5, 0x02, 0x21, 0x00, 0x89, 0x2D, 0xFE, 0xF9, 0xF2, 0xBF, 0x43,
+        0xDF, 0xB5, 0xA6, 0xA8, 0x30, 0x26, 0x1B, 0x77, 0xD7, 0xF9, 0xFE, 0xD6,
+        0xE3, 0x70, 0x8E, 0xCA, 0x47, 0xA9, 0xA6, 0x50, 0x54, 0x25, 0xCE, 0x60,
+        0xD5, 0x02, 0x21, 0x00, 0xBE, 0x5A, 0xF8, 0x82, 0xE6, 0xCE, 0xE3, 0x6A,
+        0x11, 0xED, 0xC4, 0x27, 0xBB, 0x9F, 0x70, 0xC6, 0x93, 0xAC, 0x39, 0x20,
+        0x89, 0x7D, 0xE5, 0x34, 0xD4, 0xDD, 0x30, 0x42, 0x6D, 0x07, 0x00, 0xE9,
+        0x02, 0x20, 0x05, 0x91, 0xEF, 0x12, 0xD2, 0xD3, 0x6A, 0xD2, 0x96, 0x6B,
+        0x10, 0x62, 0xF9, 0xBA, 0xA4, 0x91, 0x48, 0x84, 0x40, 0x61, 0x67, 0x80,
+        0x68, 0x68, 0xC8, 0x60, 0xB3, 0x66, 0xC8, 0xF9, 0x08, 0x9A,
+};
+
+/*
+ * PKCS#1 v1.5 padded raw "Hello, world"  Can be generated be generated by verifying
+ * the signature below in no padding mode:
+ *
+ * openssl rsautl -keyform der -inkey rsa.der -raw -verify -in test.sig
+ */
+static uint8_t TEST_SIGN_DATA_1[] = {
+        0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x77,
+        0x6F, 0x72, 0x6C, 0x64,
+};
+
+/*
+ * Signature of TEST_SIGN_DATA_1 using TEST_SIGN_KEY_1. Generated using:
+ *
+ * echo 'Hello, world' | openssl rsautl -keyform der -inkey rsa.der -sign | recode ../x1
+ */
+static uint8_t TEST_SIGN_SIGNATURE_1[] = {
+        0xA4, 0xBB, 0x76, 0x87, 0xFE, 0x61, 0x0C, 0x9D, 0xD6, 0xFF, 0x4B, 0x76,
+        0x96, 0x08, 0x36, 0x23, 0x11, 0xC6, 0x44, 0x3F, 0x88, 0x77, 0x97, 0xB2,
+        0xA8, 0x3B, 0xFB, 0x9C, 0x3C, 0xD3, 0x20, 0x65, 0xFD, 0x26, 0x3B, 0x2A,
+        0xB8, 0xB6, 0xD4, 0xDC, 0x91, 0xF7, 0xE2, 0xDE, 0x4D, 0xF7, 0x0E, 0xB9,
+        0x72, 0xA7, 0x29, 0x72, 0x82, 0x12, 0x7C, 0x53, 0x23, 0x21, 0xC4, 0xFF,
+        0x79, 0xE4, 0x91, 0x40,
+};
+
+/*
+ * Identical to TEST_SIGN_SIGNATURE_1 except the last octet is '1' instead of '0'
+ * This should fail any test.
+ */
+static uint8_t TEST_SIGN_SIGNATURE_BOGUS_1[] = {
+        0xA4, 0xBB, 0x76, 0x87, 0xFE, 0x61, 0x0C, 0x9D, 0xD6, 0xFF, 0x4B, 0x76,
+        0x96, 0x08, 0x36, 0x23, 0x11, 0xC6, 0x44, 0x3F, 0x88, 0x77, 0x97, 0xB2,
+        0xA8, 0x3B, 0xFB, 0x9C, 0x3C, 0xD3, 0x20, 0x65, 0xFD, 0x26, 0x3B, 0x2A,
+        0xB8, 0xB6, 0xD4, 0xDC, 0x91, 0xF7, 0xE2, 0xDE, 0x4D, 0xF7, 0x0E, 0xB9,
+        0x72, 0xA7, 0x29, 0x72, 0x82, 0x12, 0x7C, 0x53, 0x23, 0x21, 0xC4, 0xFF,
+        0x79, 0xE4, 0x91, 0x41,
+};
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(0,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    &sig, &sig_length))
+            << "Should sign data successfully";
+    UniqueBlob sig_blob(sig, sig_length);
+
+    UniqueBlob expected_sig(TEST_SIGN_SIGNATURE_1, sizeof(TEST_SIGN_SIGNATURE_1));
+
+    EXPECT_EQ(expected_sig, sig_blob)
+            << "Generated signature should match expected signature";
+
+    // The expected signature is actually stack data, so don't let it try to free.
+    uint8_t* unused __attribute__((unused)) = expected_sig.release();
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_InvalidSizeInput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &sig, &sig_length))
+            << "Should not be able to do raw signature on incorrect size data";
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_NullKey_Failure) {
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, NULL, 0,
+                    TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &sig, &sig_length))
+            << "Should not be able to do raw signature on incorrect size data";
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_NullInput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    NULL, 0,
+                    &sig, &sig_length))
+            << "Should error when input data is null";
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_NullOutput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_KEY_1, sizeof(TEST_KEY_1),
+                    NULL, NULL))
+            << "Should error when output is null";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_Raw_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(0,
+            sDevice->verify_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    TEST_SIGN_SIGNATURE_1, sizeof(TEST_SIGN_SIGNATURE_1)))
+            << "Should verify data successfully";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_Raw_BadSignature_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    TEST_SIGN_SIGNATURE_BOGUS_1, sizeof(TEST_SIGN_SIGNATURE_BOGUS_1)))
+            << "Should sign data successfully";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_Raw_NullKey_Failure) {
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, NULL, 0,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    TEST_SIGN_SIGNATURE_BOGUS_1, sizeof(TEST_SIGN_SIGNATURE_BOGUS_1)))
+            << "Should fail when key is null";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_NullInput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, key_blob, key_blob_length,
+                    NULL, 0,
+                    TEST_SIGN_SIGNATURE_1, sizeof(TEST_SIGN_SIGNATURE_1)))
+            << "Should fail on null input";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_NullSignature_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, key.get(), key.length(),
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    NULL, 0))
+            << "Should fail on null signature";
+}
+
+}