These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / sha256.c
index 36e02b3..0360d8d 100644 (file)
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
  */
 
-FILE_LICENCE ( GPL2_OR_LATER );
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
 /** @file
  *
@@ -47,11 +51,11 @@ struct sha256_variables {
        uint32_t f;
        uint32_t g;
        uint32_t h;
-       uint32_t w[64];
+       uint32_t w[SHA256_ROUNDS];
 } __attribute__ (( packed ));
 
 /** SHA-256 constants */
-static const uint32_t k[64] = {
+static const uint32_t k[SHA256_ROUNDS] = {
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
        0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
        0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
@@ -65,6 +69,37 @@ static const uint32_t k[64] = {
        0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 };
 
+/** SHA-256 initial digest values */
+static const struct sha256_digest sha256_init_digest = {
+       .h = {
+               cpu_to_be32 ( 0x6a09e667 ),
+               cpu_to_be32 ( 0xbb67ae85 ),
+               cpu_to_be32 ( 0x3c6ef372 ),
+               cpu_to_be32 ( 0xa54ff53a ),
+               cpu_to_be32 ( 0x510e527f ),
+               cpu_to_be32 ( 0x9b05688c ),
+               cpu_to_be32 ( 0x1f83d9ab ),
+               cpu_to_be32 ( 0x5be0cd19 ),
+       },
+};
+
+/**
+ * Initialise SHA-256 family algorithm
+ *
+ * @v context          SHA-256 context
+ * @v init             Initial digest values
+ * @v digestsize       Digest size
+ */
+void sha256_family_init ( struct sha256_context *context,
+                         const struct sha256_digest *init,
+                         size_t digestsize ) {
+
+       context->len = 0;
+       context->digestsize = digestsize;
+       memcpy ( &context->ddd.dd.digest, init,
+                sizeof ( context->ddd.dd.digest ) );
+}
+
 /**
  * Initialise SHA-256 algorithm
  *
@@ -73,15 +108,8 @@ static const uint32_t k[64] = {
 static void sha256_init ( void *ctx ) {
        struct sha256_context *context = ctx;
 
-       context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x6a09e667 );
-       context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xbb67ae85 );
-       context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x3c6ef372 );
-       context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0xa54ff53a );
-       context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0x510e527f );
-       context->ddd.dd.digest.h[5] = cpu_to_be32 ( 0x9b05688c );
-       context->ddd.dd.digest.h[6] = cpu_to_be32 ( 0x1f83d9ab );
-       context->ddd.dd.digest.h[7] = cpu_to_be32 ( 0x5be0cd19 );
-       context->len = 0;
+       sha256_family_init ( context, &sha256_init_digest,
+                            sizeof ( struct sha256_digest ) );
 }
 
 /**
@@ -139,7 +167,7 @@ static void sha256_digest ( struct sha256_context *context ) {
        }
 
        /* Initialise w[16..63] */
-       for ( i = 16 ; i < 64 ; i++ ) {
+       for ( i = 16 ; i < SHA256_ROUNDS ; i++ ) {
                s0 = ( ror32 ( w[i-15], 7 ) ^ ror32 ( w[i-15], 18 ) ^
                       ( w[i-15] >> 3 ) );
                s1 = ( ror32 ( w[i-2], 17 ) ^ ror32 ( w[i-2], 19 ) ^
@@ -148,7 +176,7 @@ static void sha256_digest ( struct sha256_context *context ) {
        }
 
        /* Main loop */
-       for ( i = 0 ; i < 64 ; i++ ) {
+       for ( i = 0 ; i < SHA256_ROUNDS ; i++ ) {
                s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
                maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
                t2 = ( s0 + maj );
@@ -186,7 +214,7 @@ static void sha256_digest ( struct sha256_context *context ) {
  * @v data             Data
  * @v len              Length of data
  */
-static void sha256_update ( void *ctx, const void *data, size_t len ) {
+void sha256_update ( void *ctx, const void *data, size_t len ) {
        struct sha256_context *context = ctx;
        const uint8_t *byte = data;
        size_t offset;
@@ -209,7 +237,7 @@ static void sha256_update ( void *ctx, const void *data, size_t len ) {
  * @v ctx              SHA-256 context
  * @v out              Output buffer
  */
-static void sha256_final ( void *ctx, void *out ) {
+void sha256_final ( void *ctx, void *out ) {
        struct sha256_context *context = ctx;
        uint64_t len_bits;
        uint8_t pad;
@@ -230,8 +258,7 @@ static void sha256_final ( void *ctx, void *out ) {
        assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
 
        /* Copy out final digest */
-       memcpy ( out, &context->ddd.dd.digest,
-                sizeof ( context->ddd.dd.digest ) );
+       memcpy ( out, &context->ddd.dd.digest, context->digestsize );
 }
 
 /** SHA-256 algorithm */