blob: e6d80c80c239e1185be3f545cef08ce1a9b8c080 [file] [log] [blame]
Stefan Berger5aa2a752015-03-23 14:22:17 -04001// Support for Calculation of SHA1 in SW
2//
3// Copyright (C) 2006-2011 IBM Corporation
4//
5// Authors:
6// Stefan Berger <stefanb@linux.vnet.ibm.com>
7//
8// This file may be distributed under the terms of the GNU LGPLv3 license.
9//
10// See: http://www.itl.nist.gov/fipspubs/fip180-1.htm
11// RFC3174, Wikipedia's SHA1 alogrithm description
12//
13
14#include "config.h"
15#include "byteorder.h" // cpu_to_*, __swab64
Stefan Berger4e57a542021-06-14 13:35:48 -040016#include "sha.h" // sha1
Stefan Berger5aa2a752015-03-23 14:22:17 -040017#include "string.h" // memcpy
18#include "x86.h" // rol
19
20typedef struct _sha1_ctx {
21 u32 h[5];
22} sha1_ctx;
23
24
25static void
26sha1_block(u32 *w, sha1_ctx *ctx)
27{
28 u32 i;
29 u32 a,b,c,d,e,f;
30 u32 tmp;
31 u32 idx;
32
33 static const u32 sha_ko[4] = {
34 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
35
Stefan Weil6bcacf72015-10-02 08:46:40 +020036 /* change endianness of given data */
Stefan Berger5aa2a752015-03-23 14:22:17 -040037 for (i = 0; i < 16; i++)
38 w[i] = be32_to_cpu(w[i]);
39
40 for (i = 16; i <= 79; i++) {
41 tmp = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
42 w[i] = rol(tmp,1);
43 }
44
45 a = ctx->h[0];
46 b = ctx->h[1];
47 c = ctx->h[2];
48 d = ctx->h[3];
49 e = ctx->h[4];
50
51 for (i = 0; i <= 79; i++) {
52 if (i <= 19) {
53 f = (b & c) | ((b ^ 0xffffffff) & d);
54 idx = 0;
55 } else if (i <= 39) {
56 f = b ^ c ^ d;
57 idx = 1;
58 } else if (i <= 59) {
59 f = (b & c) | (b & d) | (c & d);
60 idx = 2;
61 } else {
62 f = b ^ c ^ d;
63 idx = 3;
64 }
65
66 tmp = rol(a, 5) +
67 f +
68 e +
69 sha_ko[idx] +
70 w[i];
71 e = d;
72 d = c;
73 c = rol(b, 30);
74 b = a;
75 a = tmp;
76 }
77
78 ctx->h[0] += a;
79 ctx->h[1] += b;
80 ctx->h[2] += c;
81 ctx->h[3] += d;
82 ctx->h[4] += e;
83}
84
85
86static void
87sha1_do(sha1_ctx *ctx, const u8 *data32, u32 length)
88{
89 u32 offset;
90 u16 num;
91 u32 bits = 0;
92 u32 w[80];
93 u64 tmp;
94
95 /* treat data in 64-byte chunks */
96 for (offset = 0; length - offset >= 64; offset += 64) {
97 memcpy(w, data32 + offset, 64);
98 sha1_block((u32 *)w, ctx);
99 bits += (64 * 8);
100 }
101
102 /* last block with less than 64 bytes */
103 num = length - offset;
104 bits += (num << 3);
105
106 memcpy(w, data32 + offset, num);
107 ((u8 *)w)[num] = 0x80;
108 if (64 - (num + 1) > 0)
109 memset( &((u8 *)w)[num + 1], 0x0, 64 - (num + 1));
110
111 if (num >= 56) {
112 /* cannot append number of bits here */
113 sha1_block((u32 *)w, ctx);
114 memset(w, 0x0, 60);
115 }
116
117 /* write number of bits to end of block */
118 tmp = __swab64(bits);
119 memcpy(&w[14], &tmp, 8);
120
121 sha1_block(w, ctx);
122
Stefan Weil6bcacf72015-10-02 08:46:40 +0200123 /* need to switch result's endianness */
Stefan Berger5aa2a752015-03-23 14:22:17 -0400124 for (num = 0; num < 5; num++)
125 ctx->h[num] = cpu_to_be32(ctx->h[num]);
126}
127
128
Stefan Berger4e57a542021-06-14 13:35:48 -0400129void
Stefan Berger5aa2a752015-03-23 14:22:17 -0400130sha1(const u8 *data, u32 length, u8 *hash)
131{
132 if (!CONFIG_TCGBIOS)
Stefan Berger4e57a542021-06-14 13:35:48 -0400133 return;
Stefan Berger5aa2a752015-03-23 14:22:17 -0400134
135 sha1_ctx ctx = {
136 .h[0] = 0x67452301,
137 .h[1] = 0xefcdab89,
138 .h[2] = 0x98badcfe,
139 .h[3] = 0x10325476,
140 .h[4] = 0xc3d2e1f0,
141 };
142
143 sha1_do(&ctx, data, length);
144 memcpy(hash, &ctx.h[0], 20);
145
Stefan Berger4e57a542021-06-14 13:35:48 -0400146 return;
Stefan Berger5aa2a752015-03-23 14:22:17 -0400147}