Sha256: 20a35f9d5e7d39cd362208517c2c86305312452f820384786f6e6940106c249f

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

package jose

import (
	"errors"
	"fmt"
	"github.com/dvsekhvalnov/jose2go/aes"
	"github.com/dvsekhvalnov/jose2go/arrays"
)

func init() {
	RegisterJwa(&AesKW{ keySizeBits: 128})
	RegisterJwa(&AesKW{ keySizeBits: 192})
	RegisterJwa(&AesKW{ keySizeBits: 256})
}

// AES Key Wrap key management algorithm implementation
type AesKW struct {
	keySizeBits int
}

func (alg *AesKW) Name() string {
	switch alg.keySizeBits {
		case 128: return A128KW
		case 192: return A192KW
		default: return  A256KW
	}
}

func (alg *AesKW) WrapNewKey(cekSizeBits int, key interface{}, header map[string]interface{}) (cek []byte, encryptedCek []byte, err error) {	
	if kek,ok:=key.([]byte); ok {
		
		kekSizeBits := len(kek) << 3
		
		if kekSizeBits != alg.keySizeBits {
			return nil,nil, errors.New(fmt.Sprintf("AesKW.WrapNewKey(): expected key of size %v bits, but was given %v bits.",alg.keySizeBits, kekSizeBits))
		}	
		
		if cek,err = arrays.Random(cekSizeBits>>3);err==nil {			
			encryptedCek,err=aes.KeyWrap(cek,kek)
			return
		}
		
		return nil,nil,err

	}
		
	return nil,nil,errors.New("AesKW.WrapNewKey(): expected key to be '[]byte' array")	
}

func (alg *AesKW) Unwrap(encryptedCek []byte, key interface{}, cekSizeBits int, header map[string]interface{}) (cek []byte, err error) {
	
	if kek,ok:=key.([]byte); ok {
		
		kekSizeBits := len(kek) << 3
		
		if kekSizeBits != alg.keySizeBits {
			return nil,errors.New(fmt.Sprintf("AesKW.Unwrap(): expected key of size %v bits, but was given %v bits.", alg.keySizeBits, kekSizeBits))
		}	
		
		return aes.KeyUnwrap(encryptedCek, kek)
	}
		
	return nil,errors.New("AesKW.Unwrap(): expected key to be '[]byte' array")		
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruby_snowflake_client-1.3.7 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.6 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.5 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.4 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.4.pre.debug ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.3.pre.debug ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.2 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.1 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.3.0 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.2.1 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.2.0 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.1.1 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.1.0 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.0.2 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.0.1 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go
ruby_snowflake_client-1.0.0 ext/vendor/github.com/dvsekhvalnov/jose2go/aeskw.go