]> git.sur5r.net Git - u-boot/blobdiff - lib/sha256.c
SPDX: Convert all of our single license tags to Linux Kernel style
[u-boot] / lib / sha256.c
index deb63a40b6d7218145e807850ff51cce488a7e4b..c1fe93de012dc8006abd14e7c95c5f6c4852c58c 100644 (file)
@@ -1,29 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * FIPS-180-2 compliant SHA-256 implementation
  *
  * Copyright (C) 2001-2003  Christophe Devine
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #ifndef USE_HOSTCC
 #include <common.h>
+#include <linux/string.h>
+#else
+#include <string.h>
 #endif /* USE_HOSTCC */
 #include <watchdog.h>
-#include <linux/string.h>
-#include <sha256.h>
+#include <u-boot/sha256.h>
+
+const uint8_t sha256_der_prefix[SHA256_DER_LEN] = {
+       0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
+       0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
+       0x00, 0x04, 0x20
+};
 
 /*
  * 32-bit integer manipulation macros (big endian)
@@ -60,7 +55,7 @@ void sha256_starts(sha256_context * ctx)
        ctx->state[7] = 0x5BE0CD19;
 }
 
-void sha256_process(sha256_context * ctx, uint8_t data[64])
+static void sha256_process(sha256_context *ctx, const uint8_t data[64])
 {
        uint32_t temp1, temp2;
        uint32_t W[64];
@@ -191,7 +186,7 @@ void sha256_process(sha256_context * ctx, uint8_t data[64])
        ctx->state[7] += H;
 }
 
-void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length)
+void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
 
@@ -260,3 +255,37 @@ void sha256_finish(sha256_context * ctx, uint8_t digest[32])
        PUT_UINT32_BE(ctx->state[6], digest, 24);
        PUT_UINT32_BE(ctx->state[7], digest, 28);
 }
+
+/*
+ * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz'
+ * bytes of input processed.
+ */
+void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
+               unsigned char *output, unsigned int chunk_sz)
+{
+       sha256_context ctx;
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+       const unsigned char *end;
+       unsigned char *curr;
+       int chunk;
+#endif
+
+       sha256_starts(&ctx);
+
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+       curr = (unsigned char *)input;
+       end = input + ilen;
+       while (curr < end) {
+               chunk = end - curr;
+               if (chunk > chunk_sz)
+                       chunk = chunk_sz;
+               sha256_update(&ctx, curr, chunk);
+               curr += chunk;
+               WATCHDOG_RESET();
+       }
+#else
+       sha256_update(&ctx, input, ilen);
+#endif
+
+       sha256_finish(&ctx, output);
+}